• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/c_api_internal.h"
17 #include <gtest/gtest.h>
18 
19 namespace tflite {
20 
21 // NOTE: this tests only the TfLiteIntArray part of context.
22 // most of c_api_internal.h is provided in the context of using it with
23 // interpreter.h and interpreter.cc, so interpreter_test.cc tests context
24 // structures more thoroughly.
25 
TEST(IntArray,TestIntArrayCreate)26 TEST(IntArray, TestIntArrayCreate) {
27   TfLiteIntArray* a = TfLiteIntArrayCreate(0);
28   TfLiteIntArray* b = TfLiteIntArrayCreate(3);
29   TfLiteIntArrayFree(a);
30   TfLiteIntArrayFree(b);
31 }
32 
TEST(IntArray,TestIntArrayCopy)33 TEST(IntArray, TestIntArrayCopy) {
34   TfLiteIntArray* a = TfLiteIntArrayCreate(2);
35   a->data[0] = 22;
36   a->data[1] = 24;
37   TfLiteIntArray* b = TfLiteIntArrayCopy(a);
38   ASSERT_NE(a, b);
39   ASSERT_EQ(a->size, b->size);
40   ASSERT_EQ(a->data[0], b->data[0]);
41   ASSERT_EQ(a->data[1], b->data[1]);
42   TfLiteIntArrayFree(a);
43   TfLiteIntArrayFree(b);
44 }
45 
TEST(IntArray,TestIntArrayEqual)46 TEST(IntArray, TestIntArrayEqual) {
47   TfLiteIntArray* a = TfLiteIntArrayCreate(1);
48   a->data[0] = 1;
49   TfLiteIntArray* b = TfLiteIntArrayCreate(2);
50   b->data[0] = 5;
51   b->data[1] = 6;
52   TfLiteIntArray* c = TfLiteIntArrayCreate(2);
53   c->data[0] = 5;
54   c->data[1] = 6;
55   TfLiteIntArray* d = TfLiteIntArrayCreate(2);
56   d->data[0] = 6;
57   d->data[1] = 6;
58   ASSERT_FALSE(TfLiteIntArrayEqual(a, b));
59   ASSERT_TRUE(TfLiteIntArrayEqual(b, c));
60   ASSERT_TRUE(TfLiteIntArrayEqual(b, b));
61   ASSERT_FALSE(TfLiteIntArrayEqual(c, d));
62   TfLiteIntArrayFree(a);
63   TfLiteIntArrayFree(b);
64   TfLiteIntArrayFree(c);
65   TfLiteIntArrayFree(d);
66 }
67 
TEST(FloatArray,TestFloatArrayCreate)68 TEST(FloatArray, TestFloatArrayCreate) {
69   TfLiteFloatArray* a = TfLiteFloatArrayCreate(0);
70   TfLiteFloatArray* b = TfLiteFloatArrayCreate(3);
71   TfLiteFloatArrayFree(a);
72   TfLiteFloatArrayFree(b);
73 }
74 
TEST(Types,TestTypeNames)75 TEST(Types, TestTypeNames) {
76   auto type_name = [](TfLiteType t) {
77     return std::string(TfLiteTypeGetName(t));
78   };
79   EXPECT_EQ(type_name(kTfLiteNoType), "NOTYPE");
80   EXPECT_EQ(type_name(kTfLiteFloat32), "FLOAT32");
81   EXPECT_EQ(type_name(kTfLiteInt16), "INT16");
82   EXPECT_EQ(type_name(kTfLiteInt32), "INT32");
83   EXPECT_EQ(type_name(kTfLiteUInt8), "UINT8");
84   EXPECT_EQ(type_name(kTfLiteInt8), "INT8");
85   EXPECT_EQ(type_name(kTfLiteInt64), "INT64");
86   EXPECT_EQ(type_name(kTfLiteBool), "BOOL");
87   EXPECT_EQ(type_name(kTfLiteComplex64), "COMPLEX64");
88   EXPECT_EQ(type_name(kTfLiteString), "STRING");
89 }
90 
TEST(Quantization,TestQuantizationFree)91 TEST(Quantization, TestQuantizationFree) {
92   TfLiteTensor t;
93   // Set these values, otherwise TfLiteTensorFree has uninitialized values.
94   t.allocation_type = kTfLiteArenaRw;
95   t.dims = nullptr;
96   t.quantization.type = kTfLiteAffineQuantization;
97   auto* params = reinterpret_cast<TfLiteAffineQuantization*>(
98       malloc(sizeof(TfLiteAffineQuantization)));
99   params->scale = TfLiteFloatArrayCreate(3);
100   params->zero_point = TfLiteIntArrayCreate(3);
101   t.quantization.params = reinterpret_cast<void*>(params);
102   TfLiteTensorFree(&t);
103 }
104 
105 }  // namespace tflite
106 
main(int argc,char ** argv)107 int main(int argc, char** argv) {
108   ::testing::InitGoogleTest(&argc, argv);
109   return RUN_ALL_TESTS();
110 }
111