Skip to content

Commit af8bf67

Browse files
Seyi007gitster
authored andcommitted
t/unit-tests: convert strcmp-offset test to use clar test framework
Adapt strcmp-offset test script to clar framework by using clar assertions where necessary. Introduce `test_strcmp_offset__empty()` to verify `check_strcmp_offset()` behavior when both input strings are empty. This ensures the function correctly handles edge cases and returns expected values. Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b99546 commit af8bf67

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,7 @@ CLAR_TEST_SUITES += u-mem-pool
13471347
CLAR_TEST_SUITES += u-prio-queue
13481348
CLAR_TEST_SUITES += u-reftable-tree
13491349
CLAR_TEST_SUITES += u-strbuf
1350+
CLAR_TEST_SUITES += u-strcmp-offset
13501351
CLAR_TEST_SUITES += u-strvec
13511352
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13521353
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1364,7 +1365,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
13641365
UNIT_TEST_PROGRAMS += t-reftable-readwrite
13651366
UNIT_TEST_PROGRAMS += t-reftable-record
13661367
UNIT_TEST_PROGRAMS += t-reftable-stack
1367-
UNIT_TEST_PROGRAMS += t-strcmp-offset
13681368
UNIT_TEST_PROGRAMS += t-trailer
13691369
UNIT_TEST_PROGRAMS += t-urlmatch-normalization
13701370
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ clar_test_suites = [
77
'unit-tests/u-prio-queue.c',
88
'unit-tests/u-reftable-tree.c',
99
'unit-tests/u-strbuf.c',
10+
'unit-tests/u-strcmp-offset.c',
1011
'unit-tests/u-strvec.c',
1112
]
1213

@@ -58,7 +59,6 @@ unit_test_programs = [
5859
'unit-tests/t-reftable-readwrite.c',
5960
'unit-tests/t-reftable-record.c',
6061
'unit-tests/t-reftable-stack.c',
61-
'unit-tests/t-strcmp-offset.c',
6262
'unit-tests/t-trailer.c',
6363
'unit-tests/t-urlmatch-normalization.c',
6464
]

t/unit-tests/t-strcmp-offset.c

Lines changed: 0 additions & 35 deletions
This file was deleted.

t/unit-tests/u-strcmp-offset.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "unit-test.h"
2+
#include "read-cache-ll.h"
3+
4+
static void check_strcmp_offset(const char *string1, const char *string2,
5+
int expect_result, uintmax_t expect_offset)
6+
{
7+
size_t offset;
8+
int result = strcmp_offset(string1, string2, &offset);
9+
10+
/*
11+
* Because different CRTs behave differently, only rely on signs of the
12+
* result values.
13+
*/
14+
result = (result < 0 ? -1 :
15+
result > 0 ? 1 :
16+
0);
17+
18+
cl_assert_equal_i(result, expect_result);
19+
cl_assert_equal_i((uintmax_t)offset, expect_offset);
20+
}
21+
22+
void test_strcmp_offset__empty(void)
23+
{
24+
check_strcmp_offset("", "", 0, 0);
25+
}
26+
27+
void test_strcmp_offset__equal(void)
28+
{
29+
check_strcmp_offset("abc", "abc", 0, 3);
30+
}
31+
32+
void test_strcmp_offset__different(void)
33+
{
34+
check_strcmp_offset("abc", "def", -1, 0);
35+
}
36+
37+
void test_strcmp_offset__mismatch(void)
38+
{
39+
check_strcmp_offset("abc", "abz", -1, 2);
40+
}
41+
42+
void test_strcmp_offset__different_length(void)
43+
{
44+
check_strcmp_offset("abc", "abcdef", -1, 3);
45+
}

0 commit comments

Comments
 (0)