Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
"slug": "binary-search-tree",
"uuid": "3463598a-c622-4138-97d3-0c71cbe718c6",
"core": false,
"unlocked_by": "pangram",
"unlocked_by": "reverse-string",
"difficulty": 10,
"topics":[
Expand Down
12 changes: 0 additions & 12 deletions exercises/binary-search-tree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ cmake_minimum_required(VERSION 3.1.3)
# Name the project after the exercise
project(${exercise} CXX)

# Locate Boost libraries: unit_test_framework
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

Expand All @@ -38,12 +32,6 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
)
endif()

# We need boost libraries
target_link_libraries(${exercise}
PRIVATE
Boost::unit_test_framework
)

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
Expand Down
40 changes: 22 additions & 18 deletions exercises/binary-search-tree/binary_search_tree_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "binary_search_tree.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#define CATCH_CONFIG_MAIN
#include "test/catch.hpp"
#include <vector>

// test data version: 1.0.0
Expand All @@ -12,9 +12,9 @@ template<typename T>
static void test_leaf(const tree_ptr<T> &tree,
const T& data, bool has_left, bool has_right)
{
BOOST_REQUIRE_EQUAL(data, tree->data());
BOOST_REQUIRE_EQUAL((bool) tree->left(), has_left);
BOOST_REQUIRE_EQUAL((bool) tree->right(), has_right);
REQUIRE(data == tree->data());
REQUIRE((bool) tree->left() == has_left);
REQUIRE((bool) tree->right() == has_right);
}

template<typename T>
Expand All @@ -35,39 +35,39 @@ static tree_ptr<T> make_tree(const std::vector<T> &data)
return tree;
}

BOOST_AUTO_TEST_CASE(data_is_retained)
TEST_CASE("data_is_retained")
{
auto tested = make_tree<uint32_t>({4});
test_leaf<uint32_t>(tested, 4, false, false);
}

#if defined(EXERCISM_RUN_ALL_TESTS)

BOOST_AUTO_TEST_CASE(smaller_number_at_left_node)
TEST_CASE("smaller_number_at_left_node")
{
auto tested = make_tree<uint32_t>({4, 2});

test_leaf<uint32_t>(tested, 4, true, false);
test_leaf<uint32_t>(tested->left(), 2, false, false);
}

BOOST_AUTO_TEST_CASE(same_number_at_left_node)
TEST_CASE("same_number_at_left_node")
{
auto tested = make_tree<uint32_t>({4, 4});

test_leaf<uint32_t>(tested, 4, true, false);
test_leaf<uint32_t>(tested->left(), 4, false, false);
}

BOOST_AUTO_TEST_CASE(greater_number_at_right_node)
TEST_CASE("greater_number_at_right_node")
{
auto tested = make_tree<uint32_t>({4, 5});

test_leaf<uint32_t>(tested, 4, false, true);
test_leaf<uint32_t>(tested->right(), 5, false, false);
}

BOOST_AUTO_TEST_CASE(can_create_complex_tree)
TEST_CASE("can_create_complex_tree")
{
auto tested = make_tree<uint32_t>({4, 2, 6, 1, 3, 5, 7});

Expand All @@ -88,38 +88,42 @@ BOOST_AUTO_TEST_CASE(can_create_complex_tree)
template<typename T>
static void test_sort(const tree_ptr<T> &tree, const std::vector<T> &expected)
{
BOOST_REQUIRE_EQUAL_COLLECTIONS(tree->begin(), tree->end(), expected.begin(), expected.end());
std::vector<T> actual;
Comment thread
arcuru marked this conversation as resolved.
for (auto& x : *tree) {
actual.push_back(x);
}
REQUIRE(expected == actual);
}


BOOST_AUTO_TEST_CASE(can_sort_single_number)
TEST_CASE("can_sort_single_number")
{
test_sort(make_tree<uint32_t>({2}), {2});
}

BOOST_AUTO_TEST_CASE(can_sort_if_second_number_is_smaller_than_first)
TEST_CASE("can_sort_if_second_number_is_smaller_than_first")
{
test_sort(make_tree<uint32_t>({2, 1}), {1, 2});
}

BOOST_AUTO_TEST_CASE(can_sort_if_second_number_is_same_as_first)
TEST_CASE("can_sort_if_second_number_is_same_as_first")
{
test_sort(make_tree<uint32_t>({2, 2}), {2, 2});
}

BOOST_AUTO_TEST_CASE(can_sort_if_second_number_is_greater_than_first)
TEST_CASE("can_sort_if_second_number_is_greater_than_first")
{
test_sort(make_tree<uint32_t>({2, 3}), {2, 3});
}

BOOST_AUTO_TEST_CASE(can_sort_complex_tree)
TEST_CASE("can_sort_complex_tree")
{
test_sort(make_tree<uint32_t>({2, 1, 3, 6, 7, 5}), {1, 2, 3, 5, 6, 7});
}

// strings

BOOST_AUTO_TEST_CASE(can_create_complex_tree_strings)
TEST_CASE("can_create_complex_tree_strings")
{
auto tested = make_tree<std::string>({"delicious", "ballon", "flag", "apple", "cat", "elispsis", "grains"});

Expand All @@ -134,7 +138,7 @@ BOOST_AUTO_TEST_CASE(can_create_complex_tree_strings)
test_leaf<std::string>(tested->right()->right(), "grains", false, false);
}

BOOST_AUTO_TEST_CASE(can_sort_complex_tree_strings)
TEST_CASE("can_sort_complex_tree_strings")
{
test_sort(make_tree<std::string>({"A", "few", "random", "strings", "that", "should", "be", "sorted"}), {"A", "be", "few", "random", "should", "sorted", "strings", "that"});
}
Expand Down
Loading