1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_GENERATED_TEST_UTILS_H
18 #define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_GENERATED_TEST_UTILS_H
19 
20 #include <gtest/gtest.h>
21 
22 #include <memory>
23 #include <utility>
24 #include <vector>
25 
26 #include "TestHarness.h"
27 #include "TestNeuralNetworksWrapper.h"
28 
29 namespace android::nn::generated_tests {
30 
31 class GeneratedTestBase
32     : public ::testing::TestWithParam<test_helper::TestModelManager::TestParam> {
33    protected:
34     const test_helper::TestModel& testModel = *GetParam().second;
35 };
36 
37 #define INSTANTIATE_GENERATED_TEST(TestSuite, filter)                                          \
38     INSTANTIATE_TEST_SUITE_P(                                                                  \
39             TestGenerated, TestSuite,                                                          \
40             ::testing::ValuesIn(::test_helper::TestModelManager::get().getTestModels(filter)), \
41             [](const auto& info) { return info.param.first; })
42 
43 // A generated NDK model.
44 class GeneratedModel : public test_wrapper::Model {
45    public:
46     // A helper method to simplify referenced model lifetime management.
47     //
48     // Usage:
49     //     GeneratedModel model;
50     //     std::vector<Model> refModels;
51     //     createModel(&model, &refModels);
52     //     model.setRefModels(std::move(refModels));
53     //
54     // This makes sure referenced models live as long as the main model.
55     //
setRefModels(std::vector<test_wrapper::Model> refModels)56     void setRefModels(std::vector<test_wrapper::Model> refModels) {
57         mRefModels = std::move(refModels);
58     }
59 
60     // A helper method to simplify CONSTANT_REFERENCE memory lifetime management.
setConstantReferenceMemory(std::unique_ptr<test_wrapper::Memory> memory)61     void setConstantReferenceMemory(std::unique_ptr<test_wrapper::Memory> memory) {
62         mConstantReferenceMemory = std::move(memory);
63     }
64 
65    private:
66     std::vector<test_wrapper::Model> mRefModels;
67     std::unique_ptr<test_wrapper::Memory> mConstantReferenceMemory;
68 };
69 
70 // Convert TestModel to NDK model.
71 void createModel(const test_helper::TestModel& testModel, bool testDynamicOutputShape,
72                  GeneratedModel* model);
createModel(const test_helper::TestModel & testModel,GeneratedModel * model)73 inline void createModel(const test_helper::TestModel& testModel, GeneratedModel* model) {
74     createModel(testModel, /*testDynamicOutputShape=*/false, model);
75 }
76 
77 void createRequest(const test_helper::TestModel& testModel, test_wrapper::Execution* execution,
78                    std::vector<test_helper::TestBuffer>* outputs);
79 
80 }  // namespace android::nn::generated_tests
81 
82 #endif  // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_GENERATED_TEST_UTILS_H
83