1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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 <farmhash.h>
24
25namespace tflite {
26
27namespace ops {
28namespace custom {
29TfLiteRegistration* Register_EXTRACT_FEATURES();
30
31namespace {
32
33using ::testing::ElementsAre;
34
35class ExtractFeatureOpModel : public SingleOpModel {
36 public:
37  explicit ExtractFeatureOpModel(const std::vector<string>& input) {
38    input_ = AddInput(TensorType_STRING);
39    signature_ = AddOutput(TensorType_INT32);
40    weight_ = AddOutput(TensorType_FLOAT32);
41
42    SetCustomOp("ExtractFeatures", {}, Register_EXTRACT_FEATURES);
43    BuildInterpreter({{static_cast<int>(input.size())}});
44    PopulateStringTensor(input_, input);
45  }
46
47  std::vector<int> GetSignature() { return ExtractVector<int>(signature_); }
48  std::vector<float> GetWeight() { return ExtractVector<float>(weight_); }
49
50 private:
51  int input_;
52  int signature_;
53  int weight_;
54};
55
56int CalcFeature(const string& str) {
57  return ::util::Fingerprint64(str) % 1000000;
58}
59
60TEST(ExtractFeatureOpTest, RegularInput) {
61  ExtractFeatureOpModel m({"<S>", "<S> Hi", "Hi", "Hi !", "!", "! <E>", "<E>"});
62  m.Invoke();
63  EXPECT_THAT(m.GetSignature(),
64              ElementsAre(0, CalcFeature("<S> Hi"), CalcFeature("Hi"),
65                          CalcFeature("Hi !"), CalcFeature("!"),
66                          CalcFeature("! <E>"), 0));
67  EXPECT_THAT(m.GetWeight(), ElementsAre(0, 2, 1, 2, 1, 2, 0));
68}
69
70TEST(ExtractFeatureOpTest, OneInput) {
71  ExtractFeatureOpModel m({"Hi"});
72  m.Invoke();
73  EXPECT_THAT(m.GetSignature(), ElementsAre(CalcFeature("Hi")));
74  EXPECT_THAT(m.GetWeight(), ElementsAre(1));
75}
76
77TEST(ExtractFeatureOpTest, ZeroInput) {
78  ExtractFeatureOpModel m({});
79  m.Invoke();
80  EXPECT_THAT(m.GetSignature(), ElementsAre(0));
81  EXPECT_THAT(m.GetWeight(), ElementsAre(0));
82}
83
84TEST(ExtractFeatureOpTest, AllBlacklistInput) {
85  ExtractFeatureOpModel m({"<S>", "<E>"});
86  m.Invoke();
87  EXPECT_THAT(m.GetSignature(), ElementsAre(0, 0));
88  EXPECT_THAT(m.GetWeight(), ElementsAre(0, 0));
89}
90
91}  // namespace
92}  // namespace custom
93}  // namespace ops
94}  // namespace tflite
95
96int main(int argc, char** argv) {
97  // On Linux, add: tflite::LogToStderr();
98  ::testing::InitGoogleTest(&argc, argv);
99  return RUN_ALL_TESTS();
100}
101