From a3a1e9decb6a5e36b76c9c7e1a149db81c60bd6e Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Tue, 17 Sep 2019 23:08:46 -0400 Subject: add examples, fix bug --- caesar.go | 9 +++++++- caesar_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++------- client/client_test.go | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/caesar.go b/caesar.go index 191c5b8..4829e6a 100644 --- a/caesar.go +++ b/caesar.go @@ -26,9 +26,16 @@ func (rr RuneRange) contains(r rune) bool { return r >= rr.Start && r <= rr.End } +// size calculates the size of the range. +func (rr RuneRange) size() int { + return int(rr.End-rr.Start) + 1 +} + // shift shifts r by d within the range, modulo the size of the range. func (rr RuneRange) shift(r rune, d int) rune { - return rr.Start + (r - rr.Start + rune(d)%(rr.End-rr.Start)) + pos := int(r - rr.Start) + newPos := (pos + d) % rr.size() + return rr.Start + rune(newPos) } // A Coder encodes and decodes Caesar cipher messages according to its key and diff --git a/caesar_test.go b/caesar_test.go index 04b853b..6a01f43 100644 --- a/caesar_test.go +++ b/caesar_test.go @@ -6,29 +6,72 @@ import ( func TestEncode(t *testing.T) { msg := "Attack at dawn" + t.Logf("testing message %s", msg) if Encode(msg) != "Dwwdfn dw gdzq" { t.Fail() } } -func TestCaesar(t *testing.T) { +func TestLong(t *testing.T) { if testing.Short() { t.Skip() } msg := "Attack at dawn" - t.Logf("testing encoding message %s", msg) encoded := Encode(msg) if encoded == msg { - t.Log("expected ciphertext and plaintext to differ") - t.Fail() + t.Errorf("expected ciphertext and plaintext to differ") } decoded := Decode(encoded) if decoded != msg { - t.Logf("expected recovered plaintext to match message, but got: %s", decoded) - t.Fail() + t.Errorf("expected recovered plaintext to match message, but got: %s", decoded) } } -func BenchmarkCaesar(b *testing.B) { +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") + } +} diff --git a/client/client_test.go b/client/client_test.go index fb1701c..b4a1b2a 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -48,7 +48,7 @@ func TestEncode(t *testing.T) { } server := httptest.NewServer(hf) defer server.Close() - c := CaesarClient{Endpoint: server.URL} + c := CaesarClient{Endpoint: server.URL} // HL r, err := c.EncodeMessage(strings.NewReader("secret message")) if err != nil { t.Errorf("should not have gotten error: %v", err) -- cgit v1.2.3