58 lines · c
1//===-- 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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_10#define FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_11 12#include "gtest/gtest.h"13#include "flang-rt/runtime/descriptor.h"14#include "flang-rt/runtime/type-code.h"15#include "flang/Runtime/allocatable.h"16#include "flang/Runtime/cpp-type.h"17#include <cstdint>18#include <cstring>19#include <vector>20 21namespace Fortran::runtime {22 23template <typename A>24static void StoreElement(void *p, const A &x, std::size_t bytes) {25 std::memcpy(p, &x, bytes);26}27 28template <typename CHAR>29static 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 35template <TypeCategory CAT, int KIND, typename A>36static 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(kNoAsyncObject)};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::runtime57#endif // FLANG_RT_UNITTESTS_RUNTIME_TOOLS_H_58