4
4
#include " freeTreeNode.hpp"
5
5
#include " index.hpp"
6
6
#include " serializeTreeNode.hpp"
7
- #include < iostream>
8
- #include < stdio.h>
9
-
10
- // #include <set>
11
7
#include < cppunit/TestResult.h>
12
8
#include < cppunit/TestResultCollector.h>
13
9
#include < cppunit/TestRunner.h>
14
10
#include < cppunit/TextOutputter.h>
15
11
#include < cppunit/extensions/HelperMacros.h>
12
+ #include < iostream>
13
+ #include < stdio.h>
14
+ #include < string>
15
+ #include < vector>
16
16
17
17
#include < sstream>
18
18
#include < unordered_set>
@@ -25,6 +25,11 @@ using namespace std;
25
25
#include " printTreeNode.hpp"
26
26
#include " treeparse.hpp"
27
27
28
+ void println (int s)
29
+ {
30
+ cout << s << endl;
31
+ }
32
+
28
33
void println (string s)
29
34
{
30
35
cout << s << endl;
@@ -81,18 +86,48 @@ void test2()
81
86
}
82
87
println (" test2 end" );
83
88
}
84
-
89
+ struct ExampleType {
90
+ string root;
91
+ int val;
92
+ string output;
93
+ };
85
94
class StringTest : public CppUnit ::TestFixture {
86
95
CPPUNIT_TEST_SUITE (StringTest);
87
96
CPPUNIT_TEST (testSwap);
88
97
CPPUNIT_TEST (testFind);
89
98
90
99
CPPUNIT_TEST (test3);
100
+ CPPUNIT_TEST (test4);
91
101
CPPUNIT_TEST_SUITE_END ();
92
102
93
103
public:
94
104
void setUp () { }
105
+ void test4 ()
106
+ {
95
107
108
+ auto examples = vector<ExampleType> { { " [4,2,7,1,3]" , 5 , " [4,2,7,1,3,5]" },
109
+
110
+ { " [40,20,60,10,30,50,70]" , 25 , " [40,20,60,10,30,50,70,null,null,25]" },
111
+ { " [4,2,7,1,3,null,null,null,null,null,null]" , 5 , " [4,2,7,1,3,5]" } };
112
+
113
+ for (auto & example : examples) {
114
+ TreeNode* root = nullptr ;
115
+ int status = parseLeetCodeBinaryTree (example.root , &root);
116
+ CPPUNIT_ASSERT_EQUAL (0 , status);
117
+ auto output = Solution ().insertIntoBST (root, example.val );
118
+
119
+ CPPUNIT_ASSERT_EQUAL (LeetCodeTreeNodeToString (output),
120
+ example.output );
121
+ println (example.root );
122
+ println (example.val );
123
+ println (example.output );
124
+ auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { root, output };
125
+ for (auto node : nodes) {
126
+ printTreeNode (node);
127
+ freeTreeNode (node);
128
+ }
129
+ }
130
+ }
96
131
void tearDown () { }
97
132
98
133
void testSwap () { test1 (); }
0 commit comments