• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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_support/codegen/code_generator.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 namespace tflite {
22 namespace support {
23 namespace codegen {
24 namespace {
25 
26 using ::testing::ElementsAreArray;
27 
28 class CodeGeneratorTest : public ::testing::Test {
29  public:
30   class TestingCodeGenerator : public CodeGenerator {
31    public:
TestingCodeGenerator()32     explicit TestingCodeGenerator() : CodeGenerator() {}
33 
34     // Make tested method public.
ConvertToValidName(const std::string & name)35     static std::string ConvertToValidName(const std::string& name) {
36       return CodeGenerator::ConvertToValidName(name);
37     }
ResolveConflictedInputAndOutputNames(std::vector<std::string> * input,std::vector<std::string> * output)38     static void ResolveConflictedInputAndOutputNames(
39         std::vector<std::string>* input, std::vector<std::string>* output) {
40       CodeGenerator::ResolveConflictedInputAndOutputNames(input, output);
41     }
42   };
43 };
44 
TEST_F(CodeGeneratorTest,UpperCasesShouldLower)45 TEST_F(CodeGeneratorTest, UpperCasesShouldLower) {
46   EXPECT_THAT(TestingCodeGenerator::ConvertToValidName("AlphaBetCOOL"),
47               "alphabetcool");
48 }
49 
TEST_F(CodeGeneratorTest,NonAlphaNumShouldReplace)50 TEST_F(CodeGeneratorTest, NonAlphaNumShouldReplace) {
51   EXPECT_THAT(TestingCodeGenerator::ConvertToValidName("A+=B C\t"), "a__b_c_");
52 }
53 
TEST_F(CodeGeneratorTest,NoLeadingUnderscore)54 TEST_F(CodeGeneratorTest, NoLeadingUnderscore) {
55   EXPECT_THAT(TestingCodeGenerator::ConvertToValidName("+KAI Z"), "kai_z");
56 }
57 
TEST_F(CodeGeneratorTest,NoLeadingNumbers)58 TEST_F(CodeGeneratorTest, NoLeadingNumbers) {
59   EXPECT_THAT(TestingCodeGenerator::ConvertToValidName("3000 Cool Tensors"),
60               "tensor_3000_cool_tensors");
61 }
62 
TEST_F(CodeGeneratorTest,TestSimpleIONames)63 TEST_F(CodeGeneratorTest, TestSimpleIONames) {
64   std::vector<std::string> inputs = {"image"};
65   std::vector<std::string> outputs = {"output"};
66   TestingCodeGenerator::ResolveConflictedInputAndOutputNames(&inputs, &outputs);
67   EXPECT_THAT(inputs, ElementsAreArray({"image"}));
68   EXPECT_THAT(outputs, ElementsAreArray({"output"}));
69 }
70 
TEST_F(CodeGeneratorTest,TestIOConflict)71 TEST_F(CodeGeneratorTest, TestIOConflict) {
72   std::vector<std::string> inputs = {"image"};
73   std::vector<std::string> outputs = {"image"};
74   TestingCodeGenerator::ResolveConflictedInputAndOutputNames(&inputs, &outputs);
75   EXPECT_THAT(inputs, ElementsAreArray({"input_image"}));
76   EXPECT_THAT(outputs, ElementsAreArray({"output_image"}));
77 }
78 
TEST_F(CodeGeneratorTest,TestInternalConflict)79 TEST_F(CodeGeneratorTest, TestInternalConflict) {
80   std::vector<std::string> inputs = {"image", "image"};
81   std::vector<std::string> outputs = {"output"};
82   TestingCodeGenerator::ResolveConflictedInputAndOutputNames(&inputs, &outputs);
83   EXPECT_THAT(inputs, ElementsAreArray({"image1", "image2"}));
84   EXPECT_THAT(outputs, ElementsAreArray({"output"}));
85 }
86 
TEST_F(CodeGeneratorTest,TestAllConflictNTo1)87 TEST_F(CodeGeneratorTest, TestAllConflictNTo1) {
88   std::vector<std::string> inputs = {"image", "image"};
89   std::vector<std::string> outputs = {"image"};
90   TestingCodeGenerator::ResolveConflictedInputAndOutputNames(&inputs, &outputs);
91   EXPECT_THAT(inputs, ElementsAreArray({"input_image1", "input_image2"}));
92   EXPECT_THAT(outputs, ElementsAreArray({"output_image"}));
93 }
94 
TEST_F(CodeGeneratorTest,TestAllConflict)95 TEST_F(CodeGeneratorTest, TestAllConflict) {
96   std::vector<std::string> inputs = {"image", "audio", "image", "audio",
97                                      "audio"};
98   std::vector<std::string> outputs = {"image", "image", "audio", "feature",
99                                       "feature"};
100   TestingCodeGenerator::ResolveConflictedInputAndOutputNames(&inputs, &outputs);
101   EXPECT_THAT(inputs,
102               ElementsAreArray({"input_image1", "input_audio1", "input_image2",
103                                 "input_audio2", "input_audio3"}));
104   EXPECT_THAT(outputs,
105               ElementsAreArray({"output_image1", "output_image2",
106                                 "output_audio", "feature1", "feature2"}));
107 }
108 
TEST_F(CodeGeneratorTest,TestAllConflictReversed)109 TEST_F(CodeGeneratorTest, TestAllConflictReversed) {
110   std::vector<std::string> inputs = {"image", "image", "audio", "feature",
111                                      "feature"};
112   std::vector<std::string> outputs = {"image", "audio", "image", "audio",
113                                       "audio"};
114   TestingCodeGenerator::ResolveConflictedInputAndOutputNames(&inputs, &outputs);
115   EXPECT_THAT(inputs,
116               ElementsAreArray({"input_image1", "input_image2", "input_audio",
117                                 "feature1", "feature2"}));
118   EXPECT_THAT(outputs, ElementsAreArray({"output_image1", "output_audio1",
119                                          "output_image2", "output_audio2",
120                                          "output_audio3"}));
121 }
122 
123 }  // namespace
124 }  // namespace codegen
125 }  // namespace support
126 }  // namespace tflite
127