diff --git a/morse.c b/morse.c index 87a8c32..2f69e27 100644 --- a/morse.c +++ b/morse.c @@ -51,8 +51,8 @@ const char* char_to_morse(char letter){ char* string_to_morse(char* string){ int string_len = strlen(string); // worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces) - char result[string_len*8]; - char* result_ptr = result; + // +1 for NULL terminator + char* result = malloc(sizeof(char)*string_len*8); // sets everything to null in the string, just in case there was data there previously. strcpy(result, ""); for (int i = 0; i < string_len; i++){ @@ -61,7 +61,7 @@ char* string_to_morse(char* string){ strcat(result, " "); } } - return result_ptr; + return result; } const char morse_to_char(const char* morse){ @@ -72,18 +72,16 @@ const char morse_to_char(const char* morse){ } } -char* morse_to_string(char* morse_to_cpy){ +char* morse_to_string(const char* morse_to_cpy){ int morse_length = strlen(morse_to_cpy); char morse[morse_length]; strcpy(morse, morse_to_cpy); - // TODO: make this dynamic - int max_size = 1024; - char result[max_size]; - printf("%d\n", morse_length); - char* result_ptr = result; + // allocate the amount of space the morse takes up for the text string + // this could be changed, but I don't see the point in optimizing the small things here. + char* result = malloc(sizeof(char)*strlen(morse)); // copy nothing to the string to avoid extra data - strcpy(result, ""); + //strcpy(result, ""); // split by the space character char* morse_ptr = strtok(morse, " "); @@ -95,7 +93,7 @@ char* morse_to_string(char* morse_to_cpy){ // reset the morse ptr for the next section between a space morse_ptr = strtok(NULL, " "); } - return result_ptr; + return result; } char *multi_tok(char *input, char *delimiter) { diff --git a/morse.h b/morse.h index b27207a..8cb7562 100644 --- a/morse.h +++ b/morse.h @@ -4,7 +4,7 @@ const char * char_to_morse(char letter); char * string_to_morse(char* string); char * from_morse(char* string); const char morse_to_char(const char* morse); -char * morse_to_string( char* morse); +char * morse_to_string( const char* morse); void print_morse(char* string, bool b1, bool b2, long s1, long s2); void reverse_morse(char* string); diff --git a/test.c b/test.c index 74be852..adef2ec 100644 --- a/test.c +++ b/test.c @@ -1,5 +1,6 @@ #include #include +#include #include "morse.h" int tests_run = 0; @@ -21,15 +22,18 @@ const char* FAIL_FORMAT = "\e[31m[%s]: \"%s\" %s \"%s\"\e[0m\n"; const char* PASS_CHAR_FORMAT = "\e[32m[%s]: \'%c\' %s \'%c\'\e[0m\n"; const char* FAIL_CHAR_FORMAT = "\e[31m[%s]: \'%c\' %s \'%c\'\e[0m\n"; +const char* FUNCTION_CALL_CFORMAT = "%s(\'%c\'):\n"; +const char* FUNCTION_CALL_SFORMAT = "%s(\"%s\"):\n"; + void assert_char_eq(const char c1, const char c2){ - tests_run++; - if (c1 == c2){ - printf(PASS_CHAR_FORMAT, PASS, c1, EQ, c2); - tests_passed++; - } else { - printf(FAIL_CHAR_FORMAT, FAIL, c1, NEQ, c2); - tests_failed++; - } + tests_run++; + if (c1 == c2){ + printf(PASS_CHAR_FORMAT, PASS, c1, EQ, c2); + tests_passed++; + } else { + printf(FAIL_CHAR_FORMAT, FAIL, c1, NEQ, c2); + tests_failed++; + } } void assert_str_eq(const char* s1, const char* s2){ @@ -56,24 +60,28 @@ void assert_str_neq(const char* s1, const char* s2){ void assert_ctm(char input, char* output){ const char* result = char_to_morse(input); + printf(FUNCTION_CALL_CFORMAT, "char_to_morse", input); assert_str_eq(result, output); } void assert_stm(char* input, char* output){ - char result[strlen(input)*8]; + char* result = malloc(sizeof(char)*strlen(input)*8); // fill with blanks in case of previous data // strcpy(result, ""); - strcpy(result, string_to_morse(input)); + result = string_to_morse(input); + printf(FUNCTION_CALL_SFORMAT, "string_to_morse", input); assert_str_eq(result, output); } void assert_mtc(char* input, char output){ char result = morse_to_char(input); + printf(FUNCTION_CALL_SFORMAT, "morse_to_char", input); assert_char_eq(result, output); } void assert_mts(char* input, char* output){ - char* result = morse_to_string(input); + char* result = morse_to_string(input); + printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input); assert_str_eq(result, output); }