Kelsidavis-WoWee/tests/test_opcode_table.cpp
Paul 2cb47bf126 chore(testing): add unit tests and update core render/network pipelines
- add new tests:
  - test_blp_loader.cpp
  - test_dbc_loader.cpp
  - test_entity.cpp
  - test_frustum.cpp
  - test_m2_structs.cpp
  - test_opcode_table.cpp
  - test_packet.cpp
  - test_srp.cpp
  - CMakeLists.txt
- add docs and progress tracking:
  - TESTING.md
  - perf_baseline.md
- update project config/build:
  - .gitignore
  - CMakeLists.txt
  - test.sh
- core engine updates:
  - application.cpp
  - game_handler.cpp
  - world_socket.cpp
  - adt_loader.cpp
  - asset_manager.cpp
  - m2_renderer.cpp
  - post_process_pipeline.cpp
  - renderer.cpp
  - terrain_manager.cpp
  - game_screen.cpp
- add profiler header:
  - profiler.hpp
2026-04-03 09:41:34 +03:00

118 lines
3.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Phase 0 OpcodeTable load from JSON, toWire/fromWire mapping
#include <catch_amalgamated.hpp>
#include "game/opcode_table.hpp"
#include <fstream>
#include <filesystem>
#include <cstdio>
using wowee::game::OpcodeTable;
using wowee::game::LogicalOpcode;
// Helper: write a temporary JSON file and return its path.
// Uses the executable's directory to avoid permission issues.
static std::string writeTempJson(const std::string& content) {
auto path = std::filesystem::temp_directory_path() / "wowee_test_opcodes.json";
std::ofstream f(path);
f << content;
f.close();
return path.string();
}
TEST_CASE("OpcodeTable loadFromJson basic mapping", "[opcode_table]") {
// CMSG_PING and SMSG_PONG are canonical opcodes present in the generated enum.
std::string json = R"({
"CMSG_PING": "0x1DC",
"SMSG_PONG": "0x1DD"
})";
auto path = writeTempJson(json);
OpcodeTable table;
REQUIRE(table.loadFromJson(path));
REQUIRE(table.size() == 2);
REQUIRE(table.hasOpcode(LogicalOpcode::CMSG_PING));
REQUIRE(table.toWire(LogicalOpcode::CMSG_PING) == 0x1DC);
REQUIRE(table.toWire(LogicalOpcode::SMSG_PONG) == 0x1DD);
std::remove(path.c_str());
}
TEST_CASE("OpcodeTable fromWire reverse lookup", "[opcode_table]") {
std::string json = R"({ "CMSG_PING": "0x1DC" })";
auto path = writeTempJson(json);
OpcodeTable table;
table.loadFromJson(path);
auto result = table.fromWire(0x1DC);
REQUIRE(result.has_value());
REQUIRE(*result == LogicalOpcode::CMSG_PING);
std::remove(path.c_str());
}
TEST_CASE("OpcodeTable unknown wire returns nullopt", "[opcode_table]") {
std::string json = R"({ "CMSG_PING": "0x1DC" })";
auto path = writeTempJson(json);
OpcodeTable table;
table.loadFromJson(path);
auto result = table.fromWire(0x9999);
REQUIRE_FALSE(result.has_value());
std::remove(path.c_str());
}
TEST_CASE("OpcodeTable unknown logical returns 0xFFFF", "[opcode_table]") {
std::string json = R"({ "CMSG_PING": "0x1DC" })";
auto path = writeTempJson(json);
OpcodeTable table;
table.loadFromJson(path);
// SMSG_AUTH_CHALLENGE should not be in this table
REQUIRE(table.toWire(LogicalOpcode::SMSG_AUTH_CHALLENGE) == 0xFFFF);
std::remove(path.c_str());
}
TEST_CASE("OpcodeTable loadFromJson nonexistent file", "[opcode_table]") {
OpcodeTable table;
REQUIRE_FALSE(table.loadFromJson("/nonexistent/path/opcodes.json"));
REQUIRE(table.size() == 0);
}
TEST_CASE("OpcodeTable logicalToName returns enum name", "[opcode_table]") {
const char* name = OpcodeTable::logicalToName(LogicalOpcode::CMSG_PING);
REQUIRE(name != nullptr);
REQUIRE(std::string(name) == "CMSG_PING");
}
TEST_CASE("OpcodeTable decimal wire values", "[opcode_table]") {
std::string json = R"({ "CMSG_PING": "476" })";
auto path = writeTempJson(json);
OpcodeTable table;
REQUIRE(table.loadFromJson(path));
REQUIRE(table.toWire(LogicalOpcode::CMSG_PING) == 476);
std::remove(path.c_str());
}
TEST_CASE("Global active opcode table", "[opcode_table]") {
OpcodeTable table;
std::string json = R"({ "CMSG_PING": "0x1DC" })";
auto path = writeTempJson(json);
table.loadFromJson(path);
wowee::game::setActiveOpcodeTable(&table);
REQUIRE(wowee::game::getActiveOpcodeTable() == &table);
REQUIRE(wowee::game::wireOpcode(LogicalOpcode::CMSG_PING) == 0x1DC);
// Reset
wowee::game::setActiveOpcodeTable(nullptr);
REQUIRE(wowee::game::wireOpcode(LogicalOpcode::CMSG_PING) == 0xFFFF);
std::remove(path.c_str());
}