-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-rot13-feature.js
56 lines (53 loc) · 3.06 KB
/
4-rot13-feature.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
import { fakeStdin, fakeStdout } from "../helpers/fake-process.js";
import { run } from "../../app.js";
// https://en.wikipedia.org/wiki/ROT13
Feature("Rot 13", () => {
Scenario("Rot 13 on a long user input", () => {
let stdout, stdin;
Given("we have a fake stdout and stdin", () => {
stdout = fakeStdout();
stdin = fakeStdin();
});
When("Running the Rot 13 program with a long user input", async () => {
const program = `
, Read one character
----- ----- Remove 10 to check if it was the newline character
[ Skip loop if it was the newline
+++++ +++++ Add 10 to get the input character back
[ Skip forward if character is 0
>>++++[>++++++++<-] Set up divisor (32) for division loop
(MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
<+<-[ Set up dividend (x minus 1) and enter division loop
>+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward
<[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient
<<<<<- Decrement dividend
] End division loop
]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag
>--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag
++++++++++++<[ If flag then set up divisor (13) for second division loop
(MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
>-[>+>>] Reduce divisor; Normal case: increase remainder
>[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient
<<<<<- Decrease dividend
] End division loop
>>[<+>-] Add remainder back to divisor to get a useful 13
>[ Skip forward if quotient was 0
-[ Decrement quotient and skip forward if quotient was 1
-<<[-]>> Zero quotient and divisor if quotient was 2
]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1
]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0
] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
<[-] Clear remainder from first division if second division was skipped
<.[-] Output ROT13ed character from copy and clear it
<, Read next character
----- ----- Remove 10 to check if it was the newline character
] End character reading loop
`;
stdin.write("Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt\n");
await run(program, stdin, stdout);
});
Then("The output should be the exected", () => {
expect(stdout.getData()).to.eql("The Quick Brown Fox Jumps Over The Lazy Dog");
});
});
});