Line data Source code
1 : // Copyright (c) 2019 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdatabase
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // self
21 : //
22 : #include "main.h"
23 :
24 :
25 : // snaplogger lib
26 : //
27 : #include <snapdatabase/exception.h>
28 : #include <snapdatabase/data/convert.h>
29 :
30 :
31 : // C++ lib
32 : //
33 : #include <fstream>
34 :
35 :
36 : // C lib
37 : //
38 : #include <sys/stat.h>
39 : #include <sys/types.h>
40 :
41 :
42 :
43 :
44 :
45 3 : CATCH_TEST_CASE("8 bit convert", "[convert]")
46 : {
47 2 : CATCH_START_SECTION("uint8_t")
48 : {
49 257 : for(uint32_t i(0); i < (1ULL << 8); ++i)
50 : {
51 : char buf[16];
52 :
53 256 : snprintf(buf, sizeof(buf), "%d", i);
54 : {
55 256 : uint64_t const c(snapdatabase::convert_to_int(buf, 8));
56 256 : CATCH_REQUIRE(c == i);
57 : }
58 :
59 256 : snprintf(buf, sizeof(buf), "0x%X", i);
60 : {
61 256 : uint64_t const c(snapdatabase::convert_to_int(buf, 8));
62 256 : CATCH_REQUIRE(c == i);
63 : }
64 :
65 256 : snprintf(buf, sizeof(buf), "0X%X", i);
66 : {
67 256 : uint64_t const c(snapdatabase::convert_to_int(buf, 8));
68 256 : CATCH_REQUIRE(c == i);
69 : }
70 :
71 256 : snprintf(buf, sizeof(buf), "x'%X'", i);
72 : {
73 256 : uint64_t const c(snapdatabase::convert_to_int(buf, 8));
74 256 : CATCH_REQUIRE(c == i);
75 : }
76 :
77 256 : snprintf(buf, sizeof(buf), "X'%X'", i);
78 : {
79 256 : uint64_t const c(snapdatabase::convert_to_int(buf, 8));
80 256 : CATCH_REQUIRE(c == i);
81 : }
82 :
83 256 : uint32_t v(i);
84 512 : std::string r;
85 3842 : while(v != 0)
86 : {
87 1793 : r += (v & 1) + '0';
88 1793 : v >>= 1;
89 : }
90 256 : std::reverse(r.begin(), r.end());
91 256 : r = "0b" + r;
92 : {
93 256 : uint64_t const c(snapdatabase::convert_to_int(r, 8));
94 256 : CATCH_REQUIRE(c == i);
95 : }
96 :
97 256 : r[1] &= 0x5F;
98 : {
99 256 : uint64_t const c(snapdatabase::convert_to_int(r, 8));
100 256 : CATCH_REQUIRE(c == i);
101 : }
102 : }
103 : }
104 : CATCH_END_SECTION()
105 1 : }
106 :
107 :
108 3 : CATCH_TEST_CASE("16 bit convert", "[convert]")
109 : {
110 2 : CATCH_START_SECTION("uint16_t")
111 : {
112 5015 : for(uint32_t i(0); i < (1ULL << 16); i += rand() % 27)
113 : {
114 : char buf[32];
115 :
116 5014 : snprintf(buf, sizeof(buf), "%d", i);
117 : {
118 5014 : uint64_t const c(snapdatabase::convert_to_int(buf, 16));
119 5014 : CATCH_REQUIRE(c == i);
120 : }
121 :
122 5014 : snprintf(buf, sizeof(buf), "0x%X", i);
123 : {
124 5014 : uint64_t const c(snapdatabase::convert_to_int(buf, 16));
125 5014 : CATCH_REQUIRE(c == i);
126 : }
127 :
128 5014 : snprintf(buf, sizeof(buf), "0X%X", i);
129 : {
130 5014 : uint64_t const c(snapdatabase::convert_to_int(buf, 16));
131 5014 : CATCH_REQUIRE(c == i);
132 : }
133 :
134 5014 : snprintf(buf, sizeof(buf), "x'%X'", i);
135 : {
136 5014 : uint64_t const c(snapdatabase::convert_to_int(buf, 16));
137 5014 : CATCH_REQUIRE(c == i);
138 : }
139 :
140 5014 : snprintf(buf, sizeof(buf), "X'%X'", i);
141 : {
142 5014 : uint64_t const c(snapdatabase::convert_to_int(buf, 16));
143 5014 : CATCH_REQUIRE(c == i);
144 : }
145 :
146 5014 : uint32_t v(i);
147 10028 : std::string r;
148 155516 : while(v != 0)
149 : {
150 75251 : r += (v & 1) + '0';
151 75251 : v >>= 1;
152 : }
153 5014 : std::reverse(r.begin(), r.end());
154 5014 : r = "0b" + r;
155 : {
156 5014 : uint64_t const c(snapdatabase::convert_to_int(r, 16));
157 5014 : CATCH_REQUIRE(c == i);
158 : }
159 :
160 5014 : r[1] &= 0x5F;
161 : {
162 5014 : uint64_t const c(snapdatabase::convert_to_int(r, 16));
163 5014 : CATCH_REQUIRE(c == i);
164 : }
165 : }
166 : }
167 : CATCH_END_SECTION()
168 1 : }
169 :
170 :
171 3 : CATCH_TEST_CASE("32 bit convert", "[convert]")
172 : {
173 2 : CATCH_START_SECTION("uint32_t")
174 : {
175 143142 : for(uint64_t i(0); i < (1ULL << 32); i += rand() % 60000)
176 : {
177 : char buf[64];
178 :
179 143141 : snprintf(buf, sizeof(buf), "%ld", i);
180 : {
181 143141 : uint64_t const c(snapdatabase::convert_to_int(buf, 32));
182 143141 : CATCH_REQUIRE(c == i);
183 : }
184 :
185 143141 : snprintf(buf, sizeof(buf), "0x%lX", i);
186 : {
187 143141 : uint64_t const c(snapdatabase::convert_to_int(buf, 32));
188 143141 : CATCH_REQUIRE(c == i);
189 : }
190 :
191 143141 : snprintf(buf, sizeof(buf), "0X%lX", i);
192 : {
193 143141 : uint64_t const c(snapdatabase::convert_to_int(buf, 32));
194 143141 : CATCH_REQUIRE(c == i);
195 : }
196 :
197 143141 : snprintf(buf, sizeof(buf), "x'%lX'", i);
198 : {
199 143141 : uint64_t const c(snapdatabase::convert_to_int(buf, 32));
200 143141 : CATCH_REQUIRE(c == i);
201 : }
202 :
203 143141 : snprintf(buf, sizeof(buf), "X'%lX'", i);
204 : {
205 143141 : uint64_t const c(snapdatabase::convert_to_int(buf, 32));
206 143141 : CATCH_REQUIRE(c == i);
207 : }
208 :
209 143141 : uint32_t v(i);
210 286282 : std::string r;
211 9017539 : while(v != 0)
212 : {
213 4437199 : r += (v & 1) + '0';
214 4437199 : v >>= 1;
215 : }
216 143141 : std::reverse(r.begin(), r.end());
217 143141 : r = "0b" + r;
218 : {
219 143141 : uint64_t const c(snapdatabase::convert_to_int(r, 32));
220 143141 : CATCH_REQUIRE(c == i);
221 : }
222 :
223 143141 : r[1] &= 0x5F;
224 : {
225 143141 : uint64_t const c(snapdatabase::convert_to_int(r, 32));
226 143141 : CATCH_REQUIRE(c == i);
227 : }
228 : }
229 : }
230 : CATCH_END_SECTION()
231 7 : }
232 :
233 :
234 :
235 : // vim: ts=4 sw=4 et
|