summaryrefslogtreecommitdiff
path: root/libc/test/src/stdlib/atexit_test.cpp
blob: eb295adfb719a00368c08667ea0c2fca1c0e28ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===-- Unittests for atexit ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/array.h"
#include "src/__support/CPP/utility.h"
#include "src/stdlib/atexit.h"
#include "src/stdlib/exit.h"
#include "test/UnitTest/Test.h"

static int a;
TEST(LlvmLibcAtExit, Basic) {
  // In case tests ever run multiple times.
  a = 0;

  auto test = [] {
    int status = __llvm_libc::atexit(+[] {
      if (a != 1)
        __builtin_trap();
    });
    status |= __llvm_libc::atexit(+[] { a++; });
    if (status)
      __builtin_trap();

    __llvm_libc::exit(0);
  };
  EXPECT_EXITS(test, 0);
}

TEST(LlvmLibcAtExit, AtExitCallsSysExit) {
  auto test = [] {
    __llvm_libc::atexit(+[] { _Exit(1); });
    __llvm_libc::exit(0);
  };
  EXPECT_EXITS(test, 1);
}

static int size;
static __llvm_libc::cpp::array<int, 256> arr;

template <int... Ts>
void register_atexit_handlers(__llvm_libc::cpp::integer_sequence<int, Ts...>) {
  (__llvm_libc::atexit(+[] { arr[size++] = Ts; }), ...);
}

template <int count> constexpr auto getTest() {
  return [] {
    __llvm_libc::atexit(+[] {
      if (size != count)
        __builtin_trap();
      for (int i = 0; i < count; i++)
        if (arr[i] != count - 1 - i)
          __builtin_trap();
    });
    register_atexit_handlers(
        __llvm_libc::cpp::make_integer_sequence<int, count>{});
    __llvm_libc::exit(0);
  };
}

TEST(LlvmLibcAtExit, ReverseOrder) {
  // In case tests ever run multiple times.
  size = 0;

  auto test = getTest<32>();
  EXPECT_EXITS(test, 0);
}

TEST(LlvmLibcAtExit, Many) {
  // In case tests ever run multiple times.
  size = 0;

  auto test = getTest<256>();
  EXPECT_EXITS(test, 0);
}

TEST(LlvmLibcAtExit, HandlerCallsAtExit) {
  auto test = [] {
    __llvm_libc::atexit(
        +[] { __llvm_libc::atexit(+[] { __llvm_libc::exit(1); }); });
    __llvm_libc::exit(0);
  };
  EXPECT_EXITS(test, 1);
}