|
| 1 | +package polybius_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "TheAlgorithms/Go/ciphers/polybius" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNewPolybius(t *testing.T) { |
| 9 | + t.Parallel() |
| 10 | + cases := []struct { |
| 11 | + name string |
| 12 | + size int |
| 13 | + characters string |
| 14 | + key string |
| 15 | + wantErr string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "correct initalization", size: 5, characters: "HogeF", key: "abcdefghijklmnopqrstuvwxy", wantErr: "", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "truncate characters", size: 5, characters: "HogeFuga", key: "abcdefghijklmnopqrstuvwxy", wantErr: "", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "invaid key", size: 5, characters: "HogeFuga", key: "abcdefghi", wantErr: "len(key): 9 must be as long as size squared: 25", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "invalid characters", size: 5, characters: "HogeH", key: "abcdefghijklmnopqrstuvwxy", wantErr: "\"chars\" contains same character: H", |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + for _, tc := range cases { |
| 32 | + t.Run(tc.name, func(t *testing.T) { |
| 33 | + _, err := polybius.NewPolybius(tc.key, tc.size, tc.characters) |
| 34 | + if err != nil && err.Error() != tc.wantErr { |
| 35 | + t.Errorf("failed NewPolybius: %v", err) |
| 36 | + } |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestPolybiusEncrypt(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + cases := []struct { |
| 44 | + name string |
| 45 | + text string |
| 46 | + want string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "correct encryption", text: "hoge", want: "OGGFOOHF", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "invalid encryption", text: "hogz", want: "failed encipher: Z does not exist in keys", |
| 53 | + }, |
| 54 | + } |
| 55 | + // initialize |
| 56 | + const ( |
| 57 | + size = 5 |
| 58 | + characters = "HogeFuga" |
| 59 | + key = "abcdefghijklmnopqrstuvwxy" |
| 60 | + ) |
| 61 | + p, err := polybius.NewPolybius(key, size, characters) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("failed NewPolybius: %v", err) |
| 64 | + } |
| 65 | + for _, tc := range cases { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + encrypted, err := p.Encrypt(tc.text) |
| 68 | + if err != nil { |
| 69 | + if err.Error() != tc.want { |
| 70 | + t.Errorf("failed Encrypt: %v", err) |
| 71 | + } |
| 72 | + } else if encrypted != tc.want { |
| 73 | + t.Errorf("Encrypt: %v, want: %v", encrypted, tc.want) |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestPolybiusDecrypt(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + cases := []struct { |
| 82 | + name string |
| 83 | + text string |
| 84 | + want string |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "correct decryption", text: "OGGFOOHF", want: "HOGE", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "invalid decryption(position of even number)", text: "hogz", want: "failed decipher: Z does not exist in characters", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "invalid decryption(position of odd number)", text: "hode", want: "failed decipher: D does not exist in characters", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "invalid text size which is odd", text: "hog", want: "failed decipher: the size of \"chars\" must be even", |
| 97 | + }, |
| 98 | + } |
| 99 | + // initialize |
| 100 | + const ( |
| 101 | + size = 5 |
| 102 | + characters = "HogeF" |
| 103 | + key = "abcdefghijklmnopqrstuvwxy" |
| 104 | + ) |
| 105 | + p, err := polybius.NewPolybius(key, size, characters) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("failed NewPolybius: %v", err) |
| 108 | + } |
| 109 | + for _, tc := range cases { |
| 110 | + t.Run(tc.name, func(t *testing.T) { |
| 111 | + encrypted, err := p.Decrypt(tc.text) |
| 112 | + if err != nil { |
| 113 | + if err.Error() != tc.want { |
| 114 | + t.Errorf("failed Encrypt: %v", err) |
| 115 | + } |
| 116 | + } else if encrypted != tc.want { |
| 117 | + t.Errorf("Encrypt: %v, want: %v", encrypted, tc.want) |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments