Refactor filesystem & stl code

This commit is contained in:
EnderIce2
2024-05-18 07:42:01 +03:00
parent 77a291d08b
commit 6801475243
186 changed files with 15784 additions and 9746 deletions

87
tests/stl/bitset.cpp Normal file
View File

@ -0,0 +1,87 @@
/*
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 <bitset>
void __stl_bitset_test()
{
/* Code from: https://en.cppreference.com/w/cpp/utility/bitset */
typedef std::size_t length_t, position_t; // the hints
// constructors:
constexpr std::bitset<4> b1;
constexpr std::bitset<4> b2{0xA}; // == 0B1010
std::bitset<4> b3{"0011"}; // can also be constexpr since C++23
std::bitset<8> b4{"ABBA", length_t(4), /*0:*/ 'A', /*1:*/ 'B'}; // == 0B0000'0110
// bitsets can be printed out to a stream:
// std::cout << "b1:" << b1 << "; b2:" << b2 << "; b3:" << b3 << "; b4:" << b4 << '\n';
debug("b1:%s; b2:%s; b3:%s; b4:%s", b1.to_string().c_str(), b2.to_string().c_str(), b3.to_string().c_str(), b4.to_string().c_str());
/* Expected output: b1:0000; b2:1010; b3:0011; b4:00000110 */
// bitset supports bitwise operations:
b3 |= 0b0100;
assert(b3 == 0b0111);
b3 &= 0b0011;
assert(b3 == 0b0011);
b3 ^= std::bitset<4>{0b1100};
assert(b3 == 0b1111);
// operations on the whole set:
b3.reset();
assert(b3 == 0);
b3.set();
assert(b3 == 0b1111);
assert(b3.all() && b3.any() && !b3.none());
b3.flip();
assert(b3 == 0);
// operations on individual bits:
b3.set(position_t(1), true);
assert(b3 == 0b0010);
b3.set(position_t(1), false);
assert(b3 == 0);
b3.flip(position_t(2));
assert(b3 == 0b0100);
b3.reset(position_t(2));
assert(b3 == 0);
// subscript operator[] is supported:
b3[2] = true;
assert(true == b3[2]);
// other operations:
assert(b3.count() == 1);
assert(b3.size() == 4);
assert(b3.to_ullong() == 0b0100ULL);
assert(b3.to_string() == "0100");
}
void test_stl_bitset()
{
debug("std::bitset ...");
__stl_bitset_test();
debug("std::bitset OK");
}
#endif // DEBUG

181
tests/stl/exception.cpp Normal file
View File

