aboutsummaryrefslogtreecommitdiff
path: root/vm/test/vm_test.c
blob: 2d1a91f784139ff1fab2532b024e3d59c6c63f37 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "vm.h"

#include "test.h"

#include <stdio.h>

/// Create and destroy a vm.
TEST_CASE(vm_create_destroy) {
  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  vm_del(&vm);
}

// Exit with an implicit 0 exit code.
TEST_CASE(vm_exit_implicit) {
  // clang-format off
  const Inst instructions[] = {
    vmExit(),
  };
  // clang-format on

  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  const int exit_code =
      vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst));
  TEST_TRUE(exit_code == 0);
  vm_del(&vm);
}

// Exit with an explicit exit code.
TEST_CASE(vm_exit_explicit) {
  const int32_t expected = 17;

  // clang-format off
  const Inst instructions[] = {
    vmPushI32(expected),
    vmExit(),
  };
  // clang-format on

  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  const int exit_code =
      vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst));
  TEST_TRUE(exit_code == expected);
  vm_del(&vm);
}

/// Add two i32 numbers.
TEST_CASE(vm_add_i32) {
  const int n1 = 2;
  const int n2 = 3;

  // clang-format off
  const Inst instructions[] = {
      vmPushI32(n1),
      vmPushI32(n2),
      vmAdd(I32),
      vmExit(),
  };
  // clang-format on

  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  const int exit_code =
      vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst));
  TEST_EQUAL(exit_code, n1 + n2);
  vm_del(&vm);
}

/// Sum an array of numbers with 4 add instructions.
TEST_CASE(vm_sum_array_i32_explicit) {
  const int vals[5] = {1, 2, 3, 4, 5};

  // clang-format off
  const Inst instructions[] = {
      vmPushI32(vals[0]),
      vmPushI32(vals[1]),
      vmPushI32(vals[2]),
      vmPushI32(vals[3]),
      vmPushI32(vals[4]),
      vmAdd(I32),
      vmAdd(I32),
      vmAdd(I32),
      vmAdd(I32),
      vmExit(),
  };
  // clang-format on

  int sum = 0;
  for (size_t i = 0; i < sizeof(vals) / sizeof(vals[0]); ++i) {
    sum += vals[i];
  }

  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  const int exit_code =
      vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst));
  TEST_EQUAL(exit_code, sum);
  vm_del(&vm);
}

/// Sum an array of numbers with a loop.
TEST_CASE(vm_sum_array_i32_loop) {
  const int vals[5] = {1, 2, 3, 4, 5};

  const Label loop_label    = 0;
  const Label counter_index = 0;

  // clang-format off
  const Inst instructions[] = {
      vmPushI32(vals[0]),
      vmPushI32(vals[1]),
      vmPushI32(vals[2]),
      vmPushI32(vals[3]),
      vmPushI32(vals[4]),
      vmLocal(I32, counter_index),
      vmPushI32(sizeof(vals) / sizeof(vals[0]) - 1),
      vmLocalWr(I32, counter_index),
      vmLoop(loop_label),
      vmAdd(I32),
      vmLocalRd(I32, counter_index),
      vmDec(I32),
      // TODO: Could be useful to have a function that writes the local but
      // leaves its value on the stack.
      vmLocalWr(I32, counter_index),
      vmLocalRd(I32, counter_index),
      // TODO: Perhaps we should expect the comparison value to also be pushed
      // to the stack.
      vmCmpI32(0),
      vmBr_if(false, loop_label),
      vmEnd(),
      vmExit(),
  };
  // clang-format on

  int sum = 0;
  for (size_t i = 0; i < sizeof(vals) / sizeof(vals[0]); ++i) {
    sum += vals[i];
  }

  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  const int exit_code =
      vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst));
  TEST_EQUAL(exit_code, sum);
  vm_del(&vm);
}

// Call a function to add two numbers.
TEST_CASE(vm_function_call) {
  const Label   func     = 0;
  const Label   a        = 0;
  const Label   b        = 1;
  const int32_t a_val    = 3;
  const int32_t b_val    = 5;
  const int32_t expected = a + b;

  // clang-format off
  const Inst instructions[] = {
    /* Function definition */
    vmFunc(func),
    vmArg(I32, b),
    vmArg(I32, a),
    vmAdd(I32),
    vmEnd(),
    /* Main program */
    vmPushI32(a_val),
    vmPushI32(b_val),
    vmCall(func),
    vmExit(),
  };
  // clang-format on

  Vm* vm = vm_new();
  TEST_TRUE(vm != 0);
  // const int exit_code =
  //     vm_run(vm, instructions, sizeof(instructions) / sizeof(Inst));
  vm_del(&vm);
}

int main() { return 0; }