summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--caesar.go3
-rw-r--r--caesar_test.go37
-rw-r--r--go.mod6
-rw-r--r--go.sum13
-rw-r--r--run_test.go17
-rw-r--r--unusual_test.go11
6 files changed, 29 insertions, 58 deletions
diff --git a/caesar.go b/caesar.go
index 4829e6a..9e36d76 100644
--- a/caesar.go
+++ b/caesar.go
@@ -34,6 +34,9 @@ func (rr RuneRange) size() int {
// shift shifts r by d within the range, modulo the size of the range.
func (rr RuneRange) shift(r rune, d int) rune {
pos := int(r - rr.Start)
+ for d < 0 {
+ d = rr.size() + d
+ }
newPos := (pos + d) % rr.size()
return rr.Start + rune(newPos)
}
diff --git a/caesar_test.go b/caesar_test.go
index 6a01f43..9754edd 100644
--- a/caesar_test.go
+++ b/caesar_test.go
@@ -16,14 +16,16 @@ func TestLong(t *testing.T) {
if testing.Short() {
t.Skip()
}
- msg := "Attack at dawn"
- encoded := Encode(msg)
- if encoded == msg {
- t.Errorf("expected ciphertext and plaintext to differ")
+ orig := "Attack at dawn"
+ msg := orig
+ for i := 0; i < 1e6; i++ {
+ msg = Encode(msg)
+ }
+ for i := 0; i < 1e6; i++ {
+ msg = Decode(msg)
}
- decoded := Decode(encoded)
- if decoded != msg {
- t.Errorf("expected recovered plaintext to match message, but got: %s", decoded)
+ if msg != orig {
+ t.Errorf("expected %q but got %q", orig, msg)
}
}
@@ -45,27 +47,6 @@ func TestEncoderTable(t *testing.T) {
}
}
-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()
}
diff --git a/go.mod b/go.mod
index 5c4dc27..6232867 100644
--- a/go.mod
+++ b/go.mod
@@ -1,9 +1,3 @@
module bnbl.io/caesar
go 1.13
-
-require (
- github.com/emersion/go-maildir v0.0.0-20190727102040-941194b0ac70 // indirect
- github.com/ogier/pflag v0.0.1 // indirect
- golang.org/x/tools v0.0.0-20190916173350-3512ebf57407 // indirect
-)
diff --git a/go.sum b/go.sum
deleted file mode 100644
index a9ced96..0000000
--- a/go.sum
+++ /dev/null
@@ -1,13 +0,0 @@
-github.com/emersion/go-maildir v0.0.0-20190727102040-941194b0ac70 h1:aUiPu6/iCjcsnNe/WkhsnMOq7vPmkYo9kFaMX5FiNZU=
-github.com/emersion/go-maildir v0.0.0-20190727102040-941194b0ac70/go.mod h1:I2j27lND/SRLgxROe50Vam81MSaqPFvJ0OHNnDZ7n84=
-github.com/ogier/pflag v0.0.1 h1:RW6JSWSu/RkSatfcLtogGfFgpim5p7ARQ10ECk5O750=
-github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
-golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
-golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
-golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/tools v0.0.0-20190916173350-3512ebf57407 h1:8t/stSbOaNHdu6dn/zC9OeTTqND69t0sA6uEkZVcILg=
-golang.org/x/tools v0.0.0-20190916173350-3512ebf57407/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
-golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
diff --git a/run_test.go b/run_test.go
new file mode 100644
index 0000000..79a5a55
--- /dev/null
+++ b/run_test.go
@@ -0,0 +1,17 @@
+package caesar
+
+import "testing"
+
+func TestCoder(t *testing.T) {
+ coder := Coder{Key: 1, Ranges: []RuneRange{{'a', 'z'}}}
+ t.Run("Encode", func(t *testing.T) {
+ if coder.Encode("abc") != "bcd" {
+ t.Fail()
+ }
+ })
+ t.Run("Decode", func(t *testing.T) {
+ if coder.Decode("bcd") != "abc" {
+ t.Fail()
+ }
+ })
+}
diff --git a/unusual_test.go b/unusual_test.go
deleted file mode 100644
index 8faa0af..0000000
--- a/unusual_test.go
+++ /dev/null
@@ -1,11 +0,0 @@
-// +build unusual
-
-package caesar
-
-import (
- "testing"
-)
-
-func TestUnusual(t *testing.T) {
- t.Fatalf("Here is a test I never really want to run")
-}