X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=tree234.c;h=4b3151ee2f65c5d3793be15a43623d162511b313;hb=3aa4516a680f52e2e6c8a16234a7ecca0ec77233;hp=ed54dbb6f445e8c5ce2764c4c61a32e09270472d;hpb=2bef4dfb50ec69cff1bc0569b76a7eb10115f576;p=sgt-puzzles.git diff --git a/tree234.c b/tree234.c index ed54dbb..4b3151e 100644 --- a/tree234.c +++ b/tree234.c @@ -35,6 +35,9 @@ #ifdef TEST #define LOG(x) (printf x) +#define smalloc malloc +#define srealloc realloc +#define sfree free #else #define LOG(x) #endif @@ -1157,7 +1160,7 @@ tree234 *join234r(tree234 *t1, tree234 *t2) { * in t. */ static node234 *split234_internal(tree234 *t, int index) { - node234 *halves[2], *n, *sib, *sub; + node234 *halves[2] = { NULL, NULL }, *n, *sib, *sub; node234 *lparent, *rparent; int ki, pki, i, half, lcount, rcount; @@ -1179,6 +1182,7 @@ static node234 *split234_internal(tree234 *t, int index) { /* * Search down the tree to find the split point. */ + halves[0] = halves[1] = NULL; lparent = rparent = NULL; pki = -1; while (n) { @@ -1270,6 +1274,8 @@ static node234 *split234_internal(tree234 *t, int index) { */ LOG((" fell off bottom, lroot is %p, rroot is %p\n", halves[0], halves[1])); + assert(halves[0] != NULL); + assert(halves[1] != NULL); lparent->counts[pki] = rparent->counts[0] = 0; lparent->kids[pki] = rparent->kids[0] = NULL; @@ -1438,8 +1444,11 @@ tree234 *copytree234(tree234 *t, copyfn234 copyfn, void *copyfnstate) { tree234 *t2; t2 = newtree234(t->cmp); - t2->root = copynode234(t->root, copyfn, copyfnstate); - t2->root->parent = NULL; + if (t->root) { + t2->root = copynode234(t->root, copyfn, copyfnstate); + t2->root->parent = NULL; + } else + t2->root = NULL; return t2; } @@ -1469,6 +1478,7 @@ tree234 *copytree234(tree234 *t, copyfn234 copyfn, void *copyfnstate) { * if not.) */ +#include #include #define srealloc realloc @@ -1885,8 +1895,6 @@ int mycmp(void *av, void *bv) { return strcmp(a, b); } -#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) - char *strings[] = { "0", "2", "3", "I", "K", "d", "H", "J", "Q", "N", "n", "q", "j", "i", "7", "G", "F", "D", "b", "x", "g", "B", "e", "v", "V", "T", "f", "E", @@ -2120,11 +2128,12 @@ int main(void) { tree = newtree234(mycmp); cmp = mycmp; arraylen = 0; - for (i = 0; i < 16; i++) { - addtest(strings[i]); + for (i = 0; i < 17; i++) { tree2 = copytree234(tree, NULL, NULL); splittest(tree2, array, arraylen); freetree234(tree2); + if (i < 16) + addtest(strings[i]); } freetree234(tree);