summaryrefslogtreecommitdiff
path: root/ccan/tal/test/run-iter.c
diff options
context:
space:
mode:
authorWilliam Casarin <jb55@jb55.com>2018-07-09 12:10:32 -0700
committerWilliam Casarin <jb55@jb55.com>2018-07-09 12:10:32 -0700
commit1b8fbbd843ddeb5fc81c9303db9c590a436d499b (patch)
treea7227dfe8e4fbaee7b1e0b58b24994dce8078f3f /ccan/tal/test/run-iter.c
parent37a9cdd2e80386f2c94e14e4f511284ae14c745a (diff)
progress
Diffstat (limited to 'ccan/tal/test/run-iter.c')
-rw-r--r--ccan/tal/test/run-iter.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/ccan/tal/test/run-iter.c b/ccan/tal/test/run-iter.c
new file mode 100644
index 0000000..5992172
--- /dev/null
+++ b/ccan/tal/test/run-iter.c
@@ -0,0 +1,53 @@
+#include <ccan/tal/tal.h>
+#include <ccan/tal/tal.c>
+#include <ccan/tap/tap.h>
+
+#define NUM 1000
+
+static int set_children(const tal_t *parent, char val)
+{
+ char *iter;
+ int num = 0;
+
+ for (iter = tal_first(parent); iter; iter = tal_next(iter)) {
+ ok1(*iter == '0');
+ *iter = val;
+ num++;
+ num += set_children(iter, val);
+ }
+ return num;
+}
+
+static void check_children(const tal_t *parent, char val)
+{
+ const char *iter;
+
+ for (iter = tal_first(parent); iter; iter = tal_next(iter)) {
+ ok1(*iter == val);
+ check_children(iter, val);
+ }
+}
+
+int main(void)
+{
+ char *p[NUM] = { NULL };
+ int i;
+
+ plan_tests(NUM + 1 + NUM);
+
+ /* Create a random tree */
+ for (i = 0; i < NUM; i++) {
+ p[i] = tal(p[rand() % (i + 1)], char);
+ *p[i] = '0';
+ }
+
+ i = set_children(NULL, '1');
+ ok1(i == NUM);
+
+ check_children(NULL, '1');
+ for (i = NUM-1; i >= 0; i--)
+ tal_free(p[i]);
+
+ tal_cleanup();
+ return exit_status();
+}