File: Namelist.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (308 lines) | stat: -rw-r--r-- 14,542 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//===-- flang/unittests/Runtime/Namelist.cpp --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "../../runtime/namelist.h"
#include "CrashHandlerFixture.h"
#include "tools.h"
#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/io-api.h"
#include <algorithm>
#include <cinttypes>
#include <complex>
#include <cstring>
#include <gtest/gtest.h>
#include <limits>
#include <string>
#include <vector>

using namespace Fortran::runtime;
using namespace Fortran::runtime::io;

struct NamelistTests : CrashHandlerFixture {};

static void ClearDescriptorStorage(const Descriptor &descriptor) {
  std::memset(descriptor.raw().base_addr, 0,
      descriptor.Elements() * descriptor.ElementBytes());
}

TEST(NamelistTests, BasicSanity) {
  static constexpr int numLines{12};
  static constexpr int lineLength{32};
  static char buffer[numLines][lineLength];
  StaticDescriptor<1, true> statDescs[1];
  Descriptor &internalDesc{statDescs[0].descriptor()};
  SubscriptValue extent[]{numLines};
  internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/lineLength,
      &buffer, 1, extent, CFI_attribute_pointer);
  // Set up data arrays
  std::vector<int> ints;
  for (int j{0}; j < 20; ++j) {
    ints.push_back(j % 2 == 0 ? (1 << j) : -(1 << j));
  }
  std::vector<double> reals{0.0, -0.0, std::numeric_limits<double>::infinity(),
      -std::numeric_limits<double>::infinity(),
      std::numeric_limits<double>::quiet_NaN(),
      std::numeric_limits<double>::max(), std::numeric_limits<double>::lowest(),
      std::numeric_limits<double>::epsilon()};
  std::vector<std::uint8_t> logicals;
  logicals.push_back(false);
  logicals.push_back(true);
  logicals.push_back(false);
  std::vector<std::complex<float>> complexes;
  complexes.push_back(std::complex<float>{123.0, -0.5});
  std::vector<std::string> characters;
  characters.emplace_back("aBcDeFgHiJkLmNoPqRsTuVwXyZ");
  characters.emplace_back("0123456789'\"..............");
  // Copy the data into new descriptors
  OwningPtr<Descriptor> intDesc{
      MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
          std::vector<int>{5, 4}, std::move(ints))};
  OwningPtr<Descriptor> realDesc{
      MakeArray<TypeCategory::Real, static_cast<int>(sizeof(double))>(
          std::vector<int>{4, 2}, std::move(reals))};
  OwningPtr<Descriptor> logicalDesc{
      MakeArray<TypeCategory::Logical, static_cast<int>(sizeof(std::uint8_t))>(
          std::vector<int>{3}, std::move(logicals))};
  OwningPtr<Descriptor> complexDesc{
      MakeArray<TypeCategory::Complex, static_cast<int>(sizeof(float))>(
          std::vector<int>{}, std::move(complexes))};
  OwningPtr<Descriptor> characterDesc{MakeArray<TypeCategory::Character, 1>(
      std::vector<int>{2}, std::move(characters), characters[0].size())};
  // Create a NAMELIST group
  static constexpr int items{5};
  const NamelistGroup::Item itemArray[items]{{"ints", *intDesc},
      {"reals", *realDesc}, {"logicals", *logicalDesc},
      {"complexes", *complexDesc}, {"characters", *characterDesc}};
  const NamelistGroup group{"group1", items, itemArray};
  // Do an internal NAMELIST write and check results
  auto outCookie1{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(SetDelim)(outCookie1, "APOSTROPHE", 10));
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie1, group));
  auto outStatus1{IONAME(EndIoStatement)(outCookie1)};
  ASSERT_EQ(outStatus1, 0) << "Failed namelist output sanity, status "
                           << static_cast<int>(outStatus1);

  static const std::string expect{"&GROUP1 INTS= 1 -2 4 -8 16 -32  "
                                  " 64 -128 256 -512 1024 -2048    "
                                  " 4096 -8192 16384 -32768 65536  "
                                  " -131072 262144 -524288,REALS=  "
                                  " 0. -0. Inf -Inf NaN            "
                                  " 1.7976931348623157E+308        "
                                  " -1.7976931348623157E+308       "
                                  " 2.220446049250313E-16,LOGICALS="
                                  "F T F,COMPLEXES= (123.,-.5),    "
                                  " CHARACTERS= 'aBcDeFgHiJkLmNoPqR"
                                  "sTuVwXyZ' '0123456789''\"........"
                                  "......'/                        "};
  std::string got{buffer[0], sizeof buffer};
  EXPECT_EQ(got, expect);

  // Clear the arrays, read them back, write out again, and compare
  ClearDescriptorStorage(*intDesc);
  ClearDescriptorStorage(*realDesc);
  ClearDescriptorStorage(*logicalDesc);
  ClearDescriptorStorage(*complexDesc);
  ClearDescriptorStorage(*characterDesc);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  auto inStatus{IONAME(EndIoStatement)(inCookie)};
  ASSERT_EQ(inStatus, 0) << "Failed namelist input sanity, status "
                         << static_cast<int>(inStatus);
  auto outCookie2{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(SetDelim)(outCookie2, "APOSTROPHE", 10));
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie2, group));
  auto outStatus2{IONAME(EndIoStatement)(outCookie2)};
  ASSERT_EQ(outStatus2, 0) << "Failed namelist output sanity rewrite, status "
                           << static_cast<int>(outStatus2);
  std::string got2{buffer[0], sizeof buffer};
  EXPECT_EQ(got2, expect);
}

