Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ranges bigger than Int.MaxValue are not always rejected, result in empty NumericRange #4308

Closed
scabug opened this issue Mar 2, 2011 · 8 comments
Assignees

Comments

@scabug
Copy link

scabug commented Mar 2, 2011

=== What steps will reproduce the problem ===

Long.MinValue to Long.MaxValue

=== What is the expected behavior? ===

java.lang.IllegalArgumentException: Seqs cannot contain more than Int.MaxValue elements.

(Probably)

=== What do you see instead? ===

scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange()

=== Additional information ===
Very likely related to #3232.

It would be a good idea to think about how to better test these things. I guess this Range bug won't be the last ...

=== What versions of the following are you using? ===

  • Scala: 2.9.0.r24343-b20110224020040
@scabug
Copy link
Author

scabug commented Mar 2, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4308?orig=1
Reporter: @soc

@scabug
Copy link
Author

scabug commented Mar 2, 2011

@dcsobral said:
The maximum collection size is {{Int.MaxValue}} (or maybe {{Int.MaxValue - 1}}?). There are no guarantees for anything over that.

@scabug
Copy link
Author

scabug commented Mar 5, 2011

@soc said:
@dcsobral: Yes, exactly. It should behave like "Int.MinValue to Int.MaxValue".

@scabug
Copy link
Author

scabug commented Mar 5, 2011

@dcsobral said:
Replying to [comment:3 soc]:

@dcsobral: Yes, exactly. It should behave like "Int.MinValue to Int.MaxValue".

That's more than the maximum allowed number of elements.

@scabug
Copy link
Author

scabug commented Mar 6, 2011

@soc said:

That's more than the maximum allowed number of elements.
Yes. So is Long.MinValue to Long.MaxValue.

Both should behave the same and throw an exception instead of returning a wrong result silently.

@scabug
Copy link
Author

scabug commented Mar 7, 2011

@soc said:
I just verified it with BigInt too.
The check fails for inclusive ranges where end == -(start+1).

The bug is in NumericRange.count:

193	    val longCount: Long =
194	      if (start == end) { if (isInclusive) 1 else 0 }
195	      else if (end > start != step > zero) 0
196	      else {
197	        val jumps     = toLong((end - start) / step) // <-------
198	        val remainder = toLong((end - start) % step)
199	       
200	        if (!isInclusive && zero == remainder) jumps
201	        else jumps + 1L
202	      }
203	   
204	    if (longCount > scala.Int.MaxValue || longCount < 0L)
205	      throw new IllegalArgumentException("Seqs cannot contain more than Int.MaxValue elements.")

@scabug
Copy link
Author

scabug commented Mar 8, 2011

@ingoem said:
Just like #4321, this one seems for Paul.

@scabug
Copy link
Author

scabug commented Mar 19, 2011

@paulp said:
(In r24508) Some boundary conditions in range. Also bit the bullet on getting
infix implicits to Integral and Fractional. As a bonus this patch
knocked 10,000 long boxings off a specialized test. Who knew.
Closes #4308, #4321, review by community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants