1//===-- Unittests for strsignal -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/string/strsignal.h"
10#include "test/UnitTest/Test.h"
11
12#include <signal.h>
13
14TEST(LlvmLibcStrSignalTest, KnownSignals) {
15 ASSERT_STREQ(LIBC_NAMESPACE::strsignal(1), "Hangup");
16
17 const char *message_array[] = {
18 "Unknown signal 0", // unknown
19 "Hangup",
20 "Interrupt",
21 "Quit",
22 "Illegal instruction",
23 "Trace/breakpoint trap",
24 "Aborted",
25 "Bus error",
26 "Floating point exception",
27 "Killed",
28 "User defined signal 1",
29 "Segmentation fault",
30 "User defined signal 2",
31 "Broken pipe",
32 "Alarm clock",
33 "Terminated",
34 "Stack fault",
35 "Child exited",
36 "Continued",
37 "Stopped (signal)",
38 "Stopped",
39 "Stopped (tty input)",
40 "Stopped (tty output)",
41 "Urgent I/O condition",
42 "CPU time limit exceeded",
43 "File size limit exceeded",
44 "Virtual timer expired",
45 "Profiling timer expired",
46 "Window changed",
47 "I/O possible",
48 "Power failure",
49 "Bad system call",
50 };
51
52 // There are supposed to be 32 of these, but sometimes SIGRTMIN is shifted to
53 // reserve some.
54 const char *rt_message_array[] = {
55 "Real-time signal 0", "Real-time signal 1", "Real-time signal 2",
56 "Real-time signal 3", "Real-time signal 4", "Real-time signal 5",
57 "Real-time signal 6", "Real-time signal 7", "Real-time signal 8",
58 "Real-time signal 9", "Real-time signal 10", "Real-time signal 11",
59 "Real-time signal 12", "Real-time signal 13", "Real-time signal 14",
60 "Real-time signal 15", "Real-time signal 16", "Real-time signal 17",
61 "Real-time signal 18", "Real-time signal 19", "Real-time signal 20",
62 "Real-time signal 21", "Real-time signal 22", "Real-time signal 23",
63 "Real-time signal 24", "Real-time signal 25", "Real-time signal 26",
64 "Real-time signal 27", "Real-time signal 28", "Real-time signal 29",
65 "Real-time signal 30", "Real-time signal 31", "Real-time signal 32",
66 };
67
68 for (size_t i = 0; i < (sizeof(message_array) / sizeof(char *)); ++i) {
69 ASSERT_EQ(static_cast<size_t>(static_cast<int>(i)), i);
70 EXPECT_STREQ(LIBC_NAMESPACE::strsignal(static_cast<int>(i)),
71 message_array[i]);
72 }
73
74 for (int i = 0; i < SIGRTMAX - SIGRTMIN; ++i) {
75 EXPECT_STREQ(LIBC_NAMESPACE::strsignal(i + SIGRTMIN), rt_message_array[i]);
76 }
77}
78
79TEST(LlvmLibcStrsignalTest, UnknownSignals) {
80 ASSERT_STREQ(LIBC_NAMESPACE::strsignal(-1), "Unknown signal -1");
81 ASSERT_STREQ(LIBC_NAMESPACE::strsignal(65), "Unknown signal 65");
82 ASSERT_STREQ(LIBC_NAMESPACE::strsignal(2147483647),
83 "Unknown signal 2147483647");
84 ASSERT_STREQ(LIBC_NAMESPACE::strsignal(-2147483648),
85 "Unknown signal -2147483648");
86}
87

source code of libc/test/src/string/strsignal_test.cpp