Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Integer-overflow/Scala/integer-overflow.scala
Normal file
14
Task/Integer-overflow/Scala/integer-overflow.scala
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Math.{addExact => ++, multiplyExact => **, negateExact => ~~, subtractExact => --}
|
||||
|
||||
def requireOverflow(f: => Unit) =
|
||||
try {f; println("Undetected overflow")} catch{case e: Exception => /* caught */}
|
||||
|
||||
println("Testing overflow detection for 32-bit unsigned integers")
|
||||
requireOverflow(~~(--(~~(2147483647), 1))) // -(-2147483647-1)
|
||||
requireOverflow(++(2000000000, 2000000000)) // 2000000000 + 2000000000
|
||||
requireOverflow(--(~~(2147483647), 2147483647)) // -2147483647 + 2147483647
|
||||
requireOverflow(**(46341, 46341)) // 46341 * 46341
|
||||
requireOverflow(**(--(~~(2147483647),1), -1)) // same as (-2147483647-1) / -1
|
||||
|
||||
println("Test - Expect Undetected overflow:")
|
||||
requireOverflow(++(1,1)) // Undetected overflow
|
||||
Loading…
Add table
Add a link
Reference in a new issue