From 642688edcdd658eed8bba41aade53e12a53ced7f Mon Sep 17 00:00:00 2001 From: LeakedMemory Date: Sun, 10 Dec 2023 20:16:40 -0600 Subject: [PATCH] sorts solutions prior to check (#932) * sorts solutions prior to check * Update exercises/practice/word-count/word_count.h removes definition from header file Co-authored-by: Ryan Hartlage * 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 * use clang-format * name function compare word --------- Co-authored-by: Ryan Hartlage --- exercises/practice/word-count/test_word_count.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/exercises/practice/word-count/test_word_count.c b/exercises/practice/word-count/test_word_count.c index 17cf8958a..5c2b3ac44 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) { } +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; + 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,12 @@ 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), + compare_word); + qsort(actual_solution, actual_word_count, sizeof(word_count_word_t), + compare_word); + // now test the word count for the words... for (int index = 0; index < MAX_WORDS; index++) { TEST_ASSERT_EQUAL(expected_solution[index].count,