44#include " freeTreeNode.hpp"
55#include " index.hpp"
66#include " serializeTreeNode.hpp"
7- #include < iostream>
8- #include < stdio.h>
9-
10- // #include <set>
117#include < cppunit/TestResult.h>
128#include < cppunit/TestResultCollector.h>
139#include < cppunit/TestRunner.h>
1410#include < cppunit/TextOutputter.h>
1511#include < cppunit/extensions/HelperMacros.h>
12+ #include < iostream>
13+ #include < stdio.h>
14+ #include < string>
15+ #include < vector>
1616
1717#include < sstream>
1818#include < unordered_set>
@@ -25,6 +25,11 @@ using namespace std;
2525#include " printTreeNode.hpp"
2626#include " treeparse.hpp"
2727
28+ void println (int s)
29+ {
30+ cout << s << endl;
31+ }
32+
2833void println (string s)
2934{
3035 cout << s << endl;
@@ -81,18 +86,48 @@ void test2()
8186 }
8287 println (" test2 end" );
8388}
84-
89+ struct ExampleType {
90+ string root;
91+ int val;
92+ string output;
93+ };
8594class StringTest : public CppUnit ::TestFixture {
8695 CPPUNIT_TEST_SUITE (StringTest);
8796 CPPUNIT_TEST (testSwap);
8897 CPPUNIT_TEST (testFind);
8998
9099 CPPUNIT_TEST (test3);
100+ CPPUNIT_TEST (test4);
91101 CPPUNIT_TEST_SUITE_END ();
92102
93103public:
94104 void setUp () { }
105+ void test4 ()
106+ {
95107
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+ }
96131 void tearDown () { }
97132
98133 void testSwap () { test1 (); }
0 commit comments