Mesa (main): util: Add simple test for util_qsort_r
GitLab Mirror
gitlab-mirror at kemper.freedesktop.org
Thu Jun 24 20:25:10 UTC 2021
Module: Mesa
Branch: main
Commit: 1df2acfbb81f37e4c3d2a49f570ccfc96b32eb8c
URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=1df2acfbb81f37e4c3d2a49f570ccfc96b32eb8c
Author: Enrico Galli <enrico.galli at intel.com>
Date: Mon Jun 14 21:02:24 2021 -0700
util: Add simple test for util_qsort_r
Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10989>
---
src/util/meson.build | 2 +-
src/util/u_qsort_test.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/src/util/meson.build b/src/util/meson.build
index 9b0e7d3d5fb..b024cbb43e9 100644
--- a/src/util/meson.build
+++ b/src/util/meson.build
@@ -344,7 +344,7 @@ if with_tests
)
endif
- foreach t: ['bitset', 'register_allocate', 'u_debug_stack']
+ foreach t: ['bitset', 'register_allocate', 'u_debug_stack', 'u_qsort']
test(
t,
executable(
diff --git a/src/util/u_qsort_test.cpp b/src/util/u_qsort_test.cpp
new file mode 100644
index 00000000000..a0964e45228
--- /dev/null
+++ b/src/util/u_qsort_test.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright © 2021 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <gtest/gtest.h>
+
+#include "util/u_qsort.h"
+
+constexpr int CONTEXT_CHECK = 12345;
+
+static int
+cmp_func(const void *a, const void *b, void *ctx)
+{
+ int check = *reinterpret_cast<const int*>(ctx);
+ EXPECT_EQ(check, CONTEXT_CHECK);
+
+ int elem1 = *reinterpret_cast<const int*>(a);
+ int elem2 = *reinterpret_cast<const int*>(b);
+ return elem1 - elem2;
+}
+
+TEST(u_qsort_test, qsort_test)
+{
+ int data[] = { 3, 6, 4, 9, 10, 2, 5, 7, 8, 1 };
+ int ctx = CONTEXT_CHECK;
+
+ util_qsort_r(data, GTEST_ARRAY_SIZE_(data),
+ sizeof(data[0]), cmp_func,
+ reinterpret_cast<void *>(&ctx));
+
+ for (size_t i = 0; i < GTEST_ARRAY_SIZE_(data); ++i) {
+ EXPECT_EQ(data[i], i + 1);
+ }
+}
More information about the mesa-commit
mailing list