Skip to content

Commit

Permalink
All unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johanlindfors-ts committed Mar 14, 2023
1 parent 19d92fd commit a2a6f29
Show file tree
Hide file tree
Showing 30 changed files with 509 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,48 @@
"files.exclude": {
"**/zig-cache/**": true,
"**/zig-out/**": true
},
"cmake.sourceDirectory": "${workspaceFolder}/cplusplus",
"files.associations": {
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"atomic": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"numeric": "cpp",
"random": "cpp",
"ratio": "cpp",
"stdexcept": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"vector": "cpp"
}
}
6 changes: 5 additions & 1 deletion cplusplus/include/chip8/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ namespace Chip8 {
std::shared_ptr<Display> display,
std::shared_ptr<Keyboard> keyboard);

int getPc() { return _pc; }
uint16_t getPc() { return _pc; }
uint16_t getIndex() { return _index; }
void setDelayTimer(uint8_t value) { _delayTimer = value; }
uint8_t getDelayTimer() { return _delayTimer; }
uint8_t getSoundTimer() { return _soundTimer; }

private:
uint16_t getOpcode();
Expand Down
16 changes: 16 additions & 0 deletions cplusplus/tests/source/should_add_value_to_value_in_registry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x70, 0x01 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x2 == registers->get(0));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0xFF, 0x70, 0xFF };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(254 == registers->get(0));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x40, 0x01 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(516 == cpu->getPc());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x61, 0x02, 0x50, 0x10 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(518 == cpu->getPc());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x61, 0x01, 0x50, 0x10 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(520 == cpu->getPc());
}
16 changes: 16 additions & 0 deletions cplusplus/tests/source/should_test_opcode_0x6000.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x1 == registers->get(0));
}
18 changes: 18 additions & 0 deletions cplusplus/tests/source/should_test_opcode_0x8xy0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x62, 0x02, 0x80, 0x20 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, 4);

// assert
assert(0x1 == registers->get(0));
emulate(cpu, 2);
assert(0x2 == registers->get(0));
}
16 changes: 16 additions & 0 deletions cplusplus/tests/source/should_test_opcode_0x8xy1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x61, 0x06, 0x80, 0x11 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x7 == registers->get(0));
}
16 changes: 16 additions & 0 deletions cplusplus/tests/source/should_test_opcode_0x8xy2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x0C, 0x61, 0x06, 0x80, 0x12 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x4 == registers->get(0));
}
16 changes: 16 additions & 0 deletions cplusplus/tests/source/should_test_opcode_0x8xy3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x09, 0x61, 0x05, 0x80, 0x13 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(12 == registers->get(0));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x01, 0x61, 0x01, 0x80, 0x14 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x2 == registers->get(0));
assert(0 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0xF1, 0x61, 0xF1, 0x80, 0x14};
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(226 == registers->get(0));
assert(1 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x02, 0x61, 0x03, 0x80, 0x15 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0xFF == registers->get(0));
assert(0 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x03, 0x61, 0x02, 0x80, 0x15 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(1 == registers->get(0));
assert(1 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x03, 0x80, 0x16 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x1 == registers->get(0));
assert(0x1 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x02, 0x80, 0x16 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x1 == registers->get(0));
assert(0x0 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x02, 0x61, 0x03, 0x80, 0x17 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x1 == registers->get(0));
assert(0x1 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0x02, 0x61, 0x03, 0x80, 0x17 };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0x1 == registers->get(0));
assert(0x1 == registers->get(0xF));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "tests_common.h"

int main() {
// arrange
auto memory = std::make_shared<Chip8::Memory>();
auto registers = std::make_shared<Chip8::Registers>();
auto cpu = std::make_shared<Chip8::CPU>(memory, registers);
uint8_t data[] = { 0x60, 0xFF, 0x80, 0x0E };
memory->load(512, data, sizeof(data));

// act
emulate(cpu, sizeof(data));

// assert
assert(0xFE == registers->get(0));
assert(0x1 == registers->get(0xF));
}
Loading

0 comments on commit a2a6f29

Please sign in to comment.