6.6 Testing

We briefly tested our programs by executing them to see if they produced the output we expected. However, there is far more to testing than just running the program once.

How you test and what you test depend on what the program does, how it will be used, and how essential it is that it be correct.

Almost all programs have errors. They are a fact of life. Programmers create errors during the act of creating a program, and then have to find and fix them. Interestingly, whether a program has errors or not may depend on who’s doing the evaluation. The end user, who best understands how the program will be used, has a different perspective than the program designer, who is coming at the problem from another viewpoint. A program may “work,” but if it doesn’t solve the problem the end user needs solved, then it’s not correct.

For now, we will focus on straightforward correctness as defined by the stated goals of the program.

How do we test a specific program to determine its correctness? We design and implement a test plan. A test plan is a document that specifies how many times and with which data a program must be run to thoroughly test the program. Each set of input data values is called a test case. The test plan should list the reason for choosing the data, the data values, and the expected output from each case.

The test cases should be chosen carefully. Several approaches to testing can be used to guide this process. Code coverage is an approach that designs test cases to ensure that each statement in the program is executed. Because the tests are based on the code of the program, this approach is also called clear-box testing. Another approach, data coverage, calls for designing test cases to ensure that the limits of the allowable data are covered. Because this approach is based solely on input data and not the code, it is also called black-box testing. Often testing entails a combination of these two approaches.

Test plan implementation involves running each of the test cases described in the test plan and recording the results. If the results are not as expected, you must go back to your design and find and correct the error(s). The process stops when each of the test cases gives the expected results. Note that an implemented test plan gives us a measure of confidence that the program is correct; however, all we know for sure is that our program works correctly on the test cases. Therefore, the quality of the test cases is extremely important.

In the case of the program that reads in two values and sums them, a clear-box test would include just two data values. There are no conditional statements in this program to test with alternate data. However, a clear-box test would not be sufficient here, because we need to try both negative and positive data values. The numbers that are being read in are stored in one word. The problem does not limit values to ±215 2 1, but our implementation does. We should also try values at the limits of the size of the allowed input in the test plan, but because they are being summed, we need to be sure the sum does not exceed ±215 − 1.

Images

To implement this test plan, we ran the program five times, once for each test case. The results were then written in the “Observed Output” column. They were what we had predicted, which increases our confidence that the program works as planned. If any of the observed output had been different than expected, we would then explore the problem the test case uncovered.

SUMMARY

A computer can store, retrieve, and process data. A user can enter data into the machine, and the machine can display data so that the user can see it. At the lowest level of abstraction, instructions to the machine directly relate to these five operations.

A computer’s machine language is the set of instructions that the machine’s hardware is built to recognize and execute. Machine-language programs are written by entering a series of these instructions in their binary form. The Pep/9 is a virtual computer with an A register (the accumulator) and two-part instructions. One part of the instruction tells which action the instruction performs, and the other part specifies where the data to be used (if any) can be found. Programs written using the Pep/9 instruction set can be run using a simulator—a program that behaves like the Pep/9 computer.

The Pep/9 assembly language is a language that allows the user to enter mnemonic codes for each instruction rather than binary numbers. Programs written in assembly language are translated into their machine-language equivalents, which are then executed using the Pep/9 simulator.

Pseudocode is a shorthand-like language that people use to express algorithms. It allows the user to name variables (places to put values), input values into variables, and print out the values stored in variables. Pseudocode also allows us to describe algorithms that repeat actions and choose between alternative actions. Asking questions and deferring details are two problem-solving strategies used in algorithm design.

Programs, like algorithms, must be tested. Code coverage testing involves determining the input to the program by looking carefully at the program’s code. Data coverage testing involves determining the input by considering all possible input values.

KEY TERMS

EXERCISES

For Exercises 1–15, mark the answers true or false as follows:

  1. True

  2. False

  1.   1. Arithmetic can be performed in the instruction register.

  2.   2. Arithmetic can be performed in the A register.

  3.   3. Arithmetic can be performed in the accumulator.

  4.   4. LDBA 0x008B,i loads the byte 008B into register A.

  5.   5. ADDA 0x008B,i adds the contents of 008B to the A register.

  6.   6. The program counter and the instruction register are two names for the same place.

  7.   7. The A register and the accumulator are two names for the same place.

  8.   8. The instruction register is 3 bytes long.

  9.   9. The program counter is 3 bytes long.

  10. 10. The branch instruction, BR, branches to the location specified in the operand specifier.

  11. 11. The instruction specifier is 1 byte long.

  12. 12. If the data to be loaded into the accumulator is stored in the operand, the instruction specifier ends with 000.

  13. 13. If the data in the accumulator is to be stored in the place named in the operand, the instruction specifier ends with 000.

  14. 14. All Pep/9 instructions use 3 bytes.

  15. 15. At least one branching instruction is required in a loop.

Given the following state of memory (in hexadecimal), complete Exercises 16–20 by matching the problem to the solution shown.

