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 
22 TEST(TestProgramEnd, StopTest) {
23   EXPECT_EXIT(RTNAME(StopStatement)(), testing::ExitedWithCode(EXIT_SUCCESS),
24       "Fortran STOP");
25 }
26 
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 
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 
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 
74 TEST(TestProgramEnd, FailImageTest) {
75   EXPECT_EXIT(
76       RTNAME(FailImageStatement)(), testing::ExitedWithCode(EXIT_FAILURE), "");
77 }
78 
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 
85 TEST(TestProgramEnd, AbortTest) { EXPECT_DEATH(RTNAME(Abort)(), ""); }
86