comparison UnitTestsSources/GenericToolboxTests.cpp @ 1171:ba08f2b0a779 broker

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