Debian Codegraph @

llvm-project/flang/unittests/Runtime/Format.cpp

raw
1 //===-- flang/unittests/Runtime/Format.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 #include "CrashHandlerFixture.h"
10 #include "../runtime/connection.h"
11 #include "../runtime/format-implementation.h"
12 #include "../runtime/io-error.h"
13 #include <string>
14 #include <tuple>
15 #include <vector>
16
17 using namespace Fortran::runtime;
18 using namespace Fortran::runtime::io;
19 using namespace std::literals::string_literals;
20
21 using ResultsTy = std::vector<std::string>;
22
23 // A test harness context for testing FormatControl
24 class TestFormatContext : public IoErrorHandler {
25 public:
26 using CharType = char;
27 TestFormatContext() : IoErrorHandler{"format.cpp", 1} {}
28 bool Emit(const char *, std::size_t, std::size_t = 0);
29 bool AdvanceRecord(int = 1);
30 void HandleRelativePosition(std::int64_t);
31 void HandleAbsolutePosition(std::int64_t);
32 void Report(const DataEdit &);
33 ResultsTy results;
34 MutableModes &mutableModes() { return mutableModes_; }
35 ConnectionState &GetConnectionState() { return connectionState_; }
36
37 private:
38 MutableModes mutableModes_;
39 ConnectionState connectionState_;
40 };
41
42 bool TestFormatContext::Emit(const char *s, std::size_t len, std::size_t) {
43 std::string str{s, len};
44 results.push_back("'"s + str + '\'');
45 return true;
46 }
47
48 bool TestFormatContext::AdvanceRecord(int n) {
49 while (n-- > 0) {
50 results.emplace_back("/");
51 }
52 return true;
53 }
54
55 void TestFormatContext::HandleAbsolutePosition(std::int64_t n) {
56 results.push_back("T"s + std::to_string(n));
57 }
58
59 void TestFormatContext::HandleRelativePosition(std::int64_t n) {
60 if (n < 0) {
61 results.push_back("TL"s + std::to_string(-n));
62 } else {
63 results.push_back(std::to_string(n) + 'X');
64 }
65 }
66
67 void TestFormatContext::Report(const DataEdit &edit) {
68 std::string str{edit.descriptor};
69 if (edit.repeat != 1) {
70 str = std::to_string(edit.repeat) + '*' + str;
71 }
72 if (edit.variation) {
73 str += edit.variation;
74 }
75 if (edit.width) {
76 str += std::to_string(*edit.width);
77 }
78 if (edit.digits) {
79 str += "."s + std::to_string(*edit.digits);
80 }
81 if (edit.expoDigits) {
82 str += "E"s + std::to_string(*edit.expoDigits);
83 }
84 // modes?
85 results.push_back(str);
86 }
87
88 struct FormatTests : public CrashHandlerFixture {};
89
90 TEST(FormatTests, FormatStringTraversal) {
91
92 using ParamsTy = std::tuple<int, const char *, ResultsTy, int>;
93
94 static const std::vector<ParamsTy> params{
95 {1, "('PI=',F9.7)", ResultsTy{"'PI='", "F9.7"}, 1},
96 {1, "(3HPI=F9.7)", ResultsTy{"'PI='", "F9.7"}, 1},
97 {1, "(3HPI=/F9.7)", ResultsTy{"'PI='", "/", "F9.7"}, 1},
98 {2, "('PI=',F9.7)", ResultsTy{"'PI='", "F9.7", "/", "'PI='", "F9.7"}, 1},
99 {2, "(2('PI=',F9.7),'done')",
100 ResultsTy{"'PI='", "F9.7", "'PI='", "F9.7", "'done'"}, 1},
101 {2, "(3('PI=',F9.7,:),'tooFar')",
102 ResultsTy{"'PI='", "F9.7", "'PI='", "F9.7"}, 1},
103 {2, "(*('PI=',F9.7,:))", ResultsTy{"'PI='", "F9.7", "'PI='", "F9.7"}, 1},
104 {1, "(3F9.7)", ResultsTy{"2*F9.7"}, 2},
105 };
106
107 for (const auto &[n, format, expect, repeat] : params) {
108 TestFormatContext context;
109 FormatControl<decltype(context)> control{
110 context, format, std::strlen(format)};
111
112 for (auto i{0}; i < n; i++) {
113 context.Report(/*edit=*/control.GetNextDataEdit(context, repeat));
114 }
115 control.Finish(context);
116
117 auto iostat{context.GetIoStat()};
118 ASSERT_EQ(iostat, 0) << "Expected iostat == 0, but GetIoStat() == "
119 << iostat;
120
121 // Create strings of the expected/actual results for printing errors
122 std::string allExpectedResults{""}, allActualResults{""};
123 for (const auto &res : context.results) {
124 allActualResults += " "s + res;
125 }
126 for (const auto &res : expect) {
127 allExpectedResults += " "s + res;
128 }
129
130 const auto &results = context.results;
131 ASSERT_EQ(expect, results) << "Expected '" << allExpectedResults
132 << "' but got '" << allActualResults << "'";
133 }
134 }
135
136 struct InvalidFormatFailure : CrashHandlerFixture {};
137
138 TEST(InvalidFormatFailure, ParenMismatch) {
139 static constexpr const char *format{"("};
140 static constexpr int repeat{1};
141
142 TestFormatContext context;
143 FormatControl<decltype(context)> control{
144 context, format, std::strlen(format)};
145
146 ASSERT_DEATH(
147 context.Report(/*edit=*/control.GetNextDataEdit(context, repeat)),
148 R"(FORMAT missing at least one '\)')");
149 }
150
151 TEST(InvalidFormatFailure, MissingPrecision) {
152 static constexpr const char *format{"(F9.)"};
153 static constexpr int repeat{1};
154
155 TestFormatContext context;
156 FormatControl<decltype(context)> control{
157 context, format, std::strlen(format)};
158
159 ASSERT_DEATH(
160 context.Report(/*edit=*/control.GetNextDataEdit(context, repeat)),
161 R"(Invalid FORMAT: integer expected at '\)')");
162 }
163
164 TEST(InvalidFormatFailure, MissingFormatWidth) {
165 static constexpr const char *format{"(F.9)"};
166 static constexpr int repeat{1};
167
168 TestFormatContext context;
169 FormatControl<decltype(context)> control{
170 context, format, std::strlen(format)};
171
172 ASSERT_DEATH(
173 context.Report(/*edit=*/control.GetNextDataEdit(context, repeat)),
174 "Invalid FORMAT: integer expected at '.'");
175 }