vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / support / ByteOrderTest.cpp
blobac20abddc1a40aab047ddc03b6c7ceb62836fa34
1 /*
2 * Copyright 2004-2010, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "ByteOrderTest.h"
9 #include <math.h>
10 #include <string.h>
12 #include <ByteOrder.h>
14 #include <cppunit/TestCaller.h>
15 #include <cppunit/TestSuite.h>
17 #include <TestUtils.h>
20 using namespace CppUnit;
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.
28 class Swap16Test : public BTestCase {
29 public:
30 Swap16Test(std::string name = "");
32 static Test *suite(void);
33 void test(void);
37 Swap16Test::Swap16Test(std::string name)
38 : BTestCase(name)
43 Test *
44 Swap16Test::suite(void)
46 return new CppUnit::TestCaller<Swap16Test>("ByteOrderTest::Swap16Test", &Swap16Test::test);
50 void
51 Swap16Test::test(void)
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;
61 CHK(kNull == __swap_int16(kNull));
62 CHK(kAscendingSwapped == __swap_int16(kAscending));
63 CHK(kNegativeSwapped == __swap_int16(kNegative));
64 CHK(kMixSwapped == __swap_int16(kMix));
68 // #pragma mark -
71 class Swap32Test : public BTestCase {
72 public:
73 Swap32Test(std::string name = "");
75 static Test *suite(void);
76 void test(void);
80 Swap32Test::Swap32Test(std::string name)
81 : BTestCase(name)
86 Test *
87 Swap32Test::suite(void)
89 return new CppUnit::TestCaller<Swap32Test>("ByteOrderTest::Swap32Test", &Swap32Test::test);
93 void
94 Swap32Test::test(void)
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;
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));
111 // #pragma mark -
114 class Swap64Test : public BTestCase {
115 public:
116 Swap64Test(std::string name = "");
118 static Test *suite(void);
119 void test(void);
123 Swap64Test::Swap64Test(std::string name)
124 : BTestCase(name)
129 Test *
130 Swap64Test::suite(void)
132 return new CppUnit::TestCaller<Swap64Test>("ByteOrderTest::Swap64Test", &Swap64Test::test);
136 void
137 Swap64Test::test(void)
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;
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));
154 // #pragma mark -
157 class SwapFloatTest : public BTestCase {
158 public:
159 SwapFloatTest(std::string name = "");
161 static Test *suite(void);
162 void test(void);
166 SwapFloatTest::SwapFloatTest(std::string name)
167 : BTestCase(name)
172 Test *
173 SwapFloatTest::suite(void)
175 return new CppUnit::TestCaller<SwapFloatTest>("ByteOrderTest::SwapFloatTest", &SwapFloatTest::test);
179 void
180 SwapFloatTest::test(void)
182 const float kNumber = 1.125;
183 const float kNaN = NAN;
184 const float kInfinity = HUGE_VALF;
186 CHK(kNumber == __swap_float(__swap_float(kNumber)));
187 CHK(kNaN == __swap_float(__swap_float(kNaN)));
188 CHK(kInfinity == __swap_float(__swap_float(kInfinity)));
192 // #pragma mark -
195 class SwapDoubleTest : public BTestCase {
196 public:
197 SwapDoubleTest(std::string name = "");
199 static Test *suite(void);
200 void test(void);
204 SwapDoubleTest::SwapDoubleTest(std::string name)
205 : BTestCase(name)
210 Test *
211 SwapDoubleTest::suite(void)
213 return new CppUnit::TestCaller<SwapDoubleTest>("ByteOrderTest::SwapDoubleTest", &SwapDoubleTest::test);
217 void
218 SwapDoubleTest::test(void)
220 const double kNumber = 1.125;
221 const double kNaN = NAN;
222 const double kInfinity = HUGE_VAL;
224 CHK(kNumber == __swap_double(__swap_double(kNumber)));
225 CHK(kNaN == __swap_double(__swap_double(kNaN)));
226 CHK(kInfinity == __swap_double(__swap_double(kInfinity)));
230 // #pragma mark -
233 class SwapDataTest : public BTestCase {
234 public:
235 SwapDataTest(std::string name = "");
237 static Test *suite(void);
238 void test(void);
242 SwapDataTest::SwapDataTest(std::string name)
243 : BTestCase(name)
248 Test *
249 SwapDataTest::suite(void)
251 return new CppUnit::TestCaller<SwapDataTest>("ByteOrderTest::SwapDataTest", &SwapDataTest::test);
255 void
256 SwapDataTest::test(void)
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
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))); \
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))); \
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))); \
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))); \
302 const uint64 kArray64[] = {0x0123456789abcdefULL, 0x1234, 0x5678000000000000ULL, 0x0};
303 uint64 array64[4];
304 TEST(B_UINT64_TYPE, kArray64, array64);
306 const uint32 kArray32[] = {0x12345678, 0x1234, 0x56780000, 0x0};
307 uint32 array32[4];
308 TEST(B_UINT32_TYPE, kArray32, array32);
310 const uint16 kArray16[] = {0x1234, 0x12, 0x3400, 0x0};
311 uint16 array16[4];
312 TEST(B_UINT16_TYPE, kArray16, array16);
314 const float kArrayFloat[] = {3.4f, 0.0f, NAN, HUGE_VALF};
315 float arrayFloat[4];
316 TEST(B_FLOAT_TYPE, kArrayFloat, arrayFloat);
318 const float kArrayDouble[] = {3.42, 0.0, NAN, HUGE_VAL};
319 double arrayDouble[4];
320 TEST(B_DOUBLE_TYPE, kArrayDouble, arrayDouble);
322 #undef TEST
326 // #pragma mark -
329 class IsTypeSwappedTest : public BTestCase {
330 public:
331 IsTypeSwappedTest(std::string name = "");
333 static Test *suite(void);
334 void test(void);
338 IsTypeSwappedTest::IsTypeSwappedTest(std::string name)
339 : BTestCase(name)
344 Test *
345 IsTypeSwappedTest::suite(void)
347 return new CppUnit::TestCaller<IsTypeSwappedTest>("ByteOrderTest::IsTypeSwappedTest", &IsTypeSwappedTest::test);
351 void
352 IsTypeSwappedTest::test(void)
354 #define IS_SWAPPED(x) CHK(is_type_swapped(x))
355 #define NOT_SWAPPED(x) CHK(!is_type_swapped(x))
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);
395 NOT_SWAPPED(' ');
396 NOT_SWAPPED('0000');
397 NOT_SWAPPED('1111');
398 NOT_SWAPPED('aaaa');
400 #undef IS_SWAPPED
401 #undef NOT_SWAPPED
405 // #pragma mark -
408 Test *
409 ByteOrderTestSuite()
411 TestSuite *testSuite = new TestSuite();
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()"));
421 return testSuite;