module;
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <utility>
export module Packet_test;
import MMO.Packet;
import std;
// Encodes
TEST(DataEncoding, uint8Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint8_t>::min(), encoded), 1);
EXPECT_EQ(encoded, std::vector<uint8_t>{0b10000000});
encoded.clear();
}
TEST(DataEncoding, uint8Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint8_t>::max(), encoded), 2);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b01111111, 0b10000001}));
encoded.clear();
}
TEST(DataEncoding, int8Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int8_t>::min(), encoded), 2);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b01000000, 0b11111110}));
encoded.clear();
}
TEST(DataEncoding, int8Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int8_t>::max(), encoded), 2);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b00111111,0b10000001,}));
encoded.clear();
}
TEST(DataEncoding, uint16Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint16_t>::min(), encoded), 1);
EXPECT_EQ(encoded, std::vector<uint8_t>{0b10000000});
encoded.clear();
}
TEST(DataEncoding, uint16Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint16_t>::max(), encoded), 3);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b01111111, 0b01111111, 0b10000011}));
encoded.clear();
}
TEST(DataEncoding, int16Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int16_t>::min(), encoded), 3);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b01000000, 0, 0b11111100}));
encoded.clear();
}
TEST(DataEncoding, int16Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int16_t>::max(), encoded), 3);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b00111111, 0b01111111, 0b10000011}));
encoded.clear();
}
TEST(DataEncoding, uint32Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint32_t>::min(), encoded), 1);
EXPECT_EQ(encoded, std::vector<uint8_t>{0b10000000});
encoded.clear();
}
TEST(DataEncoding, uint32Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint32_t>::max(), encoded), 5);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b10001111}));
encoded.clear();
}
TEST(DataEncoding, int32Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int32_t>::min(), encoded), 5);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b01000000, 0, 0, 0, 0b11110000}));
encoded.clear();
}
TEST(DataEncoding, int32Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int32_t>::max(), encoded), 5);
EXPECT_EQ(encoded, std::vector<uint8_t>({ 0b00111111, 0b01111111, 0b01111111, 0b01111111, 0b10001111}));
encoded.clear();
}
TEST(DataEncoding, uint64Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint64_t>::min(), encoded), 1);
EXPECT_EQ(encoded, std::vector<uint8_t>{0b10000000});
encoded.clear();
}
TEST(DataEncoding, uint64Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<uint64_t>::max(), encoded), 10);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111,
0b01111111, 0b01111111, 0b10000001}));
encoded.clear();
}
TEST(DataEncoding, int64Min) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int64_t>::min(), encoded), 10);
EXPECT_EQ(encoded,
std::vector<uint8_t>({0b01000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b11111110}));
encoded.clear();
}
TEST(DataEncoding, int64Max) {
std::vector<uint8_t> encoded;
EXPECT_EQ(MMO::Networking::encodeValue(std::numeric_limits<int64_t>::max(), encoded), 10);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b00111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111,
0b01111111,
0b01111111, 0b10000001}));
encoded.clear();
}
TEST(DataEncoding, float0) {
const auto value = 0.0f;
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 1);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b11111111}));
encoded.clear();
}
TEST(DataEncoding, floatMin) {
const auto value = std::numeric_limits<float>::min();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 6);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000010, 0b11111110, 0, 0, 0, 0b10001000}));
encoded.clear();
}
TEST(DataEncoding, floatLowest) {
const auto value = std::numeric_limits<float>::lowest();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 6);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b00111111, 0b10000001, 0b01000001, 0 ,0, 0b11110000}));
encoded.clear();
}
TEST(DataEncoding, floatMax) {
const auto value = std::numeric_limits<float>::max();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 6);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b00111111, 0b10000001, 0b00111111, 0b01111111, 0b01111111, 0b10001111}));
encoded.clear();
}
TEST(DataEncoding, floatNan) {
const auto value = std::numeric_limits<float>::quiet_NaN();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 3);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000000, 0b11111110, 0b10000010}));
encoded.clear();
}
TEST(DataEncoding, floatInfPositive) {
const auto value = std::numeric_limits<float>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 3);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000000, 0b11111110, 0b10000001}));
encoded.clear();
}
TEST(DataEncoding, floatInfNegative) {
const auto value = -std::numeric_limits<float>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 3);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000000, 0b11111110, 0b10000000}));
encoded.clear();
}
TEST(DataEncoding, double0) {
const double value = 0.0;
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 1);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b10000000 }));
encoded.clear();
}
TEST(DataEncoding, doubleMin) {
const auto value = std::numeric_limits<double>::min();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 10);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000011, 0b11110000, 0, 0, 0, 0,0,0,0,0b10010000}));
encoded.clear();
}
TEST(DataEncoding, doubleLowest) {
const auto value = std::numeric_limits<double>::lowest();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 10);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0, 0b10010000, 0b01000001,0, 0 ,0,0,0,0, 0b11100000}));
encoded.clear();
}
TEST(DataEncoding, doubleMax) {
const auto value = std::numeric_limits<double>::max();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 10);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0, 0b10010000, 0b00111111, 0b01111111, 0b01111111, 0b01111111, 0b01111111,
0b01111111, 0b01111111, 0b10011111}));
encoded.clear();
}
TEST(DataEncoding, doubleNan) {
const auto value = std::numeric_limits<double>::quiet_NaN();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 3);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000000, 0b11100000, 0b10000010}));
encoded.clear();
}
TEST(DataEncoding, doubleInfPositive) {
const auto value = std::numeric_limits<double>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 3);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000000, 0b11100000, 0b10000001}));
encoded.clear();
}
TEST(DataEncoding, doubleInfNegative) {
const auto value = -std::numeric_limits<double>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
EXPECT_EQ(MMO::Networking::encodeValue(value, encoded), 3);
EXPECT_EQ(encoded,
std::vector<uint8_t>({ 0b01000000, 0b11100000, 0b10000000}));
encoded.clear();
}
// Decodes
TEST(DataDecoding, uint8Min) {
std::vector<uint8_t> encoded = { 0b10000000 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint8_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint8_t>::min());
}
TEST(DataDecoding, uint8Max) {
std::vector<uint8_t> encoded = { 0b01111111, 0b10000001 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint8_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint8_t>::max());
}
TEST(DataDecoding, int8Min) {
std::vector<uint8_t> encoded = { 0b01000000, 0b11111110 };
int read = 0;
auto value = MMO::Networking::decodeValue<int8_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int8_t>::min());
}
TEST(DataDecoding, int8Max) {
std::vector<uint8_t> encoded = { 0b00111111, 0b10000001, };
int read = 0;
auto value = MMO::Networking::decodeValue<int8_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int8_t>::max());
}
TEST(DataDecoding, uint16Min) {
std::vector<uint8_t> encoded = { 0b10000000 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint16_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint16_t>::min());
}
TEST(DataDecoding, uint16Max) {
std::vector<uint8_t> encoded = { 0b01111111, 0b01111111, 0b10000011 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint16_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint16_t>::max());
}
TEST(DataDecoding, int16Min) {
std::vector<uint8_t> encoded = { 0b01000000, 0, 0b11111100 };
int read = 0;
auto value = MMO::Networking::decodeValue<int16_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int16_t>::min());
}
TEST(DataDecoding, int16Max) {
std::vector<uint8_t> encoded = { 0b00111111, 0b01111111, 0b10000011 };
int read = 0;
auto value = MMO::Networking::decodeValue<int16_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int16_t>::max());
}
TEST(DataDecoding, uint32Min) {
std::vector<uint8_t> encoded = { 0b10000000 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint32_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint32_t>::min());
}
TEST(DataDecoding, uint32Max) {
std::vector<uint8_t> encoded = { 0b01111111, 0b01111111, 0b01111111, 0b01111111, 0b10001111 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint32_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint32_t>::max());
}
TEST(DataDecoding, int32Min) {
std::vector<uint8_t> encoded = { 0b01000000, 0, 0, 0, 0b11110000 };
int read = 0;
auto value = MMO::Networking::decodeValue<int32_t, int>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int32_t>::min());
}
TEST(DataDecoding, int32Max) {
std::vector<uint8_t> encoded = { 0b00111111, 0b01111111, 0b01111111, 0b01111111, 0b10001111 };
int read = 0;
auto value = MMO::Networking::decodeValue<int32_t, int>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int32_t>::max());
}
TEST(DataDecoding, uint64Min) {
std::vector<uint8_t> encoded = { 0b10000000 };
int read = 0;
auto value = MMO::Networking::decodeValue<uint64_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint64_t>::min());
}
TEST(DataDecoding, uint64Max) {
std::vector<uint8_t> encoded = {
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b10000001
};
int read = 0;
auto value = MMO::Networking::decodeValue<uint64_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<uint64_t>::max());
}
TEST(DataDecoding, int64Min) {
std::vector<uint8_t> encoded = {
0b01000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b11111110
};
int read = 0;
auto value = MMO::Networking::decodeValue<int64_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int64_t>::min());
}
TEST(DataDecoding, int64Max) {
std::vector<uint8_t> encoded = {
0b00111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b10000001
};
int read = 0;
auto value = MMO::Networking::decodeValue<int64_t>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<int64_t>::max());
}
TEST(DataDecoding, float0) {
std::vector<uint8_t> encoded = { 0b11111111 };
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), 0.0f);
}
TEST(DataDecoding, floatMin) {
std::vector<uint8_t> encoded = {
0b01000010,
0b11111110,
0,
0,
0,
0b10001000
};
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<float>::min());
}
TEST(DataDecoding, floatLowest) {
std::vector<uint8_t> encoded = {
0b00111111,
0b10000001,
0b01000001,
0,
0,
0b11110000
};
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<float>::lowest());
}
TEST(DataDecoding, floatMax) {
std::vector<uint8_t> encoded = {
0b00111111,
0b10000001,
0b00111111,
0b01111111,
0b01111111,
0b10001111
};
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<float>::max());
}
TEST(DataDecoding, floatNan) {
std::vector<uint8_t> encoded = {
0b01000000,
0b11111110,
0b10000010
};
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_TRUE(std::isnan(value.value()));
}
TEST(DataDecoding, floatInfPositive) {
std::vector<uint8_t> encoded = {
0b01000000,
0b11111110,
0b10000001
};
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<float>::infinity());
}
TEST(DataDecoding, floatInfNegative) {
std::vector<uint8_t> encoded = {
0b01000000,
0b11111110,
0b10000000
};
int read = 0;
auto value = MMO::Networking::decodeValue<float>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), -std::numeric_limits<float>::infinity());
}
TEST(DataDecoding, double0) {
std::vector<uint8_t> encoded = { 0b10000000 };
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), 0.0f);
}
TEST(DataDecoding, doubleMin) {
std::vector<uint8_t> encoded = { 0b01000011, 0b11110000, 0, 0, 0, 0, 0, 0, 0, 0b10010000 };
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<double>::min());
}
TEST(DataDecoding, doubleLowest) {
std::vector<uint8_t> encoded = { 0, 0b10010000, 0b01000001, 0, 0, 0, 0, 0, 0, 0b11100000 };
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<double>::lowest());
}
TEST(DataDecoding, doubleMax) {
std::vector<uint8_t> encoded = {
0,
0b10010000,
0b00111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b01111111,
0b10011111
};
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<double>::max());
}
TEST(DataDecoding, doubleNan) {
std::vector<uint8_t> encoded = {
0b01000000,
0b11100000,
0b10000010
};
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_TRUE(std::isnan(value.value()));
}
TEST(DataDecoding, doubleInfPositive) {
std::vector<uint8_t> encoded = {
0b01000000,
0b11100000,
0b10000001
};
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), std::numeric_limits<double>::infinity());
}
TEST(DataDecoding, doubleInfNegative) {
std::vector<uint8_t> encoded = {
0b01000000,
0b11100000,
0b10000000
};
int read = 0;
auto value = MMO::Networking::decodeValue<double>(encoded, read);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(read, encoded.size());
EXPECT_EQ(value.value(), -std::numeric_limits<double>::infinity());
}
template<std::integral T>
T randomValue() {
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<std::conditional_t<std::signed_integral<T>, int64_t, uint64_t>> dis(
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
return static_cast<T>(dis(gen));
}
const auto valueAttempts = 100;
TEST(DataEnDecoding, uint8) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<uint8_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<uint8_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, int8) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<int8_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<int8_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, uint16) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<uint16_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<uint16_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, int16) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<int16_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<int16_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, uint32) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<uint32_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<uint32_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, int32) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<int32_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<int32_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, uint64) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<uint64_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<uint64_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, int64) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = randomValue<int64_t>();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<int64_t>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, floats) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = static_cast<float>(randomValue<int64_t>()) / (static_cast<float>(randomValue<uint32_t>()) +
1);
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, float0) {
const auto value = 0.0f;
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, floatMin) {
const auto value = std::numeric_limits<float>::min();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, floatLowest) {
const auto value = std::numeric_limits<float>::lowest();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, floatMax) {
const auto value = std::numeric_limits<float>::min();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, floatNan) {
const auto value = std::numeric_limits<float>::quiet_NaN();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_TRUE(std::isnan(decoded.value()));
}
TEST(DataEnDecoding, floatInfPositive) {
const auto value = std::numeric_limits<float>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, floatInfNegative) {
const auto value = -std::numeric_limits<float>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<float>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, doubles) {
for (int i = 0; i < valueAttempts; ++i) {
const auto value = static_cast<double>(randomValue<int64_t>()) / (static_cast<double>(randomValue<uint32_t>()) +
1);
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
}
TEST(DataEnDecoding, double0) {
const auto value = 0.0;
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, doubleMin) {
const auto value = std::numeric_limits<double>::min();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, doubleLowest) {
const auto value = std::numeric_limits<double>::lowest();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, doubleMax) {
const auto value = std::numeric_limits<double>::min();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, doubleNan) {
const auto value = std::numeric_limits<double>::quiet_NaN();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_TRUE(std::isnan(decoded.value()));
}
TEST(DataEnDecoding, doubleInfPositive) {
const auto value = std::numeric_limits<double>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, doubleInfNegative) {
const auto value = -std::numeric_limits<double>::infinity();
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<double>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(DataEnDecoding, arrayOfValue) {
const std::vector<int32_t> value{
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
};
std::vector<uint8_t> encoded;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
MMO::Networking::encodeValue(value, encoded);
auto decoded = MMO::Networking::decodeValue<std::vector<int32_t>>(encoded);
EXPECT_TRUE(decoded.has_value());
EXPECT_EQ(decoded.value(), value);
}
TEST(MultiWriteTest, outOfOrderIsInOrder) {
const std::vector<int32_t> value{
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
randomValue<int32_t>(),
};
std::vector<uint8_t> encodedInOrder;
std::vector<uint8_t> encodedOutOrder;
// Length is variable so we can't predict it (easily, there is math for it but I've lost it)
for (const auto& v : value) {
MMO::Networking::encodeValue(v, encodedInOrder);
}
for (int i = static_cast<int>(value.size()) - 1; i >= 0; --i) {
MMO::Networking::encodeValue(value[i], encodedOutOrder, 0);
}
EXPECT_EQ(encodedInOrder, encodedOutOrder);
}
TEST(Packet, CreateFrom) {
MMO::Networking::Packet packet;
packet.write<uint32_t>(0xDEADBEEF);
packet.write<int32_t>(-1);
auto packetData = packet.finalizePacket();
auto packet2Exp = MMO::Networking::Packet::createPacket(packetData);
EXPECT_TRUE(packet2Exp.has_value());
auto packet2 = packet2Exp.value();
{
auto readValue = packet2.read<uint32_t>();
EXPECT_TRUE(readValue.has_value());
EXPECT_EQ(*readValue, 0xDEADBEEF);
}
{
auto readValue = packet2.read<int32_t>();
EXPECT_TRUE(readValue.has_value());
EXPECT_EQ(*readValue, -1);
}
}