tal: make tal_len/tal_count(NULL) return 0.
[ccan] / ccan / tal / test / run-count.c
index 0b64b887c94a47ea2b099d332c313bde81e5ba0f..33049b9f78d3e2edc9e675ed2afc7450d9b9597d 100644 (file)
@@ -2,32 +2,91 @@
 #include <ccan/tal/tal.c>
 #include <ccan/tap/tap.h>
 
+static bool move;
+#define ALIGN (sizeof(void *)*2)
+
+static void *my_alloc(size_t len)
+{
+       char *ret = malloc(len + ALIGN);
+       memcpy(ret, &len, sizeof(len));
+       return ret + ALIGN;
+}
+
+static void my_free(void *p)
+{
+       if (p)
+               free((char *)p - ALIGN);
+}
+
+static void *my_realloc(void *old, size_t new_size)
+{
+       char *ret;
+
+       /* Test what happens if we always move */
+       if (move) {
+               size_t old_size = *(size_t *)((char *)old - ALIGN);
+               ret = my_alloc(new_size);
+               memcpy(ret, old, old_size > new_size ? new_size : old_size);
+               my_free(old);
+       } else {
+               ret = realloc((char *)old - ALIGN, new_size + ALIGN);
+               memcpy(ret, &new_size, sizeof(new_size));
+               ret += ALIGN;
+       }
+       return ret;
+}
+
 int main(void)
 {
        char *p1, *p2;
+       unsigned int i;
+
+       tal_set_backend(my_alloc, my_realloc, my_free, NULL);
 
-       plan_tests(12);
+       plan_tests(2 + 19 * 3);
 
-       p1 = tal(NULL, char);
-       ok1(p1);
+       p1 = NULL;
+       ok1(tal_len(p1) == 0);
        ok1(tal_count(p1) == 0);
 
-       p2 = tal_arr(p1, char, 1);
-       ok1(p2);
-       ok1(tal_count(p2) == 1);
-       ok1(tal_resize(&p2, 2));
-       ok1(tal_count(p2) == 2);
-       ok1(tal_check(NULL, NULL));
-       tal_free(p2);
-
-       p2 = tal_arrz(p1, char, 7);
-       ok1(p2);
-       ok1(tal_count(p2) == 7);
-       ok1(tal_resize(&p2, 0));
-       ok1(tal_count(p2) == 0);
-       ok1(tal_check(NULL, NULL));
-       tal_free(p2);
-       tal_free(p1);
+       for (i = 0; i < 3; i++) {
+               move = i;
+
+               p1 = tal(NULL, char);
+               ok1(p1);
+               ok1(tal_count(p1) == 0);
+
+               p2 = tal_arr(p1, char, 1);
+               ok1(p2);
+               ok1(tal_count(p2) == 1);
+               ok1(tal_resize(&p2, 2));
+               ok1(tal_count(p2) == 2);
+               ok1(tal_check(NULL, NULL));
+               tal_free(p2);
+
+               /* Resize twice. */
+               p2 = tal_arrz(p1, char, 7);
+               ok1(p2);
+               ok1(tal_count(p2) == 7);
+               ok1(tal_check(NULL, NULL));
+               tal_resize(&p2, 20);
+               ok1(p2);
+               ok1(tal_check(NULL, NULL));
+               ok1(tal_count(p2) == 20);
+               /* Tickles non-moving logic, as we do not update bounds. */
+               if (i == 2)
+                       move = false;
+               tal_resize(&p2, 300);
+               ok1(p2);
+               ok1(tal_check(NULL, NULL));
+               ok1(tal_count(p2) == 300);
+               ok1(tal_resize(&p2, 0));
+               ok1(tal_count(p2) == 0);
+               ok1(tal_check(NULL, NULL));
+               tal_free(p2);
+               tal_free(p1);
+       }
 
+       tal_cleanup();
        return exit_status();
 }