1 //===-- flang/unittests/Runtime/tools.h -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_
10 #define FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_
12 #include "gtest/gtest.h"
13 #include "flang/Runtime/allocatable.h"
14 #include "flang/Runtime/cpp-type.h"
15 #include "flang/Runtime/descriptor.h"
16 #include "flang/Runtime/type-code.h"
21 namespace Fortran::runtime
{
24 static void StoreElement(void *p
, const A
&x
, std::size_t bytes
) {
25 std::memcpy(p
, &x
, bytes
);
28 template <typename CHAR
>
29 static void StoreElement(
30 void *p
, const std::basic_string
<CHAR
> &str
, std::size_t bytes
) {
31 ASSERT_LE(bytes
, sizeof(CHAR
) * str
.size());
32 std::memcpy(p
, str
.data(), bytes
);
35 template <TypeCategory CAT
, int KIND
, typename A
>
36 static OwningPtr
<Descriptor
> MakeArray(const std::vector
<int> &shape
,
37 const std::vector
<A
> &data
,
38 std::size_t elemLen
= CAT
== TypeCategory::Complex
? 2 * KIND
: KIND
) {
39 auto rank
{static_cast<int>(shape
.size())};
40 auto result
{Descriptor::Create(TypeCode
{CAT
, KIND
}, elemLen
, nullptr, rank
,
41 nullptr, CFI_attribute_allocatable
)};
42 for (int j
{0}; j
< rank
; ++j
) {
43 result
->GetDimension(j
).SetBounds(1, shape
[j
]);
45 int stat
{result
->Allocate()};
46 EXPECT_EQ(stat
, 0) << stat
;
47 EXPECT_LE(data
.size(), result
->Elements());
48 char *p
{result
->OffsetElement
<char>()};
50 StoreElement(p
, x
, elemLen
);
56 } // namespace Fortran::runtime
57 #endif // FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_