#include "flang/Runtime/pointer.h"
#include "gtest/gtest.h"
#include "tools.h"
#include "flang/Runtime/descriptor.h"
using namespace Fortran::runtime;
TEST(Pointer, BasicAllocateDeallocate) {
auto p{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4}, 4,
nullptr, 1, nullptr, CFI_attribute_pointer)};
RTNAME(PointerSetBounds)(*p, 0, 2, 11);
RTNAME(PointerAllocate)
(*p, false, nullptr, __FILE__, __LINE__);
EXPECT_TRUE(RTNAME(PointerIsAssociated)(*p));
EXPECT_EQ(p->Elements(), 10u);
EXPECT_EQ(p->GetDimension(0).LowerBound(), 2);
EXPECT_EQ(p->GetDimension(0).UpperBound(), 11);
RTNAME(PointerDeallocate)
(*p, false, nullptr, __FILE__, __LINE__);
EXPECT_FALSE(RTNAME(PointerIsAssociated)(*p));
}
TEST(Pointer, ApplyMoldAllocation) {
auto m{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4}, 4,
nullptr, 0, nullptr, CFI_attribute_pointer)};
RTNAME(PointerAllocate)
(*m, false, nullptr, __FILE__, __LINE__);
auto p{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4}, 4,
nullptr, 0, nullptr, CFI_attribute_pointer)};
p->raw().elem_len = 0;
p->raw().type = CFI_type_other;
RTNAME(PointerApplyMold)(*p, *m);
RTNAME(PointerAllocate)
(*p, false, nullptr, __FILE__, __LINE__);
EXPECT_EQ(p->ElementBytes(), m->ElementBytes());
EXPECT_EQ(p->type(), m->type());
}
TEST(Pointer, DeallocatePolymorphic) {
auto p{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Integer, 4},
4, nullptr, 0, nullptr, CFI_attribute_pointer)};
RTNAME(PointerAllocate)
(*p, false, nullptr, __FILE__, __LINE__);
RTNAME(PointerDeallocatePolymorphic)
(*p, nullptr, false, nullptr, __FILE__, __LINE__);
}
TEST(Pointer, AllocateFromScalarSource) {
auto p{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4}, 4,
nullptr, 1, nullptr, CFI_attribute_pointer)};
float sourecStorage{3.4F};
auto s{Descriptor::Create(Fortran::common::TypeCategory::Real, 4,
reinterpret_cast<void *>(&sourecStorage), 0, nullptr,
CFI_attribute_pointer)};
RTNAME(PointerSetBounds)(*p, 0, 2, 11);
RTNAME(PointerAllocateSource)
(*p, *s, false, nullptr, __FILE__, __LINE__);
EXPECT_TRUE(RTNAME(PointerIsAssociated)(*p));
EXPECT_EQ(p->Elements(), 10u);
EXPECT_EQ(p->GetDimension(0).LowerBound(), 2);
EXPECT_EQ(p->GetDimension(0).UpperBound(), 11);
EXPECT_EQ(*p->OffsetElement<float>(), 3.4F);
p->Destroy();
}
TEST(Pointer, AllocateSourceZeroSize) {
using Fortran::common::TypeCategory;
auto p{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Real, 4}, 4,
nullptr, 1, nullptr, CFI_attribute_pointer)};
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)};
RTNAME(PointerSetBounds)(*p, 0, -1, -2);
RTNAME(PointerAllocateSource)
(*p, *s, false, nullptr, __FILE__, __LINE__);
EXPECT_TRUE(RTNAME(PointerIsAssociated)(*p));
EXPECT_EQ(p->Elements(), 0u);
EXPECT_EQ(p->GetDimension(0).LowerBound(), 1);
EXPECT_EQ(p->GetDimension(0).UpperBound(), 0);
p->Destroy();
}