xref: /llvm-project/libc/test/src/string/strsignal_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
107793f95SMichael Jones //===-- Unittests for strsignal -------------------------------------------===//
207793f95SMichael Jones //
307793f95SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
407793f95SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
507793f95SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
607793f95SMichael Jones //
707793f95SMichael Jones //===----------------------------------------------------------------------===//
807793f95SMichael Jones 
907793f95SMichael Jones #include "src/string/strsignal.h"
10af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1107793f95SMichael Jones 
1207793f95SMichael Jones #include <signal.h>
1307793f95SMichael Jones 
TEST(LlvmLibcStrSignalTest,KnownSignals)1407793f95SMichael Jones TEST(LlvmLibcStrSignalTest, KnownSignals) {
15*b6bc9d72SGuillaume Chatelet   ASSERT_STREQ(LIBC_NAMESPACE::strsignal(1), "Hangup");
1607793f95SMichael Jones 
1707793f95SMichael Jones   const char *message_array[] = {
1807793f95SMichael Jones       "Unknown signal 0", // unknown
1907793f95SMichael Jones       "Hangup",
2007793f95SMichael Jones       "Interrupt",
2107793f95SMichael Jones       "Quit",
2207793f95SMichael Jones       "Illegal instruction",
2307793f95SMichael Jones       "Trace/breakpoint trap",
2407793f95SMichael Jones       "Aborted",
2507793f95SMichael Jones       "Bus error",
2607793f95SMichael Jones       "Floating point exception",
2707793f95SMichael Jones       "Killed",
2807793f95SMichael Jones       "User defined signal 1",
2907793f95SMichael Jones       "Segmentation fault",
3007793f95SMichael Jones       "User defined signal 2",
3107793f95SMichael Jones       "Broken pipe",
3207793f95SMichael Jones       "Alarm clock",
3307793f95SMichael Jones       "Terminated",
3407793f95SMichael Jones       "Stack fault",
3507793f95SMichael Jones       "Child exited",
3607793f95SMichael Jones       "Continued",
3707793f95SMichael Jones       "Stopped (signal)",
3807793f95SMichael Jones       "Stopped",
3907793f95SMichael Jones       "Stopped (tty input)",
4007793f95SMichael Jones       "Stopped (tty output)",
4107793f95SMichael Jones       "Urgent I/O condition",
4207793f95SMichael Jones       "CPU time limit exceeded",
4307793f95SMichael Jones       "File size limit exceeded",
4407793f95SMichael Jones       "Virtual timer expired",
4507793f95SMichael Jones       "Profiling timer expired",
4607793f95SMichael Jones       "Window changed",
4707793f95SMichael Jones       "I/O possible",
4807793f95SMichael Jones       "Power failure",
4907793f95SMichael Jones       "Bad system call",
5007793f95SMichael Jones   };
5107793f95SMichael Jones 
5207793f95SMichael Jones   // There are supposed to be 32 of these, but sometimes SIGRTMIN is shifted to
5307793f95SMichael Jones   // reserve some.
5407793f95SMichael Jones   const char *rt_message_array[] = {
5507793f95SMichael Jones       "Real-time signal 0",  "Real-time signal 1",  "Real-time signal 2",
5607793f95SMichael Jones       "Real-time signal 3",  "Real-time signal 4",  "Real-time signal 5",
5707793f95SMichael Jones       "Real-time signal 6",  "Real-time signal 7",  "Real-time signal 8",
5807793f95SMichael Jones       "Real-time signal 9",  "Real-time signal 10", "Real-time signal 11",
5907793f95SMichael Jones       "Real-time signal 12", "Real-time signal 13", "Real-time signal 14",
6007793f95SMichael Jones       "Real-time signal 15", "Real-time signal 16", "Real-time signal 17",
6107793f95SMichael Jones       "Real-time signal 18", "Real-time signal 19", "Real-time signal 20",
6207793f95SMichael Jones       "Real-time signal 21", "Real-time signal 22", "Real-time signal 23",
6307793f95SMichael Jones       "Real-time signal 24", "Real-time signal 25", "Real-time signal 26",
6407793f95SMichael Jones       "Real-time signal 27", "Real-time signal 28", "Real-time signal 29",
6507793f95SMichael Jones       "Real-time signal 30", "Real-time signal 31", "Real-time signal 32",
6607793f95SMichael Jones   };
6707793f95SMichael Jones 
6807793f95SMichael Jones   for (size_t i = 0; i < (sizeof(message_array) / sizeof(char *)); ++i) {
694e298c32SRoland McGrath     ASSERT_EQ(static_cast<size_t>(static_cast<int>(i)), i);
70*b6bc9d72SGuillaume Chatelet     EXPECT_STREQ(LIBC_NAMESPACE::strsignal(static_cast<int>(i)),
71*b6bc9d72SGuillaume Chatelet                  message_array[i]);
7207793f95SMichael Jones   }
7307793f95SMichael Jones 
744e298c32SRoland McGrath   for (int i = 0; i < SIGRTMAX - SIGRTMIN; ++i) {
75*b6bc9d72SGuillaume Chatelet     EXPECT_STREQ(LIBC_NAMESPACE::strsignal(i + SIGRTMIN), rt_message_array[i]);
7607793f95SMichael Jones   }
7707793f95SMichael Jones }
7807793f95SMichael Jones 
TEST(LlvmLibcStrsignalTest,UnknownSignals)7907793f95SMichael Jones TEST(LlvmLibcStrsignalTest, UnknownSignals) {
80*b6bc9d72SGuillaume Chatelet   ASSERT_STREQ(LIBC_NAMESPACE::strsignal(-1), "Unknown signal -1");
81*b6bc9d72SGuillaume Chatelet   ASSERT_STREQ(LIBC_NAMESPACE::strsignal(65), "Unknown signal 65");
82*b6bc9d72SGuillaume Chatelet   ASSERT_STREQ(LIBC_NAMESPACE::strsignal(2147483647),
83*b6bc9d72SGuillaume Chatelet                "Unknown signal 2147483647");
84*b6bc9d72SGuillaume Chatelet   ASSERT_STREQ(LIBC_NAMESPACE::strsignal(-2147483648),
8507793f95SMichael Jones                "Unknown signal -2147483648");
8607793f95SMichael Jones }
87