1 /* Copyright 2021 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 #include "tensorflow/lite/kernels/shim/test_util.h"
16
17 #include <string>
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include "tensorflow/lite/interpreter.h"
22
23 namespace tflite {
24 namespace {
25
TEST(TfliteTensorDebugString,Basic)26 TEST(TfliteTensorDebugString, Basic) {
27 ::tflite::Interpreter interpreter;
28 interpreter.AddTensors(3);
29 interpreter.AllocateTensors();
30
31 auto t_int32 = UniqueTfLiteTensor(interpreter.tensor(0));
32 PopulateTfLiteTensor<int32_t>({1, 2, 3, 4, 5}, {5}, t_int32.get());
33 EXPECT_EQ("[1, 2, 3, 4, 5]", TfliteTensorDebugString(t_int32.get()));
34
35 auto t_int64 = UniqueTfLiteTensor(interpreter.tensor(1));
36 PopulateTfLiteTensor<int32_t>({1, 2, 3, 4}, {2, 2}, t_int64.get());
37 EXPECT_EQ("[[1, 2], [3, 4]]", TfliteTensorDebugString(t_int64.get()));
38
39 auto t_str = UniqueTfLiteTensor(interpreter.tensor(2));
40 PopulateTfLiteTensor<std::string>({"ab", "cde", "f"}, {1, 3}, t_str.get());
41 EXPECT_EQ("[[ab, cde, f]]", TfliteTensorDebugString(t_str.get()));
42 }
43
TEST(TfliteTensorDebugString,MaxVal)44 TEST(TfliteTensorDebugString, MaxVal) {
45 ::tflite::Interpreter interpreter;
46 interpreter.AddTensors(2);
47 interpreter.AllocateTensors();
48
49 auto t_int32 = UniqueTfLiteTensor(interpreter.tensor(0));
50 PopulateTfLiteTensor<int32_t>({1, 2, 3, 4}, {4}, t_int32.get());
51 EXPECT_EQ("[1, 2, 3, 4]",
52 TfliteTensorDebugString(t_int32.get(), /*max_values=*/4));
53
54 t_int32 = UniqueTfLiteTensor(interpreter.tensor(0));
55 PopulateTfLiteTensor<int32_t>({1, 2, 3, 4, 5}, {5}, t_int32.get());
56 EXPECT_EQ("[1, 2, 3, 4, ...]",
57 TfliteTensorDebugString(t_int32.get(), /*max_values=*/4));
58 }
59
60 } // namespace
61 } // namespace tflite
62