From 72270ceab723ef9858387e0ce39f7072c42c28d1 Mon Sep 17 00:00:00 2001 From: LeakedMemory Date: Sun, 10 Dec 2023 09:53:17 -0600 Subject: [PATCH 1/5] sorts solutions prior to check --- exercises/practice/word-count/test_word_count.c | 11 +++++++++++ exercises/practice/word-count/word_count.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/exercises/practice/word-count/test_word_count.c b/exercises/practice/word-count/test_word_count.c index 17cf8958a..2751098f6 100644 --- a/exercises/practice/word-count/test_word_count.c +++ b/exercises/practice/word-count/test_word_count.c @@ -17,6 +17,13 @@ void tearDown(void) { } + +int cmp_word_count_word(const void *a, const void *b){ + word_count_word_t *w1 = (word_count_word_t *)a; + word_count_word_t *w2 = (word_count_word_t *)b; + return strcmp(w1->text, w2->text); +} + static void check_solution(word_count_word_t *expected_solution, int expected_word_count, word_count_word_t *actual_solution, @@ -25,6 +32,10 @@ static void check_solution(word_count_word_t *expected_solution, // All words counted? TEST_ASSERT_EQUAL(expected_word_count, actual_word_count); + // Sort the words before comparing + qsort(expected_solution, expected_word_count, sizeof(word_count_word_t), cmp_word_count_word); + qsort(actual_solution, actual_word_count, sizeof(word_count_word_t), cmp_word_count_word); + // now test the word count for the words... for (int index = 0; index < MAX_WORDS; index++) { TEST_ASSERT_EQUAL(expected_solution[index].count, diff --git a/exercises/practice/word-count/word_count.h b/exercises/practice/word-count/word_count.h index d42100a84..36b1a00d4 100644 --- a/exercises/practice/word-count/word_count.h +++ b/exercises/practice/word-count/word_count.h @@ -26,4 +26,7 @@ typedef struct word_count_word { // words will contain the results up to that point. int count_words(const char *sentence, word_count_word_t *words); +// used to sort the words before comparison +int cmp_word_count_word(const void *a, const void *b); + #endif From db56fa035ef049cfbb9d40da21768edd997db7b4 Mon Sep 17 00:00:00 2001 From: LeakedMemory Date: Sun, 10 Dec 2023 16:39:32 -0600 Subject: [PATCH 2/5] Update exercises/practice/word-count/word_count.h removes definition from header file Co-authored-by: Ryan Hartlage --- exercises/practice/word-count/word_count.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/exercises/practice/word-count/word_count.h b/exercises/practice/word-count/word_count.h index 36b1a00d4..d42100a84 100644 --- a/exercises/practice/word-count/word_count.h +++ b/exercises/practice/word-count/word_count.h @@ -26,7 +26,4 @@ typedef struct word_count_word { // words will contain the results up to that point. int count_words(const char *sentence, word_count_word_t *words); -// used to sort the words before comparison -int cmp_word_count_word(const void *a, const void *b); - #endif From 3c23521df84f91f7b7a2fad5b51aa6e23dadfb1c Mon Sep 17 00:00:00 2001 From: LeakedMemory Date: Sun, 10 Dec 2023 16:40:12 -0600 Subject: [PATCH 3/5] Update exercises/practice/word-count/test_word_count.c makes function static so no definition is needed in a header file. Co-authored-by: Ryan Hartlage --- exercises/practice/word-count/test_word_count.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exercises/practice/word-count/test_word_count.c b/exercises/practice/word-count/test_word_count.c index 2751098f6..3f97db871 100644 --- a/exercises/practice/word-count/test_word_count.c +++ b/exercises/practice/word-count/test_word_count.c @@ -17,8 +17,7 @@ void tearDown(void) { } - -int cmp_word_count_word(const void *a, const void *b){ +static int cmp_word_count_word(const void *a, const void *b){ word_count_word_t *w1 = (word_count_word_t *)a; word_count_word_t *w2 = (word_count_word_t *)b; return strcmp(w1->text, w2->text); From f1ae583e894ca3464576213d3584b7d5331a92cc Mon Sep 17 00:00:00 2001 From: LeakedMemory Date: Sun, 10 Dec 2023 20:12:43 -0600 Subject: [PATCH 4/5] use clang-format --- exercises/practice/word-count/test_word_count.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/exercises/practice/word-count/test_word_count.c b/exercises/practice/word-count/test_word_count.c index 3f97db871..9a372d0fd 100644 --- a/exercises/practice/word-count/test_word_count.c +++ b/exercises/practice/word-count/test_word_count.c @@ -17,10 +17,11 @@ void tearDown(void) { } -static int cmp_word_count_word(const void *a, const void *b){ - word_count_word_t *w1 = (word_count_word_t *)a; - word_count_word_t *w2 = (word_count_word_t *)b; - return strcmp(w1->text, w2->text); +static int cmp_word_count_word(const void *a, const void *b) +{ + word_count_word_t *w1 = (word_count_word_t *)a; + word_count_word_t *w2 = (word_count_word_t *)b; + return strcmp(w1->text, w2->text); } static void check_solution(word_count_word_t *expected_solution, @@ -32,8 +33,10 @@ static void check_solution(word_count_word_t *expected_solution, TEST_ASSERT_EQUAL(expected_word_count, actual_word_count); // Sort the words before comparing - qsort(expected_solution, expected_word_count, sizeof(word_count_word_t), cmp_word_count_word); - qsort(actual_solution, actual_word_count, sizeof(word_count_word_t), cmp_word_count_word); + qsort(expected_solution, expected_word_count, sizeof(word_count_word_t), + cmp_word_count_word); + qsort(actual_solution, actual_word_count, sizeof(word_count_word_t), + cmp_word_count_word); // now test the word count for the words... for (int index = 0; index < MAX_WORDS; index++) { From 632db69f721e797671237ae562c8e28e6b11303a Mon Sep 17 00:00:00 2001 From: LeakedMemory Date: Sun, 10 Dec 2023 20:15:05 -0600 Subject: [PATCH 5/5] name function compare word --- exercises/practice/word-count/test_word_count.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/practice/word-count/test_word_count.c b/exercises/practice/word-count/test_word_count.c index 9a372d0fd..5c2b3ac44 100644 --- a/exercises/practice/word-count/test_word_count.c +++ b/exercises/practice/word-count/test_word_count.c @@ -17,7 +17,7 @@ void tearDown(void) { } -static int cmp_word_count_word(const void *a, const void *b) +static int compare_word(const void *a, const void *b) { word_count_word_t *w1 = (word_count_word_t *)a; word_count_word_t *w2 = (word_count_word_t *)b; @@ -34,9 +34,9 @@ static void check_solution(word_count_word_t *expected_solution, // Sort the words before comparing qsort(expected_solution, expected_word_count, sizeof(word_count_word_t), - cmp_word_count_word); + compare_word); qsort(actual_solution, actual_word_count, sizeof(word_count_word_t), - cmp_word_count_word); + compare_word); // now test the word count for the words... for (int index = 0; index < MAX_WORDS; index++) {