1 //===-- flang/unittests/RuntimeGTest/ListInputTest.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/descriptor.h"
11 #include "../../runtime/io-api.h"
12 #include "../../runtime/io-error.h"
13 
14 using namespace Fortran::runtime;
15 using namespace Fortran::runtime::io;
16 
17 // Pads characters with whitespace when needed
SetCharacter(char * to,std::size_t n,const char * from)18 void SetCharacter(char *to, std::size_t n, const char *from) {
19   auto len{std::strlen(from)};
20   std::memcpy(to, from, std::min(len, n));
21   if (len < n) {
22     std::memset(to + len, ' ', n - len);
23   }
24 }
25 
26 struct InputTest : CrashHandlerFixture {};
27 
TEST(InputTest,TestListInputAlphabet)28 TEST(InputTest, TestListInputAlphabet) {
29   constexpr int numInputBuffers{2};
30   constexpr int maxInputBufferLength{32};
31   char inputBuffers[numInputBuffers][maxInputBufferLength];
32   const char expectedOutput[]{
33       "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "};
34   int j{0};
35 
36   // Use _two_ input buffers and _three_ output buffers. Note the `3*` in the
37   // _inputBuffers_.
38   SetCharacter(inputBuffers[j++], maxInputBufferLength,
39       "3*'abcdefghijklmnopqrstuvwxyzABC");
40   SetCharacter(
41       inputBuffers[j++], maxInputBufferLength, "DEFGHIJKLMNOPQRSTUVWXYZ'");
42 
43   StaticDescriptor<1> staticDescriptor;
44   Descriptor &whole{staticDescriptor.descriptor()};
45   SubscriptValue extent[]{numInputBuffers};
46   whole.Establish(TypeCode{CFI_type_char}, maxInputBufferLength, &inputBuffers,
47       1, extent, CFI_attribute_pointer);
48   whole.Check();
49   auto *cookie{IONAME(BeginInternalArrayListInput)(whole)};
50 
51   constexpr int numOutputBuffers{3};
52   constexpr int outputBufferLength{54};
53   char outputBuffers[numOutputBuffers][outputBufferLength]{};
54   for (j = 0; j < numOutputBuffers; ++j) {
55     IONAME(InputAscii)(cookie, outputBuffers[j], outputBufferLength - 1);
56   }
57 
58   const auto status{IONAME(EndIoStatement)(cookie)};
59   ASSERT_EQ(status, 0) << "list-directed input failed, status "
60                        << static_cast<int>(status) << '\n';
61 
62   // Verify results that the _two_ ascii inputs result in _three_ alphabets
63   for (j = 0; j < numOutputBuffers; ++j) {
64     ASSERT_EQ(std::strcmp(outputBuffers[j], expectedOutput), 0)
65         << "wanted outputBuffers[" << j << "]=" << expectedOutput << ", got '"
66         << outputBuffers[j] << "'\n";
67   }
68 }
69 
TEST(InputTest,TestListInputIntegerList)70 TEST(InputTest, TestListInputIntegerList) {
71   constexpr int numBuffers{2};
72   constexpr int maxBufferLength{32};
73   char buffer[numBuffers][maxBufferLength];
74   int j{0};
75   SetCharacter(buffer[j++], maxBufferLength, "1 2 2*3  ,");
76   SetCharacter(buffer[j++], maxBufferLength, ",6,,8,1*");
77 
78   StaticDescriptor<1> staticDescriptor;
79   Descriptor &whole{staticDescriptor.descriptor()};
80   SubscriptValue extent[]{numBuffers};
81   whole.Establish(TypeCode{CFI_type_char}, maxBufferLength, &buffer, 1, extent,
82       CFI_attribute_pointer);
83   whole.Check();
84   auto *cookie{IONAME(BeginInternalArrayListInput)(whole)};
85 
86   constexpr int listInputLength{9};
87 
88   // Negative numbers will be overwritten by _expectedOutput_, and positive
89   // numbers will not be as their indices are "Null values" of the Fortran 2018
90   // standard 13.10.3.2 in the format strings _buffer_
91   std::int64_t actualOutput[listInputLength]{-1, -2, -3, -4, 5, -6, 7, -8, 9};
92   const std::int64_t expectedOutput[listInputLength]{1, 2, 3, 3, 5, 6, 7, 8, 9};
93   for (j = 0; j < listInputLength; ++j) {
94     IONAME(InputInteger)(cookie, actualOutput[j]);
95   }
96 
97   const auto status{IONAME(EndIoStatement)(cookie)};
98   ASSERT_EQ(status, 0) << "list-directed input failed, status "
99                        << static_cast<int>(status) << '\n';
100 
101   // Verify the calls to _InputInteger_ resulted in _expectedOutput_
102   for (j = 0; j < listInputLength; ++j) {
103     ASSERT_EQ(actualOutput[j], expectedOutput[j])
104         << "wanted actualOutput[" << j << "]==" << expectedOutput[j] << ", got "
105         << actualOutput[j] << '\n';
106   }
107 }
108 
TEST(InputTest,TestListInputInvalidFormatWithSingleSuccess)109 TEST(InputTest, TestListInputInvalidFormatWithSingleSuccess) {
110   std::string formatBuffer{"1, g"};
111   constexpr int numBuffers{1};
112 
113   StaticDescriptor<1> staticDescriptor;
114   Descriptor &whole{staticDescriptor.descriptor()};
115   SubscriptValue extent[]{numBuffers};
116   whole.Establish(TypeCode{CFI_type_char}, formatBuffer.size(),
117       formatBuffer.data(), 1, extent, CFI_attribute_pointer);
118   whole.Check();
119 
120   auto *cookie{IONAME(BeginInternalArrayListInput)(whole)};
121   std::int64_t dummy;
122 
123   // Perform _InputInteger_ once successfully
124   IONAME(InputInteger)(cookie, dummy);
125 
126   // Perform failing InputInteger
127   ASSERT_DEATH(IONAME(InputInteger)(cookie, dummy),
128       "Bad character 'g' in INTEGER input field");
129 }
130 
131 // Same test as _TestListInputInvalidFormatWithSingleSuccess_, however no
132 // successful call to _InputInteger_ is performed first.
TEST(InputTest,TestListInputInvalidFormat)133 TEST(InputTest, TestListInputInvalidFormat) {
134   std::string formatBuffer{"g"};
135   constexpr int numBuffers{1};
136 
137   StaticDescriptor<1> staticDescriptor;
138   Descriptor &whole{staticDescriptor.descriptor()};
139   SubscriptValue extent[]{numBuffers};
140   whole.Establish(TypeCode{CFI_type_char}, formatBuffer.size(),
141       formatBuffer.data(), 1, extent, CFI_attribute_pointer);
142   whole.Check();
143 
144   auto *cookie{IONAME(BeginInternalArrayListInput)(whole)};
145   std::int64_t dummy;
146 
147   // Perform failing InputInteger
148   ASSERT_DEATH(IONAME(InputInteger)(cookie, dummy),
149       "Bad character 'g' in INTEGER input field");
150 }
151 
152 using ParamTy = std::tuple<std::string, std::vector<int>>;
153 
154 struct SimpleListInputTest : testing::TestWithParam<ParamTy> {};
155 
TEST_P(SimpleListInputTest,TestListInput)156 TEST_P(SimpleListInputTest, TestListInput) {
157   auto [formatBuffer, expectedOutput] = GetParam();
158   constexpr int numBuffers{1};
159 
160   StaticDescriptor<1> staticDescriptor;
161   Descriptor &whole{staticDescriptor.descriptor()};
162   SubscriptValue extent[]{numBuffers};
163   whole.Establish(TypeCode{CFI_type_char}, formatBuffer.size(),
164       formatBuffer.data(), 1, extent, CFI_attribute_pointer);
165   whole.Check();
166   auto *cookie{IONAME(BeginInternalArrayListInput)(whole)};
167 
168   const auto listInputLength{expectedOutput.size()};
169   std::vector<std::int64_t> actualOutput(listInputLength);
170   for (std::size_t j = 0; j < listInputLength; ++j) {
171     IONAME(InputInteger)(cookie, actualOutput[j]);
172   }
173 
174   const auto status{IONAME(EndIoStatement)(cookie)};
175   ASSERT_EQ(status, 0) << "list-directed input failed, status "
176                        << static_cast<int>(status) << '\n';
177 
178   // Verify the calls to _InputInteger_ resulted in _expectedOutput_
179   for (std::size_t j = 0; j < listInputLength; ++j) {
180     ASSERT_EQ(actualOutput[j], expectedOutput[j])
181         << "wanted actualOutput[" << j << "]==" << expectedOutput[j] << ", got "
182         << actualOutput[j] << '\n';
183   }
184 }
185 
186 INSTANTIATE_TEST_SUITE_P(SimpleListInputTestInstantiation, SimpleListInputTest,
187     testing::Values(std::make_tuple("", std::vector<int>{}),
188         std::make_tuple("0", std::vector<int>{}),
189         std::make_tuple("1", std::vector<int>{1}),
190         std::make_tuple("1, 2", std::vector<int>{1, 2}),
191         std::make_tuple("3*2", std::vector<int>{2, 2, 2})));
192