comparison UnitTestsSources/GenericToolboxTests.cpp @ 1679:5b8b88e5bfd6

successfully running unit tests in WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 24 Nov 2020 12:59:10 +0100
parents a1405ab3a91c
children 03afa09cfcf1
comparison
equal deleted inserted replaced
1678:1393e3393a0b 1679:5b8b88e5bfd6
24 #include <boost/lexical_cast.hpp> 24 #include <boost/lexical_cast.hpp>
25 #include <boost/enable_shared_from_this.hpp> 25 #include <boost/enable_shared_from_this.hpp>
26 26
27 #include <gtest/gtest.h> 27 #include <gtest/gtest.h>
28 #include <stdint.h> 28 #include <stdint.h>
29 #include <inttypes.h> // For PRId64
29 #include <cmath> 30 #include <cmath>
30 31
31 TEST(GenericToolbox, TestLegitDoubleString) 32 TEST(GenericToolbox, TestLegitDoubleString)
32 { 33 {
33 using OrthancStone::GenericToolbox::LegitDoubleString; 34 using OrthancStone::GenericToolbox::LegitDoubleString;
113 EXPECT_FALSE(LegitIntegerString(".5 ")); 114 EXPECT_FALSE(LegitIntegerString(".5 "));
114 EXPECT_FALSE(LegitIntegerString(" .")); 115 EXPECT_FALSE(LegitIntegerString(" ."));
115 EXPECT_FALSE(LegitIntegerString("\n0.")); 116 EXPECT_FALSE(LegitIntegerString("\n0."));
116 } 117 }
117 118
118 TEST(GenericToolbox, TestStringToDouble) 119
120 /**
121 * The very long "TestStringToDouble" was split in 4 parts. Otherwise,
122 * while running in WebAssembly (at least in "Debug" CMAKE_BUILD_TYPE
123 * with Emscripten 2.0.0), one get error "failed to asynchronously
124 * prepare wasm: CompileError: WebAssembly.instantiate(): Compiling
125 * function [...] failed: local count too large". This is because the
126 * function is too long.
127 **/
128 TEST(GenericToolbox, TestStringToDouble1)
119 { 129 {
120 using OrthancStone::GenericToolbox::StringToDouble; 130 using OrthancStone::GenericToolbox::StringToDouble;
121 131
122 const double TOLERANCE = 0.00000000000001; 132 const double TOLERANCE = 0.00000000000001;
123 double r = 0.0; 133 double r = 0.0;
124 bool ok = StringToDouble(r, "0.0001");
125 EXPECT_TRUE(ok);
126 EXPECT_NEAR(0.0001, r, TOLERANCE);
127 134
128 { 135 {
129 bool ok = StringToDouble(r, "0.0001"); 136 bool ok = StringToDouble(r, "0.0001");
130 EXPECT_TRUE(ok); 137 EXPECT_TRUE(ok);
131 EXPECT_NEAR(0.0001, r, TOLERANCE); 138 EXPECT_NEAR(0.0001, r, TOLERANCE);
132 } 139 }
133 140
134 { 141 {
142 bool ok = StringToDouble(r, "0.0001");
143 EXPECT_TRUE(ok);
144 EXPECT_NEAR(0.0001, r, TOLERANCE);
145 }
146
147 {
135 bool ok = StringToDouble(r, "-0.50217817069333900000"); 148 bool ok = StringToDouble(r, "-0.50217817069333900000");
136 EXPECT_TRUE(ok); 149 EXPECT_TRUE(ok);
137 EXPECT_NEAR(-0.50217817069333900000, r, TOLERANCE); 150 EXPECT_NEAR(-0.50217817069333900000, r, TOLERANCE);
138 } 151 }
139 152
962 { 975 {
963 bool ok = StringToDouble(r, "-0.83191012798055900000"); 976 bool ok = StringToDouble(r, "-0.83191012798055900000");
964 EXPECT_TRUE(ok); 977 EXPECT_TRUE(ok);
965 EXPECT_NEAR(-0.83191012798055900000, r, TOLERANCE); 978 EXPECT_NEAR(-0.83191012798055900000, r, TOLERANCE);
966 } 979 }
980 }
981
982 TEST(GenericToolbox, TestStringToDouble2)
983 {
984 using OrthancStone::GenericToolbox::StringToDouble;
985
986 const double TOLERANCE = 0.00000000000001;
987 double r = 0.0;
967 988
968 { 989 {
969 bool ok = StringToDouble(r, "2.58090819604341000000"); 990 bool ok = StringToDouble(r, "2.58090819604341000000");
970 EXPECT_TRUE(ok); 991 EXPECT_TRUE(ok);
971 EXPECT_NEAR(2.58090819604341000000, r, TOLERANCE); 992 EXPECT_NEAR(2.58090819604341000000, r, TOLERANCE);
1814 { 1835 {
1815 bool ok = StringToDouble(r, "0.89377268220461400000"); 1836 bool ok = StringToDouble(r, "0.89377268220461400000");
1816 EXPECT_TRUE(ok); 1837 EXPECT_TRUE(ok);
1817 EXPECT_NEAR(0.89377268220461400000, r, TOLERANCE); 1838 EXPECT_NEAR(0.89377268220461400000, r, TOLERANCE);
1818 } 1839 }
1840 }
1841
1842 TEST(GenericToolbox, TestStringToDouble3)
1843 {
1844 using OrthancStone::GenericToolbox::StringToDouble;
1845
1846 const double TOLERANCE = 0.00000000000001;
1847 double r = 0.0;
1819 1848
1820 { 1849 {
1821 bool ok = StringToDouble(r, "1.45674815825147000000"); 1850 bool ok = StringToDouble(r, "1.45674815825147000000");
1822 EXPECT_TRUE(ok); 1851 EXPECT_TRUE(ok);
1823 EXPECT_NEAR(1.45674815825147000000, r, TOLERANCE); 1852 EXPECT_NEAR(1.45674815825147000000, r, TOLERANCE);
2780 { 2809 {
2781 bool ok = StringToDouble(r, "-1.49620375847259000000"); 2810 bool ok = StringToDouble(r, "-1.49620375847259000000");
2782 EXPECT_TRUE(ok); 2811 EXPECT_TRUE(ok);
2783 EXPECT_NEAR(-1.49620375847259000000, r, TOLERANCE); 2812 EXPECT_NEAR(-1.49620375847259000000, r, TOLERANCE);
2784 } 2813 }
2814 }
2815
2816 TEST(GenericToolbox, TestStringToDouble4)
2817 {
2818 using OrthancStone::GenericToolbox::StringToDouble;
2819
2820 const double TOLERANCE = 0.00000000000001;
2821 double r = 0.0;
2785 2822
2786 { 2823 {
2787 bool ok = StringToDouble(r, "-2.04336416841016000000"); 2824 bool ok = StringToDouble(r, "-2.04336416841016000000");
2788 EXPECT_TRUE(ok); 2825 EXPECT_TRUE(ok);
2789 EXPECT_NEAR(-2.04336416841016000000, r, TOLERANCE); 2826 EXPECT_NEAR(-2.04336416841016000000, r, TOLERANCE);
4175 //const double FACTOR = 1.000000000171271211; 4212 //const double FACTOR = 1.000000000171271211;
4176 const double FACTOR = 1.71271211; 4213 const double FACTOR = 1.71271211;
4177 for (double b = DBL_EPSILON; b < DBL_MAX && i < COUNT; ++i, b *= FACTOR) 4214 for (double b = DBL_EPSILON; b < DBL_MAX && i < COUNT; ++i, b *= FACTOR)
4178 { 4215 {
4179 int64_t bi = static_cast<int64_t>(b); 4216 int64_t bi = static_cast<int64_t>(b);
4217
4180 char txt[1024]; 4218 char txt[1024];
4181 #if defined(_MSC_VER) 4219 #if defined(_MSC_VER)
4182 # if (_MSC_VER > 1800) 4220 # if (_MSC_VER > 1800)
4183 sprintf_s(txt, "%lld", bi); 4221 sprintf_s(txt, "%lld", bi);
4184 # else 4222 # else
4185 sprintf_s(txt, "%I64d", bi); 4223 sprintf_s(txt, "%I64d", bi);
4186 # endif 4224 # endif
4187 #else 4225 #else
4188 snprintf(txt, sizeof(txt) - 1, "%ld", bi); 4226 snprintf(txt, sizeof(txt) - 1, "%" PRId64, bi); // https://stackoverflow.com/a/9225648/881731
4189 #endif 4227 #endif
4228
4190 int64_t r = 0; 4229 int64_t r = 0;
4191 bool ok = StringToInteger<int64_t>(r, txt); 4230 bool ok = StringToInteger<int64_t>(r, txt);
4192 EXPECT_TRUE(ok); 4231 EXPECT_TRUE(ok);
4193 EXPECT_EQ(bi, r); 4232 EXPECT_EQ(bi, r);
4194 #if 0 4233 #if 0
4195 if (ok) 4234 if (ok)
4196 { 4235 {
4197 printf("OK for b = %.17f bi = %lld txt = \"%s\" and r = %lld\n", b, bi, txt, r); 4236 printf("OK for b = %.17f bi = %" PRId64 " txt = \"%s\" and r = %" PRId64 "\n", b, bi, txt, r);
4198 } 4237 }
4199 else 4238 else
4200 { 4239 {
4201 printf("NOK for b = %.17f bi = %lld txt = \"%s\" and r = %lld\n", b, bi, txt,r); 4240 printf("NOK for b = %.17f bi = %" PRId64 " txt = \"%s\" and r = %" PRId64 "\n", b, bi, txt, r);
4202 ok = StringToInteger<int64_t>(r, txt); 4241 ok = StringToInteger<int64_t>(r, txt);
4203 } 4242 }
4204 #endif 4243 #endif
4205 } 4244 }
4206 } 4245 }