LCOV - code coverage report
Current view: top level - gltests - test-getline.c (source / functions) Hit Total Coverage
Test: GNU SASL Lines: 35 41 85.4 %
Date: 2010-09-27 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-2010 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, write to the Free Software Foundation,
      16                 :            :    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
      17                 :            : 
      18                 :            : /* Written by Eric Blake <ebb9@byu.net>, 2007.  */
      19                 :            : 
      20                 :            : #include <config.h>
      21                 :            : 
      22                 :            : #include <stdio.h>
      23                 :            : 
      24                 :            : #include "signature.h"
      25                 :            : SIGNATURE_CHECK (getline, ssize_t, (char **, size_t *, FILE *));
      26                 :            : 
      27                 :            : #include <stdlib.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : #include "macros.h"
      31                 :            : 
      32                 :            : int
      33                 :          1 : main (void)
      34                 :            : {
      35                 :            :   FILE *f;
      36                 :            :   char *line;
      37                 :            :   size_t len;
      38                 :            :   ssize_t result;
      39                 :            : 
      40                 :            :   /* Create test file.  */
      41                 :          1 :   f = fopen ("test-getline.txt", "wb");
      42   [ +  -  +  - ]:          1 :   if (!f || fwrite ("a\nA\nbc\nd\0f", 1, 10, f) != 10 || fclose (f) != 0)
                 [ -  + ]
      43                 :            :     {
      44                 :          0 :       fputs ("Failed to create sample file.\n", stderr);
      45                 :          0 :       remove ("test-getline.txt");
      46                 :          0 :       return 1;
      47                 :            :     }
      48                 :          1 :   f = fopen ("test-getline.txt", "rb");
      49         [ -  + ]:          1 :   if (!f)
      50                 :            :     {
      51                 :          0 :       fputs ("Failed to reopen sample file.\n", stderr);
      52                 :          0 :       remove ("test-getline.txt");
      53                 :          0 :       return 1;
      54                 :            :     }
      55                 :            : 
      56                 :            :   /* Test initial allocation, which must include trailing NUL.  */
      57                 :          1 :   line = NULL;
      58                 :          1 :   len = 0;
      59                 :          1 :   result = getline (&line, &len, f);
      60         [ -  + ]:          1 :   ASSERT (result == 2);
      61         [ -  + ]:          1 :   ASSERT (strcmp (line, "a\n") == 0);
      62         [ -  + ]:          1 :   ASSERT (2 < len);
      63                 :          1 :   free (line);
      64                 :            : 
      65                 :            :   /* Test initial allocation again, with line = NULL and len != 0.  */
      66                 :          1 :   line = NULL;
      67                 :          1 :   len = (size_t)(~0) / 4;
      68                 :          1 :   result = getline (&line, &len, f);
      69         [ -  + ]:          1 :   ASSERT (result == 2);
      70         [ -  + ]:          1 :   ASSERT (strcmp (line, "A\n") == 0);
      71         [ -  + ]:          1 :   ASSERT (2 < len);
      72                 :          1 :   free (line);
      73                 :            : 
      74                 :            :   /* Test growth of buffer, must not leak.  */
      75                 :          1 :   line = malloc (1);
      76                 :          1 :   len = 0;
      77                 :          1 :   result = getline (&line, &len, f);
      78         [ -  + ]:          1 :   ASSERT (result == 3);
      79         [ -  + ]:          1 :   ASSERT (strcmp (line, "bc\n") == 0);
      80         [ -  + ]:          1 :   ASSERT (3 < len);
      81                 :            : 
      82                 :            :   /* Test embedded NULs and EOF behavior.  */
      83                 :          1 :   result = getline (&line, &len, f);
      84         [ -  + ]:          1 :   ASSERT (result == 3);
      85         [ -  + ]:          1 :   ASSERT (memcmp (line, "d\0f", 4) == 0);
      86         [ -  + ]:          1 :   ASSERT (3 < len);
      87                 :            : 
      88                 :          1 :   result = getline (&line, &len, f);
      89         [ -  + ]:          1 :   ASSERT (result == -1);
      90                 :            : 
      91                 :          1 :   free (line);
      92                 :          1 :   fclose (f);
      93                 :          1 :   remove ("test-getline.txt");
      94                 :          1 :   return 0;
      95                 :            : }

Generated by: LCOV version 1.8