Skip to content

Instantly share code, notes, and snippets.

@roryokane
Last active December 11, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roryokane/4577394 to your computer and use it in GitHub Desktop.
Save roryokane/4577394 to your computer and use it in GitHub Desktop.
MIT/GNU Scheme basic testing framework – test that function outputs given inputs are equal to expected values.

Here is a testing framework I wrote (for CS 360 class). You can use it to test if your Scheme functions give the correct output. Here’s an example of how you use it:

(load "test-framework.scm")
(load "foo.scm")


(test-with-cases foo
  (list
    '(( 1 1 ) 2)
    '(( 10 5 ) 15)
    '(( 12 2 ) 14)
  ))

test-with-cases takes the function to test and then a list of test cases. A test case is a list with two parts: a list of arguments to call the function with and the desired output. It prints either a message for each failing test case or a message saying that all tests for that function passed.

The code is here, in this Gist. The test framework is in “test-framework.scm”, while the suggested structure for your files is in “foo.scm”, “foo-test.scm”, “bar.scm”, and “bar-test.scm”. “example-test-run.txt” shows how to run your tests.

(load "test-framework.scm")
(load "bar.scm")
(test-with-cases bar
(list
; can’t use quote here – (car '(square)) is not square; square gets de-functionified
(list (list square '(1 2 3) ) '(1 4 9))
(list (list (lambda (x) (+ x 2)) '(0 3 6) ) '(2 5 8))
))
(define (bar a-function a-list)
(map a-function a-list))
$ mit-scheme --load foo-test.scm
[lots of irrelevant info about MIT/GNU Scheme]
;Loading "foo-test.scm"...
; Loading "test-framework.scm"... done
; Loading "foo.scm"... done
All tests for #[compound-procedure 2 foo] passed.
;... done
1 ]=> ^C
Interrupt option (? for help): [press q here]
Moriturus te saluto.
$
(load "test-framework.scm")
(load "foo.scm")
(test-with-cases foo
(list
'(( 1 1 ) 2)
'(( 10 5 ) 15)
'(( 12 2 ) 14)
))
(define (helper-for-foo a-value)
a-value)
(define (foo some-value another-value)
(+ (helper-for-foo some-value) another-value))
(define display-all-on-line
(lambda args
(for-each display args)
(newline)))
(define (list-and list)
(if (null? list)
#t
(and (car list))))
(define (test-with-cases function-to-test test-cases)
(let*
(
(test-statuses
(map
(lambda (test-case) (test-with-case function-to-test test-case))
test-cases))
(all-tests-passed (list-and test-statuses))
)
(if all-tests-passed
(display-all-on-line "All tests for " function-to-test " passed."))))
(define (test-with-case function-to-test test-case)
(let*
(
(input-args (first test-case))
(desired-output (second test-case))
(actual-output (apply function-to-test input-args))
)
(if (equal? desired-output actual-output)
#t
(begin
(display-all-on-line function-to-test " failure for inputs " input-args " - expected " desired-output ", got " actual-output)
#f))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment