Skip to content

Commit

Permalink
update p0 tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi <[email protected]>
  • Loading branch information
skyzh committed Aug 26, 2023
1 parent f185bcb commit ac25e57
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,4 @@ add_custom_target(submit-p4
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_dependencies(check-clang-tidy gtest bustub) # needs gtest headers, compile_commands.json
add_dependencies(check-clang-tidy gtest bustub) # needs gtest headers, compile_commands.json
11 changes: 9 additions & 2 deletions src/include/primer/trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ class TrieNode {
virtual auto Clone() const -> std::unique_ptr<TrieNode> { return std::make_unique<TrieNode>(children_); }

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

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

// You can add additional fields and methods here. But in general, you don't need to add extra fields to
// complete this project.
// You can add additional fields and methods here except storing children. But in general, you don't need to add extra
// fields to complete this project.
};

// A TrieNodeWithValue is a TrieNode that also has a value of type T associated with it.
Expand Down Expand Up @@ -97,6 +99,8 @@ class TrieNodeWithValue : public TrieNode {
// A Trie is a data structure that maps strings to values of type T. All operations on a Trie should not
// modify the trie itself. It should reuse the existing nodes as much as possible, and create new nodes to
// represent the new trie.
//
// You are NOT allowed to remove any `const` in this project, or use `mutable` to bypass the const checks.
class Trie {
private:
// The root of the trie.
Expand Down Expand Up @@ -124,6 +128,9 @@ class Trie {
// Remove the key from the trie. If the key does not exist, return the original trie.
// Otherwise, returns the new trie.
auto Remove(std::string_view key) const -> Trie;

// Get the root of the trie, should only be used in test cases.
auto GetRoot() const -> std::shared_ptr<const TrieNode> { return root_; }
};

} // namespace bustub
29 changes: 22 additions & 7 deletions test/primer/trie_debug_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <fmt/format.h>
#include <zipfian_int_distribution.h>
#include <bitset>
#include <functional>
#include <numeric>
Expand All @@ -15,13 +16,27 @@
namespace bustub {

TEST(TrieDebugger, TestCase) {
std::mt19937_64 gen(2333);
std::uniform_int_distribution<uint32_t> dis(0, 100);
std::mt19937_64 gen(23333);
zipfian_int_distribution<uint32_t> dis(0, 1000);

auto trie = Trie();
for (uint32_t i = 0; i < 10; i++) {
for (uint32_t i = 0; i < 100; i++) {
std::string key = fmt::format("{}", dis(gen));
auto value = dis(gen);
switch (i) {
// Test the first 3 values from the random generator.
case 0:
ASSERT_EQ(value, 128) << "Random generator not portable, please post on Piazza for help.";
break;
case 1:
ASSERT_EQ(value, 16) << "Random generator not portable, please post on Piazza for help.";
break;
case 2:
ASSERT_EQ(value, 41) << "Random generator not portable, please post on Piazza for help.";
break;
default:
break;
}
trie = trie.Put<uint32_t>(key, value);
}

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

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

// (3) What's the value for `93`?
// (3) What's the value for `969`?
// Replace `CASE_3_YOUR_ANSWER` in `trie_answer.h` with the correct answer.
if (CASE_3_YOUR_ANSWER != Case3CorrectAnswer()) {
ASSERT_TRUE(false);
ASSERT_TRUE(false) << "case 3 not correct";
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/primer/trie_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ TEST(TrieTest, BasicPutTest) {
trie = trie.Put<std::string>("", "empty-key");
}

TEST(TrieTest, TrieStructureCheck) {
auto trie = Trie();
// Put something
trie = trie.Put<uint32_t>("test", 233);
ASSERT_EQ(*trie.Get<uint32_t>("test"), 233);
// Ensure the trie is the same representation of the writeup
// (Some students were using '\0' as the terminator in previous semesters)
auto root = trie.GetRoot();
ASSERT_EQ(root->children_.size(), 1);
ASSERT_EQ(root->children_.at('t')->children_.size(), 1);
ASSERT_EQ(root->children_.at('t')->children_.at('e')->children_.size(), 1);
ASSERT_EQ(root->children_.at('t')->children_.at('e')->children_.at('s')->children_.size(), 1);
ASSERT_EQ(root->children_.at('t')->children_.at('e')->children_.at('s')->children_.at('t')->children_.size(), 0);
ASSERT_TRUE(root->children_.at('t')->children_.at('e')->children_.at('s')->children_.at('t')->is_value_node_);
}

TEST(TrieTest, BasicPutGetTest) {
auto trie = Trie();
// Put something
Expand Down Expand Up @@ -89,6 +105,18 @@ TEST(TrieTest, BasicRemoveTest2) {
ASSERT_EQ(trie.Get<uint32_t>("test"), nullptr);
}

TEST(TrieTest, RemoveFreeTest) {
auto trie = Trie();
trie = trie.Put<uint32_t>("test", 2333);
trie = trie.Put<uint32_t>("te", 23);
trie = trie.Put<uint32_t>("tes", 233);
trie = trie.Remove("tes");
trie = trie.Remove("test");
ASSERT_EQ(trie.GetRoot()->children_.at('t')->children_.at('e')->children_.size(), 0);
trie = trie.Remove("te");
ASSERT_EQ(trie.GetRoot(), nullptr);
}

TEST(TrieTest, MismatchTypeTest) {
auto trie = Trie();
// Put something
Expand Down
12 changes: 6 additions & 6 deletions third_party/versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

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

# libpg_query
# url: https://github.com/duckdb/duckdb/tree/master/third_party/libpg_query
Expand All @@ -37,9 +37,9 @@

# fmt
# url: https://github.com/fmtlib/fmt
# tag: 9.0.0
# commit hash: c4ee726532178e556d923372f29163bd206d7732
# commit hash date: Jul 4 2022
# tag: 10.1.0
# commit hash: e57ca2e3685b160617d3d95fcd9e789c4e06ca88
# commit hash date: Aug 12 2023

# linenoise
# url: https://github.com/antirez/linenoise
Expand Down

0 comments on commit ac25e57

Please sign in to comment.