summaryrefslogtreecommitdiff
path: root/src/mongo/base/parse_number_test.cpp
blob: d5afb89157e62ec9d4340f3865d5eafe7ca4a546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 *    Copyright (C) 2012 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    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/>.
 */

#include "mongo/pch.h"

#include <limits>

#include "mongo/base/parse_number.h"
#include "mongo/base/status.h"
#include "mongo/util/mongoutils/str.h"  // for str::stream()!
#include "mongo/unittest/unittest.h"

#define ASSERT_OK(EXPR) ASSERT_EQUALS(::mongo::Status::OK(), (EXPR))

#define ASSERT_PARSES(TYPE, INPUT_STRING, EXPECTED_VALUE) do {  \
        TYPE v;                                                 \
        ASSERT_OK(parseNumberFromString(INPUT_STRING, &v));     \
        ASSERT_EQUALS(static_cast<TYPE>(EXPECTED_VALUE), v);    \
    } while (false)

#define ASSERT_PARSES_WITH_BASE(TYPE, INPUT_STRING, BASE, EXPECTED_VALUE) do { \
        TYPE v;                                                         \
        ASSERT_OK(parseNumberFromStringWithBase(INPUT_STRING, BASE, &v)); \
        ASSERT_EQUALS(static_cast<TYPE>(EXPECTED_VALUE), v);            \
    } while (false)

namespace mongo {
namespace {

    template <typename _NumberType>
    class CommonNumberParsingTests {
    public:
        typedef _NumberType NumberType;
        typedef std::numeric_limits<NumberType> Limits;

        static void TestRejectingBadBases() {
            NumberType ignored;
            ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", -1, &ignored));
            ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("10", 1, &ignored));
            ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("-10", 37, &ignored));
            ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase(" ", -1, &ignored));
            ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("f", 37, &ignored));
            ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("^%", -1, &ignored));
        }

        static void TestParsingNonNegatives() {
            ASSERT_PARSES(NumberType, "10", 10);
            ASSERT_PARSES(NumberType, "0", 0);
            ASSERT_PARSES(NumberType, "0xff", 0xff);
            ASSERT_PARSES(NumberType, "077", 077);
        }

        static void TestParsingNegatives() {
            if (Limits::is_signed) {
                ASSERT_PARSES(NumberType, "-10", -10);
                ASSERT_PARSES(NumberType, "-0xff", -0xff);
                ASSERT_PARSES(NumberType, "-077", -077);
            }
            else {
                NumberType ignored;
                ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("-10", &ignored));
                ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("-0xff", &ignored));
                ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("-077", &ignored));
            }
        }

        static void TestParsingGarbage() {
            NumberType ignored;
            ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("", &ignored));
            ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString(" ", &ignored));
            ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString(" 10", &ignored));
            ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("15b", &ignored));
        }

        static void TestParsingWithExplicitBase() {
            NumberType ignored;
            ASSERT_PARSES_WITH_BASE(NumberType, "15b", 16, 0x15b);
            ASSERT_PARSES_WITH_BASE(NumberType, "77", 8, 077);
            ASSERT_PARSES_WITH_BASE(NumberType, "z", 36, 35);
            ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromStringWithBase("1b", 10, &ignored));
            ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromStringWithBase("80", 8, &ignored));
        }

        static void TestParsingLimits() {
            using namespace mongoutils;
            NumberType ignored;
            ASSERT_PARSES(NumberType, (str::stream() << Limits::max()), Limits::max());
            ASSERT_PARSES(NumberType, (str::stream() << Limits::min()), Limits::min());
            ASSERT_EQUALS(ErrorCodes::FailedToParse,
                          parseNumberFromString((str::stream() << Limits::max() << '0'), &ignored));

            if (Limits::is_signed)
                ASSERT_EQUALS(
                        ErrorCodes::FailedToParse,
                        parseNumberFromString((str::stream() << Limits::min() << '0'), &ignored));
        }
    };

#define GENERAL_NUMBER_TESTS(SHORT_NAME, TYPE)                          \
    class ParseNumberTests##SHORT_NAME : public unittest::Test {        \
    public:                                                             \
        typedef CommonNumberParsingTests<TYPE> TestFns;                 \
    };                                                                  \
    TEST_F(ParseNumberTests##SHORT_NAME, RejectBadBases) {              \
        TestFns::TestRejectingBadBases();                               \
    }                                                                   \
    TEST_F(ParseNumberTests##SHORT_NAME, ParseNonNegatives) {           \
        TestFns::TestParsingNonNegatives();                             \
    }                                                                   \
    TEST_F(ParseNumberTests##SHORT_NAME, ParseNegatives) {              \
        TestFns::TestParsingNegatives();                                \
    }                                                                   \
    TEST_F(ParseNumberTests##SHORT_NAME, ParseGarbage) {                \
        TestFns::TestParsingGarbage();                                  \
    }                                                                   \
    TEST_F(ParseNumberTests##SHORT_NAME, ParseWithExplicitBase) {       \
        TestFns::TestParsingWithExplicitBase();                         \
    }                                                                   \
    TEST_F(ParseNumberTests##SHORT_NAME, TestParsingLimits) {           \
        TestFns::TestParsingLimits();                                   \
    }

    GENERAL_NUMBER_TESTS(Short, short)
    GENERAL_NUMBER_TESTS(Int, int)
    GENERAL_NUMBER_TESTS(Long, long)
    GENERAL_NUMBER_TESTS(LongLong, long long)
    GENERAL_NUMBER_TESTS(UnsignedShort, unsigned short)
    GENERAL_NUMBER_TESTS(UnsignedInt, unsigned int)
    GENERAL_NUMBER_TESTS(UnsignedLong, unsigned long)
    GENERAL_NUMBER_TESTS(UnsignedLongLong, unsigned long long)
    GENERAL_NUMBER_TESTS(Int16, int16_t);
    GENERAL_NUMBER_TESTS(Int32, int32_t);
    GENERAL_NUMBER_TESTS(Int64, int64_t);
    GENERAL_NUMBER_TESTS(UInt16, uint16_t);
    GENERAL_NUMBER_TESTS(UInt32, uint32_t);
    GENERAL_NUMBER_TESTS(UInt64, uint64_t);

}  // namespace
}  // namespace mongo