Skip to content

Commit

Permalink
sorts solutions prior to check (#932)
Browse files Browse the repository at this point in the history
* sorts solutions prior to check

* Update exercises/practice/word-count/word_count.h

removes definition from header file

Co-authored-by: Ryan Hartlage <[email protected]>

* 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 <[email protected]>

* use clang-format

* name function compare word

---------

Co-authored-by: Ryan Hartlage <[email protected]>
  • Loading branch information
elmq0022 and ryanplusplus authored Dec 11, 2023
1 parent 196f7a4 commit 642688e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions exercises/practice/word-count/test_word_count.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 642688e

Please sign in to comment.