Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

rot13_test.gno

1.68 Kb · 60 lines
 1package rot13
 2
 3import (
 4	"strconv"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10func TestRot13(t *testing.T) {
11	cases := []struct {
12		in, want string
13	}{
14		{"", ""},
15		{"Hello", "Uryyb"},
16		{"Uryyb", "Hello"},                     // decode
17		{"abcxyz", "nopklm"},                   // wraps within lowercase
18		{"ABCXYZ", "NOPKLM"},                   // wraps within uppercase
19		{"Hello, Gno! 123", "Uryyb, Tab! 123"}, // punctuation & digits untouched
20	}
21	for _, c := range cases {
22		got := Rot13(c.in)
23		uassert.Equal(t, c.want, got, "Rot13(%q)", c.in)
24	}
25}
26
27func TestRot13IsItsOwnInverse(t *testing.T) {
28	inputs := []string{"", "Hello, Gno!", "The quick brown fox", "MixedCASE 42!"}
29	for _, in := range inputs {
30		uassert.Equal(t, in, Rot13(Rot13(in)), "Rot13(Rot13(%q)) should round-trip", in)
31	}
32}
33
34func TestCaesar(t *testing.T) {
35	cases := []struct {
36		in    string
37		shift int
38		want  string
39	}{
40		{"abc", 1, "bcd"},
41		{"xyz", 3, "abc"},                  // wraps around the end
42		{"ABC", 25, "ZAB"},                 // large shift, uppercase
43		{"Hello", 0, "Hello"},              // no-op
44		{"Hello", 26, "Hello"},             // full rotation
45		{"bcd", -1, "abc"},                 // negative shift
46		{"a-b c!", 1, "b-c d!"},            // non-letters untouched
47		{"Hello, Gno!", 13, "Uryyb, Tab!"}, // shift 13 == ROT13
48	}
49	for _, c := range cases {
50		got := Caesar(c.in, c.shift)
51		uassert.Equal(t, c.want, got, "Caesar("+strconv.Quote(c.in)+", "+strconv.Itoa(c.shift)+")")
52	}
53}
54
55func TestCaesarEqualsRot13At13(t *testing.T) {
56	inputs := []string{"Hello, Gno!", "The quick brown fox jumps"}
57	for _, in := range inputs {
58		uassert.Equal(t, Rot13(in), Caesar(in, 13), "Caesar(%q,13) should equal Rot13", in)
59	}
60}