[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / stdlib / atexit_test.cpp
blobc25202ff54f696e54b8cced1b375efcbae5e7b67
1 //===-- Unittests for atexit ----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/array.h"
10 #include "src/__support/CPP/utility.h"
11 #include "src/stdlib/_Exit.h"
12 #include "src/stdlib/atexit.h"
13 #include "src/stdlib/exit.h"
14 #include "test/UnitTest/Test.h"
16 static int a;
17 TEST(LlvmLibcAtExit, Basic) {
18 // In case tests ever run multiple times.
19 a = 0;
21 auto test = [] {
22 int status = LIBC_NAMESPACE::atexit(+[] {
23 if (a != 1)
24 __builtin_trap();
25 });
26 status |= LIBC_NAMESPACE::atexit(+[] { a++; });
27 if (status)
28 __builtin_trap();
30 LIBC_NAMESPACE::exit(0);
32 EXPECT_EXITS(test, 0);
35 TEST(LlvmLibcAtExit, AtExitCallsSysExit) {
36 auto test = [] {
37 LIBC_NAMESPACE::atexit(+[] { LIBC_NAMESPACE::_Exit(1); });
38 LIBC_NAMESPACE::exit(0);
40 EXPECT_EXITS(test, 1);
43 static int size;
44 static LIBC_NAMESPACE::cpp::array<int, 256> arr;
46 template <int... Ts>
47 void register_atexit_handlers(
48 LIBC_NAMESPACE::cpp::integer_sequence<int, Ts...>) {
49 (LIBC_NAMESPACE::atexit(+[] { arr[size++] = Ts; }), ...);
52 template <int count> constexpr auto getTest() {
53 return [] {
54 LIBC_NAMESPACE::atexit(+[] {
55 if (size != count)
56 __builtin_trap();
57 for (int i = 0; i < count; i++)
58 if (arr[i] != count - 1 - i)
59 __builtin_trap();
60 });
61 register_atexit_handlers(
62 LIBC_NAMESPACE::cpp::make_integer_sequence<int, count>{});
63 LIBC_NAMESPACE::exit(0);
67 TEST(LlvmLibcAtExit, ReverseOrder) {
68 // In case tests ever run multiple times.
69 size = 0;
71 auto test = getTest<32>();
72 EXPECT_EXITS(test, 0);
75 TEST(LlvmLibcAtExit, Many) {
76 // In case tests ever run multiple times.
77 size = 0;
79 auto test = getTest<256>();
80 EXPECT_EXITS(test, 0);
83 TEST(LlvmLibcAtExit, HandlerCallsAtExit) {
84 auto test = [] {
85 LIBC_NAMESPACE::atexit(
86 +[] { LIBC_NAMESPACE::atexit(+[] { LIBC_NAMESPACE::exit(1); }); });
87 LIBC_NAMESPACE::exit(0);
89 EXPECT_EXITS(test, 1);