exercism

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

test_beer_song.c (3165B)


      1 #include "vendor/unity.h"
      2 #include "../src/beer_song.h"
      3 
      4 #define BUFFER_SIZE   (1024)
      5 
      6 void setUp(void)
      7 {
      8 }
      9 
     10 void tearDown(void)
     11 {
     12 }
     13 
     14 void test_handles_arbitrary_verse(void)
     15 {
     16    char response[BUFFER_SIZE];
     17    const char expected[BUFFER_SIZE] =
     18        "84 bottles of beer on the wall, 84 bottles of beer.\n"
     19        "Take one down and pass it around, 83 bottles of beer on the wall.\n";
     20 
     21    verse(response, 84);
     22    TEST_ASSERT_EQUAL_STRING(expected, response);
     23 }
     24 
     25 void test_handles_2_bottles(void)
     26 {
     27   char response[BUFFER_SIZE];
     28    const char expected[BUFFER_SIZE] =
     29        "2 bottles of beer on the wall, 2 bottles of beer.\n"
     30        "Take one down and pass it around, 1 bottle of beer on the wall.\n";
     31 
     32    verse(response, 2);
     33    TEST_ASSERT_EQUAL_STRING(expected, response);
     34 }
     35 
     36 void test_handles_1_bottle(void)
     37 {
     38    char response[BUFFER_SIZE];
     39    const char expected[BUFFER_SIZE] =
     40        "1 bottle of beer on the wall, 1 bottle of beer.\n"
     41        "Take it down and pass it around, no more bottles of beer on the wall.\n";
     42 
     43    verse(response, 1);
     44    TEST_ASSERT_EQUAL_STRING(expected, response);
     45 }
     46 
     47 void test_handles_0_bottles(void)
     48 {
     49    char response[BUFFER_SIZE];
     50    const char expected[BUFFER_SIZE] =
     51        "No more bottles of beer on the wall, no more bottles of beer.\n"
     52        "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
     53 
     54    verse(response, 0);
     55    TEST_ASSERT_EQUAL_STRING(expected, response);
     56 }
     57 
     58 void test_sings_several_verses(void)
     59 {
     60    char response[BUFFER_SIZE];
     61    const char expected[BUFFER_SIZE] =
     62        "8 bottles of beer on the wall, 8 bottles of beer.\n"
     63        "Take one down and pass it around, 7 bottles of beer on the wall.\n"
     64        "\n"
     65        "7 bottles of beer on the wall, 7 bottles of beer.\n"
     66        "Take one down and pass it around, 6 bottles of beer on the wall.\n"
     67        "\n"
     68        "6 bottles of beer on the wall, 6 bottles of beer.\n"
     69        "Take one down and pass it around, 5 bottles of beer on the wall.\n";
     70 
     71    sing(response, 8, 6);
     72    TEST_ASSERT_EQUAL_STRING(expected, response);
     73 }
     74 
     75 void test_sings_the_last_4_verses(void)
     76 {
     77    char response[BUFFER_SIZE];
     78    const char expected[BUFFER_SIZE] =
     79        "3 bottles of beer on the wall, 3 bottles of beer.\n"
     80        "Take one down and pass it around, 2 bottles of beer on the wall.\n"
     81        "\n"
     82        "2 bottles of beer on the wall, 2 bottles of beer.\n"
     83        "Take one down and pass it around, 1 bottle of beer on the wall.\n"
     84        "\n"
     85        "1 bottle of beer on the wall, 1 bottle of beer.\n"
     86        "Take it down and pass it around, no more bottles of beer on the wall.\n"
     87        "\n"
     88        "No more bottles of beer on the wall, no more bottles of beer.\n"
     89        "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
     90 
     91    sing(response, 3, 0);
     92    TEST_ASSERT_EQUAL_STRING(expected, response);
     93 }
     94 
     95 int main(void)
     96 {
     97    UnityBegin("test/test_beer_song.c");
     98 
     99    RUN_TEST(test_handles_arbitrary_verse);
    100    RUN_TEST(test_handles_2_bottles);
    101    RUN_TEST(test_handles_1_bottle);
    102    RUN_TEST(test_handles_0_bottles);
    103    RUN_TEST(test_sings_several_verses);
    104    RUN_TEST(test_sings_the_last_4_verses);
    105 
    106    return UnityEnd();
    107 }