From f7177f92cf58f35481da7d8d9d146b68b85b5017 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Tue, 10 Jun 2025 02:02:58 +0000 Subject: [PATCH] test(kernel): add tests for std::foward_list and std::is_sorted --- Kernel/tests/stl/foward_list.cpp | 92 ++++++++++++++++++++++++++++++++ Kernel/tests/stl/is_sorted.cpp | 32 +++++++++++ Kernel/tests/stl/stl.cpp | 4 ++ 3 files changed, 128 insertions(+) create mode 100644 Kernel/tests/stl/foward_list.cpp create mode 100644 Kernel/tests/stl/is_sorted.cpp diff --git a/Kernel/tests/stl/foward_list.cpp b/Kernel/tests/stl/foward_list.cpp new file mode 100644 index 00000000..e644e33e --- /dev/null +++ b/Kernel/tests/stl/foward_list.cpp @@ -0,0 +1,92 @@ +/* + This file is part of Fennix Kernel. + + Fennix Kernel is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Kernel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Kernel. If not, see . +*/ + +#ifdef DEBUG + +#include +#include +#include +#include + +void test_stl_forward_list() +{ + debug("Running forward_list tests..."); + + std::forward_list fl1(3, 7); + assert(std::distance(fl1.begin(), fl1.end()) == 3); + for (int v : fl1) + assert(v == 7); + + fl1.assign({1, 2, 3}); + assert(std::distance(fl1.begin(), fl1.end()) == 3); + assert(*fl1.begin() == 1); + + auto alloc = fl1.get_allocator(); + int *test = alloc.allocate(1); + alloc.deallocate(test, 1); + + assert(fl1.front() == 9 || fl1.front() == 1); + + auto before = fl1.before_begin(); + auto begin = fl1.begin(); + auto end = fl1.end(); + assert(std::distance(begin, end) >= 0); + + assert(!fl1.empty()); + assert(fl1.max_size() > 0); + + fl1.insert_after(before, 42); + fl1.emplace_after(fl1.begin(), 99); + + fl1.erase_after(fl1.begin()); + + fl1.emplace_front(88); + fl1.pop_front(); + + fl1.resize(5, -1); + fl1.resize(2); + + std::forward_list fl2 = {100, 200}; + fl1.swap(fl2); + assert(*fl1.begin() == 100); + + std::forward_list a = {1, 3, 5}; + std::forward_list b = {2, 4, 6}; + a.merge(b); + assert(std::is_sorted(a.begin(), a.end())); + + std::forward_list src = {9, 8}; + a.splice_after(a.before_begin(), src); + assert(*a.begin() == 9); + + a.remove(3); + a.remove_if([](int x) + { return x == 5; }); + + a.reverse(); + + a.push_front(2); + a.push_front(2); + a.unique(); + + a.sort(); + assert(std::is_sorted(a.begin(), a.end())); + + debug("All forward_list tests passed."); +} + +#endif diff --git a/Kernel/tests/stl/is_sorted.cpp b/Kernel/tests/stl/is_sorted.cpp new file mode 100644 index 00000000..3765027d --- /dev/null +++ b/Kernel/tests/stl/is_sorted.cpp @@ -0,0 +1,32 @@ +/* + This file is part of Fennix Kernel. + + Fennix Kernel is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Kernel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Kernel. If not, see . +*/ + +#ifdef DEBUG + +#include +#include + +void test_stl_is_sorted() +{ + int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + assert(std::is_sorted(a, a + (sizeof(a) / sizeof(int)))); + + int b[] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9}; + assert(!std::is_sorted(b, b + (sizeof(b) / sizeof(int)))); +} + +#endif diff --git a/Kernel/tests/stl/stl.cpp b/Kernel/tests/stl/stl.cpp index 6a5ffdb1..f62f46df 100644 --- a/Kernel/tests/stl/stl.cpp +++ b/Kernel/tests/stl/stl.cpp @@ -26,6 +26,8 @@ void test_stl_bitset(); void test_stl_string(); void test_stl_unordered_map() {} void test_stl_future(); +void test_stl_forward_list(); +void test_stl_is_sorted(); void test_stl_array(); void test_stl_shared_ptr(); void test_stl_set(); @@ -42,6 +44,8 @@ void Test_stl() test_stl_string(); test_stl_unordered_map(); test_stl_future(); + test_stl_forward_list(); + test_stl_is_sorted(); test_stl_array(); test_stl_shared_ptr(); test_stl_set();