aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/tests
diff options
context:
space:
mode:
Diffstat (limited to 'qtmips_machine/tests')
-rw-r--r--qtmips_machine/tests/testmemory.cpp61
-rw-r--r--qtmips_machine/tests/testregisters.cpp39
-rw-r--r--qtmips_machine/tests/tests.pro24
-rw-r--r--qtmips_machine/tests/tst_machine.cpp3
-rw-r--r--qtmips_machine/tests/tst_machine.h21
5 files changed, 148 insertions, 0 deletions
diff --git a/qtmips_machine/tests/testmemory.cpp b/qtmips_machine/tests/testmemory.cpp
new file mode 100644
index 0000000..991e1f1
--- /dev/null
+++ b/qtmips_machine/tests/testmemory.cpp
@@ -0,0 +1,61 @@
+#include "tst_machine.h"
+#include "memory.h"
+
+void MachineTests::memory_data() {
+ QTest::addColumn<std::uint32_t>("address");
+
+ QTest::newRow("memory begin") << (std::uint32_t)0x00;
+ QTest::newRow("memory end") << (std::uint32_t)0xFFFFFFFC;
+ QTest::newRow("memory midle start") << (std::uint32_t)0xFFFF00;
+ QTest::newRow("memory midle end") << (std::uint32_t)0xFFFFFF;
+}
+
+void MachineTests::memory() {
+ Memory m;
+
+ QFETCH(std::uint32_t, address);
+
+ // Uninitialize memory should read as zero
+ QCOMPARE(m.read_byte(address), (std::uint8_t)0);
+ QCOMPARE(m.read_hword(address), (std::uint16_t)0);
+ QCOMPARE(m.read_word(address), (std::uint32_t)0);
+ // Just a byte
+ m.write_byte(address, 0x42);
+ QCOMPARE(m.read_byte(address), (std::uint8_t)0x42);
+ // Half word
+ m.write_hword(address, 0x4243);
+ QCOMPARE(m.read_hword(address), (std::uint16_t)0x4243);
+ // Word
+ m.write_word(address, 0x42434445);
+ QCOMPARE(m.read_word(address), (std::uint32_t)0x42434445);
+}
+
+void MachineTests::memory_section_data() {
+ QTest::addColumn<std::uint32_t>("address");
+
+ QTest::newRow("memory begin") << (std::uint32_t)0x00;
+ QTest::newRow("memory end") << (std::uint32_t)0xFFFFFFFF;
+ QTest::newRow("memory midle start") << (std::uint32_t)0xFFFF00;
+ QTest::newRow("memory midle end") << (std::uint32_t)0xFFFFFF;
+}
+
+void MachineTests::memory_section() {
+ Memory m;
+
+ QFETCH(std::uint32_t, address);
+
+ // First section shouldn't exists
+ QCOMPARE(m.get_section(address, false), (MemorySection*)nullptr);
+ // Create section
+ MemorySection *s = m.get_section(address, true);
+ QVERIFY(s != nullptr);
+
+ // Write some data to memory
+ m.write_byte(address, 0x42);
+ // Read it trough section (mask bits outside of the memory section)
+ QCOMPARE(s->read_byte(address & ((1 << MEMORY_SECTION_BITS) - 1)), (std::uint8_t)0x42);
+ // Write some other data trough section
+ s->write_byte(address & ((1 << MEMORY_SECTION_BITS) - 1), 0x66);
+ // Read trough memory
+ QCOMPARE(m.read_byte(address), (std::uint8_t)0x66);
+}
diff --git a/qtmips_machine/tests/testregisters.cpp b/qtmips_machine/tests/testregisters.cpp
new file mode 100644
index 0000000..4430beb
--- /dev/null
+++ b/qtmips_machine/tests/testregisters.cpp
@@ -0,0 +1,39 @@
+#include "tst_machine.h"
+#include <qtmipsexception.h>
+#include <registers.h>
+
+void MachineTests::registers_gp0() {
+ Registers r;
+ QCOMPARE(r.read_gp(0), (unsigned)0);
+ r.write_gp(0, 0xff);
+ QCOMPARE(r.read_gp(0), (unsigned)0);
+}
+
+void MachineTests::registers_rw_gp() {
+ Registers r;
+ for (int i = 1; i < 32; i++) {
+ r.write_gp(i, 0xf00 + i);
+ QCOMPARE(r.read_gp(i), (unsigned)(0xf00 + i));
+ }
+}
+
+void MachineTests::registers_rw_hi_lo() {
+ Registers r;
+ r.write_hi_lo(false, 0xee);
+ r.write_hi_lo(true, 0xaa);
+ QCOMPARE(r.read_hi_lo(false), (unsigned)0xee);
+ QCOMPARE(r.read_hi_lo(true), (unsigned)0xaa);
+}
+
+void MachineTests::registers_pc() {
+ Registers r;
+ QCOMPARE(r.read_pc(), (unsigned)0x80020000); // Check initial pc address
+ QCOMPARE(r.pc_inc(), (unsigned)0x80020004);
+ QCOMPARE(r.pc_inc(), (unsigned)0x80020008);
+ QCOMPARE(r.pc_jmp(-0x8), (unsigned)0x80020000);
+ QCOMPARE(r.pc_jmp(0xC), (unsigned)0x8002000C);
+ r.pc_abs_jmp(0x80020100);
+ QCOMPARE(r.read_pc(), (unsigned)0x80020100);
+ QVERIFY_EXCEPTION_THROWN(r.pc_jmp(0x1), QtMipsExceptionUnalignedJump);
+ QVERIFY_EXCEPTION_THROWN(r.pc_abs_jmp(0x80020101), QtMipsExceptionUnalignedJump);
+}
diff --git a/qtmips_machine/tests/tests.pro b/qtmips_machine/tests/tests.pro
new file mode 100644
index 0000000..9477e08
--- /dev/null
+++ b/qtmips_machine/tests/tests.pro
@@ -0,0 +1,24 @@
+QT += testlib
+QT -= gui
+
+TARGET = tst_machine
+CONFIG += console
+CONFIG -= app_bundle
+CONFIG += c++11
+
+TEMPLATE = app
+
+LIBS += -L$$OUT_PWD/../ -lqtmips_machine
+INCLUDEPATH += $$PWD/..
+DEPENDPATH += $$PWD/..
+QMAKE_CXXFLAGS += -std=c++0x
+
+DEFINES += QT_DEPRECATED_WARNINGS
+
+SOURCES += tst_machine.cpp \
+ testmemory.cpp \
+ testregisters.cpp
+
+HEADERS += tst_machine.h
+
+DEFINES += SRCDIR=\\\"$$PWD/\\\"
diff --git a/qtmips_machine/tests/tst_machine.cpp b/qtmips_machine/tests/tst_machine.cpp
new file mode 100644
index 0000000..d5fc354
--- /dev/null
+++ b/qtmips_machine/tests/tst_machine.cpp
@@ -0,0 +1,3 @@
+#include "tst_machine.h"
+
+QTEST_GUILESS_MAIN(MachineTests)
diff --git a/qtmips_machine/tests/tst_machine.h b/qtmips_machine/tests/tst_machine.h
new file mode 100644
index 0000000..214ab88
--- /dev/null
+++ b/qtmips_machine/tests/tst_machine.h
@@ -0,0 +1,21 @@
+#ifndef TST_MACHINE_H
+#define TST_MACHINE_H
+
+#include <QtTest>
+
+class MachineTests : public QObject {
+ Q_OBJECT
+private Q_SLOTS:
+ // Registers
+ void registers_gp0();
+ void registers_rw_gp();
+ void registers_rw_hi_lo();
+ void registers_pc();
+ // Memory
+ void memory();
+ void memory_data();
+ void memory_section();
+ void memory_section_data();
+};
+
+#endif // TST_MACHINE_H