aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/sorting/NameComparatorTest.java
blob: 533135d7544a03e2543c71e5ccfeaaa2f76be22d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cgeo.geocaching.sorting;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.geocaching.Geocache;

import android.test.AndroidTestCase;

import java.util.ArrayList;
import java.util.Collections;

public class NameComparatorTest extends AndroidTestCase {

    private static class NamedCache extends Geocache {

        public NamedCache(final String name) {
            this.setName(name);
        }
    }

    private NameComparator comp = new NameComparator();

    public void testLexical() {
        assertSorted(new NamedCache("A"), new NamedCache("Z"));
        assertNotSorted(new NamedCache("Z"), new NamedCache("A"));
    }

    public void testNumericalNamePart() {
        assertSorted(new NamedCache("AHR#2"), new NamedCache("AHR#11"));
        assertSorted(new NamedCache("AHR#7 LP"), new NamedCache("AHR#11 Bonsaibuche"));
        assertSorted(new NamedCache("2"), new NamedCache("11"));
    }

    public void testDuplicateNumericalParts() {
        assertSortedNames("GR8 01-01", "GR8 01-02", "GR8 01-03", "GR8 01-04", "GR8 01-05", "GR8 01-06", "GR8 01-07", "GR8 01-08", "GR8 01-09");
    }

    /**
     * Assert that a given collection of names is already sorted correctly.
     *
     * @param names
     */
    private void assertSortedNames(String... names) {
        ArrayList<Geocache> caches = new ArrayList<Geocache>(names.length);
        for (String name : names) {
            caches.add(new NamedCache(name));
        }
        Collections.sort(caches, comp);
        for (int i = 0; i < caches.size(); i++) {
            assertThat(caches.get(i).getName()).isEqualTo(names[i]);
        }
    }

    public void testNumericalWithSuffix() {
        assertSorted(new NamedCache("abc123def"), new NamedCache("abc123xyz"));
        assertThat((new NamedCache("abc123def456")).getNameForSorting()).isEqualTo("abc000123def000456");
    }

    private void assertSorted(final Geocache cache1, final Geocache cache2) {
        assertThat(comp.compare(cache1, cache2) < 0).isTrue();
    }

    private void assertNotSorted(final Geocache cache1, final Geocache cache2) {
        assertThat(comp.compare(cache1, cache2) > 0).isTrue();
    }
}