Belle II Software development
unit.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/gearbox/Unit.h>
9
10#include <framework/utilities/TestHelpers.h>
11
12#include <gtest/gtest.h>
13#include <cmath>
14
15using namespace std;
16using namespace Belle2;
17
18namespace {
20 TEST(UnitTest, MultiplyDivide)
21 {
22 //set to 10mm in standard units
23 double length = 10.0 * Unit::mm;
24 //=1.0 cm
25 EXPECT_DOUBLE_EQ(1.0, length);
26
27 EXPECT_DOUBLE_EQ(10.0, length / Unit::mm);
28 EXPECT_DOUBLE_EQ(10000.0, length / Unit::um);
29
30 EXPECT_DOUBLE_EQ(1e9, 1 * Unit::s);
31 }
32
34 TEST(UnitTest, ConvertValue)
35 {
36 //check standard units
37 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "cm"));
38 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "ns"));
39 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "rad"));
40 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "GeV"));
41 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "K"));
42 EXPECT_DOUBLE_EQ(1e-4, Unit::convertValue(1.0, "T"));
43 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "e"));
44 EXPECT_DOUBLE_EQ(1.0, Unit::convertValue(1.0, "g/cm3"));
45
46 //different orders of magnitude
47 EXPECT_DOUBLE_EQ(1e2, Unit::convertValue(1.0, "m"));
48 EXPECT_DOUBLE_EQ(1e5, Unit::convertValue(1.0, "km"));
49 EXPECT_DOUBLE_EQ(1e-1, Unit::convertValue(1.0, "mm"));
50 EXPECT_DOUBLE_EQ(1e-4, Unit::convertValue(1.0, "um"));
51 EXPECT_DOUBLE_EQ(1e-7, Unit::convertValue(1.0, "nm"));
52
53 //convert some actual values
54 EXPECT_DOUBLE_EQ(5e3 * 1e2, Unit::convertValue(5e3, "m"));
55 EXPECT_DOUBLE_EQ(5e-3 * 1e2, Unit::convertValue(5e-3, "m"));
56 EXPECT_DOUBLE_EQ(M_PI, Unit::convertValue(180.0, "deg"));
57
58 //test fall-back behaviour (return value unchanged, throw B2ERROR)
59 EXPECT_B2ERROR(EXPECT_DOUBLE_EQ(5e3, Unit::convertValue(5e3, "nonexistingunit")));
60 }
61
63 TEST(UnitTest, ConvertValueToUnit)
64 {
65 //check standard units
66 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "cm"));
67 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "ns"));
68 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "rad"));
69 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "GeV"));
70 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "K"));
71 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1e-4, "T"));
72 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "e"));
73 EXPECT_DOUBLE_EQ(1.0, Unit::convertValueToUnit(1.0, "g/cm3"));
74
75 //different orders of magnitude
76 EXPECT_DOUBLE_EQ(1e-2, Unit::convertValueToUnit(1.0, "m"));
77 EXPECT_DOUBLE_EQ(1e-5, Unit::convertValueToUnit(1.0, "km"));
78 EXPECT_DOUBLE_EQ(1e1, Unit::convertValueToUnit(1.0, "mm"));
79 EXPECT_DOUBLE_EQ(1e4, Unit::convertValueToUnit(1.0, "um"));
80 EXPECT_DOUBLE_EQ(1e7, Unit::convertValueToUnit(1.0, "nm"));
81
82 //convert some actual values
83 EXPECT_DOUBLE_EQ(5e3 * 1e-2, Unit::convertValueToUnit(5e3, "m"));
84 EXPECT_DOUBLE_EQ(5e-3 * 1e-2, Unit::convertValueToUnit(5e-3, "m"));
85 EXPECT_DOUBLE_EQ(180.0, Unit::convertValueToUnit(M_PI, "deg"));
86
87 //test fall-back behaviour (return value unchanged, throw B2ERROR)
88 EXPECT_B2ERROR(EXPECT_DOUBLE_EQ(5e3, Unit::convertValueToUnit(5e3, "nonexistingunit")));
89 }
90
91} // namespace
static const double mm
[millimeters]
Definition: Unit.h:70
static const double um
[micrometers]
Definition: Unit.h:71
static const double s
[second]
Definition: Unit.h:95
static double convertValueToUnit(double value, const std::string &unitString)
Converts a floating point value from the standard framework unit to the given unit.
Definition: UnitConst.cc:139
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:129
Abstract base class for different kinds of events.
STL namespace.