Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2017 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 #include "NeuralNetworks.h"
     18 #include "NeuralNetworksOEM.h"
     19 
     20 #include <android/sharedmem.h>
     21 #include <gtest/gtest.h>
     22 #include <sys/mman.h>
     23 #include <future>
     24 #include <string>
     25 
     26 #ifndef NNTEST_ONLY_PUBLIC_API
     27 #include "NeuralNetworksExtensions.h"
     28 #include "TypeManager.h"
     29 #endif
     30 
     31 // This file tests all the validations done by the Neural Networks API.
     32 
     33 namespace {
     34 
     35 class ValidationTest : public ::testing::Test {
     36    protected:
     37     virtual void SetUp() {}
     38 };
     39 
     40 class ValidationTestModel : public ValidationTest {
     41    protected:
     42     virtual void SetUp() {
     43         ValidationTest::SetUp();
     44         ASSERT_EQ(ANeuralNetworksModel_create(&mModel), ANEURALNETWORKS_NO_ERROR);
     45     }
     46     virtual void TearDown() {
     47         ANeuralNetworksModel_free(mModel);
     48         ValidationTest::TearDown();
     49     }
     50 
     51     uint32_t addScalarOperand(int32_t type = ANEURALNETWORKS_INT32) {
     52         ANeuralNetworksOperandType operandType = {
     53                 .type = type, .dimensionCount = 0, .dimensions = nullptr};
     54         EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &operandType), ANEURALNETWORKS_NO_ERROR);
     55         return mNumOperands++;
     56     }
     57 
     58     uint32_t addOperand(const ANeuralNetworksOperandType& operandType) {
     59         EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &operandType), ANEURALNETWORKS_NO_ERROR);
     60         return mNumOperands++;
     61     }
     62 
     63     uint32_t addTensorOperand(int32_t type = ANEURALNETWORKS_TENSOR_FLOAT32) {
     64         uint32_t dimensions[] = {2};
     65         ANeuralNetworksOperandType operandType = {
     66                 .type = type,
     67                 .dimensionCount = sizeof(dimensions) / sizeof(dimensions[0]),
     68                 .dimensions = dimensions,
     69         };
     70         return addOperand(operandType);
     71     }
     72 
     73     void createModel() {
     74         addTensorOperand();
     75         addTensorOperand();
     76         addScalarOperand();
     77         addTensorOperand();
     78         uint32_t inList[3]{0, 1, 2};
     79         uint32_t outList[1]{3};
     80         ASSERT_EQ(ANeuralNetworksModel_addOperation(mModel, ANEURALNETWORKS_ADD, 3, inList, 1,
     81                                                     outList),
     82                   ANEURALNETWORKS_NO_ERROR);
     83         ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 3, inList, 1, outList),
     84                   ANEURALNETWORKS_NO_ERROR);
     85         ASSERT_EQ(ANeuralNetworksModel_finish(mModel), ANEURALNETWORKS_NO_ERROR);
     86         mNumOperations = 1;
     87     }
     88 
     89     uint32_t mNumOperands = 0;
     90     uint32_t mNumOperations = 0;
     91     ANeuralNetworksModel* mModel = nullptr;
     92 
     93     const uint32_t kDummyDimensionValue = 1;
     94     const ANeuralNetworksOperandType kInvalidTensorType1{
     95             .type = ANEURALNETWORKS_TENSOR_FLOAT32,
     96             // dimensionCount must be consistent with dimensions.
     97             .dimensionCount = 1,
     98             .dimensions = nullptr,
     99     };
    100     const ANeuralNetworksOperandType kInvalidTensorType2{
    101             .type = ANEURALNETWORKS_TENSOR_FLOAT32,
    102             // dimensionCount must be consistent with dimensions.
    103             .dimensionCount = 0,
    104             .dimensions = &kDummyDimensionValue,
    105     };
    106 };
    107 
    108 #ifndef NNTEST_ONLY_PUBLIC_API
    109 constexpr const char* kTestExtensionName = "com.android.test_extension";
    110 constexpr int32_t kTestExtensionTensorType = ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL;
    111 
    112 class ValidationTestModelExtensions : public ValidationTestModel {
    113    protected:
    114     virtual void SetUp() {
    115         ValidationTestModel::SetUp();
    116         EXPECT_TRUE(::android::nn::TypeManager::get()->forTest_registerExtension({
    117                 .name = kTestExtensionName,
    118                 .operandTypes =
    119                         {
    120                                 {
    121                                         .type = kTestExtensionTensorType,
    122                                         .isTensor = true,
    123                                         .byteSize = 1,
    124                                 },
    125                         },
    126         }));
    127     }
    128 
    129     virtual void TearDown() {
    130         ::android::nn::TypeManager::get()->forTest_reset();
    131         ValidationTestModel::TearDown();
    132     }
    133 
    134     int32_t getExtensionOperandType(uint16_t typeWithinExtension) {
    135         int32_t result;
    136         EXPECT_EQ(ANeuralNetworksModel_getExtensionOperandType(mModel, kTestExtensionName,
    137                                                                typeWithinExtension, &result),
    138                   ANEURALNETWORKS_NO_ERROR);
    139         return result;
    140     }
    141 };
    142 #endif
    143 
    144 class ValidationTestIdentify : public ValidationTestModel {
    145     virtual void SetUp() {
    146         ValidationTestModel::SetUp();
    147 
    148         uint32_t dimensions[]{1};
    149         ANeuralNetworksOperandType tensorType{.type = ANEURALNETWORKS_TENSOR_FLOAT32,
    150                                               .dimensionCount = 1,
    151                                               .dimensions = dimensions};
    152         ANeuralNetworksOperandType scalarType{
    153                 .type = ANEURALNETWORKS_INT32, .dimensionCount = 0, .dimensions = nullptr};
    154         ASSERT_EQ(ANeuralNetworksModel_addOperand(mModel, &tensorType), ANEURALNETWORKS_NO_ERROR);
    155         ASSERT_EQ(ANeuralNetworksModel_addOperand(mModel, &tensorType), ANEURALNETWORKS_NO_ERROR);
    156         ASSERT_EQ(ANeuralNetworksModel_addOperand(mModel, &scalarType), ANEURALNETWORKS_NO_ERROR);
    157         ASSERT_EQ(ANeuralNetworksModel_addOperand(mModel, &tensorType), ANEURALNETWORKS_NO_ERROR);
    158         uint32_t inList[3]{0, 1, 2};
    159         uint32_t outList[1]{3};
    160         ASSERT_EQ(ANeuralNetworksModel_addOperation(mModel, ANEURALNETWORKS_ADD, 3, inList, 1,
    161                                                     outList),
    162                   ANEURALNETWORKS_NO_ERROR);
    163     }
    164     virtual void TearDown() { ValidationTestModel::TearDown(); }
    165 };
    166 
    167 class ValidationTestCompilation : public ValidationTestModel {
    168    protected:
    169     virtual void SetUp() {
    170         ValidationTestModel::SetUp();
    171         createModel();
    172         ASSERT_EQ(ANeuralNetworksCompilation_create(mModel, &mCompilation),
    173                   ANEURALNETWORKS_NO_ERROR);
    174     }
    175 
    176     virtual void TearDown() {
    177         ANeuralNetworksCompilation_free(mCompilation);
    178         ValidationTestModel::TearDown();
    179     }
    180 
    181     ANeuralNetworksCompilation* mCompilation = nullptr;
    182 };
    183 
    184 class ValidationTestExecution : public ValidationTestCompilation {
    185    protected:
    186     virtual void SetUp() {
    187         ValidationTestCompilation::SetUp();
    188 
    189         ASSERT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
    190 
    191         ASSERT_EQ(ANeuralNetworksExecution_create(mCompilation, &mExecution),
    192                   ANEURALNETWORKS_NO_ERROR);
    193     }
    194     virtual void TearDown() {
    195         ANeuralNetworksExecution_free(mExecution);
    196         ValidationTestCompilation::TearDown();
    197     }
    198     ANeuralNetworksExecution* mExecution = nullptr;
    199 };
    200 
    201 class ValidationTestBurst : public ValidationTestExecution {
    202    protected:
    203     virtual void SetUp() {
    204         ValidationTestExecution::SetUp();
    205 
    206         ASSERT_EQ(ANeuralNetworksBurst_create(mCompilation, &mBurst), ANEURALNETWORKS_NO_ERROR);
    207     }
    208     virtual void TearDown() {
    209         ANeuralNetworksBurst_free(mBurst);
    210         ValidationTestExecution::TearDown();
    211     }
    212     ANeuralNetworksBurst* mBurst = nullptr;
    213 };
    214 
    215 TEST_F(ValidationTest, CreateModel) {
    216     EXPECT_EQ(ANeuralNetworksModel_create(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
    217 }
    218 
    219 TEST_F(ValidationTestModel, AddOperand) {
    220     ANeuralNetworksOperandType floatType{
    221             .type = ANEURALNETWORKS_FLOAT32, .dimensionCount = 0, .dimensions = nullptr};
    222     EXPECT_EQ(ANeuralNetworksModel_addOperand(nullptr, &floatType),
    223               ANEURALNETWORKS_UNEXPECTED_NULL);
    224     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
    225 
    226     ANeuralNetworksOperandType quant8TypeInvalidScale{
    227             .type = ANEURALNETWORKS_TENSOR_QUANT8_ASYMM,
    228             .dimensionCount = 0,
    229             .dimensions = nullptr,
    230             // Scale has to be non-negative
    231             .scale = -1.0f,
    232             .zeroPoint = 0,
    233     };
    234     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &quant8TypeInvalidScale),
    235               ANEURALNETWORKS_BAD_DATA);
    236 
    237     ANeuralNetworksOperandType quant8TypeInvalidZeroPoint{
    238             .type = ANEURALNETWORKS_TENSOR_QUANT8_ASYMM,
    239             .dimensionCount = 0,
    240             .dimensions = nullptr,
    241             .scale = 1.0f,
    242             // zeroPoint has to be in [0, 255]
    243             .zeroPoint = -1,
    244     };
    245     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &quant8TypeInvalidZeroPoint),
    246               ANEURALNETWORKS_BAD_DATA);
    247 
    248     const uint32_t dim = 2;
    249     ANeuralNetworksOperandType invalidScalarType{
    250             .type = ANEURALNETWORKS_INT32,
    251             // a scalar type must have 0 dimensions.
    252             .dimensionCount = 1,
    253             .dimensions = &dim,
    254     };
    255     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &invalidScalarType),
    256               ANEURALNETWORKS_BAD_DATA);
    257 
    258     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &kInvalidTensorType1),
    259               ANEURALNETWORKS_BAD_DATA);
    260     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &kInvalidTensorType2),
    261               ANEURALNETWORKS_BAD_DATA);
    262 
    263     ANeuralNetworksModel_finish(mModel);
    264     // This should fail, as the model is already finished.
    265     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &floatType), ANEURALNETWORKS_BAD_STATE);
    266 }
    267 
    268 TEST_F(ValidationTestModel, SetOperandSymmPerChannelQuantParams) {
    269     const int32_t operandIndex = addTensorOperand(ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL);
    270 
    271     float scales[2] = {1.0, 2.0};
    272     ANeuralNetworksSymmPerChannelQuantParams channelQuant = {
    273             .channelDim = 0,
    274             .scaleCount = 2,
    275             .scales = scales,
    276     };
    277 
    278     EXPECT_EQ(ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(nullptr, operandIndex,
    279                                                                        &channelQuant),
    280               ANEURALNETWORKS_UNEXPECTED_NULL);
    281     EXPECT_EQ(
    282             ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(mModel, operandIndex, nullptr),
    283             ANEURALNETWORKS_UNEXPECTED_NULL);
    284     EXPECT_EQ(ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(mModel, operandIndex + 1,
    285                                                                        &channelQuant),
    286               ANEURALNETWORKS_BAD_DATA);
    287     EXPECT_EQ(ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(mModel, operandIndex,
    288                                                                        &channelQuant),
    289               ANEURALNETWORKS_NO_ERROR);
    290 }
    291 
    292 #ifndef NNTEST_ONLY_PUBLIC_API
    293 TEST_F(ValidationTestModelExtensions, AddOperand_UnknownPrefix) {
    294     ANeuralNetworksOperandType type = {.type = -1};
    295     ASSERT_EQ(ANeuralNetworksModel_addOperand(mModel, &type), ANEURALNETWORKS_BAD_DATA);
    296 }
    297 
    298 TEST_F(ValidationTestModelExtensions, SetOperandSymmPerChannelQuantParams_ExtensionOperand) {
    299     const int32_t operandIndex =
    300             addTensorOperand(getExtensionOperandType(kTestExtensionTensorType));
    301 
    302     float scales[2] = {1.0, 2.0};
    303     ANeuralNetworksSymmPerChannelQuantParams channelQuant = {
    304             .channelDim = 0,
    305             .scaleCount = 2,
    306             .scales = scales,
    307     };
    308 
    309     EXPECT_EQ(ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(mModel, operandIndex,
    310                                                                        &channelQuant),
    311               ANEURALNETWORKS_BAD_DATA);
    312 }
    313 
    314 TEST_F(ValidationTestModelExtensions, SetOperandExtensionData) {
    315     const int32_t operandIndex =
    316             addTensorOperand(getExtensionOperandType(kTestExtensionTensorType));
    317     const int32_t data = 42;
    318     const size_t dataLength = sizeof(data);
    319     EXPECT_EQ(
    320             ANeuralNetworksModel_setOperandExtensionData(nullptr, operandIndex, &data, dataLength),
    321             ANEURALNETWORKS_UNEXPECTED_NULL);
    322     EXPECT_EQ(
    323             ANeuralNetworksModel_setOperandExtensionData(mModel, operandIndex, nullptr, dataLength),
    324             ANEURALNETWORKS_UNEXPECTED_NULL);
    325     EXPECT_EQ(ANeuralNetworksModel_setOperandExtensionData(mModel, operandIndex, &data, 0),
    326               ANEURALNETWORKS_BAD_DATA);
    327     EXPECT_EQ(ANeuralNetworksModel_setOperandExtensionData(mModel, operandIndex + 1, &data,
    328                                                            dataLength),
    329               ANEURALNETWORKS_BAD_DATA);
    330     EXPECT_EQ(ANeuralNetworksModel_setOperandExtensionData(mModel, operandIndex, &data, dataLength),
    331               ANEURALNETWORKS_NO_ERROR);
    332 }
    333 
    334 TEST_F(ValidationTestModelExtensions, SetOperandExtensionData_Empty) {
    335     const int32_t operandIndex =
    336             addTensorOperand(getExtensionOperandType(kTestExtensionTensorType));
    337     EXPECT_EQ(ANeuralNetworksModel_setOperandExtensionData(mModel, operandIndex, nullptr, 0),
    338               ANEURALNETWORKS_NO_ERROR);
    339 }
    340 
    341 TEST_F(ValidationTestModelExtensions, SetOperandExtensionData_NonExtensionOperand) {
    342     const int32_t operandIndex = addTensorOperand();
    343     const int32_t data = 42;
    344     const size_t dataLength = sizeof(data);
    345     EXPECT_EQ(ANeuralNetworksModel_setOperandExtensionData(mModel, operandIndex, &data, dataLength),
    346               ANEURALNETWORKS_BAD_DATA);
    347 }
    348 
    349 TEST_F(ValidationTestModelExtensions, SetOperandValue_UnspecifiedDimension) {
    350     const uint32_t dimensions[2] = {3, 0};
    351     ANeuralNetworksOperandType type = {
    352             .type = getExtensionOperandType(kTestExtensionTensorType),
    353             .dimensionCount = 2,
    354             .dimensions = dimensions,
    355     };
    356     const int32_t operandIndex = addOperand(type);
    357     char buffer[20];
    358     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, operandIndex, buffer, sizeof(buffer)),
    359               ANEURALNETWORKS_BAD_DATA);
    360 }
    361 
    362 TEST_F(ValidationTestModelExtensions, SetOperandValue_UnspecifiedRank) {
    363     ANeuralNetworksOperandType type = {
    364             .type = getExtensionOperandType(kTestExtensionTensorType),
    365             .dimensionCount = 0,
    366             .dimensions = nullptr,
    367     };
    368     const int32_t operandIndex = addOperand(type);
    369     char buffer[20];
    370     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, operandIndex, buffer, sizeof(buffer)),
    371               ANEURALNETWORKS_BAD_DATA);
    372 }
    373 #endif
    374 
    375 TEST_F(ValidationTestModel, SetOptionalOperand) {
    376     ANeuralNetworksOperandType floatType{
    377             .type = ANEURALNETWORKS_FLOAT32, .dimensionCount = 0, .dimensions = nullptr};
    378     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &floatType), ANEURALNETWORKS_NO_ERROR);
    379 
    380     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, nullptr, 0),
    381               ANEURALNETWORKS_NO_ERROR);
    382 }
    383 
    384 TEST_F(ValidationTestModel, SetOperandValue) {
    385     ANeuralNetworksOperandType floatType{
    386             .type = ANEURALNETWORKS_FLOAT32, .dimensionCount = 0, .dimensions = nullptr};
    387     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &floatType), ANEURALNETWORKS_NO_ERROR);
    388 
    389     char buffer[20];
    390     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(nullptr, 0, buffer, sizeof(buffer)),
    391               ANEURALNETWORKS_UNEXPECTED_NULL);
    392     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, nullptr, sizeof(buffer)),
    393               ANEURALNETWORKS_UNEXPECTED_NULL);
    394 
    395     // This should fail, since buffer is not the size of a float32.
    396     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, buffer, sizeof(buffer)),
    397               ANEURALNETWORKS_BAD_DATA);
    398 
    399     // This should succeed.
    400     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, buffer, sizeof(float)),
    401               ANEURALNETWORKS_NO_ERROR);
    402 
    403     // This should fail, as this operand does not exist.
    404     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 1, buffer, sizeof(float)),
    405               ANEURALNETWORKS_BAD_DATA);
    406 
    407     ANeuralNetworksModel_finish(mModel);
    408     // This should fail, as the model is already finished.
    409     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, buffer, sizeof(float)),
    410               ANEURALNETWORKS_BAD_STATE);
    411 }
    412 
    413 TEST_F(ValidationTestModel, SetOperandValueFromMemory) {
    414     uint32_t dimensions[]{1};
    415     ANeuralNetworksOperandType floatType{
    416             .type = ANEURALNETWORKS_TENSOR_FLOAT32, .dimensionCount = 1, .dimensions = dimensions};
    417     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &floatType), ANEURALNETWORKS_NO_ERROR);
    418 
    419     const size_t memorySize = 20;
    420     int memoryFd = ASharedMemory_create("nnMemory", memorySize);
    421     ASSERT_GT(memoryFd, 0);
    422 
    423     ANeuralNetworksMemory* memory;
    424     EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, memoryFd, 0,
    425                                                  &memory),
    426               ANEURALNETWORKS_NO_ERROR);
    427 
    428     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(nullptr, 0, memory, 0, sizeof(float)),
    429               ANEURALNETWORKS_UNEXPECTED_NULL);
    430     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, nullptr, 0, sizeof(float)),
    431               ANEURALNETWORKS_UNEXPECTED_NULL);
    432 
    433     // This should fail, since the operand does not exist.
    434     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, -1, memory, 0, sizeof(float)),
    435               ANEURALNETWORKS_BAD_DATA);
    436 
    437     // This should fail, since memory is not the size of a float32.
    438     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, 0, memorySize),
    439               ANEURALNETWORKS_BAD_DATA);
    440 
    441     // This should fail, as this operand does not exist.
    442     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 1, memory, 0, sizeof(float)),
    443               ANEURALNETWORKS_BAD_DATA);
    444 
    445     // This should fail, since offset is larger than memorySize.
    446     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, memorySize + 1,
    447                                                              sizeof(float)),
    448               ANEURALNETWORKS_BAD_DATA);
    449 
    450     // This should fail, since requested size is larger than the memory.
    451     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, memorySize - 3,
    452                                                              sizeof(float)),
    453               ANEURALNETWORKS_BAD_DATA);
    454 
    455     ANeuralNetworksModel_finish(mModel);
    456     // This should fail, as the model is already finished.
    457     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, 0, sizeof(float)),
    458               ANEURALNETWORKS_BAD_STATE);
    459 
    460     // close memory
    461     close(memoryFd);
    462 }
    463 
    464 TEST_F(ValidationTestModel, SetOperandValueFromAHardwareBuffer) {
    465     uint32_t dimensions[]{1};
    466     ANeuralNetworksOperandType quant8Type{.type = ANEURALNETWORKS_TENSOR_QUANT8_ASYMM,
    467                                           .dimensionCount = 1,
    468                                           .dimensions = dimensions,
    469                                           .scale = 1.0,
    470                                           .zeroPoint = 0};
    471     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &quant8Type), ANEURALNETWORKS_NO_ERROR);
    472 
    473     AHardwareBuffer_Desc desc{
    474             .width = 16,
    475             .height = 16,
    476             .layers = 1,
    477             .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
    478             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
    479     };
    480 
    481     AHardwareBuffer* buffer = nullptr;
    482     ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
    483 
    484     ANeuralNetworksMemory* memory;
    485     EXPECT_EQ(ANeuralNetworksMemory_createFromAHardwareBuffer(buffer, &memory),
    486               ANEURALNETWORKS_NO_ERROR);
    487 
    488     // This should fail, since non-BLOB AHardwareBuffer is not allowed.
    489     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, 0, sizeof(uint8_t)),
    490               ANEURALNETWORKS_UNMAPPABLE);
    491 
    492     AHardwareBuffer_release(buffer);
    493 }
    494 
    495 TEST_F(ValidationTestModel, SetOperandValueFromAHardwareBufferBlob) {
    496     uint32_t dimensions[]{1};
    497     ANeuralNetworksOperandType floatType{
    498             .type = ANEURALNETWORKS_TENSOR_FLOAT32, .dimensionCount = 1, .dimensions = dimensions};
    499     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &floatType), ANEURALNETWORKS_NO_ERROR);
    500 
    501     const size_t memorySize = 20;
    502     AHardwareBuffer_Desc desc{
    503             .width = memorySize,
    504             .height = 1,
    505             .layers = 1,
    506             .format = AHARDWAREBUFFER_FORMAT_BLOB,
    507             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
    508     };
    509 
    510     AHardwareBuffer* buffer = nullptr;
    511     ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
    512 
    513     ANeuralNetworksMemory* memory;
    514     EXPECT_EQ(ANeuralNetworksMemory_createFromAHardwareBuffer(buffer, &memory),
    515               ANEURALNETWORKS_NO_ERROR);
    516 
    517     // This should fail, since offset is larger than memorySize.
    518     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, memorySize + 1,
    519                                                              sizeof(float)),
    520               ANEURALNETWORKS_BAD_DATA);
    521 
    522     // This should fail, since requested size is larger than the memory.
    523     EXPECT_EQ(ANeuralNetworksModel_setOperandValueFromMemory(mModel, 0, memory, memorySize - 3,
    524                                                              sizeof(float)),
    525               ANEURALNETWORKS_BAD_DATA);
    526 
    527     AHardwareBuffer_release(buffer);
    528 }
    529 
    530 TEST_F(ValidationTestModel, AddOEMOperand) {
    531     ANeuralNetworksOperandType OEMScalarType{
    532             .type = ANEURALNETWORKS_OEM_SCALAR, .dimensionCount = 0, .dimensions = nullptr};
    533     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &OEMScalarType), ANEURALNETWORKS_NO_ERROR);
    534     char buffer[20];
    535     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 0, buffer, sizeof(buffer)),
    536               ANEURALNETWORKS_NO_ERROR);
    537 
    538     const size_t kByteSizeOfOEMTensor = 4;
    539     uint32_t dimensions[]{kByteSizeOfOEMTensor};
    540     ANeuralNetworksOperandType OEMTensorType{
    541             .type = ANEURALNETWORKS_TENSOR_OEM_BYTE, .dimensionCount = 1, .dimensions = dimensions};
    542     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &OEMTensorType), ANEURALNETWORKS_NO_ERROR);
    543     EXPECT_EQ(ANeuralNetworksModel_setOperandValue(mModel, 1, buffer, kByteSizeOfOEMTensor),
    544               ANEURALNETWORKS_NO_ERROR);
    545 
    546     ANeuralNetworksModel_finish(mModel);
    547     // This should fail, as the model is already finished.
    548     EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &OEMTensorType), ANEURALNETWORKS_BAD_STATE);
    549 }
    550 
    551 TEST_F(ValidationTestModel, AddOperation) {
    552     uint32_t input = 0;
    553     uint32_t output = 0;
    554     EXPECT_EQ(ANeuralNetworksModel_addOperation(nullptr, ANEURALNETWORKS_AVERAGE_POOL_2D, 1, &input,
    555                                                 1, &output),
    556               ANEURALNETWORKS_UNEXPECTED_NULL);
    557     EXPECT_EQ(ANeuralNetworksModel_addOperation(mModel, ANEURALNETWORKS_AVERAGE_POOL_2D, 0, nullptr,
    558                                                 1, &output),
    559               ANEURALNETWORKS_UNEXPECTED_NULL);
    560     EXPECT_EQ(ANeuralNetworksModel_addOperation(mModel, ANEURALNETWORKS_AVERAGE_POOL_2D, 1, &input,
    561                                                 0, nullptr),
    562               ANEURALNETWORKS_UNEXPECTED_NULL);
    563 
    564     ANeuralNetworksOperationType invalidOp = -1;
    565     EXPECT_EQ(ANeuralNetworksModel_addOperation(mModel, invalidOp, 1, &input, 1, &output),
    566               ANEURALNETWORKS_BAD_DATA);
    567 
    568     ANeuralNetworksModel_finish(mModel);
    569     // This should fail, as the model is already finished.
    570     EXPECT_EQ(ANeuralNetworksModel_addOperation(mModel, ANEURALNETWORKS_AVERAGE_POOL_2D, 1, &input,
    571                                                 1, &output),
    572               ANEURALNETWORKS_BAD_STATE);
    573 }
    574 
    575 TEST_F(ValidationTestModel, IdentifyInputsAndOutputs) {
    576     uint32_t input = 0;
    577     uint32_t output = 0;
    578     EXPECT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(nullptr, 1, &input, 1, &output),
    579               ANEURALNETWORKS_UNEXPECTED_NULL);
    580     EXPECT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 0, nullptr, 1, &output),
    581               ANEURALNETWORKS_UNEXPECTED_NULL);
    582     EXPECT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 1, &input, 0, nullptr),
    583               ANEURALNETWORKS_UNEXPECTED_NULL);
    584 
    585     createModel();
    586     // This should fail, as the model is already finished.
    587     EXPECT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 1, &input, 1, &output),
    588               ANEURALNETWORKS_BAD_STATE);
    589 }
    590 
    591 TEST_F(ValidationTestModel, RelaxComputationFloat32toFloat16) {
    592     EXPECT_EQ(ANeuralNetworksModel_relaxComputationFloat32toFloat16(nullptr, true),
    593               ANEURALNETWORKS_UNEXPECTED_NULL);
    594 
    595     createModel();
    596     // This should fail, as the model is already finished.
    597     EXPECT_EQ(ANeuralNetworksModel_relaxComputationFloat32toFloat16(mModel, true),
    598               ANEURALNETWORKS_BAD_STATE);
    599     EXPECT_EQ(ANeuralNetworksModel_relaxComputationFloat32toFloat16(mModel, false),
    600               ANEURALNETWORKS_BAD_STATE);
    601 }
    602 
    603 TEST_F(ValidationTestModel, Finish) {
    604     EXPECT_EQ(ANeuralNetworksModel_finish(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
    605     createModel();
    606     EXPECT_EQ(ANeuralNetworksModel_finish(mModel), ANEURALNETWORKS_BAD_STATE);
    607 }
    608 
    609 TEST_F(ValidationTestModel, EmptyModel) {
    610     // An empty model is invalid
    611     EXPECT_EQ(ANeuralNetworksModel_finish(mModel), ANEURALNETWORKS_BAD_DATA);
    612 }
    613 
    614 TEST_F(ValidationTestModel, CreateCompilation) {
    615     ANeuralNetworksCompilation* compilation = nullptr;
    616     EXPECT_EQ(ANeuralNetworksCompilation_create(nullptr, &compilation),
    617               ANEURALNETWORKS_UNEXPECTED_NULL);
    618     EXPECT_EQ(ANeuralNetworksCompilation_create(mModel, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
    619     EXPECT_EQ(ANeuralNetworksCompilation_create(mModel, &compilation), ANEURALNETWORKS_BAD_STATE);
    620 }
    621 
    622 TEST_F(ValidationTestModel, CreateCompilationForDevices) {
    623     createModel();
    624     uint32_t numDevices = 0;
    625     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
    626 
    627     if (numDevices > 0) {
    628         ANeuralNetworksDevice* device;
    629         EXPECT_EQ(ANeuralNetworks_getDevice(0, &device), ANEURALNETWORKS_NO_ERROR);
    630         ANeuralNetworksCompilation* compilation = nullptr;
    631         EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(nullptr, &device, 1, &compilation),
    632                   ANEURALNETWORKS_UNEXPECTED_NULL);
    633         EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, &device, 1, nullptr),
    634                   ANEURALNETWORKS_UNEXPECTED_NULL);
    635 
    636         // empty device list
    637         EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, &device, 0, &compilation),
    638                   ANEURALNETWORKS_BAD_DATA);
    639 
    640         // duplicate devices in the list.
    641         ANeuralNetworksDevice* invalidDevices[2] = {device, device};
    642         EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, invalidDevices, 2,
    643                                                               &compilation),
    644                   ANEURALNETWORKS_BAD_DATA);
    645         // nullptr in the list.
    646         invalidDevices[1] = nullptr;
    647         EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, invalidDevices, 2,
    648                                                               &compilation),
    649                   ANEURALNETWORKS_UNEXPECTED_NULL);
    650     }
    651 
    652     ANeuralNetworksCompilation* compilation = nullptr;
    653     EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(nullptr, nullptr, 1, &compilation),
    654               ANEURALNETWORKS_UNEXPECTED_NULL);
    655     EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, nullptr, 1, nullptr),
    656               ANEURALNETWORKS_UNEXPECTED_NULL);
    657     EXPECT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, nullptr, 1, &compilation),
    658               ANEURALNETWORKS_UNEXPECTED_NULL);
    659 }
    660 
    661 TEST_F(ValidationTestModel, GetSupportedOperationsForDevices) {
    662     createModel();
    663     uint32_t numDevices = 0;
    664     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
    665 
    666     bool supportedOps[20];
    667     ASSERT_LE(mNumOperations, sizeof(supportedOps) / sizeof(supportedOps[0]));
    668     if (numDevices > 0) {
    669         ANeuralNetworksDevice* device;
    670         EXPECT_EQ(ANeuralNetworks_getDevice(0, &device), ANEURALNETWORKS_NO_ERROR);
    671         EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(nullptr, &device, 1,
    672                                                                         supportedOps),
    673                   ANEURALNETWORKS_UNEXPECTED_NULL);
    674         EXPECT_EQ(
    675                 ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, &device, 1, nullptr),
    676                 ANEURALNETWORKS_UNEXPECTED_NULL);
    677 
    678         // empty device list
    679         EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, &device, 0,
    680                                                                         supportedOps),
    681                   ANEURALNETWORKS_BAD_DATA);
    682 
    683         // duplicate devices in the list.
    684         ANeuralNetworksDevice* invalidDevices[2] = {device, device};
    685         EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, invalidDevices, 2,
    686                                                                         supportedOps),
    687                   ANEURALNETWORKS_BAD_DATA);
    688         // nullptr in the list.
    689         invalidDevices[1] = nullptr;
    690         EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, invalidDevices, 2,
    691                                                                         supportedOps),
    692                   ANEURALNETWORKS_UNEXPECTED_NULL);
    693     }
    694 
    695     EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(nullptr, nullptr, 1,
    696                                                                     supportedOps),
    697               ANEURALNETWORKS_UNEXPECTED_NULL);
    698     EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, nullptr, 1, nullptr),
    699               ANEURALNETWORKS_UNEXPECTED_NULL);
    700     EXPECT_EQ(
    701             ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, nullptr, 1, supportedOps),
    702             ANEURALNETWORKS_UNEXPECTED_NULL);
    703 }
    704 
    705 TEST_F(ValidationTestIdentify, Ok) {
    706     uint32_t inList[3]{0, 1, 2};
    707     uint32_t outList[1]{3};
    708 
    709     ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 3, inList, 1, outList),
    710               ANEURALNETWORKS_NO_ERROR);
    711 
    712     ASSERT_EQ(ANeuralNetworksModel_finish(mModel), ANEURALNETWORKS_NO_ERROR);
    713 }
    714 
    715 TEST_F(ValidationTestIdentify, InputIsOutput) {
    716     uint32_t inList[3]{0, 1, 2};
    717     uint32_t outList[2]{3, 0};
    718 
    719     ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 3, inList, 2, outList),
    720               ANEURALNETWORKS_BAD_DATA);
    721 }
    722 
    723 TEST_F(ValidationTestIdentify, OutputIsInput) {
    724     uint32_t inList[4]{0, 1, 2, 3};
    725     uint32_t outList[1]{3};
    726 
    727     ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 4, inList, 1, outList),
    728               ANEURALNETWORKS_BAD_DATA);
    729 }
    730 
    731 TEST_F(ValidationTestIdentify, DuplicateInputs) {
    732     uint32_t inList[4]{0, 1, 2, 0};
    733     uint32_t outList[1]{3};
    734 
    735     ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 4, inList, 1, outList),
    736               ANEURALNETWORKS_BAD_DATA);
    737 }
    738 
    739 TEST_F(ValidationTestIdentify, DuplicateOutputs) {
    740     uint32_t inList[3]{0, 1, 2};
    741     uint32_t outList[2]{3, 3};
    742 
    743     ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 3, inList, 2, outList),
    744               ANEURALNETWORKS_BAD_DATA);
    745 }
    746 
    747 // Also see TEST_F(ValidationTestCompilationForDevices_1, SetPreference)
    748 TEST_F(ValidationTestCompilation, SetPreference) {
    749     EXPECT_EQ(ANeuralNetworksCompilation_setPreference(nullptr, ANEURALNETWORKS_PREFER_LOW_POWER),
    750               ANEURALNETWORKS_UNEXPECTED_NULL);
    751 
    752     EXPECT_EQ(ANeuralNetworksCompilation_setPreference(mCompilation, 40), ANEURALNETWORKS_BAD_DATA);
    753 }
    754 
    755 // Also see TEST_F(ValidationTestCompilationForDevices_1, SetCaching)
    756 TEST_F(ValidationTestCompilation, SetCaching) {
    757     std::vector<uint8_t> token(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, 0);
    758     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(nullptr, "/data/local/tmp", token.data()),
    759               ANEURALNETWORKS_UNEXPECTED_NULL);
    760     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(mCompilation, nullptr, token.data()),
    761               ANEURALNETWORKS_UNEXPECTED_NULL);
    762     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(mCompilation, "/data/local/tmp", nullptr),
    763               ANEURALNETWORKS_UNEXPECTED_NULL);
    764 }
    765 
    766 // Also see TEST_F(ValidationTestCompilationForDevices_1, CreateExecution)
    767 TEST_F(ValidationTestCompilation, CreateExecution) {
    768     ANeuralNetworksExecution* execution = nullptr;
    769     EXPECT_EQ(ANeuralNetworksExecution_create(nullptr, &execution),
    770               ANEURALNETWORKS_UNEXPECTED_NULL);
    771     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, nullptr),
    772               ANEURALNETWORKS_UNEXPECTED_NULL);
    773     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_BAD_STATE);
    774 }
    775 
    776 // Also see TEST_F(ValidationTestCompilationForDevices_1, Finish)
    777 TEST_F(ValidationTestCompilation, Finish) {
    778     EXPECT_EQ(ANeuralNetworksCompilation_finish(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
    779     EXPECT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
    780     EXPECT_EQ(ANeuralNetworksCompilation_setPreference(mCompilation,
    781                                                        ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER),
    782               ANEURALNETWORKS_BAD_STATE);
    783     std::vector<uint8_t> token(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, 0);
    784     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(mCompilation, "/data/local/tmp", token.data()),
    785               ANEURALNETWORKS_BAD_STATE);
    786     EXPECT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_BAD_STATE);
    787 }
    788 
    789 // Also see TEST_F(ValidationTestCompilationForDevices_1, ExecutionTiming)
    790 // Also see TEST_F(ValidationTestCompilationForDevices_2, ExecutionTiming)
    791 TEST_F(ValidationTestCompilation, ExecutionTiming) {
    792     ASSERT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
    793     ANeuralNetworksExecution* execution;
    794     ASSERT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
    795     // Cannot setMeasureTiming() with Compilation rather than CompilationForDevices.
    796     EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, false),
    797               ANEURALNETWORKS_BAD_DATA);
    798     EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, true), ANEURALNETWORKS_BAD_DATA);
    799 }
    800 
    801 // Also see TEST_F(ValidationTestCompilationForDevices_1, ExecutionTiming)
    802 TEST_F(ValidationTestCompilation, ExecutionUsability) {
    803     ASSERT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
    804 
    805     enum class ExecutionType : uint32_t { ASYNC, SYNC, BURST };
    806     for (auto executionType : {ExecutionType::ASYNC, ExecutionType::SYNC, ExecutionType::BURST}) {
    807         SCOPED_TRACE(static_cast<uint32_t>(executionType));
    808 
    809         ANeuralNetworksExecution* execution;
    810         ASSERT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution),
    811                   ANEURALNETWORKS_NO_ERROR);
    812 
    813         float in0[] = {0.0f, 0.0f}, in1[] = {1.0f, 1.0f}, out0[2];
    814         int in2 = 0;
    815         ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, &in0, sizeof(in0)),
    816                   ANEURALNETWORKS_NO_ERROR);
    817         ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 1, nullptr, &in1, sizeof(in1)),
    818                   ANEURALNETWORKS_NO_ERROR);
    819         ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 2, nullptr, &in2, sizeof(in2)),
    820                   ANEURALNETWORKS_NO_ERROR);
    821         ASSERT_EQ(ANeuralNetworksExecution_setOutput(execution, 0, nullptr, &out0, sizeof(out0)),
    822                   ANEURALNETWORKS_NO_ERROR);
    823 
    824         const size_t memorySize = std::max(sizeof(in0), sizeof(out0));
    825         int memoryFd = ASharedMemory_create("nnMemory", memorySize);
    826         ASSERT_GT(memoryFd, 0);
    827         ANeuralNetworksMemory* memory;
    828         EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, memoryFd,
    829                                                      0, &memory),
    830                   ANEURALNETWORKS_NO_ERROR);
    831 
    832         auto testTooLate = [this, execution, &in0, &out0, memory] {
    833             // Try a bunch of things that are impermissible if the execution has started.
    834 
    835             // Set inputs and outputs.
    836             ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, &in0, sizeof(in0)),
    837                       ANEURALNETWORKS_BAD_STATE);
    838             ASSERT_EQ(
    839                     ANeuralNetworksExecution_setOutput(execution, 0, nullptr, &out0, sizeof(out0)),
    840                     ANEURALNETWORKS_BAD_STATE);
    841             ASSERT_EQ(ANeuralNetworksExecution_setInputFromMemory(execution, 0, nullptr, memory, 0,
    842                                                                   sizeof(in0)),
    843                       ANEURALNETWORKS_BAD_STATE);
    844             ASSERT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, memory, 0,
    845                                                                    sizeof(out0)),
    846                       ANEURALNETWORKS_BAD_STATE);
    847 
    848             // Reuse for asynchronous execution.
    849             {
    850                 ANeuralNetworksEvent* event;
    851                 ASSERT_EQ(ANeuralNetworksExecution_startCompute(execution, &event),
    852                           ANEURALNETWORKS_BAD_STATE);
    853             }
    854 
    855             // Reuse for synchronous execution.
    856             ASSERT_EQ(ANeuralNetworksExecution_compute(execution), ANEURALNETWORKS_BAD_STATE);
    857 
    858             // Reuse for burst execution.
    859             {
    860                 ANeuralNetworksBurst* burst;
    861                 ASSERT_EQ(ANeuralNetworksBurst_create(mCompilation, &burst),
    862                           ANEURALNETWORKS_NO_ERROR);
    863                 ASSERT_EQ(ANeuralNetworksExecution_burstCompute(execution, burst),
    864                           ANEURALNETWORKS_BAD_STATE);
    865                 ANeuralNetworksBurst_free(burst);
    866             }
    867         };
    868 
    869         // Compute.
    870         switch (executionType) {
    871             case ExecutionType::ASYNC: {
    872                 ANeuralNetworksEvent* event;
    873                 ASSERT_EQ(ANeuralNetworksExecution_startCompute(execution, &event),
    874                           ANEURALNETWORKS_NO_ERROR);
    875                 testTooLate();
    876                 ASSERT_EQ(ANeuralNetworksEvent_wait(event), ANEURALNETWORKS_NO_ERROR);
    877                 testTooLate();
    878                 ANeuralNetworksEvent_free(event);
    879                 break;
    880             }
    881             case ExecutionType::SYNC: {
    882                 ASSERT_EQ(ANeuralNetworksExecution_compute(execution), ANEURALNETWORKS_NO_ERROR);
    883                 testTooLate();
    884                 break;
    885             }
    886             case ExecutionType::BURST: {
    887                 ANeuralNetworksBurst* burst;
    888                 ASSERT_EQ(ANeuralNetworksBurst_create(mCompilation, &burst),
    889                           ANEURALNETWORKS_NO_ERROR);
    890                 ASSERT_EQ(ANeuralNetworksExecution_burstCompute(execution, burst),
    891                           ANEURALNETWORKS_NO_ERROR);
    892                 testTooLate();
    893                 ANeuralNetworksBurst_free(burst);
    894                 break;
    895             }
    896             default:
    897                 FAIL() << "Unreachable";
    898         }
    899     }
    900 }
    901 
    902 TEST_F(ValidationTestExecution, SetInput) {
    903     char buffer[20];
    904     EXPECT_EQ(ANeuralNetworksExecution_setInput(nullptr, 0, nullptr, buffer, sizeof(float)),
    905               ANEURALNETWORKS_UNEXPECTED_NULL);
    906     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 0, nullptr, nullptr, sizeof(float)),
    907               ANEURALNETWORKS_UNEXPECTED_NULL);
    908 
    909     // This should fail, since memory is not the size of a float32.
    910     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 0, nullptr, buffer, 20),
    911               ANEURALNETWORKS_BAD_DATA);
    912 
    913     // This should fail, as this operand does not exist.
    914     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 999, nullptr, buffer, sizeof(float)),
    915               ANEURALNETWORKS_BAD_DATA);
    916 
    917     // This should fail, as this operand does not exist.
    918     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, -1, nullptr, buffer, sizeof(float)),
    919               ANEURALNETWORKS_BAD_DATA);
    920 
    921     // These should fail, since the tensor types are invalid.
    922     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 0, &kInvalidTensorType1, buffer,
    923                                                 sizeof(float)),
    924               ANEURALNETWORKS_BAD_DATA);
    925     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 0, &kInvalidTensorType2, buffer,
    926                                                 sizeof(float)),
    927               ANEURALNETWORKS_BAD_DATA);
    928 }
    929 
    930 TEST_F(ValidationTestExecution, SetOutput) {
    931     char buffer[20];
    932     EXPECT_EQ(ANeuralNetworksExecution_setOutput(nullptr, 0, nullptr, buffer, sizeof(float)),
    933               ANEURALNETWORKS_UNEXPECTED_NULL);
    934     EXPECT_EQ(ANeuralNetworksExecution_setOutput(mExecution, 0, nullptr, nullptr, sizeof(float)),
    935               ANEURALNETWORKS_UNEXPECTED_NULL);
    936 
    937     // This should fail, since memory is not the size of a float32.
    938     EXPECT_EQ(ANeuralNetworksExecution_setOutput(mExecution, 0, nullptr, buffer, 20),
    939               ANEURALNETWORKS_BAD_DATA);
    940 
    941     // This should fail, as this operand does not exist.
    942     EXPECT_EQ(ANeuralNetworksExecution_setOutput(mExecution, 999, nullptr, buffer, sizeof(float)),
    943               ANEURALNETWORKS_BAD_DATA);
    944 
    945     // This should fail, as this operand does not exist.
    946     EXPECT_EQ(ANeuralNetworksExecution_setOutput(mExecution, -1, nullptr, buffer, sizeof(float)),
    947               ANEURALNETWORKS_BAD_DATA);
    948 
    949     // These should fail, since the tensor types are invalid.
    950     EXPECT_EQ(ANeuralNetworksExecution_setOutput(mExecution, 0, &kInvalidTensorType1, buffer,
    951                                                  sizeof(float)),
    952               ANEURALNETWORKS_BAD_DATA);
    953     EXPECT_EQ(ANeuralNetworksExecution_setOutput(mExecution, 0, &kInvalidTensorType2, buffer,
    954                                                  sizeof(float)),
    955               ANEURALNETWORKS_BAD_DATA);
    956 }
    957 
    958 TEST_F(ValidationTestExecution, SetInputFromMemory) {
    959     const size_t memorySize = 20;
    960     int memoryFd = ASharedMemory_create("nnMemory", memorySize);
    961     ASSERT_GT(memoryFd, 0);
    962 
    963     ANeuralNetworksMemory* memory;
    964     EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, memoryFd, 0,
    965                                                  &memory),
    966               ANEURALNETWORKS_NO_ERROR);
    967 
    968     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(nullptr, 0, nullptr, memory, 0,
    969                                                           sizeof(float)),
    970               ANEURALNETWORKS_UNEXPECTED_NULL);
    971     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, nullptr, 0,
    972                                                           sizeof(float)),
    973               ANEURALNETWORKS_UNEXPECTED_NULL);
    974 
    975     // This should fail, since the operand does not exist.
    976     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 999, nullptr, memory, 0,
    977                                                           sizeof(float)),
    978               ANEURALNETWORKS_BAD_DATA);
    979 
    980     // This should fail, since the operand does not exist.
    981     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, -1, nullptr, memory, 0,
    982                                                           sizeof(float)),
    983               ANEURALNETWORKS_BAD_DATA);
    984 
    985     // This should fail, since memory is not the size of a float32.
    986     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, memory, 0,
    987                                                           memorySize),
    988               ANEURALNETWORKS_BAD_DATA);
    989 
    990     // This should fail, since offset is larger than memorySize.
    991     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, memory,
    992                                                           memorySize + 1, sizeof(float)),
    993               ANEURALNETWORKS_BAD_DATA);
    994 
    995     // This should fail, since requested size is larger than the memory.
    996     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, memory,
    997                                                           memorySize - 3, sizeof(float)),
    998               ANEURALNETWORKS_BAD_DATA);
    999 
   1000     // These should fail, since the tensor types are invalid.
   1001     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, &kInvalidTensorType1,
   1002                                                           memory, 0, sizeof(float)),
   1003               ANEURALNETWORKS_BAD_DATA);
   1004     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, &kInvalidTensorType2,
   1005                                                           memory, 0, sizeof(float)),
   1006               ANEURALNETWORKS_BAD_DATA);
   1007 
   1008     // close memory
   1009     close(memoryFd);
   1010 }
   1011 
   1012 TEST_F(ValidationTestExecution, SetInputFromAHardwareBufferBlob) {
   1013     const size_t memorySize = 20;
   1014 
   1015     AHardwareBuffer_Desc desc{
   1016             .width = memorySize,
   1017             .height = 1,
   1018             .layers = 1,
   1019             .format = AHARDWAREBUFFER_FORMAT_BLOB,
   1020             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
   1021     };
   1022 
   1023     AHardwareBuffer* buffer = nullptr;
   1024     ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
   1025 
   1026     ANeuralNetworksMemory* memory;
   1027     EXPECT_EQ(ANeuralNetworksMemory_createFromAHardwareBuffer(buffer, &memory),
   1028               ANEURALNETWORKS_NO_ERROR);
   1029 
   1030     // This should fail, since memory is not the size of a float32.
   1031     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, memory, 0,
   1032                                                           memorySize),
   1033               ANEURALNETWORKS_BAD_DATA);
   1034 
   1035     // This should fail, since offset is larger than memorySize.
   1036     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, memory,
   1037                                                           memorySize + 1, sizeof(float)),
   1038               ANEURALNETWORKS_BAD_DATA);
   1039     // This should fail, since requested size is larger than the memory.
   1040     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, nullptr, memory,
   1041                                                           memorySize - 3, sizeof(float)),
   1042               ANEURALNETWORKS_BAD_DATA);
   1043 
   1044     // These should fail, since the tensor types are invalid.
   1045     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, &kInvalidTensorType1,
   1046                                                           memory, 0, sizeof(float)),
   1047               ANEURALNETWORKS_BAD_DATA);
   1048     EXPECT_EQ(ANeuralNetworksExecution_setInputFromMemory(mExecution, 0, &kInvalidTensorType2,
   1049                                                           memory, 0, sizeof(float)),
   1050               ANEURALNETWORKS_BAD_DATA);
   1051 
   1052     AHardwareBuffer_release(buffer);
   1053 }
   1054 
   1055 TEST_F(ValidationTestExecution, SetOutputFromMemory) {
   1056     ANeuralNetworksExecution* execution;
   1057     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
   1058 
   1059     const size_t memorySize = 20;
   1060     int memoryFd = ASharedMemory_create("nnMemory", memorySize);
   1061     ASSERT_GT(memoryFd, 0);
   1062 
   1063     ANeuralNetworksMemory* memory;
   1064     EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, memoryFd, 0,
   1065                                                  &memory),
   1066               ANEURALNETWORKS_NO_ERROR);
   1067 
   1068     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(nullptr, 0, nullptr, memory, 0,
   1069                                                            sizeof(float)),
   1070               ANEURALNETWORKS_UNEXPECTED_NULL);
   1071     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, nullptr, 0,
   1072                                                            sizeof(float)),
   1073               ANEURALNETWORKS_UNEXPECTED_NULL);
   1074 
   1075     // This should fail, since the operand does not exist.
   1076     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 999, nullptr, memory, 0,
   1077                                                            sizeof(float)),
   1078               ANEURALNETWORKS_BAD_DATA);
   1079 
   1080     // This should fail, since the operand does not exist.
   1081     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, -1, nullptr, memory, 0,
   1082                                                            sizeof(float)),
   1083               ANEURALNETWORKS_BAD_DATA);
   1084 
   1085     // This should fail, since memory is not the size of a float32.
   1086     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, memory, 0,
   1087                                                            memorySize),
   1088               ANEURALNETWORKS_BAD_DATA);
   1089 
   1090     // This should fail, since offset is larger than memorySize.
   1091     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, memory,
   1092                                                            memorySize + 1, sizeof(float)),
   1093               ANEURALNETWORKS_BAD_DATA);
   1094 
   1095     // This should fail, since requested size is larger than the memory.
   1096     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, memory,
   1097                                                            memorySize - 3, sizeof(float)),
   1098               ANEURALNETWORKS_BAD_DATA);
   1099 
   1100     // These should fail, since the tensor types are invalid.
   1101     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, &kInvalidTensorType1,
   1102                                                            memory, 0, sizeof(float)),
   1103               ANEURALNETWORKS_BAD_DATA);
   1104     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, &kInvalidTensorType2,
   1105                                                            memory, 0, sizeof(float)),
   1106               ANEURALNETWORKS_BAD_DATA);
   1107 
   1108     // close memory
   1109     close(memoryFd);
   1110 }
   1111 
   1112 TEST_F(ValidationTestExecution, SetOutputFromAHardwareBufferBlob) {
   1113     const size_t memorySize = 20;
   1114 
   1115     AHardwareBuffer_Desc desc{
   1116             .width = memorySize,
   1117             .height = 1,
   1118             .layers = 1,
   1119             .format = AHARDWAREBUFFER_FORMAT_BLOB,
   1120             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
   1121     };
   1122 
   1123     AHardwareBuffer* buffer = nullptr;
   1124     ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
   1125 
   1126     ANeuralNetworksMemory* memory;
   1127     EXPECT_EQ(ANeuralNetworksMemory_createFromAHardwareBuffer(buffer, &memory),
   1128               ANEURALNETWORKS_NO_ERROR);
   1129 
   1130     // This should fail, since memory is not the size of a float32.
   1131     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(mExecution, 0, nullptr, memory, 0,
   1132                                                            memorySize),
   1133               ANEURALNETWORKS_BAD_DATA);
   1134 
   1135     // This should fail, since offset is larger than memorySize.
   1136     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(mExecution, 0, nullptr, memory,
   1137                                                            memorySize + 1, sizeof(float)),
   1138               ANEURALNETWORKS_BAD_DATA);
   1139 
   1140     // This should fail, since requested size is larger than the memory.
   1141     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(mExecution, 0, nullptr, memory,
   1142                                                            memorySize - 3, sizeof(float)),
   1143               ANEURALNETWORKS_BAD_DATA);
   1144 
   1145     // These should fail, since the tensor types are invalid.
   1146     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(mExecution, 0, &kInvalidTensorType1,
   1147                                                            memory, 0, sizeof(float)),
   1148               ANEURALNETWORKS_BAD_DATA);
   1149     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(mExecution, 0, &kInvalidTensorType2,
   1150                                                            memory, 0, sizeof(float)),
   1151               ANEURALNETWORKS_BAD_DATA);
   1152 
   1153     AHardwareBuffer_release(buffer);
   1154 }
   1155 
   1156 TEST_F(ValidationTestExecution, Compute) {
   1157     EXPECT_EQ(ANeuralNetworksExecution_compute(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1158 }
   1159 
   1160 TEST_F(ValidationTestExecution, StartCompute) {
   1161     ANeuralNetworksExecution* execution;
   1162     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
   1163 
   1164     ANeuralNetworksEvent* event;
   1165     EXPECT_EQ(ANeuralNetworksExecution_startCompute(nullptr, &event),
   1166               ANEURALNETWORKS_UNEXPECTED_NULL);
   1167     EXPECT_EQ(ANeuralNetworksExecution_startCompute(execution, nullptr),
   1168               ANEURALNETWORKS_UNEXPECTED_NULL);
   1169 }
   1170 
   1171 TEST_F(ValidationTestExecution, EventWait) {
   1172     EXPECT_EQ(ANeuralNetworksEvent_wait(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1173 }
   1174 
   1175 TEST_F(ValidationTestExecution, GetOutputOperandRankAndDimensions) {
   1176     ANeuralNetworksExecution* execution;
   1177     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
   1178 
   1179     float input0[] = {1.0f, 1.0f}, input1[] = {2.0f, 2.0f}, output0[2];
   1180     int32_t input2[] = {0};
   1181     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, input0, sizeof(input0)),
   1182               ANEURALNETWORKS_NO_ERROR);
   1183     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 1, nullptr, input1, sizeof(input1)),
   1184               ANEURALNETWORKS_NO_ERROR);
   1185     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 2, nullptr, input2, sizeof(input2)),
   1186               ANEURALNETWORKS_NO_ERROR);
   1187     EXPECT_EQ(ANeuralNetworksExecution_setOutput(execution, 0, nullptr, output0, sizeof(output0)),
   1188               ANEURALNETWORKS_NO_ERROR);
   1189 
   1190     uint32_t rank, dims[4], expectedRank = 1, expectedDims = 2;
   1191     // This should fail, since the execution has not yet started to compute.
   1192     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandRank(execution, 0, &rank),
   1193               ANEURALNETWORKS_BAD_STATE);
   1194     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandDimensions(execution, 0, dims),
   1195               ANEURALNETWORKS_BAD_STATE);
   1196 
   1197     ANeuralNetworksEvent* event;
   1198     EXPECT_EQ(ANeuralNetworksExecution_startCompute(execution, &event), ANEURALNETWORKS_NO_ERROR);
   1199     EXPECT_EQ(ANeuralNetworksEvent_wait(event), ANEURALNETWORKS_NO_ERROR);
   1200 
   1201     // This should fail, since unexpected nullptr.
   1202     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandRank(nullptr, 0, &rank),
   1203               ANEURALNETWORKS_UNEXPECTED_NULL);
   1204     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandDimensions(nullptr, 0, dims),
   1205               ANEURALNETWORKS_UNEXPECTED_NULL);
   1206     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandRank(execution, 0, nullptr),
   1207               ANEURALNETWORKS_UNEXPECTED_NULL);
   1208     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandDimensions(execution, 0, nullptr),
   1209               ANEURALNETWORKS_UNEXPECTED_NULL);
   1210 
   1211     // This should fail, since the operand does not exist.
   1212     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandRank(execution, -1, &rank),
   1213               ANEURALNETWORKS_BAD_DATA);
   1214     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandRank(execution, 999, &rank),
   1215               ANEURALNETWORKS_BAD_DATA);
   1216     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandDimensions(execution, -1, dims),
   1217               ANEURALNETWORKS_BAD_DATA);
   1218     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandDimensions(execution, 999, dims),
   1219               ANEURALNETWORKS_BAD_DATA);
   1220 
   1221     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandRank(execution, 0, &rank),
   1222               ANEURALNETWORKS_NO_ERROR);
   1223     EXPECT_EQ(ANeuralNetworksExecution_getOutputOperandDimensions(execution, 0, dims),
   1224               ANEURALNETWORKS_NO_ERROR);
   1225     EXPECT_EQ(rank, expectedRank);
   1226     EXPECT_EQ(dims[0], expectedDims);
   1227 }
   1228 
   1229 TEST_F(ValidationTestBurst, BurstComputeNull) {
   1230     EXPECT_EQ(ANeuralNetworksExecution_burstCompute(mExecution, nullptr),
   1231               ANEURALNETWORKS_UNEXPECTED_NULL);
   1232     EXPECT_EQ(ANeuralNetworksExecution_burstCompute(nullptr, mBurst),
   1233               ANEURALNETWORKS_UNEXPECTED_NULL);
   1234 }
   1235 
   1236 TEST_F(ValidationTestBurst, BurstComputeBadCompilation) {
   1237     ANeuralNetworksCompilation* compilation;
   1238     ASSERT_EQ(ANeuralNetworksCompilation_create(mModel, &compilation), ANEURALNETWORKS_NO_ERROR);
   1239     // NOTE: ANeuralNetworksCompilation_finish not called
   1240 
   1241     ANeuralNetworksBurst* burst;
   1242     EXPECT_EQ(ANeuralNetworksBurst_create(compilation, &burst), ANEURALNETWORKS_BAD_STATE);
   1243 }
   1244 
   1245 TEST_F(ValidationTestBurst, BurstComputeDifferentCompilations) {
   1246     ANeuralNetworksCompilation* secondCompilation;
   1247     ASSERT_EQ(ANeuralNetworksCompilation_create(mModel, &secondCompilation),
   1248               ANEURALNETWORKS_NO_ERROR);
   1249     ASSERT_EQ(ANeuralNetworksCompilation_finish(secondCompilation), ANEURALNETWORKS_NO_ERROR);
   1250 
   1251     ANeuralNetworksExecution* execution;
   1252     EXPECT_EQ(ANeuralNetworksExecution_create(secondCompilation, &execution),
   1253               ANEURALNETWORKS_NO_ERROR);
   1254 
   1255     EXPECT_EQ(ANeuralNetworksExecution_burstCompute(execution, mBurst), ANEURALNETWORKS_BAD_DATA);
   1256 
   1257     ANeuralNetworksExecution_free(execution);
   1258     ANeuralNetworksCompilation_free(secondCompilation);
   1259 }
   1260 
   1261 TEST_F(ValidationTestBurst, BurstComputeConcurrent) {
   1262     ANeuralNetworksExecution* secondExecution;
   1263     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &secondExecution),
   1264               ANEURALNETWORKS_NO_ERROR);
   1265 
   1266     // set inputs of first execution
   1267     float inputA0[] = {1.0f, 1.0f}, inputA1[] = {2.0f, 2.0f}, outputA0[2];
   1268     int32_t inputA2[] = {0};
   1269     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 0, nullptr, inputA0, sizeof(inputA0)),
   1270               ANEURALNETWORKS_NO_ERROR);
   1271     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 1, nullptr, inputA1, sizeof(inputA1)),
   1272               ANEURALNETWORKS_NO_ERROR);
   1273     EXPECT_EQ(ANeuralNetworksExecution_setInput(mExecution, 2, nullptr, inputA2, sizeof(inputA2)),
   1274               ANEURALNETWORKS_NO_ERROR);
   1275     EXPECT_EQ(
   1276             ANeuralNetworksExecution_setOutput(mExecution, 0, nullptr, outputA0, sizeof(outputA0)),
   1277             ANEURALNETWORKS_NO_ERROR);
   1278 
   1279     // set inputs of second execution
   1280     float inputB0[] = {1.0f, 1.0f}, inputB1[] = {2.0f, 2.0f}, outputB0[2];
   1281     int32_t inputB2[] = {0};
   1282     EXPECT_EQ(ANeuralNetworksExecution_setInput(secondExecution, 0, nullptr, inputB0,
   1283                                                 sizeof(inputB0)),
   1284               ANEURALNETWORKS_NO_ERROR);
   1285     EXPECT_EQ(ANeuralNetworksExecution_setInput(secondExecution, 1, nullptr, inputB1,
   1286                                                 sizeof(inputB1)),
   1287               ANEURALNETWORKS_NO_ERROR);
   1288     EXPECT_EQ(ANeuralNetworksExecution_setInput(secondExecution, 2, nullptr, inputB2,
   1289                                                 sizeof(inputB2)),
   1290               ANEURALNETWORKS_NO_ERROR);
   1291     EXPECT_EQ(ANeuralNetworksExecution_setOutput(secondExecution, 0, nullptr, outputB0,
   1292                                                  sizeof(outputB0)),
   1293               ANEURALNETWORKS_NO_ERROR);
   1294 
   1295     // Execute on the same burst concurrently. At least one result must be
   1296     // ANEURALNETWORKS_NO_ERROR. One may return ANEURALNETWORKS_BAD_STATE if the
   1297     // other is already executing on the burst.
   1298     auto first = std::async(std::launch::async, [this] {
   1299         return ANeuralNetworksExecution_burstCompute(mExecution, mBurst);
   1300     });
   1301     auto second = std::async(std::launch::async, [this, secondExecution] {
   1302         return ANeuralNetworksExecution_burstCompute(secondExecution, mBurst);
   1303     });
   1304 
   1305     const int result1 = first.get();
   1306     const int result2 = second.get();
   1307     EXPECT_TRUE(result1 == ANEURALNETWORKS_BAD_STATE || result1 == ANEURALNETWORKS_NO_ERROR);
   1308     EXPECT_TRUE(result2 == ANEURALNETWORKS_BAD_STATE || result2 == ANEURALNETWORKS_NO_ERROR);
   1309     EXPECT_TRUE(result1 == ANEURALNETWORKS_NO_ERROR || result2 == ANEURALNETWORKS_NO_ERROR);
   1310 
   1311     ANeuralNetworksExecution_free(secondExecution);
   1312 }
   1313 
   1314 // The burst object maintains a local cache of memory objects. Because the burst
   1315 // is intended to live for multiple executions, and because memory might be
   1316 // created and freed for each execution, burst includes internal mechanisms to
   1317 // purge memory objects from its cache that have been freed by the NNAPI client.
   1318 // The following two test cases (FreeMemoryBeforeBurst and
   1319 // FreeBurstBeforeMemory) ensure that this internal cleanup is tested in both
   1320 // freeing orders.
   1321 //
   1322 // These two test cases explicitly create a new burst object and a new execution
   1323 // object so that the order of freeing can be specified. If these tests instead
   1324 // relied on the provided mExecution and mBurst, mBurst would always be freed
   1325 // before mExecution.
   1326 
   1327 TEST_F(ValidationTestBurst, FreeMemoryBeforeBurst) {
   1328     ANeuralNetworksBurst* burst;
   1329     EXPECT_EQ(ANeuralNetworksBurst_create(mCompilation, &burst), ANEURALNETWORKS_NO_ERROR);
   1330 
   1331     // prepare data for execution
   1332     float input0[] = {1.0f, 1.0f}, input1[] = {2.0f, 2.0f}, output0[2];
   1333     int32_t input2[] = {0};
   1334 
   1335     const size_t memorySize = sizeof(output0);
   1336     int memoryFd = ASharedMemory_create("nnMemory", memorySize);
   1337     ASSERT_GT(memoryFd, 0);
   1338 
   1339     ANeuralNetworksMemory* memory;
   1340     EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, memoryFd, 0,
   1341                                                  &memory),
   1342               ANEURALNETWORKS_NO_ERROR);
   1343 
   1344     // create and configure execution
   1345     ANeuralNetworksExecution* execution;
   1346     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
   1347     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, input0, sizeof(input0)),
   1348               ANEURALNETWORKS_NO_ERROR);
   1349     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 1, nullptr, input1, sizeof(input1)),
   1350               ANEURALNETWORKS_NO_ERROR);
   1351     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 2, nullptr, input2, sizeof(input2)),
   1352               ANEURALNETWORKS_NO_ERROR);
   1353     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, memory, 0,
   1354                                                            sizeof(output0)),
   1355               ANEURALNETWORKS_NO_ERROR);
   1356 
   1357     // preform execution to cache memory into burst
   1358     EXPECT_EQ(ANeuralNetworksExecution_burstCompute(execution, burst), ANEURALNETWORKS_NO_ERROR);
   1359     ANeuralNetworksExecution_free(execution);
   1360 
   1361     // free memory before burst
   1362     ANeuralNetworksMemory_free(memory);
   1363     ANeuralNetworksBurst_free(burst);
   1364 
   1365     // close memory
   1366     close(memoryFd);
   1367 }
   1368 
   1369 TEST_F(ValidationTestBurst, FreeBurstBeforeMemory) {
   1370     ANeuralNetworksBurst* burst;
   1371     EXPECT_EQ(ANeuralNetworksBurst_create(mCompilation, &burst), ANEURALNETWORKS_NO_ERROR);
   1372 
   1373     // prepare data for execution
   1374     float input0[] = {1.0f, 1.0f}, input1[] = {2.0f, 2.0f}, output0[2];
   1375     int32_t input2[] = {0};
   1376     const size_t memorySize = sizeof(output0);
   1377     int memoryFd = ASharedMemory_create("nnMemory", memorySize);
   1378     ASSERT_GT(memoryFd, 0);
   1379 
   1380     ANeuralNetworksMemory* memory;
   1381     EXPECT_EQ(ANeuralNetworksMemory_createFromFd(memorySize, PROT_READ | PROT_WRITE, memoryFd, 0,
   1382                                                  &memory),
   1383               ANEURALNETWORKS_NO_ERROR);
   1384 
   1385     // create and configure execution
   1386     ANeuralNetworksExecution* execution;
   1387     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
   1388     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, input0, sizeof(input0)),
   1389               ANEURALNETWORKS_NO_ERROR);
   1390     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 1, nullptr, input1, sizeof(input1)),
   1391               ANEURALNETWORKS_NO_ERROR);
   1392     EXPECT_EQ(ANeuralNetworksExecution_setInput(execution, 2, nullptr, input2, sizeof(input2)),
   1393               ANEURALNETWORKS_NO_ERROR);
   1394     EXPECT_EQ(ANeuralNetworksExecution_setOutputFromMemory(execution, 0, nullptr, memory, 0,
   1395                                                            sizeof(output0)),
   1396               ANEURALNETWORKS_NO_ERROR);
   1397 
   1398     // preform execution to cache memory into burst
   1399     EXPECT_EQ(ANeuralNetworksExecution_burstCompute(execution, burst), ANEURALNETWORKS_NO_ERROR);
   1400     ANeuralNetworksExecution_free(execution);
   1401 
   1402     // free burst before memory
   1403     ANeuralNetworksBurst_free(burst);
   1404     ANeuralNetworksMemory_free(memory);
   1405 
   1406     // close memory
   1407     close(memoryFd);
   1408 }
   1409 
   1410 TEST(ValidationTestIntrospection, GetNumDevices) {
   1411     uint32_t numDevices = 0;
   1412     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1413     EXPECT_EQ(ANeuralNetworks_getDeviceCount(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1414 }
   1415 
   1416 TEST(ValidationTestIntrospection, GetDevice) {
   1417     uint32_t numDevices = 0;
   1418     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1419 
   1420     ANeuralNetworksDevice* device = nullptr;
   1421     for (uint32_t i = 0; i < numDevices; i++) {
   1422         SCOPED_TRACE(i);
   1423         EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1424         EXPECT_NE(device, nullptr);
   1425     }
   1426     EXPECT_EQ(ANeuralNetworks_getDevice(0, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1427     EXPECT_EQ(ANeuralNetworks_getDevice(numDevices, &device), ANEURALNETWORKS_BAD_DATA);
   1428 }
   1429 
   1430 static void deviceStringCheck(std::function<int(const ANeuralNetworksDevice*, const char**)> func) {
   1431     uint32_t numDevices = 0;
   1432     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1433 
   1434     const char* buffer;
   1435     for (uint32_t i = 0; i < numDevices; i++) {
   1436         SCOPED_TRACE(i);
   1437         ANeuralNetworksDevice* device;
   1438         EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1439         EXPECT_EQ(func(device, &buffer), ANEURALNETWORKS_NO_ERROR);
   1440         EXPECT_EQ(func(device, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1441     }
   1442     EXPECT_EQ(func(nullptr, &buffer), ANEURALNETWORKS_UNEXPECTED_NULL);
   1443     EXPECT_EQ(func(nullptr, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1444 }
   1445 
   1446 TEST(ValidationTestIntrospection, DeviceGetName) {
   1447     deviceStringCheck(ANeuralNetworksDevice_getName);
   1448 }
   1449 
   1450 TEST(ValidationTestIntrospection, DeviceGetNameUnique) {
   1451     uint32_t numDevices = 0;
   1452     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1453 
   1454     std::set<std::string> deviceNames;
   1455     for (uint32_t i = 0; i < numDevices; i++) {
   1456         ANeuralNetworksDevice* device = nullptr;
   1457         EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1458         const char* buffer = nullptr;
   1459         EXPECT_EQ(ANeuralNetworksDevice_getName(device, &buffer), ANEURALNETWORKS_NO_ERROR);
   1460         std::string name(buffer);
   1461         EXPECT_EQ(deviceNames.count(name), (uint32_t)0);
   1462         deviceNames.insert(name);
   1463     }
   1464 }
   1465 
   1466 TEST(ValidationTestIntrospection, DeviceGetVersion) {
   1467     deviceStringCheck(ANeuralNetworksDevice_getVersion);
   1468 }
   1469 
   1470 TEST(ValidationTestIntrospection, DeviceGetFeatureLevel) {
   1471     uint32_t numDevices = 0;
   1472     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1473 
   1474     int64_t featureLevel;
   1475     for (uint32_t i = 0; i < numDevices; i++) {
   1476         SCOPED_TRACE(i);
   1477         ANeuralNetworksDevice* device;
   1478         EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1479         EXPECT_EQ(ANeuralNetworksDevice_getFeatureLevel(device, &featureLevel),
   1480                   ANEURALNETWORKS_NO_ERROR);
   1481         EXPECT_EQ(ANeuralNetworksDevice_getFeatureLevel(device, nullptr),
   1482                   ANEURALNETWORKS_UNEXPECTED_NULL);
   1483     }
   1484     EXPECT_EQ(ANeuralNetworksDevice_getFeatureLevel(nullptr, &featureLevel),
   1485               ANEURALNETWORKS_UNEXPECTED_NULL);
   1486     EXPECT_EQ(ANeuralNetworksDevice_getFeatureLevel(nullptr, nullptr),
   1487               ANEURALNETWORKS_UNEXPECTED_NULL);
   1488 }
   1489 
   1490 TEST(ValidationTestIntrospection, DeviceGetType) {
   1491     uint32_t numDevices = 0;
   1492     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1493 
   1494     int32_t validTypes[] = {ANEURALNETWORKS_DEVICE_UNKNOWN, ANEURALNETWORKS_DEVICE_OTHER,
   1495                             ANEURALNETWORKS_DEVICE_CPU, ANEURALNETWORKS_DEVICE_GPU,
   1496                             ANEURALNETWORKS_DEVICE_ACCELERATOR};
   1497     int32_t deviceType;
   1498     for (uint32_t i = 0; i < numDevices; i++) {
   1499         SCOPED_TRACE(i);
   1500         // Initialize the deviceType to be an invalid type.
   1501         deviceType = -1;
   1502         ANeuralNetworksDevice* device;
   1503         EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1504         EXPECT_EQ(ANeuralNetworksDevice_getType(device, &deviceType), ANEURALNETWORKS_NO_ERROR);
   1505         EXPECT_TRUE(std::find(std::begin(validTypes), std::end(validTypes), deviceType) !=
   1506                     std::end(validTypes));
   1507         EXPECT_EQ(ANeuralNetworksDevice_getType(device, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1508     }
   1509     EXPECT_EQ(ANeuralNetworksDevice_getType(nullptr, &deviceType), ANEURALNETWORKS_UNEXPECTED_NULL);
   1510     EXPECT_EQ(ANeuralNetworksDevice_getType(nullptr, nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1511 }
   1512 
   1513 class ValidationTestCompilationForDevices_1 : public ValidationTestModel {
   1514    protected:
   1515     virtual void SetUp() override {
   1516         ValidationTestModel::SetUp();
   1517         createModel();
   1518 
   1519         uint32_t numDevices = 0;
   1520         EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1521 
   1522         if (numDevices > 0) {
   1523             EXPECT_EQ(ANeuralNetworks_getDevice(0, &mDevice), ANEURALNETWORKS_NO_ERROR);
   1524             bool supported = false;
   1525             ASSERT_EQ(mNumOperations, static_cast<uint32_t>(1));
   1526             EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, &mDevice, 1,
   1527                                                                             &supported),
   1528                       ANEURALNETWORKS_NO_ERROR);
   1529             if (supported) {
   1530                 ASSERT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, &mDevice, 1,
   1531                                                                       &mCompilation),
   1532                           ANEURALNETWORKS_NO_ERROR);
   1533             }
   1534         }
   1535     }
   1536 
   1537     virtual void TearDown() {
   1538         ANeuralNetworksCompilation_free(mCompilation);
   1539         ValidationTestModel::TearDown();
   1540     }
   1541 
   1542     ANeuralNetworksDevice* mDevice = nullptr;
   1543     ANeuralNetworksCompilation* mCompilation = nullptr;
   1544 };
   1545 
   1546 // Also see TEST_F(ValidationTestCompilation, SetPreference)
   1547 TEST_F(ValidationTestCompilationForDevices_1, SetPreference) {
   1548     EXPECT_EQ(ANeuralNetworksCompilation_setPreference(nullptr, ANEURALNETWORKS_PREFER_LOW_POWER),
   1549               ANEURALNETWORKS_UNEXPECTED_NULL);
   1550     if (!mCompilation) {
   1551         return;
   1552     }
   1553     EXPECT_EQ(ANeuralNetworksCompilation_setPreference(mCompilation, 40), ANEURALNETWORKS_BAD_DATA);
   1554 }
   1555 
   1556 // Also see TEST_F(ValidationTestCompilation, SetCaching)
   1557 TEST_F(ValidationTestCompilationForDevices_1, SetCaching) {
   1558     std::vector<uint8_t> token(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, 0);
   1559     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(nullptr, "/data/local/tmp", token.data()),
   1560               ANEURALNETWORKS_UNEXPECTED_NULL);
   1561     if (!mCompilation) {
   1562         return;
   1563     }
   1564     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(mCompilation, nullptr, token.data()),
   1565               ANEURALNETWORKS_UNEXPECTED_NULL);
   1566     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(mCompilation, "/data/local/tmp", nullptr),
   1567               ANEURALNETWORKS_UNEXPECTED_NULL);
   1568 }
   1569 
   1570 // Also see TEST_F(ValidationTestCompilation, CreateExecution)
   1571 TEST_F(ValidationTestCompilationForDevices_1, CreateExecution) {
   1572     ANeuralNetworksExecution* execution = nullptr;
   1573     EXPECT_EQ(ANeuralNetworksExecution_create(nullptr, &execution),
   1574               ANEURALNETWORKS_UNEXPECTED_NULL);
   1575     if (!mCompilation) {
   1576         return;
   1577     }
   1578     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, nullptr),
   1579               ANEURALNETWORKS_UNEXPECTED_NULL);
   1580     EXPECT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_BAD_STATE);
   1581 }
   1582 
   1583 // Also see TEST_F(ValidationTestCompilation, Finish)
   1584 TEST_F(ValidationTestCompilationForDevices_1, Finish) {
   1585     EXPECT_EQ(ANeuralNetworksCompilation_finish(nullptr), ANEURALNETWORKS_UNEXPECTED_NULL);
   1586     if (!mCompilation) {
   1587         return;
   1588     }
   1589     EXPECT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
   1590     EXPECT_EQ(ANeuralNetworksCompilation_setPreference(mCompilation,
   1591                                                        ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER),
   1592               ANEURALNETWORKS_BAD_STATE);
   1593     std::vector<uint8_t> token(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, 0);
   1594     EXPECT_EQ(ANeuralNetworksCompilation_setCaching(mCompilation, "/data/local/tmp", token.data()),
   1595               ANEURALNETWORKS_BAD_STATE);
   1596     EXPECT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_BAD_STATE);
   1597 }
   1598 
   1599 class ValidationTestCompilationForDevices_2 : public ValidationTestModel {
   1600    protected:
   1601     virtual void SetUp() override {
   1602         ValidationTestModel::SetUp();
   1603         createModel();
   1604 
   1605         uint32_t numDevices = 0;
   1606         EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1607 
   1608         if (numDevices > 1) {
   1609             EXPECT_EQ(ANeuralNetworks_getDevice(0, &mDevices[0]), ANEURALNETWORKS_NO_ERROR);
   1610             EXPECT_EQ(ANeuralNetworks_getDevice(1, &mDevices[1]), ANEURALNETWORKS_NO_ERROR);
   1611             bool supported = false;
   1612             ASSERT_EQ(mNumOperations, static_cast<uint32_t>(1));
   1613             EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, mDevices, 2,
   1614                                                                             &supported),
   1615                       ANEURALNETWORKS_NO_ERROR);
   1616             if (supported) {
   1617                 ASSERT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, mDevices, 2,
   1618                                                                       &mCompilation),
   1619                           ANEURALNETWORKS_NO_ERROR);
   1620             }
   1621         }
   1622     }
   1623 
   1624     virtual void TearDown() {
   1625         ANeuralNetworksCompilation_free(mCompilation);
   1626         ValidationTestModel::TearDown();
   1627     }
   1628 
   1629     ANeuralNetworksDevice* mDevices[2] = {nullptr, nullptr};
   1630     ANeuralNetworksCompilation* mCompilation = nullptr;
   1631 };
   1632 
   1633 // Also see TEST_F(ValidationTestCompilation, ExecutionTiming)
   1634 // Also see TEST_F(ValidationTestCompilationForDevices_1, ExecutionTiming)
   1635 TEST_F(ValidationTestCompilationForDevices_2, ExecutionTiming) {
   1636     if (!mCompilation) {
   1637         return;
   1638     }
   1639     ASSERT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
   1640     ANeuralNetworksExecution* execution;
   1641     ASSERT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution), ANEURALNETWORKS_NO_ERROR);
   1642     // Cannot setMeasureTiming() if there are two or more devices.
   1643     EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, false),
   1644               ANEURALNETWORKS_BAD_DATA);
   1645     EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, true), ANEURALNETWORKS_BAD_DATA);
   1646 }
   1647 
   1648 class ValidationTestInvalidCompilation : public ValidationTestModel {
   1649    protected:
   1650     virtual void SetUp() override {
   1651         ValidationTestModel::SetUp();
   1652 
   1653         // Create a model with an OEM operation
   1654         uint32_t dimensions[]{1};
   1655         ANeuralNetworksOperandType OEMTensorType{.type = ANEURALNETWORKS_TENSOR_OEM_BYTE,
   1656                                                  .dimensionCount = 1,
   1657                                                  .dimensions = dimensions};
   1658         EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &OEMTensorType),
   1659                   ANEURALNETWORKS_NO_ERROR);
   1660         EXPECT_EQ(ANeuralNetworksModel_addOperand(mModel, &OEMTensorType),
   1661                   ANEURALNETWORKS_NO_ERROR);
   1662         uint32_t inList[1]{0};
   1663         uint32_t outList[1]{1};
   1664         ASSERT_EQ(ANeuralNetworksModel_addOperation(mModel, ANEURALNETWORKS_OEM_OPERATION, 1,
   1665                                                     inList, 1, outList),
   1666                   ANEURALNETWORKS_NO_ERROR);
   1667         ASSERT_EQ(ANeuralNetworksModel_identifyInputsAndOutputs(mModel, 1, inList, 1, outList),
   1668                   ANEURALNETWORKS_NO_ERROR);
   1669         ASSERT_EQ(ANeuralNetworksModel_finish(mModel), ANEURALNETWORKS_NO_ERROR);
   1670 
   1671         // Find a device that cannot handle OEM operation and create compilation on that
   1672         uint32_t numDevices = 0;
   1673         EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1674         for (uint32_t i = 0; i < numDevices; i++) {
   1675             ANeuralNetworksDevice* device;
   1676             EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1677             bool supported = false;
   1678             EXPECT_EQ(ANeuralNetworksModel_getSupportedOperationsForDevices(mModel, &device, 1,
   1679                                                                             &supported),
   1680                       ANEURALNETWORKS_NO_ERROR);
   1681             if (!supported) {
   1682                 ASSERT_EQ(ANeuralNetworksCompilation_createForDevices(mModel, &device, 1,
   1683                                                                       &mInvalidCompilation),
   1684                           ANEURALNETWORKS_NO_ERROR);
   1685                 break;
   1686             }
   1687         }
   1688     }
   1689 
   1690     virtual void TearDown() {
   1691         ANeuralNetworksCompilation_free(mInvalidCompilation);
   1692         ValidationTestModel::TearDown();
   1693     }
   1694 
   1695     ANeuralNetworksCompilation* mInvalidCompilation = nullptr;
   1696 };
   1697 
   1698 TEST_F(ValidationTestInvalidCompilation, CreateExecutionWithInvalidCompilation) {
   1699     if (!mInvalidCompilation) {
   1700         return;
   1701     }
   1702     ASSERT_EQ(ANeuralNetworksCompilation_finish(mInvalidCompilation), ANEURALNETWORKS_BAD_DATA);
   1703     ANeuralNetworksExecution* execution = nullptr;
   1704     EXPECT_EQ(ANeuralNetworksExecution_create(mInvalidCompilation, &execution),
   1705               ANEURALNETWORKS_BAD_STATE);
   1706 }
   1707 
   1708 // Also see TEST_F(ValidationTestCompilation, ExecutionTiming)
   1709 // Also see TEST_F(ValidationTestCompilationForDevices_2, ExecutionTiming)
   1710 // Also see TEST_F(ValidationTestCompilation, ExecutionUsability)
   1711 TEST_F(ValidationTestCompilationForDevices_1, ExecutionTiming) {
   1712     if (!mCompilation) {
   1713         return;
   1714     }
   1715     ASSERT_EQ(ANeuralNetworksCompilation_finish(mCompilation), ANEURALNETWORKS_NO_ERROR);
   1716 
   1717     enum class ExecutionType : uint32_t { ASYNC, SYNC, BURST };
   1718     for (auto executionType : {ExecutionType::ASYNC, ExecutionType::SYNC, ExecutionType::BURST}) {
   1719         SCOPED_TRACE(static_cast<uint32_t>(executionType));
   1720 
   1721         ANeuralNetworksExecution* execution;
   1722         ASSERT_EQ(ANeuralNetworksExecution_create(mCompilation, &execution),
   1723                   ANEURALNETWORKS_NO_ERROR);
   1724 
   1725         EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(nullptr, false),
   1726                   ANEURALNETWORKS_UNEXPECTED_NULL);
   1727         EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(nullptr, true),
   1728                   ANEURALNETWORKS_UNEXPECTED_NULL);
   1729         EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, false),
   1730                   ANEURALNETWORKS_NO_ERROR);
   1731         EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, true),
   1732                   ANEURALNETWORKS_NO_ERROR);
   1733 
   1734         float in0[] = {0.0f, 0.0f}, in1[] = {1.0f, 1.0f}, out0[2];
   1735         int in2 = 0;
   1736         ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 0, nullptr, &in0, sizeof(in0)),
   1737                   ANEURALNETWORKS_NO_ERROR);
   1738         ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 1, nullptr, &in1, sizeof(in1)),
   1739                   ANEURALNETWORKS_NO_ERROR);
   1740         ASSERT_EQ(ANeuralNetworksExecution_setInput(execution, 2, nullptr, &in2, sizeof(in2)),
   1741                   ANEURALNETWORKS_NO_ERROR);
   1742         ASSERT_EQ(ANeuralNetworksExecution_setOutput(execution, 0, nullptr, &out0, sizeof(out0)),
   1743                   ANEURALNETWORKS_NO_ERROR);
   1744 
   1745         // Cannot getDuration until the execution has finished.
   1746         uint64_t duration;
   1747         EXPECT_EQ(ANeuralNetworksExecution_getDuration(
   1748                           execution, ANEURALNETWORKS_DURATION_ON_HARDWARE, &duration),
   1749                   ANEURALNETWORKS_BAD_STATE);
   1750         EXPECT_EQ(ANeuralNetworksExecution_getDuration(
   1751                           execution, ANEURALNETWORKS_DURATION_IN_DRIVER, &duration),
   1752                   ANEURALNETWORKS_BAD_STATE);
   1753 
   1754         auto testMeasureTooLate = [execution] {
   1755             // Cannot setMeasureTiming if the execution has started.
   1756             EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, false),
   1757                       ANEURALNETWORKS_BAD_STATE);
   1758             EXPECT_EQ(ANeuralNetworksExecution_setMeasureTiming(execution, true),
   1759                       ANEURALNETWORKS_BAD_STATE);
   1760         };
   1761 
   1762         // Compute.
   1763         switch (executionType) {
   1764             case ExecutionType::ASYNC: {
   1765                 ANeuralNetworksEvent* event;
   1766                 ASSERT_EQ(ANeuralNetworksExecution_startCompute(execution, &event),
   1767                           ANEURALNETWORKS_NO_ERROR);
   1768                 testMeasureTooLate();
   1769                 ASSERT_EQ(ANeuralNetworksEvent_wait(event), ANEURALNETWORKS_NO_ERROR);
   1770                 testMeasureTooLate();
   1771                 ANeuralNetworksEvent_free(event);
   1772                 break;
   1773             }
   1774             case ExecutionType::SYNC: {
   1775                 ASSERT_EQ(ANeuralNetworksExecution_compute(execution), ANEURALNETWORKS_NO_ERROR);
   1776                 testMeasureTooLate();
   1777                 break;
   1778             }
   1779             case ExecutionType::BURST: {
   1780                 ANeuralNetworksBurst* burst;
   1781                 ASSERT_EQ(ANeuralNetworksBurst_create(mCompilation, &burst),
   1782                           ANEURALNETWORKS_NO_ERROR);
   1783                 ASSERT_EQ(ANeuralNetworksExecution_burstCompute(execution, burst),
   1784                           ANEURALNETWORKS_NO_ERROR);
   1785                 testMeasureTooLate();
   1786                 ANeuralNetworksBurst_free(burst);
   1787                 break;
   1788             }
   1789             default:
   1790                 FAIL() << "Unreachable";
   1791         }
   1792 
   1793         auto testDuration = [](ANeuralNetworksExecution* e, int32_t durationCode,
   1794                                bool nullDuration) {
   1795             SCOPED_TRACE(e);
   1796             SCOPED_TRACE(durationCode);
   1797             SCOPED_TRACE(nullDuration);
   1798 
   1799             // Strictly speaking, a duration COULD have this value, but it is
   1800             // exceedingly unlikely. We'll use it as an initial value that we expect
   1801             // to be modified by getDuration().
   1802             const uint64_t kBogusDuration = UINT64_MAX - 1;
   1803 
   1804             uint64_t duration = kBogusDuration;
   1805             uint64_t* durationPtr = nullDuration ? nullptr : &duration;
   1806 
   1807             int expectedResultCode = ANEURALNETWORKS_NO_ERROR;
   1808             if (e == nullptr | durationPtr == nullptr) {
   1809                 expectedResultCode = ANEURALNETWORKS_UNEXPECTED_NULL;
   1810             } else if (durationCode < 0) {
   1811                 expectedResultCode = ANEURALNETWORKS_BAD_DATA;
   1812             }
   1813 
   1814             EXPECT_EQ(ANeuralNetworksExecution_getDuration(e, durationCode, durationPtr),
   1815                       expectedResultCode);
   1816             if (expectedResultCode == ANEURALNETWORKS_NO_ERROR) {
   1817                 EXPECT_NE(duration, kBogusDuration);
   1818             }
   1819         };
   1820 
   1821         std::vector<ANeuralNetworksExecution*> executions = {nullptr, execution};
   1822         std::vector<int32_t> durationCodes = {-1, ANEURALNETWORKS_DURATION_ON_HARDWARE,
   1823                                               ANEURALNETWORKS_DURATION_IN_DRIVER};
   1824         std::vector<bool> nullDurations = {false, true};
   1825         for (auto e : executions) {
   1826             for (auto d : durationCodes) {
   1827                 for (auto n : nullDurations) {
   1828                     testDuration(e, d, n);
   1829                 }
   1830             }
   1831         }
   1832     }
   1833 }
   1834 
   1835 #ifndef NNTEST_ONLY_PUBLIC_API
   1836 TEST(ValidationTestDevice, GetExtensionSupport) {
   1837     bool result;
   1838     EXPECT_EQ(ANeuralNetworksDevice_getExtensionSupport(nullptr, kTestExtensionName, &result),
   1839               ANEURALNETWORKS_UNEXPECTED_NULL);
   1840 
   1841     uint32_t numDevices = 0;
   1842     EXPECT_EQ(ANeuralNetworks_getDeviceCount(&numDevices), ANEURALNETWORKS_NO_ERROR);
   1843 
   1844     for (uint32_t i = 0; i < numDevices; i++) {
   1845         SCOPED_TRACE(i);
   1846         ANeuralNetworksDevice* device;
   1847         EXPECT_EQ(ANeuralNetworks_getDevice(i, &device), ANEURALNETWORKS_NO_ERROR);
   1848         EXPECT_EQ(ANeuralNetworksDevice_getExtensionSupport(device, kTestExtensionName, nullptr),
   1849                   ANEURALNETWORKS_UNEXPECTED_NULL);
   1850         EXPECT_EQ(ANeuralNetworksDevice_getExtensionSupport(device, nullptr, &result),
   1851                   ANEURALNETWORKS_UNEXPECTED_NULL);
   1852         EXPECT_EQ(ANeuralNetworksDevice_getExtensionSupport(device, kTestExtensionName, &result),
   1853                   ANEURALNETWORKS_NO_ERROR);
   1854     }
   1855 }
   1856 #endif
   1857 
   1858 }  // namespace
   1859