summaryrefslogtreecommitdiffhomepage
path: root/tests/test-asm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-asm.cpp')
-rw-r--r--tests/test-asm.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/test-asm.cpp b/tests/test-asm.cpp
new file mode 100644
index 0000000..2d3afa0
--- /dev/null
+++ b/tests/test-asm.cpp
@@ -0,0 +1,132 @@
+#include "asm/chunk.h"
+#include "asm/assembler.h"
+#include "asm/segment.h"
+#include "asm/intel64/all_ops.h"
+
+#include "minicc.h"
+
+#include <boost/algorithm/string.hpp>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include <algorithm>
+#include <cctype>
+#include <deque>
+#include <map>
+#include <memory>
+#include <string>
+#include <system_error>
+#include <utility>
+#include <vector>
+
+using namespace std::string_literals;
+namespace fs = std::filesystem;
+
+class AsmTest: public ::testing::Test
+{
+protected:
+ AsmTest() {
+ //debug = true;
+ }
+ ~AsmTest() {
+ }
+ void SetUp(){
+ }
+ void TearDown(){
+ }
+};
+
+TEST_F(AsmTest, Intel64_add) {
+ Segment segment;
+ Asm::Args args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(1)}};
+ segment.push_back(makeOp("add", args));
+
+ ASSERT_EQ(segment.size(), 1);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>({0x05, 0x01, 0x00, 0x00, 0x00}));
+}
+
+TEST_F(AsmTest, Intel64_int_0) {
+ Segment segment;
+ Asm::Args args{{Asm::Args::Immediate8(0)}};
+ segment.push_back(makeOp("int", args));
+
+ ASSERT_EQ(segment.size(), 1);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>{0xCE});
+}
+
+TEST_F(AsmTest, Intel64_int_1) {
+ Segment segment;
+ Asm::Args args{{Asm::Args::Immediate8(1)}};
+ segment.push_back(makeOp("int", args));
+
+ ASSERT_EQ(segment.size(), 1);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>{0xF1});
+}
+
+TEST_F(AsmTest, Intel64_int_5) {
+ Segment segment;
+ Asm::Args args{{Asm::Args::Immediate8(5)}};
+ segment.push_back(makeOp("int", args));
+
+ ASSERT_EQ(segment.size(), 1);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>({0xCD, 0x05}));
+}
+
+TEST_F(AsmTest, Intel64_nop) {
+ Segment segment;
+ segment.push_back(makeOp("nop"));
+
+ ASSERT_EQ(segment.size(), 1);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>{0x90});
+}
+
+TEST_F(AsmTest, Intel64_ret) {
+ Segment segment;
+ segment.push_back(makeOp("ret"));
+
+ ASSERT_EQ(segment.size(), 1);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>{0xC3});
+}
+
+TEST_F(AsmTest, Intel64_multiple) {
+ Segment segment;
+
+ segment.push_back(makeOp("nop"));
+ Asm::Args args0{{Asm::Args::Immediate8(5)}};
+ segment.push_back(makeOp("int", args0));
+ segment.push_back(makeOp("ret"));
+ segment.push_back(makeLabel("data1"));
+ segment.push_back(makeOp("ret"));
+ Asm::Args args1{{Asm::Args::Label("data1")}};
+ segment.push_back(makeOp("jmp", args1));
+ segment.push_back(makeData({1, 2, 3}));
+
+ segment.insertAddresses();
+
+ ASSERT_EQ(segment.size(), 7);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>(
+ {
+ 0x90, // nop
+ 0xCD, 0x05, // int 5
+ 0xC3, // ret
+ // data1:
+ 0xC3, // ret
+ 0xE9, 0xFF, 0xFF, 0xFF, 0xFF, // jmp data1
+ 0x01, 0x02, 0x03 // data
+ }));
+
+ segment.optimize();
+
+ ASSERT_EQ(segment.size(), 7);
+ ASSERT_EQ(segment.getCode(), std::vector<uint8_t>(
+ {
+ 0x90, // nop
+ 0xCD, 0x05, // int 5
+ 0xC3, // ret
+ // data1:
+ 0xC3, // ret
+ 0xEB, 0xFF, // jmp data1
+ 0x01, 0x02, 0x03 // data
+ }));
+}