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