From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 22_tests_power/.gitignore | 1 + 22_tests_power/README | 46 +++++++++++++++++++++++ 22_tests_power/grade.txt | 53 ++++++++++++++++++++++++++ 22_tests_power/next-README | 18 +++++++++ 22_tests_power/run_all.sh | 36 ++++++++++++++++++ 22_tests_power/test-power | Bin 0 -> 16488 bytes 22_tests_power/test-power.c | 88 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 242 insertions(+) create mode 100644 22_tests_power/.gitignore create mode 100644 22_tests_power/README create mode 100644 22_tests_power/grade.txt create mode 100644 22_tests_power/next-README create mode 100755 22_tests_power/run_all.sh create mode 100755 22_tests_power/test-power create mode 100644 22_tests_power/test-power.c (limited to '22_tests_power') diff --git a/22_tests_power/.gitignore b/22_tests_power/.gitignore new file mode 100644 index 0000000..2177b03 --- /dev/null +++ b/22_tests_power/.gitignore @@ -0,0 +1 @@ +test_power diff --git a/22_tests_power/README b/22_tests_power/README new file mode 100644 index 0000000..24b1fd9 --- /dev/null +++ b/22_tests_power/README @@ -0,0 +1,46 @@ +For this assignment, you will be writing test cases for + +unsigned power (unsigned x, unsigned y); + +which you will be writing in the next assignment (if you want to read +the instructions for the next assignment, they are provided in next-README). + +Unlike your previous testing assignments, you will be writing +C code to perform the tests. In particular, you should create a file +called test-power.c, which has a main function that performs the tests. + +If the power function passes all test cases, your program should exit +with EXIT_SUCCESS. If the power function fails any test case, your program +should exit with EXIT_FAILURE. Note that your program's exit status is the +return value from main, if main returns. However, you can make your program +exit immediately (wherever it is) by calling exit, passing in either EXIT_SUCCESS +or EXIT_FAILURE, e.g., + +exit(EXIT_FAILURE); + +A few notes about doing this assignment: + (1) You will want to write the prototype for power in your test-power.c + file before you call power, to let the compiler know the signature + of this function, and that the implementation will be found elsewhere. + (2) One correct and many broken implementations of power are provided + in the form of compiled object files in /usr/local/l2p/power/. + Of these, power.o is correct, and the others are broken. + (3) We have provided run_all.sh, which iterates over the object + files in /usr/local/l2p/power, and compiles your test code + and links it with each object file. It will make sure that + the correct implementation passes all of your test cases, + and that each broken implementation fails at least one test case. + (4) When I did this, I wrote the following helper function: + void run_check(unsigned x, unsigned y, unsigned expected_ans) + which calls power, checks that the result is expected_ans, + and if not, prints a message and calls exit(EXIT_FAILURE). + (5) You need your own way to provide the expected answers. + You might come up with them by hand, or find some other + means to produce them. However, there is no direct way + to invoke the correct implementation. + (6) As before, these broken implementations were created by making + small modifications to the correct implementation. All of them + can be found by reasonably crafted tests. The one that is likely + to be the most tricky arises due to the programmer using a variable + of an incorrect type somewhere (hint)... +You should submit test-power.c when you are finished. diff --git a/22_tests_power/grade.txt b/22_tests_power/grade.txt new file mode 100644 index 0000000..594e9a4 --- /dev/null +++ b/22_tests_power/grade.txt @@ -0,0 +1,53 @@ +Grading at Sat 30 Oct 2021 01:44:08 AM UTC +**Testing broken implementation 10 ** +------------------------------------- + + +**Testing broken implementation 11 ** +------------------------------------- + + +**Testing broken implementation 1 ** +------------------------------------- + + +**Testing broken implementation 2 ** +------------------------------------- + + +**Testing broken implementation 3 ** +------------------------------------- + + +**Testing broken implementation 4 ** +------------------------------------- + + +**Testing broken implementation 5 ** +------------------------------------- + + +**Testing broken implementation 6 ** +------------------------------------- + + +**Testing broken implementation 7 ** +------------------------------------- + + +**Testing broken implementation 8 ** +------------------------------------- + + +**Testing broken implementation 9 ** +------------------------------------- + + +**Testing correct implementation ** +------------------------------------- + + +All tests were ok. + + +Overall Grade: PASSED diff --git a/22_tests_power/next-README b/22_tests_power/next-README new file mode 100644 index 0000000..60bc3f1 --- /dev/null +++ b/22_tests_power/next-README @@ -0,0 +1,18 @@ +Write a recursive function unsigned power(unsigned x, unsigned y) which computes x +to the y power. Note that while 0 to the 0 is undefined in mathematics, +we specify that 0 to the 0 shall be 1 for this function. + +Save it into a file called power.c + +You MUST use recursion (no iteration). + + +Use your test-power.c from your previous assignment to test your implementation +of power. You can symlink the file from the previous assignment into this directory: + +ln -s ../22_tests_power/test-power.c ./ + +You should then write a Makefile which will compile each C file separately +to an object file, and like them together. + + diff --git a/22_tests_power/run_all.sh b/22_tests_power/run_all.sh new file mode 100755 index 0000000..68f709d --- /dev/null +++ b/22_tests_power/run_all.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +for i in /usr/local/l2p/power/power*.o +do + test=`basename $i | sed 's/power//' | sed 's/.o//'` + if [ "$test" == "" ] + then + echo "**Testing correct implementation **" + else + echo "**Testing broken implementation ${test} **" + fi + echo "-------------------------------------" + echo "" + gcc -o test-power test-power.c $i + if [ "$?" != "0" ] + then + echo "Could not compile test-power.c with $i" > /dev/stderr + exit 1 + fi + ./test-power + if [ "$?" != 0 ] + then + if [ "$test" == "" ] + then + echo "Your test program falsely failed the correct implementation!" > /dev/stderr + exit 1 + fi + else + if [ "$test" != "" ] + then + echo "Your test program did not identify $i as broken!" > /dev/stderr + exit 1 + fi + fi + echo "" +done diff --git a/22_tests_power/test-power b/22_tests_power/test-power new file mode 100755 index 0000000..8b4cab0 Binary files /dev/null and b/22_tests_power/test-power differ diff --git a/22_tests_power/test-power.c b/22_tests_power/test-power.c new file mode 100644 index 0000000..95cb54e --- /dev/null +++ b/22_tests_power/test-power.c @@ -0,0 +1,88 @@ +#include +#include + +unsigned power (unsigned x, unsigned y); + +int main() { + unsigned x; + unsigned y; + unsigned ans; + + x = 2; + y = 0; + ans = 1; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + x = 1; + y = 0; + ans = 1; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 1; + y = 1; + ans = 1; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 2; + y = 1; + ans = 2; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 2; + y = 3; + ans = 8; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 3; + y = 3; + ans = 27; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 1; + y = 3; + ans = 1; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 0; + y = 0; + ans = 1; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = 3; + y = 2; + ans = 9; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = -3; + y = 2; + ans = 9; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + x = -2; + y = 3; + ans = -8; + if (ans != power(x, y)) { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3