]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
savedir: Add tests.
authorCollin Funk <collin.funk1@gmail.com>
Fri, 17 Oct 2025 23:45:30 +0000 (16:45 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Fri, 17 Oct 2025 23:47:55 +0000 (16:47 -0700)
* tests/test-savedir.c: New file.
* modules/savedir-tests: Likewise.

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

index c1e2c0f7e4c6e56b88ec205c665fc9d4875ba765..c111b6214a71fb69dc294404aac832866873ef99 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-10-17  Collin Funk  <collin.funk1@gmail.com>
+
+       savedir: Add tests.
+       * tests/test-savedir.c: New file.
+       * modules/savedir-tests: Likewise.
+
 2025-10-15  Bruno Haible  <bruno@clisp.org>
 
        glob tests: Avoid a test failure on native Windows.
diff --git a/modules/savedir-tests b/modules/savedir-tests
new file mode 100644 (file)
index 0000000..964d5be
--- /dev/null
@@ -0,0 +1,16 @@
+Files:
+tests/test-savedir.c
+tests/macros.h
+
+Depends-on:
+bool
+creat
+close
+mkdir
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-savedir
+check_PROGRAMS += test-savedir
+test_savedir_LDADD = $(LDADD) @LIBINTL@
diff --git a/tests/test-savedir.c b/tests/test-savedir.c
new file mode 100644 (file)
index 0000000..5b7e840
--- /dev/null
@@ -0,0 +1,107 @@
+/* Test the savedir module.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+
+   This file 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 file 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 Collin Funk <collin.funk1@gmail.com>, 2025.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "savedir.h"
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "ignore-value.h"
+#include "macros.h"
+
+/* Base directory.  */
+#define BASE "test-savedir.t"
+
+static void
+test_savedir_sort_none (void)
+{
+  bool seen[('z' - 'a') + 1];
+  char *name_space = savedir (BASE, SAVEDIR_SORT_NONE);
+  ASSERT (name_space != NULL);
+
+  /* Set the table of file names we have seen to false.  */
+  memset (seen, 0, sizeof seen);
+
+  /* Scan through the file names.  */
+  for (char *namep = name_space; *namep != '\0'; namep += strlen (namep) + 1)
+    {
+      int index = *namep - 'a';
+      ASSERT (index < sizeof seen);
+      seen[index] = true;
+    }
+
+  /* Make sure all of them have been seen.  */
+  for (int i = 0; i < sizeof seen; ++i)
+    ASSERT (seen[i]);
+
+  free (name_space);
+}
+
+static void
+test_savedir_sort_name (void)
+{
+  int i = 0;
+  char *name_space = savedir (BASE, SAVEDIR_SORT_NAME);
+  ASSERT (name_space != NULL);
+
+  /* Check that files "a" to "z" appear in order.  */
+  for (char *namep = name_space; *namep != '\0';
+       namep += strlen (namep) + 1, i += 1)
+    ASSERT (*namep - 'a' == i);
+
+  /* Make sure no extra files were seen.  */
+  ASSERT (i == ('z' - 'a') + 1);
+
+  free (name_space);
+}
+
+int
+main (void)
+{
+  char buffer[sizeof BASE + 2];
+
+  /* Clean up any trash from prior testsuite runs.  */
+  ignore_value (system ("rm -rf " BASE "*"));
+
+  /* Setup a directory with files "a" to "z".  */
+  strcpy (buffer, BASE);
+  ASSERT (mkdir (buffer, 0700) == 0);
+  buffer[sizeof buffer - 1] = '\0';
+  for (int i = 'a'; i <= 'z'; ++i)
+    {
+      buffer[sizeof buffer - 3] = '/';
+      buffer[sizeof buffer - 2] = i;
+      int fd = creat (buffer, 0600);
+      ASSERT (0 <= fd);
+      ASSERT (close (fd) == 0);
+    }
+
+  test_savedir_sort_none ();
+  test_savedir_sort_name ();
+
+  /* Cleanup files created during the test.  */
+  ignore_value (system ("rm -rf " BASE "*"));
+
+  return test_exit_status;
+}