Tue, 20 Dec 2022 15:08:08 +0100
add attributes to printf functions
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 | ||
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, szmul) { | |
43 | size_t r; | |
44 | int e; | |
45 | e = cx_szmul(5, 7, &r); | |
46 | EXPECT_EQ(0, e); | |
47 | EXPECT_EQ(35, r); | |
48 | ||
49 | size_t s = SIZE_MAX & ~3; | |
50 | ||
51 | e = cx_szmul(s / 4, 2, &r); | |
52 | EXPECT_EQ(0, e); | |
53 | EXPECT_EQ(s / 2, r); | |
54 | e = cx_szmul(2, s / 4, &r); | |
55 | EXPECT_EQ(0, e); | |
56 | EXPECT_EQ(s / 2, r); | |
57 | ||
58 | e = cx_szmul(s / 4, 4, &r); | |
59 | EXPECT_EQ(0, e); | |
60 | EXPECT_EQ(s, r); | |
61 | ||
62 | e = cx_szmul(4, s / 4, &r); | |
63 | EXPECT_EQ(0, e); | |
64 | EXPECT_EQ(s, r); | |
65 | ||
66 | e = cx_szmul(s / 4, 5, &r); | |
67 | EXPECT_NE(0, e); | |
68 | ||
69 | e = cx_szmul(5, s / 4, &r); | |
70 | EXPECT_NE(0, e); | |
71 | ||
72 | e = cx_szmul(SIZE_MAX - 4, 0, &r); | |
73 | EXPECT_EQ(0, e); | |
74 | EXPECT_EQ(0, r); | |
75 | ||
76 | e = cx_szmul(0, SIZE_MAX - 1, &r); | |
77 | EXPECT_EQ(0, e); | |
78 | EXPECT_EQ(0, r); | |
79 | ||
80 | e = cx_szmul(SIZE_MAX, 0, &r); | |
81 | EXPECT_EQ(0, e); | |
82 | EXPECT_EQ(0, r); | |
83 | ||
84 | e = cx_szmul(0, SIZE_MAX, &r); | |
85 | EXPECT_EQ(0, e); | |
86 | EXPECT_EQ(0, r); | |
87 | ||
88 | e = cx_szmul(0, 0, &r); | |
89 | EXPECT_EQ(0, e); | |
90 | EXPECT_EQ(0, r); | |
91 | } | |
92 | ||
93 | #ifdef CX_SZMUL_BUILTIN | |
94 | ||
95 | // also test the custom implementation | |
96 | struct Utils_szmul_impl : ::testing::Test { | |
97 | #undef CX_SZMUL_BUILTIN | |
98 | ||
99 | #include "../src/utils.c" | |
100 | ||
101 | #define CX_SZMUL_BUILTIN | |
102 | }; | |
103 | ||
104 | TEST_F(Utils_szmul_impl, Test) { | |
105 | size_t r; | |
106 | int e; | |
107 | e = cx_szmul_impl(5, 7, &r); | |
108 | EXPECT_EQ(0, e); | |
109 | EXPECT_EQ(35, r); | |
110 | ||
111 | size_t s = SIZE_MAX & ~3; | |
112 | ||
113 | e = cx_szmul_impl(s / 4, 2, &r); | |
114 | EXPECT_EQ(0, e); | |
115 | EXPECT_EQ(s / 2, r); | |
116 | e = cx_szmul_impl(2, s / 4, &r); | |
117 | EXPECT_EQ(0, e); | |
118 | EXPECT_EQ(s / 2, r); | |
119 | ||
120 | e = cx_szmul_impl(s / 4, 4, &r); | |
121 | EXPECT_EQ(0, e); | |
122 | EXPECT_EQ(s, r); | |
123 | ||
124 | e = cx_szmul_impl(4, s / 4, &r); | |
125 | EXPECT_EQ(0, e); | |
126 | EXPECT_EQ(s, r); | |
127 | ||
128 | e = cx_szmul_impl(s / 4, 5, &r); | |
129 | EXPECT_NE(0, e); | |
130 | ||
131 | e = cx_szmul_impl(5, s / 4, &r); | |
132 | EXPECT_NE(0, e); | |
133 | ||
134 | e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); | |
135 | EXPECT_EQ(0, e); | |
136 | EXPECT_EQ(0, r); | |
137 | ||
138 | e = cx_szmul_impl(0, SIZE_MAX - 1, &r); | |
139 | EXPECT_EQ(0, e); | |
140 | EXPECT_EQ(0, r); | |
141 | ||
142 | e = cx_szmul_impl(SIZE_MAX, 0, &r); | |
143 | EXPECT_EQ(0, e); | |
144 | EXPECT_EQ(0, r); | |
145 | ||
146 | e = cx_szmul_impl(0, SIZE_MAX, &r); | |
147 | EXPECT_EQ(0, e); | |
148 | EXPECT_EQ(0, r); | |
149 | ||
150 | e = cx_szmul_impl(0, 0, &r); | |
151 | EXPECT_EQ(0, e); | |
152 | EXPECT_EQ(0, r); | |
153 | } | |
154 | ||
155 | #endif // CX_SZMUL_BUILTIN |