test_complicated_system.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2007-2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <boost/type_traits/is_same.hpp>
  11. #include <boost/mpl/assert.hpp>
  12. #include <boost/units/base_unit.hpp>
  13. #include <boost/units/derived_dimension.hpp>
  14. #include <boost/units/make_system.hpp>
  15. #include <boost/units/operators.hpp>
  16. #include <boost/units/reduce_unit.hpp>
  17. #include <boost/units/unit.hpp>
  18. #include <boost/units/physical_dimensions/current.hpp>
  19. #include <boost/units/physical_dimensions/electric_potential.hpp>
  20. #include <boost/units/physical_dimensions/energy.hpp>
  21. #include <boost/units/physical_dimensions/force.hpp>
  22. #include <boost/units/physical_dimensions/length.hpp>
  23. #include <boost/units/physical_dimensions/mass.hpp>
  24. #include <boost/units/physical_dimensions/time.hpp>
  25. namespace test_system1 {
  26. // the base units in the system will be:
  27. //
  28. // volts = m^2 kg s^-2 C^-1
  29. // newtons = m kg s^-2
  30. // joules = m^2 kg s^-2
  31. // we will find the representation of m^-1 C^-1 = V N J^-2 = m^-1 C^-1
  32. // reducing the system should generate the matrix equation
  33. // 2 1 2
  34. // 1 1 1 x = c
  35. // -2 -2 -2
  36. // -1 0 0
  37. struct volt : boost::units::base_unit<volt, boost::units::electric_potential_dimension, 1> {};
  38. struct newton : boost::units::base_unit<newton, boost::units::force_dimension, 2> {};
  39. struct joule : boost::units::base_unit<joule, boost::units::energy_dimension, 3> {};
  40. typedef boost::units::make_system<volt, newton, joule>::type complicated_system;
  41. typedef boost::units::derived_dimension<
  42. boost::units::length_base_dimension, -1,
  43. boost::units::time_base_dimension, -1,
  44. boost::units::current_base_dimension, -1
  45. >::type dimension;
  46. typedef boost::units::reduce_unit<boost::units::unit<dimension, complicated_system> >::type reduced;
  47. typedef boost::units::divide_typeof_helper<
  48. boost::units::multiply_typeof_helper<volt::unit_type, newton::unit_type>::type,
  49. boost::units::power_typeof_helper<joule::unit_type, boost::units::static_rational<2> >::type
  50. >::type expected;
  51. void test() {
  52. BOOST_MPL_ASSERT((boost::is_same<reduced, expected>));
  53. }
  54. }
  55. namespace test_system2 {
  56. // the base units in the system will be:
  57. //
  58. // kilograms = kg
  59. // meters = m
  60. // we will find the representation of m and kg
  61. // reducing the system should generate the matrix equation
  62. // 0 1
  63. // 1 0 x = c
  64. struct kilogram : boost::units::base_unit<kilogram, boost::units::mass_dimension, 4> {};
  65. struct meter : boost::units::base_unit<meter, boost::units::length_dimension, 5> {};
  66. typedef boost::units::make_system<meter, kilogram>::type mk_system;
  67. typedef boost::units::reduce_unit<boost::units::unit<boost::units::mass_dimension, mk_system> >::type mass_unit;
  68. typedef boost::units::reduce_unit<boost::units::unit<boost::units::length_dimension, mk_system> >::type length_unit;
  69. void test() {
  70. BOOST_MPL_ASSERT((boost::is_same<mass_unit, kilogram::unit_type>));
  71. BOOST_MPL_ASSERT((boost::is_same<length_unit, meter::unit_type>));
  72. }
  73. }