comparison UnitTestsSources/GenericToolboxTests.cpp @ 1166:f68da12e852b

Added scientific notation support to StringToDouble
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 20 Nov 2019 10:47:42 +0100
parents 141593f1aa88
children 5594d8d4a70f bf08d28bc652
comparison
equal deleted inserted replaced
1143:e2212644eab3 1166:f68da12e852b
45 EXPECT_TRUE(LegitDoubleString(".")); 45 EXPECT_TRUE(LegitDoubleString("."));
46 EXPECT_TRUE(LegitDoubleString("")); 46 EXPECT_TRUE(LegitDoubleString(""));
47 EXPECT_TRUE(LegitDoubleString("0.")); 47 EXPECT_TRUE(LegitDoubleString("0."));
48 EXPECT_TRUE(LegitDoubleString(".0")); 48 EXPECT_TRUE(LegitDoubleString(".0"));
49 49
50 EXPECT_TRUE(LegitDoubleString("1e-15"));
51 EXPECT_TRUE(LegitDoubleString("1E-15"));
52 EXPECT_TRUE(LegitDoubleString("0.31E-15"));
53 EXPECT_TRUE(LegitDoubleString(".0031E-15"));
54 EXPECT_TRUE(LegitDoubleString("1e-15"));
55 EXPECT_TRUE(LegitDoubleString("1E015"));
56 EXPECT_TRUE(LegitDoubleString("0.31E015"));
57
58
50 EXPECT_FALSE(LegitDoubleString(".5f")); 59 EXPECT_FALSE(LegitDoubleString(".5f"));
51 EXPECT_FALSE(LegitDoubleString("1e-15"));
52 EXPECT_FALSE(LegitDoubleString("1E-15"));
53 EXPECT_FALSE(LegitDoubleString("0.31E-15"));
54 EXPECT_FALSE(LegitDoubleString(".0031E-15"));
55 EXPECT_FALSE(LegitDoubleString("1e-15"));
56 EXPECT_FALSE(LegitDoubleString("1E015"));
57 EXPECT_FALSE(LegitDoubleString("0.31E015"));
58 EXPECT_FALSE(LegitDoubleString("\n.0031E015")); 60 EXPECT_FALSE(LegitDoubleString("\n.0031E015"));
59 EXPECT_FALSE(LegitDoubleString(".05f")); 61 EXPECT_FALSE(LegitDoubleString(".05f"));
60 EXPECT_FALSE(LegitDoubleString(" 1 2 ")); 62 EXPECT_FALSE(LegitDoubleString(" 1 2 "));
61 EXPECT_FALSE(LegitDoubleString(" 0.12\t")); 63 EXPECT_FALSE(LegitDoubleString(" 0.12\t"));
62 EXPECT_FALSE(LegitDoubleString(" 0.12")); 64 EXPECT_FALSE(LegitDoubleString(" 0.12"));
3836 #endif 3838 #endif
3837 EXPECT_NEAR(b, r, TOLERANCE); 3839 EXPECT_NEAR(b, r, TOLERANCE);
3838 } 3840 }
3839 } 3841 }
3840 3842
3843
3844 TEST(GenericToolbox, TestStringToDoubleHardScientific)
3845 {
3846 using OrthancStone::GenericToolbox::StringToDouble;
3847 const double TOLERANCE = 0.00000000000001;
3848
3849 size_t i = 0;
3850 const size_t COUNT = 125;
3851 //const double FACTOR = 1.000000000171271211;
3852 const double FACTOR = 1.71271211;
3853 for (double b = DBL_EPSILON; b < DBL_MAX && i < COUNT; ++i, b *= FACTOR)
3854 {
3855
3856 // the tolerance must be adapted depending on the exponent
3857 double exponent = (b == 0) ? 0 : 1.0 + std::floor(std::log10(std::fabs(b)));
3858 double actualTolerance = TOLERANCE * pow(10.0, exponent);
3859
3860 char txt[1024];
3861 #if defined(_MSC_VER)
3862 sprintf_s(txt, "%.17e", b);
3863 #else
3864 snprintf(txt, sizeof(txt) - 1, "%.17e", b);
3865 #endif
3866 double r = 0.0;
3867 bool ok = StringToDouble(r, txt);
3868
3869 #if 0
3870 if (ok)
3871 {
3872 printf("OK for txt = \"%s\" and r = %.17e\n", txt, r);
3873 }
3874 else
3875 {
3876 printf("Not ok for txt = \"%s\" and r = %.17e\n", txt, r);
3877 ok = StringToDouble(r, txt);
3878 }
3879 #endif
3880
3881 EXPECT_TRUE(ok);
3882
3883 #if 0
3884 if (fabs(b - r) > actualTolerance)
3885 {
3886 printf("NOK fabs(b (%.17f) - r (%.17f)) ((%.17f)) > actualTolerance (%.17f)\n", b, r, fabs(b - r), actualTolerance);
3887 printf("NOK fabs(b (%.17e) - r (%.17e)) ((%.17e)) > actualTolerance (%.17e)\n", b, r, fabs(b - r), actualTolerance);
3888 ok = StringToDouble(r, txt);
3889 }
3890 else
3891 {
3892 printf("OK fabs(b (%.17f) - r (%.17f)) ((%.17f)) <= actualTolerance (%.17f)\n", b, r, fabs(b - r), actualTolerance);
3893 printf("OK fabs(b (%.17e) - r (%.17e)) ((%.17e)) <= actualTolerance (%.17e)\n", b, r, fabs(b - r), actualTolerance);
3894 }
3895 #endif
3896 EXPECT_NEAR(b, r, actualTolerance);
3897 }
3898 }
3899
3900 TEST(GenericToolbox, TestStringToDoubleHardNegScientific)
3901 {
3902 using OrthancStone::GenericToolbox::StringToDouble;
3903 const double TOLERANCE = 0.00000000000001;
3904
3905 size_t i = 0;
3906 const size_t COUNT = 125;
3907 //const double FACTOR = 1.000000000171271211;
3908 const double FACTOR = 1.71271211;
3909 for (double b = -1.0 * DBL_EPSILON; b < DBL_MAX && i < COUNT; ++i, b *= FACTOR)
3910 {
3911 // the tolerance must be adapted depending on the exponent
3912 double exponent = (b == 0) ? 0 : 1.0 + std::floor(std::log10(std::fabs(b)));
3913 double actualTolerance = TOLERANCE * pow(10.0, exponent);
3914
3915 char txt[1024];
3916 #if defined(_MSC_VER)
3917 sprintf_s(txt, "%.17e", b);
3918 #else
3919 snprintf(txt, sizeof(txt) - 1, "%.17e", b);
3920 #endif
3921 double r = 0.0;
3922 bool ok = StringToDouble(r, txt);
3923
3924 #if 0
3925 if (ok)
3926 {
3927 printf("OK for txt = \"%s\" and r = %.17e\n", txt, r);
3928 }
3929 else
3930 {
3931 printf("Not ok for txt = \"%s\" and r = %.17e\n", txt, r);
3932 ok = StringToDouble(r, txt);
3933 }
3934 #endif
3935
3936 EXPECT_TRUE(ok);
3937
3938 #if 0
3939 if (fabs(b - r) > actualTolerance)
3940 {
3941 printf("NOK fabs(b (%.17f) - r (%.17f)) ((%.17f)) > actualTolerance (%.17f)\n", b, r, fabs(b - r), actualTolerance);
3942 printf("NOK fabs(b (%.17e) - r (%.17e)) ((%.17e)) > actualTolerance (%.17e)\n", b, r, fabs(b - r), actualTolerance);
3943 ok = StringToDouble(r, txt);
3944 }
3945 else
3946 {
3947 printf("OK fabs(b (%.17f) - r (%.17f)) ((%.17f)) <= actualTolerance (%.17f)\n", b, r, fabs(b - r), actualTolerance);
3948 printf("OK fabs(b (%.17e) - r (%.17e)) ((%.17e)) <= actualTolerance (%.17e)\n", b, r, fabs(b - r), actualTolerance);
3949 }
3950 #endif
3951 EXPECT_NEAR(b, r, actualTolerance);
3952 }
3953 }
3954
3955
3841 TEST(GenericToolbox, TestStringToIntegerHard) 3956 TEST(GenericToolbox, TestStringToIntegerHard)
3842 { 3957 {
3843 using OrthancStone::GenericToolbox::StringToInteger; 3958 using OrthancStone::GenericToolbox::StringToInteger;
3844 3959
3845 size_t i = 0; 3960 size_t i = 0;