You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2012/09/16 20:08:32 UTC
svn commit: r1385335 [3/4] - in
/commons/proper/functor/branches/generators-FUNCTOR-14: ./ src/changes/
src/main/java/org/apache/commons/functor/generator/range/ src/site/xdoc/
src/test/java/org/apache/commons/functor/generator/range/
Added: commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestFloatRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestFloatRange.java?rev=1385335&view=auto
==============================================================================
--- commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestFloatRange.java (added)
+++ commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestFloatRange.java Sun Sep 16 18:08:31 2012
@@ -0,0 +1,773 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0f
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0f
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.functor.generator.range;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for float range.
+ *
+ * @since 1.0
+ * @version $Revision: $ $Date: $
+ */
+public class TestFloatRange extends BaseFunctorTest {
+
+ // A base range with all longs between -6 and 6
+ private final List<Float> fullRange = Collections.unmodifiableList(Arrays
+ .asList(-6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f,
+ 3.0f, 4.0f, 5.0f, 6.0f));
+
+ // Attributes
+ // ------------------------------------------------------------------------
+ private FloatRange ascFloatRange = null;
+ private FloatRange descFloatRange = null;
+ private Collection<Float> expectedAsc = null;
+ private Collection<Float> expectedDesc = null;
+
+ // Test set up
+ // ------------------------------------------------------------------------
+ @Before
+ public void setUp() {
+ ascFloatRange = Ranges.floatRange(0.0f, 10.0f);
+ descFloatRange = Ranges.floatRange(10.0f, 0.0f);
+ expectedAsc = Arrays.asList(0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
+ 7.0f, 8.0f, 9.0f);
+ expectedDesc = Arrays.asList(10.0f, 9.0f, 8.0f, 7.0f, 6.0f, 5.0f, 4.0f,
+ 3.0f, 2.0f, 1.0f);
+ }
+
+ @After
+ public void tearDown() {
+ ascFloatRange = null;
+ descFloatRange = null;
+ }
+
+ @Override
+ protected Object makeFunctor()
+ throws Exception {
+ return Ranges.floatRange(10, 20);
+ }
+
+ // Generator tests
+ // ---------------------------------------------------------------
+
+ @Test
+ public void testGenerateListExample() {
+ // generates a collection of Floats from 0 (inclusive) to 10 (exclusive)
+ {
+ List<? super Float> list = (List<? super Float>) (Ranges.floatRange(
+ 0,
+ 10)
+ .to(new ArrayList<Float>()));
+ for (int i = 0; i < 10; i++) {
+ assertEquals(new Float(i), list.get(i));
+ }
+ }
+
+ // generates a collection of Floats from 10 (inclusive) to 0 (exclusive)
+ {
+ List<? super Float> list = (List<? super Float>) (Ranges.floatRange(
+ 10,
+ 0)
+ .to(new ArrayList<Float>()));
+ for (int i = 10; i > 0; i--) {
+ assertEquals(new Float(i), list.get(10 - i));
+ }
+ }
+ }
+
+ @Test
+ public void testStepChecking() {
+ {
+ Ranges.floatRange(2, 2, 0); // step of 0 is ok when range is empty
+ }
+ {
+ Ranges.floatRange(2, 2, 1); // positive step is ok when range is
+ // empty
+ }
+ {
+ Ranges.floatRange(2, 2, -1); // negative step is ok when range is
+ // empty
+ }
+ {
+ Ranges.floatRange(0, 1, 10); // big steps are ok
+ }
+ {
+ Ranges.floatRange(1, 0, -10); // big steps are ok
+ }
+ try {
+ Ranges.floatRange(0, 1, 0);
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ Ranges.floatRange(0, 1, -1);
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ Ranges.floatRange(0, -1, 1);
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testObjectConstructor() {
+ FloatRange range = Ranges.floatRange(new Float(0), new Float(5));
+ assertEquals("[0.0, 1.0, 2.0, 3.0, 4.0]", range.toCollection()
+ .toString());
+ range = Ranges.floatRange(new Float(0), new Float(5), new Float(1));
+ assertEquals("[0.0, 1.0, 2.0, 3.0, 4.0]", range.toCollection()
+ .toString());
+ }
+
+ @Test
+ public void testReverseStep() {
+ FloatRange range = Ranges.floatRange(10, 0, -2);
+ assertEquals("[10.0, 8.0, 6.0, 4.0, 2.0]", range.toCollection()
+ .toString());
+ assertEquals("[10.0, 8.0, 6.0, 4.0, 2.0]", range.toCollection()
+ .toString());
+ }
+
+ @Test
+ public void testStep() {
+ FloatRange range = Ranges.floatRange(0, 10, 2);
+ assertEquals("[0.0, 2.0, 4.0, 6.0, 8.0]", range.toCollection()
+ .toString());
+ assertEquals("[0.0, 2.0, 4.0, 6.0, 8.0]", range.toCollection()
+ .toString());
+ }
+
+ @Test
+ public void testForwardRange() {
+ FloatRange range = Ranges.floatRange(0, 5);
+ assertEquals("[0.0, 1.0, 2.0, 3.0, 4.0]", range.toCollection()
+ .toString());
+ assertEquals("[0.0, 1.0, 2.0, 3.0, 4.0]", range.toCollection()
+ .toString());
+ }
+
+ @Test
+ public void testReverseRange() {
+ FloatRange range = Ranges.floatRange(5, 0);
+ assertEquals("[5.0, 4.0, 3.0, 2.0, 1.0]", range.toCollection()
+ .toString());
+ assertEquals("[5.0, 4.0, 3.0, 2.0, 1.0]", range.toCollection()
+ .toString());
+ }
+
+ @Test
+ public void testClosedClosedAscending() {
+ // [-5.0f, 5.0f], 3.0f = -5.0f, -2.0f, 1.0f, 4.0f
+ FloatRange range = Ranges.floatRange(-5.0f, BoundType.CLOSED, 5.0f,
+ BoundType.CLOSED, 3.0f);
+ // [-5.0f, 5.0f], 3.0f = -5.0f, -2.0f, 1.0f, 4.0f
+ List<Float> expected = Arrays.asList(-5.0f, -2.0f, 1.0f, 4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedAscending() {
+ // (-5.0f, 5.0f], 3.0f = -2.0f, 1.0f, 4.0f
+ FloatRange range = Ranges.floatRange(-5.0f, BoundType.OPEN, 5.0f,
+ BoundType.CLOSED, 3.0f);
+ // (-5.0f, 5.0f], 3.0f = -2.0f, 1.0f, 4.0f
+ List<Float> expected = Arrays.asList(-2.0f, 1.0f, 4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenAscending() {
+ // [-5.0f, 5.0f), 3.0f = -5.0f, -2.0f, 1.0f, 4.0f
+ FloatRange range = Ranges.floatRange(-5.0f, BoundType.CLOSED, 5.0f,
+ BoundType.OPEN, 3.0f);
+ // (-5.0f, 5.0f], 3.0f = -5.0f, -2.0f, 1.0f, 4.0f
+ List<Float> expected = Arrays.asList(-5.0f, -2.0f, 1.0f, 4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenAscending() {
+ // (-5.0f, 5.0f), 3.0f = -2.0f, 1.0f, 4.0f
+ FloatRange range = Ranges.floatRange(-5.0f, BoundType.OPEN, 5.0f,
+ BoundType.OPEN, 3.0f);
+ // (-5.0f, 5.0f), 3.0f = -2.0f, 1.0f, 4.0f
+ List<Float> expected = Arrays.asList(-2.0f, 1.0f, 4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepAscending() {
+ // (-2.0f, 2.0f], 1.0f = -1.0f, 0.0f, 1.0f, 2.0f
+ FloatRange range = Ranges.floatRange(-2.0f, BoundType.OPEN, 2.0f,
+ BoundType.CLOSED, 1.0f);
+ // (-2.0f, 2.0f], 1.0f = -1.0f, 0.0f, 1.0f, 2.0f
+ List<Float> expected = Arrays.asList(-1.0f, 0.0f, 1.0f, 2.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedClosedDescending() {
+ // [5.0f, -5.0f], -3.0f = 5.0f, 2.0f, -1.0f, -4.0f
+ FloatRange range = Ranges.floatRange(5.0f, BoundType.CLOSED, -5.0f,
+ BoundType.CLOSED, -3.0f);
+ // [5.0f, -5.0f], -3.0f = 5.0f, 2.0f, -1.0f, -4.0f
+ List<Float> expected = Arrays.asList(5.0f, 2.0f, -1.0f, -4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedDescending() {
+ // (5.0f, -5.0f], -3.0f = 2.0f, -1.0f, -4.0f
+ FloatRange range = Ranges.floatRange(5.0f, BoundType.OPEN, -5.0f,
+ BoundType.CLOSED, -3.0f);
+ // (5.0f, -5.0f], -3.0f = 2.0f, -1.0f, -4.0f
+ List<Float> expected = Arrays.asList(2.0f, -1.0f, -4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenDescending() {
+ // [5.0f, -5.0f), -3.0f = 5.0f, 2.0f, -1.0f, -4.0f
+ FloatRange range = Ranges.floatRange(5.0f, BoundType.CLOSED, -5.0f,
+ BoundType.OPEN, -3.0f);
+ // [5.0f, -5.0f), -3.0f = 5.0f, 2.0f, -1.0f, -4.0f
+ List<Float> expected = Arrays.asList(5.0f, 2.0f, -1.0f, -4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenDescending() {
+ // (5.0f, -5.0f), -3.0f = 2.0f, -1.0f, -4.0f
+ FloatRange range = Ranges.floatRange(5.0f, BoundType.OPEN, -5.0f,
+ BoundType.OPEN, -3.0f);
+ // (5.0f, -5.0f), -3.0f = 2.0f, -1.0f, -4.0f
+ List<Float> expected = Arrays.asList(2.0f, -1.0f, -4.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepDescending() {
+ // [2.0f, -2.0f), -1.0f = 2.0f, 1.0f, 0.0f, -1.0f
+ FloatRange range = Ranges.floatRange(2.0f, BoundType.CLOSED, -2.0f,
+ BoundType.OPEN, -1.0f);
+ // [2.0f, -2.0f), -1.0f = 2.0f, 1.0f, 0.0f, -1.0f
+ List<Float> expected = Arrays.asList(2.0f, 1.0f, 0.0f, -1.0f);
+ Collection<Float> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testAscending() {
+ final List<Float> list = new ArrayList<Float>();
+ ascFloatRange.run(new UnaryProcedure<Float>() {
+
+ public void run(Float obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedAsc.containsAll(list));
+ }
+
+ @Test
+ public void testDescending() {
+ final List<Float> list = new ArrayList<Float>();
+ descFloatRange.run(new UnaryProcedure<Float>() {
+
+ public void run(Float obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedDesc.containsAll(list));
+ }
+
+ @Test
+ public void testToCollection() {
+ Collection<Float> ascCol = ascFloatRange.toCollection();
+ assertEquals("Different collections", expectedAsc, ascCol);
+ Collection<Float> descCol = descFloatRange.toCollection();
+ assertEquals("Different collections", expectedDesc, descCol);
+ }
+
+ @Test
+ public void testTransformedGenerator() {
+ Float expected = 45.0f;
+ Float total = ascFloatRange
+ .to(new UnaryFunction<Generator<? extends Float>, Float>() {
+
+ public Float evaluate(Generator<? extends Float> obj) {
+ Float total = 0.0f;
+ for (Object element : obj.toCollection()) {
+ total += (Float) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ expected = 55.0f;
+ total = descFloatRange
+ .to(new UnaryFunction<Generator<? extends Float>, Float>() {
+
+ public Float evaluate(Generator<? extends Float> obj) {
+ Float total = 0.0f;
+ for (Object element : obj.toCollection()) {
+ total += (Float) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ }
+
+ // Range tests
+ // ---------------------------------------------------------------
+
+ @Test
+ public void testEmptyRanges() {
+ FloatRange empty1 = Ranges.floatRange(-2, BoundType.OPEN, -1,
+ BoundType.OPEN, 2);
+ assertTrue("The range was expected to be empty.", empty1.isEmpty());
+ FloatRange empty2 = Ranges.floatRange(2, BoundType.OPEN, 0,
+ BoundType.OPEN, -2);
+ assertTrue("The range was expected to be empty.", empty2.isEmpty());
+ FloatRange empty3 = Ranges.floatRange(0, BoundType.OPEN, 1,
+ BoundType.CLOSED, 2);
+ assertTrue("The range was expected to be empty.", empty3.isEmpty());
+ FloatRange empty4 = Ranges.floatRange(-3, BoundType.OPEN, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty4.isEmpty());
+ FloatRange empty5 = Ranges.floatRange(-3, BoundType.CLOSED, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty5.isEmpty());
+ FloatRange empty6 = Ranges.floatRange(1, BoundType.OPEN, 0,
+ BoundType.CLOSED, -2);
+ assertTrue("The range was expected to be empty.", empty6.isEmpty());
+ FloatRange notEmpty1 = Ranges.floatRange(-3, BoundType.CLOSED, -3,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty1.isEmpty());
+ FloatRange notEmpty2 = Ranges.floatRange(-3, BoundType.OPEN, -2,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty2.isEmpty());
+ FloatRange notEmpty3 = Ranges.floatRange(2, BoundType.OPEN, 1,
+ BoundType.CLOSED, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty3.isEmpty());
+ FloatRange notEmpty4 = Ranges.floatRange(2, BoundType.CLOSED, 1,
+ BoundType.OPEN, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty4.isEmpty());
+ FloatRange notEmpty5 = Ranges.floatRange(1, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty5.isEmpty());
+ }
+
+ @Test
+ public void testBoundaries() {
+ FloatRange range = Ranges.floatRange(0.0f, 10.0f);
+ assertEquals(new Endpoint<Comparable<?>>(0.0f, BoundType.CLOSED),
+ range.getLeftEndpoint());
+ assertEquals(new Endpoint<Comparable<?>>(10.0f, BoundType.OPEN),
+ range.getRightEndpoint());
+ }
+
+ @Test
+ public void testClosedClosedAscendingContains() {
+ // [-5, 5], 3 = -5, -2, 1, 4
+ FloatRange range = Ranges.floatRange(-5, BoundType.CLOSED, 5,
+ BoundType.CLOSED, 3);
+ // [-5, 5], 3 = -5, -2, 1, 4
+ List<Float> arr = Arrays.asList(-5.0f, -2.0f, 1.0f, 4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedAscendingContains() {
+ // (-5, 5], 3 = -2, 1, 4
+ FloatRange range = Ranges.floatRange(-5, BoundType.OPEN, 5,
+ BoundType.CLOSED, 3);
+ // (-5, 5], 3 = -2, 1, 4
+ List<Float> arr = Arrays.asList(-2.0f, 1.0f, 4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenAscendingContains() {
+ // [-5, 5), 3 = -5, -2, 1, 4
+ FloatRange range = Ranges.floatRange(-5, BoundType.CLOSED, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5], 3 = -5, -2, 1, 4
+ List<Float> arr = Arrays.asList(-5.0f, -2.0f, 1.0f, 4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenAscendingContains() {
+ // (-5, 5), 3 = -2, 1, 4
+ FloatRange range = Ranges.floatRange(-5, BoundType.OPEN, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5), 3 = -2, 1, 4
+ List<Float> arr = Arrays.asList(-2.0f, 1.0f, 4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepAscending() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ FloatRange ascendingRange = Ranges.floatRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ // (-2, 2], 1 = -1, 0, 1, 2
+ List<Float> arr = Arrays.asList(-1.0f, 0.0f, 1.0f, 2.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + ascendingRange + "]",
+ ascendingRange.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + ascendingRange +
+ "]",
+ ascendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedClosedDescendingContains() {
+ // [5, -5], -3 = 5, 2, -1, -4
+ FloatRange range = Ranges.floatRange(5, BoundType.CLOSED, -5,
+ BoundType.CLOSED, -3);
+ // [5, -5], -3 = 5, 2, -1, -4
+ List<Float> arr = Arrays.asList(5.0f, 2.0f, -1.0f, -4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedDescendingContains() {
+ // (5, -5], -3 = 2, -1, -4
+ FloatRange range = Ranges.floatRange(5, BoundType.OPEN, -5,
+ BoundType.CLOSED, -3);
+ // (5, -5], -3 = 2, -1, -4
+ List<Float> arr = Arrays.asList(2.0f, -1.0f, -4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenDescendingContains() {
+ // [5, -5), -3 = 5, 2, -1, -4
+ FloatRange range = Ranges.floatRange(5, BoundType.CLOSED, -5,
+ BoundType.OPEN, -3);
+ // [5, -5), -3 = 5, 2, -1, -4
+ List<Float> arr = Arrays.asList(5.0f, 2.0f, -1.0f, -4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenDescendingContains() {
+ // (5, -5), -3 = 2, -1, -4
+ FloatRange range = Ranges.floatRange(5, BoundType.OPEN, -5,
+ BoundType.OPEN, -3);
+ // (5, -5), -3 = 2, -1, -4
+ List<Float> arr = Arrays.asList(2.0f, -1.0f, -4.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepDescending() {
+ // [2, -2), -1 = 2, 1, 0, -1
+ FloatRange descendingRange = Ranges.floatRange(2, BoundType.CLOSED, -2,
+ BoundType.OPEN, -1);
+ // [2, -2), -1 = 2, 1, 0, -1
+ List<Float> arr = Arrays.asList(2.0f, 1.0f, 0.0f, -1.0f);
+ for (Float element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ List<Float> elementsNotPresent = new ArrayList<Float>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Float element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsNullOrEmpty() {
+ FloatRange range = Ranges.floatRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertFalse(range.contains(null));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testContainsAll() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ FloatRange range = Ranges.floatRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ List<Float> list = Arrays.asList(-1.0f, 0.0f, 1.0f, 2.0f);
+ assertTrue("Range [" + range +
+ "] was expected to contain all elements from list [" + list +
+ "]", range.containsAll(list));
+ List<Float> listWithExtraElements = Arrays.asList(2.0f, -1.0f, 0.0f,
+ 1.0f, 2.0f, 3.0f);
+ assertFalse("Range [" + range + "] has more elements than expected",
+ range.containsAll(listWithExtraElements));
+ assertFalse(range.containsAll(null));
+ assertFalse(range.containsAll(Collections.EMPTY_LIST));
+ }
+
+ @Test
+ public void testEquals()
+ throws Exception {
+ // equals basic properties
+ FloatRange range = Ranges.floatRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertEquals("equals must be reflexive", range, range);
+ assertEquals("hashCode must be reflexive", range.hashCode(),
+ range.hashCode());
+ assertTrue(!range.equals(null)); // should be able to compare to null
+
+ Object range2 = Ranges.floatRange(-2, BoundType.CLOSED, 2, BoundType.OPEN,
+ 1);
+ if (range.equals(range2)) {
+ assertEquals("equals implies hash equals", range.hashCode(),
+ range2.hashCode());
+ assertEquals("equals must be symmetric", range2, range);
+ } else {
+ assertTrue("equals must be symmetric", !range2.equals(range));
+ }
+
+ // Changing attributes
+ Object range3 = Ranges.floatRange(-1, BoundType.CLOSED, 2, BoundType.OPEN,
+ 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range3));
+
+ Object range4 = Ranges.floatRange(-2, BoundType.OPEN, 2, BoundType.OPEN, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range4));
+
+ Object range5 = Ranges.floatRange(-2, BoundType.CLOSED, 1, BoundType.OPEN,
+ 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range5));
+
+ Object range6 = Ranges.floatRange(-2, BoundType.CLOSED, 2,
+ BoundType.CLOSED, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range6));
+
+ Object range7 = Ranges.floatRange(-2, BoundType.CLOSED, 2, BoundType.OPEN,
+ 2);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range7));
+
+ // Using different constructors
+ FloatRange range8 = Ranges.floatRange(Long.valueOf(-2), Long.valueOf(2),
+ Long.valueOf(1));
+ assertEquals("Invalid equals using different constructor", range,
+ range8);
+
+ FloatRange range9 = Ranges.floatRange(Long.valueOf(-2), Long.valueOf(2));
+ assertEquals("Invalid equals using different constructor", range,
+ range9);
+
+ Endpoint<Float> leftEndpoint = new Endpoint<Float>(-2.0f,
+ BoundType.CLOSED);
+ Endpoint<Float> rightEndpoint = new Endpoint<Float>(2.0f,
+ BoundType.OPEN);
+ FloatRange range10 = Ranges.floatRange(leftEndpoint, rightEndpoint, 1.0f);
+ assertEquals("Invalid equals using different constructor", range,
+ range10);
+ }
+
+ @Test
+ public void testToString() {
+ FloatRange range = Ranges.floatRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertEquals("Wrong string value", "FloatRange<(-2.0, 2.0], 1.0>",
+ range.toString());
+ }
+
+ @Test
+ public void testConstructorUsingSameEndpoint() {
+ Endpoint<Float> uniqueEndpoint = new Endpoint<Float>(10.0f,
+ BoundType.CLOSED);
+ try {
+ Ranges.floatRange(uniqueEndpoint, uniqueEndpoint, 1.0f);
+ } catch (IllegalArgumentException e) {
+ fail("Not expected to get here");
+ }
+ }
+
+ @Test
+ public void testInvalidRange() {
+ try {
+ Ranges.floatRange(10.0f, BoundType.OPEN, -5.0f, BoundType.CLOSED,
+ 10.0f);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ Endpoint<Float> leftEndpoint = new Endpoint<Float>(10.0f,
+ BoundType.CLOSED);
+ Endpoint<Float> rightEndpoint = new Endpoint<Float>(-5.0f,
+ BoundType.OPEN);
+ try {
+ Ranges.floatRange(leftEndpoint, rightEndpoint, 1.0f);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ }
+
+ @Test
+ public void testDefaultStep() {
+ assertEquals("Invalid default step", Float.valueOf(-1.0f),
+ FloatRange.DEFAULT_STEP.evaluate(10.0f, 1.0f));
+ assertEquals("Invalid default step", Float.valueOf(1.0f),
+ FloatRange.DEFAULT_STEP.evaluate(1.0f, 10.0f));
+ }
+
+}
\ No newline at end of file
Propchange: commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestFloatRange.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java?rev=1385335&r1=1385334&r2=1385335&view=diff
==============================================================================
--- commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java (original)
+++ commons/proper/functor/branches/generators-FUNCTOR-14/src/test/java/org/apache/commons/functor/generator/range/TestIntegerRange.java Sun Sep 16 18:08:31 2012
@@ -17,13 +17,22 @@
package org.apache.commons.functor.generator.range;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import org.apache.commons.functor.BaseFunctorTest;
-import org.apache.commons.functor.generator.range.IntegerRange;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.generator.Generator;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
/**
@@ -31,19 +40,42 @@ import org.junit.Test;
*/
public class TestIntegerRange extends BaseFunctorTest {
+ // Attributes
+ // ------------------------------------------------------------------------
+ private IntegerRange ascIntRange = null;
+ private IntegerRange descIntRange = null;
+ private Collection<Integer> expectedAsc = null;
+ private Collection<Integer> expectedDesc = null;
+
+ // Test set up
+ // ------------------------------------------------------------------------
+ @Before
+ public void setUp() {
+ ascIntRange = Ranges.integerRange(0, 10);
+ descIntRange = Ranges.integerRange(10, 0);
+ expectedAsc = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
+ expectedDesc = Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
+ }
+
+ @After
+ public void tearDown() {
+ ascIntRange = null;
+ descIntRange = null;
+ }
+
@Override
protected Object makeFunctor() throws Exception {
- return new IntegerRange(10, 20);
+ return Ranges.integerRange(10, 20);
}
- // Tests
+ // Generator tests
// ------------------------------------------------------------------------
@Test
public void testGenerateListExample() {
// generates a collection of Integers from 0 (inclusive) to 10 (exclusive)
{
- List<? super Integer> list = (List<? super Integer>)(new IntegerRange(0,10).to(new ArrayList<Integer>()));
+ List<? super Integer> list = (List<? super Integer>)(Ranges.integerRange(0,10).to(new ArrayList<Integer>()));
for (int i=0;i<10;i++) {
assertEquals(new Integer(i),list.get(i));
}
@@ -51,7 +83,7 @@ public class TestIntegerRange extends Ba
// generates a collection of Integers from 10 (inclusive) to 0 (exclusive)
{
- List<? super Integer> list = (List<? super Integer>)(new IntegerRange(10,0).to(new ArrayList<Integer>()));
+ List<? super Integer> list = (List<? super Integer>)(Ranges.integerRange(10,0).to(new ArrayList<Integer>()));
for (int i=10;i>0;i--) {
assertEquals(new Integer(i),list.get(10-i));
}
@@ -61,34 +93,34 @@ public class TestIntegerRange extends Ba
@Test
public void testStepChecking() {
{
- new IntegerRange(2, 2, 0); // step of 0 is ok when range is empty
+ Ranges.integerRange(2, 2, 0); // step of 0 is ok when range is empty
}
{
- new IntegerRange(2, 2, 1); // positive step is ok when range is empty
+ Ranges.integerRange(2, 2, 1); // positive step is ok when range is empty
}
{
- new IntegerRange(2, 2, -1); // negative step is ok when range is empty
+ Ranges.integerRange(2, 2, -1); // negative step is ok when range is empty
}
{
- new IntegerRange(0, 1, 10); // big steps are ok
+ Ranges.integerRange(0, 1, 10); // big steps are ok
}
{
- new IntegerRange(1, 0, -10); // big steps are ok
+ Ranges.integerRange(1, 0, -10); // big steps are ok
}
try {
- new IntegerRange(0, 1, 0);
+ Ranges.integerRange(0, 1, 0);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
- new IntegerRange(0, 1, -1);
+ Ranges.integerRange(0, 1, -1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
try {
- new IntegerRange(0, -1, 1);
+ Ranges.integerRange(0, -1, 1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
@@ -97,59 +129,628 @@ public class TestIntegerRange extends Ba
@Test
public void testObjectConstructor() {
- IntegerRange range = new IntegerRange(new Integer(0), new Integer(5));
+ IntegerRange range = Ranges.integerRange(new Integer(0), new Integer(5));
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
- range = new IntegerRange(new Integer(0), new Integer(5), new Integer(1));
+ range = Ranges.integerRange(new Integer(0), new Integer(5), new Integer(1));
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
}
@Test
public void testReverseStep() {
- IntegerRange range = new IntegerRange(10, 0, -2);
+ IntegerRange range = Ranges.integerRange(10, 0, -2);
assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
}
@Test
public void testStep() {
- IntegerRange range = new IntegerRange(0, 10, 2);
+ IntegerRange range = Ranges.integerRange(0, 10, 2);
assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
}
@Test
public void testForwardRange() {
- IntegerRange range = new IntegerRange(0, 5);
+ IntegerRange range = Ranges.integerRange(0, 5);
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
}
@Test
public void testReverseRange() {
- IntegerRange range = new IntegerRange(5, 0);
+ IntegerRange range = Ranges.integerRange(5, 0);
assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
}
@Test
public void testEdgeCase() {
- IntegerRange range = new IntegerRange(Integer.MAX_VALUE - 3, Integer.MAX_VALUE);
+ IntegerRange range = Ranges.integerRange(Integer.MAX_VALUE - 3, Integer.MAX_VALUE);
assertEquals("[2147483644, 2147483645, 2147483646]", range.toCollection().toString());
assertEquals("[2147483644, 2147483645, 2147483646]", range.toCollection().toString());
}
@Test
- public void testEquals() {
- IntegerRange range = new IntegerRange(1, 5);
- assertObjectsAreEqual(range, range);
- assertObjectsAreEqual(range, new IntegerRange(1, 5));
- assertObjectsAreEqual(range, new IntegerRange(1, 5, 1));
- assertObjectsAreEqual(range, new IntegerRange(new Long(1), new Long(5)));
- assertObjectsAreEqual(range, new IntegerRange(new Long(1), new Long(5), new Long(1)));
- assertObjectsAreNotEqual(range, new IntegerRange(2, 5));
- assertObjectsAreNotEqual(range, new IntegerRange(1, 6));
- assertObjectsAreNotEqual(range, new IntegerRange(1, 5, 2));
+ public void testBoundaries() {
+ IntegerRange range = Ranges.integerRange(0, 10);
+ assertEquals(new Endpoint<Comparable<?>>(0, BoundType.CLOSED),
+ range.getLeftEndpoint());
+ assertEquals(new Endpoint<Comparable<?>>(10, BoundType.OPEN),
+ range.getRightEndpoint());
+ }
+
+ @Test
+ public void testClosedClosedAscending() {
+ // [-5, 5], 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.CLOSED, 3);
+ // [-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> expected = Arrays.asList(-5, -2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedAscending() {
+ // (-5, 5], 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.CLOSED, 3);
+ // (-5, 5], 3 = -2, 1, 4
+ List<Integer> expected = Arrays.asList(-2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenAscending() {
+ // [-5, 5), 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> expected = Arrays.asList(-5, -2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenAscending() {
+ // (-5, 5), 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5), 3 = -2, 1, 4
+ List<Integer> expected = Arrays.asList(-2, 1, 4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepAscending() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ // (-2, 2], 1 = -1, 0, 1, 2
+ List<Integer> expected = Arrays.asList(-1, 0, 1, 2);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedClosedDescending() {
+ // [5, -5], -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.CLOSED, -3);
+ // [5, -5], -3 = 5, 2, -1, -4
+ List<Integer> expected = Arrays.asList(5, 2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenClosedDescending() {
+ // (5, -5], -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.CLOSED, -3);
+ // (5, -5], -3 = 2, -1, -4
+ List<Integer> expected = Arrays.asList(2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testClosedOpenDescending() {
+ // [5, -5), -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.OPEN, -3);
+ // [5, -5), -3 = 5, 2, -1, -4
+ List<Integer> expected = Arrays.asList(5, 2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testOpenOpenDescending() {
+ // (5, -5), -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.OPEN, -3);
+ // (5, -5), -3 = 2, -1, -4
+ List<Integer> expected = Arrays.asList(2, -1, -4);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testSingleStepDescending() {
+ // [2, -2), -1 = 2, 1, 0, -1
+ IntegerRange range = Ranges.integerRange(2, BoundType.CLOSED, -2,
+ BoundType.OPEN, -1);
+ // [2, -2), -1 = 2, 1, 0, -1
+ List<Integer> expected = Arrays.asList(2, 1, 0, -1);
+ Collection<Integer> elements = range.toCollection();
+ assertEquals(expected, elements);
+ }
+
+ @Test
+ public void testAscending() {
+ final List<Integer> list = new ArrayList<Integer>();
+ ascIntRange.run(new UnaryProcedure<Integer>() {
+
+ public void run(Integer obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedAsc.containsAll(list));
+ }
+
+ @Test
+ public void testDescending() {
+ final List<Integer> list = new ArrayList<Integer>();
+ descIntRange.run(new UnaryProcedure<Integer>() {
+
+ public void run(Integer obj) {
+ list.add(obj);
+ }
+ });
+ assertTrue(expectedDesc.containsAll(list));
+ }
+
+ @Test
+ public void testToCollection() {
+ Collection<Integer> ascCol = ascIntRange.toCollection();
+ assertEquals("Different collections", expectedAsc, ascCol);
+ Collection<Integer> descCol = descIntRange.toCollection();
+ assertEquals("Different collections", expectedDesc, descCol);
+ }
+
+ @Test
+ public void testTransformedGenerator() {
+ int expected = 45;
+ int total = ascIntRange
+ .to(new UnaryFunction<Generator<? extends Integer>, Integer>() {
+
+ public Integer evaluate(Generator<? extends Integer> obj) {
+ int total = 0;
+ for (Object element : obj.toCollection()) {
+ total += (Integer) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ expected = 55;
+ total = descIntRange
+ .to(new UnaryFunction<Generator<? extends Integer>, Integer>() {
+
+ public Integer evaluate(Generator<? extends Integer> obj) {
+ int total = 0;
+ for (Object element : obj.toCollection()) {
+ total += (Integer) element;
+ }
+ return total;
+ }
+ });
+ assertEquals(expected, total);
+ }
+
+ // Range tests
+ // ---------------------------------------------------------------
+
+ // A base range with all integers between -6 and 6
+ private final List<Integer> fullRange = Collections.unmodifiableList(Arrays
+ .asList(-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6));
+
+ @Test
+ public void testEmptyRanges() {
+ IntegerRange empty1 = Ranges.integerRange(-2, BoundType.OPEN, -1,
+ BoundType.OPEN, 2);
+ assertTrue("The range was expected to be empty.", empty1.isEmpty());
+ IntegerRange empty2 = Ranges.integerRange(2, BoundType.OPEN, 0,
+ BoundType.OPEN, -2);
+ assertTrue("The range was expected to be empty.", empty2.isEmpty());
+ IntegerRange empty3 = Ranges.integerRange(0, BoundType.OPEN, 1,
+ BoundType.CLOSED, 2);
+ assertTrue("The range was expected to be empty.", empty3.isEmpty());
+ IntegerRange empty4 = Ranges.integerRange(-3, BoundType.OPEN, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty4.isEmpty());
+ IntegerRange empty5 = Ranges.integerRange(-3, BoundType.CLOSED, -3,
+ BoundType.OPEN, 1);
+ assertTrue("The range was expected to be empty.", empty5.isEmpty());
+ IntegerRange empty6 = Ranges.integerRange(1, BoundType.OPEN, 0,
+ BoundType.CLOSED, -2);
+ assertTrue("The range was expected to be empty.", empty6.isEmpty());
+ IntegerRange notEmpty1 = Ranges.integerRange(-3, BoundType.CLOSED, -3,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty1.isEmpty());
+ IntegerRange notEmpty2 = Ranges.integerRange(-3, BoundType.OPEN, -2,
+ BoundType.CLOSED, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty2.isEmpty());
+ IntegerRange notEmpty3 = Ranges.integerRange(2, BoundType.OPEN, 1,
+ BoundType.CLOSED, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty3.isEmpty());
+ IntegerRange notEmpty4 = Ranges.integerRange(2, BoundType.CLOSED, 1,
+ BoundType.OPEN, -1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty4.isEmpty());
+ IntegerRange notEmpty5 = Ranges.integerRange(1, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertFalse("The range was not expected to be empty.",
+ notEmpty5.isEmpty());
+ }
+
+ @Test
+ public void testClosedClosedAscendingContains() {
+ // [-5, 5], 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.CLOSED, 3);
+ // [-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> arr = Arrays.asList(-5, -2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedAscendingContains() {
+ // (-5, 5], 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.CLOSED, 3);
+ // (-5, 5], 3 = -2, 1, 4
+ List<Integer> arr = Arrays.asList(-2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenAscendingContains() {
+ // [-5, 5), 3 = -5, -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.CLOSED, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5], 3 = -5, -2, 1, 4
+ List<Integer> arr = Arrays.asList(-5, -2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenAscendingContains() {
+ // (-5, 5), 3 = -2, 1, 4
+ IntegerRange range = Ranges.integerRange(-5, BoundType.OPEN, 5,
+ BoundType.OPEN, 3);
+ // (-5, 5), 3 = -2, 1, 4
+ List<Integer> arr = Arrays.asList(-2, 1, 4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepAscending() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ IntegerRange ascendingRange = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ // (-2, 2], 1 = -1, 0, 1, 2
+ List<Integer> arr = Arrays.asList(-1, 0, 1, 2);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + ascendingRange + "]",
+ ascendingRange.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + ascendingRange +
+ "]",
+ ascendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedClosedDescendingContains() {
+ // [5, -5], -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.CLOSED, -3);
+ // [5, -5], -3 = 5, 2, -1, -4
+ List<Integer> arr = Arrays.asList(5, 2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenClosedDescendingContains() {
+ // (5, -5], -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.CLOSED, -3);
+ // (5, -5], -3 = 2, -1, -4
+ List<Integer> arr = Arrays.asList(2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testClosedOpenDescendingContains() {
+ // [5, -5), -3 = 5, 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.CLOSED, -5,
+ BoundType.OPEN, -3);
+ // [5, -5), -3 = 5, 2, -1, -4
+ List<Integer> arr = Arrays.asList(5, 2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testOpenOpenDescendingContains() {
+ // (5, -5), -3 = 2, -1, -4
+ IntegerRange range = Ranges.integerRange(5, BoundType.OPEN, -5,
+ BoundType.OPEN, -3);
+ // (5, -5), -3 = 2, -1, -4
+ List<Integer> arr = Arrays.asList(2, -1, -4);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + range + "]",
+ range.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + range + "]",
+ range.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsSingleStepDescending() {
+ // [2, -2), -1 = 2, 1, 0, -1
+ IntegerRange descendingRange = Ranges.integerRange(2, BoundType.CLOSED,
+ -2, BoundType.OPEN, -1);
+ // [2, -2), -1 = 2, 1, 0, -1
+ List<Integer> arr = Arrays.asList(2, 1, 0, -1);
+ for (Integer element : arr) {
+ assertTrue("Expected element [" + element +
+ "] is missing in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ List<Integer> elementsNotPresent = new ArrayList<Integer>(fullRange);
+ elementsNotPresent.removeAll(arr);
+ for (Integer element : elementsNotPresent) {
+ assertFalse("Unexpected element [" + element +
+ "] is present in range [" + descendingRange +
+ "]",
+ descendingRange.contains(element));
+ }
+ }
+
+ @Test
+ public void testContainsNullOrEmpty() {
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertFalse(range.contains(null));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testContainsAll() {
+ // (-2, 2], 1 = -1, 0, 1, 2
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ List<Integer> list = Arrays.asList(-1, 0, 1, 2);
+ assertTrue("Range [" + range +
+ "] was expected to contain all elements from list [" + list +
+ "]", range.containsAll(list));
+ List<Integer> listWithExtraElements = Arrays.asList(2, -1, 0, 1, 2, 3);
+ assertFalse("Range [" + range + "] has more elements than expected",
+ range.containsAll(listWithExtraElements));
+ assertFalse(range.containsAll(null));
+ assertFalse(range.containsAll(Collections.EMPTY_LIST));
+ }
+
+ @Test
+ public void testEquals()
+ throws Exception {
+ // equals basic properties
+ IntegerRange range = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertEquals("equals must be reflexive", range, range);
+ assertEquals("hashCode must be reflexive", range.hashCode(),
+ range.hashCode());
+ assertTrue(!range.equals(null)); // should be able to compare to null
+
+ Object range2 = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ if (range.equals(range2)) {
+ assertEquals("equals implies hash equals", range.hashCode(),
+ range2.hashCode());
+ assertEquals("equals must be symmetric", range2, range);
+ } else {
+ assertTrue("equals must be symmetric", !range2.equals(range));
+ }
+
+ // Changing attributes
+ Object range3 = Ranges.integerRange(-1, BoundType.CLOSED, 2,
+ BoundType.OPEN, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range3));
+
+ Object range4 = Ranges.integerRange(-2, BoundType.OPEN, 2, BoundType.OPEN,
+ 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range4));
+
+ Object range5 = Ranges.integerRange(-2, BoundType.CLOSED, 1,
+ BoundType.OPEN, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range5));
+
+ Object range6 = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.CLOSED, 1);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range6));
+
+ Object range7 = Ranges.integerRange(-2, BoundType.CLOSED, 2,
+ BoundType.OPEN, 2);
+ assertFalse("Invalid equals after changing attributes",
+ range.equals(range7));
+
+ // Using different constructors
+ IntegerRange range8 = Ranges.integerRange(Long.valueOf(-2),
+ Long.valueOf(2), Long.valueOf(1));
+ assertEquals("Invalid equals using different constructor", range,
+ range8);
+
+ IntegerRange range9 = Ranges.integerRange(Long.valueOf(-2),
+ Long.valueOf(2));
+ assertEquals("Invalid equals using different constructor", range,
+ range9);
+
+ Endpoint<Integer> leftEndpoint = new Endpoint<Integer>(-2,
+ BoundType.CLOSED);
+ Endpoint<Integer> rightEndpoint = new Endpoint<Integer>(2,
+ BoundType.OPEN);
+ IntegerRange range10 = Ranges.integerRange(leftEndpoint, rightEndpoint, 1);
+ assertEquals("Invalid equals using different constructor", range,
+ range10);
+ }
+
+ @Test
+ public void testToString() {
+ IntegerRange range = Ranges.integerRange(-2, BoundType.OPEN, 2,
+ BoundType.CLOSED, 1);
+ assertEquals("Wrong string value", "IntegerRange<(-2, 2], 1>",
+ range.toString());
+ }
+
+ @Test
+ public void testConstructorUsingSameEndpoint() {
+ Endpoint<Integer> uniqueEndpoint = new Endpoint<Integer>(
+ 10,
+ BoundType.CLOSED);
+ try {
+ Ranges.integerRange(uniqueEndpoint, uniqueEndpoint, 1);
+ } catch (IllegalArgumentException e) {
+ fail("Not expected to get here");
+ }
+ }
+
+ @Test
+ public void testInvalidRange() {
+ try {
+ Ranges.integerRange(10, BoundType.OPEN, -5, BoundType.CLOSED, 10);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ Endpoint<Integer> leftEndpoint = new Endpoint<Integer>(10,
+ BoundType.CLOSED);
+ Endpoint<Integer> rightEndpoint = new Endpoint<Integer>(-5,
+ BoundType.OPEN);
+ try {
+ Ranges.integerRange(leftEndpoint, rightEndpoint, 1);
+ fail("Not expected to get here");
+ } catch (IllegalArgumentException e) {
+ // Do nothing
+ }
+ }
+
+ @Test
+ public void testDefaultStep() {
+ assertEquals("Invalid default step", Integer.valueOf(-1),
+ IntegerRange.DEFAULT_STEP.evaluate(10, 1));
+ assertEquals("Invalid default step", Integer.valueOf(1),
+ IntegerRange.DEFAULT_STEP.evaluate(1, 10));
}
}
\ No newline at end of file