Fri, 21 Apr 2023 18:38:18 +0200
simplify CxHashKey
633 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 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/utils.h" | |
30 | ||
674
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
31 | #include "cx/buffer.h" |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
32 | |
633 | 33 | #include <gtest/gtest.h> |
34 | ||
674
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
35 | TEST(Utils, cx_stream_bncopy) { |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
36 | CxBuffer source, target; |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
37 | char sbuf[32], tbuf[32]; |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
38 | memset(tbuf, 0, 32); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
39 | cxBufferInit(&source, sbuf, 32, nullptr, 0); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
40 | cxBufferInit(&target, tbuf, 32, nullptr, 0); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
41 | cxBufferPutString(&source, "This is a stream copy test."); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
42 | cxBufferSeek(&source, 0, SEEK_SET); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
43 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
44 | char tmp[4]; |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
45 | size_t result = cx_stream_bncopy(&source, &target, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
46 | (cx_read_func) cxBufferRead, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
47 | (cx_write_func) cxBufferWrite, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
48 | tmp, 4, 20); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
49 | EXPECT_EQ(20, result); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
50 | EXPECT_EQ(20, target.size); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
51 | EXPECT_STREQ("This is a stream cop\0", tbuf); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
52 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
53 | result = cx_stream_bcopy(&source, &target, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
54 | (cx_read_func) cxBufferRead, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
55 | (cx_write_func) cxBufferWrite, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
56 | nullptr, 16); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
57 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
58 | EXPECT_EQ(7, result); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
59 | EXPECT_EQ(27, target.size); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
60 | EXPECT_STREQ("This is a stream copy test.\0", tbuf); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
61 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
62 | cxBufferDestroy(&source); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
63 | cxBufferDestroy(&target); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
64 | } |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
65 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
66 | TEST(Utils, cx_stream_ncopy) { |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
67 | CxBuffer source, target; |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
68 | char sbuf[32], tbuf[32]; |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
69 | memset(tbuf, 0, 32); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
70 | cxBufferInit(&source, sbuf, 32, nullptr, 0); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
71 | cxBufferInit(&target, tbuf, 32, nullptr, 0); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
72 | cxBufferPutString(&source, "This is a stream copy test."); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
73 | cxBufferSeek(&source, 0, SEEK_SET); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
74 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
75 | size_t result = cx_stream_ncopy(&source, &target, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
76 | (cx_read_func) cxBufferRead, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
77 | (cx_write_func) cxBufferWrite, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
78 | 20); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
79 | EXPECT_EQ(20, result); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
80 | EXPECT_EQ(20, target.size); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
81 | EXPECT_STREQ("This is a stream cop\0", tbuf); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
82 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
83 | result = cx_stream_copy(&source, &target, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
84 | (cx_read_func) cxBufferRead, |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
85 | (cx_write_func) cxBufferWrite); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
86 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
87 | EXPECT_EQ(7, result); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
88 | EXPECT_EQ(27, target.size); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
89 | EXPECT_STREQ("This is a stream copy test.\0", tbuf); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
90 | |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
91 | cxBufferDestroy(&source); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
92 | cxBufferDestroy(&target); |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
93 | } |
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
94 | |
633 | 95 | TEST(Utils, ForN) { |
96 | unsigned j; | |
97 | j = 0; | |
98 | cx_for_n(i, 50) { | |
99 | EXPECT_EQ(i, j); | |
100 | j++; | |
101 | } | |
102 | } | |
103 | ||
646
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
104 | TEST(Utils, swap_ptr) { |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
105 | int i = 5; |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
106 | int j = 8; |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
107 | int *ip = &i; |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
108 | int *jp = &j; |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
109 | cx_swap_ptr(ip, jp); |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
110 | EXPECT_EQ(ip, &j); |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
111 | EXPECT_EQ(jp, &i); |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
112 | } |
dfd0403ff8b6
add pointer swap utility
Mike Becker <universe@uap-core.de>
parents:
633
diff
changeset
|
113 | |
633 | 114 | TEST(Utils, szmul) { |
115 | size_t r; | |
116 | int e; | |
117 | e = cx_szmul(5, 7, &r); | |
118 | EXPECT_EQ(0, e); | |
119 | EXPECT_EQ(35, r); | |
120 | ||
121 | size_t s = SIZE_MAX & ~3; | |
122 | ||
123 | e = cx_szmul(s / 4, 2, &r); | |
124 | EXPECT_EQ(0, e); | |
125 | EXPECT_EQ(s / 2, r); | |
126 | e = cx_szmul(2, s / 4, &r); | |
127 | EXPECT_EQ(0, e); | |
128 | EXPECT_EQ(s / 2, r); | |
129 | ||
130 | e = cx_szmul(s / 4, 4, &r); | |
131 | EXPECT_EQ(0, e); | |
132 | EXPECT_EQ(s, r); | |
133 | ||
134 | e = cx_szmul(4, s / 4, &r); | |
135 | EXPECT_EQ(0, e); | |
136 | EXPECT_EQ(s, r); | |
137 | ||
138 | e = cx_szmul(s / 4, 5, &r); | |
139 | EXPECT_NE(0, e); | |
140 | ||
141 | e = cx_szmul(5, s / 4, &r); | |
142 | EXPECT_NE(0, e); | |
143 | ||
144 | e = cx_szmul(SIZE_MAX - 4, 0, &r); | |
145 | EXPECT_EQ(0, e); | |
146 | EXPECT_EQ(0, r); | |
147 | ||
148 | e = cx_szmul(0, SIZE_MAX - 1, &r); | |
149 | EXPECT_EQ(0, e); | |
150 | EXPECT_EQ(0, r); | |
151 | ||
152 | e = cx_szmul(SIZE_MAX, 0, &r); | |
153 | EXPECT_EQ(0, e); | |
154 | EXPECT_EQ(0, r); | |
155 | ||
156 | e = cx_szmul(0, SIZE_MAX, &r); | |
157 | EXPECT_EQ(0, e); | |
158 | EXPECT_EQ(0, r); | |
159 | ||
160 | e = cx_szmul(0, 0, &r); | |
161 | EXPECT_EQ(0, e); | |
162 | EXPECT_EQ(0, r); | |
163 | } | |
164 | ||
165 | #ifdef CX_SZMUL_BUILTIN | |
166 | ||
167 | // also test the custom implementation | |
168 | struct Utils_szmul_impl : ::testing::Test { | |
169 | #undef CX_SZMUL_BUILTIN | |
170 | ||
674
dc514a5d42a5
add stream copy utils - fixes #254
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
171 | #include "../src/szmul.c" |
633 | 172 | |
173 | #define CX_SZMUL_BUILTIN | |
174 | }; | |
175 | ||
176 | TEST_F(Utils_szmul_impl, Test) { | |
177 | size_t r; | |
178 | int e; | |
179 | e = cx_szmul_impl(5, 7, &r); | |
180 | EXPECT_EQ(0, e); | |
181 | EXPECT_EQ(35, r); | |
182 | ||
183 | size_t s = SIZE_MAX & ~3; | |
184 | ||
185 | e = cx_szmul_impl(s / 4, 2, &r); | |
186 | EXPECT_EQ(0, e); | |
187 | EXPECT_EQ(s / 2, r); | |
188 | e = cx_szmul_impl(2, s / 4, &r); | |
189 | EXPECT_EQ(0, e); | |
190 | EXPECT_EQ(s / 2, r); | |
191 | ||
192 | e = cx_szmul_impl(s / 4, 4, &r); | |
193 | EXPECT_EQ(0, e); | |
194 | EXPECT_EQ(s, r); | |
195 | ||
196 | e = cx_szmul_impl(4, s / 4, &r); | |
197 | EXPECT_EQ(0, e); | |
198 | EXPECT_EQ(s, r); | |
199 | ||
200 | e = cx_szmul_impl(s / 4, 5, &r); | |
201 | EXPECT_NE(0, e); | |
202 | ||
203 | e = cx_szmul_impl(5, s / 4, &r); | |
204 | EXPECT_NE(0, e); | |
205 | ||
206 | e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); | |
207 | EXPECT_EQ(0, e); | |
208 | EXPECT_EQ(0, r); | |
209 | ||
210 | e = cx_szmul_impl(0, SIZE_MAX - 1, &r); | |
211 | EXPECT_EQ(0, e); | |
212 | EXPECT_EQ(0, r); | |
213 | ||
214 | e = cx_szmul_impl(SIZE_MAX, 0, &r); | |
215 | EXPECT_EQ(0, e); | |
216 | EXPECT_EQ(0, r); | |
217 | ||
218 | e = cx_szmul_impl(0, SIZE_MAX, &r); | |
219 | EXPECT_EQ(0, e); | |
220 | EXPECT_EQ(0, r); | |
221 | ||
222 | e = cx_szmul_impl(0, 0, &r); | |
223 | EXPECT_EQ(0, e); | |
224 | EXPECT_EQ(0, r); | |
225 | } | |
226 | ||
227 | #endif // CX_SZMUL_BUILTIN |