@matfah/

Master String FR 2008

Java

No description

fork
loading
Files
  • Main.java
  • StringCoder.java
  • StringPart.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.util.ArrayList;
import java.lang.reflect.*;

public class Main {
  
  private static String[] pangrams = {
    "the quick brown fox jumps over a lazy dog",
    "pack my box with five dozen liquor jugs",
    "how quickly daft jumping zebras vex"
  };
  
  private static String[] words = {
    "can you feel the love tonight",
    "i am the very model of a modern major general",
    "all we have to fear is fear itself",
    "sally sells sea shells by the sea shore",
    "teanage mutant ninja turtles in a half shell turtle power",
    "transformers are more than meets the eye"
  };
  
  public static void main(String[] args) {
    for(int i = 0; i < 10; i++)
      testDecodeString();
      
    for(int i = 0; i < 10; i++)
      testEncodeString();
      
    System.out.println("All tests passed!");
  }
  
  public static void testEncodeString() {
    Field masterField = null;
    
    try {
      masterField = StringCoder.class.getDeclaredField("masterString");
    }
    catch(Exception e) {
      System.out.println("Did you change the master instance variable in the StringCoder class?!");
      System.exit(0);
    }
    masterField.setAccessible(true);
    
    String master = pangrams[(int)(Math.random()*pangrams.length)];
    CustomStringCoder coder = new CustomStringCoder(master);
    
    String randomWord = words[(int)(Math.random()*words.length)];
    
    ArrayList<StringPart> parts = null;
    
    try {
      parts = coder.encodeString(randomWord);
    }
    catch(Exception e) {
      System.out.println("Error calling encodeString() method of StringCoder class");
      System.exit(0);
    }
    
    if(parts == null) {
      System.out.println("encodeString() should not return null");
      System.exit(0);
    }
    
    if(coder.findPartCount != parts.size()) {
      System.out.println("encodeString() is not calling findPart the right number of times");
      System.exit(0);
    }
    
    String result = coder.decodeString(parts);
    
    if(!result.equals(randomWord)) {
      System.out.println("encodeString() is not generated the right list of StringParts");
      System.exit(0);
    }
    
    String masterChanged = null;
    
    try {
     masterChanged = (String)masterField.get(coder);
    }
    catch(Exception e){
      System.out.println("Error trying to access the master instance variable of the StringCoder class");
      System.exit(0);
    }
    
    if(!master.equals(masterChanged)) {
      System.out.println("decodeString() should not change the master instance variable!");
      System.exit(0);
    }
  }
  
  public static void testDecodeString() {
    
    Field masterField = null;
    
    try {
      masterField = StringCoder.class.getDeclaredField("masterString");
    }
    catch(Exception e) {
      System.out.println("Did you change the master instance variable in the StringCoder class?!");
      System.exit(0);
    }
    masterField.setAccessible(true);
    
    
    String master = pangrams[(int)(Math.random()*pangrams.length)];
    StringCoder coder = new StringCoder(master);
    
    String answer = "";
    ArrayList<StringPart> parts = new ArrayList<StringPart>();
    
    for(int i = (int)(Math.random()*10) + 1; i > 0; i--) {
      
      int start = (int)(Math.random()*master.length());
      
      String rest = master.substring(start);
      
      int len = (int)(Math.random() * rest.length());
      
      answer += rest.substring(0, len);
      
      parts.add(new StringPart(start, len));
      
    }
    
    String result = "";
    
    try {
      result = coder.decodeString(parts);
    }
    catch(Exception e) {
      System.out.println("Calling decodeString() generated an error!");
      System.exit(0);
    }
    
    if(!result.equals(answer)) {
      System.out.println("Result of decodeString() is not correct!");
      System.exit(0);
    }
    
    
    
    String masterChanged = null;
    
    try {
     masterChanged = (String)masterField.get(coder);
    }
    catch(Exception e){
      System.out.println("Error trying to access the master instance variable of the StringCoder class");
      System.exit(0);
    }
    
    if(!master.equals(masterChanged)) {
      System.out.println("decodeString() should not change the master instance variable!");
      System.exit(0);
    }
    
  }
}

class CustomStringCoder extends StringCoder {
  
  String copy;
  int findPartCount = 0;
  
  public CustomStringCoder(String master) { 
    super(master);
    copy = master;
  }
  
  public StringPart findPart(String str) {  
    findPartCount++;
    char start = str.charAt(0);
    
    int startPos = copy.indexOf(start);
    
    int i = 0;
    int j = startPos;
    
    while(i + 1 < str.length() && j + 1 < copy.length() && str.charAt(i+1) == copy.charAt(j+1)) {
      i++;
      j++;
    }
    
    return new StringPart(startPos, i+1);
    
  }
}