sholsapp
sholsapp

Reputation: 16070

PLT Racket test cases for multiple values

I can't seem to test a function I wrote in PLT Racket using the test-engine/racket-tests package.

The code is listed below. It returns multiple values (not sure why they don't call them tuples).

(define (euclid-ext a b)
  (cond
    [(= b 0) (values a 1 0)]
    [else (let-values ([(d x y) (euclid-ext b (modulo a b))])
            (values d y (- x (* (quotient a b) y))))]
    ))

The problem is testing it using the following format. Here are a few that I have tried.

(check-expect (values (euclid-ext 99 78)) (values 3 -11 14))
(check-expect (euclid-ext 99 78) (values 3 -11 14))
(check-expect (list (euclid-ext 99 78)) (list 3 -11 14))

Right now, this produces the error context expected 1 value, received 3 values: 3 -11 14. No matter how I try to work this (with lists, values, no values, etc) I cannot get this test case to evaluate successfully.

Upvotes: 0

Views: 567

Answers (2)

Eli Barzilay
Eli Barzilay

Reputation: 29546

The test-engine library is intended for student code, so it doesn't deal with multiple values (which most courses don't deal with). Something like the Rackunit library is more appropriate for such cases.

Upvotes: 3

clstrfsck
clstrfsck

Reputation: 14829

It looks like the test framework won't accept values. I think you will find it less painful to use a list for the return value.

However, if you really want to do things this way you can convert values to a list using call-with-values something like this:

(call-with-values (lambda () (values 1 2 3)) list)

So a test would be something like this:

(check-expect (call-with-values (lambda () (euclid-ext 99 78)) list)
              (list 3 -11 14))

Upvotes: 1

Related Questions