File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#pragma once
22
3+ #include < string_view>
4+
35#include < metatomic.h>
6+ #include < metatomic/errors.hpp>
47
58namespace metatomic {
69
10+ inline double unit_conversion_factor (
11+ std::string_view from_unit,
12+ std::string_view to_unit
13+ ) {
14+ double conversion = 0.0 ;
15+
16+ auto status = mta_unit_conversion_factor (from_unit.data (), to_unit.data (), &conversion);
17+ details::check_status (status);
18+
19+ return conversion;
20+ }
21+
722} // namespace metatomic
Original file line number Diff line number Diff line change 33#include < catch.hpp>
44
55#include " metatomic.h"
6+ #include " metatomic.hpp"
67
78
89TEST_CASE (" Version macros" ) {
@@ -48,7 +49,7 @@ TEST_CASE("mta_string_t") {
4849 mta_string_free (nullptr );
4950}
5051
51- TEST_CASE (" mta_unit_conversion_factor" ) {
52+ TEST_CASE (" mta_unit_conversion_factor" , " [C API] " ) {
5253 double factor = 0.0 ;
5354
5455 // same unit -> factor = 1.0
@@ -70,3 +71,21 @@ TEST_CASE("mta_unit_conversion_factor") {
7071 " invalid parameter: dimension mismatch in unit conversion: "
7172 " 'm' has dimension [L] but 'kg' has dimension [M]" );
7273}
74+
75+ TEST_CASE (" metatomic::mta_unit_conversion_factor" , " [C++ API]" ) {
76+ // same unit -> factor = 1.0
77+ auto factor = metatomic::unit_conversion_factor (" m" , " m" );
78+ CHECK (factor == 1.0 );
79+
80+ // kJ/mol -> eV
81+ factor = metatomic::unit_conversion_factor (" kJ/mol" , " eV" );
82+ CHECK (factor == Approx (0.010364269656262174 ).epsilon (1e-15 ));
83+
84+ // dimension mismatch -> error
85+ try {
86+ factor = metatomic::unit_conversion_factor (" m" , " kg" );
87+ }
88+ catch (metatomic::Error& e){
89+ CHECK (std::string (e.what ()) == " invalid parameter: dimension mismatch in unit conversion: 'm' has dimension [L] but 'kg' has dimension [M]" );
90+ }
91+ }
You can’t perform that action at this time.
0 commit comments