Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / unit-tests / t-example-decorate.c
blob8bf0709c41f6814ba6b12184c9e316cf972a5415
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "test-lib.h"
4 #include "object.h"
5 #include "decorate.h"
6 #include "repository.h"
8 struct test_vars {
9 struct object *one, *two, *three;
10 struct decoration n;
11 int decoration_a, decoration_b;
14 static void t_add(struct test_vars *vars)
16 void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a);
18 check(ret == NULL);
19 ret = add_decoration(&vars->n, vars->two, NULL);
20 check(ret == NULL);
23 static void t_readd(struct test_vars *vars)
25 void *ret = add_decoration(&vars->n, vars->one, NULL);
27 check(ret == &vars->decoration_a);
28 ret = add_decoration(&vars->n, vars->two, &vars->decoration_b);
29 check(ret == NULL);
32 static void t_lookup(struct test_vars *vars)
34 void *ret = lookup_decoration(&vars->n, vars->one);
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);
43 static void t_loop(struct test_vars *vars)
45 int i, objects_noticed = 0;
47 for (i = 0; i < vars->n.size; i++) {
48 if (vars->n.entries[i].base)
49 objects_noticed++;
51 check_int(objects_noticed, ==, 2);
54 int cmd_main(int argc UNUSED, const char **argv UNUSED)
56 struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
57 struct test_vars vars = { 0 };
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);
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.");
71 clear_decoration(&vars.n, NULL);
73 return test_done();