1 //===-- Unittests for at_quick_exit ---------------------------------------===//
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/_Exit.h"
12 #include "src/stdlib/at_quick_exit.h"
13 #include "src/stdlib/quick_exit.h"
14 #include "test/UnitTest/Test.h"
17 TEST(LlvmLibcAtQuickExit
, Basic
) {
18 // In case tests ever run multiple times.
22 int status
= LIBC_NAMESPACE::at_quick_exit(+[] {
26 status
|= LIBC_NAMESPACE::at_quick_exit(+[] { a
++; });
30 LIBC_NAMESPACE::quick_exit(0);
32 EXPECT_EXITS(test
, 0);
35 TEST(LlvmLibcAtQuickExit
, AtQuickExitCallsSysExit
) {
37 LIBC_NAMESPACE::at_quick_exit(+[] { LIBC_NAMESPACE::_Exit(1); });
38 LIBC_NAMESPACE::quick_exit(0);
40 EXPECT_EXITS(test
, 1);
44 static LIBC_NAMESPACE::cpp::array
<int, 256> arr
;
47 void register_at_quick_exit_handlers(
48 LIBC_NAMESPACE::cpp::integer_sequence
<int, Ts
...>) {
49 (LIBC_NAMESPACE::at_quick_exit(+[] { arr
[size
++] = Ts
; }), ...);
52 template <int count
> constexpr auto get_test() {
54 LIBC_NAMESPACE::at_quick_exit(+[] {
57 for (int i
= 0; i
< count
; i
++)
58 if (arr
[i
] != count
- 1 - i
)
61 register_at_quick_exit_handlers(
62 LIBC_NAMESPACE::cpp::make_integer_sequence
<int, count
>{});
63 LIBC_NAMESPACE::quick_exit(0);
67 TEST(LlvmLibcAtQuickExit
, ReverseOrder
) {
68 // In case tests ever run multiple times.
71 auto test
= get_test
<32>();
72 EXPECT_EXITS(test
, 0);
75 TEST(LlvmLibcAtQuickExit
, Many
) {
76 // In case tests ever run multiple times.
79 auto test
= get_test
<256>();
80 EXPECT_EXITS(test
, 0);
83 TEST(LlvmLibcAtQuickExit
, HandlerCallsAtQuickExit
) {
85 LIBC_NAMESPACE::at_quick_exit(+[] {
86 LIBC_NAMESPACE::at_quick_exit(+[] { LIBC_NAMESPACE::quick_exit(1); });
88 LIBC_NAMESPACE::quick_exit(0);
90 EXPECT_EXITS(test
, 1);