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