Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F90980726
units.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Nov 6, 15:09
Size
4 KB
Mime Type
text/x-c
Expires
Fri, Nov 8, 15:09 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
22163951
Attached To
rSPECMICP SpecMiCP / ReactMiCP
units.cpp
View Options
/*-------------------------------------------------------------------------------
Copyright (c) 2015 F. Georget <fabieng@princeton.edu>, Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------*/
#include "catch.hpp"
#include "physics/units.hpp"
#include <float.h>
using
namespace
specmicp
::
units
;
bool
inline
check_unit
(
const
std
::
string
&
unit
,
AmountUnitType
type
,
double
factor
)
{
AmountUnit
tmp
;
parse_amount_unit
(
unit
,
tmp
);
return
(
tmp
.
type
==
type
and
std
::
abs
(
tmp
.
factor_si
-
factor
)
<=
DBL_EPSILON
);
}
TEST_CASE
(
"Units"
,
"[Units]"
)
{
SECTION
(
"Length unit"
)
{
CHECK
(
scaling_factor
(
LengthUnit
::
meter
)
==
1.0
);
CHECK
(
scaling_factor
(
LengthUnit
::
decimeter
)
==
0.1
);
CHECK
(
scaling_factor
(
LengthUnit
::
centimeter
)
==
0.01
);
CHECK
(
from_scaling_factor
<
LengthUnit
>
(
5.0
)
==
LengthUnit
::
meter
);
specmicp
::
scalar_t
factor
=
0.5
;
CHECK
(
from_scaling_factor
<
LengthUnit
>
(
factor
)
==
LengthUnit
::
decimeter
);
CHECK
(
factor
==
Approx
(
5.0
));
factor
=
1e-3
;
CHECK
(
from_scaling_factor
<
LengthUnit
>
(
factor
)
==
LengthUnit
::
centimeter
);
CHECK
(
factor
==
Approx
(
0.1
));
}
SECTION
(
"Mass unit"
)
{
CHECK
(
scaling_factor
(
MassUnit
::
kilogram
)
==
1.0
);
CHECK
(
scaling_factor
(
MassUnit
::
gram
)
==
1e-3
);
CHECK
(
from_scaling_factor
<
MassUnit
>
(
0.1
)
==
MassUnit
::
kilogram
);
specmicp
::
scalar_t
factor
=
2e-3
;
CHECK
(
from_scaling_factor
<
MassUnit
>
(
factor
)
==
MassUnit
::
gram
);
CHECK
(
factor
==
Approx
(
2.0
));
}
}
TEST_CASE
(
"Find units"
,
"[Units]"
)
{
SECTION
(
"is_volume_unit"
)
{
CHECK
(
is_volume_unit
(
"m^3"
)
==
1.0
);
CHECK
(
is_volume_unit
(
"dm^3"
)
==
1e-3
);
CHECK
(
is_volume_unit
(
"cm^3"
)
==
1e-6
);
CHECK
(
is_volume_unit
(
"mm^3"
)
==
1e-9
);
CHECK
(
is_volume_unit
(
"m"
)
<
0
);
}
SECTION
(
"is_length_unit"
)
{
CHECK
(
is_length_unit
(
"m"
)
==
1.0
);
CHECK
(
is_length_unit
(
"dm"
)
==
0.1
);
CHECK
(
is_length_unit
(
"cm"
)
==
0.01
);
CHECK
(
is_length_unit
(
"mm"
)
==
1e-3
);
CHECK
(
is_length_unit
(
"m^3"
)
<
0
);
}
SECTION
(
"is_mass_unit"
)
{
CHECK
(
is_mass_unit
(
"kg"
)
==
1.0
);
CHECK
(
is_mass_unit
(
"g"
)
==
1e-3
);
CHECK
(
is_mass_unit
(
"m"
)
<
0
);
}
SECTION
(
"is_mole_unit"
)
{
CHECK
(
is_mole_unit
(
"mol"
)
==
1.0
);
CHECK
(
is_mole_unit
(
"mmol"
)
==
1e-3
);
CHECK
(
is_mole_unit
(
"kg"
)
<
0
);
}
SECTION
(
"Find units"
)
{
CHECK
(
check_unit
(
"kg"
,
AmountUnitType
::
Mass
,
1.0
));
CHECK
(
check_unit
(
"g"
,
AmountUnitType
::
Mass
,
1e-3
));
CHECK
(
check_unit
(
" m^3 "
,
AmountUnitType
::
Volume
,
1.0
));
CHECK
(
check_unit
(
"dm^3"
,
AmountUnitType
::
Volume
,
1e-3
));
CHECK
(
check_unit
(
"cm^3 "
,
AmountUnitType
::
Volume
,
1e-6
));
CHECK
(
check_unit
(
"mm^3"
,
AmountUnitType
::
Volume
,
1e-9
));
CHECK
(
check_unit
(
"mol "
,
AmountUnitType
::
NumberOfMoles
,
1.0
));
CHECK
(
check_unit
(
"mmol"
,
AmountUnitType
::
NumberOfMoles
,
1e-3
));
CHECK
(
check_unit
(
"mol/kg"
,
AmountUnitType
::
Molality
,
1.0
));
CHECK
(
check_unit
(
"mol/g"
,
AmountUnitType
::
Molality
,
1e3
));
CHECK
(
check_unit
(
"mol /cm^3"
,
AmountUnitType
::
MoleConcentration
,
1e6
));
CHECK
(
check_unit
(
"kg/ m^3"
,
AmountUnitType
::
MassConcentration
,
1.0
));
CHECK
(
check_unit
(
"oups"
,
AmountUnitType
::
Unknown
,
-
1.0
));
CHECK
(
check_unit
(
"oups/ bummer"
,
AmountUnitType
::
Unknown
,
-
1.0
));
}
}
Event Timeline
Log In to Comment