blob: 519e9a781329c06025c2483d99824b288f8c887f [file] [log] [blame]
David Gross704da4d2019-08-07 12:12:12 -07001/*
2 * Copyright (C) 2019 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// This file tests that various abnormal uses of ANeuralNetworks*_free() don't crash.
18//
19// Limitation: It doesn't set various combinations of properties on objects before
20// freeing those objects.
21
David Gross704da4d2019-08-07 12:12:12 -070022#include <gtest/gtest.h>
23
24#include <vector>
25
Slava Shklyaev19582f22021-01-13 16:12:02 +000026#include "NeuralNetworks.h"
27
David Gross704da4d2019-08-07 12:12:12 -070028namespace {
29
30ANeuralNetworksModel* createUnfinishedModel() {
31 ANeuralNetworksModel* model = nullptr;
32 EXPECT_EQ(ANeuralNetworksModel_create(&model), ANEURALNETWORKS_NO_ERROR);
33
34 uint32_t dimensions[] = {1};
35 ANeuralNetworksOperandType type = {
36 .type = ANEURALNETWORKS_TENSOR_FLOAT32, .dimensionCount = 1, .dimensions = dimensions};
37 EXPECT_EQ(ANeuralNetworksModel_addOperand(model, &type), ANEURALNETWORKS_NO_ERROR);
38 EXPECT_EQ(ANeuralNetworksModel_addOperand(model, &type), ANEURALNETWORKS_NO_ERROR);
39
40 const uint32_t inList[]{0};
41 const uint32_t outList[]{1};
42 EXPECT_EQ(
43 ANeuralNetworksModel_addOperation(model, ANEURALNETWORKS_FLOOR, 1, inList, 1, outList),
44 ANEURALNETWORKS_NO_ERROR);
45 EXPECT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(model, 1, inList, 1, outList),
46 ANEURALNETWORKS_NO_ERROR);
47
48 return model;
49}
50
51ANeuralNetworksModel* createFinishedModel() {
52 ANeuralNetworksModel* const model = createUnfinishedModel();
53 EXPECT_EQ(ANeuralNetworksModel_finish(model), ANEURALNETWORKS_NO_ERROR);
54 return model;
55}
56
57std::vector<ANeuralNetworksDevice*> createDeviceList() {
58 std::vector<ANeuralNetworksDevice*> devices;
59
60 uint32_t numDevices = 0;
61 EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
62 for (uint32_t devIndex = 0; devIndex < numDevices; ++devIndex) {
63 ANeuralNetworksDevice* device = nullptr;
64 const int getDeviceStatus = ANeuralNetworks_getDevice(devIndex, &device);
65 EXPECT_EQ(getDeviceStatus, ANEURALNETWORKS_NO_ERROR);
66 if (getDeviceStatus == ANEURALNETWORKS_NO_ERROR) {
67 devices.push_back(device);
68 }
69 }
70
71 return devices;
72}
73
74TEST(Free, Null) {
75 ANeuralNetworksBurst_free(nullptr);
76 ANeuralNetworksCompilation_free(nullptr);
77 ANeuralNetworksEvent_free(nullptr);
78 ANeuralNetworksExecution_free(nullptr);
79 ANeuralNetworksMemory_free(nullptr);
80 ANeuralNetworksModel_free(nullptr);
81}
82
83TEST(Free, UnfinishedModel) {
84 ANeuralNetworksModel* const model = createUnfinishedModel();
85 ANeuralNetworksModel_free(model);
86}
87
88TEST(Free, UnfinishedCompilation) {
89 ANeuralNetworksModel* const model = createFinishedModel();
90
91 ANeuralNetworksCompilation* compilation = nullptr;
92 ASSERT_EQ(ANeuralNetworksCompilation_create(model, &compilation), ANEURALNETWORKS_NO_ERROR);
93 ANeuralNetworksCompilation_free(compilation);
94
95 ANeuralNetworksModel_free(model);
96}
97
98TEST(Free, UnfinishedCompilationForDevices) {
99 ANeuralNetworksModel* const model = createFinishedModel();
100
101 const auto devices = createDeviceList();
102
103 ANeuralNetworksCompilation* compilation = nullptr;
104 ASSERT_EQ(ANeuralNetworksCompilation_createForDevices(model, devices.data(), devices.size(),
105 &compilation),
106 ANEURALNETWORKS_NO_ERROR);
107 ANeuralNetworksCompilation_free(compilation);
108
109 ANeuralNetworksModel_free(model);
110}
111
112TEST(Free, UnscheduledExecution) {
113 ANeuralNetworksModel* const model = createFinishedModel();
114
115 ANeuralNetworksCompilation* compilation = nullptr;
116 ASSERT_EQ(ANeuralNetworksCompilation_create(model, &compilation), ANEURALNETWORKS_NO_ERROR);
117 ASSERT_EQ(ANeuralNetworksCompilation_finish(compilation), ANEURALNETWORKS_NO_ERROR);
118
119 ANeuralNetworksExecution* execution = nullptr;
120 ASSERT_EQ(ANeuralNetworksExecution_create(compilation, &execution), ANEURALNETWORKS_NO_ERROR);
121 ANeuralNetworksExecution_free(execution);
122
123 ANeuralNetworksCompilation_free(compilation);
124
125 ANeuralNetworksModel_free(model);
126}
127
128} // namespace