-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbftest.cc
143 lines (112 loc) · 2.7 KB
/
bftest.cc
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
#include "gtest/gtest.h"
#include "brainfuck.h"
#include <sstream>
std::string RunBF(const std::string& program);
std::string RunBF(const std::string& program) {
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.FromString(program);
bf.Run();
return ss.str();
}
TEST (bf_test, basic_increment_cell)
{
ASSERT_EQ(RunBF("++++"), "");
}
TEST (bf_test, basic_increment_cell_and_print)
{
ASSERT_EQ(RunBF("++++."), "4");
}
TEST (bf_test, basic_increment_and_decrement_print)
{
ASSERT_EQ(RunBF("++++--."),"2");
}
TEST (bf_test, basic_movr_movl_print)
{
ASSERT_EQ(RunBF("++++>++++><.<."), "44");
}
TEST (bf_test, empty_loop_test)
{
ASSERT_EQ(RunBF("[]."), "0");
}
TEST (bf_test, empty_nested_loop_test)
{
ASSERT_EQ(RunBF("[[]].."), "00");
ASSERT_EQ(RunBF("[[..]].."), "00");
ASSERT_EQ(RunBF("[[[]]]..."), "000");
ASSERT_EQ(RunBF("[[[..+++...]]]..."), "000");
}
TEST (bf_test, loop_test)
{
ASSERT_EQ(RunBF("+++++++++[.-]"), "987654321");
}
TEST (bf_test, nested_loops_test_early_exit)
{
ASSERT_EQ(RunBF("[+[+[+[+[-.]]]]]"), "");
}
TEST (bf_test, nested_loops_test)
{
ASSERT_EQ(RunBF("+[+[+[+[+[-.]]]]]"), "43210");
}
TEST (bf_test, extraneous_movr)
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.FromString("++++>++++<[[-]>]<<<<.>.");
bf.Run();
ASSERT_EQ(bf.GetStatus(), bfi::Brainfuck::Status::success);
}
TEST (bf_test, zero_all_right)
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.FromString("++++>++++<[[-]>]<<.>.");
bf.Run();
ASSERT_EQ(ss.str(), "00");
}
TEST (bf_test, check_min_wrap_around)
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
const std::size_t expectedMaxIndex = bf.GetMaxNumCells() - 1;
bf.FromString("<");
bf.Run();
ASSERT_EQ(bf.GetCellPos(), expectedMaxIndex);
}
TEST (bf_test, check_max_wrap_around)
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.FromString("<>");
bf.Run();
ASSERT_EQ(bf.GetCellPos(), 0);
}
TEST (bf_test, hello_world_by_primo)
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.OutputMode(bfi::Brainfuck::OutputMode::raw);
// https://esolangs.org/wiki/Brainfuck#Examples primo's hello world in brainfuck
bf.FromString("--<-<<+[+[<+>--->->->-<<<]>]<<--.<++++++.<<-..<<.<+.>>.>>.<<<.+++.>>.>>-.<<<+.");
bf.Run();
ASSERT_EQ(ss.str(), "Hello, World!");
}
TEST (bf_test, simple_validation)
{
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.FromString("[[[.]]");
ASSERT_EQ(bf.Validate(), bfi::Brainfuck::Status::error);
}
{
std::stringstream ss;
bfi::Brainfuck bf(ss);
bf.FromString("[[[.]]]");
ASSERT_EQ(bf.Validate(), bfi::Brainfuck::Status::success);
}
}
TEST (bf_test, ignore_non_brainfuck)
{
ASSERT_EQ(RunBF("this is part.brainfuck+part.non+brainfuck."), "012");
}