1 | //===-- unittests/Runtime/Stop.cpp ------------------------------*- C++ -*-===// |
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 | //===----------------------------------------------------------------------===// |
8 | // |
9 | /// Test runtime API for STOP statement and runtime API to kill the program. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "flang/Runtime/stop.h" |
14 | #include "CrashHandlerFixture.h" |
15 | #include "flang-rt/runtime/environment.h" |
16 | #include <cstdlib> |
17 | #include <gtest/gtest.h> |
18 | |
19 | using namespace Fortran::runtime; |
20 | |
21 | struct TestProgramEnd : CrashHandlerFixture {}; |
22 | |
23 | TEST(TestProgramEnd, StopTest) { |
24 | EXPECT_EXIT(RTNAME(StopStatement)(), testing::ExitedWithCode(EXIT_SUCCESS), |
25 | "Fortran STOP" ); |
26 | } |
27 | |
28 | TEST(TestProgramEnd, StopTestNoStopMessage) { |
29 | putenv(string: const_cast<char *>("NO_STOP_MESSAGE=1" )); |
30 | Fortran::runtime::executionEnvironment.Configure( |
31 | 0, nullptr, nullptr, nullptr); |
32 | EXPECT_EXIT( |
33 | RTNAME(StopStatement)(), testing::ExitedWithCode(EXIT_SUCCESS), "" ); |
34 | } |
35 | |
36 | TEST(TestProgramEnd, StopMessageTest) { |
37 | static const char *message{"bye bye" }; |
38 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
39 | /*isErrorStop=*/false, /*quiet=*/false), |
40 | testing::ExitedWithCode(EXIT_SUCCESS), "Fortran STOP: bye bye" ); |
41 | |
42 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
43 | /*isErrorStop=*/false, /*quiet=*/true), |
44 | testing::ExitedWithCode(EXIT_SUCCESS), "" ); |
45 | |
46 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
47 | /*isErrorStop=*/true, /*quiet=*/false), |
48 | testing::ExitedWithCode(EXIT_FAILURE), "Fortran ERROR STOP: bye bye" ); |
49 | |
50 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
51 | /*isErrorStop=*/true, /*quiet=*/true), |
52 | testing::ExitedWithCode(EXIT_FAILURE), "" ); |
53 | } |
54 | |
55 | TEST(TestProgramEnd, NoStopMessageTest) { |
56 | putenv(string: const_cast<char *>("NO_STOP_MESSAGE=1" )); |
57 | Fortran::runtime::executionEnvironment.Configure( |
58 | 0, nullptr, nullptr, nullptr); |
59 | static const char *message{"bye bye" }; |
60 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
61 | /*isErrorStop=*/false, /*quiet=*/false), |
62 | testing::ExitedWithCode(EXIT_SUCCESS), "bye bye" ); |
63 | |
64 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
65 | /*isErrorStop=*/false, /*quiet=*/true), |
66 | testing::ExitedWithCode(EXIT_SUCCESS), "" ); |
67 | |
68 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
69 | /*isErrorStop=*/true, /*quiet=*/false), |
70 | testing::ExitedWithCode(EXIT_FAILURE), "Fortran ERROR STOP: bye bye" ); |
71 | |
72 | EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
73 | /*isErrorStop=*/true, /*quiet=*/true), |
74 | testing::ExitedWithCode(EXIT_FAILURE), "" ); |
75 | } |
76 | |
77 | TEST(TestProgramEnd, FailImageTest) { |
78 | EXPECT_EXIT( |
79 | RTNAME(FailImageStatement)(), testing::ExitedWithCode(EXIT_FAILURE), "" ); |
80 | } |
81 | |
82 | TEST(TestProgramEnd, ExitTest) { |
83 | EXPECT_EXIT(RTNAME(Exit)(), testing::ExitedWithCode(EXIT_SUCCESS), "" ); |
84 | EXPECT_EXIT( |
85 | RTNAME(Exit)(EXIT_FAILURE), testing::ExitedWithCode(EXIT_FAILURE), "" ); |
86 | } |
87 | |
88 | TEST(TestProgramEnd, AbortTest) { EXPECT_DEATH(RTNAME(Abort)(), "" ); } |
89 | |
90 | TEST(TestProgramEnd, CrashTest) { |
91 | static const std::string crashMessage{"bad user code" }; |
92 | static const std::string fileName{"file name" }; |
93 | static const std::string headMessage{"fatal Fortran runtime error\\(" }; |
94 | static const std::string tailMessage{":343\\): " }; |
95 | static const std::string fullMessage{ |
96 | headMessage + fileName + tailMessage + crashMessage}; |
97 | EXPECT_DEATH( |
98 | RTNAME(ReportFatalUserError)(crashMessage.c_str(), fileName.c_str(), 343), |
99 | fullMessage.c_str()); |
100 | } |
101 | |