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 "NeuralNetworksWrapper.h"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace ::android::nn::wrapper;
22 
23 // This file tests certain aspects of the interfaces from NeuralNetworksWrapper.h.
24 
25 class WrapperTestModelFinish : public ::testing::Test {
26    protected:
SetUp()27     void SetUp() override {
28         OperandType type(Type::TENSOR_FLOAT32, {1});
29         mIn = mModel.addOperand(&type);
30         mOut = mModel.addOperand(&type);
31         mModel.addOperation(ANEURALNETWORKS_TANH, {mIn}, {mOut});
32     }
33 
34     Model mModel;
35     uint32_t mIn, mOut;
36 };
37 
TEST_F(WrapperTestModelFinish,Good)38 TEST_F(WrapperTestModelFinish, Good) {
39     ASSERT_TRUE(mModel.isValid());
40     mModel.identifyInputsAndOutputs({mIn}, {mOut});
41     ASSERT_TRUE(mModel.isValid());
42     ASSERT_EQ(mModel.finish(), Result::NO_ERROR);
43     ASSERT_TRUE(mModel.isValid());
44 }
45 
TEST_F(WrapperTestModelFinish,BadIdentify)46 TEST_F(WrapperTestModelFinish, BadIdentify) {
47     ASSERT_TRUE(mModel.isValid());
48     // A Model must have at least one input and one output. Verify
49     // that if it doesn't, then isValid() goes false and finish()
50     // fails.
51     mModel.identifyInputsAndOutputs({}, {});
52     ASSERT_FALSE(mModel.isValid());
53     ASSERT_EQ(mModel.finish(), Result::BAD_STATE);
54     ASSERT_FALSE(mModel.isValid());
55 }
56 
TEST_F(WrapperTestModelFinish,BadFinish)57 TEST_F(WrapperTestModelFinish, BadFinish) {
58     ASSERT_TRUE(mModel.isValid());
59     mModel.identifyInputsAndOutputs({mIn}, {mOut});
60     ASSERT_TRUE(mModel.isValid());
61     ASSERT_EQ(mModel.finish(), Result::NO_ERROR);
62     ASSERT_TRUE(mModel.isValid());
63     // The easiest way to get a failed finish() is to call it a second
64     // time. Verify that this fails and that isValid() goes false.
65     ASSERT_EQ(mModel.finish(), Result::BAD_STATE);
66     ASSERT_FALSE(mModel.isValid());
67 }
68