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