| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "flang/Runtime/assign.h" |
| 10 | #include "tools.h" |
| 11 | #include "gtest/gtest.h" |
| 12 | #include <vector> |
| 13 | |
| 14 | using namespace Fortran::runtime; |
| 15 | using Fortran::common::TypeCategory; |
| 16 | |
| 17 | TEST(Assign, RTNAME(CopyInAssign)) { |
| 18 | // contiguous -> contiguous copy in |
| 19 | 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 in |
| 40 | 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 4 |
| 45 | 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 | |