• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_FRAMEWORK_ML_NN_RUNTIME_TEST_TESTGENERATED_H
18 #define ANDROID_FRAMEWORK_ML_NN_RUNTIME_TEST_TESTGENERATED_H
19 
20 #include <gtest/gtest.h>
21 
22 #include "TestCompliance.h"
23 #include "TestHarness.h"
24 #include "TestNeuralNetworksWrapper.h"
25 
26 #ifdef NNTEST_CTS
27 #define NNTEST_COMPUTE_MODE
28 #endif
29 
30 #ifdef NNTEST_COMPUTE_MODE
31 #define GENERATED_TESTS_BASE testing::TestWithParam<Execution::ComputeMode>
32 #undef TEST_F
33 #define TEST_F TEST_P
34 // Only generated tests include the TestGenerated.h header file, so only those
35 // tests will be affected by changing their TEST_F to TEST_P.  If we
36 // accidentally change TEST_F to TEST_P in some other context, we will get a
37 // compile-time failure, because TEST_F requires a non-value-parameterized
38 // fixture class whereas TEST_P requires a value-parameterized fixture class.
39 //
40 // Example failure:
41 //
42 // clang-format off
43 //     gtest-param-util.h:488:41: error: no type named 'ParamType' in '(anonymous namespace)::MemoryTest'
44 //       using ParamType = typename TestSuite::ParamType;
45 //                         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
46 //     TestMemory.cpp:43:1: note: in instantiation of template class 'testing::internal::ParameterizedTestSuiteInfo<(anonymous namespace)::MemoryTest>' requested here
47 //     TEST_P(MemoryTest, TestFd) {
48 //     ^
49 //     gtest-param-test.h:428:11: note: expanded from macro 'TEST_P'
50 // clang-format on
51 #else
52 #define GENERATED_TESTS_BASE ::testing::Test
53 #endif
54 
55 using namespace android::nn::test_wrapper;
56 using namespace test_helper;
57 
58 namespace generated_tests {
59 
60 class GeneratedTests : public GENERATED_TESTS_BASE {
61    protected:
62     virtual void SetUp() override;
63     virtual void TearDown() override;
64 
65     Compilation compileModel(const Model* model);
66     void executeWithCompilation(const Model* model, Compilation* compilation,
67                                 std::function<bool(int)> isIgnored,
68                                 std::vector<MixedTypedExample>& examples, std::string dumpFile);
69     void executeOnce(const Model* model, std::function<bool(int)> isIgnored,
70                      std::vector<MixedTypedExample>& examples, std::string dumpFile);
71     void executeMultithreadedOwnCompilation(const Model* model, std::function<bool(int)> isIgnored,
72                                             std::vector<MixedTypedExample>& examples);
73     void executeMultithreadedSharedCompilation(const Model* model,
74                                                std::function<bool(int)> isIgnored,
75                                                std::vector<MixedTypedExample>& examples);
76     // Test driver for those generated from ml/nn/runtime/test/spec
77     void execute(std::function<void(Model*)> createModel, std::function<bool(int)> isIgnored,
78                  std::vector<MixedTypedExample>& examples, std::string dumpFile = "");
79 
80     std::string mCacheDir;
81     std::vector<uint8_t> mToken;
82     bool mTestCompilationCaching;
83 #ifdef NNTEST_COMPUTE_MODE
84     // SetUp() uses Execution::setComputeMode() to establish a new ComputeMode,
85     // and saves off the previous ComputeMode here; TearDown() restores that
86     // previous ComputeMode, so that subsequent tests will not be affected by
87     // the SetUp() ComputeMode change.
88     Execution::ComputeMode mOldComputeMode;
89 #endif
90 };
91 
92 // Tag for the dynamic output shape tests
93 class DynamicOutputShapeTest : public GeneratedTests {};
94 
95 }  // namespace generated_tests
96 
97 using namespace generated_tests;
98 
99 #endif  // ANDROID_FRAMEWORK_ML_NN_RUNTIME_TEST_TESTGENERATED_H
100