001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.util;
017:
018: // J2SE dependencies
019: import java.util.Random;
020: import java.util.HashSet;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.ListIterator;
024:
025: // JUnit dependencies
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: /**
031: * Tests {@link KeySortedList}.
032: *
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/util/KeySortedListTest.java $
034: * @version $Id: KeySortedListTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
035: * @author Martin Desruisseaux
036: */
037: public final class KeySortedListTest extends TestCase {
038: /**
039: * Run the suit from the command line.
040: */
041: public static void main(final String[] args) {
042: org.geotools.util.logging.Logging.GEOTOOLS
043: .forceMonolineConsoleOutput();
044: junit.textui.TestRunner.run(suite());
045: }
046:
047: /**
048: * Returns the test suite.
049: */
050: public static Test suite() {
051: return new TestSuite(KeySortedListTest.class);
052: }
053:
054: /**
055: * Constructs a test case with the given name.
056: */
057: public KeySortedListTest(final String name) {
058: super (name);
059: }
060:
061: /**
062: * Inserts random floating point numbers into the list. The key is the integer part of the
063: * floating point number. This means that the number should be sorted in such a way that
064: * their integer part are in increasing order, while the fractional part remains in random
065: * order.
066: */
067: public void testAdd() {
068: final Random random = new Random(6969483179756527012L);
069: final KeySortedList list = new KeySortedList();
070: final Collection check = new ArrayList();
071: final int maxElements = 1000;
072: for (int i = 0; i < maxElements; i++) {
073: final double x = random.nextDouble() * (maxElements / 10);
074: final Integer key = new Integer((int) x);
075: final Double value = new Double(x);
076: list.add(key, value);
077: check.add(value);
078: }
079: /*
080: * Checks the content.
081: */
082: assertEquals(maxElements, check.size());
083: assertEquals(maxElements, list.size());
084: assertEquals(new HashSet(check), new HashSet(list));
085: /*
086: * Checks the iteration.
087: */
088: int count = 0, lastKey = 0;
089: for (final ListIterator it = list.listIterator(); it.hasNext(); count++) {
090: assertEquals(count, it.nextIndex());
091: final Double element = (Double) it.next();
092: assertEquals(count, it.previousIndex());
093: final double value = element.doubleValue();
094: final int key = (int) value;
095: assertTrue(key >= lastKey);
096: lastKey = key;
097: assertSame(element, list.get(count));
098: }
099: assertEquals(maxElements, count);
100: /*
101: * Checks the iteration from a middle point.
102: */
103: final Integer midKey = new Integer(maxElements / 10 / 2);
104: final KeySortedList head = list.headList(midKey);
105: final KeySortedList tail = list.tailList(midKey);
106: final Collection rebuild = new ArrayList(head);
107: rebuild.addAll(tail);
108: assertEquals(list.size(), head.size() + tail.size());
109: assertEquals(list, rebuild);
110: assertSame(list.listIterator(midKey).next(), tail
111: .listIterator().next());
112: }
113: }
|