| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Descent Developers |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include "byteswap.h" |
| 22 | |
| 23 | // This code taken from original byteswap.h for testing float conversion |
| 24 | // It cannot convert negative float numbers in 64-bit systems, so testing only non-negative numbers |
| 25 | |
| 26 | #define SWAPINT(x) (int)(((x) << 24) | (((uint32_t)(x)) >> 24) | (((x) & 0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8)) |
| 27 | |
| 28 | // Stupid function to trick the compiler into letting me byteswap a float |
| 29 | inline float SWAPFLOAT(float x) { |
| 30 | int i = SWAPINT(*((int *)&(x))); |
| 31 | return *((float *)&(i)); |
| 32 | } |
| 33 | |
| 34 | // Taken from CFILE.cpp |
| 35 | inline double SWAPDOUBLE(double x) { |
| 36 | double t; |
| 37 | int *sp = (int *)&x; |
| 38 | int *dp = (int *)&t; |
| 39 | dp[0] = SWAPINT(sp[1]); |
| 40 | dp[1] = SWAPINT(sp[0]); |
| 41 | x = t; |
| 42 | |
| 43 | return x; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | TEST(D3, ByteSwap) { |
| 48 | EXPECT_EQ(D3::byteswap((uint8_t)0x01), (uint8_t)0x01); |
| 49 | EXPECT_EQ(D3::byteswap((int8_t)0x01), (int16_t)0x01); |
| 50 | EXPECT_EQ(D3::byteswap((uint16_t)0x0123), (uint16_t)0x2301); |
| 51 | EXPECT_EQ(D3::byteswap((int16_t)0x0123), (int16_t)0x2301); |
| 52 | EXPECT_EQ(D3::byteswap((int32_t)0x01234567), (int32_t)0x67452301); |
| 53 | EXPECT_EQ(D3::byteswap((int64_t)0x0123456789ABCDEF), (int64_t)0xEFCDAB8967452301); |
| 54 | |
| 55 | EXPECT_EQ(D3::byteswap((int32_t)(0xFF000000)), (int32_t)0xFF); |
| 56 | } |
| 57 | |
| 58 | TEST(D3, Converting) { |
| 59 | #ifndef OUTRAGE_BIG_ENDIAN |
| 60 | EXPECT_EQ(D3::convert_le((int16_t)0x10), 0x10); |
| 61 | EXPECT_EQ(D3::convert_le((int32_t)0x10), 0x10); |
| 62 | EXPECT_EQ(D3::convert_le((int64_t)0x10), 0x10); |
| 63 | |
| 64 | EXPECT_EQ(D3::convert_be((int16_t)0x10), 0x1000); |
| 65 | EXPECT_EQ(D3::convert_be((int32_t)0x10), 0x10000000); |
| 66 | EXPECT_EQ(D3::convert_be((int64_t)0x10), 0x1000000000000000); |
| 67 | #else |
| 68 | EXPECT_EQ(D3::convert_be((int16_t)0x10), 0x10); |
| 69 | EXPECT_EQ(D3::convert_be((int32_t)0x10), 0x10); |
| 70 | EXPECT_EQ(D3::convert_be((int64_t)0x10), 0x10); |
| 71 | |
| 72 | EXPECT_EQ(D3::convert_le((int16_t)0x10), 0x1000); |
| 73 | EXPECT_EQ(D3::convert_le((int16_t)0x10), 0x10000000); |
| 74 | EXPECT_EQ(D3::convert_le((int16_t)0x10), 0x1000000000000000); |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | TEST(D3, FloatDoubleOperations) { |
| 79 | // float/double sanity checks |
| 80 | EXPECT_EQ(sizeof(float), 4); |
| 81 | EXPECT_EQ(sizeof(double), 8); |
| 82 | |
| 83 | EXPECT_EQ(D3::byteswap(0.0f), SWAPFLOAT(0.0f)); |
| 84 | EXPECT_EQ(D3::byteswap(10.0f), SWAPFLOAT(10.0f)); |
| 85 | EXPECT_EQ(D3::byteswap(10000.0f), SWAPFLOAT(10000.0f)); |
| 86 | |
| 87 | EXPECT_EQ(D3::byteswap(0.0), SWAPDOUBLE(0.0)); |
| 88 | EXPECT_EQ(D3::byteswap(10.0), SWAPDOUBLE(10.0)); |
| 89 | EXPECT_EQ(D3::byteswap(10000.0), SWAPDOUBLE(10000.0)); |
| 90 | } |