tests/test_utils.cpp

changeset 767
d31f4d4075dc
parent 674
dc514a5d42a5
equal deleted inserted replaced
766:e59b76889f00 767:d31f4d4075dc
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
31 #include "cx/buffer.h"
32
33 #include <gtest/gtest.h>
34
35 TEST(Utils, cx_stream_bncopy) {
36 CxBuffer source, target;
37 char sbuf[32], tbuf[32];
38 memset(tbuf, 0, 32);
39 cxBufferInit(&source, sbuf, 32, nullptr, 0);
40 cxBufferInit(&target, tbuf, 32, nullptr, 0);
41 cxBufferPutString(&source, "This is a stream copy test.");
42 cxBufferSeek(&source, 0, SEEK_SET);
43
44 char tmp[4];
45 size_t result = cx_stream_bncopy(&source, &target,
46 (cx_read_func) cxBufferRead,
47 (cx_write_func) cxBufferWrite,
48 tmp, 4, 20);
49 EXPECT_EQ(20, result);
50 EXPECT_EQ(20, target.size);
51 EXPECT_STREQ("This is a stream cop\0", tbuf);
52
53 result = cx_stream_bcopy(&source, &target,
54 (cx_read_func) cxBufferRead,
55 (cx_write_func) cxBufferWrite,
56 nullptr, 16);
57
58 EXPECT_EQ(7, result);
59 EXPECT_EQ(27, target.size);
60 EXPECT_STREQ("This is a stream copy test.\0", tbuf);
61
62 cxBufferDestroy(&source);
63 cxBufferDestroy(&target);
64 }
65
66 TEST(Utils, cx_stream_ncopy) {
67 CxBuffer source, target;
68 char sbuf[32], tbuf[32];
69 memset(tbuf, 0, 32);
70 cxBufferInit(&source, sbuf, 32, nullptr, 0);
71 cxBufferInit(&target, tbuf, 32, nullptr, 0);
72 cxBufferPutString(&source, "This is a stream copy test.");
73 cxBufferSeek(&source, 0, SEEK_SET);
74
75 size_t result = cx_stream_ncopy(&source, &target,
76 (cx_read_func) cxBufferRead,
77 (cx_write_func) cxBufferWrite,
78 20);
79 EXPECT_EQ(20, result);
80 EXPECT_EQ(20, target.size);
81 EXPECT_STREQ("This is a stream cop\0", tbuf);
82
83 result = cx_stream_copy(&source, &target,
84 (cx_read_func) cxBufferRead,
85 (cx_write_func) cxBufferWrite);
86
87 EXPECT_EQ(7, result);
88 EXPECT_EQ(27, target.size);
89 EXPECT_STREQ("This is a stream copy test.\0", tbuf);
90
91 cxBufferDestroy(&source);
92 cxBufferDestroy(&target);
93 }
94
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
104 TEST(Utils, swap_ptr) {
105 int i = 5;
106 int j = 8;
107 int *ip = &i;
108 int *jp = &j;
109 cx_swap_ptr(ip, jp);
110 EXPECT_EQ(ip, &j);
111 EXPECT_EQ(jp, &i);
112 }
113
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
171 #include "../src/szmul.c"
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

mercurial