exercism

Exercism solutions.
git clone git://code.dwrz.net/exercism
Log | Files | Refs

test_word_count.c (18785B)


      1 #include <stddef.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include "vendor/unity.h"
      5 #include "../src/word_count.h"
      6 
      7 #define STRING_SIZE (MAX_WORD_LENGTH + 1)
      8 
      9 word_count_word_t actual_solution[MAX_WORDS];
     10 word_count_word_t expected_solution[MAX_WORDS];
     11 void setUp(void)
     12 {
     13 }
     14 
     15 void tearDown(void)
     16 {
     17 }
     18 
     19 static void check_solution(word_count_word_t * expected_solution,
     20                            int expected_word_count,
     21                            word_count_word_t * actual_solution,
     22                            int actual_word_count)
     23 {
     24   // All words counted?
     25    TEST_ASSERT_EQUAL(expected_word_count, actual_word_count);
     26 
     27    // now test the word count for the words...
     28    for (int index = 0; index < MAX_WORDS; index++) {
     29        TEST_ASSERT_EQUAL(expected_solution[index].count,
     30                         actual_solution[index].count);
     31       TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_solution[index].text,
     32                                     actual_solution[index].text, STRING_SIZE);
     33    }
     34 }
     35 
     36 // Tests Start here
     37 void test_word_count_one_word(void)
     38 {
     39    int index = 0;
     40    int actual_word_count;
     41    char *input_text = "word";
     42    const int expected_word_count = 1;
     43 
     44    // build the expected solution
     45    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
     46 
     47    // fill in the expected words
     48    expected_solution[index].count = 1;
     49    strncpy(expected_solution[index++].text, "word", STRING_SIZE);
     50 
     51    actual_word_count = word_count(input_text, actual_solution);
     52 
     53    check_solution(expected_solution,
     54                   expected_word_count, actual_solution, actual_word_count);
     55 }
     56 
     57 void test_word_count_one_of_each_word(void)
     58 {
     59    int index = 0;
     60    int actual_word_count;
     61    char *input_text = "one of each";
     62    const int expected_word_count = 3;
     63 
     64    // build the expected solution
     65    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
     66 
     67    // fill in the expected words
     68    expected_solution[index].count = 1;
     69    strncpy(expected_solution[index++].text, "one", STRING_SIZE);
     70 
     71    expected_solution[index].count = 1;
     72    strncpy(expected_solution[index++].text, "of", STRING_SIZE);
     73 
     74    expected_solution[index].count = 1;
     75    strncpy(expected_solution[index++].text, "each", STRING_SIZE);
     76 
     77    actual_word_count = word_count(input_text, actual_solution);
     78 
     79    check_solution(expected_solution,
     80                   expected_word_count, actual_solution, actual_word_count);
     81 }
     82 
     83 void test_word_count_multiple_occurrences_of_a_word(void)
     84 {
     85    int index = 0;
     86    int actual_word_count;
     87    char *input_text = "one fish two fish red fish blue fish";
     88    const int expected_word_count = 5;
     89 
     90    // build the expected solution
     91    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
     92 
     93    expected_solution[index].count = 1;
     94    strncpy(expected_solution[index++].text, "one", STRING_SIZE);
     95 
     96    expected_solution[index].count = 4;
     97    strncpy(expected_solution[index++].text, "fish", STRING_SIZE);
     98 
     99    expected_solution[index].count = 1;
    100    strncpy(expected_solution[index++].text, "two", STRING_SIZE);
    101 
    102    expected_solution[index].count = 1;
    103    strncpy(expected_solution[index++].text, "red", STRING_SIZE);
    104 
    105    expected_solution[index].count = 1;
    106    strncpy(expected_solution[index++].text, "blue", STRING_SIZE);
    107 
    108    actual_word_count = word_count(input_text, actual_solution);
    109 
    110    check_solution(expected_solution,
    111                   expected_word_count, actual_solution, actual_word_count);
    112 }
    113 
    114 void test_word_count_handles_cramped_lists(void)
    115 {
    116    int index = 0;
    117    int actual_word_count;
    118    char *input_text = "one,two,three";
    119    const int expected_word_count = 3;
    120 
    121    // build the expected solution
    122    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    123 
    124    expected_solution[index].count = 1;
    125    strncpy(expected_solution[index++].text, "one", STRING_SIZE);
    126 
    127    expected_solution[index].count = 1;
    128    strncpy(expected_solution[index++].text, "two", STRING_SIZE);
    129 
    130    expected_solution[index].count = 1;
    131    strncpy(expected_solution[index++].text, "three", STRING_SIZE);
    132 
    133    actual_word_count = word_count(input_text, actual_solution);
    134 
    135    check_solution(expected_solution,
    136                   expected_word_count, actual_solution, actual_word_count);
    137 }
    138 
    139 void test_word_count_handles_expanded_lists(void)
    140 {
    141    int index = 0;
    142    int actual_word_count;
    143    char *input_text = "one,\ntwo,\nthree";
    144    const int expected_word_count = 3;
    145 
    146    // build the expected solution
    147    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    148 
    149    expected_solution[index].count = 1;
    150    strncpy(expected_solution[index++].text, "one", STRING_SIZE);
    151 
    152    expected_solution[index].count = 1;
    153    strncpy(expected_solution[index++].text, "two", STRING_SIZE);
    154 
    155    expected_solution[index].count = 1;
    156    strncpy(expected_solution[index++].text, "three", STRING_SIZE);
    157 
    158    actual_word_count = word_count(input_text, actual_solution);
    159 
    160    check_solution(expected_solution,
    161                   expected_word_count, actual_solution, actual_word_count);
    162 }
    163 
    164 void test_word_count_ignore_punctuation(void)
    165 {
    166    int index = 0;
    167    int actual_word_count;
    168    char *input_text = "car: carpet as java: javascript!!&@$%^&";
    169    const int expected_word_count = 5;
    170 
    171    // build the expected solution
    172    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    173 
    174    expected_solution[index].count = 1;
    175    strncpy(expected_solution[index++].text, "car", STRING_SIZE);
    176 
    177    expected_solution[index].count = 1;
    178    strncpy(expected_solution[index++].text, "carpet", STRING_SIZE);
    179 
    180    expected_solution[index].count = 1;
    181    strncpy(expected_solution[index++].text, "as", STRING_SIZE);
    182 
    183    expected_solution[index].count = 1;
    184    strncpy(expected_solution[index++].text, "java", STRING_SIZE);
    185 
    186    expected_solution[index].count = 1;
    187    strncpy(expected_solution[index++].text, "javascript", STRING_SIZE);
    188 
    189    actual_word_count = word_count(input_text, actual_solution);
    190 
    191    check_solution(expected_solution,
    192                   expected_word_count, actual_solution, actual_word_count);
    193 }
    194 
    195 void test_word_count_include_numbers(void)
    196 {
    197    int index = 0;
    198    int actual_word_count;
    199    char *input_text = "testing, 1, 2 testing";
    200    const int expected_word_count = 3;
    201 
    202    // build the expected solution
    203    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    204 
    205    expected_solution[index].count = 2;
    206    strncpy(expected_solution[index++].text, "testing", STRING_SIZE);
    207 
    208    expected_solution[index].count = 1;
    209    strncpy(expected_solution[index++].text, "1", STRING_SIZE);
    210 
    211    expected_solution[index].count = 1;
    212    strncpy(expected_solution[index++].text, "2", STRING_SIZE);
    213 
    214    actual_word_count = word_count(input_text, actual_solution);
    215 
    216    check_solution(expected_solution,
    217                   expected_word_count, actual_solution, actual_word_count);
    218 }
    219 
    220 void test_word_count_normalize_case(void)
    221 {
    222    int index = 0;
    223    int actual_word_count;
    224    char *input_text = "go Go GO Stop stop";
    225    const int expected_word_count = 2;
    226 
    227    // build the expected solution
    228    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    229 
    230    expected_solution[index].count = 3;
    231    strncpy(expected_solution[index++].text, "go", STRING_SIZE);
    232 
    233    expected_solution[index].count = 2;
    234    strncpy(expected_solution[index++].text, "stop", STRING_SIZE);
    235 
    236    actual_word_count = word_count(input_text, actual_solution);
    237 
    238    check_solution(expected_solution,
    239                   expected_word_count, actual_solution, actual_word_count);
    240 }
    241 
    242 void test_word_count_with_apostrophes(void)
    243 {
    244    int index = 0;
    245    int actual_word_count;
    246    char *input_text = "First: don't laugh. Then: don't cry.";
    247    const int expected_word_count = 5;
    248 
    249    // build the expected solution
    250    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    251 
    252    expected_solution[index].count = 1;
    253    strncpy(expected_solution[index++].text, "first", STRING_SIZE);
    254 
    255    expected_solution[index].count = 2;
    256    strncpy(expected_solution[index++].text, "don't", STRING_SIZE);
    257 
    258    expected_solution[index].count = 1;
    259    strncpy(expected_solution[index++].text, "laugh", STRING_SIZE);
    260 
    261    expected_solution[index].count = 1;
    262    strncpy(expected_solution[index++].text, "then", STRING_SIZE);
    263 
    264    expected_solution[index].count = 1;
    265    strncpy(expected_solution[index++].text, "cry", STRING_SIZE);
    266 
    267    actual_word_count = word_count(input_text, actual_solution);
    268 
    269    check_solution(expected_solution,
    270                   expected_word_count, actual_solution, actual_word_count);
    271 }
    272 
    273 void test_word_count_with_quotation(void)
    274 {
    275    int index = 0;
    276    int actual_word_count;
    277    char *input_text = "Joe can't tell between 'large' and large.";
    278    const int expected_word_count = 6;
    279 
    280    // build the expected solution
    281    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    282 
    283    expected_solution[index].count = 1;
    284    strncpy(expected_solution[index++].text, "joe", STRING_SIZE);
    285 
    286    expected_solution[index].count = 1;
    287    strncpy(expected_solution[index++].text, "can't", STRING_SIZE);
    288 
    289    expected_solution[index].count = 1;
    290    strncpy(expected_solution[index++].text, "tell", STRING_SIZE);
    291 
    292    expected_solution[index].count = 1;
    293    strncpy(expected_solution[index++].text, "between", STRING_SIZE);
    294 
    295    expected_solution[index].count = 2;
    296    strncpy(expected_solution[index++].text, "large", STRING_SIZE);
    297 
    298    expected_solution[index].count = 1;
    299    strncpy(expected_solution[index++].text, "and", STRING_SIZE);
    300 
    301    actual_word_count = word_count(input_text, actual_solution);
    302 
    303    check_solution(expected_solution,
    304                   expected_word_count, actual_solution, actual_word_count);
    305 }
    306 
    307 void test_word_count_from_example(void)
    308 {
    309    int index = 0;
    310    int actual_word_count;
    311    char *input_text = "olly olly in come free";
    312    const int expected_word_count = 4;
    313 
    314    // build the expected solution
    315    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    316 
    317    expected_solution[index].count = 2;
    318    strncpy(expected_solution[index++].text, "olly", STRING_SIZE);
    319 
    320    expected_solution[index].count = 1;
    321    strncpy(expected_solution[index++].text, "in", STRING_SIZE);
    322 
    323    expected_solution[index].count = 1;
    324    strncpy(expected_solution[index++].text, "come", STRING_SIZE);
    325 
    326    expected_solution[index].count = 1;
    327    strncpy(expected_solution[index++].text, "free", STRING_SIZE);
    328 
    329    actual_word_count = word_count(input_text, actual_solution);
    330 
    331    check_solution(expected_solution,
    332                   expected_word_count, actual_solution, actual_word_count);
    333 }
    334 
    335 void test_max_length_word(void)
    336 {
    337    int actual_word_count;
    338    int index = 0;
    339    char *input_text =
    340        "Look thisisaveeeeeerylongwordtypedwithoutusinganyspaces and look again, thisisaveeeeeerylongwordtypedwithoutusinganyspaces";
    341 
    342    const int expected_word_count = 4;
    343 
    344    // build the expected solution
    345    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    346 
    347    expected_solution[index].count = 2;
    348    strncpy(expected_solution[index++].text, "look", STRING_SIZE);
    349 
    350    expected_solution[index].count = 2;
    351    strncpy(expected_solution[index++].text,
    352            "thisisaveeeeeerylongwordtypedwithoutusinganyspaces", STRING_SIZE);
    353 
    354    expected_solution[index].count = 1;
    355    strncpy(expected_solution[index++].text, "and", STRING_SIZE);
    356 
    357    expected_solution[index].count = 1;
    358    strncpy(expected_solution[index++].text, "again", STRING_SIZE);
    359 
    360    actual_word_count = word_count(input_text, actual_solution);
    361 
    362    check_solution(expected_solution,
    363                   expected_word_count, actual_solution, actual_word_count);
    364 }
    365 
    366 void test_excessive_length_word(void)
    367 {
    368    int actual_word_count;
    369    int index = 0;
    370    char *input_text =
    371        "Look thisisanexcessivelylongwordthatsomeonetypedwithoutusingthespacebar enough";
    372 
    373    const int expected_word_count = EXCESSIVE_LENGTH_WORD;
    374    word_count_word_t expected_solution[MAX_WORDS];
    375 
    376    // build the expected solution
    377    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    378    expected_solution[index].count = 1;
    379    strncpy(expected_solution[index++].text, "look", STRING_SIZE);
    380 
    381    actual_word_count = word_count(input_text, actual_solution);
    382 
    383    check_solution(expected_solution,
    384                   expected_word_count, actual_solution, actual_word_count);
    385 }
    386 
    387 void test_max_number_words(void)
    388 {
    389    int actual_word_count;
    390    int index = 0;
    391    char *input_text =
    392        "Once upon a time, a long while in the past, there lived a strange little man who could spin straw into gold";
    393 
    394    const int expected_word_count = 20;
    395 
    396    // build the expected solution
    397    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    398 
    399    expected_solution[index].count = 1;
    400    strncpy(expected_solution[index++].text, "once", STRING_SIZE);
    401 
    402    expected_solution[index].count = 1;
    403    strncpy(expected_solution[index++].text, "upon", STRING_SIZE);
    404 
    405    expected_solution[index].count = 3;
    406    strncpy(expected_solution[index++].text, "a", STRING_SIZE);
    407 
    408    expected_solution[index].count = 1;
    409    strncpy(expected_solution[index++].text, "time", STRING_SIZE);
    410 
    411    expected_solution[index].count = 1;
    412    strncpy(expected_solution[index++].text, "long", STRING_SIZE);
    413 
    414    expected_solution[index].count = 1;
    415    strncpy(expected_solution[index++].text, "while", STRING_SIZE);
    416 
    417    expected_solution[index].count = 1;
    418    strncpy(expected_solution[index++].text, "in", STRING_SIZE);
    419 
    420    expected_solution[index].count = 1;
    421    strncpy(expected_solution[index++].text, "the", STRING_SIZE);
    422 
    423    expected_solution[index].count = 1;
    424    strncpy(expected_solution[index++].text, "past", STRING_SIZE);
    425 
    426    expected_solution[index].count = 1;
    427    strncpy(expected_solution[index++].text, "there", STRING_SIZE);
    428 
    429    expected_solution[index].count = 1;
    430    strncpy(expected_solution[index++].text, "lived", STRING_SIZE);
    431 
    432    expected_solution[index].count = 1;
    433    strncpy(expected_solution[index++].text, "strange", STRING_SIZE);
    434 
    435    expected_solution[index].count = 1;
    436    strncpy(expected_solution[index++].text, "little", STRING_SIZE);
    437 
    438    expected_solution[index].count = 1;
    439    strncpy(expected_solution[index++].text, "man", STRING_SIZE);
    440 
    441    expected_solution[index].count = 1;
    442    strncpy(expected_solution[index++].text, "who", STRING_SIZE);
    443 
    444    expected_solution[index].count = 1;
    445    strncpy(expected_solution[index++].text, "could", STRING_SIZE);
    446 
    447    expected_solution[index].count = 1;
    448    strncpy(expected_solution[index++].text, "spin", STRING_SIZE);
    449 
    450    expected_solution[index].count = 1;
    451    strncpy(expected_solution[index++].text, "straw", STRING_SIZE);
    452 
    453    expected_solution[index].count = 1;
    454    strncpy(expected_solution[index++].text, "into", STRING_SIZE);
    455 
    456    expected_solution[index].count = 1;
    457    strncpy(expected_solution[index++].text, "gold", STRING_SIZE);
    458 
    459    actual_word_count = word_count(input_text, actual_solution);
    460 
    461    check_solution(expected_solution,
    462                   expected_word_count, actual_solution, actual_word_count);
    463 }
    464 
    465 void test_excessive_number_words(void)
    466 {
    467    int actual_word_count;
    468    int index = 0;
    469    char *input_text =
    470        "Once upon a time, a long while in the past, there lived a strange little man who could spin straw into gold. His name was...";
    471 
    472    const int expected_word_count = EXCESSIVE_NUMBER_OF_WORDS;
    473 
    474    // build the expected solution
    475    memset(expected_solution, 0, sizeof(expected_solution));     // clear to start with a known value
    476 
    477    expected_solution[index].count = 1;
    478    strncpy(expected_solution[index++].text, "once", STRING_SIZE);
    479 
    480    expected_solution[index].count = 1;
    481    strncpy(expected_solution[index++].text, "upon", STRING_SIZE);
    482 
    483    expected_solution[index].count = 3;
    484    strncpy(expected_solution[index++].text, "a", STRING_SIZE);
    485 
    486    expected_solution[index].count = 1;
    487    strncpy(expected_solution[index++].text, "time", STRING_SIZE);
    488 
    489    expected_solution[index].count = 1;
    490    strncpy(expected_solution[index++].text, "long", STRING_SIZE);
    491 
    492    expected_solution[index].count = 1;
    493    strncpy(expected_solution[index++].text, "while", STRING_SIZE);
    494 
    495    expected_solution[index].count = 1;
    496    strncpy(expected_solution[index++].text, "in", STRING_SIZE);
    497 
    498    expected_solution[index].count = 1;
    499    strncpy(expected_solution[index++].text, "the", STRING_SIZE);
    500 
    501    expected_solution[index].count = 1;
    502    strncpy(expected_solution[index++].text, "past", STRING_SIZE);
    503 
    504    expected_solution[index].count = 1;
    505    strncpy(expected_solution[index++].text, "there", STRING_SIZE);
    506 
    507    expected_solution[index].count = 1;
    508    strncpy(expected_solution[index++].text, "lived", STRING_SIZE);
    509 
    510    expected_solution[index].count = 1;
    511    strncpy(expected_solution[index++].text, "strange", STRING_SIZE);
    512 
    513    expected_solution[index].count = 1;
    514    strncpy(expected_solution[index++].text, "little", STRING_SIZE);
    515 
    516    expected_solution[index].count = 1;
    517    strncpy(expected_solution[index++].text, "man", STRING_SIZE);
    518 
    519    expected_solution[index].count = 1;
    520    strncpy(expected_solution[index++].text, "who", STRING_SIZE);
    521 
    522    expected_solution[index].count = 1;
    523    strncpy(expected_solution[index++].text, "could", STRING_SIZE);
    524 
    525    expected_solution[index].count = 1;
    526    strncpy(expected_solution[index++].text, "spin", STRING_SIZE);
    527 
    528    expected_solution[index].count = 1;
    529    strncpy(expected_solution[index++].text, "straw", STRING_SIZE);
    530 
    531    expected_solution[index].count = 1;
    532    strncpy(expected_solution[index++].text, "into", STRING_SIZE);
    533 
    534    expected_solution[index].count = 1;
    535    strncpy(expected_solution[index++].text, "gold", STRING_SIZE);
    536 
    537    actual_word_count = word_count(input_text, actual_solution);
    538 
    539    check_solution(expected_solution,
    540                   expected_word_count, actual_solution, actual_word_count);
    541 }
    542 
    543 int main(void)
    544 {
    545    UnityBegin("test/test_word_count.c");
    546 
    547    RUN_TEST(test_word_count_one_word);
    548    RUN_TEST(test_word_count_one_of_each_word);
    549    RUN_TEST(test_word_count_multiple_occurrences_of_a_word);
    550    RUN_TEST(test_word_count_handles_cramped_lists);
    551    RUN_TEST(test_word_count_handles_expanded_lists);
    552    RUN_TEST(test_word_count_ignore_punctuation);
    553    RUN_TEST(test_word_count_include_numbers);
    554    RUN_TEST(test_word_count_normalize_case);
    555    RUN_TEST(test_word_count_with_apostrophes);
    556    RUN_TEST(test_word_count_with_quotation);
    557    RUN_TEST(test_word_count_from_example);
    558    RUN_TEST(test_max_length_word);
    559    RUN_TEST(test_excessive_length_word);
    560    RUN_TEST(test_max_number_words);
    561    RUN_TEST(test_excessive_number_words);
    562 
    563    UnityEnd();
    564 
    565    return 0;
    566 }