70 lines · cpp
1//===- TensorSpecTest.cpp - test for TensorSpec ---------------------------===//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 "llvm/Analysis/TensorSpec.h"10#include "llvm/Support/JSON.h"11#include "llvm/Support/Path.h"12#include "llvm/Support/SourceMgr.h"13#include "llvm/Testing/Support/SupportHelpers.h"14#include "gtest/gtest.h"15 16using namespace llvm;17 18extern const char *TestMainArgv0;19 20TEST(TensorSpecTest, JSONParsing) {21 auto Value = json::parse(22 R"({"name": "tensor_name", 23 "port": 2, 24 "type": "int32_t", 25 "shape":[1,4]26 })");27 EXPECT_TRUE(!!Value);28 LLVMContext Ctx;29 std::optional<TensorSpec> Spec = getTensorSpecFromJSON(Ctx, *Value);30 EXPECT_TRUE(Spec);31 EXPECT_EQ(*Spec, TensorSpec::createSpec<int32_t>("tensor_name", {1, 4}, 2));32}33 34TEST(TensorSpecTest, JSONParsingInvalidTensorType) {35 auto Value = json::parse(36 R"(37 {"name": "tensor_name", 38 "port": 2, 39 "type": "no such type", 40 "shape":[1,4]41 }42 )");43 EXPECT_TRUE(!!Value);44 LLVMContext Ctx;45 auto Spec = getTensorSpecFromJSON(Ctx, *Value);46 EXPECT_FALSE(Spec);47}48 49TEST(TensorSpecTest, TensorSpecSizesAndTypes) {50 auto Spec1D = TensorSpec::createSpec<int16_t>("Hi1", {1});51 auto Spec2D = TensorSpec::createSpec<int16_t>("Hi2", {1, 1});52 auto Spec1DLarge = TensorSpec::createSpec<float>("Hi3", {10});53 auto Spec3DLarge = TensorSpec::createSpec<float>("Hi3", {2, 4, 10});54 EXPECT_TRUE(Spec1D.isElementType<int16_t>());55 EXPECT_FALSE(Spec3DLarge.isElementType<double>());56 EXPECT_EQ(Spec1D.getElementCount(), 1U);57 EXPECT_EQ(Spec2D.getElementCount(), 1U);58 EXPECT_EQ(Spec1DLarge.getElementCount(), 10U);59 EXPECT_EQ(Spec3DLarge.getElementCount(), 80U);60 EXPECT_EQ(Spec3DLarge.getElementByteSize(), sizeof(float));61 EXPECT_EQ(Spec1D.getElementByteSize(), sizeof(int16_t));62}63 64TEST(TensorSpecTest, PrintValueForDebug) {65 std::vector<int32_t> Values{1, 3};66 EXPECT_EQ(tensorValueToString(reinterpret_cast<const char *>(Values.data()),67 TensorSpec::createSpec<int32_t>("name", {2})),68 "1,3");69}70