Skip to content

Commit ac25e57

Browse files
committed
update p0 tests
Signed-off-by: Alex Chi <[email protected]>
1 parent f185bcb commit ac25e57

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,4 @@ add_custom_target(submit-p4
380380
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
381381
)
382382

383-
add_dependencies(check-clang-tidy gtest bustub) # needs gtest headers, compile_commands.json
383+
add_dependencies(check-clang-tidy gtest bustub) # needs gtest headers, compile_commands.json

src/include/primer/trie.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ class TrieNode {
6161
virtual auto Clone() const -> std::unique_ptr<TrieNode> { return std::make_unique<TrieNode>(children_); }
6262

6363
// A map of children, where the key is the next character in the key, and the value is the next TrieNode.
64+
// You MUST store the children information in this structure. You are NOT allowed to remove the `const` from
65+
// the structure.
6466
std::map<char, std::shared_ptr<const TrieNode>> children_;
6567

6668
// Indicates if the node is the terminal node.
6769
bool is_value_node_{false};
6870

69-
// You can add additional fields and methods here. But in general, you don't need to add extra fields to
70-
// complete this project.
71+
// You can add additional fields and methods here except storing children. But in general, you don't need to add extra
72+
// fields to complete this project.
7173
};
7274

7375
// A TrieNodeWithValue is a TrieNode that also has a value of type T associated with it.
@@ -97,6 +99,8 @@ class TrieNodeWithValue : public TrieNode {
9799
// A Trie is a data structure that maps strings to values of type T. All operations on a Trie should not
98100
// modify the trie itself. It should reuse the existing nodes as much as possible, and create new nodes to
99101
// represent the new trie.
102+
//
103+
// You are NOT allowed to remove any `const` in this project, or use `mutable` to bypass the const checks.
100104
class Trie {
101105
private:
102106
// The root of the trie.
@@ -124,6 +128,9 @@ class Trie {
124128
// Remove the key from the trie. If the key does not exist, return the original trie.
125129
// Otherwise, returns the new trie.
126130
auto Remove(std::string_view key) const -> Trie;
131+
132+
// Get the root of the trie, should only be used in test cases.
133+
auto GetRoot() const -> std::shared_ptr<const TrieNode> { return root_; }
127134
};
128135

129136
} // namespace bustub

test/primer/trie_debug_test.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <fmt/format.h>
2+
#include <zipfian_int_distribution.h>
23
#include <bitset>
34
#include <functional>
45
#include <numeric>
@@ -15,13 +16,27 @@
1516
namespace bustub {
1617

1718
TEST(TrieDebugger, TestCase) {
18-
std::mt19937_64 gen(2333);
19-
std::uniform_int_distribution<uint32_t> dis(0, 100);
19+
std::mt19937_64 gen(23333);
20+
zipfian_int_distribution<uint32_t> dis(0, 1000);
2021

2122
auto trie = Trie();
22-
for (uint32_t i = 0; i < 10; i++) {
23+
for (uint32_t i = 0; i < 100; i++) {
2324
std::string key = fmt::format("{}", dis(gen));
2425
auto value = dis(gen);
26+
switch (i) {
27+
// Test the first 3 values from the random generator.
28+
case 0:
29+
ASSERT_EQ(value, 128) << "Random generator not portable, please post on Piazza for help.";
30+
break;
31+
case 1:
32+
ASSERT_EQ(value, 16) << "Random generator not portable, please post on Piazza for help.";
33+
break;
34+
case 2:
35+
ASSERT_EQ(value, 41) << "Random generator not portable, please post on Piazza for help.";
36+
break;
37+
default:
38+
break;
39+
}
2540
trie = trie.Put<uint32_t>(key, value);
2641
}
2742

@@ -30,19 +45,19 @@ TEST(TrieDebugger, TestCase) {
3045
// (1) How many children nodes are there on the root?
3146
// Replace `CASE_1_YOUR_ANSWER` in `trie_answer.h` with the correct answer.
3247
if (CASE_1_YOUR_ANSWER != Case1CorrectAnswer()) {
33-
ASSERT_TRUE(false);
48+
ASSERT_TRUE(false) << "case 1 not correct";
3449
}
3550

3651
// (2) How many children nodes are there on the node of prefix `9`?
3752
// Replace `CASE_2_YOUR_ANSWER` in `trie_answer.h` with the correct answer.
3853
if (CASE_2_YOUR_ANSWER != Case2CorrectAnswer()) {
39-
ASSERT_TRUE(false);
54+
ASSERT_TRUE(false) << "case 2 not correct";
4055
}
4156

42-
// (3) What's the value for `93`?
57+
// (3) What's the value for `969`?
4358
// Replace `CASE_3_YOUR_ANSWER` in `trie_answer.h` with the correct answer.
4459
if (CASE_3_YOUR_ANSWER != Case3CorrectAnswer()) {
45-
ASSERT_TRUE(false);
60+
ASSERT_TRUE(false) << "case 3 not correct";
4661
}
4762
}
4863

test/primer/trie_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ TEST(TrieTest, BasicPutTest) {
2222
trie = trie.Put<std::string>("", "empty-key");
2323
}
2424

25+
TEST(TrieTest, TrieStructureCheck) {
26+
auto trie = Trie();
27+
// Put something
28+
trie = trie.Put<uint32_t>("test", 233);
29+
ASSERT_EQ(*trie.Get<uint32_t>("test"), 233);
30+
// Ensure the trie is the same representation of the writeup
31+
// (Some students were using '\0' as the terminator in previous semesters)
32+
auto root = trie.GetRoot();
33+
ASSERT_EQ(root->children_.size(), 1);
34+
ASSERT_EQ(root->children_.at('t')->children_.size(), 1);
35+
ASSERT_EQ(root->children_.at('t')->children_.at('e')->children_.size(), 1);
36+
ASSERT_EQ(root->children_.at('t')->children_.at('e')->children_.at('s')->children_.size(), 1);
37+
ASSERT_EQ(root->children_.at('t')->children_.at('e')->children_.at('s')->children_.at('t')->children_.size(), 0);
38+
ASSERT_TRUE(root->children_.at('t')->children_.at('e')->children_.at('s')->children_.at('t')->is_value_node_);
39+
}
40+
2541
TEST(TrieTest, BasicPutGetTest) {
2642
auto trie = Trie();
2743
// Put something
@@ -89,6 +105,18 @@ TEST(TrieTest, BasicRemoveTest2) {
89105
ASSERT_EQ(trie.Get<uint32_t>("test"), nullptr);
90106
}
91107

108+
TEST(TrieTest, RemoveFreeTest) {
109+
auto trie = Trie();
110+
trie = trie.Put<uint32_t>("test", 2333);
111+
trie = trie.Put<uint32_t>("te", 23);
112+
trie = trie.Put<uint32_t>("tes", 233);
113+
trie = trie.Remove("tes");
114+
trie = trie.Remove("test");
115+
ASSERT_EQ(trie.GetRoot()->children_.at('t')->children_.at('e')->children_.size(), 0);
116+
trie = trie.Remove("te");
117+
ASSERT_EQ(trie.GetRoot(), nullptr);
118+
}
119+
92120
TEST(TrieTest, MismatchTypeTest) {
93121
auto trie = Trie();
94122
// Put something

third_party/versions.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
# googletest
2727
# url: https://github.com/google/googletest.git
28-
# tag: release-1.12.1
29-
# commit hash: 58d77fa8070e8cec2dc1ed015d66b454c8d78850
30-
# commit hash date: Jun 27 2022
28+
# tag: v1.14.0
29+
# commit hash: f8d7d77c06936315286eb55f8de22cd23c188571
30+
# commit hash date: Aug 2 2023
3131

3232
# libpg_query
3333
# url: https://github.com/duckdb/duckdb/tree/master/third_party/libpg_query
@@ -37,9 +37,9 @@
3737

3838
# fmt
3939
# url: https://github.com/fmtlib/fmt
40-
# tag: 9.0.0
41-
# commit hash: c4ee726532178e556d923372f29163bd206d7732
42-
# commit hash date: Jul 4 2022
40+
# tag: 10.1.0
41+
# commit hash: e57ca2e3685b160617d3d95fcd9e789c4e06ca88
42+
# commit hash date: Aug 12 2023
4343

4444
# linenoise
4545
# url: https://github.com/antirez/linenoise

0 commit comments

Comments
 (0)