]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/testfile.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cups / testfile.c
index d8a1b88c9535d7c57879684471921087e6bfa302..8555e079df1a1c42bd463200c50767fd1862cd35 100644 (file)
@@ -1,41 +1,37 @@
 /*
- * "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $"
+ * "$Id$"
  *
- *   File test program for the Common UNIX Printing System (CUPS).
+ * File test program for CUPS.
  *
- *   Copyright 2007-2008 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   main()             - Main entry.
- *   random_tests()     - Do random access tests.
- *   read_write_tests() - Perform read/write tests.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include <stdio.h>
+#include "string-private.h"
+#include "debug-private.h"
+#include "file.h"
 #include <stdlib.h>
-#include <errno.h>
 #include <time.h>
-#include "string.h"
-#include "file.h"
-#include "debug.h"
 #ifdef HAVE_LIBZ
 #  include <zlib.h>
 #endif /* HAVE_LIBZ */
-#include <unistd.h>
+#ifdef WIN32
+#  include <io.h>
+#else
+#  include <unistd.h>
+#endif /* WIN32 */
 #include <fcntl.h>
 
 
@@ -43,6 +39,7 @@
  * Local functions...
  */
 
+static int     count_lines(cups_file_t *fp);
 static int     random_tests(void);
 static int     read_write_tests(int compression);
 
