Last active
January 29, 2016 15:25
-
-
Save dulimarta/927143aa8929d068e5d4 to your computer and use it in GitHub Desktop.
ScramblerTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cis163.text_scrambler.test; | |
import cis163.text_scrambler.GVList; | |
import cis163.text_scrambler.Scrambler; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.Timeout; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
import static org.junit.Assert.*; | |
/** | |
* Created by Hans Dulimarta | |
*/ | |
public class ScramblerTester { | |
private Scrambler textor; | |
private Random rnd; | |
// @Rule | |
// public Timeout timeLimit = new Timeout(3, TimeUnit.SECONDS); | |
@Before | |
public void setup () { | |
/* TODO replace XYZ below with your text scrambling class */ | |
fail ("You must instantiate your scrambler object first"); | |
// textor = new XYZ(); | |
rnd = new Random(); | |
} | |
private GVList<String> makeListForString(String str) { | |
fail ("You must replace PQR your own list class here"); | |
GVList<String> out = null; | |
// out = new PQR<String>(); | |
if (str != null) { | |
for (String s : str.split(" ")) | |
out.append(s); | |
} | |
return out; | |
} | |
@Test | |
public void formattedTextShallNotContainLeadingOrTrailingSpace() { | |
GVList<String>[] out = textor.reformat("AAA BBB CCC DDD EEE FFF GGG", 7); | |
assertEquals(4, out.length); | |
for (GVList<String> line : out) { | |
String firstWord = line.front(); | |
String lastWord = line.back(); | |
assertFalse ("Each line of reformatted output should not contain leading space", | |
Character.isSpaceChar(firstWord.charAt(0))); | |
assertFalse ("Each line of reformatted output should not contain trailing space", | |
Character.isSpaceChar(lastWord.charAt(lastWord.length()-1))); | |
} | |
} | |
@Test | |
public void reformatMustCountSpaceBetweenWords() { | |
GVList<String>[] out = textor.reformat("AAA BBB CCC DDD EEE FFF GGG", 6); | |
assertEquals(7, out.length); | |
for (GVList<String> line : out) { | |
assertEquals (3, line.front().length()); | |
assertFalse (Character.isSpaceChar(line.front().charAt(0))); | |
assertFalse (Character.isSpaceChar(line.front().charAt(2))); | |
} | |
} | |
@Test | |
public void reformatShouldRespectRequestedLineLength() { | |
GVList<String>[] out = textor.reformat(randomSentence(30), 25); | |
for (GVList<String> line : out) { | |
int totalWordLen = 0; | |
int numWords = line.size(); | |
while (line.size() > 0) { | |
totalWordLen += line.removeFront().length(); | |
} | |
assertTrue("Eact line should not be longer that desired width", | |
totalWordLen + numWords - 1 <= 25); | |
} | |
} | |
@Test | |
public void reformatShouldNotSkipAnyWord() { | |
String data = randomSentence(30); | |
GVList<String>[] out = textor.reformat(data, 25); | |
String[] expected = data.split(" "); | |
int tokIndex = 0; | |
for (GVList<String> line : out) { | |
while (line.size() > 0) { | |
String s = line.removeFront(); | |
assertEquals(expected[tokIndex], s); | |
tokIndex++; | |
} | |
} | |
} | |
@Test (expected = IllegalArgumentException.class) | |
public void reformatShouldThrowExceptionWhenWidthTooSmall() { | |
textor.reformat("This paragraph is too big for 6 columns", 6); | |
} | |
@Test | |
public void scramblingOneWordShouldReturnThatWord() { | |
String testWord = randomWord(9); | |
GVList<String> input = makeListForString(testWord); | |
GVList<String>[] output = textor.scramble(new GVList[]{input}); | |
assertEquals(1, output.length); | |
assertEquals(testWord, output[0].front()); | |
} | |
@Test | |
public void scrambleOneLineProducesMultiLines() { | |
String testLine = randomSentence(10); | |
GVList<String> input = makeListForString(testLine); | |
GVList<String>[] output = textor.scramble(new GVList[]{input}); | |
String[] toks = testLine.split(" "); | |
assertEquals(toks.length, output.length); | |
for (int k = 0; k < toks.length; k++) { | |
assertEquals(toks[k], output[k].front()); | |
} | |
} | |
@Test | |
public void handoutExample() { | |
String[] expected = { | |
"We paragraph of the \"text algorithm in", | |
"will for a scrambling\" this project.", | |
"be demonstration", | |
"using", | |
"the", | |
"following", | |
"short" | |
}; | |
String[] input = { | |
"We will be using the following short", | |
"paragraph for a demonstration", | |
"of the \"text scrambling\"", | |
"algorithm in this project." | |
}; | |
scramblePair (input, expected); | |
} | |
@Test | |
public void requireFiller() { | |
String[] input = { | |
"efficient implementation of this", | |
"model", | |
"requires some knowledge of string", "processing"}; | |
String[] expected = { | |
"efficient model#### requires some processing###", | |
"implementation ############## knowledge of string", | |
"of", "this" | |
}; | |
scramblePair(input, expected); | |
} | |
@Test | |
public void paragraphOfUniformLength() { | |
final int NUMLINES = 50; | |
final int NUMWORDS = 10; | |
GVList<String>[] input = new GVList[NUMLINES]; | |
StringBuilder sb = new StringBuilder(); | |
for (int k = 0; k < NUMLINES; k++) { | |
sb.setLength(0); | |
for (int m = 0; m < NUMWORDS; m++) { | |
sb.append(" " + randomWord(7)); | |
} | |
input[k] = makeListForString(sb.toString().substring(1)); | |
} | |
GVList<String>[] result = textor.scramble(input); | |
assertEquals(NUMWORDS, result.length); | |
// List<String> output = textor.scramble(result); | |
// assertEquals(input, output); | |
} | |
@Test | |
public void randomParagraphOfIncreasingLength() { | |
final int NUMLINES = 50; | |
final int NUMWORDS = 10; | |
GVList<String>[] input = new GVList[NUMLINES]; | |
StringBuilder sb = new StringBuilder(); | |
int expectedLength = 0; | |
for (int k = 0; k < NUMLINES; k++) { | |
sb.setLength(0); | |
expectedLength += 4 + k; | |
for (int m = 0; m < NUMWORDS; m++) { | |
sb.append(" " + randomWord(3 + k)); | |
} | |
input[k] = makeListForString(sb.toString().substring(1)); | |
} | |
expectedLength--; | |
GVList<String>[] result = textor.scramble(input); | |
assertEquals(NUMWORDS, result.length); | |
for (GVList<String> aLine : result) { | |
int actualLen = aLine.size() - 1; | |
while (aLine.size() > 0) { | |
actualLen += aLine.removeFront().length(); | |
} | |
assertEquals("Scrambled string is too long/short", | |
expectedLength, actualLen); | |
} | |
// List<String> output = textor.scramble(result); | |
// assertEquals(input, output); | |
} | |
@Test | |
public void alphabeticalParagraphOfIncreasingLength() { | |
final int NUMLINES = 26; /* keep this number <= 26? */ | |
final int NUMWORDS = 10; | |
GVList<String>[] input = new GVList[NUMLINES]; | |
StringBuilder sb = new StringBuilder(); | |
int expectedLength = 0; | |
for (int k = 0; k < NUMLINES; k++) { | |
sb.setLength(0); | |
expectedLength += 4 + k; | |
for (int n = 0; n < 3 + k; n++) | |
sb.append((char)('A' + k)); | |
String text = sb.toString(); | |
sb.setLength(0); | |
for (int m = 0; m < NUMWORDS; m++) { | |
sb.append(" " + text); | |
} | |
input[k] = makeListForString(sb.toString().substring(1)); | |
} | |
expectedLength--; | |
GVList<String>[] result = textor.scramble(input); | |
assertEquals(NUMWORDS, result.length); | |
for (GVList<String> aLine : result) { | |
int actualLen = aLine.size() - 1; | |
while (aLine.size() > 0) { | |
String s = aLine.removeFront(); | |
int strPos = s.indexOf('#'); | |
assertEquals("The scrambled text should not contain filler strings", | |
-1, strPos); | |
actualLen += s.length(); | |
} | |
assertEquals("Scrambled string is too long/short", | |
expectedLength, actualLen); | |
} | |
/* verify the first line contains the right sequence of characters */ | |
// int len = 4; | |
// int pos = 0; | |
// char testChar = 'A'; | |
// String testLine = result.get(0); | |
// while (pos < testLine.length()) { | |
// assertEquals(testChar, testLine.charAt(pos)); | |
// pos += len; | |
// len++; | |
// testChar ++; | |
// } | |
// for (int k = 1; k < result.size(); k++) | |
// assertEquals(result.get(0), result.get(k)); | |
} | |
/* | |
AAAAAAAAAA | |
BBBBBBBBB# | |
CCCCCCCC## | |
DDDDDDD### | |
*/ | |
@Test | |
public void linesOfWordDecreasingLength() { | |
final int MAXLEN = 10; | |
StringBuilder sb = new StringBuilder(); | |
char testChar = 'A'; | |
GVList<String>[] input = new GVList[MAXLEN]; | |
for (int k = MAXLEN; k >= 1; k--) { | |
sb.setLength(0); | |
for (int n = 0; n < k; n++) | |
sb.append(testChar); | |
testChar++; | |
input[MAXLEN - k] = makeListForString(sb.toString()); | |
} | |
GVList<String>[] result = textor.scramble(input); | |
assertEquals("Only one line of output is expected", | |
1, result.length); | |
GVList<String> toks = result[0]; | |
assertEquals(MAXLEN, toks.size()); | |
testChar = 'A'; | |
int k = 0; | |
while (toks.size() > 0) { | |
String w = toks.removeFront(); | |
assertEquals(MAXLEN, w.length()); | |
/* check first char */ | |
assertEquals(testChar, w.charAt(0)); | |
testChar++; | |
int hashPos = w.indexOf('#'); | |
if (k == 0) | |
assertEquals(-1, hashPos); | |
else { | |
assertEquals(MAXLEN - k, hashPos); | |
assertEquals('#', w.charAt(MAXLEN - 1)); | |
} | |
k++; | |
} | |
} | |
/* | |
AAAAAAAAAA AAAAAAAAAA | |
BBBBBBBBB BBBBBBBBB | |
CCCCCCCC CCCCCCCC## | |
DDDDDDD DDDDDDD#### | |
EEEEEE EEEEEE###### | |
FFFFF FFFFF######## | |
GGGG GGGG########## | |
HHH HHH############ | |
II II############## | |
J J################ | |
AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA | |
BBBBBBBBB BBBBBBBBB BBBBBBBBB | |
CCCCCCCC CCCCCCCC CCCCCCCC | |
DDDDDDD DDDDDDD DDDDDDD ## | |
EEEEEE EEEEEE EEEEEE ##### | |
FFFFF FFFFF FFFFF ######## | |
GGGG GGGG GGGG ########### | |
HHH HHH HHH ############## | |
II II II ################# | |
J J J #################### | |
AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA | |
BBBBBBBBB BBBBBBBBB BBBBBBBBB BBBBBBBBB | |
CCCCCCCC CCCCCCCC CCCCCCCC CCCCCCCC | |
DDDDDDD DDDDDDD DDDDDDD DDDDDDD | |
EEEEEE EEEEEE EEEEEE EEEEEE ### | |
FFFFF FFFFF FFFFF FFFFF ####### | |
GGGG GGGG GGGG GGGG ########### | |
HHH HHH HHH HHH ############### | |
II II II II ################### | |
J J J J ####################### | |
*/ | |
@Test | |
public void paragraphOfWordsDecreasingLength () { | |
// int[][] expectedLen = { | |
// {}, | |
// {}, | |
// {190, 10}, | |
// {246, 21, 10}, | |
// {281, 50, 10, 10}, | |
// {299, 70, 21, 10, 10}, | |
// {304, 111, 30, 10, 10, 10}, | |
// {304, 138, 50, 21, 10, 10, 10} | |
// }; | |
final int MAXLEN = 10; | |
final int NUMWORDS = 7; | |
StringBuilder sb = new StringBuilder(); | |
for (int nw = 2; nw < NUMWORDS; nw++) { | |
char testChar = 'A'; | |
GVList<String>[] input = new GVList[MAXLEN]; | |
for (int k = MAXLEN; k >= 1; k--) { | |
sb.setLength(0); | |
for (int n = 0; n < k; n++) | |
sb.append(testChar); | |
testChar++; | |
String text = sb.toString(); | |
sb.setLength(0); | |
for (int n = 0; n < nw; n++) | |
sb.append(" " + text); | |
input[MAXLEN - k] = makeListForString(sb.toString().substring(1)); | |
} | |
GVList<String>[] result = textor.scramble(input); | |
assertEquals(nw, result.length); | |
int fillerCount = 0; | |
for (GVList<String> aLine : result) { | |
// String aLine = result.get(m); | |
testChar = 'A'; | |
//assertEquals(expectedLen[nw][m], aLine.length()); | |
// String[] toks = aLine.split(" "); | |
int hashPos = MAXLEN; | |
while (aLine.size() > 0) { | |
String t = aLine.removeFront(); | |
char firstOfToken = t.charAt(0); | |
if (testChar != firstOfToken) { | |
assertEquals(testChar + 1, firstOfToken); | |
testChar++; | |
} | |
int fillerCharPos = t.indexOf('#'); | |
if (fillerCharPos != -1) { | |
assertTrue(fillerCharPos < hashPos); | |
hashPos = fillerCharPos; | |
} | |
} | |
} | |
} | |
} | |
private void scramblePair (String[] test, String[] expected) { | |
GVList<String>[] input = new GVList[test.length]; | |
for (int k = 0; k < test.length; k++) | |
input[k] = makeListForString(test[k]); | |
GVList<String>[] output = textor.scramble(input); | |
assertEquals(expected.length, output.length); | |
for (int k = 0; k < expected.length; k++) { | |
String[] toks = expected[k].split(" "); | |
assertEquals(toks.length, output[k].size()); | |
int m = 0; | |
while (output[k].size() > 0) { | |
String w = output[k].removeFront(); | |
assertEquals(toks[m], w); | |
m++; | |
} | |
} | |
} | |
private GVList<String>[] convert (String[] lines) { | |
GVList<String>[] arr = new GVList[lines.length]; | |
for (int k = 0; k < arr.length; k++) | |
arr[k] = makeListForString(lines[k]); | |
return arr; | |
} | |
private void unscramblePair (String[]input, String[] expected) | |
{ | |
GVList<String>[] ref = new GVList[expected.length]; | |
for (int k = 0; k < ref.length; k++) | |
ref[k] = makeListForString(expected[k]); | |
GVList<String>[] out = textor.unscramble(convert(input)); | |
assertEquals(ref.length, out.length); | |
for (int k = 0; k < ref.length; k++) { | |
assertTrue(ref[k].equals(out[k])); | |
while (out[k].size() > 0) { | |
String s = out[k].removeFront(); | |
assertFalse("Unscramble must remove filler characters", s.contains("#")); | |
} | |
} | |
} | |
@Test | |
public void unscrambleHandoutExample() { | |
String[] text = {"We paragraph of the \"text algorithm in", | |
"will for a scrambling\" this project.", | |
"be demonstration", | |
"using", | |
"the", | |
"following", | |
"short"}; | |
String[] expected = { | |
"We will be using the following short", | |
"paragraph for a demonstration", | |
"of the \"text scrambling\"", | |
"algorithm in this project." | |
}; | |
unscramblePair(text, expected); | |
} | |
@Test | |
public void unscrambleGUIExample() { | |
String[] input = {"This is a test of scrambling algorithm", | |
"where I can check the output of the", | |
"encoded text There will be a scanning", | |
"phase"}; | |
String[] scrambled = { | |
"This where encoded phase##", | |
"is I can text There", | |
"a check will be", | |
"test the output a scanning", | |
"of of", | |
"scrambling the#######", | |
"algorithm" | |
}; | |
scramblePair(input, scrambled); | |
unscramblePair(scrambled, input); | |
} | |
@Test | |
public void unscrambleWithFiller() { | |
String[] input = { | |
"efficient model#### requires some processing###", | |
"implementation ############## knowledge of string", | |
"of", "this" | |
}; | |
String[] expected = { | |
"efficient implementation of this", "model", | |
"requires some knowledge of string", "processing" | |
}; | |
unscramblePair(input, expected); | |
} | |
@Test | |
public void scrambleUnscrambleRandomText() { | |
int numLines = 3 + rnd.nextInt(50); | |
GVList<String>[] lines = new GVList[numLines]; | |
GVList<String>[] dup = new GVList[numLines]; | |
int nwords = rnd.nextInt(6) + 1; | |
for (int k = 0; k < numLines; k++) { | |
lines[k] = makeListForString(null); | |
for (int m = 0; m < nwords; m++) { | |
lines[k].append (randomWord(rnd.nextInt(3) + 5)); | |
} | |
dup[k] = lines[k].makeCopy(); | |
} | |
GVList<String>[] temp = textor.scramble(lines); | |
GVList<String>[] recover = textor.unscramble(temp); | |
assertEquals(dup.length, recover.length); | |
for (int k = 0; k < dup.length; k++) { | |
assertTrue(dup[k].equals(recover[k])); | |
} | |
} | |
private String randomWord(int len) { | |
StringBuilder sb = new StringBuilder(); | |
for (int k = 0; k < len; k++) | |
sb.append((char) ('A' + rnd.nextInt(26))); | |
return sb.toString(); | |
} | |
private String randomSentence(int nwords) { | |
StringBuilder sb = new StringBuilder(); | |
for (int k = 0; k < nwords; k++) { | |
/* random word of 3-8 characters */ | |
String s = randomWord(rnd.nextInt(6) + 3); | |
if (k > 0) sb.append(" "); | |
sb.append(s); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment