From 6ad51b897a223e95ee852321b3c5c0ec23b1ee64 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Wed, 5 Feb 2014 16:57:49 +0100 Subject: Added CMake build instructions and test cases * Test cases are based on GoogleTest and are executed automatically after each compile * Updated README.md accordingly --- CMakeLists.txt | 26 +++++++++++++++++++++ README.md | 2 +- test.cc | 71 +++++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..342dbc8 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 2.8) +project(Trie) + +set( + CMAKE_CXX_FLAGS + "-std=c++11 -W -Wall -Wextra -Winline -pedantic" +) + +include_directories( + src/ +) + +add_executable( + test + test.cc +) + +target_link_libraries( + test + gtest +) + +add_custom_command( + TARGET test + POST_BUILD COMMAND ./test +) diff --git a/README.md b/README.md index db5d4f7..9e03d6b 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,4 @@ - Specializable tree element type - Adding, removing and resolving paths in the prefix tree - Builds on the standard library - +- Unit tests based on GoogleTest diff --git a/test.cc b/test.cc index 5105f09..2c38a3f 100644 --- a/test.cc +++ b/test.cc @@ -1,24 +1,65 @@ #include "trie.h" -#include +#include "gtest/gtest.h" + #include -int main() { - Trie test; +class TrieTest : public ::testing::Test { }; + +TEST_F(TrieTest, Basic) { + Trie trie; + + trie.add({1, 1, 1, 1}); + trie.add({1, 2, 1, 2}); + trie.add({2, 3, 4, 5}); + trie.add({2, 3, 1, 2}); + + EXPECT_EQ(trie.resolve({1, 1, 1, 1}).first, true); + EXPECT_NE(trie.resolve({1, 1, 1, 1}).second, nullptr); + + EXPECT_EQ(trie.resolve({1, 2, 1, 2}).first, true); + EXPECT_NE(trie.resolve({1, 2, 1, 2}).second, nullptr); + + EXPECT_EQ(trie.resolve({1, 2}).first, true); + EXPECT_NE(trie.resolve({1, 2}).second, nullptr); + + EXPECT_EQ(trie.resolve({1, 1, 2, 3}).first, false); + EXPECT_EQ(trie.resolve({1, 1, 2, 3}).second, nullptr); + + EXPECT_EQ(trie.resolve({2, 1, 4}).first, false); + EXPECT_EQ(trie.resolve({2, 1, 4}).second, nullptr); +} - test.add({1, 2, 3}); - test.add({1, 2, 4}); - test.add({2, 1}); - test.add({2, 1, 1}); +TEST_F(TrieTest, Remove) { + Trie trie; - assert(test.resolve({1, 2}).second != nullptr); - assert(test.resolve({1, 2, 3}).second != nullptr); - assert(test.resolve({1, 2, 4}).second != nullptr); - assert(test.resolve({3}).second == nullptr); + trie.add({1, 1, 1, 1}); + trie.add({1, 2, 1, 2}); + trie.add({2, 3, 4, 5}); + trie.add({2, 3, 1, 2}); + + trie.remove({1, 1, 1, 1}); + + EXPECT_EQ(trie.resolve({1, 1, 1, 1}).first, false); + EXPECT_EQ(trie.resolve({1, 1, 1, 1}).second, nullptr); + + EXPECT_EQ(trie.resolve({1, 2, 1, 2}).first, true); + EXPECT_NE(trie.resolve({1, 2, 1, 2}).second, nullptr); + + trie.remove({2, 3}); + + EXPECT_EQ(trie.resolve({2, 3, 4, 5}).first, false); + EXPECT_EQ(trie.resolve({2, 3, 4, 5}).second, nullptr); + + EXPECT_EQ(trie.resolve({2, 3}).first, false); + EXPECT_EQ(trie.resolve({2, 3}).second, nullptr); + + EXPECT_EQ(trie.resolve({2}).first, true); + EXPECT_NE(trie.resolve({2}).second, nullptr); +} - test.remove({1, 2}); +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); - assert(test.resolve({1, 2, 4}).second == nullptr); - assert(test.resolve({1, 2, 3}).second == nullptr); - assert(test.resolve({1, 2}).second == nullptr); + return RUN_ALL_TESTS(); } -- cgit v1.2.3