Skip to content

Commit

Permalink
Add testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
vabold committed Feb 4, 2024
1 parent 9716cd0 commit fb84a72
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 8 deletions.
5 changes: 5 additions & 0 deletions source/egg/math/Quat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ f32 Quatf::dot(const Quatf &q) const {
return w * q.w + v.dot(q.v);
}

void Quatf::read(Stream &stream) {
v.read(stream);
w = stream.read_f32();
}

const Quatf Quatf::ident = Quatf(1.0f, Vector3f::zero);

} // namespace EGG
10 changes: 10 additions & 0 deletions source/egg/math/Quat.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ struct Quatf {
return *this = *this * q;
}

bool operator==(const Quatf &rhs) const {
return w == rhs.w && v == rhs.v;
}

bool operator!=(const Quatf &rhs) const {
return !(*this == rhs);
}

void setRPY(const Vector3f &rpy);
void normalise();
Quatf conjugate() const;
Expand All @@ -43,6 +51,8 @@ struct Quatf {
f32 dot() const;
f32 dot(const Quatf &q) const;

void read(Stream &stream);

Vector3f v;
f32 w;

Expand Down
8 changes: 8 additions & 0 deletions source/egg/math/Vector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ struct Vector3f {
return *this = *this / scalar;
}

bool operator==(const Vector3f &rhs) const {
return x == rhs.x && y == rhs.y && z == rhs.z;
}

bool operator!=(const Vector3f &rhs) const {
return !(*this == rhs);
}

Vector3f cross(const EGG::Vector3f &rhs) const;
f32 dot() const;
f32 dot(const EGG::Vector3f &rhs) const;
Expand Down
8 changes: 8 additions & 0 deletions source/egg/util/Stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ void Stream::jump(u32 index) {
assert(!eof());
}

void Stream::setEndian(std::endian endian) {
m_endian = endian;
}

u32 Stream::index() const {
return m_index;
}

u8 Stream::read_u8() {
return read<u8>();
}
Expand Down
4 changes: 4 additions & 0 deletions source/egg/util/Stream.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public:
void skip(u32 count);
void jump(u32 index);

void setEndian(std::endian endian);

u32 index() const;

u8 read_u8();
u16 read_u16();
u32 read_u32();
Expand Down
5 changes: 5 additions & 0 deletions source/game/kart/KartObjectManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ void KartObjectManager::calc() {
}
}

KartObject *KartObjectManager::object(size_t i) const {
assert(i < m_count);
return m_objects[i];
}

