File: ArrayConstructor.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (159 lines) | stat: -rw-r--r-- 6,501 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===-- flang/unittests/Runtime/ArrayConstructor.cpp-------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "gtest/gtest.h"
#include "tools.h"
#include "flang/Runtime/allocatable.h"
#include "flang/Runtime/array-constructor.h"
#include "flang/Runtime/cpp-type.h"
#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/type-code.h"

#include <memory>

using namespace Fortran::runtime;
using Fortran::common::TypeCategory;

TEST(ArrayConstructor, Basic) {
  // X(4) = [1,2,3,4]
  // Y(2:3,4:6) = RESHAPE([5,6,7,8,9,10], shape=[2,3])
  //
  // Test creation of: [(i, X, Y, i=0,99,1)]
  auto x{MakeArray<TypeCategory::Integer, 4>(
      std::vector<int>{4}, std::vector<std::int32_t>{1, 2, 3, 4})};
  auto y{MakeArray<TypeCategory::Integer, 4>(
      std::vector<int>{2, 3}, std::vector<std::int32_t>{5, 6, 7, 8, 9, 10})};
  y->GetDimension(0).SetBounds(2, 3);
  y->GetDimension(1).SetBounds(4, 6);

  StaticDescriptor<1, false> statDesc;
  Descriptor &result{statDesc.descriptor()};
  result.Establish(TypeCode{CFI_type_int32_t}, 4, /*p=*/nullptr,
      /*rank=*/1, /*extents=*/nullptr, CFI_attribute_allocatable);
  std::allocator<ArrayConstructorVector> cookieAllocator;
  ArrayConstructorVector *acVector{cookieAllocator.allocate(1)};
  ASSERT_TRUE(acVector);

  // Case 1: result is a temp and extent is unknown before first call.
  result.GetDimension(0).SetBounds(1, 0);

  RTNAME(InitArrayConstructorVector)
  (*acVector, result, /*useValueLengthParameters=*/false,
      /*vectorClassSize=*/sizeof(ArrayConstructorVector));
  for (std::int32_t i{0}; i <= 99; ++i) {
    RTNAME(PushArrayConstructorSimpleScalar)(*acVector, &i);
    RTNAME(PushArrayConstructorValue)(*acVector, *x);
    RTNAME(PushArrayConstructorValue)(*acVector, *y);
  }

  ASSERT_TRUE(result.IsAllocated());
  ASSERT_EQ(result.Elements(), static_cast<std::size_t>(100 * (1 + 4 + 2 * 3)));
  SubscriptValue subscript[1]{1};
  for (std::int32_t i{0}; i <= 99; ++i) {
    ASSERT_EQ(*result.Element<std::int32_t>(subscript), i);
    ++subscript[0];
    for (std::int32_t j{1}; j <= 10; ++j) {
      EXPECT_EQ(*result.Element<std::int32_t>(subscript), j);
      ++subscript[0];
    }
  }
  EXPECT_LE(result.Elements(),
      static_cast<std::size_t>(acVector->actualAllocationSize));
  result.Deallocate();
  ASSERT_TRUE(!result.IsAllocated());

  // Case 2: result is an unallocated temp and extent is know before first call.
  // and is allocated when the first value is pushed.
  result.GetDimension(0).SetBounds(1, 1234);
  RTNAME(InitArrayConstructorVector)
  (*acVector, result, /*useValueLengthParameters=*/false,
      /*vectorClassSize=*/sizeof(ArrayConstructorVector));
  EXPECT_EQ(0, acVector->actualAllocationSize);
  std::int32_t i{42};
  RTNAME(PushArrayConstructorSimpleScalar)(*acVector, &i);
  ASSERT_TRUE(result.IsAllocated());
  EXPECT_EQ(1234, acVector->actualAllocationSize);
  result.Deallocate();

  cookieAllocator.deallocate(acVector, 1);
}

