| 281 | // Type-specific subroutine of SwapBytes test below |
| 282 | template <typename T> |
| 283 | void TestByteSwap(const T* forward, const T* swapped, int array_len) { |
| 284 | auto bytes_per_elem = sizeof(T); |
| 285 | |
| 286 | // Convert the entire array at once |
| 287 | std::unique_ptr<T[]> forward_copy(new T[array_len]); |
| 288 | std::memcpy(forward_copy.get(), forward, array_len * bytes_per_elem); |
| 289 | TF_EXPECT_OK(ByteSwapArray(reinterpret_cast<char*>(forward_copy.get()), |
| 290 | bytes_per_elem, array_len)); |
| 291 | for (int i = 0; i < array_len; i++) { |
| 292 | EXPECT_EQ(forward_copy.get()[i], swapped[i]); |
| 293 | } |
| 294 | |
| 295 | // Then the array wrapped in a tensor |
| 296 | auto shape = TensorShape({array_len}); |
| 297 | auto dtype = DataTypeToEnum<T>::value; |
| 298 | Tensor forward_tensor(dtype, shape); |
| 299 | Tensor swapped_tensor(dtype, shape); |
| 300 | std::memcpy(const_cast<char*>(forward_tensor.tensor_data().data()), forward, |
| 301 | array_len * bytes_per_elem); |
| 302 | std::memcpy(const_cast<char*>(swapped_tensor.tensor_data().data()), swapped, |
| 303 | array_len * bytes_per_elem); |
| 304 | TF_EXPECT_OK(ByteSwapTensor(&forward_tensor)); |
| 305 | test::ExpectTensorEqual<T>(forward_tensor, swapped_tensor); |
| 306 | } |
| 307 | |
| 308 | // Unit test of the byte-swapping operations that TensorBundle uses. |
| 309 | TEST(TensorBundleTest, SwapBytes) { |
no test coverage detected