[ox/std] Add some functions for comparing HashMap and SmallMap
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build / build (push) Successful in 2m31s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build / build (push) Successful in 2m31s
				
			This commit is contained in:
		
							
								
								
									
										4
									
								
								deps/ox/src/ox/std/test/CMakeLists.txt
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								deps/ox/src/ox/std/test/CMakeLists.txt
									
									
									
									
										vendored
									
									
								
							| @@ -12,8 +12,10 @@ add_test("[ox/std] ox_memcmp HIJKLMN != ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTOR | |||||||
| add_test("[ox/std] ox_memcmp ABCDEFG == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFG == ABCDEFG") | add_test("[ox/std] ox_memcmp ABCDEFG == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFG == ABCDEFG") | ||||||
| add_test("[ox/std] ox_memcmp ABCDEFGHI == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFGHI == ABCDEFG") | add_test("[ox/std] ox_memcmp ABCDEFGHI == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFGHI == ABCDEFG") | ||||||
| add_test("[ox/std] itoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "itoa") | add_test("[ox/std] itoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "itoa") | ||||||
| add_test("[ox/std] BString" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "BString") | add_test("[ox/std] IString" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "IString") | ||||||
| add_test("[ox/std] String" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "String") | add_test("[ox/std] String" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "String") | ||||||
|  | add_test("[ox/std] SmallMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap") | ||||||
|  | add_test("[ox/std] SmallMap2" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap2") | ||||||
| add_test("[ox/std] Vector" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector") | add_test("[ox/std] Vector" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector") | ||||||
| add_test("[ox/std] HashMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HashMap") | add_test("[ox/std] HashMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HashMap") | ||||||
| add_test("[ox/std] HeapMgr" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest malloc) | add_test("[ox/std] HeapMgr" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest malloc) | ||||||
|   | |||||||
							
								
								
									
										156
									
								
								deps/ox/src/ox/std/test/tests.cpp
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										156
									
								
								deps/ox/src/ox/std/test/tests.cpp
									
									
									
									
										vendored
									
									
								
							| @@ -6,13 +6,111 @@ | |||||||
|  * file, You can obtain one at https://mozilla.org/MPL/2.0/. |  * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include "ox/std/def.hpp" | #if __has_include(<chrono>) | ||||||
|  | #include<chrono> | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #include <ox/std/def.hpp> | ||||||
| #undef NDEBUG | #undef NDEBUG | ||||||
|  |  | ||||||
| #include <map> | #include <map> | ||||||
| #include <ox/std/uuid.hpp> | #include <ox/std/uuid.hpp> | ||||||
| #include <ox/std/std.hpp> | #include <ox/std/std.hpp> | ||||||
|  |  | ||||||
|  | [[nodiscard]] | ||||||
|  | static uint64_t nowMs() { | ||||||
|  | #if __has_include(<chrono>) | ||||||
|  | 	using namespace std::chrono; | ||||||
|  | 	return static_cast<uint64_t>( | ||||||
|  | 			duration_cast<milliseconds>( | ||||||
|  | 					system_clock::now().time_since_epoch()).count()); | ||||||
|  | #else | ||||||
|  | 	return 0; | ||||||
|  | #endif | ||||||
|  | } | ||||||
|  |  | ||||||
|  | template<typename Map = ox::SmallMap<ox::String, ox::UUID>> | ||||||
|  | uint64_t timeMapStrToUuid(int elemCnt, int lookups, uint64_t seed = 4321) noexcept { | ||||||
|  | 	ox::UUID::seedGenerator({1234, seed}); | ||||||
|  | 	Map map; | ||||||
|  | 	// setup test map | ||||||
|  | 	for (int i = 0; i < elemCnt; ++i) { | ||||||
|  | 		auto const uuid = ox::UUID::generate().unwrap(); | ||||||
|  | 		map[uuid.toString()] = uuid; | ||||||
|  | 	} | ||||||
|  | 	auto const keys = map.keys(); | ||||||
|  | 	ox::Random rand; | ||||||
|  | 	// start | ||||||
|  | 	auto const startTime = nowMs(); | ||||||
|  | 	for (int i = 0; i < lookups; ++i) { | ||||||
|  | 		auto const&k = keys[rand.gen() % keys.size()]; | ||||||
|  | 		map[k]; | ||||||
|  | 	} | ||||||
|  | 	return nowMs() - startTime; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | template<typename Map = ox::SmallMap<ox::UUID, ox::String>> | ||||||
|  | uint64_t timeMapUuidToStr(int elemCnt, int lookups, uint64_t seed = 4321) noexcept { | ||||||
|  | 	ox::UUID::seedGenerator({1234, seed}); | ||||||
|  | 	Map map; | ||||||
|  | 	// setup test map | ||||||
|  | 	for (int i = 0; i < elemCnt; ++i) { | ||||||
|  | 		auto const uuid = ox::UUID::generate().unwrap(); | ||||||
|  | 		map[uuid] = uuid.toString(); | ||||||
|  | 	} | ||||||
|  | 	auto const keys = map.keys(); | ||||||
|  | 	ox::Random rand; | ||||||
|  | 	// start | ||||||
|  | 	auto const startTime = nowMs(); | ||||||
|  | 	for (int i = 0; i < lookups; ++i) { | ||||||
|  | 		auto const&k = keys[rand.gen() % keys.size()]; | ||||||
|  | 		oxExpect(map[k], k.toString()); | ||||||
|  | 	} | ||||||
|  | 	return nowMs() - startTime; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static ox::Error compareMaps(int lookupCnt = 1'000'000) { | ||||||
|  | 	auto const seed = nowMs(); | ||||||
|  | 	uint64_t hashTime{}; | ||||||
|  | 	uint64_t smallTime{}; | ||||||
|  | 	int elemCnt = 1; | ||||||
|  | 	oxOut("UUIDStr to UUID:\n\n"); | ||||||
|  | 	while (hashTime >= smallTime) { | ||||||
|  | 		smallTime = timeMapStrToUuid<ox::SmallMap<ox::UUIDStr, ox::UUID>>(elemCnt, lookupCnt, seed); | ||||||
|  | 		hashTime = timeMapStrToUuid<ox::HashMap<ox::UUIDStr, ox::UUID>>(elemCnt, lookupCnt, seed); | ||||||
|  | 		oxOutf( | ||||||
|  | 				"UUIDStr to UUID: elemCnt: {}, lookupCnt: {} - hash map time: {}ms, small map time: {}ms\n", | ||||||
|  | 				elemCnt, lookupCnt, hashTime, smallTime); | ||||||
|  | 		++elemCnt; | ||||||
|  | 	} | ||||||
|  | 	oxOut("\n\nString to UUID:\n\n"); | ||||||
|  | 	hashTime = 0; | ||||||
|  | 	smallTime = 0; | ||||||
|  | 	elemCnt = 1; | ||||||
|  | 	while (hashTime >= smallTime) { | ||||||
|  | 		smallTime = timeMapStrToUuid<ox::SmallMap<ox::String, ox::UUID>>(elemCnt, lookupCnt, seed); | ||||||
|  | 		hashTime = timeMapStrToUuid<ox::HashMap<ox::String, ox::UUID>>(elemCnt, lookupCnt, seed); | ||||||
|  | 		oxOutf( | ||||||
|  | 				"String to UUID: elemCnt: {}, lookupCnt: {} - hash map time: {}ms, small map time: {}ms\n", | ||||||
|  | 				elemCnt, lookupCnt, hashTime, smallTime); | ||||||
|  | 		++elemCnt; | ||||||
|  | 	} | ||||||
|  | 	oxOut("\n\nUUID to UUIDStr:\n\n"); | ||||||
|  | 	hashTime = 0; | ||||||
|  | 	smallTime = 0; | ||||||
|  | 	elemCnt = 1; | ||||||
|  | 	while (hashTime >= smallTime) { | ||||||
|  | 		smallTime = timeMapUuidToStr<ox::SmallMap<ox::UUID, ox::UUIDStr>>(elemCnt, lookupCnt, seed); | ||||||
|  | 		hashTime = timeMapUuidToStr<ox::HashMap<ox::UUID, ox::UUIDStr>>(elemCnt, lookupCnt, seed); | ||||||
|  | 		oxOutf( | ||||||
|  | 				"UUID to UUIDStr: elemCnt: {}, lookupCnt: {} - hash map time: {}ms, small map time: {}ms\n", | ||||||
|  | 				elemCnt, lookupCnt, hashTime, smallTime); | ||||||
|  | 		++elemCnt; | ||||||
|  | 	} | ||||||
|  | 	oxOut("\n"); | ||||||
|  | 	return {}; | ||||||
|  | } | ||||||
|  |  | ||||||
| static std::map<ox::StringView, ox::Error(*)()> tests = { | static std::map<ox::StringView, ox::Error(*)()> tests = { | ||||||
| 	{ | 	{ | ||||||
| 		"malloc", | 		"malloc", | ||||||
| @@ -23,6 +121,8 @@ static std::map<ox::StringView, ox::Error(*)()> tests = { | |||||||
| 			auto a2 = static_cast<char*>(ox::heapmgr::malloc(5)); | 			auto a2 = static_cast<char*>(ox::heapmgr::malloc(5)); | ||||||
| 			oxAssert(a1 >= buff.front().unwrap() && a1 < buff.back().unwrap(), "malloc is broken"); | 			oxAssert(a1 >= buff.front().unwrap() && a1 < buff.back().unwrap(), "malloc is broken"); | ||||||
| 			oxAssert(a2 >= buff.front().unwrap() && a2 < buff.back().unwrap() && a2 > a1 + 5, "malloc is broken"); | 			oxAssert(a2 >= buff.front().unwrap() && a2 < buff.back().unwrap() && a2 > a1 + 5, "malloc is broken"); | ||||||
|  | 			ox::heapmgr::free(a1); | ||||||
|  | 			ox::heapmgr::free(a2); | ||||||
| 			return OxError(0); | 			return OxError(0); | ||||||
| 		} | 		} | ||||||
| 	}, | 	}, | ||||||
| @@ -67,14 +167,14 @@ static std::map<ox::StringView, ox::Error(*)()> tests = { | |||||||
| 		} | 		} | ||||||
| 	}, | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		"BString", | 		"IString", | ||||||
| 		[]() { | 		[]() { | ||||||
| 			ox::IString<5> s; | 			ox::IString<5> s; | ||||||
| 			oxReturnError(s.append("A")); | 			oxReturnError(s.append("A")); | ||||||
| 			oxReturnError(s.append("B")); | 			oxReturnError(s.append("B")); | ||||||
| 			oxReturnError(s.append("9")); | 			oxReturnError(s.append("9")); | ||||||
| 			oxReturnError(s.append("C")); | 			oxReturnError(s.append("C")); | ||||||
| 			oxAssert(s == "AB9C", "BString append broken"); | 			oxAssert(s == "AB9C", "IString append broken"); | ||||||
| 			s = "asdf"; | 			s = "asdf"; | ||||||
| 			oxAssert(s == "asdf", "String assign broken"); | 			oxAssert(s == "asdf", "String assign broken"); | ||||||
| 			oxAssert(s != "aoeu", "String assign broken"); | 			oxAssert(s != "aoeu", "String assign broken"); | ||||||
| @@ -134,10 +234,38 @@ static std::map<ox::StringView, ox::Error(*)()> tests = { | |||||||
| 			return OxError(0); | 			return OxError(0); | ||||||
| 		} | 		} | ||||||
| 	}, | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"SmallMap", | ||||||
|  | 		[] { | ||||||
|  | 			ox::SmallMap<ox::String, ox::String> map; | ||||||
|  | 			map["asdf"] = "aoeu"; | ||||||
|  | 			oxExpect(map["asdf"], "aoeu"); | ||||||
|  | 			oxExpect(map.size(), 1u); | ||||||
|  | 			oxExpect(map["aoeu"], ""); | ||||||
|  | 			oxExpect(map.size(), 2u); | ||||||
|  | 			return OxError(0); | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"SmallMap2", | ||||||
|  | 		[] { | ||||||
|  | 			ox::SmallMap<ox::String, int> si; | ||||||
|  | 			si["asdf"] = 42; | ||||||
|  | 			si["aoeu"] = 100; | ||||||
|  | 			oxAssert(si["asdf"] == 42, "asdf != 42"); | ||||||
|  | 			oxAssert(si["aoeu"] == 100, "aoeu != 100"); | ||||||
|  | 			ox::SmallMap<int, int> ii; | ||||||
|  | 			ii[4] = 42; | ||||||
|  | 			ii[5] = 100; | ||||||
|  | 			oxAssert(ii[4] == 42, "4 != 42"); | ||||||
|  | 			oxAssert(ii[5] == 100, "5 != 100"); | ||||||
|  | 			return OxError(0); | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		"HashMap", | 		"HashMap", | ||||||
| 		[] { | 		[] { | ||||||
| 			ox::HashMap<const char*, int> si; | 			ox::HashMap<ox::String, int> si; | ||||||
| 			si["asdf"] = 42; | 			si["asdf"] = 42; | ||||||
| 			si["aoeu"] = 100; | 			si["aoeu"] = 100; | ||||||
| 			oxAssert(si["asdf"] == 42, "asdf != 42"); | 			oxAssert(si["asdf"] == 42, "asdf != 42"); | ||||||
| @@ -150,6 +278,26 @@ static std::map<ox::StringView, ox::Error(*)()> tests = { | |||||||
| 			return OxError(0); | 			return OxError(0); | ||||||
| 		} | 		} | ||||||
| 	}, | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"TimeSmallMapMillion", | ||||||
|  | 		[] { | ||||||
|  | 			timeMapStrToUuid<ox::SmallMap<ox::String, ox::UUID>>(1'000, 1'000'000); | ||||||
|  | 			return ox::Error{}; | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"TimeHashMapMillion", | ||||||
|  | 		[] { | ||||||
|  | 			timeMapStrToUuid<ox::HashMap<ox::String, ox::UUID>>(1'000, 1'000'000); | ||||||
|  | 			return ox::Error{}; | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
|  | 	{ | ||||||
|  | 		"CompareMaps", | ||||||
|  | 		[] { | ||||||
|  | 			return compareMaps(); | ||||||
|  | 		} | ||||||
|  | 	}, | ||||||
| 	{ | 	{ | ||||||
| 		"Serialize-Int", | 		"Serialize-Int", | ||||||
| 		[] { | 		[] { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user