Skip to content

Commit 39efcac

Browse files
committed
add example_test
1 parent b0327a3 commit 39efcac

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

ciphers/polybius/polybius_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,40 @@ package polybius_test
22

33
import (
44
"TheAlgorithms/Go/ciphers/polybius"
5+
"fmt"
6+
"log"
57
"testing"
68
)
79

10+
func ExampleNewPolybius() {
11+
// initialize
12+
const (
13+
plainText = "HogeFugaPiyoSpam"
14+
size = 5
15+
characters = "HogeF"
16+
key = "abcdefghijklmnopqrstuvwxy"
17+
)
18+
p, err := polybius.NewPolybius(key, size, characters)
19+
if err != nil {
20+
log.Fatalf("failed NewPolybius: %v", err)
21+
}
22+
encryptedText, err := p.Encrypt(plainText)
23+
if err != nil {
24+
log.Fatalf("failed Encrypt: %v", err)
25+
}
26+
fmt.Printf("Encrypt=> plainText: %s, encryptedText: %s\n", plainText, encryptedText)
27+
28+
decryptedText, err := p.Decrypt(encryptedText)
29+
if err != nil {
30+
log.Fatalf("failed Decrypt: %v", err)
31+
}
32+
fmt.Printf("Decrypt=> encryptedText: %s, decryptedText: %s\n", encryptedText, decryptedText)
33+
34+
// Output:
35+
// Encrypt=> plainText: HogeFugaPiyoSpam, encryptedText: OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG
36+
// Decrypt=> encryptedText: OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG, decryptedText: HOGEFUGAPIYOSPAM
37+
}
38+
839
func TestNewPolybius(t *testing.T) {
940
t.Parallel()
1041
cases := []struct {
@@ -46,7 +77,7 @@ func TestPolybiusEncrypt(t *testing.T) {
4677
want string
4778
}{
4879
{
49-
name: "correct encryption", text: "hoge", want: "OGGFOOHF",
80+
name: "correct encryption", text: "HogeFugaPiyoSpam", want: "OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG",
5081
},
5182
{
5283
name: "invalid encryption", text: "hogz", want: "failed encipher: Z does not exist in keys",
@@ -55,7 +86,7 @@ func TestPolybiusEncrypt(t *testing.T) {
5586
// initialize
5687
const (
5788
size = 5
58-
characters = "HogeFuga"
89+
characters = "HogeF"
5990
key = "abcdefghijklmnopqrstuvwxy"
6091
)
6192
p, err := polybius.NewPolybius(key, size, characters)
@@ -84,7 +115,7 @@ func TestPolybiusDecrypt(t *testing.T) {
84115
want string
85116
}{
86117
{
87-
name: "correct decryption", text: "OGGFOOHF", want: "HOGE",
118+
name: "correct decryption", text: "OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG", want: "HOGEFUGAPIYOSPAM",
88119
},
89120
{
90121
name: "invalid decryption(position of even number)", text: "hogz", want: "failed decipher: Z does not exist in characters",

0 commit comments

Comments
 (0)