tests/test_tree.c

changeset 838
1ce90ab4fab9
parent 837
7c15fea5cbea
child 839
62d3aecc5bb7
equal deleted inserted replaced
837:7c15fea5cbea 838:1ce90ab4fab9
269 cxTreeIteratorDispose(&iter); 269 cxTreeIteratorDispose(&iter);
270 CX_TEST_ASSERT(iter.stack == NULL); 270 CX_TEST_ASSERT(iter.stack == NULL);
271 } 271 }
272 } 272 }
273 273
274 CX_TEST(test_tree_iterator_basic_test_only_enter) { 274 CX_TEST(test_tree_iterator_basic_only_enter) {
275 tree_node root = {0}; 275 tree_node root = {0};
276 tree_node a = {0}; 276 tree_node a = {0};
277 tree_node b = {0}; 277 tree_node b = {0};
278 tree_node c = {0}; 278 tree_node c = {0};
279 tree_node aa = {0}; 279 tree_node aa = {0};
328 CX_TEST_ASSERT(cc.data == 1); 328 CX_TEST_ASSERT(cc.data == 1);
329 CX_TEST_ASSERT(cba.data == 1); 329 CX_TEST_ASSERT(cba.data == 1);
330 } 330 }
331 } 331 }
332 332
333 CX_TEST(test_tree_iterator_basic_enter_and_exit) {
334 tree_node root = {0};
335 tree_node a = {0};
336 tree_node b = {0};
337 tree_node c = {0};
338 tree_node aa = {0};
339 tree_node ab = {0};
340 tree_node ba = {0};
341 tree_node ca = {0};
342 tree_node cb = {0};
343 tree_node cc = {0};
344 tree_node cba = {0};
345
346 cx_tree_link(&root, &a, tree_node_layout);
347 cx_tree_link(&root, &b, tree_node_layout);
348 cx_tree_link(&root, &c, tree_node_layout);
349 cx_tree_link(&a, &aa, tree_node_layout);
350 cx_tree_link(&a, &ab, tree_node_layout);
351 cx_tree_link(&b, &ba, tree_node_layout);
352 cx_tree_link(&c, &ca, tree_node_layout);
353 cx_tree_link(&c, &cb, tree_node_layout);
354 cx_tree_link(&c, &cc, tree_node_layout);
355 cx_tree_link(&cb, &cba, tree_node_layout);
356 CX_TEST_DO {
357 CxTreeIterator iter = cx_tree_iterator(&root, true, tree_child_list);
358 unsigned chk = 0;
359 cx_foreach(tree_node*, node, iter) {
360 CX_TEST_ASSERT(iter.exiting || node->data == 0);
361 node->data++;
362 chk++;
363 CX_TEST_ASSERT(node == iter.node);
364 if (node == &root) {
365 CX_TEST_ASSERT(iter.depth == 1);
366 } else if (node == &a || node == &b || node == &c) {
367 CX_TEST_ASSERT(iter.depth == 2);
368 } else if (node == &cba) {
369 CX_TEST_ASSERT(iter.depth == 4);
370 } else {
371 CX_TEST_ASSERT(iter.depth == 3);
372 }
373 }
374 CX_TEST_ASSERT(iter.counter == 11);
375 CX_TEST_ASSERT(chk == 22);
376 CX_TEST_ASSERT(iter.stack == NULL);
377 CX_TEST_ASSERT(root.data == 2);
378 CX_TEST_ASSERT(a.data == 2);
379 CX_TEST_ASSERT(b.data == 2);
380 CX_TEST_ASSERT(c.data == 2);
381 CX_TEST_ASSERT(aa.data == 2);
382 CX_TEST_ASSERT(ab.data == 2);
383 CX_TEST_ASSERT(ba.data == 2);
384 CX_TEST_ASSERT(ca.data == 2);
385 CX_TEST_ASSERT(cb.data == 2);
386 CX_TEST_ASSERT(cc.data == 2);
387 CX_TEST_ASSERT(cba.data == 2);
388 }
389 }
390
333 CxTestSuite *cx_test_suite_tree_low_level(void) { 391 CxTestSuite *cx_test_suite_tree_low_level(void) {
334 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); 392 CxTestSuite *suite = cx_test_suite_new("tree (low level)");
335 393
336 cx_test_register(suite, test_tree_link_new_child); 394 cx_test_register(suite, test_tree_link_new_child);
337 cx_test_register(suite, test_tree_link_add_child); 395 cx_test_register(suite, test_tree_link_add_child);
338 cx_test_register(suite, test_tree_link_move_to_other_parent); 396 cx_test_register(suite, test_tree_link_move_to_other_parent);
339 cx_test_register(suite, test_tree_unlink); 397 cx_test_register(suite, test_tree_unlink);
340 cx_test_register(suite, test_tree_search); 398 cx_test_register(suite, test_tree_search);
341 cx_test_register(suite, test_tree_iterator_create_and_dispose); 399 cx_test_register(suite, test_tree_iterator_create_and_dispose);
342 cx_test_register(suite, test_tree_iterator_basic_test_only_enter); 400 cx_test_register(suite, test_tree_iterator_basic_only_enter);
401 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit);
343 402
344 return suite; 403 return suite;
345 } 404 }

mercurial