107 lines · cpp
1//===-- unittests/Runtime/InputExtensions.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "CrashHandlerFixture.h"10#include "flang-rt/runtime/descriptor.h"11#include "flang/Runtime/io-api.h"12#include <algorithm>13#include <array>14#include <cstring>15#include <gtest/gtest.h>16#include <tuple>17 18using namespace Fortran::runtime;19using namespace Fortran::runtime::io;20 21struct InputExtensionTests : CrashHandlerFixture {};22 23TEST(InputExtensionTests, SeparatorInField_F) {24 static const struct {25 int get;26 const char *format, *data;27 double expect[3];28 } test[] = {29 {2, "(2F6)", "1.25,3.75,", {1.25, 3.75}},30 {2, "(2F6)", "1.25 ,3.75 ,", {1.25, 3.75}},31 {2, "(DC,2F6)", "1,25;3,75;", {1.25, 3.75}},32 {2, "(DC,2F6)", "1,25 ;3,75 ;", {1.25, 3.75}},33 };34 for (std::size_t j{0}; j < sizeof test / sizeof *test; ++j) {35 auto cookie{IONAME(BeginInternalFormattedInput)(test[j].data,36 std::strlen(test[j].data), test[j].format,37 std::strlen(test[j].format))};38 for (int k{0}; k < test[j].get; ++k) {39 float got;40 IONAME(InputReal32)(cookie, got);41 ASSERT_EQ(got, test[j].expect[k])42 << "expected " << test[j].expect[k] << ", got " << got;43 }44 auto status{IONAME(EndIoStatement)(cookie)};45 ASSERT_EQ(status, 0) << "error status " << status << " on F test case "46 << j;47 }48}49 50TEST(InputExtensionTests, SeparatorInField_I) {51 static const struct {52 int get;53 const char *format, *data;54 std::int64_t expect[3];55 } test[] = {56 {2, "(2I4)", "12,34,", {12, 34}},57 {2, "(2I4)", "12 ,34 ,", {12, 34}},58 {2, "(DC,2I4)", "12;34;", {12, 34}},59 {2, "(DC,2I4)", "12 ;34 ;", {12, 34}},60 };61 for (std::size_t j{0}; j < sizeof test / sizeof *test; ++j) {62 auto cookie{IONAME(BeginInternalFormattedInput)(test[j].data,63 std::strlen(test[j].data), test[j].format,64 std::strlen(test[j].format))};65 for (int k{0}; k < test[j].get; ++k) {66 std::int64_t got;67 IONAME(InputInteger)(cookie, got);68 ASSERT_EQ(got, test[j].expect[k])69 << "expected " << test[j].expect[k] << ", got " << got;70 }71 auto status{IONAME(EndIoStatement)(cookie)};72 ASSERT_EQ(status, 0) << "error status " << status << " on I test case "73 << j;74 }75}76 77TEST(InputExtensionTests, SeparatorInField_L) {78 static const struct {79 int get;80 const char *format, *data;81 bool expect[3];82 } test[] = {83 {2, "(2L4)", ".T,F,", {true, false}},84 {2, "(2L4)", ".F,T,", {false, true}},85 {2, "(2L4)", ".T.,F,", {true, false}},86 {2, "(2L4)", ".F.,T,", {false, true}},87 {2, "(DC,2L4)", ".T;F,", {true, false}},88 {2, "(DC,2L4)", ".F;T,", {false, true}},89 {2, "(DC,2L4)", ".T.;F,", {true, false}},90 {2, "(DC,2L4)", ".F.;T,", {false, true}},91 };92 for (std::size_t j{0}; j < sizeof test / sizeof *test; ++j) {93 auto cookie{IONAME(BeginInternalFormattedInput)(test[j].data,94 std::strlen(test[j].data), test[j].format,95 std::strlen(test[j].format))};96 for (int k{0}; k < test[j].get; ++k) {97 bool got;98 IONAME(InputLogical)(cookie, got);99 ASSERT_EQ(got, test[j].expect[k])100 << "expected " << test[j].expect[k] << ", got " << got;101 }102 auto status{IONAME(EndIoStatement)(cookie)};103 ASSERT_EQ(status, 0) << "error status " << status << " on L test case "104 << j;105 }106}107