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