]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-example-decorate.c
Merge branch 'cw/bisect-replay-with-dos'
[thirdparty/git.git] / t / helper / test-example-decorate.c
CommitLineData
dbceb3ec 1#include "test-tool.h"
ddd3e312
JT
2#include "cache.h"
3#include "object.h"
4#include "decorate.h"
5
dbceb3ec 6int cmd__example_decorate(int argc, const char **argv)
ddd3e312
JT
7{
8 struct decoration n;
9 struct object_id one_oid = { {1} };
10 struct object_id two_oid = { {2} };
11 struct object_id three_oid = { {3} };
12 struct object *one, *two, *three;
13
14 int decoration_a, decoration_b;
15
16 void *ret;
17
18 int i, objects_noticed = 0;
19
20 /*
21 * The struct must be zero-initialized.
22 */
23 memset(&n, 0, sizeof(n));
24
25 /*
26 * Add 2 objects, one with a non-NULL decoration and one with a NULL
27 * decoration.
28 */
0ebbcf70
JK
29 one = lookup_unknown_object(&one_oid);
30 two = lookup_unknown_object(&two_oid);
ddd3e312
JT
31 ret = add_decoration(&n, one, &decoration_a);
32 if (ret)
033abf97 33 BUG("when adding a brand-new object, NULL should be returned");
ddd3e312
JT
34 ret = add_decoration(&n, two, NULL);
35 if (ret)
033abf97 36 BUG("when adding a brand-new object, NULL should be returned");
ddd3e312
JT
37
38 /*
39 * When re-adding an already existing object, the old decoration is
40 * returned.
41 */
42 ret = add_decoration(&n, one, NULL);
43 if (ret != &decoration_a)
033abf97 44 BUG("when readding an already existing object, existing decoration should be returned");
ddd3e312
JT
45 ret = add_decoration(&n, two, &decoration_b);
46 if (ret)
033abf97 47 BUG("when readding an already existing object, existing decoration should be returned");
ddd3e312
JT
48
49 /*
50 * Lookup returns the added declarations, or NULL if the object was
51 * never added.
52 */
53 ret = lookup_decoration(&n, one);
54 if (ret)
033abf97 55 BUG("lookup should return added declaration");
ddd3e312
JT
56 ret = lookup_decoration(&n, two);
57 if (ret != &decoration_b)
033abf97 58 BUG("lookup should return added declaration");
0ebbcf70 59 three = lookup_unknown_object(&three_oid);
ddd3e312
JT
60 ret = lookup_decoration(&n, three);
61 if (ret)
033abf97 62 BUG("lookup for unknown object should return NULL");
ddd3e312
JT
63
64 /*
65 * The user can also loop through all entries.
66 */
67 for (i = 0; i < n.size; i++) {
68 if (n.entries[i].base)
69 objects_noticed++;
70 }
71 if (objects_noticed != 2)
033abf97 72 BUG("should have 2 objects");
ddd3e312
JT
73
74 return 0;
75}