exercism

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

test_binary_search.c (2732B)


      1 #include "vendor/unity.h"
      2 #include "../src/binary_search.h"
      3 
      4 void setUp(void)
      5 {
      6 }
      7 
      8 void tearDown(void)
      9 {
     10 }
     11 
     12 static void test_single_element(void)
     13 {
     14    int arr[] = { 6 };
     15    size_t length = sizeof(arr) / sizeof(arr[0]);
     16    TEST_ASSERT(&arr[0] == binary_search(6, arr, length));
     17 }
     18 
     19 static void test_value_in_middle(void)
     20 {
     21    int arr[] = { 1, 3, 4, 6, 8, 9, 11 };
     22    size_t length = sizeof(arr) / sizeof(arr[0]);
     23    TEST_ASSERT(&arr[3] == binary_search(6, arr, length));
     24 }
     25 
     26 static void test_value_at_beginning(void)
     27 {
     28    int arr[] = { 1, 3, 4, 6, 8, 9, 11 };
     29    size_t length = sizeof(arr) / sizeof(arr[0]);
     30    TEST_ASSERT(&arr[0] == binary_search(1, arr, length));
     31 }
     32 
     33 static void test_value_at_end(void)
     34 {
     35    int arr[] = { 1, 3, 4, 6, 8, 9, 11 };
     36    size_t length = sizeof(arr) / sizeof(arr[0]);
     37    TEST_ASSERT(&arr[6] == binary_search(11, arr, length));
     38 }
     39 
     40 static void test_find_value_with_odd_length(void)
     41 {
     42    int arr[] = { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 };
     43    size_t length = sizeof(arr) / sizeof(arr[0]);
     44    TEST_ASSERT(&arr[9] == binary_search(144, arr, length));
     45 }
     46 
     47 static void test_find_value_with_even_length(void)
     48 {
     49    int arr[] = { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
     50    size_t length = sizeof(arr) / sizeof(arr[0]);
     51    TEST_ASSERT(&arr[5] == binary_search(21, arr, length));
     52 }
     53 
     54 static void test_identify_missing_value(void)
     55 {
     56    int arr[] = { 1, 3, 4, 6, 8, 9, 11 };
     57    size_t length = sizeof(arr) / sizeof(arr[0]);
     58    TEST_ASSERT(NULL == binary_search(7, arr, length));
     59 }
     60 
     61 static void test_value_smaller_than_everything(void)
     62 {
     63    int arr[] = { 1, 3, 4, 6, 8, 9, 11 };
     64    size_t length = sizeof(arr) / sizeof(arr[0]);
     65    TEST_ASSERT(NULL == binary_search(0, arr, length));
     66 }
     67 
     68 static void test_value_larger_than_everything(void)
     69 {
     70    int arr[] = { 1, 3, 4, 6, 8, 9, 11 };
     71    size_t length = sizeof(arr) / sizeof(arr[0]);
     72    TEST_ASSERT(NULL == binary_search(13, arr, length));
     73 }
     74 
     75 static void test_empty_array(void)
     76 {
     77    int *arr = NULL;
     78    size_t length = 0;
     79    TEST_ASSERT(NULL == binary_search(1, arr, length));
     80 }
     81 
     82 static void test_zero_length_array(void)
     83 {
     84    int arr[] = { 1 };
     85    size_t length = 0;
     86    TEST_ASSERT(NULL == binary_search(1, arr, length));
     87 }
     88 
     89 int main(void)
     90 {
     91    UnityBegin("test/test_binary_search.c");
     92 
     93    RUN_TEST(test_single_element);
     94    RUN_TEST(test_value_in_middle);
     95    RUN_TEST(test_value_at_beginning);
     96    RUN_TEST(test_value_at_end);
     97    RUN_TEST(test_find_value_with_odd_length);
     98    RUN_TEST(test_find_value_with_even_length);
     99    RUN_TEST(test_identify_missing_value);
    100    RUN_TEST(test_value_smaller_than_everything);
    101    RUN_TEST(test_value_larger_than_everything);
    102    RUN_TEST(test_empty_array);
    103    RUN_TEST(test_zero_length_array);
    104 
    105    return UnityEnd();
    106 }