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

source code of libc/test/src/signal/signal_test.cpp