Category talk:Wren-check: Difference between revisions
Content deleted Content added
→Source code: Added Benchmark class. |
→Source code: Fixed bug in Check.char method. |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1:
===Source code===
<
/*
Line 15:
if (name == "" || name == null) name = "Value"
Fiber.abort("'%(name)' must be %(desc), have '%(value)'.")
}
static isSafeInt_(value) {
return (value is Num) && value.isInteger && value.abs <= Num.maxSafeInteger
}
Line 102 ⟶ 106:
}
static posInt(name, value)
static nonNegInt(name, value)
static
}
static posSafeInt(name, value) {
if (!(isSafeInt_(value) && value > 0)) {
abort_(name, value, "a positive 'safe' integer")
}
}
static nonNegSafeInt(name, value) {
if (!(isSafeInt_(value) && value >= 0)) {
abort_(name, value, "a non-negative 'safe' integer")
}
}
static nonZeroSafeInt(name, value) {
if (!(
abort_(name, value, "a non-zero 'safe' integer")
}
}
Line 153 ⟶ 172:
int("max", max, min)
char(name, value)
if (value.codePoints[0] < min
abort_(name, value, "a character between '%(min)' and '%(max)'")
}
Line 297 ⟶ 316:
static ok(condition) { ok(condition, "Condition is not met.") }
}
/*
SafeInt is a class containing self-explanatory static methods to check
integer arithmetic is 'safe' i.e. within the ± 2^53 limit.
*/
class SafeInt {
static add(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i + j
Check.safeInt("Result", res)
return res
}
static sub(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i - j
Check.safeInt("Result", res)
return res
}
static mul(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i * j
Check.safeInt("Result", res)
return res
}
static pow(i, j) {
Check.safeInt("Argument", i)
Check.safeInt("Argument", j)
var res = i.pow(j)
Check.safeInt("Result", res)
return res
}
}
/* SafeInts checks the safety of arithmetic operations for a list of integers. */
class SafeInts {
static sum(a) { a.reduce(0) { |acc, x| SafeInt.add(acc, x) } }
static prod(a) { a.reduce(1) { |acc, x| SafeInt.mul(acc, x) } }
}
Line 353 ⟶ 416:
return [best, mean, worst]
}
}</
|