TEST(ArrayConstructor, Character) {
  // CHARACTER(2) :: C = "12"
  // X(4) = ["ab", "cd", "ef", "gh"]
  // Y(2:3,4:6) = RESHAPE(["ij", "jl", "mn", "op", "qr","st"], shape=[2,3])
  auto x{MakeArray<TypeCategory::Character, 1>(std::vector<int>{4},
      std::vector<std::string>{"ab", "cd", "ef", "gh"}, 2)};
  auto y{MakeArray<TypeCategory::Character, 1>(std::vector<int>{2, 3},
      std::vector<std::string>{"ij", "kl", "mn", "op", "qr", "st"}, 2)};
  y->GetDimension(0).SetBounds(2, 3);
  y->GetDimension(1).SetBounds(4, 6);
  auto c{MakeArray<TypeCategory::Character, 1>(
      std::vector<int>{}, std::vector<std::string>{"12"}, 2)};

  StaticDescriptor<1, false> statDesc;
  Descriptor &result{statDesc.descriptor()};
  result.Establish(TypeCode{CFI_type_char}, 0, /*p=*/nullptr,
      /*rank=*/1, /*extents=*/nullptr, CFI_attribute_allocatable);
  std::allocator<ArrayConstructorVector> cookieAllocator;
  ArrayConstructorVector *acVector{cookieAllocator.allocate(1)};
  ASSERT_TRUE(acVector);

  // Case 1: result is a temp and extent and length are unknown before the first
  // call. Test creation of: [(C, X, Y, i=1,10,1)]
  static constexpr std::size_t expectedElements{10 * (1 + 4 + 2 * 3)};
  result.GetDimension(0).SetBounds(1, 0);
  RTNAME(InitArrayConstructorVector)
  (*acVector, result, /*useValueLengthParameters=*/true,
      /*vectorClassSize=*/sizeof(ArrayConstructorVector));
  for (std::int32_t i{1}; i <= 10; ++i) {
    RTNAME(PushArrayConstructorValue)(*acVector, *c);
    RTNAME(PushArrayConstructorValue)(*acVector, *x);
    RTNAME(PushArrayConstructorValue)(*acVector, *y);
  }
  ASSERT_TRUE(result.IsAllocated());
  ASSERT_EQ(result.Elements(), expectedElements);
  ASSERT_EQ(result.ElementBytes(), 2u);
  EXPECT_LE(result.Elements(),
      static_cast<std::size_t>(acVector->actualAllocationSize));
  std::string CXY{"12abcdefghijklmnopqrst"};
  std::string expect;
  for (int i{0}; i < 10; ++i)
    expect.append(CXY);
  EXPECT_EQ(std::memcmp(
                result.OffsetElement<char>(0), expect.data(), expect.length()),
      0);
  result.Deallocate();
  cookieAllocator.deallocate(acVector, 1);
}

TEST(ArrayConstructor, CharacterRuntimeCheck) {
  // CHARACTER(2) :: C2
  // CHARACTER(3) :: C3
  // Test the runtime catch bad [C2, C3] array constructors (Fortran 2018 7.8
  // point 2.)
  auto c2{MakeArray<TypeCategory::Character, 1>(
      std::vector<int>{}, std::vector<std::string>{"ab"}, 2)};
  auto c3{MakeArray<TypeCategory::Character, 1>(
      std::vector<int>{}, std::vector<std::string>{"abc"}, 3)};
  StaticDescriptor<1, false> statDesc;
  Descriptor &result{statDesc.descriptor()};
  result.Establish(TypeCode{CFI_type_char}, 0, /*p=*/nullptr,
      /*rank=*/1, /*extents=*/nullptr, CFI_attribute_allocatable);
  std::allocator<ArrayConstructorVector> cookieAllocator;
  ArrayConstructorVector *acVector{cookieAllocator.allocate(1)};
  ASSERT_TRUE(acVector);

  result.GetDimension(0).SetBounds(1, 0);
  RTNAME(InitArrayConstructorVector)
  (*acVector, result, /*useValueLengthParameters=*/true,
      /*vectorClassSize=*/sizeof(ArrayConstructorVector));
  RTNAME(PushArrayConstructorValue)(*acVector, *c2);
  ASSERT_DEATH(RTNAME(PushArrayConstructorValue)(*acVector, *c3),
      "Array constructor: mismatched character lengths");
}