/
lasersquad
/
DynamicArrays
Обзор
Документация
Войти
/
lasersquad
/
DynamicArrays
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
dynamicarrays/tests/IteratorsTest.cpp
673 строки
14 KB
lasersquad0
Small compilation error fix
09 июн 2025, 22:38
09 июн 2025, 22:38
2061040
Код
Авторство
О чём код?
#include "gtest/gtest.h" #include "DynamicArrays.h" #include <gtest/internal/gtest-port.h> typedef std::string TestingType; typedef std::string TestingType1; typedef std::string TestingType2; //typedef long TestingType; //typedef long TestingType1; //typedef long TestingType2; typedef THArray<TestingType> THTestArray; typedef THash<TestingType1, TestingType2> TestHash; template<class STRING> STRING MillisecToStr(uint64_t ms) { // make sure that STRING is one of instantiations of std::string static_assert(std::is_base_of<std::basic_string<typename STRING::value_type, typename STRING::traits_type>, STRING>::value); uint32_t milliseconds = ms % 1000; uint32_t seconds = (ms / 1000) % 60; uint32_t minutes = (ms / 60000) % 60; uint32_t hours = (ms / 3600000) % 24; STRING result; if constexpr (std::is_same_v<typename STRING::value_type, char>) { if (hours > 0) result = std::format("{} hours {} minutes {} seconds {} ms", hours, minutes, seconds, milliseconds); else if (minutes > 0) result = std::format("{} minutes {} seconds {} ms", minutes, seconds, milliseconds); else result = std::format("{} seconds {} ms", seconds, milliseconds); } else if constexpr (std::is_same_v<typename STRING::value_type, wchar_t>) { if (hours > 0) result = std::format(L"{} hours {} minutes {} seconds {} ms", hours, minutes, seconds, milliseconds); else if (minutes > 0) result = std::format(L"{} minutes {} seconds {} ms", minutes, seconds, milliseconds); else result = std::format(L"{} seconds {} ms", seconds, milliseconds); } else result = "UNKNOWN STRING TYPE"; return result; } TEST(InteratorsTest, ArrayTest0) { THTestArray arr; THTestArray::iterator b = arr.begin(); THTestArray::iterator e = arr.end(); EXPECT_EQ(b, e); // for empty container two iteratord are equal each other auto bb = arr.begin(); auto ee = arr.end(); EXPECT_EQ(bb, ee); // for empty container two iterators are equal each other EXPECT_EQ(b, ee); // iterators cross compare EXPECT_EQ(bb, e); // iterators cross compare EXPECT_EQ(bb, b); // iterators cross compare EXPECT_EQ(ee, e); // iterators cross compare TestingType v = TestingType{ 4 }; arr.AddValue(v); bb = arr.begin(); ee = arr.end(); EXPECT_NE(bb, ee); ++bb; EXPECT_EQ(bb, ee); ++bb; EXPECT_EQ(bb, ee); bb++; EXPECT_EQ(bb, ee); --bb; EXPECT_NE(bb, ee); EXPECT_EQ(v, *bb); bb--; EXPECT_NE(bb, ee); EXPECT_EQ(v, *bb); --bb; EXPECT_NE(bb, ee); EXPECT_EQ(v, *bb); ++ee; EXPECT_NE(bb, ee); EXPECT_NE(v, *ee); EXPECT_EQ(v, *bb); --ee; EXPECT_EQ(bb, ee); EXPECT_EQ(v, *ee); EXPECT_EQ(v, *bb); --bb; ee--; EXPECT_EQ(bb, ee); EXPECT_EQ(v, *bb); EXPECT_EQ(v, *ee); } TEST(InteratorsTest, ArrayTest1) { THTestArray arr; THTestArray::iterator b = arr.begin(); THTestArray::iterator e = arr.end(); EXPECT_EQ(b, e); // for empty container two iteratord are equal each other auto bb = arr.begin(); auto ee = arr.end(); EXPECT_EQ(bb, ee); // for empty container two iteratord are equal each other auto eee = arr.end(); EXPECT_EQ(bb, eee); // for empty container two iteratord are equal each other EXPECT_EQ(b, eee); // for empty container two iteratord are equal each other TestingType v = TestingType{ 4 }; arr.AddValue(v); bb = arr.begin(); ee = arr.end(); EXPECT_NE(bb, ee); ++bb; EXPECT_EQ(bb, ee); --bb; EXPECT_NE(bb, ee); --ee; EXPECT_EQ(bb, ee); EXPECT_EQ(v, *ee); EXPECT_EQ(v, *bb); TestingType vv = TestingType{ 1 }; arr.AddValue(vv); bb = arr.begin(); ee = arr.end(); EXPECT_NE(bb, ee); EXPECT_EQ(v, *bb); ++bb; EXPECT_NE(bb, ee); EXPECT_EQ(vv, *bb); ++bb; EXPECT_EQ(bb, ee); --bb; --ee; EXPECT_EQ(bb, ee); EXPECT_EQ(vv, *bb); EXPECT_EQ(vv, *ee); } TEST(InteratorsTest, ArrayTest2) { THTestArray arr; char counter = 0; for (TestingType a: arr) { EXPECT_EQ(true, false); // arr is empty, it should not go here } EXPECT_EQ(0, counter); counter = 0; for (TestingType& a: arr) { EXPECT_EQ(true, false); // arr is empty, it should not go here } EXPECT_EQ(0, counter); counter = 0; for (TestingType b : arr) { EXPECT_EQ(true, false); // arr is empty, it should not go here } EXPECT_EQ(0, counter); counter = 0; for (TestingType& b : arr) { EXPECT_EQ(true, false); // arr is empty, it should not go here } EXPECT_EQ(0, counter); counter = 0; for (auto iter = arr.begin(); iter != arr.end(); ++iter) { EXPECT_EQ(true, false); // arr is empty, it should not go here } EXPECT_EQ(0, counter); } TEST(InteratorsTest, ArrayTest3) { THTestArray origarr; THTestArray arr; TestingType v = TestingType{ 3 }; arr.AddValue(v); origarr.AddValue(v); int counter = 0; for(TestingType a: arr) { EXPECT_EQ(a, v); counter++; } EXPECT_EQ(1, counter); counter = 0; for (TestingType& a: arr) { EXPECT_EQ(a, v); counter++; } EXPECT_EQ(1, counter); counter = 0; for (TestingType b : arr) { EXPECT_EQ(b, v); counter++; } EXPECT_EQ(1, counter); counter = 0; for (TestingType& b : arr) { EXPECT_EQ(b, v); counter++; } EXPECT_EQ(1, counter); counter = 0; for (auto iter = arr.begin(); iter != arr.end(); ++iter) { EXPECT_EQ(*iter, v); counter++; } EXPECT_EQ(1, counter); } TEST(InteratorsTest, ArrayTest4) { THTestArray arr; THTestArray origarr; for (char i = 0; i < 5; i++) { TestingType v = TestingType{ i }; arr.AddValue(v); TestingType vv = TestingType{i}; origarr.AddValue(vv); } int counter = 0; for (TestingType a: arr) { EXPECT_EQ(a, origarr[counter++]); } EXPECT_EQ(5, counter); counter = 0; for (TestingType& a: arr) { EXPECT_EQ(a, origarr[counter++]); } EXPECT_EQ(5, counter); counter = 0; for (TestingType b : arr) { EXPECT_EQ(b, origarr[counter++]); } EXPECT_EQ(5, counter); counter = 0; for (TestingType& b : arr) { EXPECT_EQ(b, origarr[counter++]); } EXPECT_EQ(5, counter); counter = 0; for (auto iter = arr.begin(); iter != arr.end(); ++iter) { EXPECT_EQ(*iter, origarr[counter++]); } EXPECT_EQ(5, counter); } TEST(InteratorsTest, ArrayTest5) { THTestArray arr; THTestArray origarr; for (char i = 0; i < 5; i++) { TestingType v = TestingType{ 'a' + i }; arr.AddValue(v); origarr.AddValue(v); } TestingType vv = TestingType{ 'd'}; THTestArray::iterator iter = std::find(arr.begin(), arr.end(), vv); EXPECT_EQ(vv, *iter); vv = TestingType{ 10 }; iter = std::find(arr.begin(), arr.end(), vv); EXPECT_EQ(arr.end(), iter); EXPECT_EQ(TestingType{ 'a'}, *(arr.begin())); std::reverse(arr.begin(), arr.end()); EXPECT_EQ(TestingType{ 'a' + 4 }, *(arr.begin())); std::fill(arr.begin(), arr.end(), TestingType{ 3 }); } struct ATTR_FILE_NAME { uint32_t f1; uint64_t f2; }; struct FILE_NAME { ATTR_FILE_NAME Attr; std::string Name; FILE_NAME(): Attr(0) { } FILE_NAME(const FILE_NAME& fn) { Attr = fn.Attr; Name = fn.Name; } FILE_NAME& operator=(const FILE_NAME& other) { Attr = other.Attr; Name = other.Name; return *this; } bool operator==(FILE_NAME other) { return Name == other.Name; } bool operator<(FILE_NAME other); }; bool compare(const FILE_NAME& a, const FILE_NAME& b) { return a.Name < b.Name; } bool FILE_NAME::operator<(FILE_NAME other) { return Name < other.Name; //compare(*this, other); } template<class Container> bool VerifyArraySorted(const Container& cont) { for (uint i = 1; i < cont.Count(); ++i) { if (cont[i - 1] == cont[i]) continue; if (cont[i - 1] < cont[i]) continue; // we require only two operators to be in Container::value_type: < and == return false; } return true; } template<> bool VerifyArraySorted<THArray<FILE_NAME>>(const THArray<FILE_NAME>& cont) { for (uint i = 1; i < cont.Count(); ++i) { if (cont[i - 1] == cont[i]) continue; if (compare(cont[i - 1], cont[i])) continue; // we require only two operators to be in Container::value_type: < and == return false; } return true; } TEST(InteratorsTest, ArrayTestSort) { /* THTestArray arr; THTestArray origarr; std::sort(arr.begin(), arr.end()); EXPECT_EQ(true, VerifyArraySorted(arr)); THArray<std::wstring> arrW; arrW.AddValue(L"1234"); arrW.AddValue(L"dfrfrf"); arrW.AddValue(L"4red3e"); arrW.AddValue(L"g6uhj"); arrW.AddValue(L"h7"); arrW.AddValue(L" "); arrW.AddValue(L".fgrf"); arrW.AddValue(L"[][pp]]]"); std::sort(arrW.begin(), arrW.end()); EXPECT_EQ(true, VerifyArraySorted(arrW)); */ THArray<FILE_NAME> arr2; FILE_NAME fn{}; fn.Name = "cppunit.spec"; arr2.AddValue(fn); fn.Name = "1.txt"; arr2.AddValue(fn); fn.Name = "2.txt"; arr2.AddValue(fn); fn.Name = "aclocal.m4"; arr2.AddValue(fn); fn.Name = "AUTHORS"; arr2.AddValue(fn); fn.Name = "BUGS"; arr2.AddValue(fn); fn.Name = "ChangeLog"; arr2.AddValue(fn); fn.Name = "CodingGuideLines.txt"; arr2.AddValue(fn); fn.Name = "config"; arr2.AddValue(fn); fn.Name = "ac_create_prefix_config_h.m4"; arr2.AddValue(fn); fn.Name = "ac_cxxhave_sstream.m4"; arr2.AddValue(fn); fn.Name = "ac_cxx_have_strstream.m4"; arr2.AddValue(fn); fn.Name = "ac_cxx_namespaces.m4"; arr2.AddValue(fn); fn.Name = "ac_cxx_rtti.m4"; arr2.AddValue(fn); fn.Name = "ac_cxx_strng_compare_string_first.m4"; arr2.AddValue(fn); fn.Name = "ac_dll.m4"; arr2.AddValue(fn); fn.Name = "ax_cxx_gcc_abi_demangle.m4"; arr2.AddValue(fn); fn.Name = "bb_enable_doxygen.m4"; arr2.AddValue(fn); fn.Name = "config.guess"; arr2.AddValue(fn); fn.Name = "config.h.in"; arr2.AddValue(fn); fn.Name = "config.sub"; arr2.AddValue(fn); fn.Name = "depcomp"; arr2.AddValue(fn); fn.Name = "install-sh"; arr2.AddValue(fn); fn.Name = "ltmain.sh"; arr2.AddValue(fn); fn.Name = "missing"; arr2.AddValue(fn); fn.Name = "configure"; arr2.AddValue(fn); fn.Name = "configure.in"; arr2.AddValue(fn); fn.Name = "contrib"; arr2.AddValue(fn); fn.Name = "bc5"; arr2.AddValue(fn); fn.Name = "bcc-makefile.zip"; arr2.AddValue(fn); fn.Name = "msvc"; arr2.AddValue(fn); fn.Name = "AddingUnitTestMethod.dsm"; arr2.AddValue(fn); fn.Name = "CppUnit.WWTpl"; arr2.AddValue(fn); fn.Name = "readme.txt"; arr2.AddValue(fn); fn.Name = "xml-xsl"; //std::sort(arr2.begin(), arr2.end()); std::sort(arr2.begin(), arr2.end()); EXPECT_EQ(true, VerifyArraySorted(arr2)); for (auto& v : arr2) { GTEST_LOG_(INFO) << v.Name; } } bool constIteratorFind(const THTestArray& cont, TestingType& tofind) { for (auto iter = cont.cbegin(); iter != cont.cend(); ++iter) { if (*iter == tofind) return true; } return false; } TEST(InteratorsTest, ArrayTest6) { THTestArray arr; THTestArray origarr; for (char i = 0; i < 5; i++) { TestingType v = TestingType{ 'a' + i }; arr.AddValue(v); origarr.AddValue(v); } TestingType vv = TestingType{ 'd' }; EXPECT_TRUE(constIteratorFind(arr, vv)); THTestArray::const_iterator citer = arr.cbegin(); vv = TestingType{ 10 }; EXPECT_FALSE(constIteratorFind(arr, vv)); citer = arr.cbegin(); EXPECT_EQ(TestingType{ 'a' }, *(citer)); } TEST(InteratorsTest, ArrayFindTest) { srand(time(nullptr)); THArray<FILE_NAME> arr2; FILE_NAME item; const uint ARR_SZ = 100'000; //generate values for array arr2.SetCapacity(ARR_SZ); for (uint i = 0; i < ARR_SZ; i++) { item.Attr.f1 = rand(); arr2.AddValue(item); } auto start = std::chrono::high_resolution_clock::now(); for (uint i = 0; i < arr2.Count(); i++) { FILE_NAME& item2 = arr2[i]; auto iterEnd = arr2.end(); auto iter = std::find_if(arr2.begin() + i + 1, iterEnd, [&](const FILE_NAME& a) { return item2.Attr.f1 == a.Attr.f1; }); //if (iter != iterEnd) GTEST_LOG_(INFO) << "Duplicate item found: " << item2.Attr.f1 << std::endl; } auto stop = std::chrono::high_resolution_clock::now(); GTEST_LOG_(INFO) << "ArrayFindTest finished in: " << MillisecToStr<std::string>(std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count()) << std::endl; } TEST(InteratorsTest, HashTest1) { TestHash hash; THash<TestingType1, TestingType2>::iterator b = hash.begin(); THash<TestingType1, TestingType2>::iterator e = hash.end(); EXPECT_EQ(b, e); TestingType1 k = TestingType1{ 1 }; TestingType2 v = TestingType2{ 2 }; hash.SetValue(k, v); auto bb = hash.begin(); auto ee = hash.end(); EXPECT_NE(bb, ee); EXPECT_EQ(k, (*bb).first); EXPECT_EQ(v, (*bb).second); ++bb; EXPECT_EQ(bb, ee); ++bb; EXPECT_EQ(bb, ee); ++ee; EXPECT_EQ(bb, ee); --bb; --ee; EXPECT_EQ(bb, ee); EXPECT_EQ(k, (*bb).first); EXPECT_EQ(v, (*bb).second); EXPECT_EQ(k, (*ee).first); EXPECT_EQ(v, (*ee).second); } TEST(InteratorsTest, HashTest2) { TestHash hash; TestHash orighash; for (char i = 0; i < 5; i++) { TestingType1 k = TestingType1{ 'a' + i }; TestingType2 v = TestingType2{'a' + i + 10}; hash.SetValue(k, v); orighash.SetValue(k, v); } char counter = 0; for (auto iter = hash.begin(); iter != hash.end(); ++iter) { EXPECT_EQ(TestingType1{ 'a' + counter }, (*iter).first); EXPECT_EQ(TestingType2{ 'a' + counter + 10 }, (*iter).second); counter++; } counter = 0; for (TestHash::iterator::value_type a: hash) { EXPECT_EQ(TestingType1{ 'a' + counter }, a.first); EXPECT_EQ(TestingType2{ 'a' + counter + 10 }, a.second); counter++; } counter = 0; for(auto a: hash) { EXPECT_EQ(TestingType1{ 'a' + counter }, a.first); EXPECT_EQ(TestingType2{ 'a' + counter + 10 }, a.second); counter++; } } TEST(InteratorsTest, HashTest3) { TestHash hash; TestHash orighash; for (char i = 0; i < 5; i++) { TestingType1 k = TestingType1{ 'a' + i }; TestingType2 v = TestingType2{ 'a' + i + 10 }; hash.SetValue(k, v); orighash.SetValue(k, v); } auto tt1 = TestingType1{ 'a' + 3 }; auto tt2 = TestingType2{ 'a' + 13 }; auto vv = TestHash::iterator::value_type{ tt1, tt2 }; TestHash::iterator iter = std::find(hash.begin(), hash.end(), vv); EXPECT_EQ(vv, *iter); tt1 = TestingType1{ 'a' + 3 }; tt2 = TestingType2{ 'a' + 12 }; vv = TestHash::iterator::value_type{ tt1, tt2 }; iter = std::find(hash.begin(), hash.end(), vv); EXPECT_EQ(hash.end(), iter); }