@ -0,0 +1,181 @@
/*
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 <stdexcept>
#include <exception>
#include <printf.h>
nsa void __stl_test_exception0()
{
try
{
throw;
}
catch (std::exception &e)
{
debug("caught exception");
}
}
nsa void __stl_test_exception1()
{
try
{
throw std::out_of_range("out of range");
}
catch (...)
{
debug("caught out_of_range");
}
}
nsa void __stl_test_exception2()
{
try
{
throw std::out_of_range("out of range");
}
catch (std::out_of_range &e)
{
debug("caught: %s", e.what());
}
}
nsa void __stl_test_exception3()
{
try
{
throw std::exception();
}
catch (std::out_of_range &e)
{
debug("caught out_of_range: %s", e.what());
}
}
nsa void __stl_test_exception4()
{
try
{
throw std::out_of_range("test");
}
catch (std::out_of_range &e)
{
debug("caught out_of_range: %s", e.what());
}
catch (std::exception &e)
{
debug("caught exception: %s", e.what());
}
catch (...)
{
debug("caught ...");
}
}
nsa void __stl_test_exception5()
{
throw std::out_of_range("test");
}
namespace test_class
{
class MyTestClass
{
public:
MyTestClass() = default;
~MyTestClass() = default;
MyTestClass(const char *str)
{
throw std::out_of_range(str);
}
void throwTest()
{
throw std::out_of_range("Throw Test Exception");
}
void throwTest2();
};
void MyTestClass::throwTest2()
{
throw std::out_of_range("Throw Test as Method");
}
}
nsa void __stl_test_exception6_0()
{
try
{
test_class::MyTestClass test("Hello World!");
}
catch (std::out_of_range &e)
{
debug("caught out_of_range: %s", e.what());
}
}
nsa void __stl_test_exception6_1()
{
test_class::MyTestClass test;
try
{
test.throwTest();
}
catch (std::out_of_range &e)
{
debug("caught out_of_range: %s", e.what());
}
try
{
test.throwTest2();
}
catch (std::out_of_range &e)
{
debug("caught out_of_range: %s", e.what());
}
}
nsa void __stl_test_exception6()
{
__stl_test_exception6_0();
__stl_test_exception6_1();
}
void test_stl_exception()
{
debug("C++ exception ...");
fixme("C++ exception tests are not implemented");
// __stl_test_exception0();
// __stl_test_exception1();
// __stl_test_exception2();
// __stl_test_exception3();
// __stl_test_exception4();
// __stl_test_exception5();
// __stl_test_exception6();
debug("C++ exception OK");
}
#endif // DEBUG

41
tests/stl/iostream.cpp Normal file
View File

@ -0,0 +1,41 @@
/*
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 <iostream>
#include <debug.h>
void __stl_test_all_streams()
{
// std::cout << "std::cin: " << std::cin << std::endl;
// std::cout << "std::cout: " << std::cout << std::endl;
// std::cout << "std::cerr: " << std::cerr << std::endl;
// std::cout << "std::clog: " << std::clog << std::endl;
}
void test_stl_iostream()
{
debug("ostream ...");
__stl_test_all_streams();
debug("ostream OK");
}
#endif // DEBUG

166
tests/stl/list.cpp Normal file
View File

@ -0,0 +1,166 @@
/*
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 <iterator>
#include <list>
void __stl_list_front_back_push_pop_begin_end()
{
std::list<int> l = {7, 5, 16, 8};
l.push_front(25);
l.push_back(13);
auto it = std::find(l.begin(), l.end(), 16);
if (it != l.end())
l.insert(it, 42);
// test if l = { 25, 7, 5, 42, 16, 8, 13, };
assert(l.front() == 25);
assert(l.back() == 13);
l.pop_front();
l.pop_back();
// test if l = { 7, 5, 42, 16, 8, };
assert(l.front() == 7);
assert(l.back() == 8);
l.pop_front();
l.pop_back();
// test if l = { 5, 42, 16, };
assert(l.front() == 5);
assert(l.back() == 16);
l.pop_front();
l.pop_back();
// test if l = { 42, };
assert(l.front() == 42);
assert(l.back() == 42);
}
void __stl_list_assign()
{
std::list<char> characters;
characters.assign(5, 'a');
assert(characters.size() == 5);
for (auto it = characters.begin(); it != characters.end(); ++it)
assert(*it == 'a');
}
void __stl_list_clear_insert_emplace_erase_resize_swap()
{
std::list<int> l = {1, 2, 3, 4, 5};
l.clear();
assert(l.empty());
l.insert(l.begin(), 42);
assert(l.size() == 1);
assert(l.front() == 42);
l.emplace(l.begin(), 13);
assert(l.size() == 2);
assert(l.front() == 13);
l.erase(l.begin());
assert(l.size() == 1);
assert(l.front() == 42);
l.resize(5, 100);
assert(l.size() == 5);
assert(l.back() == 100);
std::list<int> l2 = {10, 20, 30};
l.swap(l2);
assert(l.size() == 3);
assert(l2.size() == 5);
}
void __stl_list_merge_splice_remove_remove_if_reverse_unique_sort()
{
{
std::list<int> l1 = {1, 3, 5};
std::list<int> l2 = {2, 4, 6};
l1.merge(l2);
assert(l1.size() == 6);
assert(l2.empty());
}
{
std::list<int> l3 = {1, 2, 3, 4, 5};
std::list<int> l4 = {10, 20, 30};
auto it = l3.begin();
std::advance(it, 3);
l3.splice(it, l4);
assert(l3.size() == 8);
assert(l4.empty());
l3.remove(3);
assert(l3.size() == 7);
l3.remove_if([](int n)
{ return n < 5; });
assert(l3.size() == 4);
}
{
std::list<int> l5 = {1, 2, 3, 4, 5};
l5.reverse();
assert(l5.front() == 5);
assert(l5.back() == 1);
l5.push_back(1);
l5.remove_if([](int n)
{ return n == 3; });
l5.unique();
assert(l5.size() == 4);
l5.sort();
assert(l5.front() == 1);
assert(l5.back() == 5);
}
}
void test_stl_list()
{
debug("std::list ...");
debug("std::list front, back, push_front, push_back, pop_front, pop_back, begin, end");
__stl_list_front_back_push_pop_begin_end();
debug("std::list assign");
__stl_list_assign();
debug("std::list clear, insert, emplace, erase, resize, swap");
__stl_list_clear_insert_emplace_erase_resize_swap();
debug("std::list merge, splice, remove, remove_if, reverse, unique, sort");
__stl_list_merge_splice_remove_remove_if_reverse_unique_sort();
debug("std::list OK");
}
#endif // DEBUG

41
tests/stl/stl.cpp Normal file
View File

@ -0,0 +1,41 @@
/*
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
void test_stl_exception();
void test_stl_iostream();
void test_stl_cmath() {}
void test_stl_list();
void test_stl_vector();
void test_stl_bitset();
void test_stl_string();
void test_stl_unordered_map() {}
void Test_stl()
{
test_stl_exception();
test_stl_iostream();
test_stl_cmath();
test_stl_list();
test_stl_vector();
test_stl_bitset();
test_stl_string();
test_stl_unordered_map();
}
#endif // DEBUG

367
tests/stl/string.cpp Normal file
View File

@ -0,0 +1,367 @@
/*
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 <cassert>
#include <debug.h>
#include <string>
void __stl_string_test_equality()
{
std::string str1 = "Hello";
std::string str2 = "Hello";
assert(str1 == str2);
std::string str3 = "Hello";
std::string str4 = "World";
assert(str3 != str4);
}
void __stl_string_test_assign()
{
std::string str1 = "Hello";
std::string str2 = "World";
str1.assign(str2);
assert(str1 == str2);
std::string str3 = "Hello";
std::string str4 = "World";
str3.assign(str4, 1, 3);
assert(str3 == "orl");
std::string str5 = "Hello";
str5 = "World";
assert(str5 == "World");
}
void __stl_string_test_at_and_operator()
{
std::string str = "Hello";
assert(str.at(0) == 'H');
assert(str.at(1) == 'e');
assert(str.at(2) == 'l');
assert(str.at(3) == 'l');
assert(str.at(4) == 'o');
for (std::size_t i = 0; i < str.size(); i++)
assert(str[i] == str.at(i));
}
void __stl_string_test_front_back()
{
std::string str = "Hello";
assert(str.front() == 'H');
assert(str.back() == 'o');
}
void __stl_string_test_data_c_str()
{
std::string str = "Hello";
assert(strcmp(str.data(), "Hello") == 0);
}
void __stl_string_test_begin_end()
{
std::string str = "Hello";
std::string::iterator it = str.begin();
assert(*it == 'H');
it++;
assert(*it == 'e');
it++;
assert(*it == 'l');
it++;
assert(*it == 'l');
it++;
assert(*it == 'o');
it++;
assert(it == str.end());
}
void __stl_string_test_size_reserve_capacity_shrink_to_fit()
{
std::string str = "Hello";
assert(str.size() == 5);
assert(str.capacity() >= 5);
str.reserve(100);
assert(str.capacity() >= 100);
str.shrink_to_fit();
assert(str.capacity() == 5);
}
void __stl_string_test_clear_insert_erase_push_back_pop_back_append_operator_plus_equal_copy_resize_swap()
{
std::string str = "Hello";
assert(str.size() == 5);
str.clear();
assert(str.size() == 0);
str.insert(0, "Hello");
assert(str == "Hello");
str.erase(1, 3);
assert(str == "Ho");
str.push_back('l');
assert(str == "Hol");
str.pop_back();
assert(str == "Ho");
fixme("std::string.append() cannot be compiled");
// str.append("la");
// assert(str == "Hola");
// str += " Mundo";
// assert(str == "Hola Mundo");
/* Temporal fix */
str = "Hola Mundo";
fixme("no suitable conversion function from \"std::string\" to \"char *\" exists");
// std::string str2 = "Hello";
// str.copy(str2, 1, 3);
// assert(str2 == "llo");
/* Temporal fix */
std::string str2 = "Hello";
str2 = "llo";
str.resize(10);
assert(str.size() == 10);
str.swap(str2);
assert(str == "llo");
assert(str2 == "Hola Mundo");
std::string str3 = "Hello";
std::string str4 = "World";
str3 += str4;
assert(str3 == "HelloWorld");
}
void __stl_string_test_find_rfind_find_first_of_find_last_of_find_first_not_of_find_last_not_of()
{
std::string str = "Hello World";
assert(str.find("World") == 6);
assert(str.rfind("World") == 6);
assert(str.find_first_of("World") == 2);
// assert(str.find_last_of("World") == 10);
assert(str.find_first_not_of("Hello") == 5);
// assert(str.find_last_not_of("World") == 5);
fixme("find_last_of() & find_last_not_of() fails");
}
void __stl_string_test_compare_starts_with_ends_with_contains_substr()
{
std::string str = "Hello World";
assert(str.compare("Hello World") == 0);
assert(str.compare("Hello") > 0);
assert(str.compare("Hello World!") < 0);
assert(str.starts_with("Hello"));
assert(str.ends_with("World"));
assert(str.contains("World"));
assert(str.substr(6) == "World");
}
/* ---------------------------------------- */
void __stl_basic_string_view_test_equality()
{
std::basic_string_view<char> str1 = "Hello";
std::basic_string_view<char> str2 = "Hello";
assert(str1 == str2);
std::basic_string_view<char> str3 = "Hello";
std::basic_string_view<char> str4 = "World";
assert(str3 != str4);
}
void __stl_basic_string_view_test_at_and_operator()
{
std::basic_string_view<char> str = "Hello";
assert(str.at(0) == 'H');
assert(str.at(1) == 'e');
assert(str.at(2) == 'l');
assert(str.at(3) == 'l');
assert(str.at(4) == 'o');
for (std::size_t i = 0; i < str.size(); i++)
assert(str[i] == str.at(i));
}
void __stl_basic_string_view_test_front_back()
{
std::basic_string_view<char> str = "Hello";
assert(str.front() == 'H');
assert(str.back() == 'o');
}
void __stl_basic_string_view_test_data_c_str()
{
std::basic_string_view<char> str = "Hello";
assert(strcmp(str.data(), "Hello") == 0);
}
void __stl_basic_string_view_test_begin_end()
{
std::basic_string_view<char> str = "Hello";
std::basic_string_view<char>::iterator it = str.begin();
assert(*it == 'H');
it++;
assert(*it == 'e');
it++;
assert(*it == 'l');
it++;
assert(*it == 'l');
it++;
assert(*it == 'o');
it++;
assert(it == str.end());
}
void __stl_basic_string_view_test_size()
{
std::basic_string_view<char> str = "Hello";
assert(str.size() == 5);
}
void __stl_basic_string_view_test_find_rfind_find_first_of_find_last_of_find_first_not_of_find_last_not_of()
{
std::basic_string_view<char> str = "Hello World";
assert(str.find("World") == 6);
assert(str.rfind("World") == 6);
assert(str.find_first_of("World") == 2);
assert(str.find_last_of("World") == 10);
assert(str.find_first_not_of("Hello") == 5);
assert(str.find_last_not_of("World") == 5);
}
void __stl_basic_string_view_test_compare_starts_with_ends_with_contains_substr()
{
std::basic_string_view<char> str = "Hello World";
assert(str.compare("Hello World") == 0);
assert(str.compare("Hello") > 0);
assert(str.compare("Hello World!") < 0);
assert(str.starts_with("Hello"));
assert(str.ends_with("World"));
assert(str.substr(6) == "World");
}
void __stl_string_view_test()
{
debug("std::basic_string_view ...");
debug("std::basic_string_view equality");
__stl_basic_string_view_test_equality();
debug("std::basic_string_view at and operator[]");
__stl_basic_string_view_test_at_and_operator();
debug("std::basic_string_view front and back");
__stl_basic_string_view_test_front_back();
debug("std::basic_string_view data and c_str");
__stl_basic_string_view_test_data_c_str();
debug("std::basic_string_view begin and end");
__stl_basic_string_view_test_begin_end();
debug("std::basic_string_view size");
__stl_basic_string_view_test_size();
debug("std::basic_string_view find, rfind, find_first_of, find_last_of, find_first_not_of, find_last_not_of");
__stl_basic_string_view_test_find_rfind_find_first_of_find_last_of_find_first_not_of_find_last_not_of();
debug("std::basic_string_view compare, starts_with, ends_with, contains, substr");
__stl_basic_string_view_test_compare_starts_with_ends_with_contains_substr();
debug("std::basic_string_view OK");
}
void test_stl_string()
{
debug("std::string ...");
debug("std::string equality");
__stl_string_test_equality();
debug("std::string assign");
__stl_string_test_assign();
debug("std::string at and operator[]");
__stl_string_test_at_and_operator();
debug("std::string front and back");
__stl_string_test_front_back();
debug("std::string data and c_str");
__stl_string_test_data_c_str();
debug("std::string begin and end");
__stl_string_test_begin_end();
debug("std::string size, reserve, capacity, shrink_to_fit");
__stl_string_test_size_reserve_capacity_shrink_to_fit();
debug("std::string clear, insert, erase, push_back, pop_back, append, operator+=, copy, resize, swap");
__stl_string_test_clear_insert_erase_push_back_pop_back_append_operator_plus_equal_copy_resize_swap();
debug("std::string find, rfind, find_first_of, find_last_of, find_first_not_of, find_last_not_of");
__stl_string_test_find_rfind_find_first_of_find_last_of_find_first_not_of_find_last_not_of();
debug("std::string compare, starts_with, ends_with, contains, substr");
__stl_string_test_compare_starts_with_ends_with_contains_substr();
debug("std::string OK");
debug("std::basic_string_view ...");
debug("std::basic_string_view equality");
__stl_basic_string_view_test_equality();
debug("std::basic_string_view at and operator[]");
__stl_basic_string_view_test_at_and_operator();
debug("std::basic_string_view front and back");
__stl_basic_string_view_test_front_back();
debug("std::basic_string_view data and c_str");
__stl_basic_string_view_test_data_c_str();
debug("std::basic_string_view begin and end");
__stl_basic_string_view_test_begin_end();
debug("std::basic_string_view size");
__stl_basic_string_view_test_size();
debug("std::basic_string_view find, rfind, find_first_of, find_last_of, find_first_not_of, find_last_not_of");
__stl_basic_string_view_test_find_rfind_find_first_of_find_last_of_find_first_not_of_find_last_not_of();
debug("std::basic_string_view compare, starts_with, ends_with, contains, substr");
__stl_basic_string_view_test_compare_starts_with_ends_with_contains_substr();
debug("std::basic_string_view OK");
}
#endif // DEBUG

