BitterSuite already has a mechanism for limiting the amount of real time a test takes. However, sometimes a finer grain of control is desired on multiuser systems; this module will track how long an expression takes to evaluate in terms of CPU time. If the expression evaluates quicker, the value it evaluates to is produced; otherwise, an error message string is produced.
Example code for this and a few test cases follow:
#lang scheme/base (provide cpu-time-eval) (define DEFAULT_TIMEOUT 100) (define (cpu-time-do-eval fn #:time-limit (timeout DEFAULT_TIMEOUT) . args) (let-values ([(result cpu-time real-time gc-time) (time-apply fn args)]) (if (> cpu-time timeout) (format "Error: Timeout of ~a exceeded; total time ~a" timeout cpu-time) result))) (define-syntax cpu-time-eval (syntax-rules () ((_ expr) (cpu-time-do-eval (lambda () expr))) ((_ expr timeout) (cpu-time-do-eval (lambda () expr) #:time-limit timeout)))) #| ; Should produce 7 (cpu-time-eval (+ 4 3)) ; Should produce failure messages (cpu-time-eval (+ 4 3) -1) (cpu-time-eval (build-list 1000000 values)) |#
-- TerryVaskor - 16 Mar 2009