Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build
run: go build .
run: go build ./...
- name: Run tests
run: go test ./...
58 changes: 58 additions & 0 deletions conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,61 @@ func TestConvertIndexToChar(t *testing.T) {
}
}
}

func TestConvertBetweenIndexAndChar(t *testing.T) {
tests := []rune{
'A',
'B',
'C',
'X',
'Y',
'Z',
}

for _, testData := range tests {
got := ConvertIndexToChar(ConvertCharToIndex(testData))
if got != testData {
t.Errorf(
"ConvertIndexToChar and ConvertCharToIndex reciprocity test failed - got=%c, expected=%c",
got,
testData,
)
}
}
}

func TestConvertWiringStringToArray(t *testing.T) {
tests := []struct {
input string
expectedOutput []int
}{
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ", []int{0, 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}},
{"DCBAFEIHGMLKONJRQPVUTSZYXW", []int{3, 2, 1, 0, 5, 4, 8, 7, 6, 12, 11, 10, 14, 13, 9, 17, 16, 15, 21, 20, 19, 18, 25, 24, 23, 22}},
{"ZYXWVUTSRQPONMLKJIHGFEDCBA", []int{25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
}

for _, testData := range tests {
got := ConvertWiringStringToArray(testData.input)
if got != [26]int(testData.expectedOutput) {
t.Errorf("ConvertWiringStringToArray(%s) failed,\ngot=%d,\nexpected=%d", testData.input, got, testData.expectedOutput)
}
}
}

func TestInverseWiringArray(t *testing.T) {
tests := []struct {
input []int
expectedOutput []int
}{
{[]int{0, 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}, []int{0, 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}},
{[]int{19, 20, 21, 22, 23, 24, 25, 15, 16, 17, 18, 10, 11, 12, 13, 14, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3}, []int{22, 23, 24, 25, 16, 17, 18, 19, 20, 21, 11, 12, 13, 14, 15, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 25, 24, 23, 22, 21}, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 25, 24, 23, 22, 21}},
}

for _, testData := range tests {
got := InverseWiringArray([26]int(testData.input))
if got != [26]int(testData.expectedOutput) {
t.Errorf("InverseWiringArray(%d) failed,\ngot=%d,\nexpected=%d", testData.input, got, testData.expectedOutput)
}
}
}
15 changes: 15 additions & 0 deletions machine/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ func TestRingCase3(t *testing.T) {
runTest(t, machine, "HELLOWORLD", "CDKSEVMKXJ")
}

func TestProcessEmptyString(t *testing.T) {
// note: this machine configuration wouldn't be possible in real life because an operator would only
// have one of each rotor (as far as I know)
machine := &Machine{
rotor.NewRotor(rotor.RotorII, 'F', 'D'),
rotor.NewRotor(rotor.RotorII, 'P', 'W'),
rotor.NewRotor(rotor.RotorII, 'K', 'L'),
nil,
reflector.New(reflector.ReflectorB),
createPlugboard(t, "JWYLFKREVPXTHOBCMQZG"),
}

runTest(t, machine, "", "")
}

func TestRandomCase1(t *testing.T) {
// note: this machine configuration wouldn't be possible in real life because an operator would only
// have one of each rotor (as far as I know)
Expand Down
7 changes: 0 additions & 7 deletions main.go

This file was deleted.

117 changes: 117 additions & 0 deletions plugboard/plugboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package plugboard

import (
"reflect"
"testing"

"github.com/atbu/ultra/conversion"
)

func TestEmptyPlugboardString(t *testing.T) {
tests := []string{
"",
" ",
"\n",
"\t",
"\n\t ",
"\r\n\t ",
}

for _, testData := range tests {
got, err := New(testData)
if got != nil {
t.Errorf("New() in plugboard package returned unexpected output - got=%v, expected=%v", got, nil)
}
if err != nil {
t.Errorf("New() in plugboard package returned unexpected error - got=%v, expected=%v", err, nil)
}
}
}

func TestUnevenPlugboardString(t *testing.T) {
tests := []string{
"ABC",
"ABCDE",
"ZJFIWDL",
"R",
"DPWERMS",
}

for _, testData := range tests {
got, err := New(testData)
if got != nil {
t.Errorf("New() in plugboard package returned unexpected output - got=%v, expected=%v", got, nil)
}
if err == nil {
t.Errorf("New() in plugboard package returned no error when we expected it to - got=%v, expected=%v", err, ErrUnevenNumberOfCharacters)
}
}
}

func generatePlugboardPair(a rune, b rune) PlugboardPair {
return PlugboardPair{int(a - 65), int(b - 65)}
}

func TestPlugboardConstructor(t *testing.T) {
tests := []struct {
input string
expectedOutput *Plugboard
}{
{"ABCD", &Plugboard{[]PlugboardPair{
generatePlugboardPair('A', 'B'),
generatePlugboardPair('C', 'D'),
}}},
{"RKWDPT", &Plugboard{[]PlugboardPair{
generatePlugboardPair('R', 'K'),
generatePlugboardPair('W', 'D'),
generatePlugboardPair('P', 'T'),
}}},
{"QPALZMWOXNEICBRUVFGH", &Plugboard{[]PlugboardPair{
generatePlugboardPair('Q', 'P'),
generatePlugboardPair('A', 'L'),
generatePlugboardPair('Z', 'M'),
generatePlugboardPair('W', 'O'),
generatePlugboardPair('X', 'N'),
generatePlugboardPair('E', 'I'),
generatePlugboardPair('C', 'B'),
generatePlugboardPair('R', 'U'),
generatePlugboardPair('V', 'F'),
generatePlugboardPair('G', 'H'),
}}},
}

for _, testData := range tests {
got, err := New(testData.input)
if !reflect.DeepEqual(got, testData.expectedOutput) {
t.Errorf("New() in plugboard package returned unexpected output - got=%v, expected=%v", got, testData.expectedOutput)
}
if err != nil {
t.Errorf("New() in plugboard package returned unexpected error - got=%v, expected=%v", err, nil)
}
}
}

func TestMapSignal(t *testing.T) {
tests := []struct {
plugboardConfiguration string
inputSignal rune
expectedOutput rune
}{
{"ABCDEFGH", 'A', 'B'},
{"CDOFRPIT", 'D', 'C'},
{"PLEFRD", 'Z', 'Z'},
}

for _, testData := range tests {
plugboard, err := New(testData.plugboardConfiguration)
if err != nil {
t.Errorf("Failed to generate plugboard for MapSignal test - err=%s", err)
}

got := plugboard.MapSignal(conversion.ConvertCharToIndex(testData.inputSignal))
expectedOutput := conversion.ConvertCharToIndex(testData.expectedOutput)
if got != expectedOutput {
t.Errorf("MapSignal() in plugboard package returned unexpected output - got=%v, expected=%v", got, expectedOutput)
}
}
}
Loading
Loading