Skip to content

Commit

Permalink
added negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbencer committed Nov 20, 2024
1 parent c1d2dbb commit 0539cb8
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tests/nnfw_api/src/GenModelTests/one_op_tests/Reshape.test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "GenModelTest.h"

TEST_F(GenModelTest, OneOp_neg_Reshape_invalid_target_shape)
{
CircleGen cgen;
const auto f32 = circle::TensorType::TensorType_FLOAT32;

const std::vector<int32_t> new_shape_data{1, 5};
const uint32_t new_shape_buf = cgen.addBuffer(new_shape_data);
const int new_shape = cgen.addTensor({{2}, f32, new_shape_buf});
const int input = cgen.addTensor({{4}, f32});
const int out = cgen.addTensor({{1, 5}, f32});

cgen.addOperatorReshape({{input, new_shape}, {out}}, &new_shape_data);
cgen.setInputsAndOutputs({input}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 3, 4}}));
_context->setBackends({"cpu", "gpu_cl"});

_context->expectFailCompile();

SUCCEED();
}

TEST_F(GenModelTest, OneOp_neg_Reshape_invalid_target_dyn_shape)
{
CircleGen cgen;
const auto f32 = circle::TensorType::TensorType_FLOAT32;
const auto i32 = circle::TensorType::TensorType_INT32;

const std::vector<float> in_data{1.f, 2.f, 3.f, 4.f};
const uint32_t input_buf = cgen.addBuffer(in_data);
const int input = cgen.addTensor({{4}, f32, input_buf});
const int new_shape = cgen.addTensor({{2}, i32});
const int out = cgen.addTensor({{}, f32}); // unspecified shape

const CircleGen::Shape empty_new_shape;
cgen.addOperatorReshape({{input, new_shape}, {out}}, &empty_new_shape);
cgen.setInputsAndOutputs({new_shape}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(
TestCaseData{}.addInput(std::vector<int>{1, 5}).addOutput(in_data).expectFailRun());
_context->output_sizes(0, sizeof(float) * in_data.size());
_context->setBackends({"cpu", "gpu_cl"});

SUCCEED();
}

TEST_F(GenModelTest, OneOp_neg_Reshape_invalid_target_dyn_type)
{
CircleGen cgen;
const auto f32 = circle::TensorType::TensorType_FLOAT32;

const std::vector<float> in_data{1.f, 2.f, 3.f, 4.f};
const uint32_t input_buf = cgen.addBuffer(in_data);
const int input = cgen.addTensor({{4}, f32, input_buf});
const int new_shape = cgen.addTensor({{2}, f32});
const int out = cgen.addTensor({{}, f32}); // unspecified shape

const CircleGen::Shape empty_new_shape;
cgen.addOperatorReshape({{input, new_shape}, {out}}, &empty_new_shape);
cgen.setInputsAndOutputs({new_shape}, {out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(
TestCaseData{}.addInput(std::vector<float>{2, 2}).addOutput(in_data).expectFailRun());
_context->output_sizes(0, sizeof(float) * in_data.size());
_context->setBackends({"cpu", "gpu_cl"});

SUCCEED();
}
55 changes: 55 additions & 0 deletions tests/nnfw_api/src/GenModelTests/one_op_tests/Squeeze.test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "GenModelTest.h"

TEST_F(GenModelTest, OneOp_neg_Squeeze_invalid_dims)
{
CircleGen cgen;
const std::vector<int32_t> squeeze_dims{0, 1}; // 1 dim here is incorrect
int input = cgen.addTensor({{1, 2, 1, 2}, circle::TensorType::TensorType_FLOAT32});
int squeeze_out = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32});

cgen.addOperatorSqueeze({{input}, {squeeze_out}}, squeeze_dims);
cgen.setInputsAndOutputs({input}, {squeeze_out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 3, 4}}));
_context->setBackends({"cpu", "gpu_cl"});

_context->expectFailCompile();

SUCCEED();
}

TEST_F(GenModelTest, OneOp_neg_Squeeze_out_of_rank_dims)
{
CircleGen cgen;
const std::vector<int32_t> squeeze_dims{0, 4}; // 4 dim here is incorrect
int input = cgen.addTensor({{1, 2, 1, 2}, circle::TensorType::TensorType_FLOAT32});
int squeeze_out = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32});

cgen.addOperatorSqueeze({{input}, {squeeze_out}}, squeeze_dims);
cgen.setInputsAndOutputs({input}, {squeeze_out});

_context = std::make_unique<GenModelTestContext>(cgen.finish());
_context->addTestCase(uniformTCD<float>({{1, 2, 3, 4}}, {{1, 2, 3, 4}}));
_context->setBackends({"cpu", "gpu_cl"});

_context->expectFailCompile();

SUCCEED();
}

0 comments on commit 0539cb8

Please sign in to comment.