1 //===-- Unittests for signal ----------------------------------------------===//
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 "include/errno.h"
10 #include "include/signal.h"
11 #include "src/errno/llvmlibc_errno.h"
12 #include "src/signal/raise.h"
13 #include "src/signal/signal.h"
14 
15 #include "test/ErrnoSetterMatcher.h"
16 #include "utils/UnitTest/Test.h"
17 
18 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
19 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
20 
TEST(Signal,Invalid)21 TEST(Signal, Invalid) {
22   llvmlibc_errno = 0;
23   __llvm_libc::sighandler_t valid = +[](int) {};
24   EXPECT_THAT((void *)__llvm_libc::signal(0, valid),
25               Fails(EINVAL, (void *)SIG_ERR));
26   EXPECT_THAT((void *)__llvm_libc::signal(65, valid),
27               Fails(EINVAL, (void *)SIG_ERR));
28 }
29 
30 static int sum;
TEST(Signal,Basic)31 TEST(Signal, Basic) {
32   // In case test get run multiple times.
33   sum = 0;
34   ASSERT_NE(__llvm_libc::signal(SIGUSR1, +[](int) { sum++; }),
35             SIG_ERR);
36   ASSERT_THAT(__llvm_libc::raise(SIGUSR1), Succeeds());
37   EXPECT_EQ(sum, 1);
38   for (int i = 0; i < 10; i++)
39     ASSERT_THAT(__llvm_libc::raise(SIGUSR1), Succeeds());
40   EXPECT_EQ(sum, 11);
41 }
42