Files
mp-units/test/unit_test/static/unit_test.cpp

194 lines
5.3 KiB
C++
Raw Normal View History

2018-08-22 12:11:19 +02:00
// 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.
2019-10-14 10:53:02 +02:00
#include <units/dimensions/acceleration.h>
#include <units/dimensions/area.h>
#include <units/dimensions/capacitance.h>
#include <units/dimensions/current.h>
2019-10-14 10:53:02 +02:00
#include <units/dimensions/electric_charge.h>
#include <units/dimensions/energy.h>
2019-10-14 10:53:02 +02:00
#include <units/dimensions/force.h>
#include <units/dimensions/frequency.h>
#include <units/dimensions/length.h>
#include <units/dimensions/luminous_intensity.h>
#include <units/dimensions/mass.h>
#include <units/dimensions/power.h>
2019-10-14 10:53:02 +02:00
#include <units/dimensions/pressure.h>
#include <units/dimensions/substance.h>
2019-11-04 06:46:53 +00:00
#include <units/dimensions/surface_tension.h>
2019-10-14 10:53:02 +02:00
#include <units/dimensions/temperature.h>
#include <units/dimensions/time.h>
#include <units/dimensions/velocity.h>
2019-10-14 10:53:02 +02:00
#include <units/dimensions/voltage.h>
#include <units/dimensions/volume.h>
2018-08-22 12:11:19 +02:00
#include <utility>
2018-09-28 07:47:37 -07:00
namespace {
2019-04-09 17:22:15 +02:00
using namespace units;
2018-08-22 12:11:19 +02:00
2019-04-11 13:36:06 +01:00
/* ************** BASE DIMENSIONS **************** */
2018-08-22 12:11:19 +02:00
2018-09-28 07:47:37 -07:00
// length
2018-08-22 12:11:19 +02:00
static_assert(1km == 1000m);
static_assert(1m == 100cm);
static_assert(1m == 1000mm);
static_assert(1km + 1m == 1001m);
static_assert(10km / 5km == 2);
2019-08-05 18:22:53 +02:00
static_assert(100mm / 5cm == 2);
static_assert(10km / 2 == 5km);
2018-08-22 12:11:19 +02:00
static_assert(1yd == 0.9144m);
static_assert(1yd == 3ft);
static_assert(1ft == 12in);
static_assert(1mi == 1760yd);
2019-04-09 13:34:55 +02:00
2019-09-18 22:26:30 -06:00
static_assert(5in + 8cm == 207mm);
2018-10-16 15:39:39 +02:00
static_assert(millimetre::symbol == "mm");
static_assert(centimetre::symbol == "cm");
static_assert(kilometre::symbol == "km");
// mass
static_assert(1kg == 1000g);
static_assert(kilogram::symbol == "kg");
// time
static_assert(1h == 3600s);
static_assert(nanosecond::symbol == "ns");
static_assert(microsecond::symbol == "µs");
static_assert(millisecond::symbol == "ms");
// current
// temperature
// substance
// luminous intensity
2019-07-24 16:55:21 +02:00
/* ************** DERIVED DIMENSIONS WITH NAMED UNITS **************** */
2019-04-11 13:36:06 +01:00
// frequency
static_assert(2 / 1s == 2Hz);
2019-08-05 18:22:53 +02:00
static_assert(120 / 1min == 2Hz);
static_assert(1000 / 1s == 1kHz);
static_assert(1 / 1ms == 1kHz);
static_assert(3.2GHz == 3'200'000'000Hz);
2019-08-05 18:22:53 +02:00
static_assert(10Hz * 1min == 600);
2019-04-11 13:36:06 +01:00
static_assert(millihertz::symbol == "mHz");
static_assert(kilohertz::symbol == "kHz");
static_assert(megahertz::symbol == "MHz");
static_assert(gigahertz::symbol == "GHz");
static_assert(terahertz::symbol == "THz");
2019-07-24 16:55:21 +02:00
// force
static_assert(10kg * 10mps_sq == 100N);
// pressure
static_assert(10N / 10sq_m == 1Pa);
// energy
static_assert(10N * 10m == 100_J);
static_assert(10Pa * 10cub_m == 100_J);
// power
static_assert(10_J / 10s == 1W);
// electric charge
static_assert(10A * 10s == 100C);
// voltage
static_assert(10W / 10A == 1V);
static_assert(10_J / 10C == 1V);
// capacitance
static_assert(10C / 10V == 1F);
/* ************** DERIVED DIMENSIONS IN TERMS OF BASE UNITS **************** */
2018-09-28 07:47:37 -07:00
// velocity
2018-08-22 12:11:19 +02:00
static_assert(std::is_same_v<decltype(1km / 1s), quantity<unit<velocity, ratio<1000, 1>>, std::int64_t>>);
2018-08-22 12:11:19 +02:00
static_assert(10m / 5s == 2mps);
static_assert(10 / 5s * 1m == 2mps);
static_assert(1km / 1s == 1000mps);
// static_assert(1km / 1h == 1kmph); // should not compile
static_assert(1.0km / 1h == 1kmph);
static_assert(1000.0m / 3600.0s == 1kmph);
2018-08-22 12:11:19 +02:00
static_assert(10.0mi / 2h == 5mph);
2019-04-09 13:34:55 +02:00
static_assert(2kmph * 2h == 4km);
// static_assert(2kmph * 15min == 500m); // should not compile
static_assert(2kmph * 15.0min == 500m);
static_assert(2.0kmph * 15min == 500m);
2018-08-22 12:11:19 +02:00
static_assert(2km / 2kmph == 1h);
// static_assert(2000m / 2kmph == 1h); // should not compile
2019-08-02 17:04:55 +02:00
static_assert(quantity_cast<quantity<kilometre, int>>(2000m) / 2kmph == 1h);
2018-08-22 12:11:19 +02:00
// static_assert(metre_per_second::symbol == basic_fixed_string("m/s"));
// static_assert(kilometre_per_hour::symbol == basic_fixed_string("km/h"));
2019-07-24 16:55:21 +02:00
// acceleration
static_assert(10mps / 10s == 1mps_sq);
2019-04-11 13:36:54 +01:00
// area
static_assert(1m * 1m == 1sq_m);
static_assert(10km * 10km == 100sq_km);
static_assert(1sq_m == 10'000sq_cm);
2019-07-24 16:55:21 +02:00
// volume
static_assert(1m * 1m * 1m == 1cub_m);
static_assert(10sq_m * 10m == 100cub_m);
static_assert(10km * 10km * 10km == 1000cub_km);
static_assert(1cub_m == 1'000'000cub_cm);
2019-11-04 06:46:53 +00:00
/* ************** DERIVED DIMENSIONS IN TERMS OF OTHER UNITS **************** */
static_assert(10N / 2m == 5Npm);
2018-09-28 07:47:37 -07:00
} // namespace