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
= __llvm_libc::atexit(+[] {
25 status
|= __llvm_libc::atexit(+[] { a
++; });
31 EXPECT_EXITS(test
, 0);
34 TEST(LlvmLibcAtExit
, AtExitCallsSysExit
) {
36 __llvm_libc::atexit(+[] { _Exit(1); });
39 EXPECT_EXITS(test
, 1);
43 static __llvm_libc::cpp::array
<int, 256> arr
;
46 void register_atexit_handlers(__llvm_libc::cpp::integer_sequence
<int, Ts
...>) {
47 (__llvm_libc::atexit(+[] { arr
[size
++] = Ts
; }), ...);
50 template <int count
> constexpr auto getTest() {
52 __llvm_libc::atexit(+[] {
55 for (int i
= 0; i
< count
; i
++)
56 if (arr
[i
] != count
- 1 - i
)
59 register_atexit_handlers(
60 __llvm_libc::cpp::make_integer_sequence
<int, count
>{});
65 TEST(LlvmLibcAtExit
, ReverseOrder
) {
66 // In case tests ever run multiple times.
69 auto test
= getTest
<32>();
70 EXPECT_EXITS(test
, 0);
73 TEST(LlvmLibcAtExit
, Many
) {
74 // In case tests ever run multiple times.
77 auto test
= getTest
<256>();
78 EXPECT_EXITS(test
, 0);
81 TEST(LlvmLibcAtExit
, HandlerCallsAtExit
) {
84 +[] { __llvm_libc::atexit(+[] { __llvm_libc::exit(1); }); });
87 EXPECT_EXITS(test
, 1);