File: Allocatable.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (134 lines) | stat: -rw-r--r-- 4,773 bytes parent folder | download | duplicates (9)
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
//===-- flang/unittests/Runtime/Allocatable.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 "flang/Runtime/allocatable.h"
#include "gtest/gtest.h"
#include "tools.h"

using namespace Fortran::runtime;

static OwningPtr<Descriptor> createAllocatable(
    Fortran::common::TypeCategory tc, int kind, int rank = 1) {
  return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr,
      CFI_attribute_allocatable);
}

TEST(AllocatableTest, MoveAlloc) {
  using Fortran::common::TypeCategory;
  // INTEGER(4), ALLOCATABLE :: a(:)
  auto a{createAllocatable(TypeCategory::Integer, 4)};
  // INTEGER(4), ALLOCATABLE :: b(:)
  auto b{createAllocatable(TypeCategory::Integer, 4)};
  // ALLOCATE(a(20))
  a->GetDimension(0).SetBounds(1, 20);
  a->Allocate();

  EXPECT_TRUE(a->IsAllocated());
  EXPECT_FALSE(b->IsAllocated());

  // Simple move_alloc
  RTNAME(MoveAlloc)(*b, *a, nullptr, false, nullptr, __FILE__, __LINE__);
  EXPECT_FALSE(a->IsAllocated());
  EXPECT_TRUE(b->IsAllocated());

  // move_alloc with stat
  std::int32_t stat{
      RTNAME(MoveAlloc)(*a, *b, nullptr, true, nullptr, __FILE__, __LINE__)};
  EXPECT_TRUE(a->IsAllocated());
  EXPECT_FALSE(b->IsAllocated());
  EXPECT_EQ(stat, 0);

  // move_alloc with errMsg
  auto errMsg{Descriptor::Create(
      sizeof(char), 64, nullptr, 0, nullptr, CFI_attribute_allocatable)};
  errMsg->Allocate();
  RTNAME(MoveAlloc)(*b, *a, nullptr, false, errMsg.get(), __FILE__, __LINE__);
  EXPECT_FALSE(a->IsAllocated());
  EXPECT_TRUE(b->IsAllocated());

  // move_alloc with stat and errMsg
  stat = RTNAME(MoveAlloc)(
      *a, *b, nullptr, true, errMsg.get(), __FILE__, __LINE__);
  EXPECT_TRUE(a->IsAllocated());
  EXPECT_FALSE(b->IsAllocated());
  EXPECT_EQ(stat, 0);

  // move_alloc with the same deallocated array
  stat = RTNAME(MoveAlloc)(
      *b, *b, nullptr, true, errMsg.get(), __FILE__, __LINE__);
  EXPECT_FALSE(b->IsAllocated());
  EXPECT_EQ(stat, 0);

  // move_alloc with the same allocated array should fail
  stat = RTNAME(MoveAlloc)(
      *a, *a, nullptr, true, errMsg.get(), __FILE__, __LINE__);
  EXPECT_EQ(stat, 109);
  std::string_view errStr{errMsg->OffsetElement(), errMsg->ElementBytes()};
  auto trim_pos = errStr.find_last_not_of(' ');
  if (trim_pos != errStr.npos)
    errStr.remove_suffix(errStr.size() - trim_pos - 1);
  EXPECT_EQ(errStr, "MOVE_ALLOC passed the same address as to and from");
}

TEST(AllocatableTest, AllocateFromScalarSource) {
  using Fortran::common::TypeCategory;
  // REAL(4), ALLOCATABLE :: a(:)
  auto a{createAllocatable(TypeCategory::Real, 4)};
  // ALLOCATE(a(2:11), SOURCE=3.4)
  float sourecStorage{3.4F};
  auto s{Descriptor::Create(TypeCategory::Real, 4,
      reinterpret_cast<void *>(&sourecStorage), 0, nullptr,
      CFI_attribute_pointer)};
  RTNAME(AllocatableSetBounds)(*a, 0, 2, 11);
  RTNAME(AllocatableAllocateSource)
  (*a, *s, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
  EXPECT_TRUE(a->IsAllocated());
  EXPECT_EQ(a->Elements(), 10u);
  EXPECT_EQ(a->GetDimension(0).LowerBound(), 2);
  EXPECT_EQ(a->GetDimension(0).UpperBound(), 11);
  EXPECT_EQ(*a->OffsetElement<float>(), 3.4F);
  a->Destroy();
}

TEST(AllocatableTest, AllocateSourceZeroSize) {
  using Fortran::common::TypeCategory;
  // REAL(4), ALLOCATABLE :: a(:)
  auto a{createAllocatable(TypeCategory::Real, 4)};
  // REAL(4) :: s(-1:-2) = 0.
  float sourecStorage{0.F};
  const SubscriptValue extents[1]{0};
  auto s{Descriptor::Create(TypeCategory::Real, 4,
      reinterpret_cast<void *>(&sourecStorage), 1, extents,
      CFI_attribute_other)};
  // ALLOCATE(a, SOURCE=s)
  RTNAME(AllocatableSetBounds)(*a, 0, -1, -2);
  RTNAME(AllocatableAllocateSource)
  (*a, *s, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__);
  EXPECT_TRUE(a->IsAllocated());
  EXPECT_EQ(a->Elements(), 0u);
  EXPECT_EQ(a->GetDimension(0).LowerBound(), 1);
  EXPECT_EQ(a->GetDimension(0).UpperBound(), 0);
  a->Destroy();
}

TEST(AllocatableTest, DoubleAllocation) {
  // CLASS(*), ALLOCATABLE :: r
  // ALLOCATE(REAL::r)
  auto r{createAllocatable(TypeCategory::Real, 4, 0)};
  EXPECT_FALSE(r->IsAllocated());
  EXPECT_TRUE(r->IsAllocatable());
  RTNAME(AllocatableAllocate)(*r);
  EXPECT_TRUE(r->IsAllocated());

  // Make sure AllocatableInitIntrinsicForAllocate doesn't reset the decsriptor
  // if it is allocated.
  // ALLOCATE(INTEGER::r)
  RTNAME(AllocatableInitIntrinsicForAllocate)
  (*r, Fortran::common::TypeCategory::Integer, 4);
  EXPECT_TRUE(r->IsAllocated());
}