tests/test_utils.c

changeset 767
d31f4d4075dc
parent 766
e59b76889f00
child 768
0e1cf2cd500e
equal deleted inserted replaced
766:e59b76889f00 767:d31f4d4075dc
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "test_utils.h" 29 #include "cx/test.h"
30
31 #include "cx/utils.h"
32 #include "cx/buffer.h"
33
34 CX_TEST(test_stream_bncopy) {
35 CxBuffer source, target;
36 char sbuf[32], tbuf[32];
37 memset(tbuf, 0, 32);
38 cxBufferInit(&source, sbuf, 32, NULL, 0);
39 cxBufferInit(&target, tbuf, 32, NULL, 0);
40 cxBufferPutString(&source, "This is a stream copy test.");
41 cxBufferSeek(&source, 0, SEEK_SET);
42 char tmp[4];
43
44 CX_TEST_DO {
45 size_t result = cx_stream_bncopy(&source, &target,
46 (cx_read_func) cxBufferRead,
47 (cx_write_func) cxBufferWrite,
48 tmp, 4, 20);
49 CX_TEST_ASSERT(result == 20);
50 CX_TEST_ASSERT(target.size == 20);
51 CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
52
53 result = cx_stream_bcopy(&source, &target,
54 (cx_read_func) cxBufferRead,
55 (cx_write_func) cxBufferWrite,
56 NULL, 16);
57
58 CX_TEST_ASSERT(result == 7);
59 CX_TEST_ASSERT(target.size == 27);
60 CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
61 }
62
63 cxBufferDestroy(&source);
64 cxBufferDestroy(&target);
65 }
66
67 CX_TEST(test_stream_ncopy) {
68 CxBuffer source, target;
69 char sbuf[32], tbuf[32];
70 memset(tbuf, 0, 32);
71 cxBufferInit(&source, sbuf, 32, NULL, 0);
72 cxBufferInit(&target, tbuf, 32, NULL, 0);
73 cxBufferPutString(&source, "This is a stream copy test.");
74 cxBufferSeek(&source, 0, SEEK_SET);
75
76 CX_TEST_DO {
77 size_t result = cx_stream_ncopy(&source, &target,
78 (cx_read_func) cxBufferRead,
79 (cx_write_func) cxBufferWrite,
80 20);
81 CX_TEST_ASSERT(result == 20);
82 CX_TEST_ASSERT(target.size == 20);
83 CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
84
85 result = cx_stream_copy(&source, &target,
86 (cx_read_func) cxBufferRead,
87 (cx_write_func) cxBufferWrite);
88
89 CX_TEST_ASSERT(result == 7);
90 CX_TEST_ASSERT(target.size == 27);
91 CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
92 }
93
94 cxBufferDestroy(&source);
95 cxBufferDestroy(&target);
96 }
97
98 CX_TEST(test_forn) {
99 unsigned j;
100 j = 0;
101 CX_TEST_DO {
102 cx_for_n(i, 50) {
103 CX_TEST_ASSERT(i == j);
104 j++;
105 }
106 }
107 }
108
109 CX_TEST(test_swap_ptr) {
110 int i = 5;
111 int j = 8;
112 int *ip = &i;
113 int *jp = &j;
114 CX_TEST_DO {
115 cx_swap_ptr(ip, jp);
116 CX_TEST_ASSERT(ip == &j);
117 CX_TEST_ASSERT(jp == &i);
118 }
119 }
120
121 CX_TEST(test_szmul) {
122 size_t r;
123 int e;
124
125 CX_TEST_DO {
126 e = cx_szmul(5, 7, &r);
127 CX_TEST_ASSERT(e == 0);
128 CX_TEST_ASSERT(r == 35);
129
130 size_t s = SIZE_MAX & ~3;
131
132 e = cx_szmul(s / 4, 2, &r);
133 CX_TEST_ASSERT(e == 0);
134 CX_TEST_ASSERT(r == s / 2);
135
136 e = cx_szmul(2, s / 4, &r);
137 CX_TEST_ASSERT(e == 0);
138 CX_TEST_ASSERT(r == s / 2);
139
140 e = cx_szmul(s / 4, 4, &r);
141 CX_TEST_ASSERT(e == 0);
142 CX_TEST_ASSERT(r == s);
143
144 e = cx_szmul(4, s / 4, &r);
145 CX_TEST_ASSERT(e == 0);
146 CX_TEST_ASSERT(r == s);
147
148 e = cx_szmul(s / 4, 5, &r);
149 CX_TEST_ASSERT(e != 0);
150
151 e = cx_szmul(5, s / 4, &r);
152 CX_TEST_ASSERT(e != 0);
153
154 e = cx_szmul(SIZE_MAX - 4, 0, &r);
155 CX_TEST_ASSERT(e == 0);
156 CX_TEST_ASSERT(r == 0);
157
158 e = cx_szmul(0, SIZE_MAX - 1, &r);
159 CX_TEST_ASSERT(e == 0);
160 CX_TEST_ASSERT(r == 0);
161
162 e = cx_szmul(SIZE_MAX, 0, &r);
163 CX_TEST_ASSERT(e == 0);
164 CX_TEST_ASSERT(r == 0);
165
166 e = cx_szmul(0, SIZE_MAX, &r);
167 CX_TEST_ASSERT(e == 0);
168 CX_TEST_ASSERT(r == 0);
169
170 e = cx_szmul(0, 0, &r);
171 CX_TEST_ASSERT(e == 0);
172 CX_TEST_ASSERT(r == 0);
173 }
174 }
175
176 #ifdef CX_SZMUL_BUILTIN
177
178 // also test the custom implementation
179 #undef CX_SZMUL_BUILTIN
180
181 #include "../src/szmul.c"
182
183 #define CX_SZMUL_BUILTIN
184
185 CX_TEST(test_szmul_impl) {
186 size_t r;
187 int e;
188
189 CX_TEST_DO {
190 e = cx_szmul_impl(5, 7, &r);
191 CX_TEST_ASSERT(e == 0);
192 CX_TEST_ASSERT(r == 35);
193
194 size_t s = SIZE_MAX & ~3;
195
196 e = cx_szmul_impl(s / 4, 2, &r);
197 CX_TEST_ASSERT(e == 0);
198 CX_TEST_ASSERT(r == s / 2);
199
200 e = cx_szmul_impl(2, s / 4, &r);
201 CX_TEST_ASSERT(e == 0);
202 CX_TEST_ASSERT(r == s / 2);
203
204 e = cx_szmul_impl(s / 4, 4, &r);
205 CX_TEST_ASSERT(e == 0);
206 CX_TEST_ASSERT(r == s);
207
208 e = cx_szmul_impl(4, s / 4, &r);
209 CX_TEST_ASSERT(e == 0);
210 CX_TEST_ASSERT(r == s);
211
212 e = cx_szmul_impl(s / 4, 5, &r);
213 CX_TEST_ASSERT(e != 0);
214
215 e = cx_szmul_impl(5, s / 4, &r);
216 CX_TEST_ASSERT(e != 0);
217
218 e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
219 CX_TEST_ASSERT(e == 0);
220 CX_TEST_ASSERT(r == 0);
221
222 e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
223 CX_TEST_ASSERT(e == 0);
224 CX_TEST_ASSERT(r == 0);
225
226 e = cx_szmul_impl(SIZE_MAX, 0, &r);
227 CX_TEST_ASSERT(e == 0);
228 CX_TEST_ASSERT(r == 0);
229
230 e = cx_szmul_impl(0, SIZE_MAX, &r);
231 CX_TEST_ASSERT(e == 0);
232 CX_TEST_ASSERT(r == 0);
233
234 e = cx_szmul_impl(0, 0, &r);
235 CX_TEST_ASSERT(e == 0);
236 CX_TEST_ASSERT(r == 0);
237 }
238 }
239
240 #endif // CX_SZMUL_BUILTIN
241
30 242
31 CxTestSuite *cx_test_suite_utils(void) { 243 CxTestSuite *cx_test_suite_utils(void) {
32 CxTestSuite *suite = cx_test_suite_new("utils"); 244 CxTestSuite *suite = cx_test_suite_new("utils");
33 245
246 cx_test_register(suite, test_stream_bncopy);
247 cx_test_register(suite, test_stream_ncopy);
248 cx_test_register(suite, test_forn);
249 cx_test_register(suite, test_swap_ptr);
250 cx_test_register(suite, test_szmul);
251 cx_test_register(suite, test_szmul_impl);
252
34 return suite; 253 return suite;
35 } 254 }
36 255
37 256

mercurial