Repository: groovy Updated Branches: refs/heads/master a472efe53 -> 8311b23ad
minor refactor Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/8311b23a Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/8311b23a Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/8311b23a Branch: refs/heads/master Commit: 8311b23ade10c4b26c4473f6cc498641ebf2acb0 Parents: a472efe Author: paulk <[email protected]> Authored: Wed Aug 10 23:02:51 2016 +1000 Committer: paulk <[email protected]> Committed: Wed Aug 10 23:02:51 2016 +1000 ---------------------------------------------------------------------- .../groovy/lang/BigDecimalObjectRangeTest.java | 49 ++ src/test/groovy/lang/BigDecimalRangeTest.java | 44 -- .../groovy/lang/BigIntegerObjectRangeTest.java | 49 ++ src/test/groovy/lang/BigIntegerRangeTest.java | 44 -- src/test/groovy/lang/DoubleObjectRangeTest.java | 49 ++ src/test/groovy/lang/EmptyRangeTest.java | 2 +- src/test/groovy/lang/FloatObjectRangeTest.java | 47 ++ src/test/groovy/lang/FloatRangeTest.java | 42 -- .../groovy/lang/IntegerObjectRangeTest.java | 47 ++ src/test/groovy/lang/IntegerRangeTest.java | 43 -- src/test/groovy/lang/LongObjectRangeTest.java | 56 ++ src/test/groovy/lang/LongRangeTest.java | 52 -- src/test/groovy/lang/NumberRangeTest.java | 737 ------------------ src/test/groovy/lang/NumberRangeTestCase.java | 749 +++++++++++++++++++ src/test/groovy/lang/ShortObjectRangeTest.java | 46 ++ src/test/groovy/lang/ShortRangeTest.java | 42 -- 16 files changed, 1093 insertions(+), 1005 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/BigDecimalObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/BigDecimalObjectRangeTest.java b/src/test/groovy/lang/BigDecimalObjectRangeTest.java new file mode 100644 index 0000000..fcaa7b2 --- /dev/null +++ b/src/test/groovy/lang/BigDecimalObjectRangeTest.java @@ -0,0 +1,49 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +import java.math.BigDecimal; + +/** + * Tests {@link ObjectRange}s of {@link BigDecimal}s. + */ +public class BigDecimalObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange(new BigDecimal(from), new BigDecimal(to)); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return new BigDecimal(value); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/BigDecimalRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/BigDecimalRangeTest.java b/src/test/groovy/lang/BigDecimalRangeTest.java deleted file mode 100644 index 61414f6..0000000 --- a/src/test/groovy/lang/BigDecimalRangeTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - -import java.math.BigDecimal; - -/** - * Tests {@link ObjectRange}s of {@link BigDecimal}s. - * - * @author Edwin Tellman - */ -public class BigDecimalRangeTest extends NumberRangeTest { - - /** - * {@inheritDoc} - */ - protected Range createRange(int from, int to) { - return new ObjectRange(new BigDecimal(from), new BigDecimal(to)); - } - - /** - * {@inheritDoc} - */ - protected Comparable createValue(int value) { - return new BigDecimal(value); - } - -} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/BigIntegerObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/BigIntegerObjectRangeTest.java b/src/test/groovy/lang/BigIntegerObjectRangeTest.java new file mode 100644 index 0000000..15fb61a --- /dev/null +++ b/src/test/groovy/lang/BigIntegerObjectRangeTest.java @@ -0,0 +1,49 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +import java.math.BigInteger; + +/** + * Tests {@link ObjectRange}s of {@link BigInteger}s. + */ +public class BigIntegerObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange(BigInteger.valueOf(from), BigInteger.valueOf(to)); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return BigInteger.valueOf(value); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/BigIntegerRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/BigIntegerRangeTest.java b/src/test/groovy/lang/BigIntegerRangeTest.java deleted file mode 100644 index ce121be..0000000 --- a/src/test/groovy/lang/BigIntegerRangeTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - -import java.math.BigInteger; - -/** - * Tests {@link ObjectRange}s of {@link BigInteger}s. - * - * @author Edwin Tellman - */ -public class BigIntegerRangeTest extends NumberRangeTest { - - /** - * {@inheritDoc} - */ - protected Range createRange(int from, int to) { - return new ObjectRange(BigInteger.valueOf(from), BigInteger.valueOf(to)); - } - - /** - * {@inheritDoc} - */ - protected Comparable createValue(int value) { - return BigInteger.valueOf(value); - } - -} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/DoubleObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/DoubleObjectRangeTest.java b/src/test/groovy/lang/DoubleObjectRangeTest.java new file mode 100644 index 0000000..6d866a2 --- /dev/null +++ b/src/test/groovy/lang/DoubleObjectRangeTest.java @@ -0,0 +1,49 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +/** + * Tests {@link ObjectRange}s of {@link Double}s. + */ +public class DoubleObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange((double) from, (double) to); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return (double) value; + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/EmptyRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/EmptyRangeTest.java b/src/test/groovy/lang/EmptyRangeTest.java index e5e09d9..16e7302 100644 --- a/src/test/groovy/lang/EmptyRangeTest.java +++ b/src/test/groovy/lang/EmptyRangeTest.java @@ -374,7 +374,7 @@ public class EmptyRangeTest extends GroovyTestCase { */ public void testStepIntClosure() { final List callLog = new ArrayList(); - final Closure closure = new NumberRangeTest.RecordingClosure(callLog); + final Closure closure = new NumberRangeTestCase.RecordingClosure(callLog); range.step(1, closure); assertEquals("wrong number of calls to closure", 0, callLog.size()); } http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/FloatObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/FloatObjectRangeTest.java b/src/test/groovy/lang/FloatObjectRangeTest.java new file mode 100644 index 0000000..e46bb48 --- /dev/null +++ b/src/test/groovy/lang/FloatObjectRangeTest.java @@ -0,0 +1,47 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +/** + * Tests {@link ObjectRange}s of {@link Float}s. + */ +public class FloatObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange(new Float(from), new Float(to)); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return new Double(value); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/FloatRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/FloatRangeTest.java b/src/test/groovy/lang/FloatRangeTest.java deleted file mode 100644 index 4b512c0..0000000 --- a/src/test/groovy/lang/FloatRangeTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - -/** - * Tests {@link ObjectRange}s of {@link Float}s. - * - * @author Edwin Tellman - */ -public class FloatRangeTest extends NumberRangeTest { - - /** - * {@inheritDoc} - */ - protected Range createRange(int from, int to) { - return new ObjectRange(new Float(from), new Float(to)); - } - - /** - * {@inheritDoc} - */ - protected Comparable createValue(int value) { - return new Double(value); - } - -} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/IntegerObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/IntegerObjectRangeTest.java b/src/test/groovy/lang/IntegerObjectRangeTest.java new file mode 100644 index 0000000..29f8adc --- /dev/null +++ b/src/test/groovy/lang/IntegerObjectRangeTest.java @@ -0,0 +1,47 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +/** + * Tests {@link ObjectRange}s of {@link Integer}s. + */ +public class IntegerObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange(new Integer(from), new Integer(to)); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return new Integer(value); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/IntegerRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/IntegerRangeTest.java b/src/test/groovy/lang/IntegerRangeTest.java deleted file mode 100644 index 48e9f4d..0000000 --- a/src/test/groovy/lang/IntegerRangeTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - - -/** - * Tests {@link ObjectRange}s of {@link Integer}s. - * - * @author Edwin Tellman - */ -public class IntegerRangeTest extends NumberRangeTest { - - /** - * {@inheritDoc} - */ - protected Range createRange(int from, int to) { - return new ObjectRange(new Integer(from), new Integer(to)); - } - - /** - * {@inheritDoc} - */ - protected Comparable createValue(int value) { - return new Integer(value); - } - -} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/LongObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/LongObjectRangeTest.java b/src/test/groovy/lang/LongObjectRangeTest.java new file mode 100644 index 0000000..5d7c8e2 --- /dev/null +++ b/src/test/groovy/lang/LongObjectRangeTest.java @@ -0,0 +1,56 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +/** + * Tests {@link ObjectRange}s of {@link Long}s. + */ +public class LongObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange(new Long(from), new Long(to)); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return new Long(value); + } + + public void testSizeWithLongTo() { + assertEquals(3, new ObjectRange(new Integer(Integer.MAX_VALUE), new Long(Integer.MAX_VALUE + 2L)).size()); + } + + // GROOVY-4973: Range made-up of from: Integer and to: Long should have 'from' promoted to type Long. + protected void checkRangeValues(Integer from, Comparable to, Range range) { + assertEquals("wrong 'from' value", Long.valueOf(from.longValue()), range.getFrom()); + assertEquals("wrong 'to' value", to, range.getTo()); + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/LongRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/LongRangeTest.java b/src/test/groovy/lang/LongRangeTest.java deleted file mode 100644 index f636f0a..0000000 --- a/src/test/groovy/lang/LongRangeTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - - -/** - * Tests {@link ObjectRange}s of {@link Long}s. - * - * @author Edwin Tellman - */ -public class LongRangeTest extends NumberRangeTest { - - /** - * {@inheritDoc} - */ - protected Range createRange(int from, int to) { - return new ObjectRange(new Long(from), new Long(to)); - } - - /** - * {@inheritDoc} - */ - protected Comparable createValue(int value) { - return new Long(value); - } - - public void testSizeWithLongTo() { - assertEquals(3, new ObjectRange(new Integer(Integer.MAX_VALUE), new Long(Integer.MAX_VALUE + 2L)).size()); - } - - // GROOVY-4973: Range made-up of from: Integer and to: Long should have 'from' promoted to type Long. - protected void checkRangeValues(Integer from, Comparable to, Range range) { - assertEquals("wrong 'from' value", Long.valueOf(from.longValue()), range.getFrom()); - assertEquals("wrong 'to' value", to, range.getTo()); - } -} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/NumberRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/NumberRangeTest.java b/src/test/groovy/lang/NumberRangeTest.java deleted file mode 100644 index e4d5a93..0000000 --- a/src/test/groovy/lang/NumberRangeTest.java +++ /dev/null @@ -1,737 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - -import junit.framework.TestCase; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; - -/** - * Provides unit tests for ranges of numbers. - * - * @author Edwin Tellman - */ -public abstract class NumberRangeTest extends TestCase { - - /** - * Records the values passed to a closure. - */ - protected static class RecordingClosure extends Closure { - /** - * Holds the values passed in - */ - final List callLog; - - /** - * Creates a new <code>RecordingClosure</code> - * - * @param callLog is filled with the values passed to <code>doCall</code> - */ - RecordingClosure(final List callLog) { - super(null); - this.callLog = callLog; - } - - /** - * Stores <code>params</code> in the <code>callLog</code>. - * - * @param params the parameters. - * @return null - */ - public Object doCall(final Object params) { - callLog.add(params); - return null; - } - } - - /** - * Creates a {@link Range} to test. - * - * @param from the first value in the range. - * @param to the last value in the range. - * @return a {@link Range} to test - */ - protected abstract Range createRange(final int from, final int to); - - /** - * Creates a value in the range. - * - * @param value the value to create. - * @return a value in the range. - */ - protected abstract Comparable createValue(final int value); - - /** - * Tests <code>hashCode</code> and <code>equals</code> comparing one {@link IntRange} to another {@link IntRange}. - */ - public final void testHashCodeAndEquals() { - Range a = createRange(1, 11); - Range b = createRange(1, 11); - Range c = createRange(2, 11); - - assertEquals("hashcode", a.hashCode(), b.hashCode()); - assertTrue("hashcode", a.hashCode() != c.hashCode()); - - assertEquals("a and b", a, b); - assertFalse("a != c", a.equals(c)); - } - - /** - * Tests using different classes for 'from' and 'to'. - */ - public void testDifferentClassesForFromAndTo() { - final Integer from = new Integer(1); - final Comparable to = createValue(5); - final Range range = new ObjectRange(from, to); - - checkRangeValues(from, to, range); - } - - protected void checkRangeValues(Integer from, Comparable to, Range range) { - assertEquals("wrong 'from' value", from, range.getFrom()); - assertEquals("wrong 'to' value", to, range.getTo()); - } - - /** - * Tests a <code>null</code> 'from' value. - */ - public void testNullFrom() { - try { - new ObjectRange(null, createValue(5)); - fail("null 'from' accepted"); - } - catch (IllegalArgumentException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests a <code>null</code> 'to' value. - */ - public void testNullTo() { - try { - new ObjectRange(createValue(23), null); - fail("null 'to' accepted"); - } - catch (IllegalArgumentException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests stepping through a range by two with a closure. - */ - public void testStepByTwoWithClosure() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(0, 4); - range.step(2, closure); - - assertEquals("wrong number of calls to closure", 3, callLog.size()); - final Iterator iter = callLog.iterator(); - for (int i = 0; i <= 4; i += 2) { - assertEquals("wrong argument passed to closure", createValue(i), iter.next()); - } - } - - /** - * Tests iterating over a one-element range. - */ - public void testOneElementRange() { - final Range range = createRange(1, 1); - int next = 1; - for (Iterator iter = range.iterator(); iter.hasNext();) { - final Number number = (Number) iter.next(); - assertEquals("wrong number", createValue(next++), number); - } - assertEquals("wrong number of elements in iteration", 2, next); - } - - /** - * Tests stepping through a reversed range by two with a closure. - */ - public void testReverseStepByTwoWithClosure() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(4, 0); - range.step(2, closure); - - assertEquals("wrong number of calls to closure", 3, callLog.size()); - final Iterator iter = callLog.iterator(); - for (int i = 4; i >= 0; i -= 2) { - assertEquals("wrong argument passed to closure", createValue(i), iter.next()); - } - } - - /** - * Tests stepping through a range with a closure. - */ - public void testStepByOneWithClosure() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(1, 5); - range.step(1, closure); - - assertEquals("wrong number of calls to closure", 5, callLog.size()); - final Iterator iter = callLog.iterator(); - for (int i = 1; i <= 5; i++) { - assertEquals("wrong argument passed to closure", createValue(i), iter.next()); - } - } - - /** - * Tests stepping through a reversed range by one with a closure. - */ - public void testReverseStepByOneWithClosure() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(5, 1); - range.step(1, closure); - - assertEquals("wrong number of calls to closure", 5, callLog.size()); - final Iterator iter = callLog.iterator(); - for (int i = 5; i >= 1; i--) { - assertEquals("wrong argument passed to closure", createValue(i), iter.next()); - } - } - - /** - * Tests stepping backwards through a range with a closure. - */ - public void testNegativeStepByOneWithClosure() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(1, 5); - range.step(-1, closure); - - assertEquals("wrong number of calls to closure", 5, callLog.size()); - final Iterator iter = callLog.iterator(); - for (int i = 5; i >= 1; i--) { - assertEquals("wrong argument passed to closure", createValue(i), iter.next()); - } - } - - /** - * Tests stepping backwards through a reversed range with a closure. - */ - public void testNegativeReverseStepByOneWithClosure() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(5, 1); - range.step(-1, closure); - - assertEquals("wrong number of calls to closure", 5, callLog.size()); - final Iterator iter = callLog.iterator(); - for (int i = 1; i <= 5; i++) { - assertEquals("wrong argument passed to closure", createValue(i), iter.next()); - } - } - - /** - * Tests stepping backwards through a range with a step size greater than the range size. - */ - public void testStepLargerThanRange() { - final List callLog = new ArrayList(); - final Closure closure = new RecordingClosure(callLog); - - final Range range = createRange(1, 5); - - range.step(6, closure); - assertEquals("wrong number of calls to closure", 1, callLog.size()); - assertEquals("wrong value", createValue(1), callLog.get(0)); - - final List stepList = range.step(6); - assertEquals("wrong number of values in result", 1, stepList.size()); - assertEquals("wrong value", createValue(1), callLog.get(0)); - } - - /** - * Tests stepping through a range by one. - */ - public void testStepByOne() { - final Range range = createRange(1, 5); - final List result = range.step(1); - - assertEquals("wrong number of calls", 5, result.size()); - final Iterator iter = result.iterator(); - for (int i = 1; i <= 5; i++) { - assertEquals("incorrect value in result", createValue(i), iter.next()); - } - } - - /** - * Tests stepping through a range by two. - */ - public void testStepByTwo() { - final Range range = createRange(1, 5); - final List result = range.step(2); - - assertEquals("wrong number of calls", 3, result.size()); - final Iterator iter = result.iterator(); - for (int i = 1; i <= 5; i += 2) { - assertEquals("incorrect value in result", createValue(i), iter.next()); - } - } - - /** - * Tests getting the size. - */ - public void testSize() { - Range range = createRange(0, 10); - assertEquals("Size of " + range, 11, range.size()); - range = createRange(0, 1); - assertEquals("Size of " + range, 2, range.size()); - range = createRange(0, 0); - assertEquals("Size of " + range, 1, range.size()); - } - - /** - * Tests asking for an index outside of the valid range - */ - public void testGetOutOfRange() { - Range r = createRange(10, 20); - - try { - r.get(-1); - fail("Should have thrown IndexOutOfBoundsException"); - } - catch (IndexOutOfBoundsException e) { - assertTrue("expected exception thrown", true); - } - try { - r.get(11); - fail("Should have thrown IndexOutOfBoundsException"); - } - catch (IndexOutOfBoundsException e) { - assertTrue("expected exception thrown", true); - } - - } - - /** - * Tests getting a sub list. - */ - public void testSubList() { - Range range = createRange(0, 5); - - List subList = range.subList(2, 4); - assertEquals("size", 2, subList.size()); - - assertTrue("sublist not a range", subList instanceof Range); - Range subListRange = (Range) subList; - - assertEquals("from", createValue(2), subListRange.getFrom()); - assertEquals("to", createValue(3), subListRange.getTo()); - - subList = range.subList(0, 6); - assertEquals("size", 6, subList.size()); - - assertTrue("sublist not a range", subList instanceof Range); - subListRange = (Range) subList; - - assertEquals("from", createValue(0), subListRange.getFrom()); - assertEquals("to", createValue(5), subListRange.getTo()); - } - - /** - * Tests creating a sub list with a negative "from" index. - */ - public void testSubListNegativeFrom() { - try { - final Range range = createRange(1, 5); - range.subList(-1, 3); - fail("accepted sub list with negative index"); - } - catch (IndexOutOfBoundsException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests creating a sub list with an out of range "to" index. - */ - public void testSubListOutOfRangeTo() { - try { - final Range range = createRange(0, 3); - range.subList(0, 5); - fail("accepted sub list with invalid 'to'"); - } - catch (IndexOutOfBoundsException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests creating a sub list with "from" grater than "to." - */ - public void testSubListFromGreaterThanTo() { - try { - final Range range = createRange(1, 5); - range.subList(3, 2); - fail("accepted sub list with 'from' greater than 'to'"); - } - catch (IllegalArgumentException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests creating an empty sub list. - */ - public void testEmptySubList() { - final Range range = createRange(1, 5); - - List subList = range.subList(0, 0); - assertEquals("wrong number of elements in sub list", 0, subList.size()); - - subList = range.subList(2, 2); - assertEquals("wrong number of elements in sub list", 0, subList.size()); - } - - /** - * Tests iterating over a non-reversed range. - */ - public void testIterate() { - final Range range = createRange(1, 5); - int next = 1; - final Iterator iter = range.iterator(); - while (iter.hasNext()) { - final Object value = iter.next(); - assertEquals("wrong next value", createValue(next++), value); - } - assertEquals("wrong number of elements in iteration", 6, next); - assertNull("got element after iterator finished", iter.next()); - } - - /** - * Tests removing an element from the range using an iterator (not supported). - */ - public void testRemoveFromIterator() { - final Range range = createRange(1, 5); - - try { - final Iterator iter = range.iterator(); - iter.remove(); - fail("successfully removed an element using an iterator"); - } - catch (UnsupportedOperationException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests iterating over a reversed range. - */ - public void testIterateReversed() { - final Range range = createRange(5, 1); - int next = 5; - for (Iterator iter = range.iterator(); iter.hasNext();) { - assertEquals("wrong number", createValue(next--), iter.next()); - } - assertEquals("wrong number of elements in iteration", 0, next); - } - - /** - * Tests creating an <code>IntRange</code> with from > to. - */ - public void testFromGreaterThanTo() { - final int from = 9; - final int to = 0; - final Range range = createRange(from, to); - - assertTrue("range not reversed", range.isReverse()); - - // make sure to/from are swapped - assertEquals("from incorrect", createValue(to), range.getFrom()); - assertEquals("to incorrect", createValue(from), range.getTo()); - - assertEquals("wrong size", 10, range.size()); - - assertEquals("wrong first element", createValue(9), range.get(0)); - assertEquals("wrong last element", createValue(0), range.get(9)); - } - - /** - * Tests creating an <code>IntRange</code> with from == to. - */ - public void testFromEqualsTo() { - final Range range = createRange(5, 5); - - assertFalse("range reversed", range.isReverse()); - assertEquals("wrong size", 1, range.size()); - } - - /** - * Tests creating an <code>IntRange</code> with from < to. - */ - public void testFromLessThanTo() { - final int from = 1; - final int to = 4; - final Range range = createRange(from, to); - - assertFalse("range reversed", range.isReverse()); - - assertEquals("to incorrect", createValue(from), range.getFrom()); - assertEquals("from incorrect", createValue(to), range.getTo()); - - assertEquals("wrong size", 4, range.size()); - } - - /** - * Making a range equal a list is not actually possible, since list.equals(range) will not evaluate to - * <code>true</code> and <code>equals</code> should be symmetric. - */ - public void testEqualsList() { - final List list = new ArrayList(); - list.add(createValue(1)); - list.add(createValue(2)); - - final Range range = createRange(1, 2); - - // cast to Object to test routing through equals(Object) - assertTrue("range does not equal list", range.equals((Object) list)); - assertTrue("list does not equal range", list.equals(range)); - assertEquals("hash codes are not equal", range.hashCode(), list.hashCode()); - - // compare lists that are the same size but contain different elements - list.set(0, createValue(3)); - assertFalse("range equals list", range.equals(list)); - assertFalse("list equals range", list.equals(range)); - assertFalse("hash codes are equal", range.hashCode() == list.hashCode()); - - // compare a list longer than the range - list.set(0, createValue(1)); - list.add(createValue(3)); - assertFalse("range equals list", range.equals(list)); - assertFalse("list equals range", list.equals(range)); - assertFalse("hash are equal", range.hashCode() == list.hashCode()); - - // compare a list shorter than the range - list.remove(2); - list.remove(1); - assertFalse("range equals list", range.equals(list)); - assertFalse("list equals range", list.equals(range)); - assertFalse("hash are equal", range.hashCode() == list.hashCode()); - } - - /** - * Tests comparing {@link Range} to an object that is not a {@link Range}. - */ - public void testEqualsNonRange() { - final Range range = createRange(1, 5); - assertFalse("range equal to string", range.equals("hello")); - } - - /** - * Tests comparing a {@link Range} cast to an {@link Object} - */ - public void testEqualsRangeAsObject() { - final Range range1 = createRange(1, 5); - final Range range2 = createRange(1, 5); - assertTrue("ranges not equal", range1.equals((Object) range2)); - } - - /** - * Tests comparing two {@link Range}s to each other. - */ - public void testEqualsRange() { - final Range range1 = createRange(1, 5); - Range range2 = createRange(1, 5); - assertTrue("ranges not equal", range1.equals((Object) range2)); - assertTrue("ranges not equal", range2.equals((Object) range1)); - assertEquals("hash codes not equal", range1.hashCode(), range2.hashCode()); - - range2 = createRange(0, 5); - assertFalse("ranges equal", range1.equals((Object) range2)); - assertFalse("ranges equal", range2.equals((Object) range1)); - assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); - - range2 = createRange(1, 6); - assertFalse("ranges equal", range1.equals((Object) range2)); - assertFalse("ranges equal", range2.equals((Object) range1)); - assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); - - range2 = createRange(0, 6); - assertFalse("ranges equal", range1.equals((Object) range2)); - assertFalse("ranges equal", range2.equals((Object) range1)); - assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); - - range2 = createRange(2, 4); - assertFalse("ranges equal", range1.equals((Object) range2)); - assertFalse("ranges equal", range2.equals((Object) range1)); - assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); - - range2 = createRange(5, 1); - assertFalse("ranges equal", range1.equals((Object) range2)); - assertFalse("ranges equal", range2.equals((Object) range1)); - assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); - } - - /** - * Tests <code>toString</code> and <code>inspect</code> - */ - public void testToStringAndInspect() { - Range range = createRange(1, 5); - String expected = range.getFrom() + ".." + range.getTo(); - assertEquals("wrong string representation", expected, range.toString()); - assertEquals("wrong string representation", expected, range.inspect()); - - range = createRange(5, 1); - expected = range.getTo() + ".." + range.getFrom(); - assertEquals("wrong string representation", expected, range.toString()); - assertEquals("wrong string representation", expected, range.inspect()); - } - - /** - * Tests <code>getFrom</code> and <code>getTo</code>. - */ - public void testGetFromAndTo() { - final int from = 1, to = 5; - final Range range = createRange(from, to); - - assertEquals("wrong 'from' value", createValue(from), range.getFrom()); - assertEquals("wrong 'to' value", createValue(to), range.getTo()); - } - - /** - * Tests comparing a {@link Range} to <code>null</code>. - */ - public void testEqualsNull() { - final Range range = createRange(1, 5); - assertFalse("range equal to null", range.equals(null)); - assertFalse("range equal to null Object", range.equals((Object) null)); - assertFalse("range equal to null Range", range.equals((Range) null)); - assertFalse("range equal to null List", range.equals((List) null)); - } - - /** - * Tests attempting to add a value to a range. - */ - public void testAddValue() { - try { - final Range range = createRange(1, 5); - range.add(createValue(20)); - fail("expected exception not thrown"); - } - catch (UnsupportedOperationException e) { - assertTrue("expected exception thrown", true); - } - } - - /** - * Tests attempting to remove a value from a range. - */ - public void testRemoveValue() { - try { - final Range range = createRange(1, 5); - range.remove(0); - fail("expected exception not thrown"); - } - catch (UnsupportedOperationException e) { - assertTrue("expected exception thrown", true); - } - } - - private void doTestContains(int from, int to, Range range) { - // test integers - assertTrue("missing 'from' value", range.contains(createValue(from))); - assertTrue("missing 'to' value", range.contains(createValue(to))); - assertTrue("missing mid point", range.contains(createValue((from + to) / 2))); - assertFalse("contains out of range value", range.contains(createValue(from - 1))); - assertFalse("contains out of range value", range.contains(createValue(to + 1))); - - // test ranges - assertTrue("missing same range", range.containsAll(createRange(from, to))); - assertTrue("missing same range", range.containsAll(createRange(to, from))); - assertTrue("missing strict subset", range.containsAll(createRange(from + 1, to - 1))); - assertTrue("missing subset", range.containsAll(createRange(from, to - 1))); - assertTrue("missing subset", range.containsAll(createRange(from + 1, to))); - assertFalse("contains non-subset", range.containsAll(createRange(from - 1, to))); - assertFalse("contains non-subset", range.containsAll(createRange(from, to + 1))); - assertFalse("contains non-subset", range.containsAll(createRange(from - 2, from - 1))); - - // ranges don't contain other ranges - assertFalse("range contains sub-range", range.contains(createRange(from + 1, to - 1))); - - // test list - final List list = new ArrayList(); - list.add(createValue(from)); - list.add(createValue(to)); - assertTrue("missing strict subset", range.containsAll(list)); - - // test non-integer number - assertFalse("contains Float", range.contains(new Float((to + from) / 2.0 + 0.3))); - } - - /** - * Tests whether the range contains a {@link Comparable} object which is not comparable with a {@link Number}. - */ - public void testContainsIncompatibleComparable() { - final Range range = createRange(1, 5); - assertFalse("range contains string", range.contains("hello")); - assertFalse("range contains string", range.contains("1")); - } - - /** - * Tests whether the range contains a non-comparable object. - */ - public void testContainsNonComparable() { - final Range range = createRange(1, 5); - assertFalse("range contains hash map", range.contains(new HashMap())); - } - - /** - * Tests whether a {@link Range} contains another {@link Range} or a specific integer. - */ - public void testContains() { - final int from = 1, to = 5; - doTestContains(from, to, createRange(from, to)); - doTestContains(from, to, createRange(to, from)); - } - - /** - * Tests <code>get</code> from a reversed range. - */ - public void testGetFromReversedRange() { - final Range range = createRange(5, 1); - - for (int i = 0; i < 5; i++) { - assertEquals("wrong element at position " + i, createValue(5 - i), range.get(i)); - } - } - - /** - * Tests getting values from the range. - */ - public void testGet() { - final Range range = createRange(10, 20); - for (int i = 0; i <= 10; i++) { - assertEquals("Item at index: " + i, createValue(i + 10), range.get(i)); - } - } -} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/NumberRangeTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/NumberRangeTestCase.java b/src/test/groovy/lang/NumberRangeTestCase.java new file mode 100644 index 0000000..c3ea57f --- /dev/null +++ b/src/test/groovy/lang/NumberRangeTestCase.java @@ -0,0 +1,749 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +import junit.framework.TestCase; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +/** + * Provides unit tests for ranges of numbers. + */ +public abstract class NumberRangeTestCase extends TestCase { + + /** + * legacy ranges in Groovy hush exceptions and return null when exhausted + */ + protected boolean expectNullFromExhausted = false; + + /** + * Records the values passed to a closure. + */ + protected static class RecordingClosure extends Closure { + /** + * Holds the values passed in + */ + final List callLog; + + /** + * Creates a new <code>RecordingClosure</code> + * + * @param callLog is filled with the values passed to <code>doCall</code> + */ + RecordingClosure(final List callLog) { + super(null); + this.callLog = callLog; + } + + /** + * Stores <code>params</code> in the <code>callLog</code>. + * + * @param params the parameters. + * @return null + */ + public Object doCall(final Object params) { + callLog.add(params); + return null; + } + } + + /** + * Creates a {@link Range} to test. + * + * @param from the first value in the range. + * @param to the last value in the range. + * @return a {@link Range} to test + */ + protected abstract Range createRange(final int from, final int to); + + /** + * Creates a value in the range. + * + * @param value the value to create. + * @return a value in the range. + */ + protected abstract Comparable createValue(final int value); + + /** + * Tests <code>hashCode</code> and <code>equals</code> comparing one {@link IntRange} to another {@link IntRange}. + */ + public final void testHashCodeAndEquals() { + Range a = createRange(1, 11); + Range b = createRange(1, 11); + Range c = createRange(2, 11); + + assertEquals("hashcode", a.hashCode(), b.hashCode()); + assertTrue("hashcode", a.hashCode() != c.hashCode()); + + assertEquals("a and b", a, b); + assertFalse("a != c", a.equals(c)); + } + + /** + * Tests using different classes for 'from' and 'to'. + */ + public void testDifferentClassesForFromAndTo() { + final Integer from = new Integer(1); + final Comparable to = createValue(5); + final Range range = new ObjectRange(from, to); + + checkRangeValues(from, to, range); + } + + protected void checkRangeValues(Integer from, Comparable to, Range range) { + assertEquals("wrong 'from' value", from, range.getFrom()); + assertEquals("wrong 'to' value", to, range.getTo()); + } + + /** + * Tests a <code>null</code> 'from' value. + */ + public void testNullFrom() { + try { + new ObjectRange(null, createValue(5)); + fail("null 'from' accepted"); + } + catch (IllegalArgumentException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests a <code>null</code> 'to' value. + */ + public void testNullTo() { + try { + new ObjectRange(createValue(23), null); + fail("null 'to' accepted"); + } + catch (IllegalArgumentException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests stepping through a range by two with a closure. + */ + public void testStepByTwoWithClosure() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(0, 4); + range.step(2, closure); + + assertEquals("wrong number of calls to closure", 3, callLog.size()); + final Iterator iter = callLog.iterator(); + for (int i = 0; i <= 4; i += 2) { + assertEquals("wrong argument passed to closure", createValue(i), iter.next()); + } + } + + /** + * Tests iterating over a one-element range. + */ + public void testOneElementRange() { + final Range range = createRange(1, 1); + int next = 1; + for (Object value : range) { + final Number number = (Number) value; + assertEquals("wrong number", createValue(next++), number); + } + assertEquals("wrong number of elements in iteration", 2, next); + } + + /** + * Tests stepping through a reversed range by two with a closure. + */ + public void testReverseStepByTwoWithClosure() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(4, 0); + range.step(2, closure); + + assertEquals("wrong number of calls to closure", 3, callLog.size()); + final Iterator iter = callLog.iterator(); + for (int i = 4; i >= 0; i -= 2) { + assertEquals("wrong argument passed to closure", createValue(i), iter.next()); + } + } + + /** + * Tests stepping through a range with a closure. + */ + public void testStepByOneWithClosure() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(1, 5); + range.step(1, closure); + + assertEquals("wrong number of calls to closure", 5, callLog.size()); + final Iterator iter = callLog.iterator(); + for (int i = 1; i <= 5; i++) { + assertEquals("wrong argument passed to closure", createValue(i), iter.next()); + } + } + + /** + * Tests stepping through a reversed range by one with a closure. + */ + public void testReverseStepByOneWithClosure() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(5, 1); + range.step(1, closure); + + assertEquals("wrong number of calls to closure", 5, callLog.size()); + final Iterator iter = callLog.iterator(); + for (int i = 5; i >= 1; i--) { + assertEquals("wrong argument passed to closure", createValue(i), iter.next()); + } + } + + /** + * Tests stepping backwards through a range with a closure. + */ + public void testNegativeStepByOneWithClosure() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(1, 5); + range.step(-1, closure); + + assertEquals("wrong number of calls to closure", 5, callLog.size()); + final Iterator iter = callLog.iterator(); + for (int i = 5; i >= 1; i--) { + assertEquals("wrong argument passed to closure", createValue(i), iter.next()); + } + } + + /** + * Tests stepping backwards through a reversed range with a closure. + */ + public void testNegativeReverseStepByOneWithClosure() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(5, 1); + range.step(-1, closure); + + assertEquals("wrong number of calls to closure", 5, callLog.size()); + final Iterator iter = callLog.iterator(); + for (int i = 1; i <= 5; i++) { + assertEquals("wrong argument passed to closure", createValue(i), iter.next()); + } + } + + /** + * Tests stepping backwards through a range with a step size greater than the range size. + */ + public void testStepLargerThanRange() { + final List callLog = new ArrayList(); + final Closure closure = new RecordingClosure(callLog); + + final Range range = createRange(1, 5); + + range.step(6, closure); + assertEquals("wrong number of calls to closure", 1, callLog.size()); + assertEquals("wrong value", createValue(1), callLog.get(0)); + + final List stepList = range.step(6); + assertEquals("wrong number of values in result", 1, stepList.size()); + assertEquals("wrong value", createValue(1), callLog.get(0)); + } + + /** + * Tests stepping through a range by one. + */ + public void testStepByOne() { + final Range range = createRange(1, 5); + final List result = range.step(1); + + assertEquals("wrong number of calls", 5, result.size()); + final Iterator iter = result.iterator(); + for (int i = 1; i <= 5; i++) { + assertEquals("incorrect value in result", createValue(i), iter.next()); + } + } + + /** + * Tests stepping through a range by two. + */ + public void testStepByTwo() { + final Range range = createRange(1, 5); + final List result = range.step(2); + + assertEquals("wrong number of calls", 3, result.size()); + final Iterator iter = result.iterator(); + for (int i = 1; i <= 5; i += 2) { + assertEquals("incorrect value in result", createValue(i), iter.next()); + } + } + + /** + * Tests getting the size. + */ + public void testSize() { + Range range = createRange(0, 10); + assertEquals("Size of " + range, 11, range.size()); + range = createRange(0, 1); + assertEquals("Size of " + range, 2, range.size()); + range = createRange(0, 0); + assertEquals("Size of " + range, 1, range.size()); + } + + /** + * Tests asking for an index outside of the valid range + */ + public void testGetOutOfRange() { + Range r = createRange(10, 20); + + try { + r.get(-1); + fail("Should have thrown IndexOutOfBoundsException"); + } + catch (IndexOutOfBoundsException e) { + assertTrue("expected exception thrown", true); + } + try { + r.get(11); + fail("Should have thrown IndexOutOfBoundsException"); + } + catch (IndexOutOfBoundsException e) { + assertTrue("expected exception thrown", true); + } + + } + + /** + * Tests getting a sub list. + */ + public void testSubList() { + Range range = createRange(0, 5); + + List subList = range.subList(2, 4); + assertEquals("size", 2, subList.size()); + + assertTrue("sublist not a range", subList instanceof Range); + Range subListRange = (Range) subList; + + assertEquals("from", createValue(2), subListRange.getFrom()); + assertEquals("to", createValue(3), subListRange.getTo()); + + subList = range.subList(0, 6); + assertEquals("size", 6, subList.size()); + + assertTrue("sublist not a range", subList instanceof Range); + subListRange = (Range) subList; + + assertEquals("from", createValue(0), subListRange.getFrom()); + assertEquals("to", createValue(5), subListRange.getTo()); + } + + /** + * Tests creating a sub list with a negative "from" index. + */ + public void testSubListNegativeFrom() { + try { + final Range range = createRange(1, 5); + range.subList(-1, 3); + fail("accepted sub list with negative index"); + } + catch (IndexOutOfBoundsException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests creating a sub list with an out of range "to" index. + */ + public void testSubListOutOfRangeTo() { + try { + final Range range = createRange(0, 3); + range.subList(0, 5); + fail("accepted sub list with invalid 'to'"); + } + catch (IndexOutOfBoundsException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests creating a sub list with "from" grater than "to." + */ + public void testSubListFromGreaterThanTo() { + try { + final Range range = createRange(1, 5); + range.subList(3, 2); + fail("accepted sub list with 'from' greater than 'to'"); + } + catch (IllegalArgumentException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests creating an empty sub list. + */ + public void testEmptySubList() { + final Range range = createRange(1, 5); + + List subList = range.subList(0, 0); + assertEquals("wrong number of elements in sub list", 0, subList.size()); + + subList = range.subList(2, 2); + assertEquals("wrong number of elements in sub list", 0, subList.size()); + } + + /** + * Tests iterating over a non-reversed range. + */ + public void testIterate() { + final Range range = createRange(1, 5); + int next = 1; + final Iterator iter = range.iterator(); + while (iter.hasNext()) { + final Object value = iter.next(); + assertEquals("wrong next value", createValue(next++), value); + } + assertEquals("wrong number of elements in iteration", 6, next); + if (expectNullFromExhausted) { + assertNull("got element after iterator finished", iter.next()); + } else { + try { + iter.next(); + fail("successfully got element from exhausted iterator"); + } catch(NoSuchElementException ignore) { + } + } + } + + /** + * Tests removing an element from the range using an iterator (not supported). + */ + public void testRemoveFromIterator() { + final Range range = createRange(1, 5); + + try { + final Iterator iter = range.iterator(); + iter.remove(); + fail("successfully removed an element using an iterator"); + } + catch (UnsupportedOperationException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests iterating over a reversed range. + */ + public void testIterateReversed() { + final Range range = createRange(5, 1); + int next = 5; + for (Object value : range) { + assertEquals("wrong number", createValue(next--), value); + } + assertEquals("wrong number of elements in iteration", 0, next); + } + + /** + * Tests creating an <code>IntRange</code> with from > to. + */ + public void testFromGreaterThanTo() { + final int from = 9; + final int to = 0; + final Range range = createRange(from, to); + + assertTrue("range not reversed", range.isReverse()); + + // make sure to/from are swapped + assertEquals("from incorrect", createValue(to), range.getFrom()); + assertEquals("to incorrect", createValue(from), range.getTo()); + + assertEquals("wrong size", 10, range.size()); + + assertEquals("wrong first element", createValue(9), range.get(0)); + assertEquals("wrong last element", createValue(0), range.get(9)); + } + + /** + * Tests creating an <code>IntRange</code> with from == to. + */ + public void testFromEqualsTo() { + final Range range = createRange(5, 5); + + assertFalse("range reversed", range.isReverse()); + assertEquals("wrong size", 1, range.size()); + } + + /** + * Tests creating an <code>IntRange</code> with from < to. + */ + public void testFromLessThanTo() { + final int from = 1; + final int to = 4; + final Range range = createRange(from, to); + + assertFalse("range reversed", range.isReverse()); + + assertEquals("to incorrect", createValue(from), range.getFrom()); + assertEquals("from incorrect", createValue(to), range.getTo()); + + assertEquals("wrong size", 4, range.size()); + } + + /** + * Making a range equal a list is not actually possible, since list.equals(range) will not evaluate to + * <code>true</code> and <code>equals</code> should be symmetric. + */ + public void testEqualsList() { + final List list = new ArrayList(); + list.add(createValue(1)); + list.add(createValue(2)); + + final Range range = createRange(1, 2); + + // cast to Object to test routing through equals(Object) + assertTrue("range does not equal list", range.equals((Object) list)); + assertTrue("list does not equal range", list.equals(range)); + assertEquals("hash codes are not equal", range.hashCode(), list.hashCode()); + + // compare lists that are the same size but contain different elements + list.set(0, createValue(3)); + assertFalse("range equals list", range.equals(list)); + assertFalse("list equals range", list.equals(range)); + assertFalse("hash codes are equal", range.hashCode() == list.hashCode()); + + // compare a list longer than the range + list.set(0, createValue(1)); + list.add(createValue(3)); + assertFalse("range equals list", range.equals(list)); + assertFalse("list equals range", list.equals(range)); + assertFalse("hash are equal", range.hashCode() == list.hashCode()); + + // compare a list shorter than the range + list.remove(2); + list.remove(1); + assertFalse("range equals list", range.equals(list)); + assertFalse("list equals range", list.equals(range)); + assertFalse("hash are equal", range.hashCode() == list.hashCode()); + } + + /** + * Tests comparing {@link Range} to an object that is not a {@link Range}. + */ + public void testEqualsNonRange() { + final Range range = createRange(1, 5); + assertFalse("range equal to string", range.equals("hello")); + } + + /** + * Tests comparing a {@link Range} cast to an {@link Object} + */ + public void testEqualsRangeAsObject() { + final Range range1 = createRange(1, 5); + final Range range2 = createRange(1, 5); + assertTrue("ranges not equal", range1.equals((Object) range2)); + } + + /** + * Tests comparing two {@link Range}s to each other. + */ + public void testEqualsRange() { + final Range range1 = createRange(1, 5); + Range range2 = createRange(1, 5); + assertTrue("ranges not equal", range1.equals((Object) range2)); + assertTrue("ranges not equal", range2.equals((Object) range1)); + assertEquals("hash codes not equal", range1.hashCode(), range2.hashCode()); + + range2 = createRange(0, 5); + assertFalse("ranges equal", range1.equals((Object) range2)); + assertFalse("ranges equal", range2.equals((Object) range1)); + assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); + + range2 = createRange(1, 6); + assertFalse("ranges equal", range1.equals((Object) range2)); + assertFalse("ranges equal", range2.equals((Object) range1)); + assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); + + range2 = createRange(0, 6); + assertFalse("ranges equal", range1.equals((Object) range2)); + assertFalse("ranges equal", range2.equals((Object) range1)); + assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); + + range2 = createRange(2, 4); + assertFalse("ranges equal", range1.equals((Object) range2)); + assertFalse("ranges equal", range2.equals((Object) range1)); + assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); + + range2 = createRange(5, 1); + assertFalse("ranges equal", range1.equals((Object) range2)); + assertFalse("ranges equal", range2.equals((Object) range1)); + assertFalse("hash codes equal", range1.hashCode() == range2.hashCode()); + } + + /** + * Tests <code>toString</code> and <code>inspect</code> + */ + public void testToStringAndInspect() { + Range range = createRange(1, 5); + String expected = range.getFrom() + ".." + range.getTo(); + assertEquals("wrong string representation", expected, range.toString()); + assertEquals("wrong string representation", expected, range.inspect()); + + range = createRange(5, 1); + expected = range.getTo() + ".." + range.getFrom(); + assertEquals("wrong string representation", expected, range.toString()); + assertEquals("wrong string representation", expected, range.inspect()); + } + + /** + * Tests <code>getFrom</code> and <code>getTo</code>. + */ + public void testGetFromAndTo() { + final int from = 1, to = 5; + final Range range = createRange(from, to); + + assertEquals("wrong 'from' value", createValue(from), range.getFrom()); + assertEquals("wrong 'to' value", createValue(to), range.getTo()); + } + + /** + * Tests comparing a {@link Range} to <code>null</code>. + */ + public void testEqualsNull() { + final Range range = createRange(1, 5); + assertFalse("range equal to null", range.equals(null)); + assertFalse("range equal to null Object", range.equals((Object) null)); + assertFalse("range equal to null Range", range.equals((Range) null)); + assertFalse("range equal to null List", range.equals((List) null)); + } + + /** + * Tests attempting to add a value to a range. + */ + public void testAddValue() { + try { + final Range range = createRange(1, 5); + range.add(createValue(20)); + fail("expected exception not thrown"); + } + catch (UnsupportedOperationException e) { + assertTrue("expected exception thrown", true); + } + } + + /** + * Tests attempting to remove a value from a range. + */ + public void testRemoveValue() { + try { + final Range range = createRange(1, 5); + range.remove(0); + fail("expected exception not thrown"); + } + catch (UnsupportedOperationException e) { + assertTrue("expected exception thrown", true); + } + } + + private void doTestContains(int from, int to, Range range) { + // test integers + assertTrue("missing 'from' value", range.contains(createValue(from))); + assertTrue("missing 'to' value", range.contains(createValue(to))); + assertTrue("missing mid point", range.contains(createValue((from + to) / 2))); + assertFalse("contains out of range value", range.contains(createValue(from - 1))); + assertFalse("contains out of range value", range.contains(createValue(to + 1))); + + // test ranges + assertTrue("missing same range", range.containsAll(createRange(from, to))); + assertTrue("missing same range", range.containsAll(createRange(to, from))); + assertTrue("missing strict subset", range.containsAll(createRange(from + 1, to - 1))); + assertTrue("missing subset", range.containsAll(createRange(from, to - 1))); + assertTrue("missing subset", range.containsAll(createRange(from + 1, to))); + assertFalse("contains non-subset", range.containsAll(createRange(from - 1, to))); + assertFalse("contains non-subset", range.containsAll(createRange(from, to + 1))); + assertFalse("contains non-subset", range.containsAll(createRange(from - 2, from - 1))); + + // ranges don't contain other ranges + assertFalse("range contains sub-range", range.contains(createRange(from + 1, to - 1))); + + // test list + final List list = new ArrayList(); + list.add(createValue(from)); + list.add(createValue(to)); + assertTrue("missing strict subset", range.containsAll(list)); + + // test non-integer number + assertFalse("contains Float", range.contains(new Float((to + from) / 2.0 + 0.3))); + } + + /** + * Tests whether the range contains a {@link Comparable} object which is not comparable with a {@link Number}. + */ + public void testContainsIncompatibleComparable() { + final Range range = createRange(1, 5); + assertFalse("range contains string", range.contains("hello")); + assertFalse("range contains string", range.contains("1")); + } + + /** + * Tests whether the range contains a non-comparable object. + */ + public void testContainsNonComparable() { + final Range range = createRange(1, 5); + assertFalse("range contains hash map", range.contains(new HashMap())); + } + + /** + * Tests whether a {@link Range} contains another {@link Range} or a specific integer. + */ + public void testContains() { + final int from = 1, to = 5; + doTestContains(from, to, createRange(from, to)); + doTestContains(from, to, createRange(to, from)); + } + + /** + * Tests <code>get</code> from a reversed range. + */ + public void testGetFromReversedRange() { + final Range range = createRange(5, 1); + + for (int i = 0; i < 5; i++) { + assertEquals("wrong element at position " + i, createValue(5 - i), range.get(i)); + } + } + + /** + * Tests getting values from the range. + */ + public void testGet() { + final Range range = createRange(10, 20); + for (int i = 0; i <= 10; i++) { + assertEquals("Item at index: " + i, createValue(i + 10), range.get(i)); + } + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/ShortObjectRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/ShortObjectRangeTest.java b/src/test/groovy/lang/ShortObjectRangeTest.java new file mode 100644 index 0000000..a2d0ccc --- /dev/null +++ b/src/test/groovy/lang/ShortObjectRangeTest.java @@ -0,0 +1,46 @@ +/* + * 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.0 (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.0 + * + * 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 groovy.lang; + +/** + * Tests {@link ObjectRange}s of {@link Short}s. + */ +public class ShortObjectRangeTest extends NumberRangeTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + expectNullFromExhausted = true; + } + + /** + * {@inheritDoc} + */ + @Override + protected Range createRange(int from, int to) { + return new ObjectRange(new Short((short) from), new Short((short) to)); + } + + /** + * {@inheritDoc} + */ + @Override + protected Comparable createValue(int value) { + return new Integer(value); + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/8311b23a/src/test/groovy/lang/ShortRangeTest.java ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/ShortRangeTest.java b/src/test/groovy/lang/ShortRangeTest.java deleted file mode 100644 index c78b563..0000000 --- a/src/test/groovy/lang/ShortRangeTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.0 (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.0 - * - * 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 groovy.lang; - - -/** - * Tests {@link ObjectRange}s of {@link Short}s. - * - * @author $Author$ - */ -public class ShortRangeTest extends NumberRangeTest { - - /** - * {@inheritDoc} - */ - protected Range createRange(int from, int to) { - return new ObjectRange(new Short((short) from), new Short((short) to)); - } - - /** - * {@inheritDoc} - */ - protected Comparable createValue(int value) { - return new Integer(value); - } -}
