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