Git fork

Merge branch 'sk/unit-tests-0130'

Convert a handful of unit tests to work with the clar framework.

* sk/unit-tests-0130:
t/unit-tests: convert strcmp-offset test to use clar test framework
t/unit-tests: convert strbuf test to use clar test framework
t/unit-tests: adapt example decorate test to use clar test framework
t/unit-tests: convert hashmap test to use clar test framework

+348 -353
+4 -4
Makefile
··· 1342 1342 THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% 1343 1343 1344 1344 CLAR_TEST_SUITES += u-ctype 1345 + CLAR_TEST_SUITES += u-example-decorate 1345 1346 CLAR_TEST_SUITES += u-hash 1347 + CLAR_TEST_SUITES += u-hashmap 1346 1348 CLAR_TEST_SUITES += u-mem-pool 1347 1349 CLAR_TEST_SUITES += u-prio-queue 1348 1350 CLAR_TEST_SUITES += u-reftable-tree 1351 + CLAR_TEST_SUITES += u-strbuf 1352 + CLAR_TEST_SUITES += u-strcmp-offset 1349 1353 CLAR_TEST_SUITES += u-strvec 1350 1354 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) 1351 1355 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) 1352 1356 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o 1353 1357 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o 1354 1358 1355 - UNIT_TEST_PROGRAMS += t-example-decorate 1356 - UNIT_TEST_PROGRAMS += t-hashmap 1357 1359 UNIT_TEST_PROGRAMS += t-oid-array 1358 1360 UNIT_TEST_PROGRAMS += t-oidmap 1359 1361 UNIT_TEST_PROGRAMS += t-oidtree ··· 1365 1367 UNIT_TEST_PROGRAMS += t-reftable-readwrite 1366 1368 UNIT_TEST_PROGRAMS += t-reftable-record 1367 1369 UNIT_TEST_PROGRAMS += t-reftable-stack 1368 - UNIT_TEST_PROGRAMS += t-strbuf 1369 - UNIT_TEST_PROGRAMS += t-strcmp-offset 1370 1370 UNIT_TEST_PROGRAMS += t-trailer 1371 1371 UNIT_TEST_PROGRAMS += t-urlmatch-normalization 1372 1372 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
+4 -4
t/meson.build
··· 1 1 clar_test_suites = [ 2 2 'unit-tests/u-ctype.c', 3 + 'unit-tests/u-example-decorate.c', 3 4 'unit-tests/u-hash.c', 5 + 'unit-tests/u-hashmap.c', 4 6 'unit-tests/u-mem-pool.c', 5 7 'unit-tests/u-prio-queue.c', 6 8 'unit-tests/u-reftable-tree.c', 9 + 'unit-tests/u-strbuf.c', 10 + 'unit-tests/u-strcmp-offset.c', 7 11 'unit-tests/u-strvec.c', 8 12 ] 9 13 ··· 44 48 test('unit-tests', clar_unit_tests) 45 49 46 50 unit_test_programs = [ 47 - 'unit-tests/t-example-decorate.c', 48 - 'unit-tests/t-hashmap.c', 49 51 'unit-tests/t-oid-array.c', 50 52 'unit-tests/t-oidmap.c', 51 53 'unit-tests/t-oidtree.c', ··· 57 59 'unit-tests/t-reftable-readwrite.c', 58 60 'unit-tests/t-reftable-record.c', 59 61 'unit-tests/t-reftable-stack.c', 60 - 'unit-tests/t-strbuf.c', 61 - 'unit-tests/t-strcmp-offset.c', 62 62 'unit-tests/t-trailer.c', 63 63 'unit-tests/t-urlmatch-normalization.c', 64 64 ]
-74
t/unit-tests/t-example-decorate.c
··· 1 - #define USE_THE_REPOSITORY_VARIABLE 2 - 3 - #include "test-lib.h" 4 - #include "object.h" 5 - #include "decorate.h" 6 - #include "repository.h" 7 - 8 - struct test_vars { 9 - struct object *one, *two, *three; 10 - struct decoration n; 11 - int decoration_a, decoration_b; 12 - }; 13 - 14 - static void t_add(struct test_vars *vars) 15 - { 16 - void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a); 17 - 18 - check(ret == NULL); 19 - ret = add_decoration(&vars->n, vars->two, NULL); 20 - check(ret == NULL); 21 - } 22 - 23 - static void t_readd(struct test_vars *vars) 24 - { 25 - void *ret = add_decoration(&vars->n, vars->one, NULL); 26 - 27 - check(ret == &vars->decoration_a); 28 - ret = add_decoration(&vars->n, vars->two, &vars->decoration_b); 29 - check(ret == NULL); 30 - } 31 - 32 - static void t_lookup(struct test_vars *vars) 33 - { 34 - void *ret = lookup_decoration(&vars->n, vars->one); 35 - 36 - check(ret == NULL); 37 - ret = lookup_decoration(&vars->n, vars->two); 38 - check(ret == &vars->decoration_b); 39 - ret = lookup_decoration(&vars->n, vars->three); 40 - check(ret == NULL); 41 - } 42 - 43 - static void t_loop(struct test_vars *vars) 44 - { 45 - int objects_noticed = 0; 46 - 47 - for (size_t i = 0; i < vars->n.size; i++) { 48 - if (vars->n.entries[i].base) 49 - objects_noticed++; 50 - } 51 - check_int(objects_noticed, ==, 2); 52 - } 53 - 54 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 55 - { 56 - struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; 57 - struct test_vars vars = { 0 }; 58 - 59 - vars.one = lookup_unknown_object(the_repository, &one_oid); 60 - vars.two = lookup_unknown_object(the_repository, &two_oid); 61 - vars.three = lookup_unknown_object(the_repository, &three_oid); 62 - 63 - TEST(t_add(&vars), 64 - "Add 2 objects, one with a non-NULL decoration and one with a NULL decoration."); 65 - TEST(t_readd(&vars), 66 - "When re-adding an already existing object, the old decoration is returned."); 67 - TEST(t_lookup(&vars), 68 - "Lookup returns the added declarations, or NULL if the object was never added."); 69 - TEST(t_loop(&vars), "The user can also loop through all entries."); 70 - 71 - clear_decoration(&vars.n, NULL); 72 - 73 - return test_done(); 74 - }
+112 -114
t/unit-tests/t-hashmap.c t/unit-tests/u-hashmap.c
··· 1 - #include "test-lib.h" 1 + #include "unit-test.h" 2 2 #include "hashmap.h" 3 3 #include "strbuf.h" 4 4 ··· 83 83 struct test_entry *entry; 84 84 85 85 entry = alloc_test_entry("key1", "value1", ignore_case); 86 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 86 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 87 87 88 88 entry = alloc_test_entry(ignore_case ? "Key1" : "key1", "value2", 89 89 ignore_case); 90 90 entry = hashmap_put_entry(map, entry, ent); 91 - if (check(entry != NULL)) 92 - check_str(get_value(entry), "value1"); 91 + cl_assert(entry != NULL); 92 + cl_assert_equal_s(get_value(entry), "value1"); 93 93 free(entry); 94 94 95 95 entry = alloc_test_entry("fooBarFrotz", "value3", ignore_case); 96 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 96 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 97 97 98 98 entry = alloc_test_entry(ignore_case ? "FOObarFrotz" : "fooBarFrotz", 99 99 "value4", ignore_case); 100 100 entry = hashmap_put_entry(map, entry, ent); 101 - if (check(entry != NULL)) 102 - check_str(get_value(entry), "value3"); 101 + cl_assert(entry != NULL); 102 + cl_assert_equal_s(get_value(entry), "value3"); 103 103 free(entry); 104 104 } 105 105 ··· 122 122 for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { 123 123 entry = alloc_test_entry(key_val[i][0], key_val[i][1], 124 124 ignore_case); 125 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 125 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 126 126 } 127 127 128 128 for (size_t i = 0; i < ARRAY_SIZE(query); i++) { 129 129 entry = get_test_entry(map, query[i][0], ignore_case); 130 - if (check(entry != NULL)) 131 - check_str(get_value(entry), query[i][1]); 132 - else 133 - test_msg("query key: %s", query[i][0]); 130 + cl_assert(entry != NULL); 131 + cl_assert_equal_s(get_value(entry), query[i][1]); 134 132 } 135 133 136 - check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL); 137 - check_int(map->tablesize, ==, 64); 138 - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val)); 134 + cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL); 135 + cl_assert_equal_i(map->tablesize, 64); 136 + cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val)); 139 137 } 140 138 141 139 static void t_add(struct hashmap *map, unsigned int ignore_case) ··· 165 163 166 164 hashmap_for_each_entry_from(map, entry, ent) 167 165 { 168 - int ret; 169 - if (!check_int((ret = key_val_contains( 170 - key_val, seen, 171 - ARRAY_SIZE(key_val), entry)), 172 - ==, 0)) { 173 - switch (ret) { 174 - case 1: 175 - test_msg("found entry was not given in the input\n" 176 - " key: %s\n value: %s", 177 - entry->key, get_value(entry)); 178 - break; 179 - case 2: 180 - test_msg("duplicate entry detected\n" 181 - " key: %s\n value: %s", 182 - entry->key, get_value(entry)); 183 - break; 184 - } 185 - } else { 186 - count++; 187 - } 166 + int ret = key_val_contains(key_val, seen, 167 + ARRAY_SIZE(key_val), entry); 168 + cl_assert_equal_i(ret, 0); 169 + count++; 188 170 } 189 - check_int(count, ==, 2); 171 + cl_assert_equal_i(count, 2); 190 172 } 191 173 192 - for (size_t i = 0; i < ARRAY_SIZE(seen); i++) { 193 - if (!check_int(seen[i], ==, 1)) 194 - test_msg("following key-val pair was not iterated over:\n" 195 - " key: %s\n value: %s", 196 - key_val[i][0], key_val[i][1]); 197 - } 174 + for (size_t i = 0; i < ARRAY_SIZE(seen); i++) 175 + cl_assert_equal_i(seen[i], 1); 198 176 199 - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val)); 200 - check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL); 177 + cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val)); 178 + cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL); 201 179 } 202 180 203 181 static void t_remove(struct hashmap *map, unsigned int ignore_case) ··· 211 189 212 190 for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { 213 191 entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case); 214 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 192 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 215 193 } 216 194 217 195 for (size_t i = 0; i < ARRAY_SIZE(remove); i++) { 218 196 entry = alloc_test_entry(remove[i][0], "", ignore_case); 219 197 removed = hashmap_remove_entry(map, entry, ent, remove[i][0]); 220 - if (check(removed != NULL)) 221 - check_str(get_value(removed), remove[i][1]); 198 + cl_assert(removed != NULL); 199 + cl_assert_equal_s(get_value(removed), remove[i][1]); 222 200 free(entry); 223 201 free(removed); 224 202 } 225 203 226 204 entry = alloc_test_entry("notInMap", "", ignore_case); 227 - check_pointer_eq(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL); 205 + cl_assert_equal_p(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL); 228 206 free(entry); 229 207 230 - check_int(map->tablesize, ==, 64); 231 - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val) - ARRAY_SIZE(remove)); 208 + cl_assert_equal_i(map->tablesize, 64); 209 + cl_assert_equal_i(hashmap_get_size(map), 210 + ARRAY_SIZE(key_val) - ARRAY_SIZE(remove)); 232 211 } 233 212 234 213 static void t_iterate(struct hashmap *map, unsigned int ignore_case) ··· 242 221 243 222 for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { 244 223 entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case); 245 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 224 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 246 225 } 247 226 248 227 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) 249 228 { 250 - int ret; 251 - if (!check_int((ret = key_val_contains(key_val, seen, 252 - ARRAY_SIZE(key_val), 253 - entry)), ==, 0)) { 254 - switch (ret) { 255 - case 1: 256 - test_msg("found entry was not given in the input\n" 257 - " key: %s\n value: %s", 258 - entry->key, get_value(entry)); 259 - break; 260 - case 2: 261 - test_msg("duplicate entry detected\n" 262 - " key: %s\n value: %s", 263 - entry->key, get_value(entry)); 264 - break; 265 - } 266 - } 229 + int ret = key_val_contains(key_val, seen, 230 + ARRAY_SIZE(key_val), 231 + entry); 232 + cl_assert(ret == 0); 267 233 } 268 234 269 - for (size_t i = 0; i < ARRAY_SIZE(seen); i++) { 270 - if (!check_int(seen[i], ==, 1)) 271 - test_msg("following key-val pair was not iterated over:\n" 272 - " key: %s\n value: %s", 273 - key_val[i][0], key_val[i][1]); 274 - } 235 + for (size_t i = 0; i < ARRAY_SIZE(seen); i++) 236 + cl_assert_equal_i(seen[i], 1); 275 237 276 - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val)); 238 + cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val)); 277 239 } 278 240 279 241 static void t_alloc(struct hashmap *map, unsigned int ignore_case) ··· 284 246 char *key = xstrfmt("key%d", i); 285 247 char *value = xstrfmt("value%d", i); 286 248 entry = alloc_test_entry(key, value, ignore_case); 287 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 249 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 288 250 free(key); 289 251 free(value); 290 252 } 291 - check_int(map->tablesize, ==, 64); 292 - check_int(hashmap_get_size(map), ==, 51); 253 + cl_assert_equal_i(map->tablesize, 64); 254 + cl_assert_equal_i(hashmap_get_size(map), 51); 293 255 294 256 entry = alloc_test_entry("key52", "value52", ignore_case); 295 - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); 296 - check_int(map->tablesize, ==, 256); 297 - check_int(hashmap_get_size(map), ==, 52); 257 + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); 258 + cl_assert_equal_i(map->tablesize, 256); 259 + cl_assert_equal_i(hashmap_get_size(map), 52); 298 260 299 261 for (int i = 1; i <= 12; i++) { 300 262 char *key = xstrfmt("key%d", i); ··· 302 264 303 265 entry = alloc_test_entry(key, "", ignore_case); 304 266 removed = hashmap_remove_entry(map, entry, ent, key); 305 - if (check(removed != NULL)) 306 - check_str(value, get_value(removed)); 267 + cl_assert(removed != NULL); 268 + cl_assert_equal_s(value, get_value(removed)); 307 269 free(key); 308 270 free(value); 309 271 free(entry); 310 272 free(removed); 311 273 } 312 - check_int(map->tablesize, ==, 256); 313 - check_int(hashmap_get_size(map), ==, 40); 274 + cl_assert_equal_i(map->tablesize, 256); 275 + cl_assert_equal_i(hashmap_get_size(map), 40); 314 276 315 277 entry = alloc_test_entry("key40", "", ignore_case); 316 278 removed = hashmap_remove_entry(map, entry, ent, "key40"); 317 - if (check(removed != NULL)) 318 - check_str("value40", get_value(removed)); 319 - check_int(map->tablesize, ==, 64); 320 - check_int(hashmap_get_size(map), ==, 39); 279 + cl_assert(removed != NULL); 280 + cl_assert_equal_s("value40", get_value(removed)); 281 + cl_assert_equal_i(map->tablesize, 64); 282 + cl_assert_equal_i(hashmap_get_size(map), 39); 321 283 free(entry); 322 284 free(removed); 323 285 } 324 286 325 - static void t_intern(void) 287 + void test_hashmap__intern(void) 326 288 { 327 289 const char *values[] = { "value1", "Value1", "value2", "value2" }; 328 290 ··· 330 292 const char *i1 = strintern(values[i]); 331 293 const char *i2 = strintern(values[i]); 332 294 333 - if (!check(!strcmp(i1, values[i]))) 334 - test_msg("strintern(%s) returns %s\n", values[i], i1); 335 - else if (!check(i1 != values[i])) 336 - test_msg("strintern(%s) returns input pointer\n", 337 - values[i]); 338 - else if (!check_pointer_eq(i1, i2)) 339 - test_msg("address('%s') != address('%s'), so strintern('%s') != strintern('%s')", 340 - i1, i2, values[i], values[i]); 341 - else 342 - check_str(i1, values[i]); 295 + cl_assert_equal_s(i1, values[i]); 296 + cl_assert(i1 != values[i]); 297 + cl_assert_equal_p(i1, i2); 343 298 } 344 299 } 345 300 346 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 301 + void test_hashmap__replace_case_sensitive(void) 302 + { 303 + setup(t_replace, 0); 304 + } 305 + 306 + void test_hashmap__replace_case_insensitive(void) 307 + { 308 + setup(t_replace, 1); 309 + } 310 + 311 + void test_hashmap__get_case_sensitive(void) 312 + { 313 + setup(t_get, 0); 314 + } 315 + 316 + void test_hashmap__get_case_insensitive(void) 347 317 { 348 - TEST(setup(t_replace, 0), "replace works"); 349 - TEST(setup(t_replace, 1), "replace (case insensitive) works"); 350 - TEST(setup(t_get, 0), "get works"); 351 - TEST(setup(t_get, 1), "get (case insensitive) works"); 352 - TEST(setup(t_add, 0), "add works"); 353 - TEST(setup(t_add, 1), "add (case insensitive) works"); 354 - TEST(setup(t_remove, 0), "remove works"); 355 - TEST(setup(t_remove, 1), "remove (case insensitive) works"); 356 - TEST(setup(t_iterate, 0), "iterate works"); 357 - TEST(setup(t_iterate, 1), "iterate (case insensitive) works"); 358 - TEST(setup(t_alloc, 0), "grow / shrink works"); 359 - TEST(t_intern(), "string interning works"); 360 - return test_done(); 318 + setup(t_get, 1); 319 + } 320 + 321 + void test_hashmap__add_case_sensitive(void) 322 + { 323 + setup(t_add, 0); 324 + } 325 + 326 + void test_hashmap__add_case_insensitive(void) 327 + { 328 + setup(t_add, 1); 329 + } 330 + 331 + void test_hashmap__remove_case_sensitive(void) 332 + { 333 + setup(t_remove, 0); 334 + } 335 + 336 + void test_hashmap__remove_case_insensitive(void) 337 + { 338 + setup(t_remove, 1); 339 + } 340 + 341 + void test_hashmap__iterate_case_sensitive(void) 342 + { 343 + setup(t_iterate, 0); 344 + } 345 + 346 + void test_hashmap__iterate_case_insensitive(void) 347 + { 348 + setup(t_iterate, 1); 349 + } 350 + 351 + void test_hashmap__alloc_case_sensitive(void) 352 + { 353 + setup(t_alloc, 0); 354 + } 355 + 356 + void test_hashmap__alloc_case_insensitive(void) 357 + { 358 + setup(t_alloc, 1); 361 359 }
-122
t/unit-tests/t-strbuf.c
··· 1 - #include "test-lib.h" 2 - #include "strbuf.h" 3 - 4 - /* wrapper that supplies tests with an empty, initialized strbuf */ 5 - static void setup(void (*f)(struct strbuf*, const void*), 6 - const void *data) 7 - { 8 - struct strbuf buf = STRBUF_INIT; 9 - 10 - f(&buf, data); 11 - strbuf_release(&buf); 12 - check_uint(buf.len, ==, 0); 13 - check_uint(buf.alloc, ==, 0); 14 - } 15 - 16 - /* wrapper that supplies tests with a populated, initialized strbuf */ 17 - static void setup_populated(void (*f)(struct strbuf*, const void*), 18 - const char *init_str, const void *data) 19 - { 20 - struct strbuf buf = STRBUF_INIT; 21 - 22 - strbuf_addstr(&buf, init_str); 23 - check_uint(buf.len, ==, strlen(init_str)); 24 - f(&buf, data); 25 - strbuf_release(&buf); 26 - check_uint(buf.len, ==, 0); 27 - check_uint(buf.alloc, ==, 0); 28 - } 29 - 30 - static int assert_sane_strbuf(struct strbuf *buf) 31 - { 32 - /* Initialized strbufs should always have a non-NULL buffer */ 33 - if (!check(!!buf->buf)) 34 - return 0; 35 - /* Buffers should always be NUL-terminated */ 36 - if (!check_char(buf->buf[buf->len], ==, '\0')) 37 - return 0; 38 - /* 39 - * Freshly-initialized strbufs may not have a dynamically allocated 40 - * buffer 41 - */ 42 - if (buf->len == 0 && buf->alloc == 0) 43 - return 1; 44 - /* alloc must be at least one byte larger than len */ 45 - return check_uint(buf->len, <, buf->alloc); 46 - } 47 - 48 - static void t_static_init(void) 49 - { 50 - struct strbuf buf = STRBUF_INIT; 51 - 52 - check_uint(buf.len, ==, 0); 53 - check_uint(buf.alloc, ==, 0); 54 - check_char(buf.buf[0], ==, '\0'); 55 - } 56 - 57 - static void t_dynamic_init(void) 58 - { 59 - struct strbuf buf; 60 - 61 - strbuf_init(&buf, 1024); 62 - check(assert_sane_strbuf(&buf)); 63 - check_uint(buf.len, ==, 0); 64 - check_uint(buf.alloc, >=, 1024); 65 - check_char(buf.buf[0], ==, '\0'); 66 - strbuf_release(&buf); 67 - } 68 - 69 - static void t_addch(struct strbuf *buf, const void *data) 70 - { 71 - const char *p_ch = data; 72 - const char ch = *p_ch; 73 - size_t orig_alloc = buf->alloc; 74 - size_t orig_len = buf->len; 75 - 76 - if (!check(assert_sane_strbuf(buf))) 77 - return; 78 - strbuf_addch(buf, ch); 79 - if (!check(assert_sane_strbuf(buf))) 80 - return; 81 - if (!(check_uint(buf->len, ==, orig_len + 1) && 82 - check_uint(buf->alloc, >=, orig_alloc))) 83 - return; /* avoid de-referencing buf->buf */ 84 - check_char(buf->buf[buf->len - 1], ==, ch); 85 - check_char(buf->buf[buf->len], ==, '\0'); 86 - } 87 - 88 - static void t_addstr(struct strbuf *buf, const void *data) 89 - { 90 - const char *text = data; 91 - size_t len = strlen(text); 92 - size_t orig_alloc = buf->alloc; 93 - size_t orig_len = buf->len; 94 - 95 - if (!check(assert_sane_strbuf(buf))) 96 - return; 97 - strbuf_addstr(buf, text); 98 - if (!check(assert_sane_strbuf(buf))) 99 - return; 100 - if (!(check_uint(buf->len, ==, orig_len + len) && 101 - check_uint(buf->alloc, >=, orig_alloc) && 102 - check_uint(buf->alloc, >, orig_len + len) && 103 - check_char(buf->buf[orig_len + len], ==, '\0'))) 104 - return; 105 - check_str(buf->buf + orig_len, text); 106 - } 107 - 108 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 109 - { 110 - if (!TEST(t_static_init(), "static initialization works")) 111 - test_skip_all("STRBUF_INIT is broken"); 112 - TEST(t_dynamic_init(), "dynamic initialization works"); 113 - TEST(setup(t_addch, "a"), "strbuf_addch adds char"); 114 - TEST(setup(t_addch, ""), "strbuf_addch adds NUL char"); 115 - TEST(setup_populated(t_addch, "initial value", "a"), 116 - "strbuf_addch appends to initial value"); 117 - TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string"); 118 - TEST(setup_populated(t_addstr, "initial value", "hello there"), 119 - "strbuf_addstr appends string to initial value"); 120 - 121 - return test_done(); 122 - }
-35
t/unit-tests/t-strcmp-offset.c
··· 1 - #include "test-lib.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 - check_int(result, ==, expect_result); 19 - check_uint((uintmax_t)offset, ==, expect_offset); 20 - } 21 - 22 - #define TEST_STRCMP_OFFSET(string1, string2, expect_result, expect_offset) \ 23 - TEST(check_strcmp_offset(string1, string2, expect_result, \ 24 - expect_offset), \ 25 - "strcmp_offset(%s, %s) works", #string1, #string2) 26 - 27 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 28 - { 29 - TEST_STRCMP_OFFSET("abc", "abc", 0, 3); 30 - TEST_STRCMP_OFFSET("abc", "def", -1, 0); 31 - TEST_STRCMP_OFFSET("abc", "abz", -1, 2); 32 - TEST_STRCMP_OFFSET("abc", "abcdef", -1, 3); 33 - 34 - return test_done(); 35 - }
+64
t/unit-tests/u-example-decorate.c
··· 1 + #define USE_THE_REPOSITORY_VARIABLE 2 + 3 + #include "unit-test.h" 4 + #include "object.h" 5 + #include "decorate.h" 6 + #include "repository.h" 7 + 8 + struct test_vars { 9 + struct object *one, *two, *three; 10 + struct decoration n; 11 + int decoration_a, decoration_b; 12 + }; 13 + 14 + static struct test_vars vars; 15 + 16 + void test_example_decorate__initialize(void) 17 + { 18 + struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; 19 + 20 + vars.one = lookup_unknown_object(the_repository, &one_oid); 21 + vars.two = lookup_unknown_object(the_repository, &two_oid); 22 + vars.three = lookup_unknown_object(the_repository, &three_oid); 23 + } 24 + 25 + void test_example_decorate__cleanup(void) 26 + { 27 + clear_decoration(&vars.n, NULL); 28 + } 29 + 30 + void test_example_decorate__add(void) 31 + { 32 + cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL); 33 + cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL); 34 + } 35 + 36 + void test_example_decorate__readd(void) 37 + { 38 + cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL); 39 + cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL); 40 + cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), &vars.decoration_a); 41 + cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL); 42 + } 43 + 44 + void test_example_decorate__lookup(void) 45 + { 46 + cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL); 47 + cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), NULL); 48 + cl_assert_equal_p(lookup_decoration(&vars.n, vars.two), &vars.decoration_b); 49 + cl_assert_equal_p(lookup_decoration(&vars.n, vars.one), NULL); 50 + } 51 + 52 + void test_example_decorate__loop(void) 53 + { 54 + int objects_noticed = 0; 55 + 56 + cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL); 57 + cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL); 58 + 59 + for (size_t i = 0; i < vars.n.size; i++) 60 + if (vars.n.entries[i].base) 61 + objects_noticed++; 62 + 63 + cl_assert_equal_i(objects_noticed, 2); 64 + }
+119
t/unit-tests/u-strbuf.c
··· 1 + #include "unit-test.h" 2 + #include "strbuf.h" 3 + 4 + /* wrapper that supplies tests with an empty, initialized strbuf */ 5 + static void setup(void (*f)(struct strbuf*, const void*), 6 + const void *data) 7 + { 8 + struct strbuf buf = STRBUF_INIT; 9 + 10 + f(&buf, data); 11 + strbuf_release(&buf); 12 + cl_assert_equal_i(buf.len, 0); 13 + cl_assert_equal_i(buf.alloc, 0); 14 + } 15 + 16 + /* wrapper that supplies tests with a populated, initialized strbuf */ 17 + static void setup_populated(void (*f)(struct strbuf*, const void*), 18 + const char *init_str, const void *data) 19 + { 20 + struct strbuf buf = STRBUF_INIT; 21 + 22 + strbuf_addstr(&buf, init_str); 23 + cl_assert_equal_i(buf.len, strlen(init_str)); 24 + f(&buf, data); 25 + strbuf_release(&buf); 26 + cl_assert_equal_i(buf.len, 0); 27 + cl_assert_equal_i(buf.alloc, 0); 28 + } 29 + 30 + static void assert_sane_strbuf(struct strbuf *buf) 31 + { 32 + /* Initialized strbufs should always have a non-NULL buffer */ 33 + cl_assert(buf->buf != NULL); 34 + /* Buffers should always be NUL-terminated */ 35 + cl_assert(buf->buf[buf->len] == '\0'); 36 + /* 37 + * In case the buffer contains anything, `alloc` must alloc must 38 + * be at least one byte larger than `len`. 39 + */ 40 + if (buf->len) 41 + cl_assert(buf->len < buf->alloc); 42 + } 43 + 44 + void test_strbuf__static_init(void) 45 + { 46 + struct strbuf buf = STRBUF_INIT; 47 + 48 + cl_assert_equal_i(buf.len, 0); 49 + cl_assert_equal_i(buf.alloc, 0); 50 + cl_assert(buf.buf[0] == '\0'); 51 + } 52 + 53 + void test_strbuf__dynamic_init(void) 54 + { 55 + struct strbuf buf; 56 + 57 + strbuf_init(&buf, 1024); 58 + assert_sane_strbuf(&buf); 59 + cl_assert_equal_i(buf.len, 0); 60 + cl_assert(buf.alloc >= 1024); 61 + cl_assert(buf.buf[0] == '\0'); 62 + strbuf_release(&buf); 63 + } 64 + 65 + static void t_addch(struct strbuf *buf, const void *data) 66 + { 67 + const char *p_ch = data; 68 + const char ch = *p_ch; 69 + size_t orig_alloc = buf->alloc; 70 + size_t orig_len = buf->len; 71 + 72 + assert_sane_strbuf(buf); 73 + strbuf_addch(buf, ch); 74 + assert_sane_strbuf(buf); 75 + cl_assert_equal_i(buf->len, orig_len + 1); 76 + cl_assert(buf->alloc >= orig_alloc); 77 + cl_assert(buf->buf[buf->len] == '\0'); 78 + } 79 + 80 + static void t_addstr(struct strbuf *buf, const void *data) 81 + { 82 + const char *text = data; 83 + size_t len = strlen(text); 84 + size_t orig_alloc = buf->alloc; 85 + size_t orig_len = buf->len; 86 + 87 + assert_sane_strbuf(buf); 88 + strbuf_addstr(buf, text); 89 + assert_sane_strbuf(buf); 90 + cl_assert_equal_i(buf->len, orig_len + len); 91 + cl_assert(buf->alloc >= orig_alloc); 92 + cl_assert(buf->buf[buf->len] == '\0'); 93 + cl_assert_equal_s(buf->buf + orig_len, text); 94 + } 95 + 96 + void test_strbuf__add_single_char(void) 97 + { 98 + setup(t_addch, "a"); 99 + } 100 + 101 + void test_strbuf__add_empty_char(void) 102 + { 103 + setup(t_addch, ""); 104 + } 105 + 106 + void test_strbuf__add_append_char(void) 107 + { 108 + setup_populated(t_addch, "initial value", "a"); 109 + } 110 + 111 + void test_strbuf__add_single_str(void) 112 + { 113 + setup(t_addstr, "hello there"); 114 + } 115 + 116 + void test_strbuf__add_append_str(void) 117 + { 118 + setup_populated(t_addstr, "initial value", "hello there"); 119 + }
+45
t/unit-tests/u-strcmp-offset.c
··· 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 + }