| /* Copyright 2021 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/builtin_op_data.h" |
| #include "tensorflow/lite/c/common.h" |
| #include "tensorflow/lite/micro/kernels/kernel_runner.h" |
| #include "tensorflow/lite/micro/test_helpers.h" |
| #include "tensorflow/lite/micro/testing/micro_test.h" |
| |
| namespace tflite { |
| namespace testing { |
| namespace { |
| |
| template <typename inputT, typename outputT> |
| void TestCast(int* input_dims_data, const inputT* input_data, |
| const outputT* expected_output_data, outputT* output_data) { |
| TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data); |
| TfLiteIntArray* output_dims = IntArrayFromInts(input_dims_data); |
| const int output_dims_count = ElementCount(*output_dims); |
| constexpr int inputs_size = 1; |
| constexpr int outputs_size = 1; |
| constexpr int tensors_size = inputs_size + outputs_size; |
| TfLiteTensor tensors[tensors_size] = { |
| CreateTensor(input_data, input_dims), |
| CreateTensor(output_data, output_dims), |
| }; |
| |
| int inputs_array_data[] = {1, 0}; |
| TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); |
| int outputs_array_data[] = {1, 1}; |
| TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data); |
| |
| const TFLMRegistration registration = Register_CAST(); |
| micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array, |
| outputs_array, |
| /*builtin_data=*/nullptr); |
| |
| TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare()); |
| TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke()); |
| |
| for (int i = 0; i < output_dims_count; ++i) { |
| TF_LITE_MICRO_EXPECT_EQ(expected_output_data[i], output_data[i]); |
| } |
| } |
| |
| } // namespace |
| } // namespace testing |
| } // namespace tflite |
| |
| TF_LITE_MICRO_TESTS_BEGIN |
| |
| TF_LITE_MICRO_TEST(CastFloatToInt8) { |
| int8_t output_data[6]; |
| int input_dims[] = {2, 3, 2}; |
| |
| // TODO(b/178391195): Test negative and out-of-range numbers. |
| const float input_values[] = {100.f, 1.0f, 0.f, 0.4f, 1.999f, 1.1f}; |
| const int8_t golden[] = {100, 1, 0, 0, 1, 1}; |
| tflite::testing::TestCast(input_dims, input_values, golden, output_data); |
| } |
| |
| TF_LITE_MICRO_TEST(CastFloatToInt16) { |
| int16_t output_data[6]; |
| int input_dims[] = {2, 3, 2}; |
| |
| // TODO(b/178391195): Test negative and out-of-range numbers. |
| const float input_values[] = {100.f, 1.0f, 0.f, 0.4f, 1.999f, 1.1f}; |
| const int16_t golden[] = {100, 1, 0, 0, 1, 1}; |
| tflite::testing::TestCast(input_dims, input_values, golden, output_data); |
| } |
| |
| TF_LITE_MICRO_TEST(CastInt8ToFloat) { |
| float output_data[6]; |
| int input_dims[] = {2, 3, 2}; |
| const int8_t input_values[] = {123, 0, 1, 2, 3, 4}; |
| const float golden[] = {123.f, 0.f, 1.f, 2.f, 3.f, 4.f}; |
| tflite::testing::TestCast(input_dims, input_values, golden, output_data); |
| } |
| |
| TF_LITE_MICRO_TEST(CastInt16ToFloat) { |
| float output_data[6]; |
| int input_dims[] = {2, 3, 2}; |
| const int16_t input_values[] = {123, 0, 1, 2, 3, 4}; |
| const float golden[] = {123.f, 0.f, 1.f, 2.f, 3.f, 4.f}; |
| tflite::testing::TestCast(input_dims, input_values, golden, output_data); |
| } |
| |
| TF_LITE_MICRO_TEST(CastInt16ToInt32) { |
| int32_t output_data[6]; |
| int input_dims[] = {2, 3, 2}; |
| const int16_t input_values[] = {123, 0, 1, 2, 3, 4}; |
| const int32_t golden[] = {123, 0, 1, 2, 3, 4}; |
| tflite::testing::TestCast(input_dims, input_values, golden, output_data); |
| } |
| |
| TF_LITE_MICRO_TEST(CastInt32ToInt16) { |
| int16_t output_data[6]; |
| int input_dims[] = {2, 3, 2}; |
| const int32_t input_values[] = {123, 0, 1, 2, 3, 4}; |
| const int16_t golden[] = {123, 0, 1, 2, 3, 4}; |
| tflite::testing::TestCast(input_dims, input_values, golden, output_data); |
| } |
| |
| TF_LITE_MICRO_TESTS_END |