|
| 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 | +} |
0 commit comments