Skip to content
Snippets Groups Projects
Commit 297ae353 authored by Davide Basso's avatar Davide Basso
Browse files

[Units] Add measurements units API

parent a68a5319
Branches
Tags
1 merge request!230Units
Pipeline #7747 canceled
......@@ -134,6 +134,7 @@ add_executable(catch-tests-boardcore
src/tests/catch/test-MEA.cpp
src/tests/catch/test-airbrakesInterp.cpp
src/tests/catch/test-pitot.cpp
src/tests/catch/test-units.cpp
)
target_compile_definitions(catch-tests-boardcore PRIVATE USE_MOCK_PERIPHERALS)
sbs_target(catch-tests-boardcore stm32f429zi_stm32f4discovery)
......
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <ratio>
#include "Units.h"
namespace Boardcore
{
namespace Units
{
namespace Acceleration
{
template <class Ratio = std::ratio<1>>
using Acceleration = Unit<UnitKind::Acceleration, Ratio>;
template <class ToAcceleration, class FromAcceleration>
ToAcceleration acceleration_cast(FromAcceleration const &from)
{
return ToAcceleration(from);
}
using MeterPerSecondSquared = Acceleration<>; // Acceleration in m/s^2
using G = Acceleration<std::ratio<981, 100>>; // Acceleration in Gs
// Floats
constexpr auto operator""_mps2(long double n)
{
return MeterPerSecondSquared(static_cast<float>(n));
};
constexpr auto operator""_g(long double n) { return G(static_cast<float>(n)); };
// Integers
constexpr auto operator""_mps2(unsigned long long n)
{
return MeterPerSecondSquared(static_cast<float>(n));
};
constexpr auto operator""_g(unsigned long long n)
{
return G(static_cast<float>(n));
};
} // namespace Acceleration
} // namespace Units
} // namespace Boardcore
\ No newline at end of file
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <ratio>
#include "Units.h"
namespace Boardcore
{
namespace Units
{
namespace Angle
{
template <class Ratio = std::ratio<1>>
using Angle = Unit<UnitKind::Angle, Ratio>;
template <class ToAngle, class FromAngle>
ToAngle angle_cast(FromAngle const &from)
{
return ToAngle(from);
}
using Degree = Angle<>; // Angle in degrees
using Radian = // Angle in radians
Angle<
std::ratio<static_cast<std::intmax_t>(180 * 1e10),
static_cast<std::intmax_t>(3.14159265358979323846 * 1e10)>>;
// Floats
constexpr auto operator""_rad(long double n)
{
return Radian(static_cast<float>(n));
};
constexpr auto operator""_deg(long double n)
{
return Degree(static_cast<float>(n));
};
// Integers
constexpr auto operator""_rad(unsigned long long n)
{
return Radian(static_cast<float>(n));
};
constexpr auto operator""_deg(unsigned long long n)
{
return Degree(static_cast<float>(n));
};
} // namespace Angle
} // namespace Units
} // namespace Boardcore
\ No newline at end of file
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <ratio>
#include "Units.h"
namespace Boardcore
{
namespace Units
{
namespace Length
{
template <class Ratio = std::ratio<1>>
using Length = Unit<UnitKind::Length, Ratio>;
template <class ToLength, class FromLength>
ToLength length_cast(FromLength const &from)
{
return ToLength(from);
}
using Millimeter = Length<std::milli>; // Length in millimeters
using Centimeter = Length<std::centi>; // Length in centimeters
using Decimeter = Length<std::deci>; // Length in decimeters
using Meter = Length<>; // Length in meters
using Kilometer = Length<std::kilo>; // Length in kilometers
// Floats
constexpr auto operator""_mm(long double n)
{
return Millimeter(static_cast<float>(n));
};
constexpr auto operator""_cm(long double n)
{
return Centimeter(static_cast<float>(n));
};
constexpr auto operator""_dm(long double n)
{
return Decimeter(static_cast<float>(n));
};
constexpr auto operator""_m(long double n)
{
return Meter(static_cast<float>(n));
};
constexpr auto operator""_km(long double n)
{
return Kilometer(static_cast<float>(n));
};
// Integers
constexpr auto operator""_mm(unsigned long long n)
{
return Millimeter(static_cast<float>(n));
};
constexpr auto operator""_cm(unsigned long long n)
{
return Centimeter(static_cast<float>(n));
};
constexpr auto operator""_dm(unsigned long long n)
{
return Decimeter(static_cast<float>(n));
};
constexpr auto operator""_m(unsigned long long n)
{
return Meter(static_cast<float>(n));
};
constexpr auto operator""_km(unsigned long long n)
{
return Kilometer(static_cast<float>(n));
};
} // namespace Length
} // namespace Units
} // namespace Boardcore
\ No newline at end of file
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <ratio>
#include "Units.h"
namespace Boardcore
{
namespace Units
{
namespace Pressure
{
template <class Ratio = std::ratio<1>>
using Pressure = Unit<UnitKind::Pressure, Ratio>;
template <class ToPressure, class FromPressure>
ToPressure pressure_cast(FromPressure const &from)
{
return ToPressure(from);
}
using Pascal = Pressure<>; // Pressure in Pascals
using Bar = Pressure<std::ratio<100000>>; // Pressure in Bars
using Atm = Pressure<std::ratio<101325>>; // Pressure in Atmospheres
// Floats
constexpr auto operator""_pa(long double n)
{
return Pascal(static_cast<float>(n));
};
constexpr auto operator""_bar(long double n)
{
return Bar(static_cast<float>(n));
};
constexpr auto operator""_atm(long double n)
{
return Atm(static_cast<float>(n));
};
// Integers
constexpr auto operator""_pa(unsigned long long n)
{
return Pascal(static_cast<float>(n));
};
constexpr auto operator""_bar(unsigned long long n)
{
return Bar(static_cast<float>(n));
};
constexpr auto operator""_atm(unsigned long long n)
{
return Atm(static_cast<float>(n));
};
} // namespace Pressure
} // namespace Units
} // namespace Boardcore
\ No newline at end of file
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <ratio>
#include "Units.h"
namespace Boardcore
{
namespace Units
{
namespace Speed
{
template <class Ratio = std::ratio<1>>
using Speed = Unit<UnitKind::Speed, Ratio>;
template <class ToSpeed, class FromSpeed>
ToSpeed speed_cast(FromSpeed const &from)
{
return ToSpeed(from);
}
using MeterPerSecond = Speed<>; // Speed in meters per second
using KilometerPerHour =
Speed<std::ratio<1000, 3600>>; // Speed in kilometers per hour
// Floats
constexpr auto operator""_mps(long double n)
{
return MeterPerSecond(static_cast<float>(n));
};
constexpr auto operator""_kmh(long double n)
{
return KilometerPerHour(static_cast<float>(n));
};
// Integers
constexpr auto operator""_mps(unsigned long long n)
{
return MeterPerSecond(static_cast<float>(n));
};
constexpr auto operator""_kmh(unsigned long long n)
{
return KilometerPerHour(static_cast<float>(n));
};
} // namespace Speed
} // namespace Units
} // namespace Boardcore
\ No newline at end of file
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <chrono>
#include <ratio>
#include "Units.h"
namespace Boardcore
{
namespace Units
{
namespace Time
{
template <class Ratio = std::ratio<1>>
using Time = Unit<UnitKind::Time, Ratio>;
template <class ToTime, class FromTime>
ToTime time_cast(FromTime const &from)
{
return ToTime(from);
}
std::chrono::duration<float> to_chrono(Time<> const &from)
{
return std::chrono::duration<float>(from.value());
}
using Nanosecond = Time<std::ratio<1, 1000000000>>; // Time in nanoseconds
using Microsecond = Time<std::ratio<1, 1000000>>; // Time in microseconds
using Millisecond = Time<std::ratio<1, 1000>>; // Time in milliseconds
using Second = Time<>; // Time in seconds
using Minute = Time<std::ratio<60>>; // Time in minutes
using Hour = Time<std::ratio<3600>>; // Time in hours
// Floats
constexpr auto operator""_ns(long double n)
{
return Nanosecond(static_cast<float>(n));
};
constexpr auto operator""_us(long double n)
{
return Microsecond(static_cast<float>(n));
};
constexpr auto operator""_ms(long double n)
{
return Millisecond(static_cast<float>(n));
};
constexpr auto operator""_s(long double n)
{
return Second(static_cast<float>(n));
};
constexpr auto operator""_min(long double n)
{
return Minute(static_cast<float>(n));
};
constexpr auto operator""_h(long double n)
{
return Hour(static_cast<float>(n));
};
// Integers
constexpr auto operator""_ns(unsigned long long n)
{
return Nanosecond(static_cast<float>(n));
};
constexpr auto operator""_us(unsigned long long n)
{
return Microsecond(static_cast<float>(n));
};
constexpr auto operator""_ms(unsigned long long n)
{
return Millisecond(static_cast<float>(n));
};
constexpr auto operator""_s(unsigned long long n)
{
return Second(static_cast<float>(n));
};
constexpr auto operator""_min(unsigned long long n)
{
return Minute(static_cast<float>(n));
};
constexpr auto operator""_h(unsigned long long n)
{
return Hour(static_cast<float>(n));
};
} // namespace Time
} // namespace Units
} // namespace Boardcore
\ No newline at end of file
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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.
*/
#pragma once
#include <utils/Debug.h>
#include <ratio>
#include <typeinfo>
namespace Boardcore
{
namespace Units
{
enum class UnitKind
{
Angle,
Length,
Pressure,
Time,
Speed,
Acceleration,
};
// Base class to implement custom measurement units logic.
template <UnitKind Kind, class Ratio = std::ratio<1>>
class Unit
{
public:
constexpr explicit Unit(float val) : _value(val) {}
template <UnitKind FromKind, class FromRatio>
constexpr explicit Unit(Unit<FromKind, FromRatio> const &from)
: _value(from.template value<Ratio>())
{
}
// Get the value of the unit in the specified ratio.
template <class TargetRatio = Ratio>
constexpr float value() const
{
constexpr auto currentRatio =
static_cast<float>(Ratio::num) / static_cast<float>(Ratio::den);
constexpr auto targetRatio = static_cast<float>(TargetRatio::num) /
static_cast<float>(TargetRatio::den);
return _value * currentRatio / targetRatio;
}
template <UnitKind TargetKind, class TargetRatio = Ratio>
constexpr explicit operator Unit<TargetKind, TargetRatio>() const
{
return Unit<TargetKind, TargetRatio>(value<TargetRatio>());
}
private:
float _value;
};
// Sum, Subtraction, Multiplication, Division
template <UnitKind Kind, class Ratio>
constexpr auto operator+(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return Unit<Kind, Ratio>(lhs.template value() + rhs.template value());
}
template <UnitKind Kind, class Ratio>
constexpr auto operator-(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return Unit<Kind, Ratio>(lhs.template value() - rhs.template value());
}
template <UnitKind Kind, class Ratio>
constexpr auto operator*(const Unit<Kind, Ratio> &lhs, float rhs)
{
return Unit<Kind, Ratio>(lhs.template value() * rhs);
}
template <UnitKind Kind, class Ratio>
constexpr auto operator*(float lhs, const Unit<Kind, Ratio> &rhs)
{
return Unit<Kind, Ratio>(lhs * rhs.template value());
}
template <UnitKind Kind, class Ratio>
constexpr auto operator/(const Unit<Kind, Ratio> &lhs, float rhs)
{
return Unit<Kind, Ratio>(lhs.template value() / rhs);
}
template <UnitKind Kind, class Ratio>
constexpr auto operator/(float lhs, const Unit<Kind, Ratio> &rhs)
{
return Unit<Kind, Ratio>(lhs / rhs.template value());
}
// Comparison operators
template <UnitKind Kind, class Ratio>
constexpr bool operator==(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return lhs.template value() == rhs.template value();
}
template <UnitKind Kind, class Ratio>
constexpr bool operator!=(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return lhs.template value() != rhs.template value();
}
template <UnitKind Kind, class Ratio>
constexpr bool operator<(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return lhs.template value() < rhs.template value();
}
template <UnitKind Kind, class Ratio>
constexpr bool operator>(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return lhs.template value() > rhs.template value();
}
template <UnitKind Kind, class Ratio>
constexpr bool operator<=(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return lhs.template value() <= rhs.template value();
}
template <UnitKind Kind, class Ratio>
constexpr bool operator>=(const Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
return lhs.template value() >= rhs.template value();
}
// Direct assignment operators
template <UnitKind Kind, class Ratio>
constexpr Unit<Kind, Ratio> &operator+=(Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
lhs = lhs + rhs;
return lhs;
}
template <UnitKind Kind, class Ratio>
constexpr Unit<Kind, Ratio> &operator-=(Unit<Kind, Ratio> &lhs,
const Unit<Kind, Ratio> &rhs)
{
lhs = lhs - rhs;
return lhs;
}
template <UnitKind Kind, class Ratio>
constexpr Unit<Kind, Ratio> &operator*=(Unit<Kind, Ratio> &lhs, float rhs)
{
lhs = lhs * rhs;
return lhs;
}
template <UnitKind Kind, class Ratio>
constexpr Unit<Kind, Ratio> &operator/=(Unit<Kind, Ratio> &lhs, float rhs)
{
lhs = lhs / rhs;
return lhs;
}
} // namespace Units
} // namespace Boardcore
/* Copyright (c) 2023 Skyward Experimental Rocketry
* Authors: Davide Basso
*
* 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 <units/Acceleration.h>
#include <units/Angle.h>
#include <units/Length.h>
#include <units/Pressure.h>
#include <units/Speed.h>
#include <units/Time.h>
#include <utils/Debug.h>
#include <catch2/catch.hpp>
#include <cmath>
using namespace Boardcore;
template <class T>
bool eq(T const &a, T const &b)
{
return std::fabs(a.value() - b.value()) < 1e-6;
}
TEST_CASE("Units Test")
{
constexpr float PI = 3.14159265358979323846;
using namespace Units::Angle;
using namespace Units::Length;
using namespace Units::Pressure;
using namespace Units::Time;
using namespace Units::Speed;
using namespace Units::Acceleration;
Meter len = 1_m;
// Verify ratios
REQUIRE(Radian(PI) == angle_cast<Radian>(Degree(180)));
REQUIRE(Millimeter(1000) == length_cast<Millimeter>(Meter(1)));
REQUIRE(Centimeter(100) == length_cast<Centimeter>(Meter(1)));
REQUIRE(Decimeter(10) == length_cast<Decimeter>(Meter(1)));
REQUIRE(Kilometer(0.001) == length_cast<Kilometer>(Meter(1)));
REQUIRE(Pascal(100000) == pressure_cast<Pascal, Bar>(Bar(1)));
REQUIRE(Atm(1) == pressure_cast<Atm, Pascal>(Pascal(101325)));
REQUIRE(Nanosecond(1000000000) == time_cast<Nanosecond>(Second(1)));
REQUIRE(Microsecond(1000000) == time_cast<Microsecond>(Second(1)));
REQUIRE(Millisecond(1000) == time_cast<Millisecond>(Second(1)));
REQUIRE(Minute(1) == time_cast<Minute>(Second(60)));
REQUIRE(Hour(1) == time_cast<Hour>(Second(3600)));
REQUIRE(MeterPerSecond(1) ==
speed_cast<MeterPerSecond>(KilometerPerHour(3.6)));
REQUIRE(MeterPerSecondSquared(9.81) ==
acceleration_cast<MeterPerSecondSquared>(G(1)));
// Test operators
REQUIRE(eq(Radian(2 * PI), Radian(PI) + Radian(PI)));
REQUIRE(eq(Radian(0), Radian(PI) - Radian(PI)));
REQUIRE(eq(Radian(PI * 2), Radian(PI) * 2));
REQUIRE(eq(Radian(2 * PI), 2 * Radian(PI)));
REQUIRE(eq(Radian(PI / 2), Radian(PI) / 2));
REQUIRE(eq(Radian(2 / PI), 2 / Radian(PI)));
// Test comparisons
REQUIRE(Radian(PI) == Radian(PI));
REQUIRE(Radian(PI) != Radian(2 * PI));
REQUIRE(Radian(PI) < Radian(2 * PI));
REQUIRE(Radian(PI) <= Radian(2 * PI));
REQUIRE(Radian(PI) <= Radian(PI));
REQUIRE(Radian(2 * PI) > Radian(PI));
REQUIRE(Radian(2 * PI) >= Radian(PI));
REQUIRE(Radian(PI) >= Radian(PI));
// Test assignment operators
auto a = Radian(PI);
a += Radian(PI);
REQUIRE(a == Radian(2 * PI));
a -= Radian(PI);
REQUIRE(a == Radian(PI));
a *= 2;
REQUIRE(a == Radian(2 * PI));
a /= 2;
REQUIRE(a == Radian(PI));
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment