-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_sort.cpp
83 lines (78 loc) · 2.8 KB
/
test_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "catch2/catch_amalgamated.hpp"
#include "jumble/sort.hpp"
#include "jumble/util/random.hpp"
#include <algorithm>
TEST_CASE("InsertionSort") {
jumble::Random *random = jumble::Random::getInstance();
const int n = 3000;
int arr1[n], arr2[n];
for (int i = 0; i < n; ++i) {
arr1[i] = arr2[i] = random->nextInt(1, 10000);
}
std::sort(arr1, arr1 + n, std::greater<int>());
jumble::insertionSort<int, std::greater<int>>(arr2, n);
REQUIRE(std::is_sorted(arr1, arr1 + n, std::greater<int>()));
REQUIRE(std::is_sorted(arr2, arr2 + n, std::greater<int>()));
for (int i = 0; i < n; ++i) {
REQUIRE(arr1[i] == arr2[i]);
}
}
TEST_CASE("SelectionSort") {
jumble::Random *random = jumble::Random::getInstance();
const int n = 3000;
int arr1[n], arr2[n];
for (int i = 0; i < n; ++i) {
arr1[i] = arr2[i] = random->nextInt(1, 10000);
}
std::sort(arr1, arr1 + n, std::greater<int>());
jumble::selectionSort<int, std::greater<int>>(arr2, n);
REQUIRE(std::is_sorted(arr1, arr1 + n, std::greater<int>()));
REQUIRE(std::is_sorted(arr2, arr2 + n, std::greater<int>()));
for (int i = 0; i < n; ++i) {
REQUIRE(arr1[i] == arr2[i]);
}
}
TEST_CASE("HeapSort") {
jumble::Random *random = jumble::Random::getInstance();
const int n = 3000;
int arr1[n], arr2[n];
for (int i = 0; i < n; ++i) {
arr1[i] = arr2[i] = random->nextInt(1, 10000);
}
std::sort(arr1, arr1 + n, std::greater<int>());
jumble::heapSort<int, std::greater<int>>(arr2, n);
REQUIRE(std::is_sorted(arr1, arr1 + n, std::greater<int>()));
REQUIRE(std::is_sorted(arr2, arr2 + n, std::greater<int>()));
for (int i = 0; i < n; ++i) {
REQUIRE(arr1[i] == arr2[i]);
}
}
TEST_CASE("QuickSort") {
jumble::Random *random = jumble::Random::getInstance();
const int n = 3000;
int arr1[n], arr2[n];
for (int i = 0; i < n; ++i) {
arr1[i] = arr2[i] = random->nextInt(1, 10000);
}
std::sort(arr1, arr1 + n, std::greater<int>());
jumble::quickSort<int, std::greater<int>>(arr2, 0, n - 1);
REQUIRE(std::is_sorted(arr1, arr1 + n, std::greater<int>()));
REQUIRE(std::is_sorted(arr2, arr2 + n, std::greater<int>()));
for (int i = 0; i < n; ++i) {
REQUIRE(arr1[i] == arr2[i]);
}
}
TEST_CASE("QuickSelect") {
jumble::Random *random = jumble::Random::getInstance();
const int n = 3000;
for (int i = 0; i < 5; ++i) {
int arr1[n], arr2[n];
for (int j = 0; j < n; ++j) {
arr1[j] = arr2[j] = random->nextInt(1, 10000);
}
unsigned k = random->nextInt(0, n - 1);
std::nth_element(arr1, arr1 + k, arr1 + n, std::greater<int>());
jumble::quickSelect<int, std::greater<int>>(arr2, 0, n - 1, k);
REQUIRE(arr1[k] == arr2[k]);
}
}