Commit c0ea2922 authored by Rusty Russell's avatar Rusty Russell

rbtree: use failtest to check handling of allocation failures.

Unfortunately this means we have to reduce run-many from 1000 nodes to 100
(as forking under valgrind is really slow: test takes about 2 minutes with
100 nodes).
parent 8d20b53e
...@@ -99,6 +99,7 @@ int main(int argc, char *argv[]) ...@@ -99,6 +99,7 @@ int main(int argc, char *argv[])
return 1; return 1;
if (strcmp(argv[1], "depends") == 0) { if (strcmp(argv[1], "depends") == 0) {
printf("ccan/failtest\n");
printf("ccan/talloc\n"); printf("ccan/talloc\n");
return 0; return 0;
} }
......
...@@ -3,8 +3,25 @@ ...@@ -3,8 +3,25 @@
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h>
#define NUM_ELEMS 100
#define NUM_ELEMS 10000 /* We want to test talloc failure paths. */
static void *my_malloc(size_t size)
{
return malloc(size);
}
static void my_free(void *ptr)
{
free(ptr);
}
static void *my_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}
static bool lookup_all(trbt_tree_t *rb, bool exist) static bool lookup_all(trbt_tree_t *rb, bool exist)
{ {
...@@ -26,15 +43,25 @@ static bool lookup_all(trbt_tree_t *rb, bool exist) ...@@ -26,15 +43,25 @@ static bool lookup_all(trbt_tree_t *rb, bool exist)
static bool add_one(trbt_tree_t *rb, bool exist, unsigned int i) static bool add_one(trbt_tree_t *rb, bool exist, unsigned int i)
{ {
int *p = trbt_insert32(rb, i, talloc_memdup(rb, &i, sizeof(i))); int *new = talloc_memdup(rb, &i, sizeof(i));
int *p;
if (!new)
return false;
p = trbt_insert32(rb, i, new);
if (p) { if (p) {
if (!exist) if (!exist)
return false; return false;
if (*p != i) if (*p != i)
return false; return false;
} else } else {
if (exist) if (exist)
return false; return false;
else
if (!trbt_lookup32(rb, i))
return false;
}
return true; return true;
} }
...@@ -63,13 +90,27 @@ static void delete_all(trbt_tree_t *rb) ...@@ -63,13 +90,27 @@ static void delete_all(trbt_tree_t *rb)
} }
} }
int main(void) static void *ctx;
static trbt_tree_t *rb;
static void exit_test(void)
{ {
trbt_tree_t *rb; talloc_free(rb);
void *ctx = talloc_strdup(NULL, "toplevel"); ok1(talloc_total_blocks(ctx) == 1);
talloc_free(ctx);
failtest_exit(exit_status());
}
int main(int argc, char *argv[])
{
failtest_init(argc, argv);
tap_fail_callback = exit_test;
plan_tests(8); plan_tests(8);
ctx = talloc_strdup(NULL, "toplevel");
talloc_set_allocator(my_malloc, my_free, my_realloc);
rb = trbt_create(ctx, 0); rb = trbt_create(ctx, 0);
ok1(rb); ok1(rb);
...@@ -100,5 +141,5 @@ int main(void) ...@@ -100,5 +141,5 @@ int main(void)
talloc_free(ctx); talloc_free(ctx);
/* This exits depending on whether all tests passed */ /* This exits depending on whether all tests passed */
return exit_status(); failtest_exit(exit_status());
} }
#include <ccan/failtest/failtest.h>
#include <ccan/rbtree/rbtree.c> #include <ccan/rbtree/rbtree.c>
#include <ccan/tap/tap.h> #include <ccan/tap/tap.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <string.h> #include <string.h>
/* We want to test talloc failure paths. */
static void *my_malloc(size_t size)
{
return malloc(size);
}
static void my_free(void *ptr)
{
free(ptr);
}
static void *my_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}
static void *insert_callback(void *param, void *data) static void *insert_callback(void *param, void *data)
{ {
ok1(data == param); ok1(data == param);
...@@ -18,6 +35,8 @@ int main(void) ...@@ -18,6 +35,8 @@ int main(void)
/* This is how many tests you plan to run */ /* This is how many tests you plan to run */
plan_tests(19); plan_tests(19);
talloc_set_allocator(my_malloc, my_free, my_realloc);
rb = trbt_create(ctx, 0); rb = trbt_create(ctx, 0);
ok1(rb); ok1(rb);
ok1(talloc_is_parent(rb, ctx)); ok1(talloc_is_parent(rb, ctx));
...@@ -73,5 +92,5 @@ int main(void) ...@@ -73,5 +92,5 @@ int main(void)
talloc_free(ctx); talloc_free(ctx);
/* This exits depending on whether all tests passed */ /* This exits depending on whether all tests passed */
return exit_status(); failtest_exit(exit_status());
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment