Home | History | Annotate | Download | only in testing
      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 #include "tensorflow/contrib/lite/testing/test_runner.h"
     16 
     17 #include <gmock/gmock.h>
     18 #include <gtest/gtest.h>
     19 
     20 namespace tflite {
     21 namespace testing {
     22 namespace {
     23 
     24 class ConcreteTestRunner : public TestRunner {
     25  public:
     26   void LoadModel(const string& bin_file_path) override {}
     27   const std::vector<int>& GetInputs() override { return ids_; }
     28   const std::vector<int>& GetOutputs() override { return ids_; }
     29   void ReshapeTensor(int id, const string& csv_values) override {}
     30   void AllocateTensors() override {}
     31   void ResetTensor(int id) override {}
     32   void SetInput(int id, const string& csv_values) override {}
     33   void SetExpectation(int id, const string& csv_values) override {}
     34   string ReadOutput(int id) override { return ""; }
     35   void Invoke() override {}
     36   bool CheckResults() override { return true; }
     37   bool CheckFloatSizes(size_t bytes, size_t values) {
     38     return CheckSizes<float>(bytes, values);
     39   }
     40 
     41  private:
     42   std::vector<int> ids_;
     43 };
     44 
     45 TEST(TestRunner, ModelPath) {
     46   ConcreteTestRunner runner;
     47   EXPECT_EQ(runner.GetFullPath("test.bin"), "test.bin");
     48   runner.SetModelBaseDir("/tmp");
     49   EXPECT_EQ(runner.GetFullPath("test.bin"), "/tmp/test.bin");
     50 }
     51 
     52 TEST(TestRunner, InvocationId) {
     53   ConcreteTestRunner runner;
     54   EXPECT_EQ(runner.GetInvocationId(), "");
     55   runner.SetInvocationId("X");
     56   EXPECT_EQ(runner.GetInvocationId(), "X");
     57 }
     58 
     59 TEST(TestRunner, Invalidation) {
     60   ConcreteTestRunner runner;
     61   EXPECT_TRUE(runner.IsValid());
     62   EXPECT_EQ(runner.GetErrorMessage(), "");
     63   runner.Invalidate("Some Error");
     64   EXPECT_FALSE(runner.IsValid());
     65   EXPECT_EQ(runner.GetErrorMessage(), "Some Error");
     66 }
     67 
     68 TEST(TestRunner, OverallSuccess) {
     69   ConcreteTestRunner runner;
     70   EXPECT_TRUE(runner.GetOverallSuccess());
     71   runner.SetOverallSuccess(false);
     72   EXPECT_FALSE(runner.GetOverallSuccess());
     73 }
     74 
     75 TEST(TestRunner, CheckSizes) {
     76   ConcreteTestRunner runner;
     77   EXPECT_TRUE(runner.CheckFloatSizes(16, 4));
     78   EXPECT_FALSE(runner.CheckFloatSizes(16, 2));
     79   EXPECT_EQ(runner.GetErrorMessage(),
     80             "Expected '4' elements for a tensor, but only got '2'");
     81 }
     82 
     83 }  // namespace
     84 }  // namespace testing
     85 }  // namespace tflite
     86