Home | History | Annotate | Download | only in ops
      1 /* Copyright 2017 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 <vector>
     17 
     18 #include <gtest/gtest.h>
     19 #include "tensorflow/contrib/lite/interpreter.h"
     20 #include "tensorflow/contrib/lite/kernels/register.h"
     21 #include "tensorflow/contrib/lite/kernels/test_util.h"
     22 #include "tensorflow/contrib/lite/model.h"
     23 #include "tensorflow/contrib/lite/string_util.h"
     24 
     25 namespace tflite {
     26 
     27 namespace ops {
     28 namespace custom {
     29 TfLiteRegistration* Register_NORMALIZE();
     30 
     31 namespace {
     32 
     33 using ::testing::ElementsAreArray;
     34 
     35 class NormalizeOpModel : public SingleOpModel {
     36  public:
     37   explicit NormalizeOpModel(const string& input) {
     38     input_ = AddInput(TensorType_STRING);
     39     output_ = AddOutput(TensorType_STRING);
     40 
     41     SetCustomOp("Normalize", {}, Register_NORMALIZE);
     42     BuildInterpreter({{static_cast<int>(input.size())}});
     43     PopulateStringTensor(input_, {input});
     44   }
     45 
     46   std::vector<string> GetStringOutput() {
     47     TfLiteTensor* output = interpreter_->tensor(output_);
     48     int num = GetStringCount(output);
     49     std::vector<string> result(num);
     50     for (int i = 0; i < num; i++) {
     51       auto ref = GetString(output, i);
     52       result[i] = string(ref.str, ref.len);
     53     }
     54     return result;
     55   }
     56 
     57  private:
     58   int input_;
     59   int output_;
     60 };
     61 
     62 TEST(NormalizeOpTest, RegularInput) {
     63   NormalizeOpModel m("I'm good; you're welcome");
     64   m.Invoke();
     65   EXPECT_THAT(m.GetStringOutput(),
     66               ElementsAreArray({"<S> i am good; you are welcome <E>"}));
     67 }
     68 
     69 TEST(NormalizeOpTest, OneInput) {
     70   NormalizeOpModel m("Hi!!!!");
     71   m.Invoke();
     72   EXPECT_THAT(m.GetStringOutput(), ElementsAreArray({"<S> hi ! <E>"}));
     73 }
     74 
     75 TEST(NormalizeOpTest, EmptyInput) {
     76   NormalizeOpModel m("");
     77   m.Invoke();
     78   EXPECT_THAT(m.GetStringOutput(), ElementsAreArray({"<S>  <E>"}));
     79 }
     80 
     81 }  // namespace
     82 }  // namespace custom
     83 }  // namespace ops
     84 }  // namespace tflite
     85 
     86 int main(int argc, char** argv) {
     87   // On Linux, add: tflite::LogToStderr();
     88   ::testing::InitGoogleTest(&argc, argv);
     89   return RUN_ALL_TESTS();
     90 }
     91