Use doctest as test framework

This commit is contained in:
madmaurice 2023-08-28 22:31:24 +02:00
parent b64c82ec92
commit eb6faab89f
6 changed files with 7148 additions and 18 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
*.o *.o
/vgbc /vgbc
/libemu.a
/vgbc.test

View file

@ -1,9 +1,8 @@
include Makeconf include Makeconf
emu-objs := $(addsuffix .o,$(modules)) emu-objs := $(addsuffix .o,$(modules))
test-targets := $(patsubst %,tests/%.bin, $(tests)) clean-objs := $(emu-objs) libemu.a vgbc vgbc.test
test-srcs := $(wildcard tests/*.cpp)
clean-objs := $(emu-objs) $(test-targets) libemu.a vgbc
.PHONY: all test clean .PHONY: all test clean
@ -12,15 +11,8 @@ all: vgbc $(test-targets)
clean: clean:
rm -rf $(clean-objs) rm -rf $(clean-objs)
test: $(test-targets) test: vgbc.test
@for test in $(tests); do \ @./$< -s
if ./tests/$$test.bin; then \
echo "-- $$test successful"; \
else \
echo "-- $$test failed"; \
exit 1; \
fi \
done
%.o: %.cpp %.o: %.cpp
g++ $(CXX_FLAGS) -c -o $@ $^ g++ $(CXX_FLAGS) -c -o $@ $^
@ -28,8 +20,8 @@ test: $(test-targets)
libemu.a: $(emu-objs) libemu.a: $(emu-objs)
ar -rc $@ $^ ar -rc $@ $^
tests/%.bin: tests/%.cpp libemu.a
g++ $(CXX_FLAGS) -o $@ $^
vgbc: main.c libemu.a vgbc: main.c libemu.a
g++ $(CXX_FLAGS) -o $@ $^ g++ $(CXX_FLAGS) -o $@ $^
vgbc.test: $(test-srcs) libemu.a
g++ $(CXX_FLAGS) -o $@ $^

2
tests/doctest.cpp Normal file
View file

@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

7106
tests/doctest.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,9 @@
#include <iostream> #include "doctest.h"
#include "memory/bus.h" #include "memory/bus.h"
#include "memory/ram.h" #include "memory/ram.h"
int main(int argc, char** argv) TEST_CASE("Bus can map same device twice")
{ {
RAM r(0x1000); RAM r(0x1000);
Bus b; Bus b;
@ -11,5 +12,5 @@ int main(int argc, char** argv)
b.map_device(0x2000, 0x2FFF, &r); b.map_device(0x2000, 0x2FFF, &r);
b.write8(0x60, 42); b.write8(0x60, 42);
return (b.read8(0x2060) == 42) ? 0 : 1; CHECK(b.read8(0x2060) == 42);
} }

27
tests/test_cpu_simple.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "doctest.h"
#include "cpu/cpu.h"
#include "memory/ram.h"
TEST_CASE("simple load and add")
{
u8 test_ram[] = {
0x3E, 0x10, // LD A, $0x10
0x87, // ADD A, A
};
RAM r(test_ram, 3, true);
Cpu cpu(&r);
CHECK(cpu.state.PC == 0x0);
cpu.step();
CHECK(cpu.state.PC == 0x2);
CHECK(cpu.state.A == 0x10);
cpu.step();
CHECK(cpu.state.PC == 0x3);
CHECK(cpu.state.A == 0x20);
}