Files
Endianness/main.cpp
2025-06-04 18:41:56 -05:00

91 lines
3.5 KiB
C++

//
// Created by josh on 6/4/2025.
//
#include <Endianness.hpp>
#include <iomanip>
#include <iostream>
// Template function to perform a round-trip test
template <typename T>
void TestRoundTrip(const std::string& type_name, T original_value) {
T network_value = Endianness::HostToNetworkOrder(original_value);
T host_value = Endianness::NetworkToHostOrder(network_value);
std::cout << "Testing " << type_name << ": " << std::endl;
if constexpr (std::is_floating_point_v<T>) {
std::cout << " Original: " << original_value << std::endl;
std::cout << " Network (raw bits): 0x" << std::hex << std::setw(sizeof(T) * 2) << std::setfill('0');
if constexpr (std::is_same_v<T, float>) {
std::cout << *reinterpret_cast<uint32_t*>(&network_value);
} else { // double
std::cout << *reinterpret_cast<uint64_t*>(&network_value);
}
std::cout << std::dec << std::endl;
std::cout << " Host: " << host_value << std::endl;
} else {
std::cout << " Original: 0x" << std::hex << original_value << std::dec << std::endl;
std::cout << " Network: 0x" << std::hex << network_value << std::dec << std::endl;
std::cout << " Host: 0x" << std::hex << host_value << std::dec << std::endl;
}
if (original_value == host_value) {
std::cout << " Result: SUCCESS" << std::endl;
} else {
std::cout << " Result: FAILED - Discrepancy found!" << std::endl;
}
std::cout << std::endl;
}
// Template function to test ReverseByteOrder
template <typename T>
void TestReverseByteOrder(const std::string& type_name, T original_value) {
T reversed_value = Endianness::ReverseByteOrder(original_value);
std::cout << "Testing ReverseByteOrder for " << type_name << ": " << std::endl;
std::cout << " Original: 0x" << std::hex << original_value << std::dec << std::endl;
std::cout << " Reversed: 0x" << std::hex << reversed_value << std::dec << std::endl;
std::cout << std::endl;
}
// Template function to test ReverseByteOrderIfLittleEndian
template <typename T>
void TestReverseByteOrderIfLittleEndian(const std::string& type_name, T original_value) {
T conditional_reversed_value = Endianness::ReverseByteOrderIfLittleEndian(original_value);
std::cout << "Testing ReverseByteOrderIfLittleEndian for " << type_name << ": " << std::endl;
std::cout << " Original: 0x" << std::hex << original_value << std::dec << std::endl;
std::cout << " Conditional Reversed: 0x" << std::hex << conditional_reversed_value << std::dec << std::endl;
if (Endianness::IsLittleEndian()) {
T expected_reversed = Endianness::ReverseByteOrder(original_value);
if (conditional_reversed_value == expected_reversed) {
std::cout << " Result (Little Endian Host): SUCCESS (Bytes were reversed)" << std::endl;
} else {
std::cout << " Result (Little Endian Host): FAILED (Bytes were NOT reversed as expected)" << std::endl;
}
} else { // Big Endian
if (conditional_reversed_value == original_value) {
std::cout << " Result (Big Endian Host): SUCCESS (No reversal expected)" << std::endl;
} else {
std::cout << " Result (Big Endian Host): FAILED (Reversal happened unexpectedly)" << std::endl;
}
}
std::cout << std::endl;
}
int main()
{
for (int i = 0; i < 256; i++)
{
TestRoundTrip("u8", i);
TestRoundTrip("u16", i*i);
TestRoundTrip("u32", i*i*i);
TestRoundTrip("u64", i*i*i*i);
}
return 0;
}