/
brainfuck.js
144 lines (118 loc) · 3.71 KB
/
brainfuck.js
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
'use strict';
var vows = require('vows'),
assert = require('assert'),
brainfuck = require('../lib/brainfuck.js');
/**
* Brainfuck mock object
*/
var brainfuckMock = {
prepare : function prepare(code) {
return {
brainfuck : brainfuck,
code : code
};
},
prepareVows : function prepareVows(code) {
return {
topic : this.prepare(code),
'WHEN it is parsed' : { topic : parse }
};
}
};
/**
* parse a Brainfuck string
*/
function parse(mock) {
var brainfuck = mock.brainfuck,
code = mock.code;
brainfuck.exec(code, this.callback);
}
/**
* assert current pointer state
*/
function assertPointer(expected) {
return function testPointer(err, output) {
assert.isNull(err); // no errors
assert.strictEqual(this.pointer, expected); // actual pointer position equals expected
};
}
/**
* strict equal assert of current memory value expected value
*/
function assertValue(expected) {
return function testValue(err, output) {
assert.isNull(err); // no errors
assert.strictEqual(this.data[this.pointer], expected); // actual value equals expected
};
}
/**
* strict equal assert of last output and expected value
*/
function assertOutput(expected) {
return function testIO(err, output) {
assert.isNull(err); // no errors
assert.strictEqual(output, expected); // last actual ouput equals expected
};
}
/**
* strict equal assert of last input and expected value
*/
function assertInput(expected) {
return function testIO(err, output) {
assert.isNull(err); // no errors
assert.strictEqual(this._input, expected); // last actual input equals expected
};
}
/**
* vows for pointer operations
*/
function pointerVows(code, expected) {
var ret = brainfuckMock.prepareVows(code);
ret['WHEN it is parsed']['THEN the pointer moves to position ' + expected] = assertPointer(expected);
return ret;
}
/**
* vows for memory value operations
*/
function valueVows(code, expected) {
var ret = brainfuckMock.prepareVows(code);
ret['WHEN it is parsed']['THEN the current value in memory changes to ' + expected] = assertValue(expected);
return ret;
}
/**
* vows for loop operations
*/
function loopVows(code, expected) {
var ret = brainfuckMock.prepareVows(code);
ret['WHEN it is parsed']['THEN the code in brackets is looped until the current value in memory is ' + expected] = assertValue(expected);
return ret;
}
/**
* vows for input operations
*/
function inputVows(code, expected) {
var ret = brainfuckMock.prepareVows(code);
ret['WHEN it is parsed']['THEN the value in STDIN equals ' + expected] = assertInput(expected);
return ret;
}
/**
* vows for output operations
*/
function outputVows(code, expected) {
var ret = brainfuckMock.prepareVows(code);
ret['WHEN it is parsed']['THEN the value in STDOUT equals ' + expected] = assertOutput(expected);
return ret;
}
/**
* setup the Brainfuck test suite
*/
vows.describe('Brainfuck')
.addBatch({ 'GIVEN a string ">"' : pointerVows('>', 1) })
.addBatch({ 'GIVEN a string ">><"' : pointerVows('>><', 1) })
.addBatch({ 'GIVEN a string "+"' : valueVows('+', 1) })
.addBatch({ 'GIVEN a string "++--"' : valueVows('++-', 1) })
.addBatch({ 'GIVEN a string "+[[[[-]]]]+++"' : valueVows('+[[[[-]]]]+++', 3) })
.addBatch({ 'GIVEN a string ">+++++++++[<++++++++>-]<"' : loopVows('>+++++++++[<++++++++>-]<', 72) })
.addBatch({ 'GIVEN a string ">+++++++++[<++++++++>-]<."' : outputVows('>+++++++++[<++++++++>-]<.', 'H') })
.addBatch({ 'GIVEN a string ","' : inputVows(',', 'H') })
.export(module);