Writing, building, and testing your programIt is usually not possible to be sure that a computer program is written correctly. To do this, we would need to generate ALL possible input combinations, and run our programs on them. The can literally take more than your lifetime, for some programs. Therefore, we usually create a set of tests to get an idea of whether our programs are working correctly. These tests do not PROVE that our program is correct, but can prove that the program works for certain inputs. There still might be other inputs that would cause our programs to break!
However, all is not lost. We can learn ways to write "test cases" that can help us be reasonably sure that our programs will work for most of our expected inputs, especially for small programs. In this class, we will learn some ways to construct test cases, and you will learn more ways in future computer science courses. For now, remember that we will try to think of ways that people can break our code, and test for these in our test cases.
In this page, you will learn how to write and build your programs, and how to read input from a file, and save your output to a file, and test your program on provided test files for your assignment. This will save you time!
Writing and building your programs
Saving your program output to a file
- Download the Quincy installer which can also be found at: http://www.codecutter.net/tools/quincy/
- Install Quincy on your computer by double-clicking the downloaded file.
- Run Quincy by selecting it from the START menu under "All Programs".
- From the File menu, select New and then C++ source. Type in your program.
- Save your program, for example as MyProgram.cpp, and then select Project->Build.
- To execute your program, select Project->Execute, or go to the place you've saved your file and double click on your MyProgram.exe file.
In order to test your program output, we need to save it to a file. We do this by "redirecting" the output of your program to a file using the > character.
Testing your output
- Write and build your program as above.
- Run the program as above to test until you are ready to test it for correctness.
- Select Tools->CommandPrompt to get a Command window to appear.
- At the prompt, type the name of your .exe file, followed by >, and then by the filename you wish to use to save your output, such as test.1.out. For example, in this case we would type:
MyProgram.exe > test.1.out- Press enter. You will not see any output from your program. That is because the > character tells the operating system to send your program output to the file that you enter after >, in this case test.1.out.
- Now, go to the directory where you stored your program, and open test.1.out. Your program output should appear in this file.
- We are now ready to test the output of your program against the output provided for you.
Getting program input from a file
- Save the test output, named for example HW2-test.out from your Blackboard assignment to the same directory where you saved your program and program output.
- In the Command window, type
fc test.1.out HW2-test.outand press enter.- FC stands for File Compare. If the files are not different, a line will appear that says:
In this case, your program output is exactly correct.FC: no differences encountered.- If the files are different, FC will print the lines that are different in each file.
Similar to redirecting output for our program, we can also get program input from a file.
- Write and build your program as above.
- Run the program as above to test until you are ready to test it for correctness.
- Select Tools->CommandPrompt to get a Command window to appear.
- At the prompt, type the name of your .exe file, followed by <, and then by the filename you wish to use for loading input. This filename must contain all of the inputs needed for your program, formatted the same way you would type them for input. For example, if your program gathers three integers as input, your input file should contain three lines, each one with an integer on that line, with no extra spaces or characters. For example, in this case we would type:
MyProgram.exe < test.1.in- Press enter. Your program output should appear in the Command window.
- If you wish to also redirect the program output, try:
MyProgram.exe < test.1.in > test.1.out- This command will run your file using inputs from test.1.in and save the output in test.1.out.
- If available, download any test input files provided in your assignment, save them in your program directory, and test your program with these files to ensure it works with these files.