]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-example-decorate.c
Merge branch 'eb/hash-transition'
[thirdparty/git.git] / 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 }