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/delegates/gpu/gl/compiler/preprocessor.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 namespace tflite {
25 namespace gpu {
26 namespace gl {
27 namespace {
28 
29 class AccuInlineRewrite : public InlineRewrite {
30  public:
AccuInlineRewrite(std::vector<std::string> * blocks)31   explicit AccuInlineRewrite(std::vector<std::string>* blocks)
32       : blocks_(blocks) {}
33 
Rewrite(absl::string_view input,std::string * output)34   RewriteStatus Rewrite(absl::string_view input, std::string* output) final {
35     blocks_->push_back(std::string(input.data(), input.size()));
36     output->append("r:");
37     output->append(input.data(), input.size());
38     return RewriteStatus::SUCCESS;
39   }
40 
41   std::vector<std::string>* blocks_;
42 };
43 
ParseInlines(const std::string & text)44 std::vector<std::string> ParseInlines(const std::string& text) {
45   std::vector<std::string> blocks;
46   TextPreprocessor preprocessor('$', false);
47   AccuInlineRewrite rewrite(&blocks);
48   preprocessor.AddRewrite(&rewrite);
49   std::string discard;
50   preprocessor.Rewrite(text, &discard).IgnoreError();
51   return blocks;
52 }
53 
TEST(Preprocessor,CornerCases)54 TEST(Preprocessor, CornerCases) {
55   EXPECT_THAT(ParseInlines(""), testing::ElementsAre());
56   EXPECT_THAT(ParseInlines("text text"), testing::ElementsAre());
57   EXPECT_THAT(ParseInlines("$$"), testing::ElementsAre(""));
58 }
59 
TEST(Preprocessor,One)60 TEST(Preprocessor, One) {
61   EXPECT_THAT(ParseInlines("$text$"), testing::ElementsAre("text"));
62   EXPECT_THAT(ParseInlines(" $text$ "), testing::ElementsAre("text"));
63 }
64 
TEST(Preprocessor,More)65 TEST(Preprocessor, More) {
66   EXPECT_THAT(ParseInlines("Test $inline1$\n$inline2$ test $inline3$ "),
67               testing::ElementsAre("inline1", "inline2", "inline3"));
68 }
69 
RewriteInlines(const std::string & text)70 std::string RewriteInlines(const std::string& text) {
71   std::vector<std::string> blocks;
72   TextPreprocessor preprocessor('$', false);
73   AccuInlineRewrite rewrite(&blocks);
74   preprocessor.AddRewrite(&rewrite);
75   std::string out;
76   preprocessor.Rewrite(text, &out).IgnoreError();
77   return out;
78 }
79 
TEST(Preprocessor,RewriteCornerCases)80 TEST(Preprocessor, RewriteCornerCases) {
81   EXPECT_EQ(RewriteInlines(""), "");
82   EXPECT_EQ(RewriteInlines("text text"), "text text");
83   EXPECT_EQ(RewriteInlines("$$"), "r:");
84 }
85 
TEST(Preprocessor,RewriteOne)86 TEST(Preprocessor, RewriteOne) {
87   EXPECT_EQ(RewriteInlines("$text$"), "r:text");
88   EXPECT_EQ(RewriteInlines(" $text$ "), " r:text ");
89 }
90 
TEST(Preprocessor,RewriteMore)91 TEST(Preprocessor, RewriteMore) {
92   EXPECT_EQ(RewriteInlines("Test $inline1$\n$inline2$ test $inline3$ "),
93             "Test r:inline1\nr:inline2 test r:inline3 ");
94 }
95 
96 class SingleRewrite : public InlineRewrite {
97  public:
Rewrite(absl::string_view input,std::string * output)98   RewriteStatus Rewrite(absl::string_view input, std::string* output) final {
99     if (input == "foo") {
100       output->append("bla");
101       return RewriteStatus::SUCCESS;
102     }
103     return RewriteStatus::NOT_RECOGNIZED;
104   }
105 
106   std::vector<std::string>* blocks_;
107 };
108 
TEST(Preprocessor,KeepUnknownRewrites)109 TEST(Preprocessor, KeepUnknownRewrites) {
110   TextPreprocessor preprocessor('$', true);
111   SingleRewrite rewrite;
112   preprocessor.AddRewrite(&rewrite);
113   std::string out;
114   ASSERT_TRUE(preprocessor.Rewrite("Good morning, $name$! $foo$", &out).ok());
115   EXPECT_EQ("Good morning, $name$! bla", out);
116 }
117 
TEST(Preprocessor,KeepUnknownRewrites_Fail)118 TEST(Preprocessor, KeepUnknownRewrites_Fail) {
119   TextPreprocessor preprocessor('$', false);
120   SingleRewrite rewrite;
121   preprocessor.AddRewrite(&rewrite);
122   std::string out;
123   EXPECT_FALSE(preprocessor.Rewrite("Good morning, $name$! $foo$", &out).ok());
124 }
125 
126 }  // namespace
127 }  // namespace gl
128 }  // namespace gpu
129 }  // namespace tflite
130