Skip to content

Commit f148912

Browse files
committed
unit conversion and tests
1 parent 1a5bdce commit f148912

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
#pragma once
22

3+
#include <string_view>
4+
35
#include <metatomic.h>
6+
#include <metatomic/errors.hpp>
47

58
namespace 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

metatomic-core/tests/misc.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <catch.hpp>
44

55
#include "metatomic.h"
6+
#include "metatomic.hpp"
67

78

89
TEST_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+
}

0 commit comments

Comments
 (0)