diff options
Diffstat (limited to 'week_1/bagFns1_static/testBag.cxx')
| -rw-r--r-- | week_1/bagFns1_static/testBag.cxx | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/week_1/bagFns1_static/testBag.cxx b/week_1/bagFns1_static/testBag.cxx new file mode 100644 index 0000000..bd42f95 --- /dev/null +++ b/week_1/bagFns1_static/testBag.cxx @@ -0,0 +1,120 @@ +#include <cmath> +#include <functional> +#include <iostream> +#include <sstream> + +#include "bag.h" + +template <typename T> +void test(std::string, std::function<T()>, T, bool = false); + +int main() { + main_savitch_3::bag bag; + main_savitch_3::bag bag_2; + + test<int>( + "size", [&bag]() { return bag.size(); }, 0); + test<int>( + "insert", + [&bag, &bag_2]() { + bag.insert(1); + bag_2.insert(3); + bag_2.insert(3); + bag_2.insert(7); + + return bag.size(); + }, + 1); + test<int>( + "+=", + [&bag, &bag_2]() { + bag += bag_2; + + return bag.size(); + }, + 4); + test<int>( + "count", [&bag]() { return bag.count(3); }, 2); + test<int>( + "erase_one", + [&bag]() { + bag.erase_one(3); + + return bag.count(3); + }, + 1); + test<int>( + "erase", + [&bag]() { + bag.insert(3); + bag.erase(3); + + return bag.count(3); + }, + 0); + test<int>( + "smallest", [&bag]() { return bag.smallest(); }, 1); + test<int>( + "average", [&bag]() { return bag.average(); }, + static_cast<int>((1 + 7) / 2)); + test<int>( + "sort", + [&bag]() { + std::stringstream sort_buffer; + std::streambuf *original_cout_buffer = std::cout.rdbuf(); + std::cout.rdbuf(sort_buffer.rdbuf()); + + bag.insert(3); + bag.sort(); + + std::cout.rdbuf(original_cout_buffer); + + return sort_buffer.str() == "1\n3\n7\n"; + }, + true); + test<int>( + "==", + [&bag]() { + main_savitch_3::bag bag_test; + + bag_test.insert(7); + bag_test.insert(1); + bag_test.insert(3); + + return bag == bag_test; + }, + true); + test<int>( + "==", + [&bag]() { + main_savitch_3::bag bag_test; + + // Slightly different test values that fail, as to prove validity of + // test suite. + // I mark the expected value as `false` for this `test` to treat an + // intentional fail expectation as a pass. + bag_test.insert(7); + bag_test.insert(1); + bag_test.insert(9); + + return bag == bag_test; + }, + false); + // This test is purposefully set to fail as to prove the validity of `test`. + test<int>( + "test", [&bag]() { return bag.smallest(); }, 0, true); + + return 0; +} + +template <typename T> +void test(std::string name, std::function<T()> evaluate, T expected, + bool should_fail) { + T evaluated = evaluate(); + + if (should_fail) + evaluated = !evaluated; + + std::cout << (evaluated == expected ? "pass(" : "FAIL(") << name + << "): expected " << expected << ", got " << evaluated << std::endl; +} |