[RDF] Add RegisterRef::idx and make toUnitId constexpr
[llvm-project.git] / libc / test / UnitTest / LibcDeathTestExecutors.cpp
blobf25c0f77240024a0eafc0020ec65d30510353d02
1 //===-- Implementation of libc death test executors -----------------------===//
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 "LibcTest.h"
11 #include "test/UnitTest/ExecuteFunction.h"
12 #include "test/UnitTest/TestLogger.h"
14 #include <cassert>
16 namespace __llvm_libc {
17 namespace testing {
19 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
20 const char *LHSStr, const char *RHSStr,
21 internal::Location Loc) {
22 testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 500);
24 if (const char *error = Result.get_error()) {
25 Ctx->markFail();
26 tlog << Loc;
27 tlog << error << '\n';
28 return false;
31 if (Result.timed_out()) {
32 Ctx->markFail();
33 tlog << Loc;
34 tlog << "Process timed out after " << 500 << " milliseconds.\n";
35 return false;
38 if (Result.exited_normally()) {
39 Ctx->markFail();
40 tlog << Loc;
41 tlog << "Expected " << LHSStr
42 << " to be killed by a signal\nBut it exited normally!\n";
43 return false;
46 int KilledBy = Result.get_fatal_signal();
47 assert(KilledBy != 0 && "Not killed by any signal");
48 if (Signal == -1 || KilledBy == Signal)
49 return true;
51 using testutils::signal_as_string;
52 Ctx->markFail();
53 tlog << Loc;
54 tlog << " Expected: " << LHSStr << '\n'
55 << "To be killed by signal: " << Signal << '\n'
56 << " Which is: " << signal_as_string(Signal) << '\n'
57 << " But it was killed by: " << KilledBy << '\n'
58 << " Which is: " << signal_as_string(KilledBy) << '\n';
59 return false;
62 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
63 const char *LHSStr, const char *RHSStr,
64 internal::Location Loc) {
65 testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 500);
67 if (const char *error = Result.get_error()) {
68 Ctx->markFail();
69 tlog << Loc;
70 tlog << error << '\n';
71 return false;
74 if (Result.timed_out()) {
75 Ctx->markFail();
76 tlog << Loc;
77 tlog << "Process timed out after " << 500 << " milliseconds.\n";
78 return false;
81 if (!Result.exited_normally()) {
82 Ctx->markFail();
83 tlog << Loc;
84 tlog << "Expected " << LHSStr << '\n'
85 << "to exit with exit code " << ExitCode << '\n'
86 << "But it exited abnormally!\n";
87 return false;
90 int ActualExit = Result.get_exit_code();
91 if (ActualExit == ExitCode)
92 return true;
94 Ctx->markFail();
95 tlog << Loc;
96 tlog << "Expected exit code of: " << LHSStr << '\n'
97 << " Which is: " << ActualExit << '\n'
98 << " To be equal to: " << RHSStr << '\n'
99 << " Which is: " << ExitCode << '\n';
100 return false;
103 } // namespace testing
104 } // namespace __llvm_libc