1 | //===-- unittests/Evaluate/reshape.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-rt/runtime/descriptor.h" |
10 | #include "flang/Runtime/transformational.h" |
11 | #include "flang/Testing/testing.h" |
12 | #include <cinttypes> |
13 | |
14 | using namespace Fortran::common; |
15 | using namespace Fortran::runtime; |
16 | |
17 | int main() { |
18 | static const SubscriptValue sourceExtent[]{2, 3, 4}; |
19 | auto source{Descriptor::Create(TypeCategory::Integer, sizeof(std::int32_t), |
20 | nullptr, 3, sourceExtent, CFI_attribute_allocatable)}; |
21 | source->Check(); |
22 | MATCH(3, source->rank()); |
23 | MATCH(sizeof(std::int32_t), source->ElementBytes()); |
24 | TEST(source->IsAllocatable()); |
25 | TEST(!source->IsPointer()); |
26 | for (int j{0}; j < 3; ++j) { |
27 | source->GetDimension(j).SetBounds(1, sourceExtent[j]); |
28 | } |
29 | TEST(source->Allocate(kNoAsyncObject) == CFI_SUCCESS); |
30 | TEST(source->IsAllocated()); |
31 | MATCH(2, source->GetDimension(0).Extent()); |
32 | MATCH(3, source->GetDimension(1).Extent()); |
33 | MATCH(4, source->GetDimension(2).Extent()); |
34 | MATCH(24, source->Elements()); |
35 | for (std::size_t j{0}; j < 24; ++j) { |
36 | *source->OffsetElement<std::int32_t>(j * sizeof(std::int32_t)) = j; |
37 | } |
38 | |
39 | static const std::int16_t shapeData[]{8, 4}; |
40 | static const SubscriptValue shapeExtent{2}; |
41 | auto shape{Descriptor::Create(TypeCategory::Integer, |
42 | static_cast<int>(sizeof shapeData[0]), |
43 | const_cast<void *>(reinterpret_cast<const void *>(shapeData)), 1, |
44 | &shapeExtent, CFI_attribute_pointer)}; |
45 | shape->Check(); |
46 | MATCH(1, shape->rank()); |
47 | MATCH(2, shape->GetDimension(0).Extent()); |
48 | TEST(shape->IsPointer()); |
49 | TEST(!shape->IsAllocatable()); |
50 | |
51 | StaticDescriptor<3> padDescriptor; |
52 | Descriptor &pad{padDescriptor.descriptor()}; |
53 | static const std::int32_t padData[]{24, 25, 26, 27, 28, 29, 30, 31}; |
54 | static const SubscriptValue padExtent[]{2, 2, 3}; |
55 | pad.Establish(TypeCategory::Integer, static_cast<int>(sizeof padData[0]), |
56 | const_cast<void *>(reinterpret_cast<const void *>(padData)), 3, padExtent, |
57 | CFI_attribute_pointer); |
58 | padDescriptor.Check(); |
59 | pad.Check(); |
60 | MATCH(3, pad.rank()); |
61 | MATCH(2, pad.GetDimension(0).Extent()); |
62 | MATCH(2, pad.GetDimension(1).Extent()); |
63 | MATCH(3, pad.GetDimension(2).Extent()); |
64 | StaticDescriptor<1> orderDescriptor; |
65 | Descriptor &order{orderDescriptor.descriptor()}; |
66 | static const std::int32_t orderData[]{1, 2}; |
67 | static const SubscriptValue orderExtent[]{2}; |
68 | order.Establish(TypeCategory::Integer, static_cast<int>(sizeof orderData[0]), |
69 | const_cast<void *>(reinterpret_cast<const void *>(orderData)), 1, |
70 | orderExtent, CFI_attribute_pointer); |
71 | orderDescriptor.Check(); |
72 | order.Check(); |
73 | MATCH(1, order.rank()); |
74 | MATCH(2, order.GetDimension(0).Extent()); |
75 | |
76 | auto result{Descriptor::Create(TypeCategory::Integer, sizeof(std::int32_t), |
77 | nullptr, 2, nullptr, CFI_attribute_allocatable)}; |
78 | TEST(result.get() != nullptr); |
79 | RTNAME(Reshape)(*result, *source, *shape, &pad, &order, __FILE__, __LINE__); |
80 | result->Check(); |
81 | MATCH(sizeof(std::int32_t), result->ElementBytes()); |
82 | MATCH(2, result->rank()); |
83 | TEST(result->type().IsInteger()); |
84 | for (std::int32_t j{0}; j < 32; ++j) { |
85 | MATCH(j, *result->OffsetElement<std::int32_t>(j * sizeof(std::int32_t))); |
86 | } |
87 | for (std::int32_t j{0}; j < 32; ++j) { |
88 | SubscriptValue ss[2]{1 + (j % 8), 1 + (j / 8)}; |
89 | MATCH(j, *result->Element<std::int32_t>(ss)); |
90 | } |
91 | |
92 | return testing::Complete(); |
93 | } |
94 | |