From e2200572fc74bc83cc7e5281b4257f895b5e0dc1 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 17 Jun 2015 15:28:41 -0400 Subject: [PATCH 04/35] Add selftests to et-forest.c Jeff approved an earlier version of this: https://gcc.gnu.org/ml/gcc-patches/2015-10/msg03295.html > OK if/when prereqs are approved. Minor twiddling if we end up > moving it elsewhere or standardizing/reducing header files > is pre-approved. gcc/ChangeLog: * et-forest.c: Include "selftest.h". (et_forest_test, single_node): New selftest. (et_forest_test, simple_tree): New selftest. --- gcc/et-forest.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/gcc/et-forest.c b/gcc/et-forest.c index cd36752..9a65e9d 100644 --- a/gcc/et-forest.c +++ b/gcc/et-forest.c @@ -27,6 +27,7 @@ License along with libiberty; see the file COPYING3. If not see #include "coretypes.h" #include "alloc-pool.h" #include "et-forest.h" +#include "selftest.h" /* We do not enable this with CHECKING_P, since it is awfully slow. */ #undef DEBUG_ET @@ -764,3 +765,101 @@ et_root (struct et_node *node) return r->of; } + +#if CHECKING_P + +namespace { + +TEST (et_forest_test, single_node) +{ + void *test_data = (void *)0xcafebabe; + + et_node *n = et_new_tree (test_data); + EXPECT_EQ (n->data, test_data); + EXPECT_EQ (n, et_root (n)); + et_free_tree (n); +} + +/* Test of this tree: + a + / \ + / \ + b c + / \ | + d e f. */ + +TEST (et_forest_test, simple_tree) +{ + et_node *a = et_new_tree (NULL); + et_node *b = et_new_tree (NULL); + et_node *c = et_new_tree (NULL); + et_node *d = et_new_tree (NULL); + et_node *e = et_new_tree (NULL); + et_node *f = et_new_tree (NULL); + + et_set_father (b, a); + et_set_father (c, a); + et_set_father (d, b); + et_set_father (e, b); + et_set_father (f, c); + + EXPECT_TRUE (et_below (a, a)); + EXPECT_TRUE (et_below (b, a)); + EXPECT_TRUE (et_below (c, a)); + EXPECT_TRUE (et_below (d, a)); + EXPECT_TRUE (et_below (e, a)); + EXPECT_TRUE (et_below (f, a)); + + EXPECT_FALSE (et_below (a, b)); + EXPECT_TRUE (et_below (b, b)); + EXPECT_FALSE (et_below (c, b)); + EXPECT_TRUE (et_below (d, b)); + EXPECT_TRUE (et_below (e, b)); + EXPECT_FALSE (et_below (f, b)); + + EXPECT_FALSE (et_below (a, c)); + EXPECT_FALSE (et_below (b, c)); + EXPECT_TRUE (et_below (c, c)); + EXPECT_FALSE (et_below (d, c)); + EXPECT_FALSE (et_below (e, c)); + EXPECT_TRUE (et_below (f, c)); + + EXPECT_FALSE (et_below (a, d)); + EXPECT_FALSE (et_below (b, d)); + EXPECT_FALSE (et_below (c, d)); + EXPECT_TRUE (et_below (d, d)); + EXPECT_FALSE (et_below (e, d)); + EXPECT_FALSE (et_below (f, d)); + + EXPECT_FALSE (et_below (a, e)); + EXPECT_FALSE (et_below (b, e)); + EXPECT_FALSE (et_below (c, e)); + EXPECT_FALSE (et_below (d, e)); + EXPECT_TRUE (et_below (e, e)); + EXPECT_FALSE (et_below (f, e)); + + EXPECT_FALSE (et_below (a, f)); + EXPECT_FALSE (et_below (b, f)); + EXPECT_FALSE (et_below (c, f)); + EXPECT_FALSE (et_below (d, f)); + EXPECT_FALSE (et_below (e, f)); + EXPECT_TRUE (et_below (f, f)); + + et_free_tree_force (a); +} + +TEST (et_forest_test, disconnected_nodes) +{ + et_node *a = et_new_tree (NULL); + et_node *b = et_new_tree (NULL); + + EXPECT_FALSE (et_below (a, b)); + EXPECT_FALSE (et_below (b, a)); + + et_free_tree (a); + et_free_tree (b); +} + +} // anon namespace + +#endif /* CHECKING_P */ -- 1.8.5.3