0001 A2
 0002 11
 0003 00
 0004 FF

  1. A2 11

  2. A2 12

  3. 00 02

  4. 11 00

  5. 00 FF

  1. 16. What are the contents of the A register after the execution of this instruction?

          C1 00 01

  2. 17. What are the contents of the A register after the execution of this instruction?

          C1 00 02

  3. 18. What are the contents of the A register after the execution of the following two instructions?

          C0 00 01

          60 00 01

  4. 19. What are the contents of the A register after the execution of the following two instructions?

          C1 00 01

          60 00 01

  5. 20. What are the contents of location 0001 after the execution of the following two instructions?

          C1 00 03

          E0 00 01

Exercises 21–60 are programs or short-answer questions.

  1. 21. What does it mean when we say that a computer is a programmable device?

  2. 22. List five operations that any machine language must include.

  3. 23. How many low-level tasks can each machine-language instruction perform?

  4. 24. What is a virtual machine? Discuss this definition in terms of the Pep/9 computer.

  5. 25. How many bits are used to represent an instruction in Pep/9?

  6. 26. Describe the features of the Pep/9 CPU that we covered in this chapter.

  7. 27. Where is the data (operand) if the address mode specifier is

  1. 000?

  2. 001?

  1. 28. We discussed two mode specifiers. How many are there?

  2. 29. Distinguish between the IR (instruction register) and the PC (program counter).

  3. 30. How many bits are required to address the Pep/9 memory?

  4. 31. How many more cells could be added to memory without having to change the instruction format? Justify your answer.

  5. 32. Some Pep/9 instructions are unary, taking only 1 byte. Other instructions require 3 bytes. Given the instructions that we have covered in this chapter, would it be useful to define instructions that require only 2 bytes?

  6. 33. What is the result of executing the following instructions?

          0001 D0 00 48
    0004 F1 FC 16

  7. 34. What is the result of executing the following instructions?

          0001 D0 00 07
    0004 70 00 02

  8. 35. Write a program in Pep/9 machine code to output the word “go”.

  9. 36. Write a program in Pep/9 assembly language to output the word “go”.

  10. 37. Write a program in Pep/9 machine code to output the word “home”.

  11. 38. Write a program in Pep/9 assembly language to output the word “home”.

  12. 39. Explain how input and output work in Pep/9.

  13. 40. Distinguish between the Pep/9 menu options Assemble, Load, and Execute (run).

  14. 41. The following program seems to run, but does strange things with certain input values. Can you find the bug?

    BR main
    sum: .WORD 0x0000
    numl: .BLOCK 1
    num2: .BLOCK 1
    num3: .BLOCK 1
    main: LDWA sum,d
    DECI numl,d
    DECI num2,d
    DECI num3,d
    ADDA num3,d
    ADDA num2,d
    ADDA numl,d
    STWA sum,d
    DECO sum,d
    STOP
    .END
  15. 42. Correct the code in Exercise 41.

  16. 43. What is the purpose of the .ASCII pseudo-operation?

  17. 44. Write a pseudocode algorithm that reads in three values and writes out the result of subtracting the second value from the sum of the first and third values.

  18. 45. Implement the algorithm in Exercise 44 as Pep/9 assembly-language program.

  19. 46. Write a test plan for the program in Exercise 45.

  20. 47. Design with pseudocode, then implement, an assembly language program that uses a loop to read four values and print their sum.

  21. 48. Is the test plan for a machine-language program valid for the same solution written in assembly language? Explain your answer.

  22. 49. Distinguish between the pseudo-operations .BLOCK and .WORD.

  23. 50. Distinguish between assembly-language pseudo-operations and mnemonic instructions.

  24. 51. Distinguish between test plans based on code coverage and data coverage.

  25. 52. Which button on the Pep/9 console must be clicked for keyboard input?

  26. 53. Write the Pep/9 assembly-language statement for the following instructions:

  1. Branch to location Branch1 if the accumulator is zero.

  2. Branch to location Branch1 if the accumulator is negative.

  3. Branch to location Branch1 if the accumulator is negative and to Branch2 if the accumulator is not negative.

  1. 54. Write a pseudocode algorithm to read in a name and write a “Good morning” message.

  2. 55. Write a pseudocode algorithm to get three integers from the user and print them in numeric order.

  3. 56. Enclose the design in Exercise 55 within a loop that reads in the three values until the user enters the first value of the trio as negative.

  4. 57. Rewrite the algorithm in Exercise 56 so that the user has to enter only one negative value to stop (that is, the second and third values are not entered).

  5. 58. Distinguish between pseudocode and pseudo-operations.

  6. 59. What are the constructs that pseudocode must be able to express?

  7. 60. Distinguish between the looping construct and the selection construct.

THOUGHT QUESTIONS

  1. Would you like to do assembly-language programming? Can you think of any personality types that would be well suited for such detail-oriented work?

  2. The translation process has been demonstrated by showing the machine-language program that is the result of the assembly-language program. Look carefully at the solution in Exercise 45. Think about the steps that the assembler program must execute. Do you think that the translation can be made by looking at each assembly-language instruction once, or must each one be examined twice? Convince a friend that you are right.

  3. If a person has two computers of the same kind, is it ethical to buy one copy of a software package and install it on both machines? What are the arguments on the yes side? What are the arguments on the no side?

  4. Has anyone borrowed software from you? Do you think he or she copied it?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.33.107