如何正确使用TDD来实现数值方法? [英] How to use TDD correctly to implement a numerical method?

查看:118
本文介绍了如何正确使用TDD来实现数值方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用测试驱动开发来实现我的信号处理库.但我有点怀疑:假设我正在尝试实现一个正弦方法(我不是):

I am trying to use Test Driven Development to implement my signal processing library. But I have a little doubt: Assume I am trying to implement a sine method (I'm not):

  1. 编写测试(伪代码)

  1. Write the test (pseudo-code)

assertEqual(0, sine(0))

  • 编写第一个实现

  • Write the first implementation

    function sine(radians)
        return 0
    

  • 第二次测试

  • Second test

    assertEqual(1, sine(pi))
    

  • 此时,我应该:

    1. 实现适用于 pi 和其他值的智能代码,或
    2. 实现仅适用于 0 和 pi 的最愚蠢的代码?

    如果选择第二个选项,我什么时候可以跳转到第一个选项?我最终将不得不这样做...

    If you choose the second option, when can I jump to the first option? I will have to do it eventually...

    推荐答案

    此时,我应该:

    1. 实现可以在两个简单测试之外工作的真实代码?

    1. implement real code that will work outside the two simple tests?

    实现更愚蠢的代码,只适用于两个简单的测试?

    implement more dumbest code that will work only for the two simple tests?

    都没有.我不确定您从哪里得到一次只编写一个测试"的方法,但这肯定是一种缓慢的方法.

    Neither. I'm not sure where you got the "write just one test at a time" approach from, but it sure is a slow way to go.

    重点是编写清晰的测试并使用清晰的测试来设计您的程序.

    The point is to write clear tests and use that clear testing to design your program.

    因此,编写足够的测试来实际验证正弦函数.两个测试显然是不够的.

    So, write enough tests to actually validate a sine function. Two tests are clearly inadequate.

    在连续函数的情况下,您最终必须提供已知良好值的表.为什么要等?

    In the case of a continuous function, you have to provide a table of known good values eventually. Why wait?

    然而,测试连续函数有一些问题.你不能遵循愚蠢的 TDD 过程.

    However, testing continuous functions has some problems. You can't follow a dumb TDD procedure.

    您无法测试 0 到 2*pi 之间的所有浮点值.您无法测试一些随机值.

    You can't test all floating-point values between 0 and 2*pi. You can't test a few random values.

    在连续函数的情况下,严格的、不经思考的 TDD"是行不通的.这里的问题是你知道你的正弦函数实现将基于一堆对称性.您必须根据您使用的那些对称规则进行测试.虫子藏在裂缝和角落里.边缘情况和极端情况是实现的一部分,如果您不假思索地遵循 TDD,则无法对其进行测试.

    In the case of continuous functions, a "strict, unthinking TDD" doesn't work. The issue here is that you know your sine function implementation will be based on a bunch of symmetries. You have to test based on those symmetry rules you're using. Bugs hide in cracks and corners. Edge cases and corner cases are part of the implementation and if you unthinkingly follow TDD you can't test that.

    但是,对于连续函数,您必须测试实现的边缘和角落情况.

    However, for continuous functions, you must test the edge and corner cases of the implementation.

    这并不意味着 TDD 已损坏或不足.它说如果不考虑您的真正目标是什么,那么盲目地致力于测试优先"是行不通的.

    This doesn't mean TDD is broken or inadequate. It says that slavish devotion to a "test first" can't work without some thinking about what you real goal is.

    这篇关于如何正确使用TDD来实现数值方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