Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2018 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 #include <gtest/gtest.h>
     16 #include "tensorflow/lite/interpreter.h"
     17 #include "tensorflow/lite/kernels/register.h"
     18 #include "tensorflow/lite/kernels/test_util.h"
     19 #include "tensorflow/lite/model.h"
     20 
     21 namespace tflite {
     22 namespace {
     23 
     24 using ::testing::ElementsAreArray;
     25 
     26 class ExpOpModel : public SingleOpModel {
     27  public:
     28   ExpOpModel(const TensorData& input, const TensorType& output) {
     29     input_ = AddInput(input);
     30     output_ = AddOutput(output);
     31     SetBuiltinOp(BuiltinOperator_EXP, BuiltinOptions_ExpOptions,
     32                  CreateExpOptions(builder_).Union());
     33     BuildInterpreter({GetShape(input_)});
     34   }
     35 
     36   template <class T>
     37   void SetInput(std::initializer_list<T> data) {
     38     PopulateTensor(input_, data);
     39   }
     40 
     41   template <class T>
     42   std::vector<T> GetOutput() {
     43     return ExtractVector<T>(output_);
     44   }
     45   std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
     46 
     47  protected:
     48   int input_;
     49   int output_;
     50 };
     51 
     52 TEST(ExpOpTest, FloatTest) {
     53   std::initializer_list<float> data = {1.0, 0.0, -1.0, 1.0, 1.0, -1.0};
     54   ExpOpModel m({TensorType_FLOAT32, {3, 1, 2}}, TensorType_FLOAT32);
     55   m.SetInput<float>(data);
     56   m.Invoke();
     57   EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({3, 1, 2}));
     58   EXPECT_THAT(m.GetOutput<float>(),
     59               ElementsAreArray(ArrayFloatNear(
     60                   {2.71828, 1, 0.367879, 2.71828, 2.71828, 0.367879})));
     61 }
     62 
     63 }  // namespace
     64 }  // namespace tflite
     65 
     66 int main(int argc, char** argv) {
     67   ::tflite::LogToStderr();
     68   ::testing::InitGoogleTest(&argc, argv);
     69   return RUN_ALL_TESTS();
     70 }
     71