| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.imgfmt.app.labelenc; |
| 14 |
|
| 15 |
import java.util.Arrays; |
| 16 |
|
| 17 |
import org.junit.Test; |
| 18 |
|
| 19 |
import static org.junit.Assert.*; |
| 20 |
|
| 21 |
public class CodeFunctionsTest { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
@Test |
| 26 |
public void testFormat6() { |
| 27 |
CodeFunctions functions = CodeFunctions.createEncoderForLBL(6, 0); |
| 28 |
assertEquals("code page", 0, functions.getCodepage()); |
| 29 |
assertEquals("encoding type", 6, functions.getEncodingType()); |
| 30 |
CharacterEncoder enc = functions.getEncoder(); |
| 31 |
|
| 32 |
EncodedText etext = enc.encodeText("hello world"); |
| 33 |
byte[] ctext = etext.getCtext(); |
| 34 |
int len = etext.getLength(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
assertEquals("encoded length", 9, len); |
| 39 |
byte[] foo = { |
| 40 |
0x20, 0x53, 0xc, 0x3c, 0x5, 0xffffffcf, 0x48, 0xffffffc1, 0x3f, |
| 41 |
}; |
| 42 |
assertArrayEquals("encoded text", foo, Arrays.copyOf(ctext, len)); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void testAscii() { |
| 47 |
CodeFunctions f = CodeFunctions.createEncoderForLBL("ascii"); |
| 48 |
assertEquals("code page", 0, f.getCodepage()); |
| 49 |
assertEquals("encoding type", 6, f.getEncodingType()); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
@Test |
| 57 |
public void testTransliterate6() { |
| 58 |
CodeFunctions functions = CodeFunctions.createEncoderForLBL(6, 0); |
| 59 |
|
| 60 |
CharacterEncoder encoder = functions.getEncoder(); |
| 61 |
) |
| 62 |
EncodedText text = encoder.encodeText("Körnerstraße, Twülpstedt, Velkomezeříčská, Skólavörðustigur"); |
| 63 |
|
| 64 |
CharacterDecoder decoder = functions.getDecoder(); |
| 65 |
byte[] ctext = text.getCtext(); |
| 66 |
boolean finished = false; |
| 67 |
int i = 0; |
| 68 |
while (!finished) { |
| 69 |
finished = decoder.addByte(ctext[i++]); |
| 70 |
} |
| 71 |
String result = decoder.getText().getText(); |
| 72 |
assertEquals("transliterated text", "KORNERSTRASSE, TWULPSTEDT, VELKOMEZERICSKA, SKOLAVORDUSTIGUR", result); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
@Test |
| 80 |
public void testTransliterateLatin() { |
| 81 |
CodeFunctions functions = CodeFunctions.createEncoderForLBL("latin1"); |
| 82 |
|
| 83 |
CharacterEncoder encoder = functions.getEncoder(); |
| 84 |
) |
| 85 |
EncodedText text = encoder.encodeText("Körnerstraße, Twülpstedt, Velkomezeříčská, Skólavörðustigur"); |
| 86 |
|
| 87 |
CharacterDecoder decoder = functions.getDecoder(); |
| 88 |
byte[] ctext = text.getCtext(); |
| 89 |
boolean finished = false; |
| 90 |
int i = 0; |
| 91 |
while (!finished) { |
| 92 |
finished = decoder.addByte(ctext[i++]); |
| 93 |
} |
| 94 |
|
| 95 |
String result = decoder.getText().getText(); |
| 96 |
) |
| 97 |
assertEquals("transliterated text", "Körnerstraße, Twülpstedt, Velkomezerícská, Skólavörðustigur", result); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@Test |
| 104 |
public void testLatin1() { |
| 105 |
CodeFunctions functions = CodeFunctions.createEncoderForLBL("latin1"); |
| 106 |
assertEquals("code page", 1252, functions.getCodepage()); |
| 107 |
assertEquals("encoding type", 9, functions.getEncodingType()); |
| 108 |
|
| 109 |
StringBuilder sb = new StringBuilder(); |
| 110 |
for (char c = 1; c < 256; c++) { |
| 111 |
sb.append(c); |
| 112 |
} |
| 113 |
|
| 114 |
CharacterEncoder encoder = functions.getEncoder(); |
| 115 |
EncodedText text = encoder.encodeText(sb.toString()); |
| 116 |
|
| 117 |
|
| 118 |
assertEquals("length of encoded text", 256, text.getLength()); |
| 119 |
|
| 120 |
for (int i = 1; i < 256; i++) { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
if (i >= 0x80 && i <= 0xbf) |
| 125 |
continue; |
| 126 |
assertEquals("character", i, text.getCtext()[i-1] & 0xff); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
} |