TEST(NamelistTests, Subscripts) {
  // INTEGER :: A(-1:0, -1:1)
  OwningPtr<Descriptor> aDesc{
      MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
          std::vector<int>{2, 3}, std::vector<int>(6, 0))};
  aDesc->GetDimension(0).SetBounds(-1, 0);
  aDesc->GetDimension(1).SetBounds(-1, 1);
  const NamelistGroup::Item items[]{{"a", *aDesc}};
  const NamelistGroup group{"justa", 1, items};
  static char t1[]{"&justa A(0,+1:-1:-2)=1 2/"};
  StaticDescriptor<1, true> statDesc;
  Descriptor &internalDesc{statDesc.descriptor()};
  internalDesc.Establish(TypeCode{CFI_type_char},
      /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  auto inStatus{IONAME(EndIoStatement)(inCookie)};
  ASSERT_EQ(inStatus, 0) << "Failed namelist input subscripts, status "
                         << static_cast<int>(inStatus);
  char out[40];
  internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
      out, 0, nullptr, CFI_attribute_pointer);
  auto outCookie{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
  auto outStatus{IONAME(EndIoStatement)(outCookie)};
  ASSERT_EQ(outStatus, 0)
      << "Failed namelist output subscripts rewrite, status "
      << static_cast<int>(outStatus);
  std::string got{out, sizeof out};
  static const std::string expect{"&JUSTA A= 0 2 0 0 0 1/                  "};
  EXPECT_EQ(got, expect);
}

TEST(NamelistTests, ShortArrayInput) {
  OwningPtr<Descriptor> aDesc{
      MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
          std::vector<int>{2}, std::vector<int>(2, -1))};
  OwningPtr<Descriptor> bDesc{
      MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
          std::vector<int>{2}, std::vector<int>(2, -2))};
  const NamelistGroup::Item items[]{{"a", *aDesc}, {"b", *bDesc}};
  const NamelistGroup group{"nl", 2, items};
  // Two 12-character lines of internal input
  static char t1[]{"&nl a = 1 b "
                   " = 2 /      "};
  StaticDescriptor<1, true> statDesc;
  Descriptor &internalDesc{statDesc.descriptor()};
  SubscriptValue shape{2};
  internalDesc.Establish(1, 12, t1, 1, &shape, CFI_attribute_pointer);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  auto inStatus{IONAME(EndIoStatement)(inCookie)};
  ASSERT_EQ(inStatus, 0) << "Failed namelist input subscripts, status "
                         << static_cast<int>(inStatus);
  EXPECT_EQ(*aDesc->ZeroBasedIndexedElement<int>(0), 1);
  EXPECT_EQ(*aDesc->ZeroBasedIndexedElement<int>(1), -1);
  EXPECT_EQ(*bDesc->ZeroBasedIndexedElement<int>(0), 2);
  EXPECT_EQ(*bDesc->ZeroBasedIndexedElement<int>(1), -2);
}

