Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 6EFDB200B26 for ; Mon, 27 Jun 2016 12:12:15 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 6D8CF160A5B; Mon, 27 Jun 2016 10:12:15 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B4B82160A3C for ; Mon, 27 Jun 2016 12:12:14 +0200 (CEST) Received: (qmail 73109 invoked by uid 500); 27 Jun 2016 10:12:13 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 73100 invoked by uid 99); 27 Jun 2016 10:12:13 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Jun 2016 10:12:13 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C2946DFDC3; Mon, 27 Jun 2016 10:12:13 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulk@apache.org To: commits@groovy.apache.org Message-Id: <9fe4cd2730834ae99977ed3eb7c2c7c8@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: groovy git commit: GROOVY-7875: IntRange fail fast on too large a range out by one Date: Mon, 27 Jun 2016 10:12:13 +0000 (UTC) archived-at: Mon, 27 Jun 2016 10:12:15 -0000 Repository: groovy Updated Branches: refs/heads/GROOVY_2_4_X c7ae32bed -> 8725d926c GROOVY-7875: IntRange fail fast on too large a range out by one Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/8725d926 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/8725d926 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/8725d926 Branch: refs/heads/GROOVY_2_4_X Commit: 8725d926c06f004d2f3cae3069243eb9917e0d35 Parents: c7ae32b Author: paulk Authored: Mon Jun 27 20:08:14 2016 +1000 Committer: paulk Committed: Mon Jun 27 20:11:48 2016 +1000 ---------------------------------------------------------------------- src/main/groovy/lang/IntRange.java | 24 ++++++++++++------------ src/test/groovy/lang/IntRangeTest.groovy | 23 +++++++++++------------ 2 files changed, 23 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/8725d926/src/main/groovy/lang/IntRange.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/IntRange.java b/src/main/groovy/lang/IntRange.java index 5282a7c..fbb8bc9 100644 --- a/src/main/groovy/lang/IntRange.java +++ b/src/main/groovy/lang/IntRange.java @@ -134,12 +134,7 @@ public class IntRange extends AbstractList implements Range { this.from = from; this.to = to; } - - // size() in the Collection interface returns an integer, so ranges can have no more than Integer.MAX_VALUE elements - Long size = 0L + this.to - this.from; - if (size >= Integer.MAX_VALUE) { - throw new IllegalArgumentException("A range must have no more than " + Integer.MAX_VALUE + " elements but attempted " + size + " elements"); - } + checkSize(); } /** @@ -160,12 +155,7 @@ public class IntRange extends AbstractList implements Range { this.from = from; this.to = to; this.reverse = reverse; - - // size() in the Collection interface returns an integer, so ranges can have no more than Integer.MAX_VALUE elements - Long size = 0L + this.to - this.from; - if (size >= Integer.MAX_VALUE) { - throw new IllegalArgumentException("A range must have no more than " + Integer.MAX_VALUE + " elements but attempted " + size + " elements"); - } + checkSize(); } /** @@ -179,6 +169,16 @@ public class IntRange extends AbstractList implements Range { this.from = from; this.to = to; this.inclusive = inclusive; + this.reverse = false; // range may still be reversed, this value is ignored for inclusive-aware ranges + checkSize(); + } + + private void checkSize() { + // size() in the Collection interface returns an integer, so ranges can have no more than Integer.MAX_VALUE elements + Long size = (long) this.to - this.from + 1; + if (size > Integer.MAX_VALUE) { + throw new IllegalArgumentException("A range must have no more than " + Integer.MAX_VALUE + " elements but attempted " + size + " elements"); + } } /** http://git-wip-us.apache.org/repos/asf/groovy/blob/8725d926/src/test/groovy/lang/IntRangeTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/lang/IntRangeTest.groovy b/src/test/groovy/lang/IntRangeTest.groovy index 15b988d..73c5a5e 100644 --- a/src/test/groovy/lang/IntRangeTest.groovy +++ b/src/test/groovy/lang/IntRangeTest.groovy @@ -20,18 +20,17 @@ package groovy.lang; /** * Provides unit tests for the IntRange class. - * - * @author James Strachan */ class IntRangeTest extends GroovyTestCase { void testCreateTooBigRange() { try { - new IntRange(0, Integer.MAX_VALUE); - fail("too large range accepted"); + assert new IntRange(1, Integer.MAX_VALUE).size() == Integer.MAX_VALUE // biggest allowed + new IntRange(0, Integer.MAX_VALUE) // too big + fail("too large range accepted") } catch (IllegalArgumentException ignore) { - assertTrue("expected exception thrown", true); + assert ignore.message == 'A range must have no more than 2147483647 elements but attempted 2147483648 elements' } } @@ -40,11 +39,11 @@ class IntRangeTest extends GroovyTestCase { */ void testInvalidArgumentsToConstructor() { try { - new IntRange(2, 1, true); - fail("invalid range created"); + new IntRange(2, 1, true) + fail("invalid range created") } catch (IllegalArgumentException ignore) { - assertTrue("expected exception thrown", true); + assertTrue("expected exception thrown", true) } } @@ -59,10 +58,10 @@ class IntRangeTest extends GroovyTestCase { * Tests getting the to and from values as ints. */ void testGetToFromInt() { - final int from = 3, to = 7; - final IntRange range = new IntRange(from, to); - assertEquals("wrong 'from'", from, range.getFromInt()); - assertEquals("wrong 'to'", to, range.getToInt()); + final int from = 3, to = 7 + final IntRange range = new IntRange(from, to) + assertEquals("wrong 'from'", from, range.getFromInt()) + assertEquals("wrong 'to'", to, range.getToInt()) } void test_Step_ShouldNotOverflowForIntegerMaxValue() {