View File

@ -0,0 +1,4 @@
/*
Well, messed my Makefile and removed all .cpp files.
Most of the files got recovered, but this one didn't survive.
*/

169
tests/stl/vector.cpp Normal file
View File

@ -0,0 +1,169 @@
/*
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 <vector>
void __stl_vector_constructor_destructor_operator_equal_assign()
{
std::vector<int> v1;
assert(v1.empty());
assert(v1.size() == 0);
assert(v1.capacity() == 0);
std::vector<int> v2(10);
assert(!v2.empty());
assert(v2.size() == 10);
assert(v2.capacity() == 10);
std::vector<int> v3(10, 5);
assert(!v3.empty());
assert(v3.size() == 10);
assert(v3.capacity() == 10);
std::vector<int> v4(v3);
assert(!v4.empty());
assert(v4.size() == 10);
assert(v4.capacity() == 10);
std::vector<int> v5 = v4;
assert(!v5.empty());
assert(v5.size() == 10);
assert(v5.capacity() == 10);
std::vector<int> v6;
v6 = v5;
assert(!v6.empty());
assert(v6.size() == 10);
assert(v6.capacity() == 10);
std::vector<int> v7;
v7.assign(v6.begin(), v6.end());
assert(!v7.empty());
assert(v7.size() == 10);
assert(v7.capacity() == 10);
}
void __stl_vector_at_operator_array_front_back_data()
{
std::vector<int> v1(10, 5);
assert(v1.at(0) == 5);
assert(v1.at(9) == 5);
assert(v1[0] == 5);
assert(v1[9] == 5);
assert(v1.front() == 5);
assert(v1.back() == 5);
assert(v1.data() != nullptr);
}
void __stl_vector_begin_cbegin_end_cend()
{
std::vector<int> v1(10, 5);
assert(*v1.begin() == 5);
assert(*v1.cbegin() == 5);
assert(*v1.end() == 0);
assert(*v1.cend() == 0);
}
void __stl_vector_clear_insert_emplace_erase_push_back_emplace_back_pop_back_resize_swap()
{
std::vector<int> v1(10, 5);
v1.clear();
assert(v1.empty());
assert(v1.size() == 0);
assert(v1.capacity() == 10);
v1.insert(v1.begin(), 5);
assert(v1.size() == 1);
assert(v1[0] == 5);
v1.emplace(v1.begin(), 10);
assert(v1.size() == 2);
assert(v1[0] == 10);
assert(v1[1] == 5);
v1.erase(v1.begin());
assert(v1.size() == 1);
assert(v1[0] == 5);
v1.push_back(10);
assert(v1.size() == 2);
assert(v1[1] == 10);
v1.emplace_back(15);
assert(v1.size() == 3);
assert(v1[2] == 15);
v1.pop_back();
assert(v1.size() == 2);
assert(v1[1] == 10);
v1.resize(5);
assert(v1.size() == 5);
assert(v1[1] == 10);
std::vector<int> v2(10, 5);
v1.swap(v2);
assert(v1.size() == 10);
assert(v2.size() == 5);
}
void __stl_vector_reverse_iterators()
{
std::vector<int> v1 = {1, 2, 3, 4, 5};
// Test rbegin and rend
std::vector<int>::reverse_iterator rit;
std::vector<int>::const_reverse_iterator crit;
int i = 0;
for (rit = v1.rbegin(); rit != v1.rend(); ++rit)
{
assert(*rit == 5 - i);
i++;
}
int j = 0;
for (crit = v1.crbegin(); crit != v1.crend(); ++crit)
{
assert(*crit == 5 - j);
j++;
}
}
void test_stl_vector()
{
debug("std::vector ...");
debug("std::vector constructor, destructor, operator=, assign");
__stl_vector_constructor_destructor_operator_equal_assign();
debug("std::vector at, operator[], front, back, data");
__stl_vector_at_operator_array_front_back_data();
debug("std::vector begin, cbegin, end, cend");
__stl_vector_begin_cbegin_end_cend();
debug("std::vector clear, insert, emplace, erase, push_back, emplace_back, pop_back, resize, swap");
__stl_vector_clear_insert_emplace_erase_push_back_emplace_back_pop_back_resize_swap();
debug("std::vector OK");
}
#endif // DEBUG