1 //===- TensorSpecTest.cpp - test for TensorSpec ---------------------------===//
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 #include "llvm/Analysis/TensorSpec.h"
10 #include "llvm/Support/Path.h"
11 #include "llvm/Support/SourceMgr.h"
12 #include "llvm/Testing/Support/SupportHelpers.h"
13 #include "gtest/gtest.h"
17 extern const char *TestMainArgv0
;
19 TEST(TensorSpecTest
, JSONParsing
) {
20 auto Value
= json::parse(
21 R
"({"name
": "tensor_name
",
28 Optional
<TensorSpec
> Spec
= getTensorSpecFromJSON(Ctx
, *Value
);
30 EXPECT_EQ(*Spec
, TensorSpec::createSpec
<int32_t>("tensor_name", {1, 4}, 2));
33 TEST(TensorSpecTest
, JSONParsingInvalidTensorType
) {
34 auto Value
= json::parse(
36 {"name
": "tensor_name
",
38 "type
": "no such type
",
44 auto Spec
= getTensorSpecFromJSON(Ctx
, *Value
);
48 TEST(TensorSpecTest
, TensorSpecSizesAndTypes
) {
49 auto Spec1D
= TensorSpec::createSpec
<int16_t>("Hi1", {1});
50 auto Spec2D
= TensorSpec::createSpec
<int16_t>("Hi2", {1, 1});
51 auto Spec1DLarge
= TensorSpec::createSpec
<float>("Hi3", {10});
52 auto Spec3DLarge
= TensorSpec::createSpec
<float>("Hi3", {2, 4, 10});
53 EXPECT_TRUE(Spec1D
.isElementType
<int16_t>());
54 EXPECT_FALSE(Spec3DLarge
.isElementType
<double>());
55 EXPECT_EQ(Spec1D
.getElementCount(), 1U);
56 EXPECT_EQ(Spec2D
.getElementCount(), 1U);
57 EXPECT_EQ(Spec1DLarge
.getElementCount(), 10U);
58 EXPECT_EQ(Spec3DLarge
.getElementCount(), 80U);
59 EXPECT_EQ(Spec3DLarge
.getElementByteSize(), sizeof(float));
60 EXPECT_EQ(Spec1D
.getElementByteSize(), sizeof(int16_t));