Skip to content

Commit aa14b72

Browse files
committed
测试用例
1 parent 6e3e813 commit aa14b72

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

insert-into-a-binary-search-tree/insert-into-a-binary-search-tree.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<ClInclude Include="freeTreeNode.hpp" />
108108
<ClInclude Include="HashTreeNode.hpp" />
109109
<ClInclude Include="index.hpp" />
110+
<ClInclude Include="LeetCodeTreeNodeToString.hpp" />
110111
<ClInclude Include="printTreeNode.hpp" />
111112
<ClInclude Include="serializeTreeNode.hpp" />
112113
<ClInclude Include="TreeNode.hpp" />

insert-into-a-binary-search-tree/insert-into-a-binary-search-tree.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050
<ClInclude Include="treeparse.hpp">
5151
<Filter>Header Files</Filter>
5252
</ClInclude>
53+
<ClInclude Include="LeetCodeTreeNodeToString.hpp">
54+
<Filter>Header Files</Filter>
55+
</ClInclude>
5356
</ItemGroup>
5457
</Project>

insert-into-a-binary-search-tree/test.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
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+
2833
void 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+
};
8594
class 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

93103
public:
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

Comments
 (0)