What is the Table Test?
Table Testing is a manual process that is used to validate the logic of a given algorithm. It is mainly used in algorithms when the language used has no automated debugging tools. As programming languages often have such tools, it is more common to use them to do the table test itself, although for those who are still beginners, I particularly still recommend usinglo, since you probably won’t have mastery over the debugging tool.
In the book Computer & Internet Dictionary there is the definition:
Table test: analysis to know if a program works logically.
In the book Training in Programming Logic there is:
After the development of an algorithm, it is necessary to check each of the steps that have been determined, i.e., perform a test. To do this, read each of the instructions and write down the result of each task/step, checking for possible errors or other ways to solve the problem. This test is better known as Table Test.
In the book Programming Techniques - A Modern Approach there is:
One of the most widely employed tools to verify that a pseudocode (or algorithm in general) is being executed correctly is the so-called Bench Test (synonym for Table Test). This test accurately depicts what the pseudocode is performing, step by step, showing the programmer the steps foreseen in the algorithm.
How is it possible to apply it to check the logic in a program?
There is no canonical way to prepare a Table Test, as it will depend a lot on what you want to verify in the algorithm and its level of understanding. In general, you should create on paper a table with all the variables of the program and execute step by step your code, always noting the values of the variables. So you will be able to identify if the values match the expected or locate the exact line of code where the value of the variable goes wrong.
The book Programming Techniques - A Modern Approach also presents a four-step procedure describing the implementation of the Table Test:
- Draw up a table where each column refers to each variable involved and the outcome of a particular operation (or relevant observation);
- Perform the steps provided in the algorithm;
- Verify that the results obtained are consistent with the expected;
- End the test after a reasonable number of correct results obtained;
That is, identify all variables in your program and check their values for each line of code executed.
What is the step-by-step to perform the Table Test?
As said, there is no definite sequence of steps, but the ones I usually follow and which have always had a good acceptance by beginners in programming is:
- Identify all variables in your program;
- Create a table where the first column is called "Step", the second called "Row". From this, create a column for each variable of the program;
- In the first row of the table, fill the "Step" column with "Start", you can leave the "Row" column blank and fill each column of the variables with their initial values;
- Scroll through your code row by row, filling in the table. The column "Step" should be incremented for each new row in the table; the column "Row" should indicate the number of the row in the code being analyzed and in each column of the variables should contain the respective value for each variable after the code row is executed;
- Run step 4 until the program is finished;
For example, let’s consider a program that practically all beginners do at the beginning of the studies: factor calculation. An algorithm for pseudo-factorial calculation code is:
1 numero <- 0;
2 resultado <- 1;
3
4 leia(numero);
5
6 se (numero < 0) então
7 imprima("O número não pode ser negativo");
8 senão
9 enquanto (numero > 0) faça
10 resultado <- resultado * numero;
11 numero <- numero - 1;
12 fim
13
14 imprima("O fatorial de vale", resultado);
15 fim
Step 1: Identify all variables in the program;
The variables of the program are numero
, which will receive the value from which we wish to calculate the factorial, and resultado
, which shall store the result of the calculation.
Step 2: Create the table;
Remembering that the first column is called "Step", the second "Row" and the others represent the variables of the program.
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| | | | |
+-----------+-----------+------------+---------------+
Step 3: Fill the first row of the table;
In the "Step" column put "Start", in the "Row" column no value and in the columns of the variables the initial values of each.
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
Step 4: walk through each line of the program by filling in the table;
The lines of definition of the variables have already been considered in step 3, when we have already filled in the table with the initial values. Therefore, we begin analyzing the program from line 4. Let’s assume that we want to calculate the factorial of 3, therefore, when the function leia(numero)
request the user a number, it will enter the value 3, being stored in the variable numero
. The variable resultado
does not vary, so we keep its value.
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
In line 6 it is checked if the value entered by the user is less than zero. Since 3 is greater than zero, the condition is false and thus we jump to line 8. On line 9, we create a loop that will last as long as the value of numero
is greater than zero. At this point the value is 3 (see the table above), then we must execute the loop starting at line 10. At this line, the value of resultado
is updated to the value resultado * numero
, that is, the new value of resultado
will be the current value multiplied by the value of numero
. So:
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
| 2 | 10 | 3 | 1 * 3 = 3 |
+-----------+-----------+------------+---------------+
Naturally we move to line 11, where the value of numero
becomes its present value decreasing into a unit, then:
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
| 2 | 10 | 3 | 1 * 3 = 3 |
+-----------+-----------+------------+---------------+
| 3 | 11 | 3 - 1 = 2 | 3 |
+-----------+-----------+------------+---------------+
Once the code has finished inside the loop, we must return to line 9 and check again the condition to determine whether the loop should continue or not. At this time, numero
is worth 2 and therefore is still higher than 0, so we start to line 10 again. The value of resultado
will be the current one multiplied by the value of numero
, then:
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
| 2 | 10 | 3 | 1 * 3 = 3 |
+-----------+-----------+------------+---------------+
| 3 | 11 | 3 - 1 = 2 | 3 |
+-----------+-----------+------------+---------------+
| 4 | 10 | 2 | 3 * 2 = 6 |
+-----------+-----------+------------+---------------+
In line 11, again the value of numero
will receive the current value down into one unit, then:
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
| 2 | 10 | 3 | 1 * 3 = 3 |
+-----------+-----------+------------+---------------+
| 3 | 11 | 3 - 1 = 2 | 3 |
+-----------+-----------+------------+---------------+
| 4 | 10 | 2 | 3 * 2 = 6 |
+-----------+-----------+------------+---------------+
| 5 | 11 | 2 - 1 = 1 | 6 |
+-----------+-----------+------------+---------------+
We returned to line 9, analyzing again the condition of the loop. Since 1 is still greater than zero, then we move to line 10, where again the value of resultado
will be modified:
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
| 2 | 10 | 3 | 1 * 3 = 3 |
+-----------+-----------+------------+---------------+
| 3 | 11 | 3 - 1 = 2 | 3 |
+-----------+-----------+------------+---------------+
| 4 | 10 | 2 | 3 * 2 = 6 |
+-----------+-----------+------------+---------------+
| 5 | 11 | 2 - 1 = 1 | 6 |
+-----------+-----------+------------+---------------+
| 6 | 10 | 1 | 6 * 1 = 6 |
+-----------+-----------+------------+---------------+
And in line 11 the value of numero
will be updated:
+-----------+-----------+------------+---------------+
| Passo | Linha | numero | resultado |
+-----------+-----------+------------+---------------+
| Início | - | 0 | 1 |
+-----------+-----------+------------+---------------+
| 1 | 4 | 3 | 1 |
+-----------+-----------+------------+---------------+
| 2 | 10 | 3 | 1 * 3 = 3 |
+-----------+-----------+------------+---------------+
| 3 | 11 | 3 - 1 = 2 | 3 |
+-----------+-----------+------------+---------------+
| 4 | 10 | 2 | 3 * 2 = 6 |
+-----------+-----------+------------+---------------+
| 5 | 11 | 2 - 1 = 1 | 6 |
+-----------+-----------+------------+---------------+
| 6 | 10 | 1 | 6 * 1 = 6 |
+-----------+-----------+------------+---------------+
| 7 | 11 | 1 - 1 = 0 | 6 |
+-----------+-----------+------------+---------------+
After, we went back to line 9 to check the condition of the loop again, but now the value of numero
is 0 and does not satisfy the condition of being greater than zero, so we move to line 14, where the message "The factor is worth 6" is displayed, because the current value of resultado
is 6.
Although the Table Test is often used to test loops it is not limited to just that. Any algorithm can be tested using the Table Test. For example, in the question How to Do Table Test for a Particular Algorithm? an example Table Test is presented in a simple algorithm, with only mathematical operations. In the question Recursive Functions in Javascript i answered about how recursion works in factorial calculation using Table Test. Already in question Recursiveness in Python I explained how recursion works for the calculation of the Fibonacci series also using Table Test.
There is a software that runs this test?
I don’t know any that has that purpose. As I commented at the beginning of the answer, if you are using any programming language it is very likely that there is some debugging tool (debug) that does such a service for you. The Table Test basically has this name because it will require you to have paper, pen and a desk to perform it. Certainly performing the Table Test requires a lot of willingness, but, especially for those who are starting, it is very worthwhile. Many of the logic errors (ideally all) in the code can be identified when performing the Table Test.
Discussion related to this question in Meta.
– Woss
Related: https://answall.com/q/136393/101 who knows dup.
– Maniero
@bigown the answer is good, but as the question deals with a specific algorithm, I thought it could generate confusion for a beginner: "oh, my program is different, so it’s not valid". But apparently more people agree that it’s duplicate and they didn’t express themselves in the Meta :/
– Woss
I don’t usually do table test in hand when I’m using VS, it already shows me everything that’s in memory when I’m debugging. If you want to know more you can see here.
– gato
Test of table?
– mari
@What? Could you further detail your comment?
– Woss
It seems to me that the most suitable translation for 'trace table' would be table test (tracking), not table test =)
– mari
@But I don’t know the opinion about it; the term table test is what I know and see being highly used in Portuguese, nor was it a matter of translation. I’ve never seen "table test".
– Woss
I understand, but look at the logical side: table is table and tb table. You use a table in the test, but it has nothing to do with table. So... It may have been a bad translation and no one questioned it and got like this, like the story of the monkeys that don’t eat the banana and don’t know why ;-)
– mari