tests/test_utils.cpp

Tue, 28 Mar 2023 19:13:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Mar 2023 19:13:33 +0200
changeset 669
dce9b8450656
parent 654
c9d008861178
child 674
dc514a5d42a5
permissions
-rw-r--r--

add docs for CX_STORE_POINTERS and remove cxHashMapCreateForPointers()

633
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63a692642aa2 add utils tests
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
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/utils.h"
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <gtest/gtest.h>
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 TEST(Utils, ForN) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 unsigned j;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 j = 0;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 cx_for_n(i, 50) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 EXPECT_EQ(i, j);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 j++;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
646
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
42 TEST(Utils, swap_ptr) {
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
43 int i = 5;
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
44 int j = 8;
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
45 int *ip = &i;
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
46 int *jp = &j;
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
47 cx_swap_ptr(ip, jp);
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
48 EXPECT_EQ(ip, &j);
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
49 EXPECT_EQ(jp, &i);
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
50 }
dfd0403ff8b6 add pointer swap utility
Mike Becker <universe@uap-core.de>
parents: 633
diff changeset
51
633
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 TEST(Utils, szmul) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 size_t r;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 int e;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 e = cx_szmul(5, 7, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 EXPECT_EQ(35, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 size_t s = SIZE_MAX & ~3;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 e = cx_szmul(s / 4, 2, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 e = cx_szmul(2, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 e = cx_szmul(s / 4, 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 e = cx_szmul(4, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 e = cx_szmul(s / 4, 5, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 e = cx_szmul(5, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 e = cx_szmul(SIZE_MAX - 4, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 e = cx_szmul(0, SIZE_MAX - 1, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 e = cx_szmul(SIZE_MAX, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 e = cx_szmul(0, SIZE_MAX, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 e = cx_szmul(0, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 #ifdef CX_SZMUL_BUILTIN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 // also test the custom implementation
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 struct Utils_szmul_impl : ::testing::Test {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 #undef CX_SZMUL_BUILTIN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 #include "../src/utils.c"
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 #define CX_SZMUL_BUILTIN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 };
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 TEST_F(Utils_szmul_impl, Test) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 size_t r;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 int e;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 e = cx_szmul_impl(5, 7, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 EXPECT_EQ(35, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 size_t s = SIZE_MAX & ~3;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 e = cx_szmul_impl(s / 4, 2, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 e = cx_szmul_impl(2, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 e = cx_szmul_impl(s / 4, 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 e = cx_szmul_impl(4, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 e = cx_szmul_impl(s / 4, 5, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 e = cx_szmul_impl(5, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 e = cx_szmul_impl(SIZE_MAX, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 e = cx_szmul_impl(0, SIZE_MAX, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 e = cx_szmul_impl(0, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 #endif // CX_SZMUL_BUILTIN

mercurial