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#include <gtest/gtest.h>
16#include "tensorflow/contrib/lite/interpreter.h"
17#include "tensorflow/contrib/lite/kernels/register.h"
18#include "tensorflow/contrib/lite/kernels/test_util.h"
19#include "tensorflow/contrib/lite/model.h"
20
21namespace tflite {
22namespace {
23
24using ::testing::ElementsAreArray;
25
26class L2NormOpModel : public SingleOpModel {
27 public:
28  L2NormOpModel(std::initializer_list<int> input_shape,
29                ActivationFunctionType activation_type) {
30    input_ = AddInput(TensorType_FLOAT32);
31    output_ = AddOutput(TensorType_FLOAT32);
32    SetBuiltinOp(BuiltinOperator_L2_NORMALIZATION, BuiltinOptions_L2NormOptions,
33                 CreateL2NormOptions(builder_, activation_type).Union());
34    BuildInterpreter({input_shape});
35  }
36
37  void SetInput(std::initializer_list<float> data) {
38    PopulateTensor(input_, data);
39  }
40
41  std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
42
43 private:
44  int input_;
45  int output_;
46};
47
48TEST(L2NormOpTest, SimpleTest) {
49  L2NormOpModel m({1, 1, 1, 6}, ActivationFunctionType_NONE);
50  m.SetInput({-1.1, 0.6, 0.7, 1.2, -0.7, 0.1});
51  m.Invoke();
52  EXPECT_THAT(m.GetOutput(),
53              ElementsAreArray({-0.55, 0.3, 0.35, 0.6, -0.35, 0.05}));
54}
55
56}  // namespace
57}  // namespace tflite
58
59int main(int argc, char** argv) {
60  ::tflite::LogToStderr();
61  ::testing::InitGoogleTest(&argc, argv);
62  return RUN_ALL_TESTS();
63}
64