Skip to content

Commit

Permalink
Adding files + submission
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat committed Feb 28, 2020
1 parent 6c08a47 commit bcb2f58
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 75 deletions.
36 changes: 0 additions & 36 deletions exercises/ex04/bigbadint.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
Expand All @@ -64,9 +40,7 @@ its test. See if you can debug it.
#include <assert.h>

/* reverse_string: Returns a new string with the characters reversed.
It is the caller's responsibility to free the result.
s: string
returns: string
*/
Expand All @@ -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
*/
Expand All @@ -98,7 +71,6 @@ int ctoi(char c) {
}

/* itoc: Converts an integer to character.
i: integer 0 to 9
returns: character '0' to '9'
*/
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
*/
Expand Down
3 changes: 0 additions & 3 deletions exercises/ex04/endswith.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* Example code for Software Systems at Olin College.
Copyright 2017 Allen Downey
License: Creative Commons Attribution-ShareAlike 3.0
*/

#include <stdio.h>
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions exercises/ex04/minunit.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
/* 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.
*/
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)


/* 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)
Expand All @@ -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;
*/
52 changes: 25 additions & 27 deletions exercises/ex04/test_endswith.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* Example code for Software Systems at Olin College.
Copyright 2017 Allen Downey
License: Creative Commons Attribution-ShareAlike 3.0
*/

#include <stdio.h>
Expand All @@ -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;
}
53 changes: 53 additions & 0 deletions exercises/ex04/test_util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Example code for Software Systems at Olin College.
Copyright 2017 Allen Downey
License: Creative Commons Attribution-ShareAlike 3.0
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#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;
}

0 comments on commit bcb2f58

Please sign in to comment.