|
| 1 | +//: annotations/AtUnitExample4.java |
| 2 | +// ©2015 MindView LLC: see Copyright.txt |
| 3 | +package annotations; |
| 4 | +import java.util.*; |
| 5 | +import com.mindviewinc.atunit.*; |
| 6 | +import com.mindviewinc.util.*; |
| 7 | +import static com.mindviewinc.util.Print.*; |
| 8 | + |
| 9 | +public class AtUnitExample4 { |
| 10 | + static String theory = "All brontosauruses " + |
| 11 | + "are thin at one end, much MUCH thicker in the " + |
| 12 | + "middle, and then thin again at the far end."; |
| 13 | + private String word; |
| 14 | + private Random rand = new Random(); // Time-based seed |
| 15 | + public AtUnitExample4(String word) { this.word = word; } |
| 16 | + public String getWord() { return word; } |
| 17 | + public String scrambleWord() { |
| 18 | + List<Character> chars = new ArrayList<>(); |
| 19 | + for(Character c : word.toCharArray()) |
| 20 | + chars.add(c); |
| 21 | + Collections.shuffle(chars, rand); |
| 22 | + StringBuilder result = new StringBuilder(); |
| 23 | + for(char ch : chars) |
| 24 | + result.append(ch); |
| 25 | + return result.toString(); |
| 26 | + } |
| 27 | + @TestProperty static List<String> input = |
| 28 | + Arrays.asList(theory.split(" ")); |
| 29 | + @TestProperty |
| 30 | + static Iterator<String> words = input.iterator(); |
| 31 | + @TestObjectCreate static AtUnitExample4 create() { |
| 32 | + if(words.hasNext()) |
| 33 | + return new AtUnitExample4(words.next()); |
| 34 | + else |
| 35 | + return null; |
| 36 | + } |
| 37 | + @Test boolean words() { |
| 38 | + print("'" + getWord() + "'"); |
| 39 | + return getWord().equals("are"); |
| 40 | + } |
| 41 | + @Test boolean scramble1() { |
| 42 | + // Change to a specific seed to get verifiable results: |
| 43 | + rand = new Random(47); |
| 44 | + print("'" + getWord() + "'"); |
| 45 | + String scrambled = scrambleWord(); |
| 46 | + print(scrambled); |
| 47 | + return scrambled.equals("lAl"); |
| 48 | + } |
| 49 | + @Test boolean scramble2() { |
| 50 | + rand = new Random(74); |
| 51 | + print("'" + getWord() + "'"); |
| 52 | + String scrambled = scrambleWord(); |
| 53 | + print(scrambled); |
| 54 | + return scrambled.equals("tsaeborornussu"); |
| 55 | + } |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + System.out.println("starting"); |
| 58 | + OSExecute.command( |
| 59 | + "java com.mindviewinc.atunit.AtUnit AtUnitExample4"); |
| 60 | + } |
| 61 | +} /* Output: |
| 62 | +starting |
| 63 | +annotations.AtUnitExample4 |
| 64 | + . scramble1 'All' |
| 65 | +lAl |
| 66 | + . scramble2 'brontosauruses' |
| 67 | +tsaeborornussu |
| 68 | + . words 'are' |
| 69 | +OK (3 tests) |
| 70 | +*///:~ |
0 commit comments