summaryrefslogtreecommitdiff
path: root/tests/nnfw_api/src/fixtures.h
blob: b2fa2c292938781577b1f7efed0759e08ce6f7ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NNFW_API_TEST_FIXTURES_H__
#define __NNFW_API_TEST_FIXTURES_H__

#include <array>
#include <gtest/gtest.h>
#include <nnfw.h>

#include "model_path.h"

inline uint64_t num_elems(const nnfw_tensorinfo *ti)
{
  uint64_t n = 1;
  for (uint32_t i = 0; i < ti->rank; ++i)
  {
    n *= ti->dims[i];
  }
  return n;
}

struct SessionObject
{
  nnfw_session *session = nullptr;
  std::vector<std::vector<float>> inputs;
  std::vector<std::vector<float>> outputs;
};

class ValidationTest : public ::testing::Test
{
protected:
  void SetUp() override {}
};

class ValidationTestSessionCreated : public ValidationTest
{
protected:
  void SetUp() override
  {
    ValidationTest::SetUp();
    ASSERT_EQ(nnfw_create_session(&_session), NNFW_STATUS_NO_ERROR);
  }

  void TearDown() override
  {
    ASSERT_EQ(nnfw_close_session(_session), NNFW_STATUS_NO_ERROR);
    ValidationTest::TearDown();
  }

protected:
  nnfw_session *_session = nullptr;
};

template <int PackageNo> class ValidationTestModelLoaded : public ValidationTestSessionCreated
{
protected:
  void SetUp() override
  {
    ValidationTestSessionCreated::SetUp();
    ASSERT_EQ(nnfw_load_model_from_file(_session,
                                        ModelPath::get().getModelAbsolutePath(PackageNo).c_str()),
              NNFW_STATUS_NO_ERROR);
    ASSERT_NE(_session, nullptr);
  }

  void TearDown() override { ValidationTestSessionCreated::TearDown(); }
};

template <int PackageNo> class ValidationTestFourModelsSetInput : public ValidationTest
{
protected:
  static const uint32_t NUM_SESSIONS = 4;

  void SetUp() override
  {
    ValidationTest::SetUp();

    auto model_path = ModelPath::get().getModelAbsolutePath(ModelPath::ADD);
    for (auto &obj : _objects)
    {
      ASSERT_EQ(nnfw_create_session(&obj.session), NNFW_STATUS_NO_ERROR);
      ASSERT_EQ(nnfw_load_model_from_file(obj.session, model_path.c_str()), NNFW_STATUS_NO_ERROR);
      ASSERT_EQ(nnfw_prepare(obj.session), NNFW_STATUS_NO_ERROR);

      obj.inputs.resize(1);
      nnfw_tensorinfo ti;
      ASSERT_EQ(nnfw_input_tensorinfo(obj.session, 0, &ti), NNFW_STATUS_NO_ERROR);
      uint64_t input_elements = num_elems(&ti);
      obj.inputs[0].resize(input_elements);
      ASSERT_EQ(nnfw_set_input(obj.session, 0, ti.dtype, obj.inputs[0].data(),
                               sizeof(float) * input_elements),
                NNFW_STATUS_NO_ERROR);

      obj.outputs.resize(1);
      nnfw_tensorinfo ti_output;
      ASSERT_EQ(nnfw_output_tensorinfo(obj.session, 0, &ti_output), NNFW_STATUS_NO_ERROR);
      uint64_t output_elements = num_elems(&ti_output);
      obj.outputs[0].resize(output_elements);
      ASSERT_EQ(nnfw_set_output(obj.session, 0, ti_output.dtype, obj.outputs[0].data(),
                                sizeof(float) * output_elements),
                NNFW_STATUS_NO_ERROR);
    }
  }

  void TearDown() override
  {
    for (auto &obj : _objects)
    {
      ASSERT_EQ(nnfw_close_session(obj.session), NNFW_STATUS_NO_ERROR);
    }
    ValidationTest::TearDown();
  }

protected:
  std::array<SessionObject, NUM_SESSIONS> _objects;
};

#endif // __NNFW_API_TEST_FIXTURES_H__