summaryrefslogtreecommitdiff
path: root/chromium/net/base/parse_number_unittest.cc
blob: 79cac9f43ba211b26aa8d58d1c943154b387f911 (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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/parse_number.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace {

TEST(ParseNumberTest, IntValidInputs) {
  const struct {
    const char* input;
    int output;
  } kTests[] = {
      {"0", 0},     {"00000", 0}, {"003", 3}, {"003", 3}, {"1234566", 1234566},
      {"987", 987}, {"010", 10},
  };

  for (const auto& test : kTests) {
    int result;
    ASSERT_TRUE(ParseNonNegativeDecimalInt(test.input, &result))
        << "Failed to parse: " << test.input;
    EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input;
  }
}

TEST(ParseNumberTest, IntInvalidInputs) {
  const char* kTests[] = {
      "",
      "-23",
      "+42",
      " 123",
      "123 ",
      "123\n",
      "0xFF",
      "0x11",
      "x11",
      "F11",
      "AF",
      "0AF",
      "0.0",
      "13.",
      "13,000",
      "13.000",
      "13/5",
      "9999999999999999999999999999999999999999999999999999999999999999",
      "Inf",
      "NaN",
      "null",
      "dog",
  };

  for (const auto& input : kTests) {
    int result = 0xDEAD;
    ASSERT_FALSE(ParseNonNegativeDecimalInt(input, &result))
        << "Succeeded to parse: " << input;
    EXPECT_EQ(0xDEAD, result) << "Modified output for failed parsing";
  }
}

TEST(ParseNumberTest, IntInvalidInputsContainsNul) {
  int result = 0xDEAD;
  ASSERT_FALSE(
      ParseNonNegativeDecimalInt(base::StringPiece("123\0", 4), &result));
  EXPECT_EQ(0xDEAD, result);
}

}  // namespace
}  // namespace net