Make test more lenient for custom clang version strings
[llvm-project.git] / libc / test / src / stdlib / at_quick_exit_test.cpp
blobc0aac4d20d92ccb200eb20a4da15d767c9575b52
1 //===-- Unittests for at_quick_exit ---------------------------------------===//
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/at_quick_exit.h"
13 #include "src/stdlib/quick_exit.h"
14 #include "test/UnitTest/Test.h"
16 static int a;
17 TEST(LlvmLibcAtQuickExit, Basic) {
18 // In case tests ever run multiple times.
19 a = 0;
21 auto test = [] {
22 int status = LIBC_NAMESPACE::at_quick_exit(+[] {
23 if (a != 1)
24 __builtin_trap();
25 });
26 status |= LIBC_NAMESPACE::at_quick_exit(+[] { a++; });
27 if (status)
28 __builtin_trap();
30 LIBC_NAMESPACE::quick_exit(0);
32 EXPECT_EXITS(test, 0);
35 TEST(LlvmLibcAtQuickExit, AtQuickExitCallsSysExit) {
36 auto test = [] {
37 LIBC_NAMESPACE::at_quick_exit(+[] { LIBC_NAMESPACE::_Exit(1); });
38 LIBC_NAMESPACE::quick_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_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() {
53 return [] {
54 LIBC_NAMESPACE::at_quick_exit(+[] {
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_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.
69 size = 0;
71 auto test = get_test<32>();
72 EXPECT_EXITS(test, 0);
75 TEST(LlvmLibcAtQuickExit, Many) {
76 // In case tests ever run multiple times.
77 size = 0;
79 auto test = get_test<256>();
80 EXPECT_EXITS(test, 0);
83 TEST(LlvmLibcAtQuickExit, HandlerCallsAtQuickExit) {
84 auto test = [] {
85 LIBC_NAMESPACE::at_quick_exit(+[] {
86 LIBC_NAMESPACE::at_quick_exit(+[] { LIBC_NAMESPACE::quick_exit(1); });
87 });
88 LIBC_NAMESPACE::quick_exit(0);
90 EXPECT_EXITS(test, 1);