aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/strings/numbers_test.cc
diff options
context:
space:
mode:
authorGravatar akindyakov <akindyakov@gmail.com>2018-04-20 11:23:15 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-04-20 11:25:53 -0700
commit570d90b9c7e6a19bc2606fdaf7ad0f85b8590c0e (patch)
treeae3617cdb70686297ddf6bab05d99fd44bb64224 /tensorflow/core/lib/strings/numbers_test.cc
parent49f3469d9533cb12d06ed3907b4ced975e2fcea4 (diff)
Speed up safe_strtod and safe_strtof functions by using double-conversion library
Closes #12102. PiperOrigin-RevId: 193696537
Diffstat (limited to 'tensorflow/core/lib/strings/numbers_test.cc')
-rw-r--r--tensorflow/core/lib/strings/numbers_test.cc87
1 files changed, 87 insertions, 0 deletions
diff --git a/tensorflow/core/lib/strings/numbers_test.cc b/tensorflow/core/lib/strings/numbers_test.cc
index e15161de66..0f22dac262 100644
--- a/tensorflow/core/lib/strings/numbers_test.cc
+++ b/tensorflow/core/lib/strings/numbers_test.cc
@@ -15,6 +15,7 @@ limitations under the License.
#include "tensorflow/core/lib/strings/numbers.h"
+#include <cmath>
#include <string>
#include "tensorflow/core/platform/test.h"
@@ -277,7 +278,49 @@ TEST(safe_strtof, Float) {
EXPECT_TRUE(safe_strtof("-0x2A", &result));
EXPECT_EQ(-42.0f, result);
+ EXPECT_TRUE(safe_strtof(" -0x2", &result));
+ EXPECT_EQ(-2.0f, result);
+
+ EXPECT_TRUE(safe_strtof("8 \t", &result));
+ EXPECT_EQ(8.0f, result);
+
+ EXPECT_TRUE(safe_strtof("\t20.0\t ", &result));
+ EXPECT_EQ(20.0f, result);
+
EXPECT_FALSE(safe_strtof("-infinity is awesome", &result));
+
+ // Make sure we exit cleanly if the string is not terminated
+ char test_str[2 * kFastToBufferSize];
+ for (int i = 0; i < 2 * kFastToBufferSize; ++i) test_str[i] = 'a';
+ EXPECT_FALSE(safe_strtof(test_str, &result));
+
+ // Make sure we exit cleanly if the string is too long
+ test_str[kFastToBufferSize + 1] = '\0';
+ EXPECT_FALSE(safe_strtof(test_str, &result));
+
+ EXPECT_TRUE(safe_strtof("-inf", &result));
+ EXPECT_EQ(-std::numeric_limits<float>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtof("+inf", &result));
+ EXPECT_EQ(std::numeric_limits<float>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtof("InF", &result));
+ EXPECT_EQ(std::numeric_limits<float>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtof("-INF", &result));
+ EXPECT_EQ(-std::numeric_limits<float>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtof("nan", &result));
+ EXPECT_TRUE(std::isnan(result));
+
+ EXPECT_TRUE(safe_strtof("-nan", &result));
+ EXPECT_TRUE(std::isnan(result));
+
+ EXPECT_TRUE(safe_strtof("-NaN", &result));
+ EXPECT_TRUE(std::isnan(result));
+
+ EXPECT_TRUE(safe_strtof("+NAN", &result));
+ EXPECT_TRUE(std::isnan(result));
}
TEST(safe_strtod, Double) {
@@ -287,6 +330,15 @@ TEST(safe_strtod, Double) {
EXPECT_EQ(0.1234567890123, result);
EXPECT_FALSE(safe_strtod("0.1234567890123abc", &result));
+ // Make sure we exit cleanly if the string is not terminated
+ char test_str[2 * kFastToBufferSize];
+ for (int i = 0; i < 2 * kFastToBufferSize; ++i) test_str[i] = 'a';
+ EXPECT_FALSE(safe_strtod(test_str, &result));
+
+ // Make sure we exit cleanly if the string is too long
+ test_str[kFastToBufferSize + 1] = '\0';
+ EXPECT_FALSE(safe_strtod(test_str, &result));
+
// Overflow to infinity, underflow to 0.
EXPECT_TRUE(safe_strtod("1e310", &result));
EXPECT_EQ(std::numeric_limits<double>::infinity(), result);
@@ -296,6 +348,41 @@ TEST(safe_strtod, Double) {
EXPECT_TRUE(safe_strtod("1e-325", &result));
EXPECT_EQ(0, result);
+
+ EXPECT_TRUE(safe_strtod(" -0x1c", &result));
+ EXPECT_EQ(-28.0, result);
+
+ EXPECT_TRUE(safe_strtod("50 \t", &result));
+ EXPECT_EQ(50.0, result);
+
+ EXPECT_TRUE(safe_strtod("\t82.0\t ", &result));
+ EXPECT_EQ(82.0, result);
+
+ EXPECT_FALSE(safe_strtod("infinity", &result));
+
+ EXPECT_TRUE(safe_strtod("-inf", &result));
+ EXPECT_EQ(-std::numeric_limits<double>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtod("+inf", &result));
+ EXPECT_EQ(std::numeric_limits<double>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtod("InF", &result));
+ EXPECT_EQ(std::numeric_limits<double>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtod("-INF", &result));
+ EXPECT_EQ(-std::numeric_limits<double>::infinity(), result);
+
+ EXPECT_TRUE(safe_strtod("nan", &result));
+ EXPECT_TRUE(std::isnan(result));
+
+ EXPECT_TRUE(safe_strtod("-nan", &result));
+ EXPECT_TRUE(std::isnan(result));
+
+ EXPECT_TRUE(safe_strtod("-NaN", &result));
+ EXPECT_TRUE(std::isnan(result));
+
+ EXPECT_TRUE(safe_strtod("+NAN", &result));
+ EXPECT_TRUE(std::isnan(result));
}
} // namespace strings