Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / Analysis / TensorSpecTest.cpp
blob2364f9e3a99a8fc2ed5063fabe21174b44f0d7fc
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-exception
6 //
7 //===----------------------------------------------------------------------===//
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"
16 using namespace llvm;
18 extern const char *TestMainArgv0;
20 TEST(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));
34 TEST(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]
42 )");
43 EXPECT_TRUE(!!Value);
44 LLVMContext Ctx;
45 auto Spec = getTensorSpecFromJSON(Ctx, *Value);
46 EXPECT_FALSE(Spec);
49 TEST(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));
64 TEST(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");