Unit test of the byte-swapping operations that TensorBundle uses.
| 307 | |
| 308 | // Unit test of the byte-swapping operations that TensorBundle uses. |
| 309 | TEST(TensorBundleTest, SwapBytes) { |
| 310 | // A bug in the compiler on MacOS causes ByteSwap() and FlipEndiannessBit() |
| 311 | // to be removed from the executable if they are only called from templated |
| 312 | // functions. As a workaround, we make some dummy calls here. |
| 313 | // TODO(frreiss): Remove this workaround when the compiler bug is fixed. |
| 314 | ByteSwap(Constant_2x3<int>(42)); |
| 315 | EXPECT_NE(Status::OK(), FlipEndiannessBit(Prefix("not_a_valid_prefix"))); |
| 316 | |
| 317 | // Test patterns, manually swapped so that we aren't relying on the |
| 318 | // correctness of our own byte-swapping macros when testing those macros. |
| 319 | // At least one of the entries in each list has the sign bit set when |
| 320 | // interpreted as a signed int. |
| 321 | const int arr_len_16 = 4; |
| 322 | const uint16_t forward_16[] = {0x1de5, 0xd017, 0xf1ea, 0xc0a1}; |
| 323 | const uint16_t swapped_16[] = {0xe51d, 0x17d0, 0xeaf1, 0xa1c0}; |
| 324 | const int arr_len_32 = 2; |
| 325 | const uint32_t forward_32[] = {0x0ddba115, 0xf01dab1e}; |
| 326 | const uint32_t swapped_32[] = {0x15a1db0d, 0x1eab1df0}; |
| 327 | const int arr_len_64 = 2; |
| 328 | const uint64_t forward_64[] = {0xf005ba11caba1000, 0x5ca1ab1ecab005e5}; |
| 329 | const uint64_t swapped_64[] = {0x0010baca11ba05f0, 0xe505b0ca1eaba15c}; |
| 330 | |
| 331 | // 16-bit types |
| 332 | TestByteSwap(forward_16, swapped_16, arr_len_16); |
| 333 | TestByteSwap(reinterpret_cast<const int16_t*>(forward_16), |
| 334 | reinterpret_cast<const int16_t*>(swapped_16), arr_len_16); |
| 335 | TestByteSwap(reinterpret_cast<const bfloat16*>(forward_16), |
| 336 | reinterpret_cast<const bfloat16*>(swapped_16), arr_len_16); |
| 337 | |
| 338 | // 32-bit types |
| 339 | TestByteSwap(forward_32, swapped_32, arr_len_32); |
| 340 | TestByteSwap(reinterpret_cast<const int32_t*>(forward_32), |
| 341 | reinterpret_cast<const int32_t*>(swapped_32), arr_len_32); |
| 342 | TestByteSwap(reinterpret_cast<const float*>(forward_32), |
| 343 | reinterpret_cast<const float*>(swapped_32), arr_len_32); |
| 344 | |
| 345 | // 64-bit types |
| 346 | // Cast to uint64*/int64* to make DataTypeToEnum<T> happy |
| 347 | TestByteSwap(reinterpret_cast<const uint64*>(forward_64), |
| 348 | reinterpret_cast<const uint64*>(swapped_64), arr_len_64); |
| 349 | TestByteSwap(reinterpret_cast<const int64*>(forward_64), |
| 350 | reinterpret_cast<const int64*>(swapped_64), arr_len_64); |
| 351 | TestByteSwap(reinterpret_cast<const double*>(forward_64), |
| 352 | reinterpret_cast<const double*>(swapped_64), arr_len_64); |
| 353 | |
| 354 | // Complex types. |
| 355 | // Logic for complex number handling is only in ByteSwapTensor, so don't test |
| 356 | // ByteSwapArray |
| 357 | const float* forward_float = reinterpret_cast<const float*>(forward_32); |
| 358 | const float* swapped_float = reinterpret_cast<const float*>(swapped_32); |
| 359 | const double* forward_double = reinterpret_cast<const double*>(forward_64); |
| 360 | const double* swapped_double = reinterpret_cast<const double*>(swapped_64); |
| 361 | Tensor forward_complex64 = Constant_2x3<complex64>( |
| 362 | std::complex<float>(forward_float[0], forward_float[1])); |
| 363 | Tensor swapped_complex64 = Constant_2x3<complex64>( |
| 364 | std::complex<float>(swapped_float[0], swapped_float[1])); |
| 365 | Tensor forward_complex128 = Constant_2x3<complex128>( |
| 366 | std::complex<double>(forward_double[0], forward_double[1])); |
nothing calls this directly
no test coverage detected