| 1 |
//===-- flang/unittests/Runtime/tools.h -------------------------*- 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 |
#ifndef FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_ |
| 10 |
#define FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_ |
| 11 |
|
| 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" |
| 17 |
#include <cstdint> |
| 18 |
#include <cstring> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
namespace Fortran::runtime { |
| 22 |
|
| 23 |
template <typename A> |
| 24 |
static void StoreElement(void *p, const A &x, std::size_t bytes) { |
| 25 |
std::memcpy(p, &x, bytes); |
| 26 |
} |
| 27 |
|
| 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); |
| 33 |
} |
| 34 |
|
| 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]); |
| 44 |
} |
| 45 |
int stat{result->Allocate()}; |
| 46 |
EXPECT_EQ(stat, 0) << stat; |
| 47 |
EXPECT_LE(data.size(), result->Elements()); |
| 48 |
char *p{result->OffsetElement<char>()}; |
| 49 |
for (A x : data) { |
| 50 |
StoreElement(p, x, elemLen); |
| 51 |
p += elemLen; |
| 52 |
} |
| 53 |
return result; |
| 54 |
} |
| 55 |
|
| 56 |
} // namespace Fortran::runtime |
| 57 |
#endif // FORTRAN_UNITTESTS_RUNTIME_TOOLS_H_ |