What is a Table Test? How to apply it?

Asked

Viewed 43,935 times

42

It is quite common to read while studying algorithms and programming logic that the Table Test is a means of verifying the functioning of an algorithm.

  • What is the Table Test?
  • How is it possible to apply it to check the logic in a program?
  • What is the step-by-step to perform the Table Test?
  • There is a software that runs this test?

If possible, give examples of how a desktop test is applied in a program.

Note: the purpose of the question is to create objective and quality content on Table Test for the purpose of helping beginners to verify errors in their programs.

  • 3

    Related: https://answall.com/q/136393/101 who knows dup.

  • @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 :/

  • 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.

  • Test of table?

  • @What? Could you further detail your comment?

  • It seems to me that the most suitable translation for 'trace table' would be table test (tracking), not table test =)

  • @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".

  • 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 ;-)

Show 4 more comments

5 answers

31


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:

  1. Draw up a table where each column refers to each variable involved and the outcome of a particular operation (or relevant observation);
  2. Perform the steps provided in the algorithm;
  3. Verify that the results obtained are consistent with the expected;
  4. 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:

  1. Identify all variables in your program;
  2. 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;
  3. 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;
  4. 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;
  5. 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.

21

There are basically two ways to check whether an algorithm is doing what is expected. One of them is the formal method where he "proves" mathematically that he does what he wishes.

There are also the informal methods, where some kind of inspection is done to check if a code is correct. One of these checks is the table test or checking desk as it is called in English. It is so informal that it is not found academic information or reliable literature that talks about it. It’s almost an urban legend that goes by, although it’s real.

There was a time when it was very useful, today less because the debuggers are sophisticated to do it for you, you can follow the execution intuitively, automatically and without risk of getting lost in what you are doing, After all, humans lose easy attention to boring tasks like this. It’s not like wearing a Debugger be the table test, but it is replaced by the existing computerized mechanism across the IDE or even command line.

The table test is still a process of debuging.

Today I consider it a method of learning more than something to use in everyday life. I think all development should do table tests to understand what happens in the code until you master the functioning of the execution.

In general we use a trace table or tracking table to track the states of the algorithm that are changing. State is a huge problem in computing and its mutation always causes effects on the application. Almost all problems are caused by mistaken state change, even if the reason for doing wrong is a wrong flow.

Often you also control what is executed, which is still a change of state, even if it is not in variables. It’s not just about changing values in variables.

You can do it in Excel, Although it is easier if you are another machine or monitor. Or you can use it just to mount the tables. But it is common to make blank paper, perhaps at least with lines. Examples:

Teste de mesa

This is a form showing which line is running. Others focus only on changing the state.

It is even possible to do with SQL:

Teste de mesa em SQL

My technique

The exact technique of making depends on the person, there is no rule. I have already answered about the process I use in another answer:

I create a table with the declared variables, each in a column. Each row in the column will be used to annotate the new variable value whenever there is a new assignment. Some people like to scratch the previous value to avoid confusion and only make visible the same last, which is what counts.

It is also possible to have columns for sub-expressions, which can help find errors in them.

Then from the third line I start the annotation in the column of n1. I’ll write down a number I wish to test.

I then note the result of the calculation of the n1 (is always the last value available in the column) times 3.

Then I do the same taking the last value of n1 minus 1 plus the last value of n2.

Then I write down separately what should appear on the screen (a page only for screen demonstration), in case The final result will be n3= and the last value of N3.

If it were something more complex it would continue. If it had a loop it would keep the notes.

I would test with various values, with 0, positive, negative, some high numbers, try for a text, in order to try to create situations that cause error in the algorithm.

Some people like to put columns for sub-expressions as well. It’s a good idea.

To make an easy table test it is important that the algorithm is not too big.

In some cases there may be some specifics of how to proceed.

Some people like to create a data line for each line or sub-expression executed by copying the previous data. I think exaggeration, but it can be useful in some cases. I only do something when the state changes.

Complement

Has a video classroom on Youtube, for lack of a better option, it helps to understand. There are other suggestions that you can follow.

Has slides very interesting that show the process.

19

What is the Table Test?

The Table Test or Trace Table as it can be called. It is a technique used to test algorithms or computer programs, in order to check if there is an error in the logic during the execution of the algorithm or program. It also simulates the execution flow of a part of a program or the entire program. The declarations are executed step by step, and the values of the variables change as the declaration is executed.

This technique is typically used by beginner programmers to help them visualize how a particular algorithm or program works. Programmers with more experience can also use desktop testing to detect errors, although Ides provide great resources for this purpose.

How is it possible to apply it to check the logic in a program?

Let’s consider this program in C#, see:

static void Main(string[] args)
{
    int x, y, z;

    x = 10;
    y = 15;
    z = x * y;
    z++;

    Console.WriteLine(z);            
}

Based on this code, we can build a table to check the execution of this program:

tabela para teste de mesa

It is in this table that it is possible to apply the table test to verify the logic of the program. Of course the table can have more columns or less depending on the program code.

What is the step-by-step to perform the Table Test?

Based on the table that was created, just fill the columns corresponding to each step of the program, this way will be possible to view its execution flow.

See the filled table:

tabela preenchida para o teste de mesa

For each execution step, we place the corresponding declaration followed by a comment regarding the declaration, and finally, the value that was assigned to the variable corresponding to the declaration until the final step that is the display of the variable’s value z on the console.

In this way, it is possible to visualize the execution flow of the program or algorithm, and this technique for those who are starting in the world of programming, is great. Due to errors that are committed most frequently by novice programmers, it is easier to identify and correct them with the Table Test.

There is a software that runs this test?

In C I usually use a program called DDD (Data Display Debugger) to do the table test (it is not like the traditional table test, but the goal is the same). Ides provide great resources for this purpose as well.

Completion

It is very important that beginners learn Table Test, and even advanced programmers can use from time to time, because with it, it is easier to identify the errors and helps to fix better what is being learned. It can even be a bit of a boondoggle to do the table test, depending on the code, but, due to the results that will be acquired, it makes up a lot of effort. This is the first steps to learn how to debug a computer program or algorithm.

Sources:
What is a Trace table?
Trace table

8

Table test is a programming logic test, is usually used in repetition systems, example:

A=1;
B=10;
While(A!=B) {
  A++;
  B--;
}

Table test:
1° Loop: Valor A inicial=1, Valor B inicial=10 Valor A final= 2, Valor B final=9    
2° Loop: Valor A inicial=2, Valor B inicial=9 Valor A final=3, Valor B final=8
3° Loop: Valor A inicial=3, Valor B Inicial=7 Valor A final=4, Valor B final=6
4° Loop: Valor A inicial= 4, Valor B inicial= 7 Valor A Final=5, Valor B final=5

FIM LOOP, A==B

3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.