]> git.lizzy.rs Git - irrlicht.git/commitdiff
Unit tests for irrArray (#103)
authorparadust7 <102263465+paradust7@users.noreply.github.com>
Sat, 7 May 2022 09:21:41 +0000 (02:21 -0700)
committerGitHub <noreply@github.com>
Sat, 7 May 2022 09:21:41 +0000 (11:21 +0200)
examples/AutomatedTest/main.cpp
examples/AutomatedTest/test_array.cpp [new file with mode: 0644]
examples/AutomatedTest/test_helper.h [new file with mode: 0644]
examples/CMakeLists.txt

index 0966892cc7e8aa4ede1b9985806af2c2c26e4cb3..2f8072266211ba2631b5f5b2034fff136988997e 100644 (file)
@@ -1,3 +1,4 @@
+#include <iostream>\r
 #include <irrlicht.h>\r
 #include "exampleHelper.h"\r
 \r
@@ -6,6 +7,8 @@ using namespace irr;
 static IrrlichtDevice *device = nullptr;\r
 static int test_fail = 0;\r
 \r
+extern void test_irr_array();\r
+\r
 static video::E_DRIVER_TYPE chooseDriver(const char *arg_)\r
 {\r
        if (core::stringc(arg_) == "null")\r
@@ -27,8 +30,15 @@ static inline void check(bool ok, const char *msg)
        }\r
 }\r
 \r
