| 1 | //===-- unittests/Runtime/Inquiry.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 "flang/Runtime/inquiry.h" |
| 10 | #include "tools.h" |
| 11 | #include "gtest/gtest.h" |
| 12 | #include "flang-rt/runtime/type-code.h" |
| 13 | |
| 14 | using namespace Fortran::runtime; |
| 15 | using Fortran::common::TypeCategory; |
| 16 | |
| 17 | TEST(Inquiry, LboundDim) { |
| 18 | // ARRAY 1 3 5 |
| 19 | // 2 4 6 |
| 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); |
| 24 | |
| 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}); |
| 27 | } |
| 28 | |
| 29 | TEST(Inquiry, Lbound) { |
| 30 | // ARRAY 1 3 5 |
| 31 | // 2 4 6 |
| 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); |
| 36 | |
| 37 | // LBOUND(ARRAY, KIND=1) |
| 38 | auto int8Result{ |
| 39 | MakeArray<TypeCategory::Integer, 1>(std::vector<int>{array->rank()}, |
| 40 | std::vector<std::int8_t>(array->rank(), 0))}; |
| 41 | RTNAME(Lbound) |
| 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); |
| 45 | |
| 46 | // LBOUND(ARRAY, KIND=4) |
| 47 | auto int32Result{ |
| 48 | MakeArray<TypeCategory::Integer, 4>(std::vector<int>{array->rank()}, |
| 49 | std::vector<std::int32_t>(array->rank(), 0))}; |
| 50 | RTNAME(Lbound) |
| 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); |
| 54 | |
| 55 | // LBOUND(ARRAY, KIND=8) |
| 56 | auto int64Result{ |
| 57 | MakeArray<TypeCategory::Integer, 8>(std::vector<int>{array->rank()}, |
| 58 | std::vector<std::int64_t>(array->rank(), 0))}; |
| 59 | RTNAME(Lbound) |
| 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); |
| 63 | } |
| 64 | |
| 65 | TEST(Inquiry, Ubound) { |
| 66 | // ARRAY 1 3 5 |
| 67 | // 2 4 6 |
| 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); |
| 72 | |
| 73 | // UBOUND(ARRAY, KIND=1) |
| 74 | auto int8Result{ |
| 75 | MakeArray<TypeCategory::Integer, 1>(std::vector<int>{array->rank()}, |
| 76 | std::vector<std::int8_t>(array->rank(), 0))}; |
| 77 | RTNAME(Ubound) |
| 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); |
| 81 | |
| 82 | // UBOUND(ARRAY, KIND=4) |
| 83 | auto int32Result{ |
| 84 | MakeArray<TypeCategory::Integer, 4>(std::vector<int>{array->rank()}, |
| 85 | std::vector<std::int32_t>(array->rank(), 0))}; |
| 86 | RTNAME(Ubound) |
| 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); |
| 90 | |
| 91 | // UBOUND(ARRAY, KIND=8) |
| 92 | auto int64Result{ |
| 93 | MakeArray<TypeCategory::Integer, 8>(std::vector<int>{array->rank()}, |
| 94 | std::vector<std::int64_t>(array->rank(), 0))}; |
| 95 | RTNAME(Ubound) |
| 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); |
| 99 | } |
| 100 | |
| 101 | TEST(Inquiry, Size) { |
| 102 | // ARRAY 1 3 5 |
| 103 | // 2 4 6 |
| 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); |
| 108 | |
| 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}); |
| 112 | } |
| 113 | |
| 114 | TEST(Inquiry, Shape) { |
| 115 | // ARRAY 1 3 5 |
| 116 | // 2 4 6 |
| 117 | auto array{MakeArray<TypeCategory::Integer, 4>( |
| 118 | std::vector<int>{2, 3}, std::vector<std::int32_t>{1, 2, 3, 4, 5, 6})}; |
| 119 | |
| 120 | // SHAPE(ARRAY, KIND=1) |
| 121 | auto int8Result{ |
| 122 | MakeArray<TypeCategory::Integer, 1>(std::vector<int>{array->rank()}, |
| 123 | std::vector<std::int8_t>(array->rank(), 0))}; |
| 124 | RTNAME(Shape) |
| 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); |
| 128 | |
| 129 | // SHAPE(ARRAY, KIND=4) |
| 130 | auto int32Result{ |
| 131 | MakeArray<TypeCategory::Integer, 4>(std::vector<int>{array->rank()}, |
| 132 | std::vector<std::int32_t>(array->rank(), 0))}; |
| 133 | RTNAME(Shape) |
| 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); |
| 137 | |
| 138 | // SHAPE(ARRAY, KIND=8) |
| 139 | auto int64Result{ |
| 140 | MakeArray<TypeCategory::Integer, 8>(std::vector<int>{array->rank()}, |
| 141 | std::vector<std::int64_t>(array->rank(), 0))}; |
| 142 | RTNAME(Shape) |
| 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); |
| 146 | } |
| 147 | |