tests/test_utils.cpp

changeset 654
c9d008861178
parent 646
dfd0403ff8b6
parent 653
e081643aae2a
child 674
dc514a5d42a5
equal deleted inserted replaced
647:2e6e9d9f2159 654:c9d008861178
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 <gtest/gtest.h>
32
33 TEST(Utils, ForN) {
34 unsigned j;
35 j = 0;
36 cx_for_n(i, 50) {
37 EXPECT_EQ(i, j);
38 j++;
39 }
40 }
41
42 TEST(Utils, swap_ptr) {
43 int i = 5;
44 int j = 8;
45 int *ip = &i;
46 int *jp = &j;
47 cx_swap_ptr(ip, jp);
48 EXPECT_EQ(ip, &j);
49 EXPECT_EQ(jp, &i);
50 }
51
52 TEST(Utils, szmul) {
53 size_t r;
54 int e;
55 e = cx_szmul(5, 7, &r);
56 EXPECT_EQ(0, e);
57 EXPECT_EQ(35, r);
58
59 size_t s = SIZE_MAX & ~3;
60
61 e = cx_szmul(s / 4, 2, &r);
62 EXPECT_EQ(0, e);
63 EXPECT_EQ(s / 2, r);
64 e = cx_szmul(2, s / 4, &r);
65 EXPECT_EQ(0, e);
66 EXPECT_EQ(s / 2, r);
67
68 e = cx_szmul(s / 4, 4, &r);
69 EXPECT_EQ(0, e);
70 EXPECT_EQ(s, r);
71
72 e = cx_szmul(4, s / 4, &r);
73 EXPECT_EQ(0, e);
74 EXPECT_EQ(s, r);
75
76 e = cx_szmul(s / 4, 5, &r);
77 EXPECT_NE(0, e);
78
79 e = cx_szmul(5, s / 4, &r);
80 EXPECT_NE(0, e);
81
82 e = cx_szmul(SIZE_MAX - 4, 0, &r);
83 EXPECT_EQ(0, e);
84 EXPECT_EQ(0, r);
85
86 e = cx_szmul(0, SIZE_MAX - 1, &r);
87 EXPECT_EQ(0, e);
88 EXPECT_EQ(0, r);
89
90 e = cx_szmul(SIZE_MAX, 0, &r);
91 EXPECT_EQ(0, e);
92 EXPECT_EQ(0, r);
93
94 e = cx_szmul(0, SIZE_MAX, &r);
95 EXPECT_EQ(0, e);
96 EXPECT_EQ(0, r);
97
98 e = cx_szmul(0, 0, &r);
99 EXPECT_EQ(0, e);
100 EXPECT_EQ(0, r);
101 }
102
103 #ifdef CX_SZMUL_BUILTIN
104
105 // also test the custom implementation
106 struct Utils_szmul_impl : ::testing::Test {
107 #undef CX_SZMUL_BUILTIN
108
109 #include "../src/utils.c"
110
111 #define CX_SZMUL_BUILTIN
112 };
113
114 TEST_F(Utils_szmul_impl, Test) {
115 size_t r;
116 int e;
117 e = cx_szmul_impl(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_impl(s / 4, 2, &r);
124 EXPECT_EQ(0, e);
125 EXPECT_EQ(s / 2, r);
126 e = cx_szmul_impl(2, s / 4, &r);
127 EXPECT_EQ(0, e);
128 EXPECT_EQ(s / 2, r);
129
130 e = cx_szmul_impl(s / 4, 4, &r);
131 EXPECT_EQ(0, e);
132 EXPECT_EQ(s, r);
133
134 e = cx_szmul_impl(4, s / 4, &r);
135 EXPECT_EQ(0, e);
136 EXPECT_EQ(s, r);
137
138 e = cx_szmul_impl(s / 4, 5, &r);
139 EXPECT_NE(0, e);
140
141 e = cx_szmul_impl(5, s / 4, &r);
142 EXPECT_NE(0, e);
143
144 e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
145 EXPECT_EQ(0, e);
146 EXPECT_EQ(0, r);
147
148 e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
149 EXPECT_EQ(0, e);
150 EXPECT_EQ(0, r);
151
152 e = cx_szmul_impl(SIZE_MAX, 0, &r);
153 EXPECT_EQ(0, e);
154 EXPECT_EQ(0, r);
155
156 e = cx_szmul_impl(0, SIZE_MAX, &r);
157 EXPECT_EQ(0, e);
158 EXPECT_EQ(0, r);
159
160 e = cx_szmul_impl(0, 0, &r);
161 EXPECT_EQ(0, e);
162 EXPECT_EQ(0, r);
163 }
164
165 #endif // CX_SZMUL_BUILTIN

mercurial