xref: /haiku/src/tests/kits/support/ByteOrderTest.cpp (revision 2897df967633aab846ff4917b53e2af7d1e54eeb)
1 /*
2  * Copyright 2004-2010, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ByteOrderTest.h"
8 
9 #include <math.h>
10 #include <string.h>
11 
12 #include <ByteOrder.h>
13 
14 #include <cppunit/TestCaller.h>
15 #include <cppunit/TestSuite.h>
16 
17 #include <TestUtils.h>
18 
19 
20 using namespace CppUnit;
21 
22 
23 // ToDo: swap_int16() and friends don't really belong here as they are in libroot.so
24 //	The tests might be messed up because of that, and don't test the real thing, as
25 //	long as they don't run on Haiku itself.
26 
27 
28 class Swap16Test : public BTestCase {
29 	public:
30 		Swap16Test(std::string name = "");
31 
32 		static Test *suite(void);
33 		void test(void);
34 };
35 
36 
37 Swap16Test::Swap16Test(std::string name)
38 	: BTestCase(name)
39 {
40 }
41 
42 
43 Test *
44 Swap16Test::suite(void)
45 {
46 	return new CppUnit::TestCaller<Swap16Test>("ByteOrderTest::Swap16Test", &Swap16Test::test);
47 }
48 
49 
50 void
51 Swap16Test::test(void)
52 {
53 	static int16 kNull = 0;
54 	static int16 kAscending = 0x1234;
55 	static int16 kAscendingSwapped = 0x3412;
56 	static int16 kNegative = 0xfedc;
57 	static int16 kNegativeSwapped = 0xdcfe;
58 	static uint16 kMix = 0xfefd;
59 	static uint16 kMixSwapped = 0xfdfe;
60 
61 	CHK(kNull == __swap_int16(kNull));
62 	CHK(kAscendingSwapped == __swap_int16(kAscending));
63 	CHK(kNegativeSwapped == __swap_int16(kNegative));
64 	CHK(kMixSwapped == __swap_int16(kMix));
65 }
66 
67 
68 //	#pragma mark -
69 
70 
71 class Swap32Test : public BTestCase {
72 	public:
73 		Swap32Test(std::string name = "");
74 
75 		static Test *suite(void);
76 		void test(void);
77 };
78 
79 
80 Swap32Test::Swap32Test(std::string name)
81 	: BTestCase(name)
82 {
83 }
84 
85 
86 Test *
87 Swap32Test::suite(void)
88 {
89 	return new CppUnit::TestCaller<Swap32Test>("ByteOrderTest::Swap32Test", &Swap32Test::test);
90 }
91 
92 
93 void
94 Swap32Test::test(void)
95 {
96 	static int32 kNull = 0;
97 	static int32 kAscending = 0x12345678;
98 	static int32 kAscendingSwapped = 0x78563412;
99 	static int32 kNegative = 0xfedcba98;
100 	static int32 kNegativeSwapped = 0x98badcfe;
101 	static uint32 kMix = 0xfefdfcfb;
102 	static uint32 kMixSwapped = 0xfbfcfdfe;
103 
104 	CHK((uint32)kNull == __swap_int32(kNull));
105 	CHK((uint32)kAscendingSwapped == __swap_int32(kAscending));
106 	CHK((uint32)kNegativeSwapped == __swap_int32(kNegative));
107 	CHK(kMixSwapped == __swap_int32(kMix));
108 }
109 
110 
111 //	#pragma mark -
112 
113 
114 class Swap64Test : public BTestCase {
115 	public:
116 		Swap64Test(std::string name = "");
117 
118 		static Test *suite(void);
119 		void test(void);
120 };
121 
122 
123 Swap64Test::Swap64Test(std::string name)
124 	: BTestCase(name)
125 {
126 }
127 
128 
129 Test *
130 Swap64Test::suite(void)
131 {
132 	return new CppUnit::TestCaller<Swap64Test>("ByteOrderTest::Swap64Test", &Swap64Test::test);
133 }
134 
135 
136 void
137 Swap64Test::test(void)
138 {
139 	static int64 kNull = 0LL;
140 	static int64 kAscending = 0x1234567890LL;
141 	static int64 kAscendingSwapped = 0x0000009078563412LL;
142 	static int64 kNegative = 0xfedcba9876543210LL;
143 	static int64 kNegativeSwapped = 0x1032547698badcfeLL;
144 	static uint64 kMix = 0xfefdLL;
145 	static uint64 kMixSwapped = 0xfdfe000000000000LL;
146 
147 	CHK((uint64)kNull == __swap_int64(kNull));
148 	CHK((uint64)kAscendingSwapped == __swap_int64(kAscending));
149 	CHK((uint64)kNegativeSwapped == __swap_int64(kNegative));
150 	CHK(kMixSwapped == __swap_int64(kMix));
151 }
152 
153 
154 //	#pragma mark -
155 
156 
157 class SwapFloatTest : public BTestCase {
158 	public:
159 		SwapFloatTest(std::string name = "");
160 
161 		static Test *suite(void);
162 		void test(void);
163 };
164 
165 
166 SwapFloatTest::SwapFloatTest(std::string name)
167 	: BTestCase(name)
168 {
169 }
170 
171 
172 Test *
173 SwapFloatTest::suite(void)
174 {
175 	return new CppUnit::TestCaller<SwapFloatTest>("ByteOrderTest::SwapFloatTest", &SwapFloatTest::test);
176 }
177 
178 
179 void
180 SwapFloatTest::test(void)
181 {
182 	const float kNumber = 1.125;
183 	const float kNaN = NAN;
184 	const float kInfinity = HUGE_VALF;
185 
186 	CHK(kNumber == __swap_float(__swap_float(kNumber)));
187 	CHK(kNaN == __swap_float(__swap_float(kNaN)));
188 	CHK(kInfinity == __swap_float(__swap_float(kInfinity)));
189 }
190 
191 
192 //	#pragma mark -
193 
194 
195 class SwapDoubleTest : public BTestCase {
196 	public:
197 		SwapDoubleTest(std::string name = "");
198 
199 		static Test *suite(void);
200 		void test(void);
201 };
202 
203 
204 SwapDoubleTest::SwapDoubleTest(std::string name)
205 	: BTestCase(name)
206 {
207 }
208 
209 
210 Test *
211 SwapDoubleTest::suite(void)
212 {
213 	return new CppUnit::TestCaller<SwapDoubleTest>("ByteOrderTest::SwapDoubleTest", &SwapDoubleTest::test);
214 }
215 
216 
217 void
218 SwapDoubleTest::test(void)
219 {
220 	const double kNumber = 1.125;
221 	const double kNaN = NAN;
222 	const double kInfinity = HUGE_VAL;
223 
224 	CHK(kNumber == __swap_double(__swap_double(kNumber)));
225 	CHK(kNaN == __swap_double(__swap_double(kNaN)));
226 	CHK(kInfinity == __swap_double(__swap_double(kInfinity)));
227 }
228 
229 
230 //	#pragma mark -
231 
232 
233 class SwapDataTest : public BTestCase {
234 	public:
235 		SwapDataTest(std::string name = "");
236 
237 		static Test *suite(void);
238 		void test(void);
239 };
240 
241 
242 SwapDataTest::SwapDataTest(std::string name)
243 	: BTestCase(name)
244 {
245 }
246 
247 
248 Test *
249 SwapDataTest::suite(void)
250 {
251 	return new CppUnit::TestCaller<SwapDataTest>("ByteOrderTest::SwapDataTest", &SwapDataTest::test);
252 }
253 
254 
255 void
256 SwapDataTest::test(void)
257 {
258 	// error checking
259 	char string[4];
260 	CHK(swap_data(B_STRING_TYPE, string, 4, B_SWAP_ALWAYS) == B_BAD_VALUE);
261 	int32 num32 = 0;
262 	CHK(swap_data(B_INT32_TYPE, &num32, 0, B_SWAP_ALWAYS) == B_BAD_VALUE);
263 	CHK(swap_data(B_INT32_TYPE, NULL, 4, B_SWAP_ALWAYS) == B_BAD_VALUE);
264 #if B_HOST_IS_LENDIAN
265 	CHK(swap_data(B_INT32_TYPE, NULL, 4, B_SWAP_HOST_TO_LENDIAN) == B_BAD_VALUE);
266 #else
267 	CHK(swap_data(B_INT32_TYPE, NULL, 4, B_SWAP_HOST_TO_BENDIAN) == B_BAD_VALUE);
268 #endif
269 
270 	// algorithm checking
271 #define TEST(type, source, target) \
272 	memcpy(target, source, sizeof(source)); \
273 	for (int32 i = 0; i < 4; i++) { \
274 		if (B_HOST_IS_LENDIAN) { \
275 			swap_data(type, target, sizeof(target), B_SWAP_HOST_TO_LENDIAN); \
276 			CHK(!memcmp(target, source, sizeof(source))); \
277 			swap_data(type, target, sizeof(target), B_SWAP_LENDIAN_TO_HOST); \
278 			CHK(!memcmp(target, source, sizeof(source))); \
279 			\
280 			swap_data(type, target, sizeof(target), B_SWAP_HOST_TO_BENDIAN); \
281 			CHK(memcmp(target, source, sizeof(source))); \
282 			swap_data(type, target, sizeof(target), B_SWAP_BENDIAN_TO_HOST); \
283 			CHK(!memcmp(target, source, sizeof(source))); \
284 		} else if (B_HOST_IS_BENDIAN) { \
285 			swap_data(type, target, sizeof(target), B_SWAP_HOST_TO_BENDIAN); \
286 			CHK(!memcmp(target, source, sizeof(source))); \
287 			swap_data(type, target, sizeof(target), B_SWAP_BENDIAN_TO_HOST); \
288 			CHK(!memcmp(target, source, sizeof(source))); \
289 			\
290 			swap_data(type, target, sizeof(target), B_SWAP_HOST_TO_LENDIAN); \
291 			CHK(memcmp(target, source, sizeof(source))); \
292 			swap_data(type, target, sizeof(target), B_SWAP_LENDIAN_TO_HOST); \
293 			CHK(!memcmp(target, source, sizeof(source))); \
294 		} \
295 		\
296 		swap_data(type, target, sizeof(target), B_SWAP_ALWAYS); \
297 		CHK(memcmp(target, source, sizeof(source))); \
298 		swap_data(type, target, sizeof(target), B_SWAP_ALWAYS); \
299 		CHK(!memcmp(target, source, sizeof(source))); \
300 	}
301 
302 	const uint64 kArray64[] = {0x0123456789abcdefULL, 0x1234, 0x5678000000000000ULL, 0x0};
303 	uint64 array64[4];
304 	TEST(B_UINT64_TYPE, kArray64, array64);
305 
306 	const uint32 kArray32[] = {0x12345678, 0x1234, 0x56780000, 0x0};
307 	uint32 array32[4];
308 	TEST(B_UINT32_TYPE, kArray32, array32);
309 
310 	const uint16 kArray16[] = {0x1234, 0x12, 0x3400, 0x0};
311 	uint16 array16[4];
312 	TEST(B_UINT16_TYPE, kArray16, array16);
313 
314 	const float kArrayFloat[] = {3.4f, 0.0f, NAN, HUGE_VALF};
315 	float arrayFloat[4];
316 	TEST(B_FLOAT_TYPE, kArrayFloat, arrayFloat);
317 
318 	const double kArrayDouble[] = {3.42, 0.0, NAN, HUGE_VAL};
319 	double arrayDouble[4];
320 	TEST(B_DOUBLE_TYPE, kArrayDouble, arrayDouble);
321 
322 #undef TEST
323 }
324 
325 
326 //	#pragma mark -
327 
328 
329 class IsTypeSwappedTest : public BTestCase {
330 	public:
331 		IsTypeSwappedTest(std::string name = "");
332 
333 		static Test *suite(void);
334 		void test(void);
335 };
336 
337 
338 IsTypeSwappedTest::IsTypeSwappedTest(std::string name)
339 	: BTestCase(name)
340 {
341 }
342 
343 
344 Test *
345 IsTypeSwappedTest::suite(void)
346 {
347 	return new CppUnit::TestCaller<IsTypeSwappedTest>("ByteOrderTest::IsTypeSwappedTest", &IsTypeSwappedTest::test);
348 }
349 
350 
351 void
352 IsTypeSwappedTest::test(void)
353 {
354 #define IS_SWAPPED(x) CHK(is_type_swapped(x))
355 #define NOT_SWAPPED(x) CHK(!is_type_swapped(x))
356 
357 	IS_SWAPPED(B_ANY_TYPE);
358 	IS_SWAPPED(B_BOOL_TYPE);
359 	IS_SWAPPED(B_CHAR_TYPE);
360 	IS_SWAPPED(B_COLOR_8_BIT_TYPE);
361 	IS_SWAPPED(B_DOUBLE_TYPE);
362 	IS_SWAPPED(B_FLOAT_TYPE);
363 	IS_SWAPPED(B_GRAYSCALE_8_BIT_TYPE);
364 	IS_SWAPPED(B_INT64_TYPE);
365 	IS_SWAPPED(B_INT32_TYPE);
366 	IS_SWAPPED(B_INT16_TYPE);
367 	IS_SWAPPED(B_INT8_TYPE);
368 	IS_SWAPPED(B_MESSAGE_TYPE);
369 	IS_SWAPPED(B_MESSENGER_TYPE);
370 	IS_SWAPPED(B_MIME_TYPE);
371 	IS_SWAPPED(B_MONOCHROME_1_BIT_TYPE);
372 	IS_SWAPPED(B_OBJECT_TYPE);
373 	IS_SWAPPED(B_OFF_T_TYPE);
374 	IS_SWAPPED(B_PATTERN_TYPE);
375 	IS_SWAPPED(B_POINTER_TYPE);
376 	IS_SWAPPED(B_POINT_TYPE);
377 	IS_SWAPPED(B_RAW_TYPE);
378 	IS_SWAPPED(B_RECT_TYPE);
379 	IS_SWAPPED(B_REF_TYPE);
380 	IS_SWAPPED(B_RGB_32_BIT_TYPE);
381 	IS_SWAPPED(B_RGB_COLOR_TYPE);
382 	IS_SWAPPED(B_SIZE_T_TYPE);
383 	IS_SWAPPED(B_SSIZE_T_TYPE);
384 	IS_SWAPPED(B_STRING_TYPE);
385 	IS_SWAPPED(B_TIME_TYPE);
386 	IS_SWAPPED(B_UINT64_TYPE);
387 	IS_SWAPPED(B_UINT32_TYPE);
388 	IS_SWAPPED(B_UINT16_TYPE);
389 	IS_SWAPPED(B_UINT8_TYPE);
390 	IS_SWAPPED(B_MEDIA_PARAMETER_TYPE);
391 	IS_SWAPPED(B_MEDIA_PARAMETER_WEB_TYPE);
392 	IS_SWAPPED(B_MEDIA_PARAMETER_GROUP_TYPE);
393 	IS_SWAPPED(B_ASCII_TYPE);
394 
395 	NOT_SWAPPED('    ');
396 	NOT_SWAPPED('0000');
397 	NOT_SWAPPED('1111');
398 	NOT_SWAPPED('aaaa');
399 
400 #undef IS_SWAPPED
401 #undef NOT_SWAPPED
402 }
403 
404 
405 //	#pragma mark -
406 
407 
408 Test *
409 ByteOrderTestSuite()
410 {
411 	TestSuite *testSuite = new TestSuite();
412 
413 	testSuite->addTest(new Swap16Test("__swap_int16()"));
414 	testSuite->addTest(new Swap32Test("__swap_int32()"));
415 	testSuite->addTest(new Swap64Test("__swap_int64()"));
416 	testSuite->addTest(new SwapFloatTest("__swap_float()"));
417 	testSuite->addTest(new SwapDoubleTest("__swap_double()"));
418 	testSuite->addTest(new SwapDataTest("swap_data()"));
419 	testSuite->addTest(new IsTypeSwappedTest("is_type_swapped()"));
420 
421 	return testSuite;
422 }
423 
424 
425 
426 
427 
428 
429 
430