1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/c/common.h"
17
18 #include <gtest/gtest.h>
19
20 namespace tflite {
21
22 // NOTE: this tests only the TfLiteIntArray part of context.
23 // most of common.h is provided in the context of using it with
24 // interpreter.h and interpreter.cc, so interpreter_test.cc tests context
25 // structures more thoroughly.
26
TEST(IntArray,TestIntArrayCreate)27 TEST(IntArray, TestIntArrayCreate) {
28 TfLiteIntArray* a = TfLiteIntArrayCreate(0);
29 TfLiteIntArray* b = TfLiteIntArrayCreate(3);
30 TfLiteIntArrayFree(a);
31 TfLiteIntArrayFree(b);
32 }
33
TEST(IntArray,TestIntArrayCopy)34 TEST(IntArray, TestIntArrayCopy) {
35 TfLiteIntArray* a = TfLiteIntArrayCreate(2);
36 a->data[0] = 22;
37 a->data[1] = 24;
38 TfLiteIntArray* b = TfLiteIntArrayCopy(a);
39 ASSERT_NE(a, b);
40 ASSERT_EQ(a->size, b->size);
41 ASSERT_EQ(a->data[0], b->data[0]);
42 ASSERT_EQ(a->data[1], b->data[1]);
43 TfLiteIntArrayFree(a);
44 TfLiteIntArrayFree(b);
45 }
46
TEST(IntArray,TestIntArrayEqual)47 TEST(IntArray, TestIntArrayEqual) {
48 TfLiteIntArray* a = TfLiteIntArrayCreate(1);
49 a->data[0] = 1;
50 TfLiteIntArray* b = TfLiteIntArrayCreate(2);
51 b->data[0] = 5;
52 b->data[1] = 6;
53 TfLiteIntArray* c = TfLiteIntArrayCreate(2);
54 c->data[0] = 5;
55 c->data[1] = 6;
56 TfLiteIntArray* d = TfLiteIntArrayCreate(2);
57 d->data[0] = 6;
58 d->data[1] = 6;
59 ASSERT_FALSE(TfLiteIntArrayEqual(a, b));
60 ASSERT_TRUE(TfLiteIntArrayEqual(b, c));
61 ASSERT_TRUE(TfLiteIntArrayEqual(b, b));
62 ASSERT_FALSE(TfLiteIntArrayEqual(c, d));
63 TfLiteIntArrayFree(a);
64 TfLiteIntArrayFree(b);
65 TfLiteIntArrayFree(c);
66 TfLiteIntArrayFree(d);
67 }
68
TEST(FloatArray,TestFloatArrayCreate)69 TEST(FloatArray, TestFloatArrayCreate) {
70 TfLiteFloatArray* a = TfLiteFloatArrayCreate(0);
71 TfLiteFloatArray* b = TfLiteFloatArrayCreate(3);
72 TfLiteFloatArrayFree(a);
73 TfLiteFloatArrayFree(b);
74 }
75
TEST(Types,TestTypeNames)76 TEST(Types, TestTypeNames) {
77 auto type_name = [](TfLiteType t) {
78 return std::string(TfLiteTypeGetName(t));
79 };
80 EXPECT_EQ(type_name(kTfLiteNoType), "NOTYPE");
81 EXPECT_EQ(type_name(kTfLiteFloat64), "FLOAT64");
82 EXPECT_EQ(type_name(kTfLiteFloat32), "FLOAT32");
83 EXPECT_EQ(type_name(kTfLiteFloat16), "FLOAT16");
84 EXPECT_EQ(type_name(kTfLiteInt16), "INT16");
85 EXPECT_EQ(type_name(kTfLiteInt32), "INT32");
86 EXPECT_EQ(type_name(kTfLiteUInt32), "UINT32");
87 EXPECT_EQ(type_name(kTfLiteUInt8), "UINT8");
88 EXPECT_EQ(type_name(kTfLiteUInt64), "UINT64");
89 EXPECT_EQ(type_name(kTfLiteInt8), "INT8");
90 EXPECT_EQ(type_name(kTfLiteInt64), "INT64");
91 EXPECT_EQ(type_name(kTfLiteBool), "BOOL");
92 EXPECT_EQ(type_name(kTfLiteComplex64), "COMPLEX64");
93 EXPECT_EQ(type_name(kTfLiteComplex128), "COMPLEX128");
94 EXPECT_EQ(type_name(kTfLiteString), "STRING");
95 EXPECT_EQ(type_name(kTfLiteResource), "RESOURCE");
96 EXPECT_EQ(type_name(kTfLiteVariant), "VARIANT");
97 }
98
TEST(Quantization,TestQuantizationFree)99 TEST(Quantization, TestQuantizationFree) {
100 TfLiteTensor t;
101 // Set these values, otherwise TfLiteTensorFree has uninitialized values.
102 t.allocation_type = kTfLiteArenaRw;
103 t.dims = nullptr;
104 t.dims_signature = nullptr;
105 t.quantization.type = kTfLiteAffineQuantization;
106 t.sparsity = nullptr;
107 auto* params = reinterpret_cast<TfLiteAffineQuantization*>(
108 malloc(sizeof(TfLiteAffineQuantization)));
109 params->scale = TfLiteFloatArrayCreate(3);
110 params->zero_point = TfLiteIntArrayCreate(3);
111 t.quantization.params = reinterpret_cast<void*>(params);
112 TfLiteTensorFree(&t);
113 }
114
TEST(Sparsity,TestSparsityFree)115 TEST(Sparsity, TestSparsityFree) {
116 TfLiteTensor t = {};
117 // Set these values, otherwise TfLiteTensorFree has uninitialized values.
118 t.allocation_type = kTfLiteArenaRw;
119 t.dims = nullptr;
120 t.dims_signature = nullptr;
121
122 // A dummy CSR sparse matrix.
123 t.sparsity = static_cast<TfLiteSparsity*>(malloc(sizeof(TfLiteSparsity)));
124 t.sparsity->traversal_order = TfLiteIntArrayCreate(2);
125 t.sparsity->block_map = nullptr;
126
127 t.sparsity->dim_metadata = static_cast<TfLiteDimensionMetadata*>(
128 malloc(sizeof(TfLiteDimensionMetadata) * 2));
129 t.sparsity->dim_metadata_size = 2;
130
131 t.sparsity->dim_metadata[0].format = kTfLiteDimDense;
132 t.sparsity->dim_metadata[0].dense_size = 4;
133
134 t.sparsity->dim_metadata[1].format = kTfLiteDimSparseCSR;
135 t.sparsity->dim_metadata[1].array_segments = TfLiteIntArrayCreate(2);
136 t.sparsity->dim_metadata[1].array_indices = TfLiteIntArrayCreate(3);
137
138 TfLiteTensorFree(&t);
139 }
140
141 } // namespace tflite
142
main(int argc,char ** argv)143 int main(int argc, char** argv) {
144 ::testing::InitGoogleTest(&argc, argv);
145 return RUN_ALL_TESTS();
146 }
147