summaryrefslogtreecommitdiffhomepage
path: root/tests/test-asm.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-19 22:31:33 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-19 22:31:33 +0100
commit5c0611b998e039c8547cfa3841da3567e13446a8 (patch)
tree3f1dc0a8371996426f99d395ad3f0fa9be503ea5 /tests/test-asm.cpp
parent1937e301b6cd185c8ce907b9184142e82e76fda4 (diff)
Add assembler parser (WIP)
Diffstat (limited to 'tests/test-asm.cpp')
-rw-r--r--tests/test-asm.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/test-asm.cpp b/tests/test-asm.cpp
index 2d3afa0..f4a1a2c 100644
--- a/tests/test-asm.cpp
+++ b/tests/test-asm.cpp
@@ -1,5 +1,6 @@
#include "asm/chunk.h"
#include "asm/assembler.h"
+#include "asm/parse.h"
#include "asm/segment.h"
#include "asm/intel64/all_ops.h"
@@ -37,6 +38,20 @@ protected:
}
};
+class AsmParseTest: public ::testing::Test
+{
+protected:
+ AsmParseTest() {
+ //debug = true;
+ }
+ ~AsmParseTest() {
+ }
+ void SetUp(){
+ }
+ void TearDown(){
+ }
+};
+
TEST_F(AsmTest, Intel64_add) {
Segment segment;
Asm::Args args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(1)}};
@@ -130,3 +145,100 @@ TEST_F(AsmTest, Intel64_multiple) {
0x01, 0x02, 0x03 // data
}));
}
+
+TEST_F(AsmParseTest, parse_empty) {
+ std::vector<std::shared_ptr<Chunk>> chunks0{parseAsm("")};
+ ASSERT_EQ(chunks0.size(), 0);
+
+ std::vector<std::shared_ptr<Chunk>> chunks1{parseAsm("\n\n")};
+ ASSERT_EQ(chunks1.size(), 0);
+
+ std::vector<std::shared_ptr<Chunk>> chunks2{parseAsm("\n\n")};
+ ASSERT_EQ(chunks2.size(), 0);
+}
+
+TEST_F(AsmParseTest, parse_op_0) {
+ std::vector<std::shared_ptr<Chunk>> chunks0{parseAsm("nop")};
+ ASSERT_EQ(chunks0.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_1) {
+ std::vector<std::shared_ptr<Chunk>> chunks1{parseAsm("neg edi")};
+ ASSERT_EQ(chunks1.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_2) {
+ std::vector<std::shared_ptr<Chunk>> chunks2{parseAsm("add eax, edx")};
+ ASSERT_EQ(chunks2.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_3) {
+ std::vector<std::shared_ptr<Chunk>> chunks3{parseAsm("add eax, 3")};
+ ASSERT_EQ(chunks3.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_4) {
+ std::vector<std::shared_ptr<Chunk>> chunks4{parseAsm("add [edi], 3")};
+ ASSERT_EQ(chunks4.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_5) {
+ std::vector<std::shared_ptr<Chunk>> chunks5{parseAsm("add byte ptr [edi], 3")};
+ ASSERT_EQ(chunks5.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_6) {
+ std::vector<std::shared_ptr<Chunk>> chunks6{parseAsm("add dword ptr[edi], 3")};
+ ASSERT_EQ(chunks6.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_op_7) {
+ std::vector<std::shared_ptr<Chunk>> chunks7{parseAsm("add qword ptr[edi], 3")};
+ ASSERT_EQ(chunks7.size(), 1);
+}
+
+TEST_F(AsmParseTest, parse_label) {
+ std::vector<std::shared_ptr<Chunk>> chunks0{parseAsm("label1:")};
+ ASSERT_EQ(chunks0.size(), 1);
+
+ std::vector<std::shared_ptr<Chunk>> chunks1{parseAsm("label2: add eax, 3")};
+ ASSERT_EQ(chunks1.size(), 2);
+}
+
+TEST_F(AsmParseTest, parse_multiline) {
+ std::vector<std::shared_ptr<Chunk>> chunks1{parseAsm("add eax, 3\n")};
+ ASSERT_EQ(chunks1.size(), 1);
+
+ std::vector<std::shared_ptr<Chunk>> chunks2{parseAsm("label2: add eax, 3\n mul rdx")};
+ ASSERT_EQ(chunks2.size(), 3);
+
+ std::vector<std::shared_ptr<Chunk>> chunks3{parseAsm("label2: add eax, 3\n mul rdx\n")};
+ ASSERT_EQ(chunks3.size(), 3);
+
+ std::vector<std::shared_ptr<Chunk>> chunks4{parseAsm("label2: add eax, 3\n\n\n mul rdx")};
+ ASSERT_EQ(chunks4.size(), 3);
+}
+
+TEST_F(AsmParseTest, parse_comment) {
+ std::vector<std::shared_ptr<Chunk>> chunks0{parseAsm(" ; Comment 1")};
+ ASSERT_EQ(chunks0.size(), 0);
+
+ std::vector<std::shared_ptr<Chunk>> chunks1{parseAsm("label2: add eax, 3 ; A comment")};
+ ASSERT_EQ(chunks1.size(), 2);
+
+ std::vector<std::shared_ptr<Chunk>> chunks3{parseAsm("label2: add eax, 3 // Another comment")};
+ ASSERT_EQ(chunks3.size(), 2);
+
+ std::vector<std::shared_ptr<Chunk>> chunks4{parseAsm("label2: add eax, 3 // Another comment\nadd rax, rdi")};
+ ASSERT_EQ(chunks4.size(), 3);
+
+ std::vector<std::shared_ptr<Chunk>> chunks5{parseAsm("label2: add eax, 3 // Another comment\n \t add rax, rdi")};
+ ASSERT_EQ(chunks5.size(), 3);
+}
+
+TEST_F(AsmParseTest, parse_error) {
+ ASSERT_THROW(parseAsm(" add label1:"), std::runtime_error);
+
+ ASSERT_THROW(parseAsm(" add eax , "), std::runtime_error);
+ ASSERT_THROW(parseAsm(" add eax,"), std::runtime_error);
+}