| 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( |
| 30 |
0, nullptr, nullptr, nullptr); |
| 31 |
EXPECT_EXIT( |
| 32 |
RTNAME(StopStatement)(), testing::ExitedWithCode(EXIT_SUCCESS), ""); |
| 33 |
} |
| 34 |
|
| 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"); |
| 40 |
|
| 41 |
EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
| 42 |
/*isErrorStop=*/false, /*quiet=*/true), |
| 43 |
testing::ExitedWithCode(EXIT_SUCCESS), ""); |
| 44 |
|
| 45 |
EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
| 46 |
/*isErrorStop=*/true, /*quiet=*/false), |
| 47 |
testing::ExitedWithCode(EXIT_FAILURE), "Fortran ERROR STOP: bye bye"); |
| 48 |
|
| 49 |
EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
| 50 |
/*isErrorStop=*/true, /*quiet=*/true), |
| 51 |
testing::ExitedWithCode(EXIT_FAILURE), ""); |
| 52 |
} |
| 53 |
|
| 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"); |
| 62 |
|
| 63 |
EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
| 64 |
/*isErrorStop=*/false, /*quiet=*/true), |
| 65 |
testing::ExitedWithCode(EXIT_SUCCESS), ""); |
| 66 |
|
| 67 |
EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
| 68 |
/*isErrorStop=*/true, /*quiet=*/false), |
| 69 |
testing::ExitedWithCode(EXIT_FAILURE), "Fortran ERROR STOP: bye bye"); |
| 70 |
|
| 71 |
EXPECT_EXIT(RTNAME(StopStatementText)(message, std::strlen(message), |
| 72 |
/*isErrorStop=*/true, /*quiet=*/true), |
| 73 |
testing::ExitedWithCode(EXIT_FAILURE), ""); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(TestProgramEnd, FailImageTest) { |
| 77 |
EXPECT_EXIT( |
| 78 |
RTNAME(FailImageStatement)(), testing::ExitedWithCode(EXIT_FAILURE), ""); |
| 79 |
} |
| 80 |
|
| 81 |
TEST(TestProgramEnd, ExitTest) { |
| 82 |
EXPECT_EXIT(RTNAME(Exit)(), testing::ExitedWithCode(EXIT_SUCCESS), ""); |
| 83 |
EXPECT_EXIT( |
| 84 |
RTNAME(Exit)(EXIT_FAILURE), testing::ExitedWithCode(EXIT_FAILURE), ""); |
| 85 |
} |
| 86 |
|
| 87 |
TEST(TestProgramEnd, AbortTest) { EXPECT_DEATH(RTNAME(Abort)(), ""); } |
| 88 |
|
| 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}; |
| 96 |
EXPECT_DEATH( |
| 97 |
RTNAME(ReportFatalUserError)(crashMessage.c_str(), fileName.c_str(), 343), |
| 98 |
fullMessage.c_str()); |
| 99 |
} |