#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) {
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, nullptr,
1, 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, false,
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());
result.GetDimension(0).SetBounds(1, 1234);
RTNAME(InitArrayConstructorVector)
(*acVector, result, false,
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) {
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, nullptr,
1, nullptr, CFI_attribute_allocatable);
std::allocator<ArrayConstructorVector> cookieAllocator;
ArrayConstructorVector *acVector{cookieAllocator.allocate(1)};
ASSERT_TRUE(acVector);
static constexpr std::size_t expectedElements{10 * (1 + 4 + 2 * 3)};
result.GetDimension(0).SetBounds(1, 0);
RTNAME(InitArrayConstructorVector)
(*acVector, result, true,
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) {
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, nullptr,
1, 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, true,
sizeof(ArrayConstructorVector));
RTNAME(PushArrayConstructorValue)(*acVector, *c2);
ASSERT_DEATH(RTNAME(PushArrayConstructorValue)(*acVector, *c3),
"Array constructor: mismatched character lengths");
}