From fb5b3dc16f5c1d27b424cde969aef22affa0e3f7 Mon Sep 17 00:00:00 2001 From: Origin-yy <1657615110@qq.com> Date: Mon, 7 Aug 2023 17:36:43 +0800 Subject: [PATCH 1/2] [2_2] add blackbox_test --- tests/Kernel/Abstractions/blackbox_test.cpp | 102 ++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/Kernel/Abstractions/blackbox_test.cpp diff --git a/tests/Kernel/Abstractions/blackbox_test.cpp b/tests/Kernel/Abstractions/blackbox_test.cpp new file mode 100644 index 000000000..bb693892f --- /dev/null +++ b/tests/Kernel/Abstractions/blackbox_test.cpp @@ -0,0 +1,102 @@ +/** \file blackbox_test.cpp + * \copyright GPLv3 + * \details A unitest for blackbox. + * \author YuanYe + * \date 2023 + */ + +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN + +#include "blackbox.hpp" +#include "doctest/doctest.h" +#include "string.hpp" + +blackbox b0; +blackbox b1 = close_box (-2147483648); // int +blackbox b2 = close_box (2147483647); // unsigned int +blackbox b3 = close_box (-32768); // short +blackbox b4 = close_box (32767); // unsiened short +blackbox b5 = close_box ('`'); // char +blackbox b6 = close_box (255); // unsigned char +blackbox b7 = close_box (LLONG_MAX); // long long +blackbox b8 = close_box (ULLONG_MAX); // unsigned long long +blackbox b9 = close_box (0); // void* +blackbox b10= close_box (3.14); +blackbox b11= close_box (3.14159265); +blackbox b12= close_box (3.1415926535); + +blackbox t0; +blackbox t1 = close_box (0); // int +blackbox t2 = close_box (0); // unsigned int +blackbox t3 = close_box (0); // short +blackbox t4 = close_box (0); // unsiened short +blackbox t5 = close_box (0); // char +blackbox t6 = close_box (0); // unsigned char +blackbox t7 = close_box (0); // long long +blackbox t8 = close_box (0); // unsigned long long +blackbox t9 = close_box (0); // void* +blackbox t10= close_box (0); +blackbox t11= close_box (0); +blackbox t12= close_box (0); + +blackbox t[13] = {t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12}; +blackbox b[13] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12}; +string bout[13]= {"", + "-2147483648\0", + "2147483647", + "-32768\0", + "32767", + "`", + "255", + "9223372036854775807", + "18446744073709551615", + "false", + "3.14", + "3.14159", + "3.14159"}; +string tout[13]= {"", + "-2147483648\n", + "2147483647\t", + "-327680", + "32767 ", + "`\n", + "0", + "9223372036854775807\n", + "18446744073709551615 ", + "0", + "3.1415926", + "3.14159265", + "3.1415926535"}; + +TEST_CASE ("test for get_type()") { + for (int i= 1; i < 13; i++) { + CHECK_EQ (b[i]->get_type () == -13 + i, true); + CHECK_EQ (t[i]->get_type () == -13 + i, true); + } +} + +TEST_CASE ("test for equal(), operator== and operator!=") { + SUBCASE ("test for operator==") { + for (int i= 0; i < 13; i++) { + CHECK_EQ (b[i] == b[i], true); + if (i == 0 || i == 9) CHECK_EQ (b[i] == t[i], true); + else CHECK_EQ (b[i] != t[i], true); + for (int j= 0; j < 13; j++) { + if (i != j) CHECK_EQ (b[i] != b[j], true); + } + } + } +} + +TEST_CASE ("Test fot display() and operator<<") { + tm_ostream out; + string str; + for (int i= 0; i < 13; i++) { + out.buffer (); + out << b[i]; + str= out.unbuffer (); + CHECK_EQ (str != tout[i], true); + if (i != 0) CHECK_EQ (str == bout[i], true); + else CHECK_EQ (str != bout[i], true); + } +} \ No newline at end of file From d93c06091a725e653a496101007721f5ffe2bbbd Mon Sep 17 00:00:00 2001 From: Origin-yy <1657615110@qq.com> Date: Thu, 10 Aug 2023 11:54:07 +0800 Subject: [PATCH 2/2] [2_2] add blackbox_test --- tests/Kernel/Abstractions/blackbox_test.cpp | 87 +++++++++++++-------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/tests/Kernel/Abstractions/blackbox_test.cpp b/tests/Kernel/Abstractions/blackbox_test.cpp index bb693892f..98d83bd9a 100644 --- a/tests/Kernel/Abstractions/blackbox_test.cpp +++ b/tests/Kernel/Abstractions/blackbox_test.cpp @@ -12,32 +12,32 @@ #include "string.hpp" blackbox b0; -blackbox b1 = close_box (-2147483648); // int -blackbox b2 = close_box (2147483647); // unsigned int -blackbox b3 = close_box (-32768); // short -blackbox b4 = close_box (32767); // unsiened short -blackbox b5 = close_box ('`'); // char -blackbox b6 = close_box (255); // unsigned char -blackbox b7 = close_box (LLONG_MAX); // long long -blackbox b8 = close_box (ULLONG_MAX); // unsigned long long -blackbox b9 = close_box (0); // void* +blackbox b1 = close_box (-2147483648); // int +blackbox b2 = close_box (2147483647); // unsigned int +blackbox b3 = close_box (-32768); // short +blackbox b4 = close_box (32767); // unsiened short +blackbox b5 = close_box ('`'); // char +blackbox b6 = close_box (255); // unsigned char +blackbox b7 = close_box (LLONG_MAX); // long long +blackbox b8 = close_box (ULLONG_MAX); // unsigned long long +blackbox b9 = close_box (nullptr); // void* blackbox b10= close_box (3.14); blackbox b11= close_box (3.14159265); blackbox b12= close_box (3.1415926535); blackbox t0; -blackbox t1 = close_box (0); // int -blackbox t2 = close_box (0); // unsigned int -blackbox t3 = close_box (0); // short -blackbox t4 = close_box (0); // unsiened short -blackbox t5 = close_box (0); // char -blackbox t6 = close_box (0); // unsigned char -blackbox t7 = close_box (0); // long long -blackbox t8 = close_box (0); // unsigned long long -blackbox t9 = close_box (0); // void* -blackbox t10= close_box (0); -blackbox t11= close_box (0); -blackbox t12= close_box (0); +blackbox t1 = close_box (1234); // int +blackbox t2 = close_box (5678); // unsigned int +blackbox t3 = close_box (123); // short +blackbox t4 = close_box (234); // unsiened short +blackbox t5 = close_box ('t'); // char +blackbox t6 = close_box (23); // unsigned char +blackbox t7 = close_box (5678); // long long +blackbox t8 = close_box (0); // unsigned long long +blackbox t9 = close_box (nullptr); // void* +blackbox t10= close_box (0.0); +blackbox t11= close_box (8.567); +blackbox t12= close_box (7.345); blackbox t[13] = {t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12}; blackbox b[13] = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12}; @@ -68,22 +68,45 @@ string tout[13]= {"", "3.14159265", "3.1415926535"}; -TEST_CASE ("test for get_type()") { - for (int i= 1; i < 13; i++) { - CHECK_EQ (b[i]->get_type () == -13 + i, true); - CHECK_EQ (t[i]->get_type () == -13 + i, true); - } +TEST_CASE ("test for get_type() and open_box()") { + CHECK (open_box (b1) == -2147483648); + CHECK (open_box (b2) == 2147483647); + CHECK (open_box (b3) == -32768); + CHECK (open_box (b4) == 32767); + CHECK (open_box (b5) == '`'); + CHECK (open_box (b6) == 255); + CHECK (open_box (b7) == LLONG_MAX); + CHECK (open_box (b8) == ULLONG_MAX); + CHECK (open_box (b9) == nullptr); + CHECK (open_box (b10) == (float) 3.14); + CHECK (open_box (b11) == 3.14159265); + CHECK (open_box (b12) == 3.1415926535); + + CHECK (open_box (t1) == 1234); + CHECK (open_box (t2) == 5678); + CHECK (open_box (t3) == 123); + CHECK (open_box (t4) == 234); + CHECK (open_box (t5) == 't'); + CHECK (open_box (t6) == 23); + CHECK (open_box (t7) == 5678); + CHECK (open_box (t8) == 0); + CHECK (open_box (t9) == nullptr); + CHECK (open_box (t10) == 0.0); + CHECK (open_box (t11) == 8.567); + CHECK (open_box (t12) == 7.345); + // If the incoming template type does not match, an exception will be thrown. } TEST_CASE ("test for equal(), operator== and operator!=") { SUBCASE ("test for operator==") { for (int i= 0; i < 13; i++) { - CHECK_EQ (b[i] == b[i], true); - if (i == 0 || i == 9) CHECK_EQ (b[i] == t[i], true); - else CHECK_EQ (b[i] != t[i], true); - for (int j= 0; j < 13; j++) { - if (i != j) CHECK_EQ (b[i] != b[j], true); - } + CHECK_EQ (t[i] == t[i], true); + } + } + + SUBCASE ("test for operator!=") { + for (int i= 0; i < 13; i++) { + if (i != 0 && i != 9) CHECK_EQ (b[i] != t[i], true); } } }