// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool TestBasicSorting() { std::vector<int> aVectorOfUnsortedIntegers(::GenerateVectorOfUnsortedIntegersWithinRange(-1000, 1000, 250)); for (PFN_Sort pfnSort : g_kapfnSort ) { std::vector<int> aVectorOfSortedIntegers(pfnSort(aVectorOfUnsortedIntegers)); if ( ! ::IsVectorSorted(aVectorOfSortedIntegers) ) return false; } return true; }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool TestReallyBasicSorting() { std::vector<int> aVectorOfUnsortedIntegers({4, 10, 7, 1, 2, 5, 3, 9, 8, 4, 6, 7}); for (PFN_Sort pfnSort : g_kapfnSort ) { std::vector<int> aVectorOfSortedIntegers(pfnSort(aVectorOfUnsortedIntegers)); if ( ! ::IsVectorSorted(aVectorOfSortedIntegers) ) return false; } return true; }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool TestEmptyArraySorting() { std::vector<int> aVectorOfUnsortedIntegers(0); for (PFN_Sort pfnSort : g_kapfnSort ) { std::vector<int> aVectorOfSortedIntegers(pfnSort(aVectorOfUnsortedIntegers)); if ( aVectorOfSortedIntegers.size() != 0 ) return false; } return true; }
void TestSortingFunction(void pfnSort(SAMPLE_TYPE*, size_t), const char* Name) { SAMPLE_TYPE DataSamples[][10] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, { 1, 0, 3, 2, 5, 4, 7, 6, 9, 8 }, { 1, 9, 6, 7, 1, 3, 2, 1, 5, 0 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 } }; const int SampleSize = 10; const int SamplesCount = 6; // Извеждаме информация за тестовете std::cout << "Testing " << Name; std::cout << "\n Samples: " << SamplesCount; std::cout << "\n Sample size: " << SampleSize; std::cout << "\n Samples:"; for (int i = 0; i < SamplesCount; i++) { std::cout << "\n Sample " << i << ": "; PrintArray<SAMPLE_TYPE>(DataSamples[i], SampleSize); } // Изпълняваме тестовете std::cout << "\n\n Sorting results:"; for (int i = 0; i < SamplesCount; i++) { pfnSort(DataSamples[i], SampleSize); std::cout << "\n Sorted " << i << ": "; PrintArray(DataSamples[i], SampleSize); } std::cout << "\n [end]\n\n\n"; }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool TestSortedArrayContainsSameElements() { std::vector<int> aVectorOfUnsortedIntegers(::GenerateVectorOfUnsortedIntegersWithinRange(-1000, 1000, 250)); const size_t kuVectorSize = aVectorOfUnsortedIntegers.size(); std::bitset<250> aBitsetOfMatchedIntegers; for (PFN_Sort pfnSort : g_kapfnSort ) { aBitsetOfMatchedIntegers.reset(); std::vector<int> aVectorOfSortedIntegers(pfnSort(aVectorOfUnsortedIntegers)); assert( kuVectorSize == aVectorOfSortedIntegers.size() ); const int g_kiSentinel = 2000; for (size_t uIndex = 0; uIndex < kuVectorSize; uIndex++) { int iUnsortedValue = aVectorOfUnsortedIntegers[uIndex]; size_t uBeginSearchIndex = 0; size_t uEndSearchIndex = aVectorOfSortedIntegers.size() - 1; while ( uBeginSearchIndex <= uEndSearchIndex ) { const size_t uMiddleIndex = (uBeginSearchIndex + uEndSearchIndex) / 2; size_t uCurrentIndex = uMiddleIndex; bool bFoundMatch = false; while ( aVectorOfSortedIntegers[uCurrentIndex] == iUnsortedValue ) { if ( ! aBitsetOfMatchedIntegers[uCurrentIndex] ) { aBitsetOfMatchedIntegers[uCurrentIndex] = true; bFoundMatch = true; break; } if ( uCurrentIndex == 0 ) break; uCurrentIndex--; } if ( ! bFoundMatch ) { uCurrentIndex = uMiddleIndex; while ( aVectorOfSortedIntegers[uCurrentIndex] == iUnsortedValue ) { if ( ! aBitsetOfMatchedIntegers[uCurrentIndex] ) { aBitsetOfMatchedIntegers[uCurrentIndex] = true; bFoundMatch = true; break; } uCurrentIndex++; if ( uCurrentIndex == kuVectorSize ) break; } } if ( bFoundMatch ) break; if ( aVectorOfSortedIntegers[uMiddleIndex] < iUnsortedValue ) { uBeginSearchIndex = uMiddleIndex + 1; } else { if ( uMiddleIndex == 0 ) break; uEndSearchIndex = uMiddleIndex - 1; } } } if ( ! aBitsetOfMatchedIntegers.all() ) return false; } return true; }