Skip to content
Snippets Groups Projects
Commit bd13ce3e authored by Dmitry Yu. Naumov's avatar Dmitry Yu. Naumov
Browse files

Add quicksort tests to check permutation vector.

parent 83e637ac
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@
#include "quicksort.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
......@@ -58,3 +59,74 @@ TEST(BaseLib, QuicksortSortsCorrectly) {
ASSERT_TRUE(quicksortSortsAsSTLSort.check());
}
// Permutations of sorted, unique vector remain untouched.
class QuicksortCheckPermutations : public Property<std::vector<int>> {
bool accepts(const std::vector<int>& xs)
{
return xs.size() > 2 &&
std::is_sorted(xs.begin(), xs.end()) &&
(std::adjacent_find(xs.begin(), xs.end()) == xs.end());
}
bool holdsFor(const std::vector<int>& xs)
{
std::vector<size_t> perm(xs.size());
for (size_t i = 0; i < perm.size(); ++i)
perm[i] = i;
BaseLib::quicksort((int*)&(xs[0]), 0, xs.size(), &(perm[0]));
for (size_t i = 0; i < perm.size(); ++i)
if (perm[i] != i)
return false;
return true;
}
const std::string classify(const std::vector<int>& xs)
{
std::stringstream ss;
ss << "size " << xs.size();
return ss.str();
}
};
TEST(BaseLib, QuicksortReportCorrectPermutations) {
QuicksortCheckPermutations quicksortCheckPermutations;
ASSERT_TRUE(quicksortCheckPermutations.check(200, 100000));
}
// Permutations of reverse sorted, unique vector is also reversed.
class QuicksortCheckPermutationsReverse : public Property<std::vector<int>> {
bool accepts(const std::vector<int>& xs)
{
return xs.size() > 2 &&
std::is_sorted(xs.rbegin(), xs.rend()) &&
(std::adjacent_find(xs.rbegin(), xs.rend()) == xs.rend());
}
bool holdsFor(const std::vector<int>& xs)
{
std::vector<size_t> perm(xs.size());
for (size_t i = 0; i < perm.size(); ++i)
perm[i] = i;
BaseLib::quicksort((int*)&(xs[0]), 0, xs.size(), &(perm[0]));
for (size_t i = 0; i < perm.size(); ++i)
if (perm[i] != perm.size() - i - 1)
return false;
return true;
}
const std::string classify(const std::vector<int>& xs)
{
std::stringstream ss;
ss << "size " << xs.size();
return ss.str();
}
};
TEST(BaseLib, QuicksortReportCorrectPermutationsReverse) {
QuicksortCheckPermutationsReverse quicksortCheckPermutationsReverse;
ASSERT_TRUE(quicksortCheckPermutationsReverse.check(200, 100000));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment