LCOV - code coverage report
Current view: top level - example/methods - iterate.c (source / functions) Hit Total Coverage
Test: Basic example ( view descriptions ) Lines: 8 8 100.0 %
Date: 2016-12-20 14:12:28 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  methods/iterate.c
       3                 :            :  *  
       4                 :            :  *  Calculate the sum of a given range of integer numbers.
       5                 :            :  *
       6                 :            :  *  This particular method of implementation works by way of brute force,
       7                 :            :  *  i.e. it iterates over the entire range while adding the numbers to finally
       8                 :            :  *  get the total sum. As a positive side effect, we're able to easily detect
       9                 :            :  *  overflows, i.e. situations in which the sum would exceed the capacity
      10                 :            :  *  of an integer variable.
      11                 :            :  *
      12                 :            :  */
      13                 :            : 
      14                 :            : #include <stdio.h>
      15                 :            : #include <stdlib.h>
      16                 :            : #include "iterate.h"
      17                 :            : 
      18                 :            : 
      19                 :          3 : int iterate_get_sum (int min, int max)
      20                 :            : {
      21                 :            :         int i, total;
      22                 :            : 
      23                 :          3 :         total = 0;
      24                 :            : 
      25                 :            :         /* This is where we loop over each number in the range, including
      26                 :            :            both the minimum and the maximum number. */
      27                 :            : 
      28         [ +  + ]:      67548 :         for (i = min; i <= max; i++)
      29                 :            :         {
      30                 :            :                 /* We can detect an overflow by checking whether the new
      31                 :            :                    sum would become negative. */
      32                 :            : 
      33         [ +  + ]:      67546 :                 if (total + i < total)
      34                 :            :                 {
      35                 :          1 :                         printf ("Error: sum too large!\n");
      36                 :          1 :                         exit (1);
      37                 :            :                 }
      38                 :            : 
      39                 :            :                 /* Everything seems to fit into an int, so continue adding. */
      40                 :            : 
      41                 :      67545 :                 total += i;
      42                 :            :         }
      43                 :            : 
      44                 :          2 :         return total;
      45                 :            : }

Generated by: LCOV version 1.13