I noticed that sig2str and str2sig may be part of the next POSIX
release and they are already provided by Solaris [1] [2].

I think it would be nice to put these two functions inside of signal.h
since it is becoming standardized. We can keep the sig2str.h header
with a warning in a similar way to getprogname.h.

Before doing that, I think it would be best to add a basic test to
ensure no breakage occurs.

Any thoughts on this? I've provided a patch with some basic tests
testing behavior on the ISO C signals.

The behavior for signals between SIGRTMIN + 1 and
(SIGRTMIN + SIGRTMAX) / 2 is untested since I am unsure how to handle
that for Windows. IIRC they define all of the ISO C ones so this test
should work fine.

[1] https://www.austingroupbugs.net/view.php?id=1138
[2] https://docs.oracle.com/cd/E86824_01/html/E54766/sig2str-3c.html

Collin
From 7d22aeb8c246e0a25a6bea4ab2d8503542340141 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 13 Mar 2024 12:17:41 -0700
Subject: [PATCH] sig2str: Add tests.

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

diff --git a/ChangeLog b/ChangeLog
index ff9cca6439..67cf87c8a9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-03-13  Collin Funk  <collin.fu...@gmail.com>
+
+	sig2str: Add tests.
+	* modules/sig2str-tests: New file.
+	* tests/test-sig2str.c: New file.
+
 2024-03-12  Collin Funk  <collin.fu...@gmail.com>
 
 	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..d390f5d28d
--- /dev/null
+++ b/tests/test-sig2str.c
@@ -0,0 +1,65 @@
+/* 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 <collin.fu...@gmail.com>, 2024.  */
+
+#include <config.h>
+
+#include <string.h>
+
+#include "sig2str.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 on invalid signals.  */
+  ASSERT (sig2str (-714, buffer) == -1);
+  ASSERT (str2sig ("Not a signal", &signo) == -1);
+
+  return 0;
+}
-- 
2.44.0

Reply via email to