Skip to content

Commit 94825c1

Browse files
authoredJul 12, 2024
[1_8] improve test of tree, add tests for edge cases
1 parent 44be84e commit 94825c1

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed
 

‎tests/Kernel/Types/tree_test.cpp

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "moe_doctests.hpp"
22
#include "tree.hpp"
3+
const int label= 123; // 123 can be a number != 0, thus the tree is compound.
34

45
TEST_CASE ("test_is_atomic") {
56
CHECK (is_atomic (tree ()));
@@ -55,10 +56,13 @@ TEST_CASE ("test_is_string") {
5556
}
5657

5758
TEST_CASE ("test N()") {
58-
CHECK (N (tree ()) == 0);
59-
CHECK (N (tree (0, tree ())) == 1);
60-
CHECK (N (tree (0, tree (), tree ())) == 2);
61-
CHECK (N (tree (0, tree (), tree (), tree ())) == 3);
59+
CHECK_EQ (N (tree ()), 0);
60+
CHECK_EQ (N (tree ("")), 0);
61+
CHECK_EQ (N (tree ("atomic tree")), 11);
62+
CHECK_EQ (N (tree (1)), 0);
63+
CHECK_EQ (N (tree (1, tree ())), 1);
64+
CHECK_EQ (N (tree (1, tree (), tree ())), 2);
65+
CHECK_EQ (N (tree (1, tree (), tree (), tree ())), 3);
6266
}
6367

6468
TEST_CASE ("test_arity") {
@@ -107,8 +111,31 @@ TEST_CASE ("test operator==") {
107111
CHECK (tree ("hello") == string ("hello"));
108112
}
109113

114+
TEST_CASE ("tree operator*") {
115+
CHECK_EQ (tree (label) * tree (label, "1", "2", "4"),
116+
tree (label, "1", "2", "4"));
117+
CHECK_EQ (tree ("str") * tree (label, "1", "2", "4"),
118+
tree (label, "str", "1", "2", "4"));
119+
CHECK_EQ (tree (label, "1", "2", "4") * tree ("str"),
120+
tree (label, "1", "2", "4", "str"));
121+
tree string_concat= tree ("str") * tree ("ing");
122+
CHECK_EQ (string_concat->op, 0);
123+
CHECK_EQ (N (string_concat), 2);
124+
CHECK_EQ (A (string_concat), array<tree> ("str", "ing"));
125+
}
126+
127+
TEST_CASE ("tree operator()") {
128+
tree whole= tree (label, "str", "ing", "1", "2", "4");
129+
CHECK_EQ (whole (2, N (whole)), tree (label, "1", "2", "4"));
130+
CHECK_EQ (whole (0, 4), tree (label, "str", "ing", "1", "2"));
131+
CHECK_EQ (whole (0, N (whole)), whole);
132+
133+
// tree atomic_tree= tree ("str"); this will cause crash!
134+
// CHECK_EQ (atomic_tree (0, 1), atomic_tree);
135+
}
136+
110137
TEST_CASE ("tree operator<<") {
111-
tree t= tree (123); // 123 can be a number > 1
138+
tree t= tree (label);
112139
t << "1"
113140
<< "2"
114141
<< "4";
@@ -125,6 +152,7 @@ TEST_CASE ("test strong_equal") {
125152
auto a= tree (0, tree ());
126153
auto b= &a;
127154
CHECK (strong_equal (a, *b));
155+
CHECK (!strong_equal (tree (label, 1, 2), tree (label, 1, 2)));
128156
CHECK (!strong_equal (tree (0, 1, 2), tree (3, tree ())));
129157
CHECK (!strong_equal (tree (0, 1, 2), tree (4, tree ())));
130158
}

0 commit comments

Comments
 (0)
Please sign in to comment.