summaryrefslogtreecommitdiff
path: root/Tests/LibC/qsort-sorts-and-copies.cpp
diff options
context:
space:
mode:
authorBrian Gianforcaro <bgianf@serenityos.org>2021-05-06 01:14:50 -0700
committerAndreas Kling <kling@serenityos.org>2021-05-06 17:54:28 +0200
commitfd0dbd1ebfbcbc29d46393061daa49dc7390caa7 (patch)
tree278ea94a46900e47ff7dae46b1017cd31095971a /Tests/LibC/qsort-sorts-and-copies.cpp
parent6e641fadfa61d4b890db52fb60cc3709352336b6 (diff)
downloadserenity-fd0dbd1ebfbcbc29d46393061daa49dc7390caa7.zip
Tests: Establish root Tests directory, move Userland/Tests there
With the goal of centralizing all tests in the system, this is a first step to establish a Tests sub-tree. It will contain all of the unit tests and test harnesses for the various components in the system.
Diffstat (limited to 'Tests/LibC/qsort-sorts-and-copies.cpp')
-rw-r--r--Tests/LibC/qsort-sorts-and-copies.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/Tests/LibC/qsort-sorts-and-copies.cpp b/Tests/LibC/qsort-sorts-and-copies.cpp
new file mode 100644
index 0000000000..95b4579cbb
--- /dev/null
+++ b/Tests/LibC/qsort-sorts-and-copies.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2020, Sahan Fernando <sahan.h.fernando@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/QuickSort.h>
+#include <AK/String.h>
+#include <AK/Vector.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+const size_t NUM_RUNS = 100;
+
+struct SortableObject {
+ int m_key;
+ int m_payload;
+};
+
+static int compare_sortable_object(const void* a, const void* b)
+{
+ const int key1 = static_cast<const SortableObject*>(a)->m_key;
+ const int key2 = static_cast<const SortableObject*>(b)->m_key;
+ if (key1 < key2) {
+ return -1;
+ } else if (key1 == key2) {
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+static int calc_payload_for_pos(size_t pos)
+{
+ pos *= 231;
+ return pos ^ (pos << 8) ^ (pos << 16) ^ (pos << 24);
+}
+
+static void shuffle_vec(Vector<SortableObject>& test_objects)
+{
+ for (size_t i = 0; i < test_objects.size() * 3; ++i) {
+ auto i1 = rand() % test_objects.size();
+ auto i2 = rand() % test_objects.size();
+ swap(test_objects[i1], test_objects[i2]);
+ }
+}
+
+int main()
+{
+ // Generate vector of SortableObjects in sorted order, with payloads determined by their sorted positions
+ Vector<SortableObject> test_objects;
+ for (auto i = 0; i < 1024; ++i) {
+ test_objects.append({ i * 137, calc_payload_for_pos(i) });
+ }
+ for (size_t i = 0; i < NUM_RUNS; i++) {
+ // Shuffle the vector, then sort it again
+ shuffle_vec(test_objects);
+ qsort(test_objects.data(), test_objects.size(), sizeof(SortableObject), compare_sortable_object);
+ // Check that the objects are sorted by key
+ for (auto i = 0u; i + 1 < test_objects.size(); ++i) {
+ const auto& key1 = test_objects[i].m_key;
+ const auto& key2 = test_objects[i + 1].m_key;
+ if (key1 > key2) {
+ printf("\x1b[01;35mTests failed: saw key %d before key %d\n", key1, key2);
+ return 1;
+ }
+ }
+ // Check that the object's payloads have not been corrupted
+ for (auto i = 0u; i < test_objects.size(); ++i) {
+ const auto expected = calc_payload_for_pos(i);
+ const auto payload = test_objects[i].m_payload;
+ if (payload != expected) {
+ printf("\x1b[01;35mTests failed: expected payload %d for pos %u, got payload %d\n", expected, i, payload);
+ return 1;
+ }
+ }
+ }
+ printf("PASS\n");
+ return 0;
+}