xref: /llvm-project/flang/unittests/Runtime/LogicalFormatTest.cpp (revision eb77f442b342a1bb234254e05759933bad9dfab1)
1 //===-- flang/unittests/Runtime/LogicalFormatTest.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 "flang/Runtime/descriptor.h"
11 #include "flang/Runtime/io-api-consts.h"
12 #include <algorithm>
13 #include <array>
14 #include <cstring>
15 #include <gtest/gtest.h>
16 #include <tuple>
17 
18 using namespace Fortran::runtime;
19 using namespace Fortran::runtime::io;
20 
21 TEST(IOApiTests, LogicalFormatTest) {
22   static constexpr int bufferSize{29};
23   char buffer[bufferSize];
24 
25   // Create format for all types and values to be written
26   const char *format{"(L0,L3,I3,L2,L2,I3,L2,A3,L2,L,F4.1,L2)"};
27   auto cookie{IONAME(BeginInternalFormattedOutput)(
28       buffer, bufferSize, format, std::strlen(format))};
29 
30   // Write string, integer, and logical values to buffer
31   IONAME(OutputLogical)(cookie, true);
32   IONAME(OutputLogical)(cookie, false);
33   IONAME(OutputInteger64)(cookie, 6);
34   IONAME(OutputInteger32)(cookie, 22);
35   IONAME(OutputInteger32)(cookie, 0);
36   IONAME(OutputInteger32)(cookie, -2);
37   IONAME(OutputCharacter)(cookie, "ABC", 3);
38   IONAME(OutputCharacter)(cookie, "AB", 2);
39   IONAME(OutputReal64)(cookie, 0.0);
40   IONAME(OutputCharacter)(cookie, "", 0);
41   IONAME(OutputReal32)(cookie, 2.3);
42   IONAME(OutputReal32)(cookie, 2.3);
43 
44   // Ensure IO succeeded
45   auto status{IONAME(EndIoStatement)(cookie)};
46   ASSERT_EQ(status, 0) << "logical format: '" << format << "' failed, status "
47                        << static_cast<int>(status);
48 
49   // Ensure final buffer matches expected string output
50   static const std::string expect{"T  F  6 T F -2 T AB FF 2.3 T"};
51 
52   // expect.size() == bufferSize - 1
53   std::string bufferStr = std::string(buffer, bufferSize - 1);
54   ASSERT_TRUE(expect == bufferStr)
55       << "Expected '" << expect << "', got '" << bufferStr << "'";
56 }
57