TEST(NamelistTests, ScalarSubstring) {
  OwningPtr<Descriptor> scDesc{MakeArray<TypeCategory::Character, 1>(
      std::vector<int>{}, std::vector<std::string>{"abcdefgh"}, 8)};
  const NamelistGroup::Item items[]{{"a", *scDesc}};
  const NamelistGroup group{"justa", 1, items};
  static char t1[]{"&justa A(2:5)='BCDE'/"};
  StaticDescriptor<1, true> statDesc;
  Descriptor &internalDesc{statDesc.descriptor()};
  internalDesc.Establish(TypeCode{CFI_type_char},
      /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
      << "namelist scalar substring input";
  char out[32];
  internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
      out, 0, nullptr, CFI_attribute_pointer);
  auto outCookie{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(SetDelim)(outCookie, "apostrophe", 10));
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
  std::string got{out, sizeof out};
  static const std::string expect{"&JUSTA A= 'aBCDEfgh'/           "};
  EXPECT_EQ(got, expect);
}

TEST(NamelistTests, ArraySubstring) {
  OwningPtr<Descriptor> scDesc{
      MakeArray<TypeCategory::Character, 1>(std::vector<int>{2},
          std::vector<std::string>{"abcdefgh", "ijklmnop"}, 8)};
  const NamelistGroup::Item items[]{{"a", *scDesc}};
  const NamelistGroup group{"justa", 1, items};
  static char t1[]{"&justa A(:)(2:+5)='BCDE' 'JKLM'/"};
  StaticDescriptor<1, true> statDesc;
  Descriptor &internalDesc{statDesc.descriptor()};
  internalDesc.Establish(TypeCode{CFI_type_char},
      /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
      << "namelist scalar substring input";
  char out[40];
  internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
      out, 0, nullptr, CFI_attribute_pointer);
  auto outCookie{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(SetDelim)(outCookie, "apostrophe", 10));
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
  std::string got{out, sizeof out};
  static const std::string expect{"&JUSTA A= 'aBCDEfgh' 'iJKLMnop'/        "};
  EXPECT_EQ(got, expect);
}

TEST(NamelistTests, Skip) {
  OwningPtr<Descriptor> scDesc{
      MakeArray<TypeCategory::Integer, static_cast<int>(sizeof(int))>(
          std::vector<int>{}, std::vector<int>{-1})};
  const NamelistGroup::Item items[]{{"j", *scDesc}};
  const NamelistGroup group{"nml", 1, items};
  static char t1[]{"&skip a='str''ing'/&nml j=123/"};
  StaticDescriptor<1, true> statDesc;
  Descriptor &internalDesc{statDesc.descriptor()};
  internalDesc.Establish(TypeCode{CFI_type_char},
      /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
      << "namelist input with skipping";
  char out[20];
  internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
      out, 0, nullptr, CFI_attribute_pointer);
  auto outCookie{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
  std::string got{out, sizeof out};
  static const std::string expect{"&NML J= 123/        "};
  EXPECT_EQ(got, expect);
}

// Tests DECIMAL=COMMA mode
TEST(NamelistTests, Comma) {
  OwningPtr<Descriptor> scDesc{
      MakeArray<TypeCategory::Complex, static_cast<int>(sizeof(float))>(
          std::vector<int>{2}, std::vector<std::complex<float>>{{}, {}})};
  const NamelistGroup::Item items[]{{"z", *scDesc}};
  const NamelistGroup group{"nml", 1, items};
  static char t1[]{"&nml z=(-1,0;2,0);(-3,0;0,5)/"};
  StaticDescriptor<1, true> statDesc;
  Descriptor &internalDesc{statDesc.descriptor()};
  internalDesc.Establish(TypeCode{CFI_type_char},
      /*elementBytes=*/std::strlen(t1), t1, 0, nullptr, CFI_attribute_pointer);
  auto inCookie{IONAME(BeginInternalArrayListInput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(SetDecimal)(inCookie, "COMMA", 5));
  ASSERT_TRUE(IONAME(InputNamelist)(inCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(inCookie), IostatOk)
      << "namelist input with skipping";
  char out[30];
  internalDesc.Establish(TypeCode{CFI_type_char}, /*elementBytes=*/sizeof out,
      out, 0, nullptr, CFI_attribute_pointer);
  auto outCookie{IONAME(BeginInternalArrayListOutput)(
      internalDesc, nullptr, 0, __FILE__, __LINE__)};
  ASSERT_TRUE(IONAME(SetDecimal)(outCookie, "COMMA", 5));
  ASSERT_TRUE(IONAME(OutputNamelist)(outCookie, group));
  ASSERT_EQ(IONAME(EndIoStatement)(outCookie), IostatOk) << "namelist output";
  std::string got{out, sizeof out};
  static const std::string expect{"&NML Z= (-1,;2,) (-3,;,5)/    "};
  EXPECT_EQ(got, expect);
}

// TODO: Internal NAMELIST error tests