|
| 1 | +packagecom.thealgorithms.ciphers; |
| 2 | + |
| 3 | +importstaticorg.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +importstaticorg.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +importorg.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +publicclassMyCaesarTest { |
| 9 | +privatefinalCaesarcaesar =newCaesar(); |
| 10 | + |
| 11 | +@Test |
| 12 | +voidshouldReturnSameAsInputWhenShiftingZeroOr26() { |
| 13 | +Stringmessage ="message"; |
| 14 | + |
| 15 | +Stringencoded1 =caesar.encode(message,0); |
| 16 | +Stringencoded2 =caesar.encode(message,26); |
| 17 | + |
| 18 | +assertEquals(message,encoded1,"Encoded should be same as original"); |
| 19 | +assertEquals(message,encoded2,"Encoded should be same as original"); |
| 20 | + } |
| 21 | + |
| 22 | +@Test |
| 23 | +voidshouldReturnsameAsInputWhenUsingCharactersOutsideLatinAlphabet() { |
| 24 | +Stringmessage ="!#¤%&/()=?`^½§@£$€{[]}´`¨~'*-.,_:;<>|"; |
| 25 | + |
| 26 | +Stringencoded =caesar.encode(message,10); |
| 27 | + |
| 28 | +assertEquals(message,encoded); |
| 29 | + } |
| 30 | + |
| 31 | +@Test |
| 32 | +voidshouldWrapZToAWhenEncodingSingleShift() { |
| 33 | +Stringmessage ="zZ"; |
| 34 | + |
| 35 | +Stringencoded =caesar.encode(message,1); |
| 36 | +Stringexptected ="aA"; |
| 37 | + |
| 38 | +assertEquals(exptected,encoded,"zZ should wrap to aA"); |
| 39 | + } |
| 40 | + |
| 41 | +@Test |
| 42 | +voidshouldWrapAToZWhenDecodingSingleShift() { |
| 43 | +Stringmessage ="aA"; |
| 44 | + |
| 45 | +Stringdecoded =caesar.decode(message,1); |
| 46 | +Stringexpected ="zZ"; |
| 47 | + |
| 48 | +assertEquals(expected,decoded); |
| 49 | + } |
| 50 | + |
| 51 | +@Test |
| 52 | +voidshouldNotWrapWhenEncodingFromYToZ() { |
| 53 | +Stringmessage ="yY"; |
| 54 | + |
| 55 | +Stringencoded =caesar.encode(message,1); |
| 56 | +Stringexpected ="zZ"; |
| 57 | + |
| 58 | +assertEquals(expected,encoded); |
| 59 | + } |
| 60 | + |
| 61 | +@Test |
| 62 | +voidshouldNotWrapWhenDecodingFromBToA() { |
| 63 | +Stringmessage ="bB"; |
| 64 | + |
| 65 | +Stringdecoded =caesar.decode(message,1); |
| 66 | +Stringexpected ="aA"; |
| 67 | + |
| 68 | +assertEquals(expected,decoded); |
| 69 | + } |
| 70 | + |
| 71 | +@Test |
| 72 | +voidshouldContain27CombinationsFromBruteForce() { |
| 73 | +Stringmessage ="message"; |
| 74 | + |
| 75 | +Stringencoded =caesar.encode(message,10); |
| 76 | +String[]combinations =caesar.bruteforce(encoded); |
| 77 | +System.out.println(encoded); |
| 78 | +Stringexpected ="wocckqo"; |
| 79 | + |
| 80 | +assertEquals(27,combinations.length,"Should contain 27 possible decoded combinations"); |
| 81 | +assertEquals(expected,combinations[0],"First combination should contain encoded message"); |
| 82 | +assertEquals(message,combinations[10],"10:th entry should contain original message"); |
| 83 | +assertEquals(expected,combinations[26],"26:th entry should be the same as the 0:th entry, the encoded message"); |
| 84 | + } |
| 85 | +} |