diff --git a/Makefile b/Makefile index 4e255c81f22386..bd5ab467530f00 100644 --- a/Makefile +++ b/Makefile @@ -1347,6 +1347,7 @@ UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-ctype UNIT_TEST_PROGRAMS += t-prio-queue +UNIT_TEST_PROGRAMS += t-strcmp-offset UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o diff --git a/t/unit-tests/t-strcmp-offset.c b/t/unit-tests/t-strcmp-offset.c new file mode 100644 index 00000000000000..83f3e5a7d6cc4f --- /dev/null +++ b/t/unit-tests/t-strcmp-offset.c @@ -0,0 +1,27 @@ +#include "test-lib.h" +#include "read-cache-ll.h" + +static void check_strcmp_offset(const char *string1, const char *string2, int expect_result, uintmax_t expect_offset) +{ + int result; + size_t offset; + + result = strcmp_offset(string1, string2, &offset); + + /* Because different CRTs behave differently, only rely on signs of the result values. */ + result = (result < 0 ? -1 : + result > 0 ? 1 : + 0); + + check_int(result, ==, expect_result); + check_uint((uintmax_t)offset, ==, expect_offset); +} + +int cmd_main(int argc, const char **argv) { + TEST(check_strcmp_offset("abc", "abc", 0, 3), "strcmp_offset works"); + TEST(check_strcmp_offset("abc", "def", -1, 0), "strcmp_offset works"); + TEST(check_strcmp_offset("abc", "abz", -1, 2), "strcmp_offset works"); + TEST(check_strcmp_offset("abc", "abcdef", -1, 3), "strcmp_offset works"); + + return test_done(); +}