From aee307f02b26f0d1ea6ac4818f74eb2764247ac1 Mon Sep 17 00:00:00 2001 From: Daniel Thayer Date: Thu, 2 Nov 2017 16:16:06 -0500 Subject: [PATCH 1/2] Add more test cases to ascii-double.bro --- .../json.log | 22 ++++++ .../test.log | 18 ++++- .../base/frameworks/logging/ascii-double.bro | 76 ++++++++++++++++--- 3 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/json.log diff --git a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/json.log b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/json.log new file mode 100644 index 0000000000..49b3c5d172 --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/json.log @@ -0,0 +1,22 @@ +{"d":2.153226e+09} +{"d":2.153226e+09} +{"d":2.153226e+09} +{"d":1.0} +{"d":1.1} +{"d":1.123457} +{"d":-1.123457} +{"d":1.1234} +{"d":0.1234} +{"d":50000.0} +{"d":-50000.0} +{"d":3.140000e+15} +{"d":-3.140000e+15} +{"d":1.790000e+308} +{"d":-1.790000e+308} +{"d":0.000012} +{"d":0} +{"d":-0} +{"d":inf} +{"d":-inf} +{"d":0.0} +{"d":nan} diff --git a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log index 7fb6492f1b..45f8783c7b 100644 --- a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log +++ b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log @@ -3,7 +3,7 @@ #empty_field (empty) #unset_field - #path test -#open 2016-05-23-22-44-54 +#open 2017-11-02-21-00-25 #fields d #types double 2153226000.0 @@ -12,6 +12,20 @@ 1.0 1.1 1.123457 +-1.123457 1.1234 +0.1234 +50000.0 +-50000.0 3140000000000000.0 -#close 2016-05-23-22-44-54 +-3140000000000000.0 +NAN.0 +NAN.0 +0.000012 +0 +-0 +inf +-inf +0.0 +nan +#close 2017-11-02-21-00-25 diff --git a/testing/btest/scripts/base/frameworks/logging/ascii-double.bro b/testing/btest/scripts/base/frameworks/logging/ascii-double.bro index e6d9a05e28..b824d93676 100644 --- a/testing/btest/scripts/base/frameworks/logging/ascii-double.bro +++ b/testing/btest/scripts/base/frameworks/logging/ascii-double.bro @@ -1,6 +1,10 @@ +# @TEST-DOC: Test that the ASCII writer logs values of type "double" correctly. # +# @TEST-EXEC: bro -b %INPUT test-json.bro +# @TEST-EXEC: mv test.log json.log # @TEST-EXEC: bro -b %INPUT # @TEST-EXEC: btest-diff test.log +# @TEST-EXEC: btest-diff json.log # # Make sure we do not write out scientific notation for doubles. @@ -14,16 +18,68 @@ export { }; } -event bro_init() +function logwrite(val: double) { - Log::create_stream(Test::LOG, [$columns=Info]); - Log::write(Test::LOG, [$d=2153226000.0]); - Log::write(Test::LOG, [$d=2153226000.1]); - Log::write(Test::LOG, [$d=2153226000.123456789]); - Log::write(Test::LOG, [$d=1.0]); - Log::write(Test::LOG, [$d=1.1]); - Log::write(Test::LOG, [$d=1.123456789]); - Log::write(Test::LOG, [$d=1.1234]); - Log::write(Test::LOG, [$d=3.14e15]); + Log::write(Test::LOG, [$d=val]); } +event bro_init() +{ + local d: double; + local dmax: double = 1.79e308; + local dmin: double = 2.23e-308; + + Log::create_stream(Test::LOG, [$columns=Info]); + + # relatively large values + logwrite(2153226000.0); + logwrite(2153226000.1); + logwrite(2153226000.123456789); + + # relatively small values + logwrite(1.0); + logwrite(1.1); + logwrite(1.123456789); + logwrite(-1.123456789); + logwrite(1.1234); + logwrite(.1234); + + # scientific notation (positive exponents) + logwrite(5e4); + logwrite(-5e4); + logwrite(3.14e15); + logwrite(-3.14e15); + logwrite(dmax); + logwrite(-dmax); + + # scientific notation (negative exponents) + logwrite(1.23456789e-5); + logwrite(dmin); + logwrite(-dmin); + + # inf + d = dmax; # ok + d = d * 2.0; # inf + logwrite(d); + + # -inf + d = -dmax; # ok + d = d * 2.0; # -inf + logwrite(d); + + # negative zero (compares equal to 0.0, but has different representation) + d = -0.0; + logwrite(d); + + # nan + d = dmax; # ok + d = d * 2.0; # inf + d = d * 0.0; # nan + logwrite(d); +} + +# @TEST-START-FILE test-json.bro + +redef LogAscii::use_json = T; + +# @TEST-END-FILE From 6e89505d06f4acb83abcebf167795a6e6bf01d0b Mon Sep 17 00:00:00 2001 From: Daniel Thayer Date: Mon, 6 Nov 2017 14:01:07 -0600 Subject: [PATCH 2/2] Fix ASCII logging of very large values of type "double" Increased the size of a buffer to be large enough to contain all the characters of the largest possible "double" value when scientific notation is not being used (previously, the nonsensical "NAN.0" would be written to ASCII logs for any value >= 1e248). --- src/Desc.cc | 4 +++- .../scripts.base.frameworks.logging.ascii-double/test.log | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Desc.cc b/src/Desc.cc index 1d76c32e55..b64bcec8d8 100644 --- a/src/Desc.cc +++ b/src/Desc.cc @@ -145,7 +145,9 @@ void ODesc::Add(double d, bool no_exp) AddBytes(&d, sizeof(d)); else { - char tmp[256]; + // Buffer needs enough chars to store max. possible "double" value + // of 1.79e308 without using scientific notation. + char tmp[350]; if ( no_exp ) modp_dtoa3(d, tmp, sizeof(tmp), IsReadable() ? 6 : 8); diff --git a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log index 45f8783c7b..9d5dd6ecf0 100644 --- a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log +++ b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-double/test.log @@ -3,7 +3,7 @@ #empty_field (empty) #unset_field - #path test -#open 2017-11-02-21-00-25 +#open 2017-11-06-19-58-08 #fields d #types double 2153226000.0 @@ -19,8 +19,8 @@ -50000.0 3140000000000000.0 -3140000000000000.0 -NAN.0 -NAN.0 +178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.0 +-178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.0 0.000012 0 -0 @@ -28,4 +28,4 @@ inf -inf 0.0 nan -#close 2017-11-02-21-00-25 +#close 2017-11-06-19-58-08