summaryrefslogtreecommitdiff
path: root/caesar_test.go
blob: 6a01f43a026f5bcd79c5102c5e6adda610967793 (plain)
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
package caesar

import (
	"testing"
)

func TestEncode(t *testing.T) {
	msg := "Attack at dawn"
	t.Logf("testing message %s", msg)
	if Encode(msg) != "Dwwdfn dw gdzq" {
		t.Fail()
	}
}

func TestLong(t *testing.T) {
	if testing.Short() {
		t.Skip()
	}
	msg := "Attack at dawn"
	encoded := Encode(msg)
	if encoded == msg {
		t.Errorf("expected ciphertext and plaintext to differ")
	}
	decoded := Decode(encoded)
	if decoded != msg {
		t.Errorf("expected recovered plaintext to match message, but got: %s", decoded)
	}
}

func TestEncoderTable(t *testing.T) {
	tests := []struct {
		in  string
		out string
	}{
		{"abcxyz", "defabc"},
		{"ABCXYZ", "DEFABC"},
		{"1234567890", "4567890123"},
		{"!@#$%^&*()", "!@#$%^&*()"},
	}
	for _, test := range tests {
		result := Encode(test.in)
		if result != test.out {
			t.Errorf("encode %s: expected %s, got %s", test.in, test.out, result)
		}
	}
}

func TestEncoderIndividualTable(t *testing.T) {
	tests := []struct {
		name string
		in   string
		out  string
	}{
		{"lowers", "abcxyz", "defabc"},
		{"uppers", "ABCXYZ", "DEFABC"},
		{"nums", "1234567890", "4567890123"},
		{"symbols", "!@#$%^&*()", "!@#$%^&*()"},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result := Encode(test.in)
			if result != test.out {
				t.Fatalf("expected %s, got %s", test.out, result) // HL
			}
		})
	}
}

func BenchmarkFailure(b *testing.B) {
	b.Fail()
}

func BenchmarkEncode(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Encode("attack at dawn")
	}
}