KartObjectManager *KartObjectManager::CreateInstance() {
assert(!s_instance);
s_instance = new KartObjectManager;
Expand Down
2 changes: 2 additions & 0 deletions source/game/kart/KartObjectManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public:
void init();
void calc();

KartObject *object(size_t i) const;

static KartObjectManager *CreateInstance();
static void DestroyInstance();
static KartObjectManager *Instance();
Expand Down
8 changes: 8 additions & 0 deletions source/game/kart/KartObjectProxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void KartObjectProxy::setRot(const EGG::Quatf &q) {
dynamics()->setMainRot(q);
}

const EGG::Vector3f &KartObjectProxy::pos() const {
return dynamics()->pos();
}

const EGG::Quatf &KartObjectProxy::fullRot() const {
return dynamics()->fullRot();
}

Abstract::List *KartObjectProxy::list() const {
return s_list;
}
Expand Down
3 changes: 3 additions & 0 deletions source/game/kart/KartObjectProxy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public:
void setPos(const EGG::Vector3f &pos);
void setRot(const EGG::Quatf &q);

const EGG::Vector3f &pos() const;
const EGG::Quatf &fullRot() const;

Abstract::List *list() const;

protected:
Expand Down
11 changes: 7 additions & 4 deletions source/host/System.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ namespace Host {
int KSystem::main() {
init();
m_sceneMgr->changeScene(0);
if (!m_testDirector->init()) {
return 1;
}
run();
return 0;
}

void KSystem::init() {
auto *sceneCreator = new SceneCreatorDynamic;
m_sceneMgr = new EGG::SceneManager(sceneCreator);

m_testDirector = new Test::TestDirector;
}

void KSystem::run() {
K_LOG("Initialized successfully!");
K_LOG("The program will now run in an infinite loop to test 'calc' functionality.");
while (true) {
do {
m_sceneMgr->calc();
}
} while (m_testDirector->calc());
}

KSystem &KSystem::Instance() {
Expand Down
6 changes: 2 additions & 4 deletions source/host/System.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <egg/core/SceneManager.hh>
#include <test/TestDirector.hh>

namespace Host {

Expand All @@ -10,10 +11,6 @@ public:
void init();
void run();

EGG::SceneManager *sceneMgr() const {
return m_sceneMgr;
}

static KSystem &Instance();

private:
Expand All @@ -23,6 +20,7 @@ private:
~KSystem();

EGG::SceneManager *m_sceneMgr;
Test::TestDirector *m_testDirector;
};

} // namespace Host
21 changes: 21 additions & 0 deletions source/test/Test.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <egg/math/Quat.hh>

namespace Test {

struct TestHeader {
u32 signature;
u16 byteOrderMark;
u16 frameCount;
u16 versionMajor;
u16 versionMinor;
u32 dataOffset;
};

struct TestData {
EGG::Vector3f pos;
EGG::Quatf fullRot;
};

} // namespace Test
101 changes: 101 additions & 0 deletions source/test/TestDirector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "TestDirector.hh"

#include <game/kart/KartObjectManager.hh>

#include <abstract/File.hh>

#include <cstddef>

namespace Test {

TestDirector::TestDirector() {
size_t size;
u8 *file = Abstract::File::Load("Test/rmc3-rta-1-17-843.krkg", size);
m_file = file;
m_stream = EGG::RamStream(file, static_cast<u32>(size));
m_currentFrame = -1;
m_sync = true;

// Initialize endianness for the RAM stream
u16 mark = *reinterpret_cast<u16 *>(file + offsetof(TestHeader, byteOrderMark));
std::endian endian = parse<u16>(mark) == 0xfeff ? std::endian::big : std::endian::little;
m_stream.setEndian(endian);

readHeader();
}

TestDirector::~TestDirector() = default;

bool TestDirector::init() {
assert(m_stream.read_u32() == m_stream.index());
return calc();
}

bool TestDirector::calc() {
// Check if we're out of frames
constexpr u16 TARGET_FRAME = 1;
assert(TARGET_FRAME <= m_frameCount);
if (++m_currentFrame > TARGET_FRAME) {
return false;
}

// Test the current frame
TestData data = findNextEntry();
m_sync = test(data);
return m_sync;
}

bool TestDirector::test(const TestData &data) {
auto logVectorDesync = [this](const EGG::Vector3f &v0, const EGG::Vector3f v1,
const char *name) {
K_LOG("DESYNC! Frame: %d; Name: %s", m_currentFrame, name);
K_LOG("Expected [%f, %f, %f], got [%f, %f, %f]", v0.x, v0.y, v0.z, v1.x, v1.y, v1.z);
};

auto logQuatDesync = [this](const EGG::Quatf &q0, const EGG::Quatf q1, const char *name) {
K_LOG("DESYNC! Frame: %d; Name: %s", m_currentFrame, name);
K_LOG("Expected [%f, %f, %f, %f], got [%f, %f, %f, %f]", q0.v.x, q0.v.y, q0.v.z, q0.w,
q1.v.x, q1.v.y, q1.v.z, q1.w);
};

auto *object = Kart::KartObjectManager::Instance()->object(0);
const auto &pos = object->pos();
const auto &fullRot = object->fullRot();

if (data.pos != pos) {
logVectorDesync(data.pos, pos, "pos");
return false;
}
if (data.fullRot != fullRot) {
logQuatDesync(data.fullRot, fullRot, "fullRot");
return false;
}

return true;
}

TestData TestDirector::findNextEntry() {
EGG::Vector3f pos;
EGG::Quatf fullRot;
pos.read(m_stream);
fullRot.read(m_stream);

TestData data;
data.pos = pos;
data.fullRot = fullRot;
return data;
}

bool TestDirector::sync() const {
return m_sync;
}

void TestDirector::readHeader() {
assert(m_stream.read_u32() == 0x4b524b47); // 'KRKG'
m_stream.skip(2);
m_frameCount = m_stream.read_u16();
m_versionMajor = m_stream.read_u16();
m_versionMinor = m_stream.read_u16();
}

} // namespace Test
35 changes: 35 additions & 0 deletions source/test/TestDirector.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "test/Test.hh"

#include <egg/util/Stream.hh>

namespace Test {

class TestDirector {
public:
TestDirector();
~TestDirector();

bool init();
bool calc();
bool test(const TestData &data);

TestData findNextEntry();

bool sync() const;

private:
void readHeader();

void *m_file;
EGG::RamStream m_stream;

u16 m_versionMajor;
u16 m_versionMinor;
u16 m_frameCount;
u16 m_currentFrame;
bool m_sync;
};

} // namespace Test

0 comments on commit fb84a72

Please sign in to comment.