From 1df2acfbb81f37e4c3d2a49f570ccfc96b32eb8c Mon Sep 17 00:00:00 2001 From: Enrico Galli Date: Mon, 14 Jun 2021 21:02:24 -0700 Subject: [PATCH] util: Add simple test for util_qsort_r Reviewed-by: Jason Ekstrand Part-of: --- src/util/meson.build | 2 +- src/util/u_qsort_test.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/util/u_qsort_test.cpp diff --git a/src/util/meson.build b/src/util/meson.build index 9b0e7d3..b024cbb 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 0000000..a0964e4 --- /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 + +#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(ctx); + EXPECT_EQ(check, CONTEXT_CHECK); + + int elem1 = *reinterpret_cast(a); + int elem2 = *reinterpret_cast(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(&ctx)); + + for (size_t i = 0; i < GTEST_ARRAY_SIZE_(data); ++i) { + EXPECT_EQ(data[i], i + 1); + } +} -- 2.7.4