1 /*
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
19 #include "1.0/Callbacks.h"
20 #include "GeneratedTestHarness.h"
21 #include "VtsHalNeuralnetworks.h"
22
23 namespace android::hardware::neuralnetworks::V1_0::vts::functional {
24
25 using implementation::ExecutionCallback;
26
27 using ExecutionMutation = std::function<void(Request*)>;
28
29 ///////////////////////// UTILITY FUNCTIONS /////////////////////////
30
31 // Primary validation function. This function will take a valid request, apply a
32 // mutation to it to invalidate the request, then pass it to interface calls
33 // that use the request.
validate(const sp<IPreparedModel> & preparedModel,const std::string & message,const Request & originalRequest,const ExecutionMutation & mutate)34 static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
35 const Request& originalRequest, const ExecutionMutation& mutate) {
36 Request request = originalRequest;
37 mutate(&request);
38 SCOPED_TRACE(message + " [execute]");
39
40 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
41 Return<ErrorStatus> executeLaunchStatus = preparedModel->execute(request, executionCallback);
42 ASSERT_TRUE(executeLaunchStatus.isOk());
43 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
44
45 executionCallback->wait();
46 ErrorStatus executionReturnStatus = executionCallback->getStatus();
47 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
48 }
49
50 // Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
51 // so this is efficiently accomplished by moving the element to the end and
52 // resizing the hidl_vec to one less.
53 template <typename Type>
hidl_vec_removeAt(hidl_vec<Type> * vec,uint32_t index)54 static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
55 if (vec) {
56 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
57 vec->resize(vec->size() - 1);
58 }
59 }
60
61 template <typename Type>
hidl_vec_push_back(hidl_vec<Type> * vec,const Type & value)62 static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
63 // assume vec is valid
64 const uint32_t index = vec->size();
65 vec->resize(index + 1);
66 (*vec)[index] = value;
67 return index;
68 }
69
70 ///////////////////////// REMOVE INPUT ////////////////////////////////////
71
removeInputTest(const sp<IPreparedModel> & preparedModel,const Request & request)72 static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
73 for (size_t input = 0; input < request.inputs.size(); ++input) {
74 const std::string message = "removeInput: removed input " + std::to_string(input);
75 validate(preparedModel, message, request,
76 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
77 }
78 }
79
80 ///////////////////////// REMOVE OUTPUT ////////////////////////////////////
81
removeOutputTest(const sp<IPreparedModel> & preparedModel,const Request & request)82 static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
83 for (size_t output = 0; output < request.outputs.size(); ++output) {
84 const std::string message = "removeOutput: removed Output " + std::to_string(output);
85 validate(preparedModel, message, request,
86 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
87 }
88 }
89
90 ///////////////////////////// ENTRY POINT //////////////////////////////////
91
validateRequest(const sp<IPreparedModel> & preparedModel,const Request & request)92 void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
93 removeInputTest(preparedModel, request);
94 removeOutputTest(preparedModel, request);
95 }
96
97 } // namespace android::hardware::neuralnetworks::V1_0::vts::functional
98