Debian Codegraph @

test/uk/me/parabola/imgfmt/app/srt/SortExpandTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2011.
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.srt;
14
15 import java.io.Reader;
16 import java.io.StringReader;
17 import java.text.Collator;
18
19 import uk.me.parabola.mkgmap.srt.SrtTextReader;
20
21 import org.junit.Before;
22 import org.junit.Test;
23
24 import static org.junit.Assert.*;
25
26 /**
27 * Tests for characters that are expanded into two or more sort
28 * positions.
29 */
30 public class SortExpandTest {
31 private Sort sort;
32 private Collator collator;
33
34 @Before
35 public void setUp() throws Exception {
36 Reader r = new StringReader("codepage 1252\n" +
37 "code 01\n" +
38 "code a, A; â, Â < b, B\n" +
39 "code c < d < e <f < g < h < i < j < k < l < m < n < o\n" +
40 "code p < q < r,R < s,S < t,T < u < v < w < x < y < z\n" +
41 "expand ß to s s\n");
42 SrtTextReader srr = new SrtTextReader(r);
43 sort = srr.getSort();
44 collator = sort.getCollator();
45 }
46
47 @Test
48 public void testNormal() {
49 checkOrder("asßst", "astst");
50 checkOrder("asrst", "asßst");
51 }
52
53 @Test
54 public void testExpandSize() {
55 // make sure buffer doesn't overflow when all characters are expanded.
56 assertEquals(0, compareKey("……………………", "……………………"));
57 }
58
59 private int compareKey(String s1, String s2) {
60 SortKey<Object> key1 = sort.createSortKey(null, s1);
61 SortKey<Object> key2 = sort.createSortKey(null, s2);
62 return key1.compareTo(key2);
63 }
64
65 @Test
66 public void testGreaterThanInExpansion() {
67 checkOrder("aßzaa", "astb");
68 }
69
70 @Test
71 public void testLessThanInExpansion() {
72 checkOrder("asrb", "aßaaa");
73 }
74
75 /**
76 * Check and assert that the second string is greater than the first.
77 * @param s First string.
78 * @param s1 Second string.
79 */
80 private void checkOrder(String s, String s1) {
81 SortKey<Object> k1 = sort.createSortKey(null, s);
82 SortKey<Object> k2 = sort.createSortKey(null, s1);
83
84 assertEquals(1, k2.compareTo(k1));
85 assertEquals(-1, k1.compareTo(k2));
86
87 assertEquals(-1, collator.compare(s, s1));
88 assertEquals(1, collator.compare(s1, s));
89 }
90 }