views:

41

answers:

2

Hello,

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.

+1  A: 

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))
spong
Thanks for the suggestion, I'll try it with lists. I did this because it was convenient to return a tuple. Can you do something similar with lists? But thank you for the solution, anyway. :)
gnucom
I'm not sure if this is an answer to the question you are asking, but you can return a list of three values using something similar to: `(define (example) (list 3 -11 14))`. So instead of returning `(values d y (- x (* (quotient a b) y)))` you could return `(list d y ...)`.
spong
+3  A: 

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.

Eli Barzilay
Thanks for the recommendation. I'll use that library next time.
gnucom