summaryrefslogtreecommitdiff
path: root/src/mongo/base/parse_number_test.cpp
diff options
context:
space:
mode:
authorApollon Oikonomopoulos <apoikos@debian.org>2016-01-14 00:10:06 +0200
committerApollon Oikonomopoulos <apollon@skroutz.gr>2016-01-14 00:10:06 +0200
commit374e1947abcd3e127a2a613aff73ecffdb9199ea (patch)
treed83973c3c9802450acd5b5e86fe0d4e8e60a3a1b /src/mongo/base/parse_number_test.cpp
parent65585c90b12d6523bea75a2aebaae2a2fdf9e641 (diff)
Imported Upstream version 2.6.11upstream/2.6.11
Diffstat (limited to 'src/mongo/base/parse_number_test.cpp')
-rw-r--r--src/mongo/base/parse_number_test.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/mongo/base/parse_number_test.cpp b/src/mongo/base/parse_number_test.cpp
index e18066b742d..585b6f33377 100644
--- a/src/mongo/base/parse_number_test.cpp
+++ b/src/mongo/base/parse_number_test.cpp
@@ -12,6 +12,18 @@
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects
+ * for all of the code used other than as permitted herein. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you do not
+ * wish to do so, delete this exception statement from your version. If you
+ * delete this exception statement from all source files in the program,
+ * then also delete it in the license file.
*/
#include "mongo/pch.h"
@@ -21,6 +33,7 @@
#include "mongo/base/parse_number.h"
#include "mongo/base/status.h"
#include "mongo/platform/cstdint.h"
+#include "mongo/platform/float_utils.h"
#include "mongo/util/mongoutils/str.h" // for str::stream()!
#include "mongo/unittest/unittest.h"
@@ -222,5 +235,77 @@ namespace {
ASSERT_PARSES(uint8_t, std::string(mongoutils::str::stream() << i), i);
}
+ TEST(Double, TestRejectingBadBases) {
+ double ignored;
+
+ // Only supported base for parseNumberFromStringWithBase<double> is 0.
+ ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", -1, &ignored));
+ ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", 1, &ignored));
+ ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", 8, &ignored));
+ ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", 10, &ignored));
+ ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", 16, &ignored));
+ }
+
+ TEST(Double, TestParsingGarbage) {
+ double d;
+ CommonNumberParsingTests<double>::TestParsingGarbage();
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>("1.0.1", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>("1.0-1", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>(" 1.0", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>("1.0P4", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>("1e6 ", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>(" 1e6", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>("1e6 ", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>(" 1e6", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString<double>("0xabcab.defPa", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse,
+ parseNumberFromString<double>(StringData("1.0\0garbage",
+ StringData::LiteralTag()),
+ &d));
+ }
+
+ TEST(Double, TestParsingOverflow) {
+ double d;
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("1e309", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("-1e309", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("1e-400", &d));
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("-1e-400", &d));
+ }
+
+ TEST(Double, TestParsingNan) {
+ double d = 0;
+ ASSERT_OK(parseNumberFromString("NaN", &d));
+ ASSERT_TRUE(isNaN(d));
+ }
+
+ TEST(Double, TestParsingInfinity) {
+ double d = 0;
+ ASSERT_OK(parseNumberFromString("infinity", &d));
+ ASSERT_TRUE(isInf(d));
+ d = 0;
+ ASSERT_OK(parseNumberFromString("-Infinity", &d));
+ ASSERT_TRUE(isInf(d));
+ }
+
+ TEST(Double, TestParsingNormal) {
+ ASSERT_PARSES(double, "10", 10);
+ ASSERT_PARSES(double, "0", 0);
+ ASSERT_PARSES(double, "1", 1);
+ ASSERT_PARSES(double, "-10", -10);
+ ASSERT_PARSES(double, "1e8", 1e8);
+ ASSERT_PARSES(double, "1e-8", 1e-8);
+ ASSERT_PARSES(double, "12e-8", 12e-8);
+ ASSERT_PARSES(double, "-485.381e-8", -485.381e-8);
+
+#if !(defined(_WIN32) || defined(__sunos__))
+ // Parse hexadecimal representations of a double. Hex literals not supported by MSVC, and
+ // not parseable by the Windows SDK libc or the Solaris libc in the mode we build.
+
+ ASSERT_PARSES(double, "0xff", 0xff);
+ ASSERT_PARSES(double, "-0xff", -0xff);
+ ASSERT_PARSES(double, "0xabcab.defdefP-10", 0xabcab.defdefP-10);
+#endif
+ }
+
} // namespace
} // namespace mongo