gpt4 book ai didi

testing - 无限循环的clojure测试

转载 作者:行者123 更新时间:2023-11-28 21:00:42 27 4
gpt4 key购买 nike

我想编写一个测试(使用 clojure.test)来验证某些示例代码不会导致无限循环。如果我正在测试的代码有问题并且确实会导致无限循环,我希望测试工具报告失败——而不是停止运行并永远循环。 (我假设一种不会溢出堆栈的无限循环。)

有什么办法吗?

最佳答案

虽然在一般情况下你当然不能这样做,但如果你有兴趣解决一个更简单的问题,比如“这个函数是否在最多五秒内完成执行?”那么你可以相对容易地做到这一点。参见,例如,test for the same thing在 Clojure 本身:

(defn sample [& args]
0)

(deftest test-vars-apply-lazily
(is (= 0 (deref (future (apply sample (range)))
1000 :timeout)))
(is (= 0 (deref (future (apply #'sample (range)))
1000 :timeout))))

创建一个做你想做的事情的 future ,然后用超时取消引用那个 future 。

关于testing - 无限循环的clojure测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39212638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com