blob: 0baa85bc8e1dc3d1b46750f693d8587a721f170c [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 Butlerbbe5dad2019-08-26 23:55:47 -070023namespace android::hardware::neuralnetworks::V1_0::vts::functional {
Michael Butlerf76acd02018-03-22 16:37:57 -070024
Michael Butlerbbe5dad2019-08-26 23:55:47 -070025using implementation::ExecutionCallback;
Michael Butlerf76acd02018-03-22 16:37:57 -070026
Michael Butlerda1a6922020-03-11 18:45:45 -070027using ExecutionMutation = std::function<void(Request*)>;
28
Michael Butlerf76acd02018-03-22 16:37:57 -070029///////////////////////// UTILITY FUNCTIONS /////////////////////////
30
Michael Butlerf76acd02018-03-22 16:37:57 -070031// 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
Michael Butlerda1a6922020-03-11 18:45:45 -070033// that use the request.
Michael Butlerf76acd02018-03-22 16:37:57 -070034static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
Michael Butlerda1a6922020-03-11 18:45:45 -070035 const Request& originalRequest, const ExecutionMutation& mutate) {
36 Request request = originalRequest;
37 mutate(&request);
Michael Butlerf76acd02018-03-22 16:37:57 -070038 SCOPED_TRACE(message + " [execute]");
39
40 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
Michael Butlerf76acd02018-03-22 16:37:57 -070041 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.
53template <typename Type>
54static 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
61template <typename Type>
62static 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
72static 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
82static 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
Michael Butlere16af0a2019-08-29 22:17:24 -070092void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wang8e8b70c2019-08-09 16:38:14 -070093 removeInputTest(preparedModel, request);
94 removeOutputTest(preparedModel, request);
Michael Butlerf76acd02018-03-22 16:37:57 -070095}
96
Michael Butlerbbe5dad2019-08-26 23:55:47 -070097} // namespace android::hardware::neuralnetworks::V1_0::vts::functional