|
|
|
|
6
|
|
6
|
|
7
|
[Verano 2016 - Ive - Coralys]
|
7
|
[Verano 2016 - Ive - Coralys]
|
8
|
|
8
|
|
9
|
-As you have learned in previous labs, getting a program to compile is only a minor part of programming. The compiler will tell you if there are any syntactical errors, but it isn't capable of detecting logical problems in your program. It's very important to test the program's functions to validate that they produce correct results.
|
|
|
|
|
9
|
+As you have learned in previous laboratory experiences, getting a program to compile is only a minor part of programming. The compiler will tell you if there are any syntactical errors, but it isn't capable of detecting logical problems in your program. It's very important to test the program's functions to validate that they produce correct results.
|
10
|
|
10
|
|
11
|
-These tests can be performed by hand, this is, running the program multiple times, providing representative inputs and visually checking that the program outputs correct results. A more convenient way is to implement functions in the program whose sole purpose is to validate that other functions are working correctly. In this lab you will be practicing both testing methods.
|
|
|
|
|
11
|
+These tests can be performed by hand; this is, running the program multiple times, providing representative inputs and visually checking that the program outputs correct results. A more convenient way is to implement functions in the program whose sole purpose is to validate that other functions are working correctly. In this laboratory experience, you will be practicing both testing methods.
|
12
|
|
12
|
|
13
|
|
13
|
|
14
|
## Objectives:
|
14
|
## Objectives:
|
|
|
|
|
33
|
|
33
|
|
34
|
---
|
34
|
---
|
35
|
|
35
|
|
36
|
-## Testing a function
|
|
|
|
|
36
|
+## Testing a Function
|
37
|
|
37
|
|
38
|
When we test a function's validity we should test cases that activate the various results the function could return.
|
38
|
When we test a function's validity we should test cases that activate the various results the function could return.
|
39
|
|
39
|
|
|
|
|
|
63
|
|
63
|
|
64
|
---
|
64
|
---
|
65
|
|
65
|
|
66
|
-### The`assert` function:
|
|
|
|
|
66
|
+### The`assert` Function
|
67
|
|
67
|
|
68
|
-The `assert(bool expression)` function can be used as a rudimentary tool to validate functions. `assert` has a very powerful, yet simple functionality. If the expression that we place between the `assert` parenthesis is *true*, the function allows the program to continue onto the next instruction. Otherwise, if the expression we place between the parenthesis is *false*, the `assert` function causes the program to terminate and prints an error message on the terminal, that informs the user about the `assert` instruction that failed.
|
|
|
|
|
68
|
+The `assert(bool expression)` function can be used as a rudimentary tool to validate functions. `assert` has a very powerful, yet simple functionality. If the expression that we place between the `assert` parenthesis is *true*, the function allows the program to continue onto the next instruction. Otherwise, if the expression we place between the parenthesis is *false*, the `assert` function causes the program to terminate and print out an error message on the terminal, that informs the user about the `assert` instruction that failed.
|
69
|
|
69
|
|
70
|
For example, the following program will run from start to finish without problems since every expression included in the assert's parentheses evaluates to *true*.
|
70
|
For example, the following program will run from start to finish without problems since every expression included in the assert's parentheses evaluates to *true*.
|
71
|
|
71
|
|
|
|
|
|
125
|
The program will not execute the remaining instructions after line 8.
|
125
|
The program will not execute the remaining instructions after line 8.
|
126
|
|
126
|
|
127
|
|
127
|
|
128
|
-#### How to use assert to validate functions?
|
|
|
|
|
128
|
+#### How to Use Assert to Validate Functions?
|
129
|
|
129
|
|
130
|
Suppose that you want to automate the validation of the `ageRange`. One way to do it is by implementing and calling a function that calls the `ageRange` function with different arguments and verifies that each returned value is equal to the expected result. If the `ageRange` function returns a value that is not expected, the testing function aborts the program and reports the test that failed. The following illustrates a function to test the `ageRange` function. Observe that it consists of one assert per each of the tests we had listed earlier.
|
130
|
Suppose that you want to automate the validation of the `ageRange`. One way to do it is by implementing and calling a function that calls the `ageRange` function with different arguments and verifies that each returned value is equal to the expected result. If the `ageRange` function returns a value that is not expected, the testing function aborts the program and reports the test that failed. The following illustrates a function to test the `ageRange` function. Observe that it consists of one assert per each of the tests we had listed earlier.
|
131
|
|
131
|
|
|
|
|
|
166
|
|
166
|
|
167
|
## Laboratory Session:
|
167
|
## Laboratory Session:
|
168
|
|
168
|
|
169
|
-### Exercise 1: Designing tests by hand
|
|
|
|
|
169
|
+### Exercise 1 - Designing Tests by Hand
|
170
|
|
170
|
|
171
|
In this exercise you will practice how to design tests to validate functions, using only the function's description and the graphical user interface that is used to interact with the function.
|
171
|
In this exercise you will practice how to design tests to validate functions, using only the function's description and the graphical user interface that is used to interact with the function.
|
172
|
|
172
|
|
|
|
|
|
182
|
|
182
|
|
183
|
![figure4.png](images/figure4.png)
|
183
|
![figure4.png](images/figure4.png)
|
184
|
|
184
|
|
185
|
-**Figure 4** - Interface for a program that finds the max value out of three integers.
|
|
|
|
|
185
|
+**Figure 4.** Interface for a program that finds the max value out of three integers.
|
186
|
|
186
|
|
187
|
---
|
187
|
---
|
188
|
|
188
|
|
|
|
|
|
195
|
If one of these three cases does not have the expected result, your friend's program does not work. On the other hand, if the three cases work, then the program has a high probability of being correct.
|
195
|
If one of these three cases does not have the expected result, your friend's program does not work. On the other hand, if the three cases work, then the program has a high probability of being correct.
|
196
|
|
196
|
|
197
|
|
197
|
|
198
|
-#### Functions to validate
|
|
|
|
|
198
|
+#### Functions to Validate
|
199
|
|
199
|
|
200
|
In this exercise you will be designing tests to validate various versions of the functions that are described below. Each one of the functions has four versions, "Alpha", "Beta", "Gamma" and "Delta".
|
200
|
In this exercise you will be designing tests to validate various versions of the functions that are described below. Each one of the functions has four versions, "Alpha", "Beta", "Gamma" and "Delta".
|
201
|
|
201
|
|
|
|
|
|
205
|
|
205
|
|
206
|
![figure5.png](images/figure5.png)
|
206
|
![figure5.png](images/figure5.png)
|
207
|
|
207
|
|
208
|
- **Figure 5** - Interface for the `3 Sorts` function.
|
|
|
|
|
208
|
+ **Figure 5.** Interface for the `3 Sorts` function.
|
209
|
|
209
|
|
210
|
---
|
210
|
---
|
211
|
|
211
|
|
|
|
|
|
216
|
|
216
|
|
217
|
![figure6.png](images/figure6.png)
|
217
|
![figure6.png](images/figure6.png)
|
218
|
|
218
|
|
219
|
- **Figure 6** - Interface for the `Dice` function.
|
|
|
|
|
219
|
+ **Figure 6.** Interface for the `Dice` function.
|
220
|
|
220
|
|
221
|
---
|
221
|
---
|
222
|
|
222
|
|
|
|
|
|
227
|
|
227
|
|
228
|
![figure7.jpg](images/figure7.jpg)
|
228
|
![figure7.jpg](images/figure7.jpg)
|
229
|
|
229
|
|
230
|
- **Figure 7** - Ways to win in "Rock, paper, scissors".
|
|
|
|
|
230
|
+ **Figure 7.** Ways to win in the "Rock, paper, scissors" game.
|
231
|
|
231
|
|
232
|
---
|
232
|
---
|
233
|
|
233
|
|
234
|
![figure8.png](images/figure8.png)
|
234
|
![figure8.png](images/figure8.png)
|
235
|
|
235
|
|
236
|
- **Figure 8** - Interface for the `Rock, Paper, Scissors` function.
|
|
|
|
|
236
|
+ **Figure 8.** Interface for the `Rock, Paper, Scissors` function.
|
237
|
|
237
|
|
238
|
---
|
238
|
---
|
239
|
|
239
|
|
|
|
|
|
248
|
|
248
|
|
249
|
![figure9.png](images/figure9.png)
|
249
|
![figure9.png](images/figure9.png)
|
250
|
|
250
|
|
251
|
- **Figure 9** - Interface for the `Zulu time` function.
|
|
|
|
|
251
|
+ **Figure 9.** Interface for the `Zulu time` function.
|
252
|
|
252
|
|
253
|
---
|
253
|
---
|
254
|
|
254
|
|
|
|
|
|
269
|
| 2 | "deer", "fox", "coyote" | "fox", "deer", "coyote" | .... | .... | |
|
269
|
| 2 | "deer", "fox", "coyote" | "fox", "deer", "coyote" | .... | .... | |
|
270
|
| .... | .... | .... | .... | .... | .... |
|
270
|
| .... | .... | .... | .... | .... | .... |
|
271
|
|
271
|
|
272
|
- **Figure 10** - Table to organize the test results.
|
|
|
|
|
272
|
+ **Figure 10.** Table to organize the test results.
|
273
|
|
273
|
|
274
|
---
|
274
|
---
|
275
|
|
275
|
|
276
|
You can see examples of how to organize your results [here](images/example01.png) and [here](images/example02.png).
|
276
|
You can see examples of how to organize your results [here](images/example01.png) and [here](images/example02.png).
|
277
|
|
277
|
|
278
|
|
278
|
|
279
|
-### Exercise 2: Doing tests “by hand”
|
|
|
|
|
279
|
+### Exercise 2 - Doing Tests “by Hand”
|
280
|
|
280
|
|
281
|
The `testing` project implements several versions of each of the four functions that were described in Exercise 1. Some or all of the implementations could be incorrect. Your task is, using the tests you designed in Exercise 1, to test the versions for each function to determine which of them, if any, are implemented correctly.
|
281
|
The `testing` project implements several versions of each of the four functions that were described in Exercise 1. Some or all of the implementations could be incorrect. Your task is, using the tests you designed in Exercise 1, to test the versions for each function to determine which of them, if any, are implemented correctly.
|
282
|
|
282
|
|
|
|
|
|
296
|
|
296
|
|
297
|
![figure11.png](images/figure11.png)
|
297
|
![figure11.png](images/figure11.png)
|
298
|
|
298
|
|
299
|
- **Figure 11** - Window to select the function that will be tested.
|
|
|
|
|
299
|
+ **Figure 11.** Window to select the function that will be tested.
|
300
|
|
300
|
|
301
|
---
|
301
|
---
|
302
|
|
302
|
|
|
|
|
|
306
|
|
306
|
|
307
|
|
307
|
|
308
|
|
308
|
|
309
|
-### Exercise 3: Using `assert` to make unit tests
|
|
|
|
|
309
|
+### Exercise 3 - Using `assert` to Make Unit Tests
|
310
|
|
310
|
|
311
|
Doing tests by hand each time you run a program is a tiresome task. In the previous exercises you did it for a few simple functions. Imagine doing the same for a complex program like a search engine or a word processor!
|
311
|
Doing tests by hand each time you run a program is a tiresome task. In the previous exercises you did it for a few simple functions. Imagine doing the same for a complex program like a search engine or a word processor!
|
312
|
|
312
|
|
313
|
*Unit tests* help programmers validate code and simplify the process of debugging while avoiding having to do these tests by hand in each execution.
|
313
|
*Unit tests* help programmers validate code and simplify the process of debugging while avoiding having to do these tests by hand in each execution.
|
314
|
|
314
|
|
315
|
-#### Instructions:
|
|
|
|
|
315
|
+#### Instructions
|
316
|
|
316
|
|
317
|
1. In the `QtCreator` menu, go to `Build` and select `Clean Project "Testing"`. Then go to `File` and select `Close Project "Testing"`.
|
317
|
1. In the `QtCreator` menu, go to `Build` and select `Clean Project "Testing"`. Then go to `File` and select `Close Project "Testing"`.
|
318
|
|
318
|
|
|
|
|
|
330
|
|
330
|
|
331
|
This is evidence enough to establish that the `fact` function is NOT correctly implemented.
|
331
|
This is evidence enough to establish that the `fact` function is NOT correctly implemented.
|
332
|
|
332
|
|
333
|
-4. Notice that, by failing the previous test, the program did not continue its execution. To test the code you will write, comment the `test_fact()` call in `main`.
|
|
|
|
|
333
|
+4. Notice that, by failing the previous test, the program did not continue its execution. To test the code you will write, comment the `test_fact()` called in `main`.
|
334
|
|
334
|
|
335
|
5. Write a unit test called `test_isALetter` for the `isALetter` function. Write various asserts in the unit test to try some data and its expected values (use the `test_fact` function for inspiration). Invoke `test_isALetter` from `main` and execute your program. If the `isALetter` function passes the test you wrote, continue writing asserts and executing the program until one of them fails.
|
335
|
5. Write a unit test called `test_isALetter` for the `isALetter` function. Write various asserts in the unit test to try some data and its expected values (use the `test_fact` function for inspiration). Invoke `test_isALetter` from `main` and execute your program. If the `isALetter` function passes the test you wrote, continue writing asserts and executing the program until one of them fails.
|
336
|
|
336
|
|
|
|
|
|
346
|
|
346
|
|
347
|
1. Use "Deliverable 1" in Moodle to turn in the table with the tests you designed in Exercise 1 and that you completed in Exercise 2, with the results from the function tests.
|
347
|
1. Use "Deliverable 1" in Moodle to turn in the table with the tests you designed in Exercise 1 and that you completed in Exercise 2, with the results from the function tests.
|
348
|
|
348
|
|
349
|
-2. Use "Deliverable 2" in Moodle to turn in the `main.cpp` file that contains the `test_isALetter`, `test_isValidTime`, `test_gcd` functions and their calls. Remember to use good programming techniques by including the name of the programmers involved and documenting your program.
|
|
|
|
|
349
|
+2. Use "Deliverable 2" in Moodle to turn in the `main.cpp` file that contains the `test_isALetter`, `test_isValidTime`, `test_gcd` functions and their calls. Remember to use good programming techniques, include the name of the programmers involved, and document your program.
|
350
|
|
350
|
|
351
|
---
|
351
|
---
|
352
|
|
352
|
|