+void run_unit_tests() {\r
+       std::cout << "Running unit tests:" << std::endl;\r
+       test_irr_array();\r
+}\r
+\r
 int main(int argc, char *argv[])\r
 {\r
+       run_unit_tests();\r
+\r
        SIrrlichtCreationParameters p;\r
        p.DriverType = chooseDriver(argc > 1 ? argv[1] : "");\r
        p.WindowSize = core::dimension2du(640, 480);\r
diff --git a/examples/AutomatedTest/test_array.cpp b/examples/AutomatedTest/test_array.cpp
new file mode 100644 (file)
index 0000000..5ec55ee
--- /dev/null
@@ -0,0 +1,135 @@
+#include "irrArray.h"
+#include "test_helper.h"
+
+using namespace irr;
+using core::array;
+
+static void test_basics() {
+       array<int> v;
+       v.push_back(1);  // 1
+       v.push_front(2); // 2, 1
+       v.insert(4, 0);  // 4, 2, 1
+       v.insert(3, 1);  // 4, 3, 2, 1
+       v.insert(0, 4);  // 4, 3, 2, 1, 0
+       UASSERTEQ(v.size(), 5);
+       UASSERTEQ(v[0], 4);
+       UASSERTEQ(v[1], 3);
+       UASSERTEQ(v[2], 2);
+       UASSERTEQ(v[3], 1);
+       UASSERTEQ(v[4], 0);
+       array<int> w = v;
+       UASSERTEQ(w.size(), 5);
+       UASSERT(w == v);
+       w.clear();
+       UASSERTEQ(w.size(), 0);
+       UASSERTEQ(w.allocated_size(), 0);
+       UASSERT(w.empty());
+       w = v;
+       UASSERTEQ(w.size(), 5);
+       w.set_used(3);
+       UASSERTEQ(w.size(), 3);
+       UASSERTEQ(w[0], 4);
+       UASSERTEQ(w[1], 3);
+       UASSERTEQ(w[2], 2);
+       UASSERTEQ(w.getLast(), 2);
+       w.set_used(20);
+       UASSERTEQ(w.size(), 20);
+       w = v;
+       w.sort();
+       UASSERTEQ(w.size(), 5);
+       UASSERTEQ(w[0], 0);
+       UASSERTEQ(w[1], 1);
+       UASSERTEQ(w[2], 2);
+       UASSERTEQ(w[3], 3);
+       UASSERTEQ(w[4], 4);
+       w.erase(0);
+       UASSERTEQ(w.size(), 4);
+       UASSERTEQ(w[0], 1);
+       UASSERTEQ(w[1], 2);
+       UASSERTEQ(w[2], 3);
+       UASSERTEQ(w[3], 4);
+       w.erase(1, 2);
+       UASSERTEQ(w.size(), 2);
+       UASSERTEQ(w[0], 1);
+       UASSERTEQ(w[1], 4);
+       w.swap(v);
+       UASSERTEQ(w.size(), 5);
+       UASSERTEQ(v.size(), 2);
+}
+
+static void test_linear_searches() {
+       // Populate the array with 0, 1, 2, ..., 100, 100, 99, 98, 97, ..., 0
+       array<int> arr;
+       for (int i = 0; i <= 100; i++)
+               arr.push_back(i);
+       for (int i = 100; i >= 0; i--)
+               arr.push_back(i);
+       s32 end = arr.size() - 1;
+       for (int i = 0; i <= 100; i++) {
+               s32 index = arr.linear_reverse_search(i);
+               UASSERTEQ(index, end - i);
+       }
+       for (int i = 0; i <= 100; i++) {
+               s32 index = arr.linear_search(i);
+               UASSERTEQ(index, i);
+       }
+}
+
+static void test_binary_searches() {
+       const auto& values = { 3, 5, 1, 2, 5, 10, 19, 9, 7, 1, 2, 5, 8, 15 };
+       array<int> arr;
+       for (int value : values) {
+               arr.push_back(value);
+       }
+       // Test the const form first, it uses a linear search without sorting
+       const array<int> & carr = arr;
+       UASSERTEQ(carr.binary_search(20), -1);
+       UASSERTEQ(carr.binary_search(0), -1);
+       UASSERTEQ(carr.binary_search(1), 2);
+
+       // Sorted: 1, 1, 2, 2, 3, 5, 5, 5, 7, 8, 9, 10, 15, 19
+       UASSERTEQ(arr.binary_search(20), -1);
+       UASSERTEQ(arr.binary_search(0), -1);
+
+       for (int value : values) {
+               s32 i = arr.binary_search(value);
+               UASSERTNE(i, -1);
+               UASSERTEQ(arr[i], value);
+       }
+
+       s32 first, last;
+       first = arr.binary_search_multi(1, last);
+       UASSERTEQ(first, 0);
+       UASSERTEQ(last, 1);
+
+       first = arr.binary_search_multi(2, last);
+       UASSERTEQ(first, 2);
+       UASSERTEQ(last, 3);
+
+       first = arr.binary_search_multi(3, last);
+       UASSERTEQ(first, 4);
+       UASSERTEQ(last, 4);
+
+       first = arr.binary_search_multi(4, last);
+       UASSERTEQ(first, -1);
+
+       first = arr.binary_search_multi(5, last);
+       UASSERTEQ(first, 5);
+       UASSERTEQ(last, 7);
+
+       first = arr.binary_search_multi(7, last);
+       UASSERTEQ(first, 8);
+       UASSERTEQ(last, 8);
+
+       first = arr.binary_search_multi(19, last);
+       UASSERTEQ(first, 13);
+       UASSERTEQ(last, 13);
+}
+
+void test_irr_array()
+{
+       test_basics();
+       test_linear_searches();
+       test_binary_searches();
+       std::cout << "    test_irr_array PASSED" << std::endl;
+}
diff --git a/examples/AutomatedTest/test_helper.h b/examples/AutomatedTest/test_helper.h
new file mode 100644 (file)
index 0000000..9774cdd
--- /dev/null
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <exception>
+#include <iostream>
+
+class TestFailedException : public std::exception {
+};
+
+// Asserts the comparison specified by CMP is true, or fails the current unit test
+#define UASSERTCMP(CMP, actual, expected) do {                                        \
+       const auto &a = (actual);                                                     \
+       const auto &e = (expected);                                                   \
+       if (!CMP(a, e)) {                                                             \
+               std::cout                                                             \
+                       << "Test assertion failed: " << #actual << " " << #CMP << " " \
+                       << #expected << std::endl                                     \
+                       << "    at " << __FILE__ << ":" << __LINE__ << std::endl      \
+                       << "    actual:   " << a << std::endl << "    expected: "     \
+                       << e << std::endl;                                            \
+               throw TestFailedException();                                          \
+       }                                                                             \
+} while (0)
+
+#define CMPEQ(a, e)    (a == e)
+#define CMPTRUE(a, e)  (a)
+#define CMPNE(a, e)    (a != e)
+
+#define UASSERTEQ(actual, expected)  UASSERTCMP(CMPEQ, actual, expected)
+#define UASSERTNE(actual, nexpected) UASSERTCMP(CMPNE, actual, nexpected)
+#define UASSERT(actual)              UASSERTCMP(CMPTRUE, actual, true)
index 9aab740f32bc1f3f44f4f6d2f2992c11560f4b8a..7f00e5e53f5ea92a18182d5c781beba9a49a0c37 100644 (file)
@@ -1,3 +1,5 @@
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 set(IRREXAMPLES
        # removed