Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- public class Lucifere20171208Test extends TestCase {
- public void testKnownEncodings() {
- assertFirstEncodesToSecondAndBack("", "");
- assertFirstEncodesToSecondAndBack("a", "0");
- assertFirstEncodesToSecondAndBack("b", "01");
- assertFirstEncodesToSecondAndBack("c", "11");
- assertFirstEncodesToSecondAndBack("aa", "00");
- assertFirstEncodesToSecondAndBack("bb", "0101");
- assertFirstEncodesToSecondAndBack("cc", "1111");
- assertFirstEncodesToSecondAndBack("ab", "001");
- assertFirstEncodesToSecondAndBack("ac", "011");
- assertFirstEncodesToSecondAndBack("ba", "010");
- assertFirstEncodesToSecondAndBack("bc", "0111");
- assertFirstEncodesToSecondAndBack("ca", "110");
- assertFirstEncodesToSecondAndBack("cb", "1101");
- }
- public void testGeneratedStrings() {
- final StringBuilder inputValue = new StringBuilder();
- while (inputValue.length() <= 13) {
- boolean carry = true;
- for (int idx = inputValue.length() - 1; idx >= 0 && carry; --idx) {
- final char nextChar = (char)(inputValue.charAt(idx) + 1);
- if (nextChar <= 'c') {
- inputValue.setCharAt(idx, nextChar);
- carry = false;
- } else {
- inputValue.setCharAt(idx, 'a');
- carry = true;
- }
- }
- if (carry) {
- inputValue.insert(0, "a");
- }
- final String plainText = inputValue.toString();
- //System.out.println(plainText);
- final String encodedValue = encode(plainText);
- assertEquals("Plain text <" + plainText + ">, encoded as <" + encodedValue + ">;", plainText, decode(encodedValue));
- }
- }
- public void testBadEncodedString() {
- try {
- decode("1");
- fail("Expected IllegalStateException.");
- } catch (final IllegalStateException ex) {
- assertEquals("Encoded string started with a 'wayward' '1' char <1>.", ex.getMessage());
- }
- }
- public void testBadCharacterInEncodedString() {
- try {
- decode("020");
- fail("Expected IllegalStateException.");
- } catch (final IllegalStateException ex) {
- assertEquals("Not expecting character <2> in value to decode <020>. Only '0' and '1' are valid.", ex.getMessage());
- }
- }
- private static void assertFirstEncodesToSecondAndBack(final String plainText, final String encodedString) {
- assertEquals("Plain text string <" + plainText + "> encoding;", encodedString, encode(plainText));
- assertEquals("Encoded text string <" + encodedString + "> decoding;", plainText, decode(encodedString));
- }
- private static String encode(final String plainText) {
- final StringBuilder encodedValue = new StringBuilder();
- for (final char chr : plainText.toCharArray()) {
- switch (chr) {
- case 'a': encodedValue.append("0"); break;
- case 'b': encodedValue.append("01"); break;
- case 'c': encodedValue.append("11"); break;
- }
- }
- return encodedValue.toString();
- }
- private static String decode(String encodedString) {
- final StringBuilder decodedValue = new StringBuilder();
- boolean lastCharWasOne = false;
- for (final char chr : new StringBuilder(encodedString).reverse().toString().toCharArray()) {
- switch (chr) {
- case '0':
- if (lastCharWasOne) {
- decodedValue.append('b');
- lastCharWasOne = false;
- } else {
- decodedValue.append('a');
- }
- break;
- case '1':
- if (lastCharWasOne) {
- decodedValue.append('c');
- lastCharWasOne = false;
- } else {
- lastCharWasOne = true;
- }
- break;
- default:
- throw new IllegalStateException("Not expecting character <" + chr + "> in value to decode <" + encodedString + ">. Only '0' and '1' are valid.");
- }
- }
- if (lastCharWasOne) {
- throw new IllegalStateException("Encoded string started with a 'wayward' '1' char <" + encodedString + ">.");
- }
- return decodedValue.reverse().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement