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