summaryrefslogtreecommitdiffhomepage
path: root/test-asm.cpp
blob: 882f998c327172b3618f16f26bdb52c2f732f6fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#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_int_0) {
 Segment segment;
 AsmArgs 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;
 AsmArgs 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;
 AsmArgs 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"));
 AsmArgs args{{Immediate8(5)}};
 segment.push_back(makeOp("int", args));
 segment.push_back(makeOp("ret"));

 ASSERT_EQ(segment.size(), 3);
 ASSERT_EQ(segment.getCode(), std::vector<uint8_t>({0x90, 0xCD, 0x05, 0xC3}));
}