| 1 |
//===-- flang/unittests/Runtime/Allocatable.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/allocatable.h" |
| 10 |
#include "gtest/gtest.h" |
| 11 |
#include "tools.h" |
| 12 |
|
| 13 |
using namespace Fortran::runtime; |
| 14 |
|
| 15 |
static OwningPtr<Descriptor> createAllocatable( |
| 16 |
Fortran::common::TypeCategory tc, int kind, int rank = 1) { |
| 17 |
return Descriptor::Create(TypeCode{tc, kind}, kind, nullptr, rank, nullptr, |
| 18 |
CFI_attribute_allocatable); |
| 19 |
} |
| 20 |
|
| 21 |
TEST(AllocatableTest, MoveAlloc) { |
| 22 |
using Fortran::common::TypeCategory; |
| 23 |
// INTEGER(4), ALLOCATABLE :: a(:) |
| 24 |
auto a{createAllocatable(TypeCategory::Integer, 4)}; |
| 25 |
// INTEGER(4), ALLOCATABLE :: b(:) |
| 26 |
auto b{createAllocatable(TypeCategory::Integer, 4)}; |
| 27 |
// ALLOCATE(a(20)) |
| 28 |
a->GetDimension(0).SetBounds(1, 20); |
| 29 |
a->Allocate(); |
| 30 |
|
| 31 |
EXPECT_TRUE(a->IsAllocated()); |
| 32 |
EXPECT_FALSE(b->IsAllocated()); |
| 33 |
|
| 34 |
// Simple move_alloc |
| 35 |
RTNAME(MoveAlloc)(*b, *a, false, nullptr, __FILE__, __LINE__); |
| 36 |
EXPECT_FALSE(a->IsAllocated()); |
| 37 |
EXPECT_TRUE(b->IsAllocated()); |
| 38 |
|
| 39 |
// move_alloc with stat |
| 40 |
std::int32_t stat{ |
| 41 |
RTNAME(MoveAlloc)(*a, *b, true, nullptr, __FILE__, __LINE__)}; |
| 42 |
EXPECT_TRUE(a->IsAllocated()); |
| 43 |
EXPECT_FALSE(b->IsAllocated()); |
| 44 |
EXPECT_EQ(stat, 0); |
| 45 |
|
| 46 |
// move_alloc with errMsg |
| 47 |
auto errMsg{Descriptor::Create( |
| 48 |
sizeof(char), 64, nullptr, 0, nullptr, CFI_attribute_allocatable)}; |
| 49 |
errMsg->Allocate(); |
| 50 |
RTNAME(MoveAlloc)(*b, *a, false, errMsg.get(), __FILE__, __LINE__); |
| 51 |
EXPECT_FALSE(a->IsAllocated()); |
| 52 |
EXPECT_TRUE(b->IsAllocated()); |
| 53 |
|
| 54 |
// move_alloc with stat and errMsg |
| 55 |
stat = RTNAME(MoveAlloc)(*a, *b, true, errMsg.get(), __FILE__, __LINE__); |
| 56 |
EXPECT_TRUE(a->IsAllocated()); |
| 57 |
EXPECT_FALSE(b->IsAllocated()); |
| 58 |
EXPECT_EQ(stat, 0); |
| 59 |
|
| 60 |
// move_alloc with the same deallocated array |
| 61 |
stat = RTNAME(MoveAlloc)(*b, *b, true, errMsg.get(), __FILE__, __LINE__); |
| 62 |
EXPECT_FALSE(b->IsAllocated()); |
| 63 |
EXPECT_EQ(stat, 0); |
| 64 |
|
| 65 |
// move_alloc with the same allocated array should fail |
| 66 |
stat = RTNAME(MoveAlloc)(*a, *a, true, errMsg.get(), __FILE__, __LINE__); |
| 67 |
EXPECT_EQ(stat, 18); |
| 68 |
std::string_view errStr{errMsg->OffsetElement(), errMsg->ElementBytes()}; |
| 69 |
auto trim_pos = errStr.find_last_not_of(' '); |
| 70 |
if (trim_pos != errStr.npos) |
| 71 |
errStr.remove_suffix(errStr.size() - trim_pos - 1); |
| 72 |
EXPECT_EQ(errStr, "Invalid descriptor"); |
| 73 |
} |