/*
 * Copyright (c) 2018-2021, 2024 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/NEON/functions/NERange.h"
#include "arm_compute/runtime/Tensor.h"
#include "arm_compute/runtime/TensorAllocator.h"
#include "tests/NEON/Accessor.h"
#include "tests/PaddingCalculator.h"
#include "tests/datasets/ShapeDatasets.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Macros.h"
#include "tests/framework/datasets/Datasets.h"
#include "tests/validation/Validation.h"
#include "tests/validation/fixtures/RangeFixture.h"

namespace arm_compute
{
namespace test
{
namespace validation
{
namespace
{
constexpr RelativeTolerance<float> tolerance(0.01f);
constexpr AbsoluteTolerance<float> abs_tolerance(0.02f);

const auto start_dataset          = framework::dataset::make("Start", { float(3), float(-17), float(16) });
const auto unsigned_start_dataset = framework::dataset::make("Start", { float(3), float(16) });
const auto float_step_dataset     = framework::dataset::make("Step", { float(1), float(-0.2f), float(0.2), float(12.2), float(-12.2), float(-1.2), float(-3), float(3) });
const auto step_dataset           = framework::dataset::make("Step", { float(1), float(12), float(-12), float(-1), float(-3), float(3) });
const auto unsigned_step_dataset  = framework::dataset::make("Step", { float(1), float(12), float(3) });
} // namespace

TEST_SUITE(NEON)
TEST_SUITE(Range)

// *INDENT-OFF*
// clang-format off

DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
               framework::dataset::make("OutputInfo", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
                                                        TensorInfo(TensorShape(32U), 1, DataType::U8),
                                                        TensorInfo(TensorShape(27U), 1, DataType::U8),
                                                        TensorInfo(TensorShape(32U), 1, DataType::U8),
                                                        TensorInfo(TensorShape(32U), 1, DataType::F32),
                                                        TensorInfo(TensorShape(27U), 1, DataType::U8),
                                                        TensorInfo(TensorShape(27U), 1, DataType::U8),
                                                        TensorInfo(TensorShape(10U), 1, DataType::QASYMM8),
                                                        TensorInfo(TensorShape(10U), 1, DataType::U8),
                                                      }),
               framework::dataset::make("Start",{ 0.0f,
                                                  15.0f,
                                                  1500.0f,
                                                  100.0f,
                                                  -15.0f,
                                                  0.2f,
                                                  2.0f,
                                                  10.0f,
                                                  10.0f
                                                })),
               framework::dataset::make("End",{ 100.0f,
                                                15.0f,
                                                2500.0f,
                                                -1000.0f,
                                                15.0f,
                                                10.0f,
                                                10.0f,
                                                100.0f,
                                                100.0f
                                              })),
               framework::dataset::make("Step",{ 100.0f,
                                                 15.0f,
                                                 10.0f,
                                                 100.0f,
                                                 -15.0f,
                                                 1.0f,
                                                 0.0f,
                                                 10.0f,
                                                 10.0f
                                              })),
               framework::dataset::make("Expected", { false, // 1-D tensor expected
                                                    false, // start == end
                                                    false, // output vector size insufficient
                                                    false, // sign of step incorrect
                                                    false, // sign of step incorrect
                                                    false, // data type incompatible
                                                    false, // step = 0
                                                    false, // invalid QASYMM8 datatype
                                                    true,
                                                    })),
               output_info, start, end, step, expected)
{
    ARM_COMPUTE_EXPECT(bool(NERange::validate(&output_info, start, end, step)) == expected, framework::LogLevel::ERRORS);
}
// clang-format on
// *INDENT-ON*

template <typename T>
using NERangeFixture = RangeFixture<Tensor, Accessor, NERange, T>;

TEST_SUITE(U8)
FIXTURE_DATA_TEST_CASE(RunSmall, NERangeFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
                                                                                                                 framework::dataset::make("DataType", DataType::U8),
                                                                                                                 unsigned_start_dataset),
                                                                                                             unsigned_step_dataset),
                                                                                                     framework::dataset::make("QuantizationInfo", { QuantizationInfo() })))
{
    // Validate output
    validate(Accessor(_target), _reference, tolerance, 0.f, abs_tolerance);
}
TEST_SUITE_END() // U8

TEST_SUITE(S16)
FIXTURE_DATA_TEST_CASE(RunSmall, NERangeFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
                                                                                                                 framework::dataset::make("DataType", DataType::S16),
                                                                                                                 start_dataset),
                                                                                                             step_dataset),
                                                                                                     framework::dataset::make("QuantizationInfo", { QuantizationInfo() })))
{
    // Validate output
    validate(Accessor(_target), _reference, tolerance, 0.f, abs_tolerance);
}
TEST_SUITE_END() // S16

TEST_SUITE(Float)
#ifdef ARM_COMPUTE_ENABLE_FP16
TEST_SUITE(FP16)
FIXTURE_DATA_TEST_CASE(RunSmall, NERangeFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
                                                                                                              framework::dataset::make("DataType", DataType::F16),
                                                                                                              start_dataset),
                                                                                                          float_step_dataset),
                                                                                                  framework::dataset::make("QuantizationInfo", { QuantizationInfo() })))
{
    if(CPUInfo::get().has_fp16())
    {
        // Validate output
        validate(Accessor(_target), _reference, tolerance, 0.f, abs_tolerance);
    }
    else
    {
        ARM_COMPUTE_TEST_INFO("Device does not support fp16 vector operations. Test SKIPPED.");
        framework::ARM_COMPUTE_PRINT_INFO();
    }
}
TEST_SUITE_END() // FP16
#endif           // ARM_COMPUTE_ENABLE_FP16

TEST_SUITE(FP32)
FIXTURE_DATA_TEST_CASE(RunSmall, NERangeFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
                                                                                                               framework::dataset::make("DataType", DataType::F32),
                                                                                                               start_dataset),
                                                                                                           float_step_dataset),
                                                                                                   framework::dataset::make("QuantizationInfo", { QuantizationInfo() })))
{
    // Validate output
    validate(Accessor(_target), _reference, tolerance, 0.f, abs_tolerance);
}
TEST_SUITE_END() // FP32
TEST_SUITE_END() // Float

TEST_SUITE_END() // Range
TEST_SUITE_END() // Neon
} // namespace validation
} // namespace test
} // namespace arm_compute
