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

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

index 421c2b277f4300577bc8ed46f89b457e51579d58..6e44c146481fbbae06ea16eb507a898da59dc104 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-11-14  Bruno Haible  <bruno@clisp.org>
+
+       ftello: Add tests for large files.
+       * tests/test-ftello-largefile.c: New file.
+       * modules/ftello-extra-tests: New file.
+       * modules/ftello-tests (Depends-on): Add ftello-extra-tests.
+
 2024-11-14  Bruno Haible  <bruno@clisp.org>
 
        fseeko: Add tests for large files.
diff --git a/modules/ftello-extra-tests b/modules/ftello-extra-tests
new file mode 100644 (file)
index 0000000..0f980f2
--- /dev/null
@@ -0,0 +1,18 @@
+Status:
+longrunning-test
+
+Files:
+tests/test-ftello-largefile.c
+tests/macros.h
+
+Depends-on:
+fsync
+fseeko
+ftruncate
+write
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-ftello-largefile
+check_PROGRAMS += test-ftello-largefile
index 90d269eae7f1af699130e4610d81d3d46ed4ee0d..d709a6ebe5db726505d5545f274f726d3bbe7fe3 100644 (file)
@@ -12,6 +12,7 @@ m4/ungetc.m4
 Depends-on:
 binary-io
 fdopen
+ftello-extra-tests
 
 configure.ac:
 gl_FUNC_UNGETC_WORKS
diff --git a/tests/test-ftello-largefile.c b/tests/test-ftello-largefile.c
new file mode 100644 (file)
index 0000000..4dd778b
--- /dev/null
@@ -0,0 +1,83 @@
+/* Test of ftello() 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 <unistd.h>
+
+#include "macros.h"
+
+#define TESTFILE "test-ftello.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 the value of ftello().  */
+  {
+    FILE *fp = fopen (TESTFILE, "r");
+    ASSERT (fp != NULL);
+
+    ASSERT (fseeko (fp, 0, SEEK_END) == 0);
+
+    off_t length = ftello (fp);
+    ASSERT (length > 0);
+    ASSERT (length == TESTFILE_ZEROES + TESTFILE_DATA_LEN);
+
+    int ret = fclose (fp);
+    ASSERT (ret == 0);
+  }
+
+  remove (TESTFILE);
+
+  return test_exit_status;
+}