-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
129 lines (112 loc) · 5.26 KB
/
Copy pathtest.cpp
File metadata and controls
129 lines (112 loc) · 5.26 KB
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
#include <cassert>
#include "compiler.h"
int main()
{
typedef std::pair<char*, int32_t> TestCase;
std::vector<TestCase> testcases;
testcases.emplace_back(TestCase((char*) "\
( simple calculation ) \n\
: main \n\
1 2 3 4 * + + . \n\
; \n\
", 1+2+3*4));
testcases.emplace_back(TestCase((char*) "\
( simple function definition/call/return ) \n\
: triple \n\
3 * \n\
; \n\
: main \n\
5 triple . \n\
; \n\
", 5*3));
testcases.emplace_back(TestCase((char*) "\
( simple conditional statement ) \n\
: main \n\
2 1 > if 100 then . \n\
; \n\
", 100));
testcases.emplace_back(TestCase((char*) "\
( nested conditional statements ) \n\
: main \n\
1 2 + 2 > if \n\
7 2 4 3 - < if \n\
0 else \n\
7 then \n\
7 then \n\
* * . \n\
; \n\
",7*7*7));
testcases.emplace_back(TestCase((char*) "\
( recursive call ) \n\
: factorial \n\
dup 1 > if \n\
dup 1 - factorial * then \n\
; \n\
: main \n\
5 factorial . \n\
; \n\
", 5*4*3*2*1));
testcases.emplace_back(TestCase((char*) "\
( tail-call ) \n\
: tail_factorial \n\
dup 1 > if \n\
swap over * swap 1 - tail_factorial then \n\
; \n\
: main \n\
1 5 tail_factorial drop . \n\
; \n\
", 5*4*3*2*1));
testcases.emplace_back(TestCase((char*) "\
( nested function call ) \n\
: sq \n\
dup * \n\
; \n\
: fourth_power \n\
sq sq \n\
; \n\
: main \n\
3 fourth_power . \n\
; \n\
", 3*3*3*3));
testcases.emplace_back(TestCase((char*) "\
( calling functions at old blocks ) \n\
: main \n\
3 factorial fourth_power . \n\
; \n\
", (3*2*1)*(3*2*1)*(3*2*1)*(3*2*1)));
testcases.emplace_back(TestCase((char*) "\
( volatile variables ) \n\
: main \n\
variable XXX 10 \n\
variable YYY 20 \n\
XXX YYY * . \n\
; \n\
", 10*20));
testcases.emplace_back(TestCase((char*) "\
( storage variables ) \n\
: main \n\
3 XXX ! \n\
XXX @ . \n\
; \n\
", 3));
Blockchain blockchain = Blockchain();
VM vm = VM();
Compiler compiler = Compiler();
int32_t bin_data[Block::DATA_SIZE];
for (auto testcase: testcases){
std::cout << testcase.first << std::flush;
compiler.compile(bin_data, testcase.first, blockchain.chain.size());
vm.exec(bin_data, blockchain);
assert(vm.peep() == testcase.second);
std::cout << "Mining block... " << std::flush;
blockchain.addBlock(Block(bin_data));
std::cout << blockchain.chain.back().hash << std::endl;
}
std::cout << "Checking validity..." << std::endl;
assert(blockchain.isValid());
std::cout << "Checking unmodifiability..." << std::endl;
blockchain.chain[1].data[3] = 999;
assert(!blockchain.isValid());
std::cout << "All tests passed!" << std::endl;
return 0;
}