Git fork

t/: migrate helper/test-example-decorate to the unit testing framework

helper/test-example-decorate.c along with t9004-example.sh provide
an example of how to use the functions in decorate.h (which provides
a data structure that associates Git objects to void pointers) and
also test their output.

Migrate them to the new unit testing framework for better debugging
and runtime performance.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Ghanshyam Thakkar and committed by
Junio C Hamano
456b4dce b7a1d47b

+82 -94
+1 -1
Makefile
··· 793 TEST_BUILTINS_OBJS += test-dump-split-index.o 794 TEST_BUILTINS_OBJS += test-dump-untracked-cache.o 795 TEST_BUILTINS_OBJS += test-env-helper.o 796 - TEST_BUILTINS_OBJS += test-example-decorate.o 797 TEST_BUILTINS_OBJS += test-example-tap.o 798 TEST_BUILTINS_OBJS += test-find-pack.o 799 TEST_BUILTINS_OBJS += test-fsmonitor-client.o ··· 1334 THIRD_PARTY_SOURCES += sha1collisiondetection/% 1335 THIRD_PARTY_SOURCES += sha1dc/% 1336 1337 UNIT_TEST_PROGRAMS += t-mem-pool 1338 UNIT_TEST_PROGRAMS += t-strbuf 1339 UNIT_TEST_PROGRAMS += t-ctype
··· 793 TEST_BUILTINS_OBJS += test-dump-split-index.o 794 TEST_BUILTINS_OBJS += test-dump-untracked-cache.o 795 TEST_BUILTINS_OBJS += test-env-helper.o 796 TEST_BUILTINS_OBJS += test-example-tap.o 797 TEST_BUILTINS_OBJS += test-find-pack.o 798 TEST_BUILTINS_OBJS += test-fsmonitor-client.o ··· 1333 THIRD_PARTY_SOURCES += sha1collisiondetection/% 1334 THIRD_PARTY_SOURCES += sha1dc/% 1335 1336 + UNIT_TEST_PROGRAMS += t-example-decorate 1337 UNIT_TEST_PROGRAMS += t-mem-pool 1338 UNIT_TEST_PROGRAMS += t-strbuf 1339 UNIT_TEST_PROGRAMS += t-ctype
+1 -1
decorate.h
··· 3 4 /* 5 * A data structure that associates Git objects to void pointers. See 6 - * t/helper/test-example-decorate.c for a demonstration of how to use these 7 * functions. 8 */ 9
··· 3 4 /* 5 * A data structure that associates Git objects to void pointers. See 6 + * t/unit-tests/t-example-decorate.c for a demonstration of how to use these 7 * functions. 8 */ 9
-78
t/helper/test-example-decorate.c
··· 1 - #include "test-tool.h" 2 - #include "git-compat-util.h" 3 - #include "object.h" 4 - #include "decorate.h" 5 - #include "repository.h" 6 - 7 - int cmd__example_decorate(int argc UNUSED, const char **argv UNUSED) 8 - { 9 - struct decoration n; 10 - struct object_id one_oid = { {1} }; 11 - struct object_id two_oid = { {2} }; 12 - struct object_id three_oid = { {3} }; 13 - struct object *one, *two, *three; 14 - 15 - int decoration_a, decoration_b; 16 - 17 - void *ret; 18 - 19 - int i, objects_noticed = 0; 20 - 21 - /* 22 - * The struct must be zero-initialized. 23 - */ 24 - memset(&n, 0, sizeof(n)); 25 - 26 - /* 27 - * Add 2 objects, one with a non-NULL decoration and one with a NULL 28 - * decoration. 29 - */ 30 - one = lookup_unknown_object(the_repository, &one_oid); 31 - two = lookup_unknown_object(the_repository, &two_oid); 32 - ret = add_decoration(&n, one, &decoration_a); 33 - if (ret) 34 - BUG("when adding a brand-new object, NULL should be returned"); 35 - ret = add_decoration(&n, two, NULL); 36 - if (ret) 37 - BUG("when adding a brand-new object, NULL should be returned"); 38 - 39 - /* 40 - * When re-adding an already existing object, the old decoration is 41 - * returned. 42 - */ 43 - ret = add_decoration(&n, one, NULL); 44 - if (ret != &decoration_a) 45 - BUG("when readding an already existing object, existing decoration should be returned"); 46 - ret = add_decoration(&n, two, &decoration_b); 47 - if (ret) 48 - BUG("when readding an already existing object, existing decoration should be returned"); 49 - 50 - /* 51 - * Lookup returns the added declarations, or NULL if the object was 52 - * never added. 53 - */ 54 - ret = lookup_decoration(&n, one); 55 - if (ret) 56 - BUG("lookup should return added declaration"); 57 - ret = lookup_decoration(&n, two); 58 - if (ret != &decoration_b) 59 - BUG("lookup should return added declaration"); 60 - three = lookup_unknown_object(the_repository, &three_oid); 61 - ret = lookup_decoration(&n, three); 62 - if (ret) 63 - BUG("lookup for unknown object should return NULL"); 64 - 65 - /* 66 - * The user can also loop through all entries. 67 - */ 68 - for (i = 0; i < n.size; i++) { 69 - if (n.entries[i].base) 70 - objects_noticed++; 71 - } 72 - if (objects_noticed != 2) 73 - BUG("should have 2 objects"); 74 - 75 - clear_decoration(&n, NULL); 76 - 77 - return 0; 78 - }
···
-1
t/helper/test-tool.c
··· 29 { "dump-split-index", cmd__dump_split_index }, 30 { "dump-untracked-cache", cmd__dump_untracked_cache }, 31 { "env-helper", cmd__env_helper }, 32 - { "example-decorate", cmd__example_decorate }, 33 { "example-tap", cmd__example_tap }, 34 { "find-pack", cmd__find_pack }, 35 { "fsmonitor-client", cmd__fsmonitor_client },
··· 29 { "dump-split-index", cmd__dump_split_index }, 30 { "dump-untracked-cache", cmd__dump_untracked_cache }, 31 { "env-helper", cmd__env_helper }, 32 { "example-tap", cmd__example_tap }, 33 { "find-pack", cmd__find_pack }, 34 { "fsmonitor-client", cmd__fsmonitor_client },
-1
t/helper/test-tool.h
··· 23 int cmd__dump_untracked_cache(int argc, const char **argv); 24 int cmd__dump_reftable(int argc, const char **argv); 25 int cmd__env_helper(int argc, const char **argv); 26 - int cmd__example_decorate(int argc, const char **argv); 27 int cmd__example_tap(int argc, const char **argv); 28 int cmd__find_pack(int argc, const char **argv); 29 int cmd__fsmonitor_client(int argc, const char **argv);
··· 23 int cmd__dump_untracked_cache(int argc, const char **argv); 24 int cmd__dump_reftable(int argc, const char **argv); 25 int cmd__env_helper(int argc, const char **argv); 26 int cmd__example_tap(int argc, const char **argv); 27 int cmd__find_pack(int argc, const char **argv); 28 int cmd__fsmonitor_client(int argc, const char **argv);
-12
t/t9004-example.sh
··· 1 - #!/bin/sh 2 - 3 - test_description='check that example code compiles and runs' 4 - 5 - TEST_PASSES_SANITIZE_LEAK=true 6 - . ./test-lib.sh 7 - 8 - test_expect_success 'decorate' ' 9 - test-tool example-decorate 10 - ' 11 - 12 - test_done
···
+80
t/unit-tests/t-example-decorate.c
···
··· 1 + #include "test-lib.h" 2 + #include "object.h" 3 + #include "decorate.h" 4 + #include "repository.h" 5 + 6 + struct test_vars { 7 + struct object *one, *two, *three; 8 + struct decoration n; 9 + int decoration_a, decoration_b; 10 + }; 11 + 12 + static void t_add(struct test_vars *vars) 13 + { 14 + void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a); 15 + 16 + if (!check(ret == NULL)) 17 + test_msg("when adding a brand-new object, NULL should be returned"); 18 + ret = add_decoration(&vars->n, vars->two, NULL); 19 + if (!check(ret == NULL)) 20 + test_msg("when adding a brand-new object, NULL should be returned"); 21 + } 22 + 23 + static void t_readd(struct test_vars *vars) 24 + { 25 + void *ret = add_decoration(&vars->n, vars->one, NULL); 26 + 27 + if (!check(ret == &vars->decoration_a)) 28 + test_msg("when readding an already existing object, existing decoration should be returned"); 29 + ret = add_decoration(&vars->n, vars->two, &vars->decoration_b); 30 + if (!check(ret == NULL)) 31 + test_msg("when readding an already existing object, existing decoration should be returned"); 32 + } 33 + 34 + static void t_lookup(struct test_vars *vars) 35 + { 36 + void *ret = lookup_decoration(&vars->n, vars->one); 37 + 38 + if (!check(ret == NULL)) 39 + test_msg("lookup should return added declaration"); 40 + ret = lookup_decoration(&vars->n, vars->two); 41 + if (!check(ret == &vars->decoration_b)) 42 + test_msg("lookup should return added declaration"); 43 + ret = lookup_decoration(&vars->n, vars->three); 44 + if (!check(ret == NULL)) 45 + test_msg("lookup for unknown object should return NULL"); 46 + } 47 + 48 + static void t_loop(struct test_vars *vars) 49 + { 50 + int i, objects_noticed = 0; 51 + 52 + for (i = 0; i < vars->n.size; i++) { 53 + if (vars->n.entries[i].base) 54 + objects_noticed++; 55 + } 56 + if (!check_int(objects_noticed, ==, 2)) 57 + test_msg("should have 2 objects"); 58 + } 59 + 60 + int cmd_main(int argc UNUSED, const char **argv UNUSED) 61 + { 62 + struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; 63 + struct test_vars vars = { 0 }; 64 + 65 + vars.one = lookup_unknown_object(the_repository, &one_oid); 66 + vars.two = lookup_unknown_object(the_repository, &two_oid); 67 + vars.three = lookup_unknown_object(the_repository, &three_oid); 68 + 69 + TEST(t_add(&vars), 70 + "Add 2 objects, one with a non-NULL decoration and one with a NULL decoration."); 71 + TEST(t_readd(&vars), 72 + "When re-adding an already existing object, the old decoration is returned."); 73 + TEST(t_lookup(&vars), 74 + "Lookup returns the added declarations, or NULL if the object was never added."); 75 + TEST(t_loop(&vars), "The user can also loop through all entries."); 76 + 77 + clear_decoration(&vars.n, NULL); 78 + 79 + return test_done(); 80 + }