WOrking on tests

master
Tait Hoyem 5 years ago
parent 9a3e0b6a43
commit a9d69ba892

@ -1,9 +0,0 @@
/* file: minunit.h */
#include <string.h>
#define mu_assert_str(function_name, input, output) do{ char* result = function_name(input);tests_run++; if (!(strcmp(result, output)==0)){printf("[FAILED] ");tests_failed++;}else{printf("[PASSED] ");tests_passed++;}; printf("%s(\"%s\"): \"%s\" == \"%s\"\n", function_name, input, result, output); } while (0)
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_assert_neg(message, test) do { if ((test)){tests_failed++;return message;} else {tests_passed++;} } while (0)
#define mu_run_test(test) do { char *message = test(); tests_run++; \
if (message) return message; } while (0)
extern int tests_run;

@ -6,34 +6,44 @@
#include <limits.h>
#include <stdlib.h>
#define NUM_OF_LETTERS 36
#define NUM_OF_SYMBOLS 37
static char LOWERCASE_LETTERS[NUM_OF_LETTERS] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
static char UPPERCASE_LETTERS[NUM_OF_LETTERS] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
static const char LOWERCASE_LETTERS[NUM_OF_SYMBOLS] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '};
static const char UPPERCASE_LETTERS[NUM_OF_SYMBOLS] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '};
// 6 spaces are needed to store the null terminator :)
// C is fun?
static char MORSE[NUM_OF_LETTERS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
char* char_to_morse(char letter){
char result[6];
for (int i = 0; i < NUM_OF_LETTERS; i++){
static const char MORSE[NUM_OF_SYMBOLS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "/"};
// This function returns a pointer to the morse code written above.
// The genuis of this function feels amazing because I've never used C before, but I see why
// ... it's used for performence!
//
// I do not copy ANY data. The pointer returned points to the above array!
const char* char_to_morse(char letter){
const char* result_ptr;
for (int i = 0; i < NUM_OF_SYMBOLS; i++){
if (letter == UPPERCASE_LETTERS[i] || letter == LOWERCASE_LETTERS[i]){
strcpy(&result, MORSE[i]);
//result = MORSE[i];
result_ptr = MORSE[i];
}
}
return result;
return result_ptr;
}
char * to_morse(char* string){
char* string_to_morse(char* string){
int string_len = strlen(string);
// worse possible case is 6 times the length (assuming all numbers)
char* result;
for (int i = 0; i < strlen(string); i++){
// worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces)
char result[string_len*8];
char* result_ptr = result;
// 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++){
strcat(result, char_to_morse(string[i]));
if (i != string_len-1){
strcat(result, " ");
}
}
return result;
return result_ptr;
}
char *multi_tok(char *input, char *delimiter) {
@ -64,7 +74,7 @@ void print_single_morse(char charToMorse, bool isSlow, bool addLetter, long dotD
char convertedTo[6];
// Loop through every character in the list of letters
for (int j = 0; j < NUM_OF_LETTERS; j++){
for (int j = 0; j < NUM_OF_SYMBOLS; j++){
// If the character in the input string matches the character the character from the list
if (charToMorse == LOWERCASE_LETTERS[j] || charToMorse == UPPERCASE_LETTERS[j]){
strcpy(convertedTo, MORSE[j]);
@ -120,7 +130,7 @@ void reverse_morse(char* morseToNormalize){
char convertedChar;
// go through every morse encoding
for (int i = 0; i < NUM_OF_LETTERS; i++){
for (int i = 0; i < NUM_OF_SYMBOLS; i++){
// if the morse code is euqal to the input string
if (strcmp(MORSE[i], morseToNormalize) == 0){
isConvertable = true;

@ -1,7 +1,7 @@
#include <stdbool.h>
char * char_to_morse(char letter);
char * to_morse(char* string);
const char * char_to_morse(char letter);
char * string_to_morse(char* string);
char * from_morse(char* string);
void print_morse(char* string, bool b1, bool b2, long s1, long s2);

163
test.c

@ -1,68 +1,133 @@
#include <stdio.h>
#include "minunit.h"
#include <string.h>
#include "morse.h"
int tests_run = 0;
int tests_passed = 0;
int tests_failed = 0;
static char * test_tests1(){
mu_assert("Error, - != -", "-" == "-");
return 0;
const char* FAIL = "FAIL";
const char* PASS = "PASS";
const char* EQ = "==";
const char* NEQ = "!=";
const char* PASS_FORMAT = "[%s]: \"%s\" %s \"%s\"\n";
const char* FAIL_FORMAT = "[%s]: \"%s\" %s \"%s\"\n";
void assert_str_eq(const char* s1, const char* s2){
tests_run++;
if (strcmp(s1, s2) == 0){
printf(PASS_FORMAT, PASS, s1, EQ, s2);
tests_passed++;
} else {
printf(PASS_FORMAT, FAIL, s1, NEQ, s2);
tests_failed++;
}
}
static char * test_tests2(){
mu_assert_neg("Error, - == .", "-" == ".");
return 0;
void assert_str_neq(const char* s1, const char* s2){
tests_run++;
if (strcmp(s1, s2) != 0){
printf(PASS_FORMAT, PASS, s1, NEQ, s2);
tests_passed++;
} else {
printf(PASS_FORMAT, FAIL, s1, EQ, s2);
tests_failed++;
}
}
static char * test_to_morse(){
char_to_morse('a');
printf("trying to run ctm\n");
char* r1;
r1 = char_to_morse('a');
printf("ctm('a') works!");
mu_assert_str(char_to_morse, 'a', ".-");
mu_assert_str(char_to_morse, 'b', "-...");
mu_assert_str(char_to_morse, 'c', "-.-.");
mu_assert_str(char_to_morse, 'd', "-..");
mu_assert_str(char_to_morse, 'e', ".");
mu_assert_str(char_to_morse, 'f', "..-.");
mu_assert_str(char_to_morse, 'g', "--.");
mu_assert_str(char_to_morse, 'h', "....");
mu_assert_str(char_to_morse, 'i', "..");
mu_assert_str(char_to_morse, 'j', ".---");
mu_assert_str(char_to_morse, 'k', "-.-");
mu_assert_str(char_to_morse, 'l', ".-..");
mu_assert_str(char_to_morse, 'm', "--");
mu_assert_str(char_to_morse, 'n', "-.");
mu_assert_str(char_to_morse, 'o', "---");
mu_assert_str(char_to_morse, 'p', ".--.");
mu_assert_str(char_to_morse, 'q', "--.-");
mu_assert_str(char_to_morse, 'r', ".-.");
mu_assert_str(char_to_morse, 's', "...");
mu_assert_str(char_to_morse, 't', "-");
mu_assert_str(char_to_morse, 'u', "..-");
mu_assert_str(char_to_morse, 'v', "...-");
mu_assert_str(char_to_morse, 'w', ".--");
mu_assert_str(char_to_morse, 'x', "-..-");
mu_assert_str(char_to_morse, 'y', "-.--");
mu_assert_str(char_to_morse, 'z', "--..");
return 0;
void assert_ctm(char input, char* output){
const char* result = char_to_morse(input);
assert_str_eq(result, output);
}
static char * all_tests(){
mu_run_test(test_tests1);
printf("test_tests1 complete!\n");
mu_run_test(test_tests2);
printf("test_tests2 complete!\n");
mu_run_test(test_to_morse);
printf("test_to_morse complete!\n");
return 0;
void assert_stm(char* input, char* output){
char result[strlen(input)*8];
// fill with blanks in case of previous data
strcpy(result, "");
strcpy(result, string_to_morse(input));
assert_str_eq(result, output);
}
void test_tests(){
assert_str_eq(".", ".");
assert_str_neq("+", "-");
}
void test_to_morse(){
assert_ctm('a', ".-");
assert_ctm('A', ".-");
assert_ctm('b', "-...");
assert_ctm('B', "-...");
assert_ctm('c', "-.-.");
assert_ctm('C', "-.-.");
assert_ctm('d', "-..");
assert_ctm('D', "-..");
assert_ctm('e', ".");
assert_ctm('E', ".");
assert_ctm('f', "..-.");
assert_ctm('F', "..-.");
assert_ctm('g', "--.");
assert_ctm('G', "--.");
assert_ctm('h', "....");
assert_ctm('H', "....");
assert_ctm('i', "..");
assert_ctm('I', "..");
assert_ctm('j', ".---");
assert_ctm('J', ".---");
assert_ctm('k', "-.-");
assert_ctm('K', "-.-");
assert_ctm('l', ".-..");
assert_ctm('L', ".-..");
assert_ctm('m', "--");
assert_ctm('M', "--");
assert_ctm('n', "-.");
assert_ctm('N', "-.");
assert_ctm('o', "---");
assert_ctm('O', "---");
assert_ctm('p', ".--.");
assert_ctm('P', ".--.");
assert_ctm('q', "--.-");
assert_ctm('Q', "--.-");
assert_ctm('r', ".-.");
assert_ctm('R', ".-.");
assert_ctm('s', "...");
assert_ctm('S', "...");
assert_ctm('t', "-");
assert_ctm('T', "-");
assert_ctm('u', "..-");
assert_ctm('U', "..-");
assert_ctm('v', "...-");
assert_ctm('V', "...-");
assert_ctm('w', ".--");
assert_ctm('W', ".--");
assert_ctm('x', "-..-");
assert_ctm('X', "-..-");
assert_ctm('y', "-.--");
assert_ctm('Y', "-.--");
assert_ctm('z', "--..");
assert_ctm('Z', "--..");
}
void test_string_to_morse(){
assert_stm("Hello", ".... . .-. .-. ..");
assert_stm("world", ".-- --- .-. .-.. -..");
assert_stm("Hello world!", ".... . .-. .-. .--- / .-- --- .-. .-.. -.. / -.-.--");
assert_stm("I! HATE! YOU!", ".. -.-.-- / .... .- - . -.-.-- / -.-- -- ..- -.-.--");
}
void all_tests(){
test_tests();
printf("test_tests tests complete!\n");
test_to_morse();
printf("char_to_morse tests complete!\n");
test_string_to_morse();
printf("string_to_morse tests complete!\n");
}
int main(int argc, char **argv) {
char *result = all_tests();
all_tests();
if (tests_run == tests_passed){
printf("ALL TESTS PASSED\n");
}

Loading…
Cancel
Save