Category talk:Wren-check: Difference between revisions

→‎Source code: Fixed bug in Check.char method.
(→‎Source code: Added an overload for Check.type method to cater for custom error messages.)
(→‎Source code: Fixed bug in Check.char method.)
 
(8 intermediate revisions by the same user not shown)
Line 1:
===Source code===
 
<langsyntaxhighlight ecmascriptlang="wren">/* Module "check.wren" */
 
/*
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) { int(name, value, 1) }
static nonNegInt(name, value) { int(name, value, 0) }
 
static safeInt(name, value) { int(name, value, Num.minSafeInteger, Num.maxSafeInteger) }
static posSafeIntsafeInt(name, value) { int(name, value, 1, Num.maxSafeInteger) }
static nonNegSafeInt(name, if (!isSafeInt_(value)) { intabort_(name, value, 0,"a Num.maxSafeInteger)'safe' }integer")
}
 
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 (!(safeIntisSafeInt_(value) && value != 0)) abort_(name, value, "a non-zero 'safe' integer"){
abort_(name, value, "a non-zero 'safe' integer")
}
}
 
Line 153 ⟶ 172:
int("max", max, min)
char(name, value)
if (value.codePoints[0] < min.codePoints[0] || value.codePoints[0] > max.codePoints[0]) {
abort_(name, value, "a character between '%(min)' and '%(max)'")
}
Line 297 ⟶ 316:
 
static ok(condition) { ok(condition, "Condition is not met.") }
}
}</lang>
 
/*
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) } }
}
 
/*
Benchmark is a class containing static methods to report
how fast a piece of code runs over a given number of iterations.
*/
class Benchmark {
// Private helper method to format results to 3 decimal places.
static fmt_(s) {
s = s.toString
var len = s.count
var ix = s.indexOf(".")
if (ix == len - 4) return s
if (ix == len - 3) return s + "0"
if (ix == len - 2) return s + "00"
if (ix == -1) return s + ".000"
return s[0..ix+3]
}
 
// Returns how fast 'fn' runs (best, mean, worst) over 'iter' iterations
// in milliseconds rounded to 3 decimal places.
// If 'print' is true, prints the results to the terminal in a fixed format.
static run(name, iter, print, fn) {
Check.str("Name", name, 1)
Check.posInt("Iterations", iter)
Check.func("Function", fn, 0)
var best
var worst
var sum = 0
for (i in 1..iter) {
var start = System.clock
fn.call()
var time = System.clock - start
sum = sum + time
if (i == 1) {
best = time
worst = time
} else if (time < best) {
best = time
} else if (time > worst) {
worst = time
}
}
best = (best * 1e6).round/ 1e3
worst = (worst * 1e6).round / 1e3
var mean = (sum/iter * 1e6).round / 1e3
if (print) {
var title = "Running '%(name)' over %(iter) iteration(s):"
System.print(title)
System.print("-" * title.count)
System.print("Best %(fmt_(best)) ms")
System.print("Mean %(fmt_(mean)) ms")
System.print("Worst %(fmt_(worst)) ms\n")
}
return [best, mean, worst]
}
}</syntaxhighlight>
9,476

edits