@@ -57,8 +54,12 @@ main(int  argc,                              /* I - Number of command-line arguments */
 {
   int          status;                 /* Exit status */
   char         filename[1024];         /* Filename buffer */
+  cups_file_t  *fp;                    /* File pointer */
+#ifndef WIN32
   int          fds[2];                 /* Open file descriptors */
   cups_file_t  *fdfile;                /* File opened with cupsFileOpenFd() */
+#endif /* !WIN32 */
+  int          count;                  /* Number of lines in file */
 
 
   if (argc == 1)
@@ -83,8 +84,9 @@ main(int  argc,                               /* I - Number of command-line arguments */
     * Do uncompressed random I/O tests...
     */
 
-    status = random_tests();
+    status += random_tests();
 
+#ifndef WIN32
    /*
     * Test fdopen and close without reading...
     */
@@ -118,6 +120,56 @@ main(int  argc,                            /* I - Number of command-line arguments */
 
       puts("PASS");
     }
+#endif /* !WIN32 */
+
+   /*
+    * Count lines in psglyphs, rewind, then count again.
+    */
+
+    fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout);
+
+    if ((fp = cupsFileOpen("../data/media.defs", "r")) == NULL)
+    {
+      puts("FAIL");
+      status ++;
+    }
+    else
+    {
+      puts("PASS");
+      fputs("cupsFileGets: ", stdout);
+
+      if ((count = count_lines(fp)) != 208)
+      {
+        printf("FAIL (got %d lines, expected 208)\n", count);
+       status ++;
+      }
+      else
+      {
+        puts("PASS");
+       fputs("cupsFileRewind: ", stdout);
+
+       if (cupsFileRewind(fp) != 0)
+       {
+         puts("FAIL");
+         status ++;
+       }
+       else
+       {
+         puts("PASS");
+         fputs("cupsFileGets: ", stdout);
+
+         if ((count = count_lines(fp)) != 208)
+         {
+           printf("FAIL (got %d lines, expected 208)\n", count);
+           status ++;
+         }
+         else
+           puts("PASS");
+        }
+      }
+
+      cupsFileClose(fp);
+    }
 
    /*
     * Test path functions...
@@ -153,10 +205,8 @@ main(int  argc,                            /* I - Number of command-line arguments */
     * Cat the filename on the command-line...
     */
 
-    cups_file_t        *fp;                    /* File pointer */
     char       line[1024];             /* Line from file */
 
-
     if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
     {
       perror(argv[1]);
@@ -180,6 +230,23 @@ main(int  argc,                            /* I - Number of command-line arguments */
 }
 
 
+/*
+ * 'count_lines()' - Count the number of lines in a file.
+ */
+
+static int                             /* O - Number of lines */
+count_lines(cups_file_t *fp)           /* I - File to read from */
+{
+  int  count;                          /* Number of lines */
+  char line[1024];                     /* Line buffer */
+
+
+  for (count = 0; cupsFileGets(fp, line, sizeof(line)); count ++);
+
+  return (count);
+}
+
+
 /*
  * 'random_tests()' - Do random access tests.
  */
@@ -192,8 +259,8 @@ random_tests(void)
                count,                  /* Number of records read */
                record,                 /* Current record */
                num_records;            /* Number of records */
-  ssize_t      pos,                    /* Position in file */
-               expected;               /* Expected position in file */
+  off_t                pos;                    /* Position in file */
+  ssize_t      expected;               /* Expected position in file */
   cups_file_t  *fp;                    /* File */
   char         buffer[512];            /* Data buffer */
 
@@ -224,10 +291,10 @@ random_tests(void)
     * cupsFileTell()
     */
 
-    expected = 256 * sizeof(buffer) * pass;
+    expected = 256 * (ssize_t)sizeof(buffer) * pass;
 
     fputs("cupsFileTell(): ", stdout);
-    if ((pos = cupsFileTell(fp)) != expected)
+    if ((pos = cupsFileTell(fp)) != (off_t)expected)
     {
       printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
             CUPS_LLCAST pos, CUPS_LLCAST expected);
@@ -245,7 +312,7 @@ random_tests(void)
     for (record = 0; record < 256; record ++)
     {
       memset(buffer, record, sizeof(buffer));
-      if (cupsFileWrite(fp, buffer, sizeof(buffer)) < sizeof(buffer))
+      if (cupsFileWrite(fp, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer))
         break;
     }
 
@@ -262,10 +329,10 @@ random_tests(void)
     * cupsFileTell()
     */
 
-    expected += 256 * sizeof(buffer);
+    expected += 256 * (ssize_t)sizeof(buffer);
 
     fputs("cupsFileTell(): ", stdout);
-    if ((pos = cupsFileTell(fp)) != expected)
+    if ((pos = cupsFileTell(fp)) != (off_t)expected)
     {
       printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
              CUPS_LLCAST pos, CUPS_LLCAST expected);
@@ -298,11 +365,9 @@ random_tests(void)
 
     fputs("cupsFileSeek(), cupsFileRead(): ", stdout);
 
-    for (num_records = (pass + 1) * 256, count = (pass + 1) * 256,
-             record = rand() % num_records;
+    for (num_records = (pass + 1) * 256, count = (pass + 1) * 256, record = ((int)CUPS_RAND() & 65535) % num_records;
          count > 0;
-        count --, record = (record + (rand() & 31) - 16 + num_records) %
-                           num_records)
+        count --, record = (record + ((int)CUPS_RAND() & 31) - 16 + num_records) % num_records)
     {
      /*
       * The last record is always the first...
@@ -316,7 +381,7 @@ random_tests(void)
       * contents...
       */
 
-      expected = sizeof(buffer) * record;
+      expected = (ssize_t)sizeof(buffer) * record;
 
       if ((pos = cupsFileSeek(fp, expected)) != expected)
       {
@@ -355,7 +420,7 @@ random_tests(void)
   */
 
   unlink("testfile.dat");
-                    
+
  /*
   * Return the test status...
   */
@@ -380,6 +445,7 @@ read_write_tests(int compression)   /* I - Use compression? */
   unsigned char        readbuf[8192],          /* Read buffer */
                writebuf[8192];         /* Write buffer */
   int          byte;                   /* Byte from file */
+  ssize_t      bytes;                  /* Number of bytes read/written */
   off_t                length;                 /* Length of file */
   static const char *partial_line = "partial line";
                                        /* Partial line */
@@ -395,14 +461,10 @@ read_write_tests(int compression) /* I - Use compression? */
   * Initialize the write buffer with random data...
   */
 
-#ifdef WIN32
-  srand((unsigned)time(NULL));
-#else
-  srand(time(NULL));
-#endif /* WIN32 */
+  CUPS_SRAND((unsigned)time(NULL));
 
   for (i = 0; i < (int)sizeof(writebuf); i ++)
-    writebuf[i] = rand();
+    writebuf[i] = (unsigned char)CUPS_RAND();
 
  /*
   * cupsFileOpen(write)
@@ -603,10 +665,10 @@ read_write_tests(int compression) /* I - Use compression? */
 
     fputs("cupsFileGetConf(): ", stdout);
 
-    for (i = 0; i < 1000; i ++)
+    for (i = 0, value = NULL; i < 1000; i ++)
       if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
         break;
-      else if (strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
+      else if (_cups_strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
                linenum != (i + 2))
         break;
 
@@ -630,7 +692,7 @@ read_write_tests(int compression)   /* I - Use compression? */
 
     fputs("cupsFileGetChar(): ", stdout);
 
-    for (i = 0; i < 256; i ++)
+    for (i = 0, byte = 0; i < 256; i ++)
       if ((byte = cupsFileGetChar(fp)) != i)
         break;
 
@@ -653,15 +715,15 @@ read_write_tests(int compression) /* I - Use compression? */
 
     fputs("cupsFileRead(): ", stdout);
 
-    for (i = 0; i < 10000; i ++)
-      if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
+    for (i = 0, bytes = 0; i < 10000; i ++)
+      if ((bytes = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
         break;
       else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
         break;
 
     if (i >= 10000)
       puts("PASS");
-    else if (byte > 0)
+    else if (bytes > 0)
     {
       printf("FAIL (Pass %d, ", i);
 
@@ -684,7 +746,7 @@ read_write_tests(int compression)   /* I - Use compression? */
 
     fputs("cupsFileGetChar(partial line): ", stdout);
 
-    for (i = 0; i < strlen(partial_line); i ++)
+    for (i = 0; i < (int)strlen(partial_line); i ++)
       if ((byte = cupsFileGetChar(fp)) < 0)
         break;
       else if (byte != partial_line[i])
@@ -737,7 +799,7 @@ read_write_tests(int compression)   /* I - Use compression? */
   */
 
   unlink(compression ? "testfile.dat.gz" : "testfile.dat");
-                    
+
  /*
   * Return the test status...
   */
@@ -747,5 +809,5 @@ read_write_tests(int compression)   /* I - Use compression? */
 
 
 /*
- * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $".
+ * End of "$Id$".
  */