| 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.tools; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | import java.util.Random; |
| 22 | |
| 23 | import junit.framework.TestCase; |
| 24 | |
| 25 | /** |
| 26 | * TestCase for NaturalOrderComparator. |
| 27 | * |
| 28 | * @author Thomas Aglassinger |
| 29 | */ |
| 30 | public class NaturalOrderComparatorTest extends TestCase |
| 31 | { |
| 32 | private static final int RANDOM_SEED = 17; |
| 33 | |
| 34 | private static final int RANDOM_SHUFFLE_COUNT = 1723; |
| 35 | |
| 36 | /** |
| 37 | * Test code coverage. |
| 38 | */ |
| 39 | public void testCodeCoverage() { |
| 40 | assertComparesEqual("001", "1"); |
| 41 | assertComparesEqual("1", "1"); |
| 42 | assertComparesEqual(" 1", "1"); |
| 43 | assertComparesEqual(" 001", "1"); |
| 44 | assertComparesEqual("1", "001"); |
| 45 | assertComparesEqual("1", " 001"); |
| 46 | assertComparesEqual("", ""); |
| 47 | assertComparesEqual(" ", " "); |
| 48 | |
| 49 | assertSorted("a0.jpg", "a001.jpg"); |
| 50 | assertSorted("pic02.jpg", "pic3.jpg"); |
| 51 | assertSorted("0000001", "2"); |
| 52 | assertSorted("2", "22"); |
| 53 | assertSorted("22", "00123"); |
| 54 | assertSorted("0000001", "00123"); |
| 55 | assertSorted("0a1", "0a2"); |
| 56 | assertSorted("0a1", "0aa"); |
| 57 | assertSorted("0", "9"); |
| 58 | assertSorted("p 5", "p 5 s"); |
| 59 | assertSorted("1", "a"); |
| 60 | assertSorted("1", "2"); |
| 61 | assertSorted(" 0 1", "2"); |
| 62 | assertSorted(" 1", "2"); |
| 63 | assertSorted(" 1", " 2"); |
| 64 | assertSorted("", "1"); |
| 65 | } |
| 66 | |
| 67 | public void testLeading0sAndBlanks() { |
| 68 | assertSorted(" 0 3", "2"); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Test that a shuffled array gets sorted properly. |
| 73 | */ |
| 74 | public void testShuffled() { |
| 75 | String[] strings = new String[]{"1-2", "1-02", "1-20", "10-20", |
| 76 | "fred", "jane", "pic01", "pic2", "pic02", "pic02a", "pic3", |
| 77 | "pic4", "pic 4 else", "pic 5", "pic05", "pic 5", |
| 78 | "pic 5 something", "pic 6", "pic 7", "pic100", "pic100a", |
| 79 | "pic120", "pic121", "pic02000", "tom", "x2-g8", "x2-y7", |
| 80 | "x2-y08", "x8-y8"}; |
| 81 | |
| 82 | for (int i = 0; i < RANDOM_SHUFFLE_COUNT; i += 1) { |
| 83 | List orig = Arrays.asList(strings); |
| 84 | List scrambled = Arrays.asList(strings); |
| 85 | Random random = new Random(RANDOM_SEED); |
| 86 | |
| 87 | Collections.shuffle(scrambled, random); |
| 88 | Collections.sort(scrambled, new NaturalOrderComparator()); |
| 89 | assertEquals(orig, scrambled); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public void testU0000() { |
| 94 | assertSorted("1\0a", "1\0b"); |
| 95 | } |
| 96 | |
| 97 | private void assertCompares(String left, String right, int expected) { |
| 98 | assert left != null; |
| 99 | assert right != null; |
| 100 | String[] relationName = new String[]{"lesser than", "equal to", |
| 101 | "greater than"}; |
| 102 | NaturalOrderComparator comparator = new NaturalOrderComparator(); |
| 103 | |
| 104 | int relation = comparator.compare(left, right); |
| 105 | |
| 106 | assertTrue("result of compareTo(\"" + left + ", \"" + right |
| 107 | + "\") must be -1, 0, or 1, but is: " + relation, |
| 108 | (relation >= -1) && (relation <= 1)); |
| 109 | assertEquals("\"" + left + "\" must be " + relationName[expected + 1] + " \"" + right |
| 110 | + "\". comparator result", expected, relation); |
| 111 | } |
| 112 | |
| 113 | private void assertComparesEqual(String left, String right) { |
| 114 | assertCompares(left, right, 0); |
| 115 | assertCompares(right, left, 0); |
| 116 | } |
| 117 | |
| 118 | private void assertSorted(String little, String big) { |
| 119 | assertCompares(little, big, -1); |
| 120 | assertCompares(big, little, 1); |
| 121 | } |
| 122 | } |