void prefix_increment_t(void) { struct prefix px; sput_fail_unless(prefix_increment(&px, &p1, 8) == 0, "Ret is 0"); sput_fail_if(prefix_cmp(&px, &p2), "px == p2"); sput_fail_unless(prefix_increment(&px, &p10, 8) == 0, "Ret is 0"); sput_fail_if(prefix_cmp(&px, &p11), "px == p11"); sput_fail_unless(prefix_increment(&px, &p10, 16) == 1, "Single possibility"); sput_fail_if(prefix_cmp(&px, &p10), "px == p10"); sput_fail_unless(prefix_increment(&px, &p1f, 12) == 1, "Looping"); sput_fail_if(prefix_cmp(&px, &p10), "px == p10"); sput_fail_unless(prefix_increment(&px, &p1f, 120) == -1, "Forbidden"); px.plen = 120; sput_fail_unless(prefix_increment(&px, &px, 4) == -1, "Forbidden"); px.plen = 42; sput_fail_unless(prefix_increment(&px, &px, 10) == 0, "32 bits authorized"); px.plen = 43; sput_fail_unless(prefix_increment(&px, &px, 10) == -1, "33 bits forbidden"); }
uint8_t append_tree(int16_t value, uint16_t depth){ static uint8_t prefix[PREFIX_SIZE_B]; static uint8_t cdepth=0; node_t* current=tree; int8_t i,t; for(;cdepth<depth;++cdepth){ prefix_shiftleft(prefix); } for(i=depth-1; i>=0; --i){ t=(prefix[i/8])&(1<<(i%8)); if(t==0){ if(current->left==NULL){ // current->left=calloc(1, sizeof(node_t)); current->left=&(tree[tree_index++]); ((node_t*)(current->left))->value = V_NODE; } current = current->left; } else { if(current->right==NULL){ // current->right=calloc(1, sizeof(node_t)); current->right=&(tree[tree_index++]); ((node_t*)(current->right))->value = V_NODE; } current = current->right; } } if(current==NULL) ALLOC_ERROR current->value=value; prefix_increment(prefix); return prefix[depth/8]&(1<<(depth%8)); }
uint8_t append_tree(int16_t value, uint16_t depth){ static uint8_t prefix[PREFIX_SIZE_B]; static uint8_t cdepth=0; node_t* current=tree; int8_t i,t; for(;cdepth<depth;++cdepth){ prefix_shiftleft(prefix); } #if DEBUG printf("\n %2.2X (%c) => ", value, (value>32&&value<128)?(char)value:' '); #endif for(i=depth-1; i>=0; --i){ t=(prefix[i/8])&(1<<(i%8)); if(t==0){ #if DEBUG putchar('0'); #endif if(current->left==NULL){ // current->left=calloc(1, sizeof(node_t)); current->left=&(tree[tree_index++]); ((node_t*)(current->left))->value = V_NODE; } current = current->left; } else { #if DEBUG putchar('1'); #endif if(current->right==NULL){ // current->right=calloc(1, sizeof(node_t)); current->right=&(tree[tree_index++]); ((node_t*)(current->right))->value = V_NODE; } current = current->right; } } #if DEBUG puts(""); #endif if(current==NULL) ALLOC_ERROR current->value=value; prefix_increment(prefix); return prefix[depth/8]&(1<<(depth%8)); }