diff --git a/TinySTL/Test/Unordered_setTest.cpp b/TinySTL/Test/Unordered_setTest.cpp new file mode 100644 index 0000000..d805ac4 --- /dev/null +++ b/TinySTL/Test/Unordered_setTest.cpp @@ -0,0 +1,95 @@ +#include "Unordered_setTest.h" + +namespace TinySTL{ + namespace Unordered_setTest{ + template + bool container_equal(Container1& con1, Container2& con2){ + std::vector vec1, vec2; + for (auto& item : con1){ + vec1.push_back(item); + } + for (auto& item : con2){ + vec2.push_back(item); + } + std::sort(vec1.begin(), vec1.end()); + std::sort(vec2.begin(), vec2.end()); + return vec1 == vec2; + } + void testCase1(){ + stdUst ust1(10); + tsUst ust2(10); + assert(container_equal(ust1, ust2)); + + int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + stdUst ust3(std::begin(arr), std::end(arr)); + tsUst ust4(std::begin(arr), std::end(arr)); + assert(container_equal(ust3, ust4)); + + auto ust5(ust3); + auto ust6(ust4); + assert(container_equal(ust5, ust6)); + + auto ust7 = ust3; + auto ust8 = ust4; + assert(container_equal(ust7, ust8)); + } + void testCase2(){ + tsUst ust1(10); + assert(ust1.empty()); + assert(ust1.size() == 0); + + int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + tsUst ust2(std::begin(arr), std::end(arr)); + assert(!ust2.empty()); + assert(ust2.size() == 10); + } + void testCase3(){ + tsUst ust1(10); + stdUst ust2(10); + std::random_device rd; + for (auto i = 0; i != 100; ++i){ + auto n = std::to_string(rd() % 65536); + ust1.insert(n); + ust2.insert(n); + } + assert(container_equal(ust1, ust2)); + + tsUst ust3(10); + stdUst ust4(10); + std::vector v(200); + std::generate(v.begin(), v.end(), [&rd](){return rd() % 65536; }); + ust3.insert(v.begin(), v.end()); + ust4.insert(v.begin(), v.end()); + assert(container_equal(ust3, ust4)); + } + void testCase4(){ + int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + stdUst ust1(std::begin(arr), std::end(arr)); + tsUst ust2(std::begin(arr), std::end(arr)); + + ust1.erase(9); + auto n = ust2.erase(9); + assert(n == 1); + ust1.erase(7); + auto it = ust2.find(7); + it = ust2.erase(it); + assert(it != ust2.end()); + assert(container_equal(ust1, ust2)); + } + void testCase5(){ + int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9 }; + tsUst ust(std::begin(arr), std::end(arr)); + + assert(ust.find(0) != ust.end()); + assert(ust.count(10) == 0); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/Unordered_setTest.h b/TinySTL/Test/Unordered_setTest.h new file mode 100644 index 0000000..2ee9451 --- /dev/null +++ b/TinySTL/Test/Unordered_setTest.h @@ -0,0 +1,32 @@ +#ifndef _UNORDERED_SET_TEST_H_ +#define _UNORDERED_SET_TEST_H_ + +#include "TestUtil.h" + +#include "../Unordered_set.h" +#include + +#include +#include +#include +#include +#include + +namespace TinySTL{ + namespace Unordered_setTest{ + template + using stdUst = std::unordered_set < T > ; + template + using tsUst = TinySTL::Unordered_set < T > ; + + void testCase1(); + void testCase2(); + void testCase3(); + void testCase4(); + void testCase5(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 6f0b90e..7f9aeb6 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -16,9 +16,9 @@ #include "Test\StackTest.h" #include "Test\StringTest.h" #include "Test\SuffixArrayTest.h" +#include "Test\Unordered_setTest.h" #include "Test\VectorTest.h" -using namespace std; using namespace TinySTL::Profiler; int main(){ @@ -35,6 +35,7 @@ int main(){ TinySTL::StackTest::testAllCases(); TinySTL::StringTest::testAllCases(); TinySTL::SuffixArrayTest::testAllCases(); + TinySTL::Unordered_setTest::testAllCases(); TinySTL::VectorTest::testAllCases(); system("pause");