blob: 6939859e90ac2db2f9596a2403217ad5eb6ea655 (
plain)
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
|
/*
* Copyright (c) 2022, Marc Luqué <marc.luque@outlook.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/InsertionSort.h>
#include <AK/Random.h>
#include <AK/Vector.h>
static constexpr int const n = 10;
static constexpr int const iterations = 10;
static constexpr int const seed = 1337;
TEST_CASE(sorts_ascending)
{
srand(seed);
for (int i = 0; i < iterations; ++i) {
Vector<int, n> ints;
for (int j = 0; j < n; ++j)
ints.append(get_random<int>());
Vector<int, n> ints_copy = ints;
insertion_sort(ints);
insertion_sort(ints_copy);
for (int j = 0; j < n - 1; ++j) {
EXPECT(ints[j] <= ints[j + 1]);
EXPECT_EQ(ints[j], ints_copy[j]);
EXPECT_EQ(ints[j + 1], ints_copy[j + 1]);
}
}
}
TEST_CASE(sorts_decending)
{
srand(seed);
for (int i = 0; i < iterations; ++i) {
Vector<int, n> ints;
for (int j = 0; j < n; ++j)
ints.append(get_random<int>());
Vector<int, n> ints_copy = ints;
insertion_sort(ints, [](auto& a, auto& b) { return a > b; });
insertion_sort(ints_copy, [](auto& a, auto& b) { return a > b; });
for (int j = 0; j < n - 1; ++j) {
EXPECT(ints[j] >= ints[j + 1]);
EXPECT_EQ(ints[j], ints_copy[j]);
EXPECT_EQ(ints[j + 1], ints_copy[j + 1]);
}
}
}
|