56 lines · cpp
1//===-- unittests/Runtime/Assign.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "flang/Runtime/assign.h"10#include "tools.h"11#include "gtest/gtest.h"12#include <vector>13 14using namespace Fortran::runtime;15using Fortran::common::TypeCategory;16 17TEST(Assign, RTNAME(CopyInAssign)) {18 // contiguous -> contiguous copy in19 auto intArray{MakeArray<TypeCategory::Integer, 1>(20 std::vector<int>{2, 3}, std::vector<int>{1, 2, 3, 4, 5, 6}, sizeof(int))};21 StaticDescriptor<2> staticIntResult;22 Descriptor &intResult{staticIntResult.descriptor()};23 24 RTNAME(CopyInAssign(intResult, *intArray));25 ASSERT_TRUE(intResult.IsAllocated());26 ASSERT_TRUE(intResult.IsContiguous());27 ASSERT_EQ(intResult.type(), intArray->type());28 ASSERT_EQ(intResult.ElementBytes(), sizeof(int));29 EXPECT_EQ(intResult.GetDimension(0).LowerBound(), 1);30 EXPECT_EQ(intResult.GetDimension(0).Extent(), 2);31 EXPECT_EQ(intResult.GetDimension(1).LowerBound(), 1);32 EXPECT_EQ(intResult.GetDimension(1).Extent(), 3);33 int expected[6] = {1, 2, 3, 4, 5, 6};34 EXPECT_EQ(35 std::memcmp(intResult.OffsetElement<int>(0), expected, 6 * sizeof(int)),36 0);37 intResult.Destroy();38 39 // discontiguous -> contiguous rank-1 copy in40 intArray = MakeArray<TypeCategory::Integer, 1>(std::vector<int>{8},41 std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8}, sizeof(int));42 StaticDescriptor<1> staticIntResultStrided;43 Descriptor &intResultStrided{staticIntResultStrided.descriptor()};44 // Treat the descriptor as a strided array of 445 intArray->GetDimension(0).SetByteStride(sizeof(int) * 2);46 intArray->GetDimension(0).SetExtent(4);47 RTNAME(CopyInAssign(intResultStrided, *intArray));48 49 int expectedStrided[4] = {1, 3, 5, 7};50 EXPECT_EQ(std::memcmp(intResultStrided.OffsetElement<int>(0), expectedStrided,51 4 * sizeof(int)),52 0);53 54 intResultStrided.Destroy();55}56