LCOV - code coverage report
Current view: top level - lib/gltests - test-getline.c (source / functions) Hit Total Coverage
Test: GNU SASL Lines: 35 41 85.4 %
Date: 2012-05-28 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 17 34 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* Test of getline() function.
       2                 :            :    Copyright (C) 2007-2012 Free Software Foundation, Inc.
       3                 :            : 
       4                 :            :    This program is free software; you can redistribute it and/or modify
       5                 :            :    it under the terms of the GNU General Public License as published by
       6                 :            :    the Free Software Foundation; either version 3, or (at your option)
       7                 :            :    any later version.
       8                 :            : 
       9                 :            :    This program is distributed in the hope that it will be useful,
      10                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12                 :            :    GNU General Public License for more details.
      13                 :            : 
      14                 :            :    You should have received a copy of the GNU General Public License
      15                 :            :    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
      16                 :            : 
      17                 :            : /* Written by Eric Blake <ebb9@byu.net>, 2007.  */
      18                 :            : 
      19                 :            : #include <config.h>
      20                 :            : 
      21                 :            : #include <stdio.h>
      22                 :            : 
      23                 :            : #include "signature.h"
      24                 :            : SIGNATURE_CHECK (getline, ssize_t, (char **, size_t *, FILE *));
      25                 :            : 
      26                 :            : #include <stdlib.h>
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include "macros.h"
      30                 :            : 
      31                 :            : int
      32                 :          1 : main (void)
      33                 :            : {
      34                 :            :   FILE *f;
      35                 :            :   char *line;
      36                 :            :   size_t len;
      37                 :            :   ssize_t result;
      38                 :            : 
      39                 :            :   /* Create test file.  */
      40                 :          1 :   f = fopen ("test-getline.txt", "wb");
      41 [ +  - ][ +  - ]:          1 :   if (!f || fwrite ("a\nA\nbc\nd\0f", 1, 10, f) != 10 || fclose (f) != 0)
                 [ -  + ]
      42                 :            :     {
      43                 :          0 :       fputs ("Failed to create sample file.\n", stderr);
      44                 :          0 :       remove ("test-getline.txt");
      45                 :          0 :       return 1;
      46                 :            :     }
      47                 :          1 :   f = fopen ("test-getline.txt", "rb");
      48         [ -  + ]:          1 :   if (!f)
      49                 :            :     {
      50                 :          0 :       fputs ("Failed to reopen sample file.\n", stderr);
      51                 :          0 :       remove ("test-getline.txt");
      52                 :          0 :       return 1;
      53                 :            :     }
      54                 :            : 
      55                 :            :   /* Test initial allocation, which must include trailing NUL.  */
      56                 :          1 :   line = NULL;
      57                 :          1 :   len = 0;
      58                 :          1 :   result = getline (&line, &len, f);
      59         [ -  + ]:          1 :   ASSERT (result == 2);
      60         [ -  + ]:          1 :   ASSERT (strcmp (line, "a\n") == 0);
      61         [ -  + ]:          1 :   ASSERT (2 < len);
      62                 :          1 :   free (line);
      63                 :            : 
      64                 :            :   /* Test initial allocation again, with line = NULL and len != 0.  */
      65                 :          1 :   line = NULL;
      66                 :          1 :   len = (size_t)(~0) / 4;
      67                 :          1 :   result = getline (&line, &len, f);
      68         [ -  + ]:          1 :   ASSERT (result == 2);
      69         [ -  + ]:          1 :   ASSERT (strcmp (line, "A\n") == 0);
      70         [ -  + ]:          1 :   ASSERT (2 < len);
      71                 :          1 :   free (line);
      72                 :            : 
      73                 :            :   /* Test growth of buffer, must not leak.  */
      74                 :          1 :   line = malloc (1);
      75                 :          1 :   len = 0;
      76                 :          1 :   result = getline (&line, &len, f);
      77         [ -  + ]:          1 :   ASSERT (result == 3);
      78         [ -  + ]:          1 :   ASSERT (strcmp (line, "bc\n") == 0);
      79         [ -  + ]:          1 :   ASSERT (3 < len);
      80                 :            : 
      81                 :            :   /* Test embedded NULs and EOF behavior.  */
      82                 :          1 :   result = getline (&line, &len, f);
      83         [ -  + ]:          1 :   ASSERT (result == 3);
      84         [ -  + ]:          1 :   ASSERT (memcmp (line, "d\0f", 4) == 0);
      85         [ -  + ]:          1 :   ASSERT (3 < len);
      86                 :            : 
      87                 :          1 :   result = getline (&line, &len, f);
      88         [ -  + ]:          1 :   ASSERT (result == -1);
      89                 :            : 
      90                 :          1 :   free (line);
      91                 :          1 :   fclose (f);
      92                 :          1 :   remove ("test-getline.txt");
      93                 :          1 :   return 0;
      94                 :            : }

Generated by: LCOV version 1.9