Hey Bruno,

On 3/13/24 2:57 PM, Bruno Haible wrote:
> Currently, these functions are only available in Solaris and IRIX
> (according to the 'show-portability' tool in
> gnulib/maint-tools/platforms/various-symlists [1]).

Thanks for the link. I don't have access to MacOS, HP-UX, or IRIX
machines so that is helpful. The test environment documentation also
looks useful. If there is any interest in GNU Hurd or Solaris then I
could write them once I end up needing a virtual machine for them
again. Speaking of Solaris, I believe that this line should have
"Solaris 11 OmniOS" instead of "Solaris 11 OmnioOS" [1].

> we should better wait for the new POSIX standard to become available,
> or for glibc to implement the two functions. (glibc, so far, has
> sigabbrev_np [3].)

That sounds good. I don't see sig2str used much so it is probably best
to wait until glibc or POSIX.

> The patch is nearly good. I'd like to ask for three things:

Sure. My first time touching files in modules/* and tests/* so I was
expecting it not to be perfect. :)

>   Rationale: This verifies, en passant, that the file can be #included
>   with only <config.h> as a prerequisite.

Ah, that makes sense. I am in the habit of doing #include <...> headers,
empty line, and then #include "..." headers. But since it makes the
tests less useful, your method works better.

I've changed it to this:

#include <config.h>

#include "sig2str.h"

#include <string.h>

#include "macros.h"

So we have sig2str.h included second and the minimal amount of headers
needed to compile. We get the definition of STREQ from macros.h but
must include <string.h> as well for strcmp.

> * In the .c file, separate individual tests somehow, for example through blank
>   lines:

Rationale makes sense to me. Fixed.

> * In the ChangeLog entry, list the "interesting" things first, and the
>   changes that are merely mechanical updates later. This makes it nicer
>   to follow the changes.

Fixed. Usually I just copied and pasted from git status. Your
reasoning makes sense though.

[1] 
https://git.savannah.gnu.org/cgit/gnulib/maint-tools.git/tree/platforms/test-environments.txt#n37

Collin
From ba4402808278b7741ed0e4a208fbabc0fc8e1a96 Mon Sep 17 00:00:00 2001
From: Collin Funk <[email protected]>
Date: Wed, 13 Mar 2024 12:17:41 -0700
Subject: [PATCH] sig2str: Add tests.

* tests/test-sig2str.c: New file.
* modules/sig2str-tests: New file.
---
 ChangeLog             |  6 ++++
 modules/sig2str-tests | 11 ++++++
 tests/test-sig2str.c  | 82 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 modules/sig2str-tests
 create mode 100644 tests/test-sig2str.c

diff --git a/ChangeLog b/ChangeLog
index ff9cca6439..ff539332cb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-03-13  Collin Funk  <[email protected]>
+
+	sig2str: Add tests.
+	* tests/test-sig2str.c: New file.
+	* modules/sig2str-tests: New file.
+
 2024-03-12  Collin Funk  <[email protected]>
 
 	gnulib-tool.py: Follow gnulib-tool changes, part 56.
diff --git a/modules/sig2str-tests b/modules/sig2str-tests
new file mode 100644
index 0000000000..c995f1474a
--- /dev/null
+++ b/modules/sig2str-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-sig2str.c
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sig2str
+check_PROGRAMS += test-sig2str
diff --git a/tests/test-sig2str.c b/tests/test-sig2str.c
new file mode 100644
index 0000000000..c7b65d50b3
--- /dev/null
+++ b/tests/test-sig2str.c
@@ -0,0 +1,82 @@
+/* Test the sig2str and str2sig functions.
+   Copyright (C) 2024 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 <[email protected]>, 2024.  */
+
+#include <config.h>
+
+#include "sig2str.h"
+
+#include <string.h>
+
+#include "macros.h"
+
+int
+main (void)
+{
+  char buffer[SIG2STR_MAX];
+  int signo;
+
+  /* Test sig2str on signals specified by ISO C.  */
+
+  ASSERT (sig2str (SIGABRT, buffer) == 0);
+  ASSERT (STREQ (buffer, "ABRT"));
+
+  ASSERT (sig2str (SIGFPE, buffer) == 0);
+  ASSERT (STREQ (buffer, "FPE"));
+
+  ASSERT (sig2str (SIGILL, buffer) == 0);
+  ASSERT (STREQ (buffer, "ILL"));
+
+  ASSERT (sig2str (SIGINT, buffer) == 0);
+  ASSERT (STREQ (buffer, "INT"));
+
+  ASSERT (sig2str (SIGSEGV, buffer) == 0);
+  ASSERT (STREQ (buffer, "SEGV"));
+
+  ASSERT (sig2str (SIGTERM, buffer) == 0);
+  ASSERT (STREQ (buffer, "TERM"));
+
+  /* Test str2sig on signals specified by ISO C.  */
+
+  ASSERT (str2sig ("ABRT", &signo) == 0);
+  ASSERT (signo == SIGABRT);
+
+  ASSERT (str2sig ("FPE", &signo) == 0);
+  ASSERT (signo == SIGFPE);
+
+  ASSERT (str2sig ("ILL", &signo) == 0);
+  ASSERT (signo == SIGILL);
+
+  ASSERT (str2sig ("INT", &signo) == 0);
+  ASSERT (signo == SIGINT);
+
+  ASSERT (str2sig ("SEGV", &signo) == 0);
+  ASSERT (signo == SIGSEGV);
+
+  ASSERT (str2sig ("TERM", &signo) == 0);
+  ASSERT (signo == SIGTERM);
+
+  /* Check behavior of sig2str on invalid signals.  */
+
+  ASSERT (sig2str (-714, buffer) == -1);
+
+  /* Check behavior of str2sig on invalid signals.  */
+
+  ASSERT (str2sig ("Not a signal", &signo) == -1);
+
+  return 0;
+}
-- 
2.44.0

Reply via email to