LCOV - code coverage report
Current view: top level - test - itertest.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 84 84 100.0 %
Date: 2015-05-24 18:28:16 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /***************************************************************************//**
       2             : 
       3             :   @file         itertest.c
       4             : 
       5             :   @author       Stephen Brennan
       6             : 
       7             :   @date         Created Monday,  4 August 2014
       8             : 
       9             :   @brief        Libstephen: Iterator Tests
      10             : 
      11             :   @copyright    Copyright (c) 2013-2015, Stephen Brennan.  Released under the
      12             :                 Revised BSD License.  See the LICENSE.txt file for details.
      13             : 
      14             : *******************************************************************************/
      15             : 
      16             : #include "libstephen/ut.h"
      17             : 
      18             : #include "libstephen/list.h"
      19             : #include "libstephen/ll.h"
      20             : #include "libstephen/al.h"
      21             : 
      22             : // Setup and tear down declarations.
      23             : 
      24             : /**
      25             :    @brief Return and iterator to test.
      26             :    @param n Number of elements to return.
      27             : 
      28             :    The elements should be integers of 100 * index.
      29             :  */
      30             : smb_iter (*get_iter)(int n);
      31             : /**
      32             :    @brief Clean up the iterator's underlying list.
      33             :  */
      34             : void (*cleanup)(void);
      35             : void *test_data;
      36             : 
      37             : 
      38             : /*******************************************************************************
      39             : 
      40             :                                      Tests
      41             : 
      42             : *******************************************************************************/
      43             : 
      44             : /**
      45             :    @brief Tests that empty iterators have no values.
      46             :  */
      47           2 : int iter_test_empty()
      48             : {
      49           2 :   smb_iter it = get_iter(0);
      50           2 :   TEST_ASSERT(! it.has_next(&it));
      51           2 :   it.destroy(&it);
      52           2 :   cleanup();
      53           2 :   return 0;
      54             : }
      55             : 
      56             : /**
      57             :    @brief Tests that iterators can destroy.
      58             :  */
      59           2 : int iter_test_destroy()
      60             : {
      61           2 :   smb_iter it = get_iter(10);
      62           2 :   it.destroy(&it);
      63           2 :   cleanup();
      64           2 :   return 0;
      65             : }
      66             : 
      67             : /**
      68             :    @brief Tests that iterators can free.
      69             :  */
      70           2 : int iter_test_delete()
      71             : {
      72           2 :   smb_iter *it = smb_new(smb_iter, 1);
      73           2 :   *it = get_iter(10);
      74           2 :   it->delete(it);
      75           2 :   cleanup();
      76           2 :   return 0;
      77             : }
      78             : 
      79             : /**
      80             :    @brief Tests that iterators have the correct amounts of values.
      81             :  */
      82           2 : int iter_test_count()
      83             : {
      84           2 :   smb_status status = SMB_SUCCESS;
      85             :   #define MAX_TEST_COUNT 1000
      86        2002 :   for (int i = 0; i < MAX_TEST_COUNT; i++) {
      87        2000 :     smb_iter it = get_iter(i);
      88        2000 :     int n = 0;
      89     1003000 :     while (it.has_next(&it)) {
      90      999000 :       it.next(&it, &status);
      91      999000 :       TEST_ASSERT(status == SMB_SUCCESS);
      92      999000 :       n++;
      93             :     }
      94        2000 :     it.next(&it, &status);
      95        2000 :     TEST_ASSERT(status == SMB_STOP_ITERATION);
      96        2000 :     it.destroy(&it);
      97        2000 :     cleanup();
      98        2000 :     TEST_ASSERT(i == n);
      99             :   }
     100           2 :   return 0;
     101             : }
     102             : 
     103             : /**
     104             :    @brief Tests that iterators return correct values in the correct order.
     105             :  */
     106           2 : int iter_test_values()
     107             : {
     108             :   #define TEST_COUNT 1000
     109             :   smb_status status;
     110           2 :   smb_iter it = get_iter(TEST_COUNT);
     111             :   DATA d;
     112           2 :   int i = 0;
     113        2004 :   while (it.has_next(&it)) {
     114        2000 :     d = it.next(&it, &status);
     115        2000 :     TEST_ASSERT(status == SMB_SUCCESS);
     116        2000 :     TEST_ASSERT(d.data_llint == 100 * i);
     117        2000 :     i++;
     118             :   }
     119           2 :   it.destroy(&it);
     120           2 :   cleanup();
     121           2 :   return 0;
     122             : }
     123             : 
     124             : 
     125             : /*******************************************************************************
     126             : 
     127             :                                 Setup/Tear Down
     128             : 
     129             : *******************************************************************************/
     130             : 
     131             : /**
     132             :    @brief Return an iterator for an array list.
     133             :    @param size Number of elements to add.
     134             :    @return An iterator.
     135             :  */
     136        1004 : smb_iter get_al_iter(int size)
     137             : {
     138        1004 :   test_data = al_create();
     139             : 
     140      501524 :   for (int i = 0; i < size; i++) {
     141      500520 :     DATA d = { .data_llint = 100 * i };
     142      500520 :     al_append(test_data, d);
     143             :   }
     144             : 
     145        1004 :   return al_get_iter(test_data);
     146             : }
     147             : 
     148             : /**
     149             :    @brief Clean up array list.
     150             :  */
     151        1004 : void al_cleanup(void)
     152             : {
     153        1004 :   al_delete(test_data);
     154        1004 : }
     155             : 
     156             : /**
     157             :    @brief Return a linked list iterator.
     158             :    @param Number of elements in the list.
     159             :    @return An iterator.
     160             :  */
     161        1004 : smb_iter get_ll_iter(int size)
     162             : {
     163        1004 :   test_data = ll_create();
     164             : 
     165      501524 :   for (int i = 0; i < size; i++) {
     166      500520 :     DATA d = { .data_llint = 100 * i };
     167      500520 :     ll_append(test_data, d);
     168             :   }
     169             : 
     170        1004 :   return ll_get_iter(test_data);
     171             : }
     172             : 
     173             : /**
     174             :    @brief Clean up linked list.
     175             :  */
     176        1004 : void ll_cleanup(void)
     177             : {
     178        1004 :   ll_delete(test_data);
     179        1004 : }
     180             : 
     181             : /*******************************************************************************
     182             : 
     183             :                                   Test Running
     184             : 
     185             : *******************************************************************************/
     186             : 
     187             : /**
     188             :    @brief Run the tests in this file with a specified description.
     189             : 
     190             :    This allows you to set the setup and tear down functions to different values
     191             :    and run the tests again with a different name.
     192             : 
     193             :    @param desc The description for this run through.
     194             :  */
     195           2 : void run_tests(char *desc)
     196             : {
     197           2 :   smb_ut_group *group = su_create_test_group(desc);
     198             : 
     199           2 :   smb_ut_test *empty = su_create_test("empty", iter_test_empty);
     200           2 :   su_add_test(group, empty);
     201             : 
     202           2 :   smb_ut_test *destroy = su_create_test("destroy", iter_test_destroy);
     203           2 :   su_add_test(group, destroy);
     204             : 
     205           2 :   smb_ut_test *delete = su_create_test("delete", iter_test_delete);
     206           2 :   su_add_test(group, delete);
     207             : 
     208           2 :   smb_ut_test *count = su_create_test("count", iter_test_count);
     209           2 :   su_add_test(group, count);
     210             : 
     211           2 :   smb_ut_test *values = su_create_test("values", iter_test_values);
     212           2 :   su_add_test(group, values);
     213             : 
     214           2 :   su_run_group(group);
     215           2 :   su_delete_group(group);
     216           2 : }
     217             : 
     218             : /**
     219             :    @brief Run the tests on iterators.
     220             :  */
     221           1 : void iter_test(void)
     222             : {
     223           1 :   get_iter = &get_al_iter;
     224           1 :   cleanup = &al_cleanup;
     225           1 :   run_tests("al_iter");
     226             : 
     227           1 :   get_iter = &get_ll_iter;
     228           1 :   cleanup = &ll_cleanup;
     229           1 :   run_tests("ll_iter");
     230           1 : }

Generated by: LCOV version 1.11