]> Savannah Git Hosting - gnulib.git/commitdiff
fseeko: Add tests for large files.
authorBruno Haible <bruno@clisp.org>
Thu, 14 Nov 2024 04:20:33 +0000 (05:20 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 14 Nov 2024 04:20:33 +0000 (05:20 +0100)
* tests/test-fseeko-largefile.c: New file.
* modules/fseeko-extra-tests: New file.
* modules/fseeko-tests (Depends-on): Add fseeko-extra-tests.

ChangeLog
modules/fseeko-extra-tests [new file with mode: 0644]
modules/fseeko-tests
tests/test-fseeko-largefile.c [new file with mode: 0644]

index 671d025e30228079a2d5d57eba4cef67de0926f1..421c2b277f4300577bc8ed46f89b457e51579d58 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-11-14  Bruno Haible  <bruno@clisp.org>
+
+       fseeko: Add tests for large files.
+       * tests/test-fseeko-largefile.c: New file.
+       * modules/fseeko-extra-tests: New file.
+       * modules/fseeko-tests (Depends-on): Add fseeko-extra-tests.
+
 2024-11-14  Bruno Haible  <bruno@clisp.org>
 
        ftello: Fix override on mingw.
diff --git a/modules/fseeko-extra-tests b/modules/fseeko-extra-tests
new file mode 100644 (file)
index 0000000..e59b800
--- /dev/null
@@ -0,0 +1,17 @@
+Status:
+longrunning-test
+
+Files:
+tests/test-fseeko-largefile.c
+tests/macros.h
+
+Depends-on:
+fsync
+ftruncate
+write
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-fseeko-largefile
+check_PROGRAMS += test-fseeko-largefile
index 22e1e2b4724e487c111b6362d1d80f860c14df4f..4fc539364a72f5cf2107bacde135ed0cb9ecab47 100644 (file)
@@ -12,6 +12,7 @@ m4/ungetc.m4
 
 Depends-on:
 fdopen
+fseeko-extra-tests
 
 configure.ac:
 gl_FUNC_UNGETC_WORKS
diff --git a/tests/test-fseeko-largefile.c b/tests/test-fseeko-largefile.c
new file mode 100644 (file)
index 0000000..7cb7edc
--- /dev/null
@@ -0,0 +1,86 @@
+/* Test of fseeko() function on large files.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2024.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdio.h>
+
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+#define TESTFILE "test-fseeko.data"
+#define TESTFILE_ZEROES 3000000000LL
+#define TESTFILE_DATA "GNU"
+#define TESTFILE_DATA_LEN 3
+
+int
+main (void)
+{
+  remove (TESTFILE);
+
+  if (sizeof (off_t) <= 4)
+    {
+      fprintf (stderr, "off_t is only 32 bits.\n");
+      return 77;
+    }
+  /* off_t is larger than 32-bit.  */
+
+  /* Create a file that is larger than 2 GiB.
+     On file systems such as ext2, the file will have "holes" and thus
+     not consume much disk space.  */
+  {
+    int fd = open (TESTFILE, O_CREAT | O_TRUNC | O_RDWR, 0600);
+    ASSERT (fd >= 0);
+    if (ftruncate (fd, TESTFILE_ZEROES) < 0
+        || lseek (fd, TESTFILE_ZEROES, SEEK_SET) < 0
+        || write (fd, TESTFILE_DATA, TESTFILE_DATA_LEN) < 0
+        || fsync (fd) < 0)
+      {
+        close (fd);
+        remove (TESTFILE);
+        fprintf (stderr, "Could not create 3 GB large file.\n");
+        return 77;
+      }
+    close (fd);
+  }
+
+  /* Check that fseeko() works.  */
+  {
+    FILE *fp = fopen (TESTFILE, "r");
+    ASSERT (fp != NULL);
+
+    int ret;
+    ret = fseeko (fp, TESTFILE_ZEROES, SEEK_SET);
+    ASSERT (ret == 0);
+
+    char buf[TESTFILE_DATA_LEN];
+    ASSERT (fread (buf, 1, TESTFILE_DATA_LEN, fp) == TESTFILE_DATA_LEN);
+    ASSERT (memcmp (buf, TESTFILE_DATA, TESTFILE_DATA_LEN) == 0);
+
+    ret = fclose (fp);
+    ASSERT (ret == 0);
+  }
+
+  remove (TESTFILE);
+
+  return test_exit_status;
+}