OnJava8-Examples/annotations/AtUnitExample4.java

81 lines
2.3 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// annotations/AtUnitExample4.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
package annotations;
import java.util.*;
2015-12-06 11:45:16 -08:00
import onjava.atunit.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
public class AtUnitExample4 {
static String theory = "All brontosauruses " +
"are thin at one end, much MUCH thicker in the " +
"middle, and then thin again at the far end.";
private String word;
private Random rand = new Random(); // Time-based seed
public AtUnitExample4(String word) { this.word = word; }
public String getWord() { return word; }
public String scrambleWord() {
2015-11-03 12:00:44 -08:00
// <* Improve this: *>
2015-06-15 17:47:35 -07:00
List<Character> chars = new ArrayList<>();
for(Character c : word.toCharArray())
chars.add(c);
Collections.shuffle(chars, rand);
StringBuilder result = new StringBuilder();
for(char ch : chars)
result.append(ch);
return result.toString();
}
@TestProperty static List<String> input =
Arrays.asList(theory.split(" "));
@TestProperty
static Iterator<String> words = input.iterator();
@TestObjectCreate static AtUnitExample4 create() {
if(words.hasNext())
return new AtUnitExample4(words.next());
else
return null;
}
@Test boolean words() {
2015-11-03 12:00:44 -08:00
System.out.println("'" + getWord() + "'");
2015-06-15 17:47:35 -07:00
return getWord().equals("are");
}
@Test boolean scramble1() {
// Change to a specific seed to get verifiable results:
rand = new Random(47);
2015-11-03 12:00:44 -08:00
System.out.println("'" + getWord() + "'");
2015-06-15 17:47:35 -07:00
String scrambled = scrambleWord();
2015-11-03 12:00:44 -08:00
System.out.println(scrambled);
2015-06-15 17:47:35 -07:00
return scrambled.equals("lAl");
}
@Test boolean scramble2() {
rand = new Random(74);
2015-11-03 12:00:44 -08:00
System.out.println("'" + getWord() + "'");
2015-06-15 17:47:35 -07:00
String scrambled = scrambleWord();
2015-11-03 12:00:44 -08:00
System.out.println(scrambled);
2015-06-15 17:47:35 -07:00
return scrambled.equals("tsaeborornussu");
}
public static void main(String[] args) throws Exception {
System.out.println("starting");
2015-12-16 13:50:01 -08:00
OSExecute.command("java -cp .. " +
"onjava.atunit.AtUnit AtUnitExample4.class");
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
starting
annotations.AtUnitExample4
2015-12-02 09:20:27 -08:00
. words 'All'
(failed)
2015-12-15 11:47:04 -08:00
. scramble2 'brontosauruses'
tsaeborornussu
. scramble1 'are'
rae
2015-12-02 09:20:27 -08:00
(failed)
(3 tests)
2015-12-15 11:47:04 -08:00
>>> 2 FAILURES <<<
2015-12-02 09:20:27 -08:00
annotations.AtUnitExample4: words
annotations.AtUnitExample4: scramble1
2015-09-07 11:44:36 -06:00
*/