test(kernel): add tests for std::foward_list and std::is_sorted

This commit is contained in:
2025-06-10 02:02:58 +00:00
parent 6a6c3bfc67
commit f7177f92cf
3 changed files with 128 additions and 0 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifdef DEBUG
#include <foward_list>
#include <iostream>
#include <cassert>
#include <algorithm>
void test_stl_forward_list()
{
debug("Running forward_list tests...");
std::forward_list<int> 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<int> fl2 = {100, 200};
fl1.swap(fl2);
assert(*fl1.begin() == 100);
std::forward_list<int> a = {1, 3, 5};
std::forward_list<int> b = {2, 4, 6};
a.merge(b);
assert(std::is_sorted(a.begin(), a.end()));
std::forward_list<int> 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

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifdef DEBUG
#include <assert.h>
#include <algorithm>
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

View File

@ -26,6 +26,8 @@ void test_stl_bitset();
void test_stl_string(); void test_stl_string();
void test_stl_unordered_map() {} void test_stl_unordered_map() {}
void test_stl_future(); void test_stl_future();
void test_stl_forward_list();
void test_stl_is_sorted();
void test_stl_array(); void test_stl_array();
void test_stl_shared_ptr(); void test_stl_shared_ptr();
void test_stl_set(); void test_stl_set();
@ -42,6 +44,8 @@ void Test_stl()
test_stl_string(); test_stl_string();
test_stl_unordered_map(); test_stl_unordered_map();
test_stl_future(); test_stl_future();
test_stl_forward_list();
test_stl_is_sorted();
test_stl_array(); test_stl_array();
test_stl_shared_ptr(); test_stl_shared_ptr();
test_stl_set(); test_stl_set();