lots of exercises in java... from https://github.com/exercism/java

CryptoSquareTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import org.junit.Test;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import static org.junit.Assert.assertEquals;
  5. public class CryptoSquareTest {
  6. @Test
  7. public void strangeCharactersAreStrippedDuringNormalization() {
  8. Crypto crypto = new Crypto("s#$%^&plunk");
  9. String expectedOutput = "splunk";
  10. assertEquals(expectedOutput, crypto.getNormalizedPlaintext());
  11. }
  12. @Test
  13. public void lettersAreLowerCasedDuringNormalization() {
  14. Crypto crypto = new Crypto("WHOA HEY!");
  15. String expectedOutput = "whoahey";
  16. assertEquals(expectedOutput, crypto.getNormalizedPlaintext());
  17. }
  18. @Test
  19. public void numbersAreKeptDuringNormalization() {
  20. Crypto crypto = new Crypto("1, 2, 3, GO!");
  21. String expectedOutput = "123go";
  22. assertEquals(expectedOutput, crypto.getNormalizedPlaintext());
  23. }
  24. @Test
  25. public void smallestSquareSizeIs2() {
  26. Crypto crypto = new Crypto("1234");
  27. int expectedOutput = 2;
  28. assertEquals(expectedOutput, crypto.getSquareSize());
  29. }
  30. @Test
  31. public void sizeOfTextWhoseLengthIsPerfectSquareIsItsSquareRoot() {
  32. Crypto crypto = new Crypto("123456789");
  33. int expectedOutput = 3;
  34. assertEquals(expectedOutput, crypto.getSquareSize());
  35. }
  36. @Test
  37. public void sizeOfTextWhoseLengthIsNoPerfectSquareIsNextBiggestSquareRoot() {
  38. Crypto crypto = new Crypto("123456789abc");
  39. int expectedOutput = 4;
  40. assertEquals(expectedOutput, crypto.getSquareSize());
  41. }
  42. @Test
  43. public void sizeIsDeterminedByNormalizedText() {
  44. Crypto crypto = new Crypto("Oh hey, this is nuts!");
  45. int expectedOutput = 4;
  46. assertEquals(expectedOutput, crypto.getSquareSize());
  47. }
  48. @Test
  49. public void segmentsAreSplitBySquareSize() {
  50. Crypto crypto = new Crypto("Never vex thine heart with idle woes");
  51. List<String> expectedOutput = Arrays.asList(new String[]{"neverv", "exthin", "eheart", "withid", "lewoes"});
  52. assertEquals(expectedOutput, crypto.getPlaintextSegments());
  53. }
  54. @Test
  55. public void segmentsAreSplitBySquareSizeUntilTextRunsOut() {
  56. Crypto crypto = new Crypto("ZOMG! ZOMBIES!!!");
  57. List<String> expectedOutput = Arrays.asList(new String[]{"zomg", "zomb", "ies"});
  58. assertEquals(expectedOutput, crypto.getPlaintextSegments());
  59. }
  60. @Test
  61. public void cipherTextCombinesTextByColumn() {
  62. Crypto crypto = new Crypto("First, solve the problem. Then, write the code.");
  63. String expectedOutput = "foeewhilpmrervrticseohtottbeedshlnte";
  64. assertEquals(expectedOutput, crypto.getCipherText());
  65. }
  66. @Test
  67. public void cipherTextSkipsCellsWithNoText() {
  68. Crypto crypto = new Crypto("Time is an illusion. Lunchtime doubly so.");
  69. String expectedOutput = "tasneyinicdsmiohooelntuillibsuuml";
  70. assertEquals(expectedOutput, crypto.getCipherText());
  71. }
  72. @Test
  73. public void normalizedCipherTextIsSplitByHeightOfSquare() {
  74. Crypto crypto = new Crypto("Vampires are people too!");
  75. String expectedOutput = "vrel aepe mset paoo irpo";
  76. assertEquals(expectedOutput, crypto.getNormalizedCipherText());
  77. }
  78. @Test
  79. public void normalizedCipherNotExactlyDivisibleBy5SpillsIntoSmallerSegment() {
  80. Crypto crypto = new Crypto("Madness, and then illumination.");
  81. String expectedOutput = "msemo aanin dninn dlaet ltshu i";
  82. assertEquals(expectedOutput, crypto.getNormalizedCipherText());
  83. }
  84. @Test
  85. public void normalizedCipherIsSplitIntoSegmentsOfCorrectSize() {
  86. Crypto crypto = new Crypto("If man was meant to stay on the ground god would have given us roots");
  87. String expectedOutput = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghns seoau";
  88. assertEquals(expectedOutput, crypto.getNormalizedCipherText());
  89. }
  90. @Test
  91. public void normalizedCipherTextIsSplitIntoSegmentsOfCorrectSizeWithPunctuation() {
  92. Crypto crypto = new Crypto("Have a nice day. Feed the dog & chill out!");
  93. String expectedOutput = "hifei acedl veeol eddgo aatcu nyhht";
  94. assertEquals(expectedOutput, crypto.getNormalizedCipherText());
  95. }
  96. }