Browse Source

Safe copier tested

Mitch Taylor 6 years ago
parent
commit
4b6f4905f4
2 changed files with 69 additions and 2 deletions
  1. 12
    2
      src/main/java/io/zipcoder/MonkeyTypewriter.java
  2. 57
    0
      src/main/test/SafeCopierTest.java

+ 12
- 2
src/main/java/io/zipcoder/MonkeyTypewriter.java View File

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class MonkeyTypewriter {
4
+
4 5
     public static void main(String[] args) {
5 6
         String introduction = "It was the best of times,\n" +
6 7
                 "it was the blurst of times,\n" +
@@ -74,7 +75,16 @@ public class MonkeyTypewriter {
74 75
         }
75 76
 
76 77
         // Print out the copied versions here.
77
-        System.out.println(unsafeCopier.copied);
78
-        System.out.println(safeCopier.copied);
78
+        unsafeOutput(unsafeCopier.copied);
79
+        safeOutput(safeCopier.copied);
80
+    }
81
+
82
+    // Made these for testing and didn't need them lol
83
+    public static void unsafeOutput(String output) {
84
+        System.out.println(output);
85
+    }
86
+
87
+    public static void safeOutput(String output) {
88
+        System.out.println(output);
79 89
     }
80 90
 }

+ 57
- 0
src/main/test/SafeCopierTest.java View File

@@ -0,0 +1,57 @@
1
+import io.zipcoder.Copier;
2
+import io.zipcoder.SafeCopier;
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.io.ByteArrayOutputStream;
7
+import java.io.PrintStream;
8
+
9
+public class SafeCopierTest {
10
+
11
+    @Test
12
+    public void outputTest() {
13
+        Copier safeCopier = new SafeCopier("It was the best of times,\n" +
14
+                "it was the blurst of times,\n" +
15
+                "it was the age of wisdom,\n" +
16
+                "it was the age of foolishness,\n" +
17
+                "it was the epoch of belief,\n" +
18
+                "it was the epoch of incredulity,\n" +
19
+                "it was the season of Light,\n" +
20
+                "it was the season of Darkness,\n" +
21
+                "it was the spring of hope,\n" +
22
+                "it was the winter of despair,\n" +
23
+                "we had everything before us,\n" +
24
+                "we had nothing before us,\n" +
25
+                "we were all going direct to Heaven,\n" +
26
+                "we were all going direct the other way--\n" +
27
+                "in short, the period was so far like the present period, that some of\n" +
28
+                "its noisiest authorities insisted on its being received, for good or for\n" +
29
+                "evil, in the superlative degree of comparison only.");
30
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
31
+        System.setOut(new PrintStream(outputStream));
32
+        while (safeCopier.stringIterator.hasNext()) {
33
+            for (int i = 0; i < 5; i++) {
34
+                Thread monkey = new Thread(safeCopier);
35
+                monkey.start();
36
+            }
37
+        }
38
+        Assert.assertEquals(safeCopier.copied, "It was the best of times,\n" +
39
+                "it was the blurst of times,\n" +
40
+                "it was the age of wisdom,\n" +
41
+                "it was the age of foolishness,\n" +
42
+                "it was the epoch of belief,\n" +
43
+                "it was the epoch of incredulity,\n" +
44
+                "it was the season of Light,\n" +
45
+                "it was the season of Darkness,\n" +
46
+                "it was the spring of hope,\n" +
47
+                "it was the winter of despair,\n" +
48
+                "we had everything before us,\n" +
49
+                "we had nothing before us,\n" +
50
+                "we were all going direct to Heaven,\n" +
51
+                "we were all going direct the other way--\n" +
52
+                "in short, the period was so far like the present period, that some of\n" +
53
+                "its noisiest authorities insisted on its being received, for good or for\n" +
54
+                "evil, in the superlative degree of comparison only.");
55
+    }
56
+
57
+}