-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfck_test.go
176 lines (150 loc) · 4.27 KB
/
brainfck_test.go
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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"testing"
)
func TestSimpelOutput(t *testing.T) {
fileName := "./test/simpleOutputTest.bf"
expectedOutput := '7'
var writer bytes.Buffer
file, err := os.Open(fileName)
defer file.Close()
if err != nil {
t.Errorf("Could not open test file %s", fileName)
} else {
brainfckr := NewBrainfckr(file, os.Stdin, &writer)
err = brainfckr.Interpret()
}
if err != io.EOF {
t.Errorf(fmt.Sprintf("Failed to execute Test TestOutput %s", fileName))
} else {
result, _, _ := writer.ReadRune()
if result != expectedOutput {
t.Errorf("result = %c; want 7", result)
}
}
}
func Test99BottlesOfBeer(t *testing.T) {
fileName := "./test/99BottlesOfBeer.bf"
resultFile := "./test/99BottlesOfBeer_RESULT"
var resultBuffer *bytes.Buffer = bytes.NewBuffer([]byte{})
file, err := os.Open(fileName)
if err != nil {
t.Errorf("Could not open test file %s", fileName)
} else {
defer file.Close()
brainfckr := NewBrainfckr(file, os.Stdin, resultBuffer)
err = brainfckr.Interpret()
}
file, err = os.Open(resultFile)
if err != nil {
t.Errorf("Could not open result file %s", resultFile)
} else {
defer file.Close()
var expected *bufio.Reader = bufio.NewReader(file)
a := make([]byte, 1)
b := make([]byte, 1)
for err != io.EOF {
if a[0] != b[0] {
t.Errorf("Difference in bottles of beer detected. Calculated %x, Got %x\n", a[0], b[0])
}
_, _ = resultBuffer.Read(a)
_, err = expected.Read(b)
if a[0] == 10 && b[0] == 13 { // skip CRLF from the result file
_, err = expected.Read(b)
}
}
var c, d int
c, _ = resultBuffer.Read(a)
d, err = expected.Read(b)
if c != 0 && d != 0 {
t.Errorf("Not all characters from either input or result have been processed.\n")
}
}
}
func TestPrintBrainfuck(t *testing.T) {
fileName := "./test/printBrainfuck.bf"
resultFile := "./test/printBrainfuck_RESULT"
var resultBuffer *bytes.Buffer = bytes.NewBuffer([]byte{})
file, err := os.Open(fileName)
if err != nil {
t.Errorf("Could not open test file %s", fileName)
} else {
defer file.Close()
brainfckr := NewBrainfckr(file, os.Stdin, resultBuffer)
err = brainfckr.Interpret()
}
file, err = os.Open(resultFile)
if err != nil {
t.Errorf("Could not open result file %s", resultFile)
} else {
defer file.Close()
var expected *bufio.Reader = bufio.NewReader(file)
a := make([]byte, 1)
b := make([]byte, 1)
for err != io.EOF {
if a[0] != b[0] {
t.Errorf("Difference detected. Calculated %x, Got %x\n", a[0], b[0])
}
_, _ = resultBuffer.Read(a)
_, err = expected.Read(b)
if a[0] == 10 && b[0] == 13 { // skip CRLF from the result file
_, err = expected.Read(b)
}
}
var c, d int
c, _ = resultBuffer.Read(a)
d, err = expected.Read(b)
if c != 0 && d != 0 {
t.Errorf("Not all characters from either input or result have been processed.\n")
}
}
}
func TestReverseInput(t *testing.T) {
fileName := "./test/reverseInput.bf"
input := []byte{1, 2, 3, 4, 5, 0}
expectedOutput := []byte{5, 4, 3, 2, 1}
var writer bytes.Buffer
file, err := os.Open(fileName)
if err != nil {
t.Errorf("Could not open test file %s", fileName)
} else {
defer file.Close()
brainfckr := NewBrainfckr(file, bytes.NewReader(input), &writer)
err = brainfckr.Interpret()
}
if err != io.EOF {
t.Errorf(fmt.Sprintf("Failed to execute Test TestOutput %s", fileName))
} else {
if !bytes.Equal(expectedOutput, writer.Bytes()) {
t.Errorf("Reversed input doesn't look like expected.")
}
}
}
func TestWebRequestInput(t *testing.T) {
// Setup web server
t.Run("ServerSetup", func(t *testing.T) {
http.HandleFunc("/bf", brainfckHandler)
go http.ListenAndServe(":8080", nil)
})
input := []byte(">++++[>++++++<-]>-[[<+++++>>+<-]>-]<<[<]>>>>-\n-.<<<-.>>>-.<.<.>---.<<+++.>>>++.<<---.[>]<<.")
expectedOutput := "brainfuck"
resp, err := http.Post("http://localhost:8080/bf", "text/plain", bytes.NewReader(input))
if err != nil {
// handle error
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil || string(body) != expectedOutput {
t.Errorf("Response %s doesn't match expectedOutput %s\n", string(body), expectedOutput)
}
defer resp.Body.Close()
}
func brainfckHandler(w http.ResponseWriter, r *http.Request) {
NewBrainfckr(r.Body, os.Stdin, w).Interpret()
}