1 //===-- flang/unittests/Runtime/Inquiry.cpp -------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "flang/Runtime/inquiry.h"
10 #include "gtest/gtest.h"
12 #include "flang/Runtime/type-code.h"
14 using namespace Fortran::runtime
;
15 using Fortran::common::TypeCategory
;
17 TEST(Inquiry
, LboundDim
) {
20 auto array
{MakeArray
<TypeCategory::Integer
, 4>(
21 std::vector
<int>{2, 3}, std::vector
<std::int32_t>{1, 2, 3, 4, 5, 6})};
22 array
->GetDimension(0).SetLowerBound(0);
23 array
->GetDimension(1).SetLowerBound(-1);
25 EXPECT_EQ(RTNAME(LboundDim
)(*array
, 1, __FILE__
, __LINE__
), std::int64_t{0});
26 EXPECT_EQ(RTNAME(LboundDim
)(*array
, 2, __FILE__
, __LINE__
), std::int64_t{-1});
29 TEST(Inquiry
, Lbound
) {
32 auto array
{MakeArray
<TypeCategory::Integer
, 4>(
33 std::vector
<int>{2, 3}, std::vector
<std::int32_t>{1, 2, 3, 4, 5, 6})};
34 array
->GetDimension(0).SetLowerBound(0);
35 array
->GetDimension(1).SetLowerBound(-1);
37 // LBOUND(ARRAY, KIND=1)
39 MakeArray
<TypeCategory::Integer
, 1>(std::vector
<int>{array
->rank()},
40 std::vector
<std::int8_t>(array
->rank(), 0))};
42 (int8Result
->raw().base_addr
, *array
, /*KIND=*/1, __FILE__
, __LINE__
);
43 EXPECT_EQ(*int8Result
->ZeroBasedIndexedElement
<std::int8_t>(0), 0);
44 EXPECT_EQ(*int8Result
->ZeroBasedIndexedElement
<std::int8_t>(1), -1);
46 // LBOUND(ARRAY, KIND=4)
48 MakeArray
<TypeCategory::Integer
, 4>(std::vector
<int>{array
->rank()},
49 std::vector
<std::int32_t>(array
->rank(), 0))};
51 (int32Result
->raw().base_addr
, *array
, /*KIND=*/4, __FILE__
, __LINE__
);
52 EXPECT_EQ(*int32Result
->ZeroBasedIndexedElement
<std::int32_t>(0), 0);
53 EXPECT_EQ(*int32Result
->ZeroBasedIndexedElement
<std::int32_t>(1), -1);
55 // LBOUND(ARRAY, KIND=8)
57 MakeArray
<TypeCategory::Integer
, 8>(std::vector
<int>{array
->rank()},
58 std::vector
<std::int64_t>(array
->rank(), 0))};
60 (int64Result
->raw().base_addr
, *array
, /*KIND=*/8, __FILE__
, __LINE__
);
61 EXPECT_EQ(*int64Result
->ZeroBasedIndexedElement
<std::int64_t>(0), 0);
62 EXPECT_EQ(*int64Result
->ZeroBasedIndexedElement
<std::int64_t>(1), -1);
65 TEST(Inquiry
, Ubound
) {
68 auto array
{MakeArray
<TypeCategory::Integer
, 4>(
69 std::vector
<int>{2, 3}, std::vector
<std::int32_t>{1, 2, 3, 4, 5, 6})};
70 array
->GetDimension(0).SetLowerBound(1000);
71 array
->GetDimension(1).SetLowerBound(1);
73 // UBOUND(ARRAY, KIND=1)
75 MakeArray
<TypeCategory::Integer
, 1>(std::vector
<int>{array
->rank()},
76 std::vector
<std::int8_t>(array
->rank(), 0))};
78 (int8Result
->raw().base_addr
, *array
, /*KIND=*/1, __FILE__
, __LINE__
);
79 EXPECT_EQ(*int8Result
->ZeroBasedIndexedElement
<std::int8_t>(0), -23);
80 EXPECT_EQ(*int8Result
->ZeroBasedIndexedElement
<std::int8_t>(1), 3);
82 // UBOUND(ARRAY, KIND=4)
84 MakeArray
<TypeCategory::Integer
, 4>(std::vector
<int>{array
->rank()},
85 std::vector
<std::int32_t>(array
->rank(), 0))};
87 (int32Result
->raw().base_addr
, *array
, /*KIND=*/4, __FILE__
, __LINE__
);
88 EXPECT_EQ(*int32Result
->ZeroBasedIndexedElement
<std::int32_t>(0), 1001);
89 EXPECT_EQ(*int32Result
->ZeroBasedIndexedElement
<std::int32_t>(1), 3);
91 // UBOUND(ARRAY, KIND=8)
93 MakeArray
<TypeCategory::Integer
, 8>(std::vector
<int>{array
->rank()},
94 std::vector
<std::int64_t>(array
->rank(), 0))};
96 (int64Result
->raw().base_addr
, *array
, /*KIND=*/8, __FILE__
, __LINE__
);
97 EXPECT_EQ(*int64Result
->ZeroBasedIndexedElement
<std::int64_t>(0), 1001);
98 EXPECT_EQ(*int64Result
->ZeroBasedIndexedElement
<std::int64_t>(1), 3);
101 TEST(Inquiry
, Size
) {
104 auto array
{MakeArray
<TypeCategory::Integer
, 4>(
105 std::vector
<int>{2, 3}, std::vector
<std::int32_t>{1, 2, 3, 4, 5, 6})};
106 array
->GetDimension(0).SetLowerBound(0); // shouldn't matter
107 array
->GetDimension(1).SetLowerBound(-1);
109 EXPECT_EQ(RTNAME(SizeDim
)(*array
, 1, __FILE__
, __LINE__
), std::int64_t{2});
110 EXPECT_EQ(RTNAME(SizeDim
)(*array
, 2, __FILE__
, __LINE__
), std::int64_t{3});
111 EXPECT_EQ(RTNAME(Size
)(*array
, __FILE__
, __LINE__
), std::int64_t{6});
114 TEST(Inquiry
, Shape
) {
117 auto array
{MakeArray
<TypeCategory::Integer
, 4>(
118 std::vector
<int>{2, 3}, std::vector
<std::int32_t>{1, 2, 3, 4, 5, 6})};
120 // SHAPE(ARRAY, KIND=1)
122 MakeArray
<TypeCategory::Integer
, 1>(std::vector
<int>{array
->rank()},
123 std::vector
<std::int8_t>(array
->rank(), 0))};
125 (int8Result
->raw().base_addr
, *array
, /*KIND=*/1, __FILE__
, __LINE__
);
126 EXPECT_EQ(*int8Result
->ZeroBasedIndexedElement
<std::int8_t>(0), 2);
127 EXPECT_EQ(*int8Result
->ZeroBasedIndexedElement
<std::int8_t>(1), 3);
129 // SHAPE(ARRAY, KIND=4)
131 MakeArray
<TypeCategory::Integer
, 4>(std::vector
<int>{array
->rank()},
132 std::vector
<std::int32_t>(array
->rank(), 0))};
134 (int32Result
->raw().base_addr
, *array
, /*KIND=*/4, __FILE__
, __LINE__
);
135 EXPECT_EQ(*int32Result
->ZeroBasedIndexedElement
<std::int32_t>(0), 2);
136 EXPECT_EQ(*int32Result
->ZeroBasedIndexedElement
<std::int32_t>(1), 3);
138 // SHAPE(ARRAY, KIND=8)
140 MakeArray
<TypeCategory::Integer
, 8>(std::vector
<int>{array
->rank()},
141 std::vector
<std::int64_t>(array
->rank(), 0))};
143 (int64Result
->raw().base_addr
, *array
, /*KIND=*/8, __FILE__
, __LINE__
);
144 EXPECT_EQ(*int64Result
->ZeroBasedIndexedElement
<std::int64_t>(0), 2);
145 EXPECT_EQ(*int64Result
->ZeroBasedIndexedElement
<std::int64_t>(1), 3);