Serenity Operating System
at master 57 lines 1.4 kB view raw
1/* 2 * Copyright (c) 2022, Marc Luqué <marc.luque@outlook.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8 9#include <AK/InsertionSort.h> 10#include <AK/Random.h> 11#include <AK/Vector.h> 12 13static constexpr int const n = 10; 14static constexpr int const iterations = 10; 15static constexpr int const seed = 1337; 16 17TEST_CASE(sorts_ascending) 18{ 19 srand(seed); 20 21 for (int i = 0; i < iterations; ++i) { 22 Vector<int, n> ints; 23 for (int j = 0; j < n; ++j) 24 ints.append(get_random<int>()); 25 Vector<int, n> ints_copy = ints; 26 27 insertion_sort(ints); 28 insertion_sort(ints_copy); 29 30 for (int j = 0; j < n - 1; ++j) { 31 EXPECT(ints[j] <= ints[j + 1]); 32 EXPECT_EQ(ints[j], ints_copy[j]); 33 EXPECT_EQ(ints[j + 1], ints_copy[j + 1]); 34 } 35 } 36} 37 38TEST_CASE(sorts_decending) 39{ 40 srand(seed); 41 42 for (int i = 0; i < iterations; ++i) { 43 Vector<int, n> ints; 44 for (int j = 0; j < n; ++j) 45 ints.append(get_random<int>()); 46 Vector<int, n> ints_copy = ints; 47 48 insertion_sort(ints, [](auto& a, auto& b) { return a > b; }); 49 insertion_sort(ints_copy, [](auto& a, auto& b) { return a > b; }); 50 51 for (int j = 0; j < n - 1; ++j) { 52 EXPECT(ints[j] >= ints[j + 1]); 53 EXPECT_EQ(ints[j], ints_copy[j]); 54 EXPECT_EQ(ints[j + 1], ints_copy[j + 1]); 55 } 56 } 57}