-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainfuckBasicCommandsTests.cs
133 lines (119 loc) · 3.31 KB
/
BrainfuckBasicCommandsTests.cs
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
using System;
using NUnit.Framework;
namespace func.brainfuck
{
[TestFixture]
public class BrainfuckBasicCommandsTests
{
[Test]
public void Print()
{
Assert.AreEqual("\0", Run("."));
}
[Test]
public void Inc()
{
Assert.AreEqual("\x1", Run("+."));
Assert.AreEqual("\x5", Run("+++++."));
Assert.AreEqual("A", Run(new string('+', 'A') + "."));
Assert.AreEqual("Z", Run(new string('+', 'Z') + "."));
Assert.AreEqual("\xFF", Run(new string('+', 255) + "."));
}
[Test]
public void Dec()
{
Assert.AreEqual("\0", Run("+-."));
Assert.AreEqual("\x1", Run("+++--."));
Assert.AreEqual("A", Run(new string('+', 'C') + "--."));
}
[Test]
public void IncOverflow()
{
Assert.AreEqual("\x1", Run(new string('+', 257) + "."));
}
[Test]
public void DecOverflow()
{
Assert.AreEqual("\xFF", Run("-."));
Assert.AreEqual("\x1", Run(new string('-', 255) + "."));
}
[Test]
public void Shift()
{
Assert.AreEqual(3, Vm(">>>").MemoryPointer);
Assert.AreEqual(2, Vm(">>><").MemoryPointer);
Assert.AreEqual(1, Vm(">>><<").MemoryPointer);
Assert.AreEqual(0, Vm("><").MemoryPointer);
Assert.AreEqual("\x2", Run("+>++."));
Assert.AreEqual("\x1", Run("+>++<."));
Assert.AreEqual("\x1", Run("+>++>+++<<."));
}
[Test]
public void ShiftOverflow()
{
Assert.AreEqual(2, Vm("<", 3).MemoryPointer);
Assert.AreEqual(0, Vm("<>", 3).MemoryPointer);
Assert.AreEqual(0, Vm(">>>", 3).MemoryPointer);
Assert.AreEqual(1, Vm(">>>>", 3).MemoryPointer);
Assert.AreEqual("\x1", Run("<+."));
Assert.AreEqual("\x2", Run("++<>."));
Assert.AreEqual("\x1", Run("+<++<+++>>."));
Assert.AreEqual("\x3", Run("+++" + new string('>', 30000) + "."));
Assert.AreEqual("\x3", Run("+++" + new string('<', 30000) + "."));
}
[Test]
public void Read()
{
Assert.AreEqual("A", Run(",.", "A"));
Assert.AreEqual("ABC", Run(",.,.,.", "ABC"));
}
[Test]
public void HelloWorld()
{
Assert.AreEqual("Hello World!\n", Run(@"
+++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++.+++++++++++++++++
++++++++++++.+++++++..+++.-------------------
---------------------------------------------
---------------.+++++++++++++++++++++++++++++
++++++++++++++++++++++++++.++++++++++++++++++
++++++.+++.------.--------.------------------
---------------------------------------------
----.-----------------------.
"));
}
[Test]
public void Constants()
{
Assert.AreEqual("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890",
Run(
"Q.W.E.R.T.Y.U.I.O.P.A.S.D.F.G.H.J.K.L.Z.X.C.V.B.N.M." +
"q.w.e.r.t.y.u.i.o.p.a.s.d.f.g.h.j.k.l.z.x.c.v.b.n.m." +
"1.2.3.4.5.6.7.8.9.0."));
}
[Test]
public void IgnoreOtehrSymbols()
{
Assert.AreEqual("Q",
Run("Q!@#$%&*()."));
}
[Test]
public void HelloWorldWithConstants()
{
Assert.AreEqual("Hello world",
Run("H>e>l>l>o>++++++++++++++++++++++++++++++++>w>o>r>l>d<<<<<<<<<<.>.>.>.>.>.>.>.>.>.>."));
}
private string Run(string program, string input = "")
{
return Brainfuck.Run(program, input);
}
private IVirtualMachine Vm(string program, int memorySize = 10)
{
var vm = new VirtualMachine(program, memorySize);
BrainfuckBasicCommands.RegisterTo(vm, () => -1, c => {});
BrainfuckLoopCommands.RegisterTo(vm);
vm.Run();
return vm;
}
}
}