tests/test_compare.cpp

Fri, 07 Jul 2023 18:20:07 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 07 Jul 2023 18:20:07 +0200
changeset 739
529c35a768f3
parent 653
e081643aae2a
child 762
4523f6d42512
permissions
-rw-r--r--

fix typo in list.h

631
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2022 Mike Becker, Olaf Wintermann All rights reserved.
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/compare.h"
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <gtest/gtest.h>
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 template<typename T>
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 static void test_compare(
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 int (*fnc)(
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 void const *,
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 void const *
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 )
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 ) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 auto m = std::numeric_limits<T>::max() / 400;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 T x, y;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 x = (std::is_signed_v<T> ? -3 : 3) * m;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 y = 5 * m;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 EXPECT_LT(fnc(&x, &y), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 EXPECT_GT(fnc(&y, &x), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 x = 120 * m;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 y = 348 * m;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 EXPECT_LT(fnc(&x, &y), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 EXPECT_GT(fnc(&y, &x), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 if constexpr (std::is_signed_v<T>) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 x = -120 * m;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 y = -348 * m;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 EXPECT_GT(fnc(&x, &y), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 EXPECT_LT(fnc(&y, &x), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 x = y;
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 EXPECT_EQ(fnc(&x, &y), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 EXPECT_EQ(fnc(&y, &x), 0);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 TEST(Compare, Int) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 test_compare<int>(cx_cmp_int);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 TEST(Compare, Longint) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 test_compare<long int>(cx_cmp_longint);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 TEST(Compare, Longlong) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 test_compare<long long>(cx_cmp_longlong);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 TEST(Compare, Int16) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 test_compare<int16_t>(cx_cmp_int16);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 TEST(Compare, Int32) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 test_compare<int32_t>(cx_cmp_int32);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 TEST(Compare, Int64) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 test_compare<int64_t>(cx_cmp_int64);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 TEST(Compare, Uint) {
650
77021e06b1a8 fix code not compiling under windows+mingw
Mike Becker <universe@uap-core.de>
parents: 631
diff changeset
90 test_compare<unsigned int>(cx_cmp_uint);
631
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 TEST(Compare, Ulongint) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 test_compare<unsigned long int>(cx_cmp_ulongint);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 TEST(Compare, Ulonglong) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 test_compare<unsigned long long>(cx_cmp_ulonglong);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 TEST(Compare, Uint16) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 test_compare<uint16_t>(cx_cmp_uint16);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 TEST(Compare, Uint32) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 test_compare<uint32_t>(cx_cmp_uint32);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 TEST(Compare, Uint64) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 test_compare<uint64_t>(cx_cmp_uint64);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 TEST(Compare, Float) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 test_compare<float>(cx_cmp_float);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 TEST(Compare, Double) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 test_compare<double>(cx_cmp_double);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 TEST(Compare, IntPtr) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 test_compare<intptr_t>(cx_cmp_intptr);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 }
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 TEST(Compare, UintPtr) {
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 test_compare<uintptr_t>(cx_cmp_uintptr);
406376e64fd8 tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 }

mercurial