STT-tensorflow/tensorflow/lite/c/c_api_internal_test.cc
A. Unique TensorFlower c58ddf2520 Propagate half precision float through tflite
PiperOrigin-RevId: 247082214
2019-05-07 15:01:57 -07:00

112 lines
3.6 KiB
C++

/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/lite/c/c_api_internal.h"
#include <gtest/gtest.h>
namespace tflite {
// NOTE: this tests only the TfLiteIntArray part of context.
// most of c_api_internal.h is provided in the context of using it with
// interpreter.h and interpreter.cc, so interpreter_test.cc tests context
// structures more thoroughly.
TEST(IntArray, TestIntArrayCreate) {
TfLiteIntArray* a = TfLiteIntArrayCreate(0);
TfLiteIntArray* b = TfLiteIntArrayCreate(3);
TfLiteIntArrayFree(a);
TfLiteIntArrayFree(b);
}
TEST(IntArray, TestIntArrayCopy) {
TfLiteIntArray* a = TfLiteIntArrayCreate(2);
a->data[0] = 22;
a->data[1] = 24;
TfLiteIntArray* b = TfLiteIntArrayCopy(a);
ASSERT_NE(a, b);
ASSERT_EQ(a->size, b->size);
ASSERT_EQ(a->data[0], b->data[0]);
ASSERT_EQ(a->data[1], b->data[1]);
TfLiteIntArrayFree(a);
TfLiteIntArrayFree(b);
}
TEST(IntArray, TestIntArrayEqual) {
TfLiteIntArray* a = TfLiteIntArrayCreate(1);
a->data[0] = 1;
TfLiteIntArray* b = TfLiteIntArrayCreate(2);
b->data[0] = 5;
b->data[1] = 6;
TfLiteIntArray* c = TfLiteIntArrayCreate(2);
c->data[0] = 5;
c->data[1] = 6;
TfLiteIntArray* d = TfLiteIntArrayCreate(2);
d->data[0] = 6;
d->data[1] = 6;
ASSERT_FALSE(TfLiteIntArrayEqual(a, b));
ASSERT_TRUE(TfLiteIntArrayEqual(b, c));
ASSERT_TRUE(TfLiteIntArrayEqual(b, b));
ASSERT_FALSE(TfLiteIntArrayEqual(c, d));
TfLiteIntArrayFree(a);
TfLiteIntArrayFree(b);
TfLiteIntArrayFree(c);
TfLiteIntArrayFree(d);
}
TEST(FloatArray, TestFloatArrayCreate) {
TfLiteFloatArray* a = TfLiteFloatArrayCreate(0);
TfLiteFloatArray* b = TfLiteFloatArrayCreate(3);
TfLiteFloatArrayFree(a);
TfLiteFloatArrayFree(b);
}
TEST(Types, TestTypeNames) {
auto type_name = [](TfLiteType t) {
return std::string(TfLiteTypeGetName(t));
};
EXPECT_EQ(type_name(kTfLiteNoType), "NOTYPE");
EXPECT_EQ(type_name(kTfLiteFloat32), "FLOAT32");
EXPECT_EQ(type_name(kTfLiteFloat16), "FLOAT16");
EXPECT_EQ(type_name(kTfLiteInt16), "INT16");
EXPECT_EQ(type_name(kTfLiteInt32), "INT32");
EXPECT_EQ(type_name(kTfLiteUInt8), "UINT8");
EXPECT_EQ(type_name(kTfLiteInt8), "INT8");
EXPECT_EQ(type_name(kTfLiteInt64), "INT64");
EXPECT_EQ(type_name(kTfLiteBool), "BOOL");
EXPECT_EQ(type_name(kTfLiteComplex64), "COMPLEX64");
EXPECT_EQ(type_name(kTfLiteString), "STRING");
}
TEST(Quantization, TestQuantizationFree) {
TfLiteTensor t;
// Set these values, otherwise TfLiteTensorFree has uninitialized values.
t.allocation_type = kTfLiteArenaRw;
t.dims = nullptr;
t.quantization.type = kTfLiteAffineQuantization;
auto* params = reinterpret_cast<TfLiteAffineQuantization*>(
malloc(sizeof(TfLiteAffineQuantization)));
params->scale = TfLiteFloatArrayCreate(3);
params->zero_point = TfLiteIntArrayCreate(3);
t.quantization.params = reinterpret_cast<void*>(params);
TfLiteTensorFree(&t);
}
} // namespace tflite
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}