Test Drive Development (TDD) Instance - Fibonacca

zhaozj2021-02-16  69

Fibonacci number

As a solution to a reviewer of this book, I provided a number of Fibonacci Cilia using test-driven development. Several reviewers commented that this example makes them Mouton, truly understand how TDD works. However, this example of this example is still not long enough, and the key demonstration of TDD is not enough. If you read the main example of this book, you will be very vague, please take a look here.

??? The first test displays FIB (0) = 0. Implement a constant.

??? Public void testfibonacci () {

?????? assertequals (0, FIB (0));

???}

??? INT FIB (int? n) {

?????? return 0;

???}

??? (I just as the starting point of the Testcase class as the code, because the object we have to develop is just a function.)

??? The second test shows FIB (1) = 1.

??? Public void testfibonacci () {

?????? assertequals (0, FIB (0));

?????? assertequals (1, FIB (1));

???}

??? Let this test run a few ways, I choose to treat 0 as a special case:

??? INT FIB (int N) {

?????? IF (n == 0) returnography;

?????? Return 1;

???}

The repetition of the test case begins to be annoying, and then adding new test cases, it will only make the situation worse. By using a table containing input and desired output values ​​to drive the test, we can separate the common structure of the assertion.

??? Public void testfibonacci () {

?????? int Cases [] = {{0,0}, {1, 1}};

?????? for (int i = 0; i

????????? assertequals (Cases [i] [1], FIB (Cases [i] [0]));

???}

??? Now adding a test case only six keystrokes and no additional rows are added.

??? Public void testfibonacci () {

?????? int Cases [] = {{0, 0}, {1, 1}, {2, 1}};

?????? for (int i = 0; i

????????? assertequals (Cases [i] [1], FIB (Cases [i] [0]));

???}

??? In the battle, the test program has passed, and now continue the next test:

??? Public void testfibonacci () {

?????? int Cases [] = {{0, 0}, {1, 1}, {2, 1}, {3, 2}};

?????? for (int i = 0; i

????????? assertequals (Cases [i] [1], FIB (Cases [i] [0]));

???}

??? Finally failed, such as the method of work (small input as a special case), we wrote:

??? INT FIB (int N) {

?????? IF (n == 0) returnography;

?????? IF (n <= 2) return1;

?????? return 2;

???}

??? Now we can do it. We write 2, but it is really meaningful to 2, but 1 1.

??? INT FIB (int N) {

?????? IF (n == 0) returnography;

?????? IF (n <= 2) return1;

?????? Return 1 1;

???}

??? The first 1 is an instance of FIT (N-1), the second 1 is an example of FIB (N-2):???

??? INT FIB (int N) {

?????? IF (n == 0) returnography;

?????? IF (n <= 2) return1;

?????? Return FIT (N-1) FIT (N-2);

???}

??? Sir, the same structure also applies to FIB (2), so we have strengthened the second condition:

??? INT FIB (int N) {

?????? IF (n == 0) returnography;

?????? IF (n == 1) Return 1;

?????? Return FIT (N-1) FIT (N-2);

???}

So, we got the entire Fibonaccai number, completely from the test. ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????-The above is taken from the Appendix B of the "Test Drive Development" book.

转载请注明原文地址:https://www.9cbs.com/read-15786.html

New Post(0)