1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
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 as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.comic; |
17 | |
18 | import java.util.ArrayList; |
19 | import java.util.Collections; |
20 | import java.util.List; |
21 | |
22 | import net.sf.jomic.tools.TestTools; |
23 | |
24 | import junit.framework.TestCase; |
25 | |
26 | /** |
27 | * TestCase for ComicSheetindexOverlapComparator. |
28 | * |
29 | * @see net.sf.jomic.comic.ComicSheetindexOverlapComparator |
30 | * @author Thomas Aglassinger |
31 | */ |
32 | public class ComicSheetindexOverlapComparatorTest extends TestCase |
33 | { |
34 | |
35 | private static final int NONE = ComicSheet.NO_IMAGE; |
36 | |
37 | private ComicSheetindexOverlapComparator comparator; |
38 | |
39 | /* |
40 | * (non-Javadoc) |
41 | * @see junit.framework.TestCase#setUp() |
42 | */ |
43 | protected void setUp() |
44 | throws Exception { |
45 | super.setUp(); |
46 | TestTools.instance(); |
47 | comparator = new ComicSheetindexOverlapComparator(); |
48 | } |
49 | |
50 | public void testFound() { |
51 | assertComicSheetBinarySearch(0, new int[]{0, NONE}, 0); |
52 | assertComicSheetBinarySearch(0, new int[]{0, 1}, 1); |
53 | assertComicSheetBinarySearch(0, new int[]{0, 1}, 0); |
54 | assertComicSheetBinarySearch(1, new int[]{0, 1, 2, 3}, 2); |
55 | assertComicSheetBinarySearch(2, new int[]{0, 1, 2, NONE, 3, 4, 5, NONE}, 3); |
56 | assertComicSheetBinarySearch(2, new int[]{0, 1, 2, NONE, 3, 4, 5, NONE}, 4); |
57 | assertComicSheetBinarySearch( |
58 | 0, |
59 | new int[]{0, 1, 2, NONE, 3, 4, 5, NONE, 6, NONE, 7, NONE, 8, NONE, 9, NONE, 10, NONE}, |
60 | 1); |
61 | } |
62 | |
63 | public void testNotFound() { |
64 | assertComicSheetBinarySearch(-2, new int[]{0, NONE}, 1); |
65 | assertComicSheetBinarySearch(-2, new int[]{0, 1}, 2); |
66 | assertComicSheetBinarySearch(-3, new int[]{0, 1, 2, 3}, 4); |
67 | assertComicSheetBinarySearch(-5, new int[]{0, 1, 2, NONE, 3, 4, 5, NONE}, 6); |
68 | assertComicSheetBinarySearch( |
69 | -10, |
70 | new int[]{0, 1, 2, NONE, 3, 4, 5, NONE, 6, NONE, 7, NONE, 8, NONE, 9, NONE, 10, NONE}, |
71 | 11); |
72 | } |
73 | |
74 | /* |
75 | * (non-Javadoc) |
76 | * @see junit.framework.TestCase#tearDown() |
77 | */ |
78 | protected void tearDown() |
79 | throws Exception { |
80 | comparator = null; |
81 | super.tearDown(); |
82 | } |
83 | |
84 | private void assertComicSheetBinarySearch(int expected, int[] indexes, int leftIndex) { |
85 | assertComicSheetBinarySearch(expected, indexes, leftIndex, ComicSheet.NO_IMAGE); |
86 | } |
87 | |
88 | private void assertComicSheetBinarySearch(int expected, int[] indexes, int leftIndex, int rightIndex) { |
89 | List sheets = createComicSheetList(indexes); |
90 | ComicSheet targetSheet = new ComicSheet(Integer.MAX_VALUE, leftIndex, rightIndex); |
91 | int actual = Collections.binarySearch(sheets, targetSheet, comparator); |
92 | |
93 | assertEquals(expected, actual); |
94 | } |
95 | |
96 | private List createComicSheetList(int[] indexes) { |
97 | assert indexes.length % 2 == 0 : "indexes.length must be even, but is " + indexes.length; |
98 | List result = new ArrayList(); |
99 | int lastIndex = -1; |
100 | int page = 0; |
101 | |
102 | for (int i = 0; i < indexes.length; i += 2) { |
103 | int left = indexes[i]; |
104 | int right = indexes[i + 1]; |
105 | |
106 | assert left == lastIndex + 1 : "left at " + i + " must be " + (lastIndex + 1) + " but is " + left; |
107 | if (right == NONE) { |
108 | lastIndex = left; |
109 | } else { |
110 | assert right == left + 1 : "right at " + (i + 1) + " must be " + (left + 1) + " but is " + right; |
111 | lastIndex = right; |
112 | } |
113 | |
114 | ComicSheet sheet = new ComicSheet(page, left, right); |
115 | |
116 | result.add(sheet); |
117 | page += 1; |
118 | } |
119 | return result; |
120 | } |
121 | } |
122 | |