Skip to content

Commit 979c469

Browse files
author
Michele Caci
committed
tests for rot13 algorithm
1 parent 6629aad commit 979c469

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

ciphers/Rot13.go renamed to ciphers/rot13/rot13.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package main
1+
package rot13
22

33
import (
44
"bytes"
5-
"fmt"
65
"strings"
76
)
87

@@ -24,11 +23,4 @@ func rot13(input string) string {
2423
outputBuffer.WriteString(string(newByte))
2524
}
2625
return outputBuffer.String()
27-
28-
}
29-
func main() {
30-
cleartext := "We'll just make him an offer he can't refuse... tell me you get the pop culture reference"
31-
encrypted := rot13(cleartext)
32-
decrypted := rot13(encrypted)
33-
fmt.Printf("Cleartext: %v\nencrypted: %v\ndecrypted: %v\n", cleartext, encrypted, decrypted)
3426
}

ciphers/rot13/rot13_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package rot13
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var rot13TestData = []struct {
8+
description string
9+
input string
10+
expected string
11+
}{
12+
{
13+
"Basic rotation with letter 'a' gives 'n",
14+
"a",
15+
"n",
16+
},
17+
{
18+
"Rotation with wrapping around alphabet on letter 'z' gives 'm'",
19+
"z",
20+
"m",
21+
},
22+
{
23+
"Rotation on 'hello world'",
24+
"hello world",
25+
"uryyb jbeyq",
26+
},
27+
{
28+
"Rotation on the rotation of 'hello world' gives 'hello world' back",
29+
"uryyb jbeyq",
30+
"hello world",
31+
},
32+
{
33+
"Full sentence rotation",
34+
"the quick brown fox jumps over the lazy dog.",
35+
"gur dhvpx oebja sbk whzcf bire gur ynml qbt.",
36+
},
37+
{
38+
"Sentence from Rot13.go main function",
39+
"we'll just make him an offer he can't refuse... tell me you get the pop culture reference",
40+
"jr'yy whfg znxr uvz na bssre ur pna'g ershfr... gryy zr lbh trg gur cbc phygher ersrerapr",
41+
},
42+
}
43+
44+
func TestRot13Encrypt(t *testing.T) {
45+
for _, test := range rot13TestData {
46+
t.Run(test.description, func(t *testing.T) {
47+
input := test.input
48+
expected := test.expected
49+
assertRot13Output(t, input, expected)
50+
})
51+
}
52+
}
53+
54+
func TestRot13Decrypt(t *testing.T) {
55+
for _, test := range rot13TestData {
56+
t.Run(test.description, func(t *testing.T) {
57+
input := test.expected
58+
expected := test.input
59+
assertRot13Output(t, input, expected)
60+
})
61+
}
62+
}
63+
64+
func assertRot13Output(t *testing.T, input, expected string) {
65+
actual := rot13(input)
66+
if actual != expected {
67+
t.Fatalf("With input string '%s' was expecting '%s' but actual was '%s'",
68+
input, expected, actual)
69+
}
70+
}

0 commit comments

Comments
 (0)