-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
caesar_test.go
167 lines (158 loc) · 3.6 KB
/
caesar_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
package caesar
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestEncrypt(t *testing.T) {
var caesarTestData = []struct {
description string
input string
key int
expected string
}{
{
"Basic caesar encryption with letter 'a'",
"a",
3,
"d",
},
{
"Basic caesar encryption wrap around alphabet on letter 'z'",
"z",
3,
"c",
},
{
"Encrypt a simple string with caesar encryiption",
"hello",
3,
"khoor",
},
{
"Encrypt a simple string with key 13",
"hello",
13,
"uryyb",
},
{
"Encrypt a simple string with key -13",
"hello",
-13,
"uryyb",
},
{
"With key of 26 output should be the same as the input",
"no change",
26,
"no change",
},
{
"Encrypt sentence with key 10",
"the quick brown fox jumps over the lazy dog.",
10,
"dro aesmu lbygx pyh tewzc yfob dro vkji nyq.",
},
{
"Encrypt sentence with key 10",
"The Quick Brown Fox Jumps over the Lazy Dog.",
10,
"Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq.",
},
}
for _, test := range caesarTestData {
t.Run(test.description, func(t *testing.T) {
actual := Encrypt(test.input, test.key)
if actual != test.expected {
t.Logf("FAIL: %s", test.description)
t.Fatalf("With input string '%s' and key '%d' was expecting '%s' but actual was '%s'",
test.input, test.key, test.expected, actual)
}
})
}
}
func TestDecrypt(t *testing.T) {
var caesarTestData = []struct {
description string
input string
key int
expected string
}{
{
"Basic caesar decryption with letter 'a'",
"a",
3,
"x",
},
{
"Basic caesar decryption wrap around alphabet on letter 'z'",
"z",
3,
"w",
},
{
"Decrypt a simple string with caesar encryiption",
"hello",
3,
"ebiil",
},
{
"Decrypt a simple string with key 13",
"hello",
13,
"uryyb",
},
{
"Decrypt a simple string with key -13",
"hello",
-13,
"uryyb",
},
{
"With key of 26 output should be the same as the input",
"no change",
26,
"no change",
},
{
"Decrypt sentence with key 10",
"Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq.",
10,
"The Quick Brown Fox Jumps over the Lazy Dog.",
},
}
for _, test := range caesarTestData {
t.Run(test.description, func(t *testing.T) {
actual := Decrypt(test.input, test.key)
if actual != test.expected {
t.Logf("FAIL: %s", test.description)
t.Fatalf("With input string '%s' and key '%d' was expecting '%s' but actual was '%s'",
test.input, test.key, test.expected, actual)
}
})
}
}
func Example() {
const (
key = 10
input = "The Quick Brown Fox Jumps over the Lazy Dog."
)
encryptedText := Encrypt(input, key)
fmt.Printf("Encrypt=> key: %d, input: %s, encryptedText: %s\n", key, input, encryptedText)
decryptedText := Decrypt(encryptedText, key)
fmt.Printf("Decrypt=> key: %d, input: %s, decryptedText: %s\n", key, encryptedText, decryptedText)
// Output:
// Encrypt=> key: 10, input: The Quick Brown Fox Jumps over the Lazy Dog., encryptedText: Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq.
// Decrypt=> key: 10, input: Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq., decryptedText: The Quick Brown Fox Jumps over the Lazy Dog.
}
func FuzzCaesar(f *testing.F) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
f.Add("The Quick Brown Fox Jumps over the Lazy Dog.")
f.Fuzz(func(t *testing.T, input string) {
key := rnd.Intn(26)
if result := Decrypt(Encrypt(input, key), key); result != input {
t.Fatalf("With input: '%s' and key: %d\n\tExpected: '%s'\n\tGot: '%s'", input, key, input, result)
}
})
}