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