Custom unit and data tests enabled

This commit is contained in:
Mateusz Pusz
2019-12-07 18:33:55 +01:00
parent 22eda11bea
commit 2c29af9670
8 changed files with 301 additions and 134 deletions
+2 -1
View File
@@ -22,7 +22,8 @@
add_library(unit_tests_static
# cgs_test.cpp
# custom_unit_test.cpp
custom_unit_test.cpp
data_test.cpp
dimension_op_test.cpp
# fixed_string_test.cpp
math_test.cpp
+18 -51
View File
@@ -20,70 +20,37 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <units/dimensions/voltage.h>
#include <units/dimensions/frequency.h>
#include <units/physical/si/voltage.h>
#include <units/physical/si/frequency.h>
#include <units/math.h>
/* ************** DERIVED DIMENSIONS THAT INCLUDE UNITS WITH SPECIAL NAMES **************** */
namespace {
struct base_dim_digital_information : units::base_dimension<"digital information", "b"> {};
using namespace units;
using namespace units::si;
struct digital_information : units::derived_dimension<digital_information, units::exp<base_dim_digital_information, 1>> {};
// power spectral density
struct sq_volt_per_hertz : unit<sq_volt_per_hertz> {};
struct dim_power_spectral_density : derived_dimension<dim_power_spectral_density, sq_volt_per_hertz, units::exp<dim_voltage, 2>, units::exp<dim_frequency, -1>> {};
template<typename T>
concept DigitalInformation = units::QuantityOf<T, digital_information>;
template<Unit U, Scalar Rep = double>
using power_spectral_density = quantity<dim_power_spectral_density, U, Rep>;
struct data_prefix : units::prefix_type {};
// amplitude spectral density
struct volt_per_sqrt_hertz : unit<volt_per_sqrt_hertz> {};
struct dim_amplitude_spectral_density : derived_dimension<dim_amplitude_spectral_density, volt_per_sqrt_hertz, units::exp<dim_voltage, 1>, units::exp<dim_frequency, -1, 2>> {};
struct kibi : units::prefix<kibi, data_prefix, units::ratio< 1'024>, "Ki"> {};
struct bit : units::named_coherent_derived_unit<bit, digital_information, "b", data_prefix> {};
struct kilobit : units::prefixed_derived_unit<kilobit, kibi, bit> {};
struct byte : units::named_scaled_derived_unit<byte, digital_information, "B", units::ratio<8>, data_prefix> {};
struct kilobyte : units::prefixed_derived_unit<kilobyte, kibi, byte> {};
inline namespace literals {
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); }
constexpr auto operator""_Kib(unsigned long long l) { return units::quantity<kilobit, std::int64_t>(l); }
constexpr auto operator""_B(unsigned long long l) { return units::quantity<byte, std::int64_t>(l); }
constexpr auto operator""_KiB(unsigned long long l) { return units::quantity<kilobyte, std::int64_t>(l); }
}
}
namespace {
static_assert(1_B == 8_b);
static_assert(1024_b == 1_Kib);
static_assert(1024_B == 1_KiB);
static_assert(8 * 1024_b == 1_KiB);
static_assert(8 * 1_Kib == 1_KiB);
template<Unit U, Scalar Rep = double>
using amplitude_spectral_density = quantity<dim_amplitude_spectral_density, U, Rep>;
}
namespace {
using namespace units;
static_assert(std::is_same_v<dimension_sqrt<dim_power_spectral_density>, dim_amplitude_spectral_density>);
static_assert(std::is_same_v<dimension_pow<dim_amplitude_spectral_density, 2>, dim_power_spectral_density>);
// power spectral density
struct power_spectral_density : derived_dimension<power_spectral_density, units::exp<voltage, 2>, units::exp<frequency, -1>> {};
struct sq_volt_per_hertz : coherent_derived_unit<sq_volt_per_hertz, power_spectral_density> {};
// amplitude spectral density
struct amplitude_spectral_density : derived_dimension<amplitude_spectral_density, units::exp<voltage, 1>, units::exp<frequency, -1, 2>> {};
struct volt_per_sqrt_hertz : coherent_derived_unit<volt_per_sqrt_hertz, amplitude_spectral_density> {};
}
namespace {
static_assert(std::is_same_v<dimension_sqrt<power_spectral_density>, amplitude_spectral_density>);
static_assert(std::is_same_v<dimension_pow<amplitude_spectral_density, 2>, power_spectral_density>);
static_assert(std::is_same_v<decltype(pow<2>(quantity<volt_per_sqrt_hertz>(4))), decltype(quantity<sq_volt_per_hertz>(16))>);
static_assert(std::is_same_v<decltype(sqrt(quantity<sq_volt_per_hertz>(16))), decltype(quantity<volt_per_sqrt_hertz>(4))>);
static_assert(std::is_same_v<decltype(pow<2>(amplitude_spectral_density<volt_per_sqrt_hertz>(4))), decltype(power_spectral_density<sq_volt_per_hertz>(16))>);
static_assert(std::is_same_v<decltype(sqrt(power_spectral_density<sq_volt_per_hertz>(16))), decltype(amplitude_spectral_density<volt_per_sqrt_hertz>(4))>);
}
+48
View File
@@ -0,0 +1,48 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// 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/data/bitrate.h>
#include <units/data/information.h>
/* ************** DERIVED DIMENSIONS THAT INCLUDE UNITS WITH SPECIAL NAMES **************** */
namespace {
using namespace units::data;
// information
static_assert(1B == 8b);
static_assert(1024b == 1Kib);
static_assert(1024B == 1KiB);
static_assert(8 * 1024b == 1KiB);
static_assert(8 * 1Kib == 1KiB);
static_assert(1Kib == 1024b);
static_assert(1Mib == 1024Kib);
static_assert(1Gib == 1024Mib);
static_assert(1Tib == 1024Gib);
static_assert(1Pib == 1024Tib);
// bitrate
}