PLT Racket 多个值的测试用例

发布于 2024-09-24 10:15:12 字数 660 浏览 3 评论 0 原文

我似乎无法使用 test-engine/racket-tests 包测试我在 PLT Racket 中编写的函数。

代码如下所示。它返回多个值(不知道为什么他们不称它们为元组)。

(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))))]
    ))

问题是使用以下格式对其进行测试。以下是我尝试过的一些方法。

(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))

现在,这会产生错误上下文期望 1 个值,收到 3 个值:3 -11 14。无论我如何尝试解决此问题(使用列表、值、无值等),我都无法成功评估此测试用例。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-10-01 10:15:12

test-engine 库适用于学生代码,因此它不处理多个值(大多数课程不处理)。像 Rackunit 库这样的东西更适合这种情况。

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.

染柒℉ 2024-10-01 10:15:12

看起来测试框架不接受值。我认为您会发现使用列表作为返回值会更轻松。

但是,如果您确实想这样做,您可以使用 values 转换为列表="nofollow noreferrer">call-with-values 像这样:

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

所以测试将是这样的:

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文