Skip to content
Snippets Groups Projects
Commit 8ed63d19 authored by Maxence Naud's avatar Maxence Naud Committed by Maxence Naud
Browse files

fix: use aidge 'approxEq' function instead of manual comparison and change...

fix: use aidge 'approxEq' function instead of manual comparison and change CHECKs for REQUIREs in interpolation test
parent e342d787
No related branches found
No related tags found
No related merge requests found
Pipeline #68877 passed
......@@ -9,15 +9,21 @@
*
********************************************************************************/
#include <aidge/backend/cpu/data/Interpolation.hpp>
#include <aidge/data/Interpolation.hpp>
#include <aidge/data/Tensor.hpp>
#include <aidge/filler/Filler.hpp>
#include <aidge/utils/Types.h>
#include <catch2/catch_test_macros.hpp>
#include <cmath> // std::fabs
#include <cstdlib> // std::abs
#include <limits>
#include <memory>
#include <set>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include "aidge/backend/cpu/data/Interpolation.hpp"
#include "aidge/data/Interpolation.hpp"
#include "aidge/data/Tensor.hpp"
#include "aidge/filler/Filler.hpp"
#include "aidge/utils/Types.h"
#include "aidge/utils/TensorUtils.hpp"
namespace Aidge {
......@@ -30,12 +36,12 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
SECTION("1D") {
pointsToInterpolateInt =
std::set<Interpolation::Point<int>>({{{0}, 10}, {{1}, 20}});
CHECK(abs(InterpolationCPU::linear({0.5}, pointsToInterpolateInt) -
REQUIRE(std::abs(InterpolationCPU::linear({0.5}, pointsToInterpolateInt) -
15) <= std::numeric_limits<int>::epsilon());
pointsToInterpolateFloat = std::set<Interpolation::Point<float>>(
{{{0}, .0F}, {{1}, 0.2F}});
CHECK(fabs(InterpolationCPU::linear({0.3},
REQUIRE(std::fabs(InterpolationCPU::linear({0.3},
pointsToInterpolateFloat) -
.06F) <= 1e-5);
}
......@@ -46,21 +52,21 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
{{14, 21}, 162.F},
{{15, 20}, 210.F},
{{15, 21}, 95.F}};
CHECK(fabs(InterpolationCPU::linear<float>(
{14.5F, 20.2F},
pointsToInterpolateFloat) -
146.1) < 1e-5);
const Tensor interpolatedValue = Tensor(std::fabs(InterpolationCPU::linear<float>(
{14.5F, 20.2F},
pointsToInterpolateFloat)));
REQUIRE(approxEq<float>(interpolatedValue, Tensor(146.1f)));
// pointsToInterpolateFloat = {{{0, 0}, .10F},
// {{0, 1}, .20F},
// {{1, 0}, .30F},
// {{1, 1}, .40F}};
// CHECK(abs(InterpolationCPU::linear<float>({1.5, 0.5},
// REQUIRE(std::abs(InterpolationCPU::linear<float>({1.5, 0.5},
// pointsToInterpolateInt)
// -
// 25) < std::numeric_limits<int>::epsilon());
// pointsToInterpolateFloat = std::vector({0.1F, 0.2F, 0.3F,
// 0.4F}); CHECK(InterpolationCPU::linear(pointsToInterpolateFloat)
// 0.4F}); REQUIRE(InterpolationCPU::linear(pointsToInterpolateFloat)
// == .25f);
}
SECTION("3D") {
......@@ -72,7 +78,7 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
{{1, 0, 1}, .6F},
{{1, 1, 0}, .7F},
{{1, 1, 1}, .8F}};
CHECK(fabs(InterpolationCPU::linear({.5, .5, .5},
REQUIRE(std::fabs(InterpolationCPU::linear({.5, .5, .5},
pointsToInterpolateFloat) -
.45f) < 1e-5);
}
......@@ -94,7 +100,7 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
{{1, 1, 0, 1}, 1.4F},
{{1, 1, 1, 0}, 1.5F},
{{1, 1, 1, 1}, 1.6F}};
CHECK(fabs(InterpolationCPU::linear<float>(
REQUIRE(std::fabs(InterpolationCPU::linear<float>(
{.5, .5, .5, .5},
pointsToInterpolateFloat) -
.85f) < 0.0001);
......@@ -139,25 +145,25 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
{{4}, 5.0F}};
SECTION("Floor") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::Floor) == 1);
}
SECTION("Ceil") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::Ceil) == 2);
}
SECTION("RoundPreferFloor") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::RoundPreferFloor) == 1);
}
SECTION("RoundPreferCeil") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::RoundPreferCeil) == 2);
......@@ -172,26 +178,26 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
{{3, 3}, 50.0},
{{3, 4}, 60.0}};
SECTION("Floor") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::Floor) == 30.);
}
SECTION("Ceil") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::Ceil) == 60.);
}
SECTION("RoundPreferFloor") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::RoundPreferFloor) ==
40.);
}
SECTION("RoundPreferCeil") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::RoundPreferCeil) == 60.);
......@@ -207,26 +213,26 @@ TEST_CASE("Interpolation", "[Interpolation][Data]") {
{{2, 3, 4}, 50.0},
{{3, 3, 4}, 60.0}};
SECTION("Floor") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::Floor) == 10.);
}
SECTION("Ceil") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::Ceil) == 50.);
}
SECTION("RoundPreferFloor") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::RoundPreferFloor) ==
30.);
}
SECTION("RoundPreferCeil") {
CHECK(InterpolationCPU::nearest(
REQUIRE(InterpolationCPU::nearest(
coordToInterpolate,
pointsToInterpolate,
Interpolation::Mode::RoundPreferCeil) == 30.);
......
......@@ -156,7 +156,7 @@ TEST_CASE("[cpu/operator] ReduceMean(forward)", "[ReduceMean][CPU]") {
}
SECTION("KeepDims") {
SECTION("test 1") {
std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array3D<float,3,2,2> {
std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array3D<cpptype_t<DataType::Float32>,3,2,2> {
{
{
{ 5.0, 1.0 },
......@@ -172,12 +172,12 @@ TEST_CASE("[cpu/operator] ReduceMean(forward)", "[ReduceMean][CPU]") {
}
}
});
Tensor myOutput = Tensor(Array3D<float,3,1,2> {
Tensor myOutput = Tensor(Array3D<cpptype_t<DataType::Float32>,3,1,2> {
{
{{ 12.5, 1.5 }},
{{ 35.0, 1.5 }},
{{ 57.5, 1.5 }}
{{ 12.5f, 1.5f }},
{{ 35.0f, 1.5f }},
{{ 57.5f, 1.5f }}
}
});
......
......@@ -482,7 +482,7 @@ TEST_CASE("[cpu/scheduler] Accumulate", "[scheduler]") {
{{2.0, 3.0}, {4.0, 5.0}, {6.0, 7.0}}}});
std::shared_ptr<Tensor> MemInit =
std::make_shared<Tensor>(Array2D<float, 3, 2>{
std::make_shared<Tensor>(Array2D<cpptype_t<DataType::Float32>, 3, 2>{
{{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}}});
auto meta = Accumulate(2, "accumulate");
......@@ -517,14 +517,14 @@ TEST_CASE("[cpu/scheduler] Accumulate", "[scheduler]") {
REQUIRE_NOTHROW(scheduler.forward(true));
std::shared_ptr<Tensor> expectedOutput = std::make_shared<Tensor>(
Array2D<float, 3, 2>{{{3.0, 5.0}, {7.0, 9.0}, {11.0, 13.0}}});
Array2D<cpptype_t<DataType::Float32>, 3, 2>{{{3.0, 5.0}, {7.0, 9.0}, {11.0, 13.0}}});
std::shared_ptr<Tensor> output = std::static_pointer_cast<OperatorTensor>(pop_o->getOperator())->getOutput(0);
REQUIRE(*output == *expectedOutput);
}
TEST_CASE("[cpu/scheduler] Branch", "[scheduler]") {
std::shared_ptr<Tensor> in = std::make_shared<Tensor>(
Array2D<float, 2, 3>{{{1, 2, 3}, {4, 5, 6}}});
Array2D<cpptype_t<DataType::Float32>, 2, 3>{{{1, 2, 3}, {4, 5, 6}}});
std::shared_ptr<GraphView> g = Sequential({
Producer(in, "input"),
......@@ -576,7 +576,7 @@ TEST_CASE("[cpu/scheduler] Branch", "[scheduler]") {
#ifdef WITH_OPENSSL
TEST_CASE("[cpu/scheduler] Select", "[scheduler]") {
std::shared_ptr<Tensor> in = std::make_shared<Tensor>(
Array2D<float, 2, 3>{{{1, 2, 3}, {4, 5, 6}}});
Array2D<cpptype_t<DataType::Float32>, 2, 3>{{{1, 2, 3}, {4, 5, 6}}});
std::shared_ptr<GraphView> g = Sequential({
Producer(in, "input"),
......@@ -605,21 +605,21 @@ TEST_CASE("[cpu/scheduler] Select", "[scheduler]") {
scheduler.generateScheduling();
scheduler.saveStaticSchedulingDiagram("select_scheduling");
REQUIRE_NOTHROW(scheduler.forward(true));
g->save("select_forwarded");
auto expectedOutputHash = std::make_shared<Tensor>(
Array1D<uint64_t, 4>{{0x1b7cf58dfe2dae24, 0x3bac903def4ce580, 0x5f5a347389d97f41, 0x2c2dc759abc6b61}});
Array1D<cpptype_t<DataType::UInt64>, 4>{{0x1b7cf58dfe2dae24, 0x3bac903def4ce580, 0x5f5a347389d97f41, 0x2c2dc759abc6b61}});
auto outputHash = std::static_pointer_cast<OperatorTensor>(g->getNode("hash")->getOperator())->getOutput(0);
REQUIRE(*outputHash == *expectedOutputHash);
auto expectedOutputMod = std::make_shared<Tensor>(
Array1D<uint64_t, 4>{{2, 1, 1, 2}});
Array1D<cpptype_t<DataType::UInt64>, 4>{{2, 1, 1, 2}});
auto outputMod = std::static_pointer_cast<OperatorTensor>(g->getNode("mod")->getOperator())->getOutput(0);
REQUIRE(*outputMod == *expectedOutputMod);
auto expectedOutput = std::make_shared<Tensor>(
Array2D<float, 2, 3>{{{std::sqrt(1), std::sqrt(2), std::sqrt(3)}, {std::sqrt(4), std::sqrt(5), std::sqrt(6)}}});
Array2D<cpptype_t<DataType::Float32>, 2, 3>{{{std::sqrt(1.0f), std::sqrt(2.0f), std::sqrt(3.0f)}, {std::sqrt(4.0f), std::sqrt(5.0f), std::sqrt(6.0f)}}});
auto output = std::static_pointer_cast<OperatorTensor>(g->getNode("select")->getOperator())->getOutput(0);
REQUIRE(*output == *expectedOutput);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment