Debian Codegraph @

test/uk/me/parabola/imgfmt/app/labelenc/CodeFunctionsTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
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 * Quick check of the ascii 6 bit format conversion.
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 // This was determined from the behaviour of the existing code, and not
37 // from first principles.
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 * Transliteration when going to ascii in format 6. This was originally
54 * the only place where transliteration was available.
55 */
56 @Test
57 public void testTransliterate6() {
58 CodeFunctions functions = CodeFunctions.createEncoderForLBL(6, 0);
59
60 CharacterEncoder encoder = functions.getEncoder();
61 // Twülpstedt contains u + "COMBINING DIAERESIS" (0x75 + 0x308)
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 * Transliteration when going to ascii in format 6. This was originally
77 * the only place where transliteration was available.
78 */
79 @Test
80 public void testTransliterateLatin() {
81 CodeFunctions functions = CodeFunctions.createEncoderForLBL("latin1");
82
83 CharacterEncoder encoder = functions.getEncoder();
84 // Twülpstedt contains u + "COMBINING DIAERESIS" (0x75 + 0x308)
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 // Twülpstedt now contains LATIN SMALL LETTER U WITH DIAERESIS (u+00fc)
97 assertEquals("transliterated text", "Körnerstraße, Twülpstedt, Velkomezerícská, Skólavörðustigur", result);
98 }
99
100 /**
101 * Backward compatibility test.
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 // This encoder appends a null byte.
118 assertEquals("length of encoded text", 256, text.getLength());
119
120 for (int i = 1; i < 256; i++) {
121 // The following characters do not display on my GPS. This covers
122 // the region where windows-1252 differs from iso 8859 so we don't
123 // really know which it is meant to be.
124 if (i >= 0x80 && i <= 0xbf)
125 continue;
126 assertEquals("character", i, text.getCtext()[i-1] & 0xff);
127 }
128 }
129
130
131 }