1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "cx/test.h"
30
31 CX_TEST(test_szmul) {
32 size_t r;
33 int e;
34
35 CX_TEST_DO {
36 e = cx_szmul(
5,
7, &r);
37 CX_TEST_ASSERT(e ==
0);
38 CX_TEST_ASSERT(r ==
35);
39
40 size_t s =
SIZE_MAX & ~
3;
41
42 e = cx_szmul(s /
4,
2, &r);
43 CX_TEST_ASSERT(e ==
0);
44 CX_TEST_ASSERT(r == s /
2);
45
46 e = cx_szmul(
2, s /
4, &r);
47 CX_TEST_ASSERT(e ==
0);
48 CX_TEST_ASSERT(r == s /
2);
49
50 e = cx_szmul(s /
4,
4, &r);
51 CX_TEST_ASSERT(e ==
0);
52 CX_TEST_ASSERT(r == s);
53
54 e = cx_szmul(
4, s /
4, &r);
55 CX_TEST_ASSERT(e ==
0);
56 CX_TEST_ASSERT(r == s);
57
58 e = cx_szmul(s /
4,
5, &r);
59 CX_TEST_ASSERT(e !=
0);
60
61 e = cx_szmul(
5, s /
4, &r);
62 CX_TEST_ASSERT(e !=
0);
63
64 e = cx_szmul(
SIZE_MAX -
4,
0, &r);
65 CX_TEST_ASSERT(e ==
0);
66 CX_TEST_ASSERT(r ==
0);
67
68 e = cx_szmul(
0,
SIZE_MAX -
1, &r);
69 CX_TEST_ASSERT(e ==
0);
70 CX_TEST_ASSERT(r ==
0);
71
72 e = cx_szmul(
SIZE_MAX,
0, &r);
73 CX_TEST_ASSERT(e ==
0);
74 CX_TEST_ASSERT(r ==
0);
75
76 e = cx_szmul(
0,
SIZE_MAX, &r);
77 CX_TEST_ASSERT(e ==
0);
78 CX_TEST_ASSERT(r ==
0);
79
80 e = cx_szmul(
0,
0, &r);
81 CX_TEST_ASSERT(e ==
0);
82 CX_TEST_ASSERT(r ==
0);
83 }
84 }
85
86 #ifdef CX_SZMUL_BUILTIN
87
88
89 #undef CX_SZMUL_BUILTIN
90
91 #include "../src/szmul.c"
92
93 #define CX_SZMUL_BUILTIN
94
95 CX_TEST(test_szmul_impl) {
96 size_t r;
97 int e;
98
99 CX_TEST_DO {
100 e = cx_szmul_impl(
5,
7, &r);
101 CX_TEST_ASSERT(e ==
0);
102 CX_TEST_ASSERT(r ==
35);
103
104 size_t s =
SIZE_MAX & ~
3;
105
106 e = cx_szmul_impl(s /
4,
2, &r);
107 CX_TEST_ASSERT(e ==
0);
108 CX_TEST_ASSERT(r == s /
2);
109
110 e = cx_szmul_impl(
2, s /
4, &r);
111 CX_TEST_ASSERT(e ==
0);
112 CX_TEST_ASSERT(r == s /
2);
113
114 e = cx_szmul_impl(s /
4,
4, &r);
115 CX_TEST_ASSERT(e ==
0);
116 CX_TEST_ASSERT(r == s);
117
118 e = cx_szmul_impl(
4, s /
4, &r);
119 CX_TEST_ASSERT(e ==
0);
120 CX_TEST_ASSERT(r == s);
121
122 e = cx_szmul_impl(s /
4,
5, &r);
123 CX_TEST_ASSERT(e !=
0);
124
125 e = cx_szmul_impl(
5, s /
4, &r);
126 CX_TEST_ASSERT(e !=
0);
127
128 e = cx_szmul_impl(
SIZE_MAX -
4,
0, &r);
129 CX_TEST_ASSERT(e ==
0);
130 CX_TEST_ASSERT(r ==
0);
131
132 e = cx_szmul_impl(
0,
SIZE_MAX -
1, &r);
133 CX_TEST_ASSERT(e ==
0);
134 CX_TEST_ASSERT(r ==
0);
135
136 e = cx_szmul_impl(
SIZE_MAX,
0, &r);
137 CX_TEST_ASSERT(e ==
0);
138 CX_TEST_ASSERT(r ==
0);
139
140 e = cx_szmul_impl(
0,
SIZE_MAX, &r);
141 CX_TEST_ASSERT(e ==
0);
142 CX_TEST_ASSERT(r ==
0);
143
144 e = cx_szmul_impl(
0,
0, &r);
145 CX_TEST_ASSERT(e ==
0);
146 CX_TEST_ASSERT(r ==
0);
147 }
148 }
149
150 #endif
151
152
153 CxTestSuite *cx_test_suite_szmul(
void) {
154 CxTestSuite *suite = cx_test_suite_new(
"szmul");
155
156 cx_test_register(suite, test_szmul);
157 #ifdef CX_SZMUL_BUILTIN
158 cx_test_register(suite, test_szmul_impl);
159 #endif
160
161 return suite;
162 }
163