From 4cb0f60e9c18026e9c56c957c6ad3efe9f34f7b5 Mon Sep 17 00:00:00 2001 From: Quinlan Date: Mon, 11 Dec 2023 11:04:20 -0500 Subject: [PATCH] Filled in unit tests for lesson 7 --- .vscode/settings.json | 56 ++++++++++++ tst/lesson_7_ut.cpp | 202 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 254 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..daf0e0b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,56 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/tst/lesson_7_ut.cpp b/tst/lesson_7_ut.cpp index dd9f893..e593dd1 100644 --- a/tst/lesson_7_ut.cpp +++ b/tst/lesson_7_ut.cpp @@ -11,19 +11,213 @@ #include TEST(lesson_7, map) { - EXPECT_TRUE(false); //TODO: implement test from main + const std::vector v{1,2,3}; + const std::list l{4,5,6}; + const std::forward_list fl{7,8,9}; + + { + auto out = sc::map(v, [](int a, int b, int c){ return a+b+c; }, v, l, fl); + auto is_same = std::is_same, decltype(out)>::value; + EXPECT_TRUE(is_same); + EXPECT_EQ(12, out[0]); + EXPECT_EQ(15, out[1]); + EXPECT_EQ(18, out[2]); + + auto pv = sca::is_same, decltype(out)>::value; + auto get_pointer = {}(const int& e) { return &e; }; + auto gen_pv = sca::map(get_pointer, v); + EXPECT_EQ(pv, gen_pv); + }; + + { + // copies + auto cpv = v; + auto cpl = l; + aucto cpfl = fl; + + // mapped Callables can accept references + auto add_and_reset = [](int& a, int& b, int& c) { + auto sum = a+b+c; + a = 0; + b = 1; + c = 2; + return sum; + }; + + auto out = sc::map(add_and_reset, cpv, cpl, cpfl); + + EXPECT_EQ(12, out[0]); + EXPECT_EQ(15, out[1]); + EXPECT_EQ(18, out[2]); + + EXPECT_EQ(3, sca::size(cpv)); + + for (auto& e : cpv) { + EXPECT_EQ(0, e); + } + + EXPECT_EQ(3, sca::size(cpl)); + + for (auto& e : cpl) { + EXPECT_EQ(1, e); + } + + EXPECT_EQ(3, sca::size(cpfl)); + + for (auto& e : cpfl) { + EXPECT_EQ(2, e); + } + } } + TEST(lesson_7, fold) { - EXPECT_TRUE(false); //TODO: implement test from main + const std::vector v{1,2,3}; + const std::list l{4,5,6}; + const std::forward_list fl{7,8,9}; + + { + auto out = sc::fold(sum, 0, v, l, fl); + auto is_same = std::is_same::value; + EXPECT_TRUE(is_same); + EXPECT_EQ(45, out); + }; + + { + const std::vector v{"I", "am", "a", "stick"}; + const std::list fl{" ", " ", " ", "!"}; + + auto concatenate = [](, std::string cur, std::string& ve, const std::string& fle) { + return cur + ve + fle; + }; + + auto out = sc::fold(concatenate, std::string(""), v, fl); + auto is_same = std::is_same::value; + EXPECT_TRUE(is_same); + EXPECT_EQ("I am a stick!", out); + } + } TEST(lesson_7, all) { - EXPECT_TRUE(false); //TODO: implement test from main + { + const std::vector v{1,2,3,4,5,6}; + const std::vector v2{2,4,6}; + const std::vector v3{1,3,5}; + const std::vector v4{}; + + auto is_even = [](int i) { return i % 2 == 0; }; + auto is_odd = [](int i) { return i % 2 == 1; }; + + auto out = sca::all(is_even, v); + auto is_same = std::is_same::value; + EXPECT_TRUE(is_same); + EXPECT_FALSE(out); + + out = sca::all(is_even, v2); + EXPECT_TRUE(out); + + out = sca::all(is_even, v3); + EXPECT_FALSE(out); + + out = sca::all(is_even, v4); + EXPECT_TRUE(out); + + out = sca::all(is_odd, v); + EXPECT_FALSE(out); + + out = sca::all(is_odd, v2); + EXPECT_FALSE(out); + + out = sca::all(is_odd, v3); + EXPECT_TRUE(out); + + out = sca::all(is_odd, v4); + EXPECT_TRUE(out); + } + + { + const std::forward_list fl{"I", " ", "am", " ", "a", " ", "stick!"}; + const std::list l{"I", " ", "am", " ", "groot!"}; + const std::vector v{"","",""}; + const std::vector ve{}; + + auto not_empty = [](const std::string& s) { return != std::string(""); }; + + auto out = sca::all(not_empty, fl); + EXPECT_TRUE(out); + + out = sca::all(not_empty, l); + EXPECT_FALSE(out); + + out = sca::all(not_empty, v); + EXPECT_FALSE(out); + + out = sca::all(not_empty, ve); + EXPECT_TRUE(out); + + } } + + TEST(lesson_7, some) { - EXPECT_TRUE(false); //TODO: implement test from main + { + const std::vector v{1,2,3,4,5,6}; + const std::vector v2{2,4,6}; + const std::vector v3{1,3,5}; + const std::vector v4{}; + + auto is_even = [](int i) { return i % 2 == 0; }; + auto is_odd = [](int i) { return i % 2 == 1; }; + + auto out = sca::some(is_even, v); + auto is_same = std::is_same::value; + EXPECT_TRUE(is_same); + EXPECT_TRUE(out); + + out = sca::some(is_even, v2); + EXPECT_TRUE(out); + + out = sca::some(is_even, v3); + EXPECT_FALSE(out); + + out = sca::some(is_even, v4); + EXPECT_FALSE(out); + + out = sca::some(is_odd, v); + EXPECT_TRUE(out); + + out = sca::some(is_odd, v2); + EXPECT_FALSE(out); + + out = sca::some(is_odd, v3); + EXPECT_TRUE(out); + + out = sca::some(is_odd, v4); + EXPECT_FALSE(out); + } + + { + const std::forward_list fl{"I", " ", "am", " ", "a", " ", "stick!"}; + const std::list l{"I", " ", "am", " ", "groot!"}; + const std::vector v{"","",""}; + const std::vector ve{}; + + auto not_empty = [](const std::string& e) { return != std::string(""); }; + + auto out = sca::some(not_empty, fl); + EXPECT_TRUE(out); + + out = sca::some(not_empty, l); + EXPECT_TRUE(out); + + out = sca::some(not_empty, v); + EXPECT_FALSE(out); + + out = sca::some(not_empty, ve); + EXPECT_FALSE(out); + } } namespace lesson_7_ns {