blob: 730e0547575330b6cf294535e14c5ceffbbd15bd [file] [log] [blame]
Michael Butlerf76acd02018-03-22 16:37:57 -07001/*
2 * Copyright (C) 2018 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#define LOG_TAG "neuralnetworks_hidl_hal_test"
18
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010019#include "1.0/Callbacks.h"
Xusong Wang9e2b97b2019-08-23 16:10:54 -070020#include "GeneratedTestHarness.h"
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010021#include "VtsHalNeuralnetworks.h"
22
Michael Butlerf76acd02018-03-22 16:37:57 -070023namespace android {
24namespace hardware {
25namespace neuralnetworks {
26namespace V1_0 {
27namespace vts {
28namespace functional {
29
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010030using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
Michael Butlerf76acd02018-03-22 16:37:57 -070031
32///////////////////////// UTILITY FUNCTIONS /////////////////////////
33
Michael Butlerf76acd02018-03-22 16:37:57 -070034// Primary validation function. This function will take a valid request, apply a
35// mutation to it to invalidate the request, then pass it to interface calls
36// that use the request. Note that the request here is passed by value, and any
37// mutation to the request does not leave this function.
38static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
39 Request request, const std::function<void(Request*)>& mutation) {
40 mutation(&request);
41 SCOPED_TRACE(message + " [execute]");
42
43 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
44 ASSERT_NE(nullptr, executionCallback.get());
45 Return<ErrorStatus> executeLaunchStatus = preparedModel->execute(request, executionCallback);
46 ASSERT_TRUE(executeLaunchStatus.isOk());
47 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
48
49 executionCallback->wait();
50 ErrorStatus executionReturnStatus = executionCallback->getStatus();
51 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
52}
53
54// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
55// so this is efficiently accomplished by moving the element to the end and
56// resizing the hidl_vec to one less.
57template <typename Type>
58static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
59 if (vec) {
60 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
61 vec->resize(vec->size() - 1);
62 }
63}
64
65template <typename Type>
66static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
67 // assume vec is valid
68 const uint32_t index = vec->size();
69 vec->resize(index + 1);
70 (*vec)[index] = value;
71 return index;
72}
73
74///////////////////////// REMOVE INPUT ////////////////////////////////////
75
76static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
77 for (size_t input = 0; input < request.inputs.size(); ++input) {
78 const std::string message = "removeInput: removed input " + std::to_string(input);
79 validate(preparedModel, message, request,
80 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
81 }
82}
83
84///////////////////////// REMOVE OUTPUT ////////////////////////////////////
85
86static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
87 for (size_t output = 0; output < request.outputs.size(); ++output) {
88 const std::string message = "removeOutput: removed Output " + std::to_string(output);
89 validate(preparedModel, message, request,
90 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
91 }
92}
93
94///////////////////////////// ENTRY POINT //////////////////////////////////
95
Xusong Wang8e8b70c2019-08-09 16:38:14 -070096void ValidationTest::validateRequest(const sp<IPreparedModel>& preparedModel,
97 const Request& request) {
98 removeInputTest(preparedModel, request);
99 removeOutputTest(preparedModel, request);
Michael Butlerf76acd02018-03-22 16:37:57 -0700100}
101
102} // namespace functional
103} // namespace vts
104} // namespace V1_0
105} // namespace neuralnetworks
106} // namespace hardware
107} // namespace android