diff --git a/exercises/ex04/bigbadint.c b/exercises/ex04/bigbadint.c index 04ffe991..a4cadde2 100644 --- a/exercises/ex04/bigbadint.c +++ b/exercises/ex04/bigbadint.c @@ -1,60 +1,36 @@ /* Example code for Exercises in C. - This program shows a way to represent a BigInt type (arbitrary length integers) using C strings, with numbers represents as a string of decimal digits in reverse order. - This program contains two deliberate errors as a debugging exercise. - 1) Compile and run this program like this: - gcc -g -std=c99 bigbadint.c ./a.out - You should see that reverse_string passes its test, but itoc and add_digits don't. - 2) Read the GDB tutorial at - http://web.eecs.umich.edu/~sugih/pointers/summary.html - 3) Start gdb like this: - gdb a.out - 4) At the gdb prompt, type - run - to run the program. Since itoc is failing, let's look at its source code: - list itoc - And set a break point at the beginning of itoc - break itoc - Now if you run the program again, it should stop every time itoc is called, and you'll be able to see the value of the parameter. - To run a single line of code, type - step - To print the value of a variable - print i - 5) Read the descriptions of step and next, try them out, and make sure you know the difference. - 6) See if you can find the error in itoc, and fix it. - 7) In main, uncomment the line that calls test_reverse_string_again. Compile and run the program again. You should see that reverse_string is actually not correct, even though it passes its test. See if you can debug it. - */ #include @@ -64,9 +40,7 @@ its test. See if you can debug it. #include /* reverse_string: Returns a new string with the characters reversed. - It is the caller's responsibility to free the result. - s: string returns: string */ @@ -88,7 +62,6 @@ char *reverse_string(char *s) { } /* ctoi: Converts a character to integer. - c: one of the characters '0' to '9' returns: integer 0 to 9 */ @@ -98,7 +71,6 @@ int ctoi(char c) { } /* itoc: Converts an integer to character. - i: integer 0 to 9 returns: character '0' to '9' */ @@ -108,16 +80,13 @@ char itoc(int i) { } /* add_digits: Adds two decimal digits, returns the total and carry. - For example, if a='5', b='6', and carry='1', the sum is 12, so the output value of total should be '2' and carry should be '1' - a: character '0' to '9' b: character '0' to '9' c: character '0' to '9' total: pointer to char carry: pointer to char - */ void add_digits(char a, char b, char c, char *total, char *carry) { int sum = ctoi(a) + ctoi(b) + ctoi(c); @@ -132,9 +101,7 @@ void add_digits(char a, char b, char c, char *total, char *carry) { typedef char * BigInt; /* add_bigint: Adds two BigInts - Stores the result in z. - x: BigInt y: BigInt carry_in: char @@ -180,7 +147,6 @@ void add_bigint(BigInt x, BigInt y, char carry_in, BigInt z) { } /* print_bigint: Prints the digits of BigInt in the normal order. - big: BigInt */ void print_bigint(BigInt big) { @@ -191,9 +157,7 @@ void print_bigint(BigInt big) { } /* make_bigint: Creates and returns a BigInt. - Caller is responsible for freeing. - s: string of digits in the usual order returns: BigInt */ diff --git a/exercises/ex04/endswith.c b/exercises/ex04/endswith.c index af0d9432..a8ee9d64 100644 --- a/exercises/ex04/endswith.c +++ b/exercises/ex04/endswith.c @@ -1,8 +1,6 @@ /* Example code for Software Systems at Olin College. - Copyright 2017 Allen Downey License: Creative Commons Attribution-ShareAlike 3.0 - */ #include @@ -12,7 +10,6 @@ License: Creative Commons Attribution-ShareAlike 3.0 #include "endswith.h" /* endswith: Checks whether s ends with suffix. - s: string suffix: string returns: 1 if true, 0 otherwise diff --git a/exercises/ex04/minunit.h b/exercises/ex04/minunit.h index b059e9e5..5a60a379 100644 --- a/exercises/ex04/minunit.h +++ b/exercises/ex04/minunit.h @@ -1,17 +1,12 @@ /* minunit.h - From: http://www.jera.com/techinfo/jtns/jtn002.html - License: "You may use the code in this tech note for any purpose, with the understanding that it comes with NO WARRANTY." - Note on why the macros are wrapped in a do..while statement: http://www.eskimo.com/~scs/C-faq/q10.4.html - */ /* mu_assert: If test is false, returns message. - Note that because this is a macro, it returns from whatever function it is used in. */ @@ -19,10 +14,8 @@ function it is used in. /* mu_run_test: Runs the given test function. - If the function returns a non-null message, mu_run_test returns the same message. - */ #define mu_run_test(test) do { char *message = test(); tests_run++; \ if (message) return message; } while (0) @@ -31,7 +24,5 @@ the same message. `tests_run` is only used in the testing module, and I don't see a reason it should be accessible from the unit under test. - extern int tests_run; - */ diff --git a/exercises/ex04/test_endswith.c b/exercises/ex04/test_endswith.c index 21dfd464..3d4d2222 100644 --- a/exercises/ex04/test_endswith.c +++ b/exercises/ex04/test_endswith.c @@ -1,8 +1,6 @@ /* Example code for Software Systems at Olin College. - Copyright 2017 Allen Downey License: Creative Commons Attribution-ShareAlike 3.0 - */ #include @@ -15,41 +13,41 @@ License: Creative Commons Attribution-ShareAlike 3.0 int tests_run = 0; static char *test1() { - int res = endswith("endswith", "swith"); - char *message = "test1 failed: endswith(\"endswith\", \"swith\") should return 1"; - mu_assert(message, res == 1); - return NULL; + int res = endswith("endswith", "swith"); + char *message = "test1 failed: endswith(\"endswith\", \"swith\") should return 1"; + mu_assert(message, res == 1); + return NULL; } static char *test2() { - int res = endswith("endswith", "ends"); - char *message = "test2 failed: endswith(\"endswith\", \"ends\") should return 0"; - mu_assert(message, res == 0); - return NULL; + int res = endswith("endswith", "ends"); + char *message = "test2 failed: endswith(\"endswith\", \"ends\") should return 0"; + mu_assert(message, res == 0); + return NULL; } static char *test3() { - int res = endswith("endswith", "offendswith"); - char *message = "test3 failed: endswith(\"offendswith\", \"swith\") should return 0"; - mu_assert(message, res == 0); - return NULL; + int res = endswith("endswith", "offendswith"); + char *message = "test3 failed: endswith(\"offendswith\", \"swith\") should return 0"; + mu_assert(message, res == 0); + return NULL; } static char * all_tests() { - mu_run_test(test1); - mu_run_test(test2); - mu_run_test(test3); - return NULL; + mu_run_test(test1); + mu_run_test(test2); + mu_run_test(test3); + return NULL; } int main(int argc, char **argv) { - char *result = all_tests(); - if (result != NULL) { - printf("%s\n", result); - } else { - printf("ALL TESTS PASSED\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; + char *result = all_tests(); + if (result != NULL) { + printf("%s\n", result); + } else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return result != 0; } diff --git a/exercises/ex04/test_util.c b/exercises/ex04/test_util.c new file mode 100644 index 00000000..91db5165 --- /dev/null +++ b/exercises/ex04/test_util.c @@ -0,0 +1,53 @@ +/* Example code for Software Systems at Olin College. +Copyright 2017 Allen Downey +License: Creative Commons Attribution-ShareAlike 3.0 +*/ + +#include +#include +#include +#include +#include "endswith.h" +#include "minunit.h" + +int tests_run = 0; + +static char *test1() { + int res = endswith("endswith", "swith"); + char *message = "test1 failed: endswith(\"endswith\", \"swith\") should return 1"; + mu_assert(message, res == 1); + return NULL; +} + +static char *test2() { + int res = endswith("endswith", "ends"); + char *message = "test2 failed: endswith(\"endswith\", \"ends\") should return 0"; + mu_assert(message, res == 0); + return NULL; +} + +static char *test3() { + int res = endswith("endswith", "offendswith"); + char *message = "test3 failed: endswith(\"offendswith\", \"swith\") should return 0"; + mu_assert(message, res == 0); + return NULL; +} + +static char * all_tests() { + mu_run_test(test1); + mu_run_test(test2); + mu_run_test(test3); + return NULL; +} + +int main(int argc, char **argv) { + char *result = all_tests(); + if (result != NULL) { + printf("%s\n", result); + } else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return result != 0; +}