Skip to content

Commit 1478bf1

Browse files
committed
[CI] Add tests for formatDouble in serialization
1 parent cf7bbeb commit 1478bf1

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test/general/test_serialization.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,63 @@
1010
#include "cantera/transport/TransportData.h"
1111

1212
using namespace Cantera;
13+
using namespace YAML;
14+
15+
TEST(YamlWriter, formatDouble)
16+
{
17+
AnyMap m;
18+
int count;
19+
float delta;
20+
21+
// check least significant digit
22+
// default precision is 15 (see AnyMap.cpp::getPrecicion)
23+
delta = 1.e-15;
24+
count = 0;
25+
for (int i = -9; i < 10; i += 2) {
26+
m["a"] = 4. + i * delta;
27+
if (i < -5 || i > 4) {
28+
// round away from 4.
29+
EXPECT_NE(m.toYamlString(), "a: 4.0\n");
30+
} else {
31+
// round towards 4.
32+
EXPECT_EQ(m.toYamlString(), "a: 4.0\n");
33+
count++;
34+
}
35+
}
36+
EXPECT_EQ(count, 5); // only checking even multiples of delta
37+
38+
// check edge cases
39+
m["a"] = -1061.793215682400;
40+
EXPECT_EQ(m.toYamlString(), "a: -1061.7932156824\n");
41+
}
42+
43+
TEST(YamlWriter, formatDoubleExp)
44+
{
45+
AnyMap m;
46+
int count;
47+
float delta;
48+
49+
// check least significant digit
50+
// default precision is 15 (see AnyMap.cpp::getPrecicion)
51+
delta = 1.e-5;
52+
count = 0;
53+
for (int i = -9; i < 10; i += 2) {
54+
m["a"] = 4.e10 + i * delta;
55+
if (i < -5 || i > 4) {
56+
// round away from 4.
57+
EXPECT_NE(m.toYamlString(), "a: 4.0e+10\n");
58+
} else {
59+
// round towards 4.
60+
EXPECT_EQ(m.toYamlString(), "a: 4.0e+10\n");
61+
count++;
62+
}
63+
}
64+
EXPECT_EQ(count, 5); // only checking even multiples of delta
65+
66+
// check edge cases
67+
m["a"] = 1.629771953878800e+13;
68+
EXPECT_EQ(m.toYamlString(), "a: 1.6297719538788e+13\n");
69+
}
1370

1471
TEST(YamlWriter, thermoDef)
1572
{

0 commit comments

Comments
 (0)