1 //===-- Unittests for atexit ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/array.h"
10 #include "src/__support/CPP/utility.h"
11 #include "src/stdlib/atexit.h"
12 #include "src/stdlib/exit.h"
13 #include "test/UnitTest/Test.h"
16 TEST(LlvmLibcAtExit
, Basic
) {
17 // In case tests ever run multiple times.
21 int status
= LIBC_NAMESPACE::atexit(+[] {
25 status
|= LIBC_NAMESPACE::atexit(+[] { a
++; });
29 LIBC_NAMESPACE::exit(0);
31 EXPECT_EXITS(test
, 0);
34 TEST(LlvmLibcAtExit
, AtExitCallsSysExit
) {
36 LIBC_NAMESPACE::atexit(+[] { _Exit(1); });
37 LIBC_NAMESPACE::exit(0);
39 EXPECT_EXITS(test
, 1);
43 static LIBC_NAMESPACE::cpp::array
<int, 256> arr
;
46 void register_atexit_handlers(
47 LIBC_NAMESPACE::cpp::integer_sequence
<int, Ts
...>) {
48 (LIBC_NAMESPACE::atexit(+[] { arr
[size
++] = Ts
; }), ...);
51 template <int count
> constexpr auto getTest() {
53 LIBC_NAMESPACE::atexit(+[] {
56 for (int i
= 0; i
< count
; i
++)
57 if (arr
[i
] != count
- 1 - i
)
60 register_atexit_handlers(
61 LIBC_NAMESPACE::cpp::make_integer_sequence
<int, count
>{});
62 LIBC_NAMESPACE::exit(0);
66 TEST(LlvmLibcAtExit
, ReverseOrder
) {
67 // In case tests ever run multiple times.
70 auto test
= getTest
<32>();
71 EXPECT_EXITS(test
, 0);
74 TEST(LlvmLibcAtExit
, Many
) {
75 // In case tests ever run multiple times.
78 auto test
= getTest
<256>();
79 EXPECT_EXITS(test
, 0);
82 TEST(LlvmLibcAtExit
, HandlerCallsAtExit
) {
84 LIBC_NAMESPACE::atexit(
85 +[] { LIBC_NAMESPACE::atexit(+[] { LIBC_NAMESPACE::exit(1); }); });
86 LIBC_NAMESPACE::exit(0);
88 EXPECT_EXITS(test
, 1);