Преглед изворни кода

Merge pull request #62 from jmluy/master

atbash: Add test case and sample implementation for decode.
John Ryan пре 11 година
родитељ
комит
9eb0486136
2 измењених фајлова са 63 додато и 22 уклоњено
  1. 11
    0
      atbash-cipher/src/example/java/Atbash.java
  2. 52
    22
      atbash-cipher/src/test/java/AtbashTest.java

+ 11
- 0
atbash-cipher/src/example/java/Atbash.java Прегледај датотеку

@@ -18,6 +18,17 @@ public class Atbash {
18 18
         return splitIntoFiveLetterWords(cyphered);
19 19
     }
20 20
 
21
+    public static String decode(String input) {
22
+        String encoded = stripInvalidCharacters(input).toLowerCase();
23
+        String deciphered = "";
24
+
25
+        for (char c : encoded.toCharArray()) {
26
+            deciphered += applyCipher(c);
27
+        }
28
+
29
+        return deciphered;
30
+    }
31
+
21 32
     private static String stripInvalidCharacters(String input) {
22 33
         String filteredValue = "";
23 34
 

+ 52
- 22
atbash-cipher/src/test/java/AtbashTest.java Прегледај датотеку

@@ -1,38 +1,68 @@
1 1
 import org.junit.Test;
2
+import org.junit.experimental.runners.Enclosed;
2 3
 import org.junit.runner.RunWith;
3 4
 import org.junit.runners.Parameterized;
5
+import org.junit.runners.Parameterized.Parameters;
4 6
 
5 7
 import java.util.Arrays;
6 8
 import java.util.Collection;
7 9
 
8 10
 import static org.junit.Assert.assertEquals;
9 11
 
10
-@RunWith(Parameterized.class)
12
+@RunWith(Enclosed.class)
11 13
 public class AtbashTest {
12 14
 
13
-    private String input;
14
-    private String expectedOutput;
15
-
16
-    @Parameterized.Parameters
17
-    public static Collection<Object[]> data() {
18
-        return Arrays.asList(new Object[][]{
19
-                {"no", "ml"},
20
-                {"yes", "bvh"},
21
-                {"OMG", "lnt"},
22
-                {"mindblowingly", "nrmwy oldrm tob"},
23
-                {"Testing, 1 2 3, testing.", "gvhgr mt123 gvhgr mt"},
24
-                {"Truth is fiction.", "gifgs rhurx grlm"},
25
-                {"The quick brown fox jumps over the lazy dog.", "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"}
26
-        });
27
-    }
15
+    @RunWith(Parameterized.class)
16
+    public static class EncodeTest {
17
+        private String input;
18
+        private String expectedOutput;
19
+
20
+        @Parameters
21
+        public static Collection<Object[]> data() {
22
+            return Arrays.asList(new Object[][] {
23
+                    { "no", "ml" },
24
+                    { "yes", "bvh" },
25
+                    { "OMG", "lnt" },
26
+                    { "mindblowingly", "nrmwy oldrm tob" },
27
+                    { "Testing, 1 2 3, testing.", "gvhgr mt123 gvhgr mt" },
28
+                    { "Truth is fiction.", "gifgs rhurx grlm" },
29
+                    { "The quick brown fox jumps over the lazy dog.", "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" }
30
+            });
31
+        }
32
+
33
+        public EncodeTest(String input, String expectedOutput) {
34
+            this.input = input;
35
+            this.expectedOutput = expectedOutput;
36
+        }
28 37
 
29
-    public AtbashTest(String input, String expectedOutput) {
30
-        this.input = input;
31
-        this.expectedOutput = expectedOutput;
38
+        @Test
39
+        public void test() {
40
+            assertEquals(expectedOutput, Atbash.encode(input));
41
+        }
32 42
     }
33 43
 
34
-    @Test
35
-    public void test() {
36
-        assertEquals(expectedOutput, Atbash.encode(input));
44
+    @RunWith(Parameterized.class)
45
+    public static class DecodeTest {
46
+        private String input;
47
+        private String expectedOutput;
48
+
49
+        @Parameters
50
+        public static Collection<Object[]> data() {
51
+            return Arrays.asList(new Object[][] {
52
+                    { "vcvix rhn", "exercism" },
53
+                    { "zmlyh gzxov rhlug vmzhg vkkrm thglm v", "anobstacleisoftenasteppingstone" },
54
+                    { "gvhgr mt123 gvhgr mt", "testing123testing" }
55
+            });
56
+        }
57
+
58
+        public DecodeTest(String input, String expectedOutput) {
59
+            this.input = input;
60
+            this.expectedOutput = expectedOutput;
61
+        }
62
+
63
+        @Test
64
+        public void test() {
65
+            assertEquals(expectedOutput, Atbash.decode(input));
66
+        }
37 67
     }
38 68
 }