parsedouble_test.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "android-base/parsedouble.h"
  17. #include <gtest/gtest.h>
  18. TEST(parsedouble, double_smoke) {
  19. double d;
  20. ASSERT_FALSE(android::base::ParseDouble("", &d));
  21. ASSERT_FALSE(android::base::ParseDouble("x", &d));
  22. ASSERT_FALSE(android::base::ParseDouble("123.4x", &d));
  23. ASSERT_TRUE(android::base::ParseDouble("123.4", &d));
  24. ASSERT_DOUBLE_EQ(123.4, d);
  25. ASSERT_TRUE(android::base::ParseDouble("-123.4", &d));
  26. ASSERT_DOUBLE_EQ(-123.4, d);
  27. ASSERT_TRUE(android::base::ParseDouble("0", &d, 0.0));
  28. ASSERT_DOUBLE_EQ(0.0, d);
  29. ASSERT_FALSE(android::base::ParseDouble("0", &d, 1e-9));
  30. ASSERT_FALSE(android::base::ParseDouble("3.0", &d, -1.0, 2.0));
  31. ASSERT_TRUE(android::base::ParseDouble("1.0", &d, 0.0, 2.0));
  32. ASSERT_DOUBLE_EQ(1.0, d);
  33. ASSERT_FALSE(android::base::ParseDouble("123.4x", nullptr));
  34. ASSERT_TRUE(android::base::ParseDouble("-123.4", nullptr));
  35. ASSERT_FALSE(android::base::ParseDouble("3.0", nullptr, -1.0, 2.0));
  36. ASSERT_TRUE(android::base::ParseDouble("1.0", nullptr, 0.0, 2.0));
  37. }
  38. TEST(parsedouble, float_smoke) {
  39. float f;
  40. ASSERT_FALSE(android::base::ParseFloat("", &f));
  41. ASSERT_FALSE(android::base::ParseFloat("x", &f));
  42. ASSERT_FALSE(android::base::ParseFloat("123.4x", &f));
  43. ASSERT_TRUE(android::base::ParseFloat("123.4", &f));
  44. ASSERT_FLOAT_EQ(123.4, f);
  45. ASSERT_TRUE(android::base::ParseFloat("-123.4", &f));
  46. ASSERT_FLOAT_EQ(-123.4, f);
  47. ASSERT_TRUE(android::base::ParseFloat("0", &f, 0.0));
  48. ASSERT_FLOAT_EQ(0.0, f);
  49. ASSERT_FALSE(android::base::ParseFloat("0", &f, 1e-9));
  50. ASSERT_FALSE(android::base::ParseFloat("3.0", &f, -1.0, 2.0));
  51. ASSERT_TRUE(android::base::ParseFloat("1.0", &f, 0.0, 2.0));
  52. ASSERT_FLOAT_EQ(1.0, f);
  53. ASSERT_FALSE(android::base::ParseFloat("123.4x", nullptr));
  54. ASSERT_TRUE(android::base::ParseFloat("-123.4", nullptr));
  55. ASSERT_FALSE(android::base::ParseFloat("3.0", nullptr, -1.0, 2.0));
  56. ASSERT_TRUE(android::base::ParseFloat("1.0", nullptr, 0.0, 2.0));
  57. }