Learning tests for Scheme

test-alist-ref: pass

(define (test-alist-ref _)
  (let ((alst '((a 3) (b 2))))
    (⊦= '(3) (alist-ref 'a alst))
    (⊦= '(2) (alist-ref 'b alst))
    (⊦= #f (alist-ref 'c alst))))
((eta 0.0) (memory #(6291456 1367376 1048576)) (stdout "") (stderr ""))

test/len: pass

(define (test/len _)
  (let ((my-strlen
          (foreign-lambda*
            int
            ((scheme-object cons))
            "C_return(C_header_size(cons));")))
    (⊦= 2 (my-strlen (cons 1 '())))
    (⊦= 2 (my-strlen (cons 1 (cons 2 (cons 3 '())))))
    (⊦= 4 (my-strlen "hello world"))))
((eta 0.0) (memory #(6291456 1368352 1048576)) (stdout "") (stderr ""))

test/unquote: pass

Very interesting test about quasiquotation Quasiquotation, Chicken Scheme manual. : it shows how unquote can be used in a quasiquotation pattern in the cdr slot.

(define (test/unquote _) (let1 (a '(3)) (⊦= (cons 1 a) `(1 unquote a))))
((eta 0.0) (memory #(6291456 1369952 1048576)) (stdout "") (stderr ""))

test/c_callback: pass

(define (test/c_callback _)
  (let ((witness (gensym))
        (my-strlen
          (foreign-safe-lambda*
            scheme-object
            ((scheme-object f))
            "\t\t\t\t\t  C_word res = C_callback(f, 0);\n\t\t\t\t\t  printf(\"from within a safe lambda\\n\");\n\t\t\t\t\t  C_return (res);")))
    (⊦= witness (car (my-strlen (lambda () (list witness 4)))))))
((eta 0.0) (memory #(6291456 1371104 1048576)) (stdout "") (stderr ""))

test/foreign-safe-lambda*/allocate_string/inline: pass

(define (test/foreign-safe-lambda*/allocate_string/inline _)
  (let1 (allocate_string
          (foreign-safe-lambda*
            scheme-object
            ()
            "                            char* str = \"hello world\";\n                            C_word length = strlen(str);\n                            C_word* ptr = C_alloc (C_SIZEOF_STRING (length));\n                            C_word res = C_string (&ptr, length, str);\n                            C_return (res);"))
        (⊦= "hello world" (allocate_string))))
((eta 0.0) (memory #(6291456 1372328 1048576)) (stdout "") (stderr ""))

test/foreign-primitive/allocate-string: pass

An example of foreign-primitive foreign-primitive, Chicken Scheme manual. that allocates a string in C and returns it to Scheme. The C code is inlined in the Scheme source code.

(define (test/foreign-primitive/allocate-string _)
  (let1 (allocate_string
          (foreign-primitive scheme-object () "C_my_allocate_string(C_k);"))
        (⊦= "hello world" (allocate_string))))
((eta 0.0) (memory #(6291456 1373976 1048576)) (stdout "") (stderr ""))

test/foreign-safe-lambda/list-walk: pass

(define (test/foreign-safe-lambda/list-walk _)
  (let1 (list-walk
          (foreign-safe-lambda
            scheme-object
            "C_list_walk"
            scheme-object
            scheme-object))
        (⊦= '(1 2 3) (list-walk '(1 2 3) identity))))
((eta 0.0) (memory #(6291456 1375168 1048576)) (stdout "") (stderr ""))

test/foreign/callout-callin: pass

This test shows how to call out to C code that in turn calls back into Scheme code. The C code is in the file bar.c, and the function called from Scheme is callout. The function that is called back from C into Scheme is callin. Taken from the Chicken Scheme manual An example for simple calls to foreign code involving callbacks, Chicken Scheme manual..

(define (test/foreign/callout-callin _)
  (define callout (foreign-safe-lambda int "callout" int int int))
  (define-external
    (callin (scheme-object xyz))
    int
    (print "This is 'callin': " xyz)
    123)
  (⊦= 123 (callout 1 2 3)))
((eta 0.0)
 (memory #(6291456 1376992 1048576))
 (stdout "This is 'callin': (1 2 3)\n")
 (stderr ""))

test/string/ref: pass

(define (test/string/ref _) (⊦ equal? #\o (string-ref "hello" 4)))
((eta 0.0) (memory #(6291456 1378144 1048576)) (stdout "") (stderr ""))

test/string/first: pass

(define (test/string/first _) (⊦ equal? #\h (first/string "hello")))
((eta 0.0) (memory #(6291456 1379304 1048576)) (stdout "") (stderr ""))

test/string/last: pass

(define (test/string/last _) (⊦ equal? #\o (last/string "hello")))
((eta 0.0) (memory #(6291456 1380464 1048576)) (stdout "") (stderr ""))

test/string/take-right: pass

(define (test/string/take-right _)
  (⊦ equal? "o" (string-take-right "hello" 1)))
((eta 0.0) (memory #(6291456 1381624 1048576)) (stdout "") (stderr ""))

test-null-eq?: pass

(define (test-null-eq? _) (⊨ (eq? '() '())))
((eta 0.0) (memory #(6291456 1382768 1048576)) (stdout "") (stderr ""))

test-last-syntax: pass

(define (test-last-syntax _)
  (define-syntax-rule (var-last body ... last) last)
  (⊦ equal? 'a (var-last 'a))
  (⊦ equal? 7 (var-last 'a (+ 3 4))))
((eta 0.0) (memory #(6291456 1383928 1048576)) (stdout "") (stderr ""))

test-syntax-with-_: pass

(define (test-syntax-with-_ _)
  (define-syntax
    with-underscore
    (syntax-rules () ((_ body ...) (list '_ body ...))))
  (⊦ equal? '(_ a) (with-underscore 'a)))
((eta 0.0) (memory #(6291456 1385088 1048576)) (stdout "") (stderr ""))

test-syntax-with-literal: pass

(define (test-syntax-with-literal _)
  (define where +)
  (define-syntax-rule (literal where) (my-macro a where b) '(a b where))
  (⊦ equal? '(c d where) (my-macro c where d)))
((eta 0.0) (memory #(6291456 1386264 1048576)) (stdout "") (stderr ""))

test-syntax-with-literal-and-ellipses: pass

(define (test-syntax-with-literal-and-ellipses _)
  (define-syntax-rule (literal :) (my-macro a ... : b ...) '((a ...) (b ...)))
  (⊦ equal? '((a b c) (d e)) (my-macro a b c : d e)))
((eta 0.0) (memory #(6291456 1387456 1048576)) (stdout "") (stderr ""))

test-vec-syntax: pass

(define (test-vec-syntax _)
  (define-syntax-rule
    (v-aux (v #(x z ... y)) body ...)
    (let1 (v (list z ...)) body ...))
  (⊦ equal? '(0 2) (v-aux (v #(1 2 3)) (cons 0 v))))
((eta 0.0) (memory #(6291456 1388608 1048576)) (stdout "") (stderr ""))
Categories: scheme 

See also

The (aux anthropic) module The Anthropic Messages API, offline The (aux cml) module Events, threads, synchronous variables, the library and I/O The (aux hakaru10) modules Random sources, distributions, the language, exact inference and examples The (aux kanren) modules Arithmetic, dates, presentation of answers and a union-find miniKanren The (aux games dice-of-doom) module Dice of Doom The (aux machine) modules Environments, the SC machine and the SECD machine The (aux schemer seasoned) module The Seasoned Schemer The (aux continuation) modules Classic operators, escapers, amb and Collatz sequences The (aux category) modules Monads: list, maybe, probability, ziplist, difference lists and state The (aux timsort) module Sorting and the trace buffer The (aux fds) modules Functional queues and union-find The (aux sxml) module Rendering SXML to HTML The (aux variable) module Logic variables introduced by fresh The (aux tabling) module Memoization and tabled definitions The (aux commons) module Learning tests for CHICKEN Scheme Promises delay and make-promise The Ackermann function On unary numbers, as lists Concurrent ML for CHICKEN Scheme Porting SML/NJ CML as the (aux cml) library CHICKEN Scheme Versions and implementation notes The SBRAL data structure Skew Binary Random-Access Lists Pattern matching Scheme macrology: a generalized pattern matcher The (aux hansei) modules Inference, random sources, lazy lists, logic programming and example models The (aux nondeterministic) module The (aux stream) modules Streams, streams after SICP, and sequences and power series The (aux unittest) module Bootstrapping a unittest framework Scheme lang: Chez, 48 and Chicken Some docs, refs and containers, respectively. The Reasoned Schemer miniKanren, microKanren and some implementations Aggregation predicates for µkanren Groups, window functions, sets and enumerations miniKanren, live and untagged Quine generation via relational interpreters Pure, declarative, and constructive arithmetic relations A declarative pearl Final shift for call/cc Direct implementation of shift and reset Call with Current Continuation Patterns Cogen in six lines The Seasoned Schemer The Little Schemer Adding generic functions to Scheme Handling control