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