You've already forked openscreen

This utility function replaces the usage of absl::SimpleAtoi. Bug: 158433927 Change-Id: I8bfe880e8fd8723ef41afe7db4b1fc606496fc38 Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/5749961 Reviewed-by: Muyao Xu <muyaoxu@google.com> Commit-Queue: Mark Foltz <mfoltz@chromium.org>
40 lines
1.3 KiB
C++
40 lines
1.3 KiB
C++
// Copyright 2024 The Chromium Authors
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "util/string_parse.h"
|
|
|
|
#include <limits>
|
|
#include <string_view>
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace openscreen::string_parse {
|
|
|
|
void ExpectParseInt(std::string_view number, int expected_value) {
|
|
int result = 0;
|
|
EXPECT_TRUE(string_parse::ParseAsciiNumber(number, result));
|
|
EXPECT_EQ(expected_value, result);
|
|
}
|
|
|
|
TEST(StringParseTest, ParseAsciiNumberInt) {
|
|
ExpectParseInt("0", 0);
|
|
ExpectParseInt("0100", 100);
|
|
ExpectParseInt("13245", 13245);
|
|
ExpectParseInt("-77377", -77377);
|
|
ExpectParseInt("-2147483648", std::numeric_limits<int>::min());
|
|
ExpectParseInt("2147483647", std::numeric_limits<int>::max());
|
|
}
|
|
|
|
TEST(StringParseTest, ParseAsciiNumberFails) {
|
|
int result = 0;
|
|
EXPECT_FALSE(string_parse::ParseAsciiNumber("", result));
|
|
EXPECT_FALSE(string_parse::ParseAsciiNumber("- 100", result));
|
|
EXPECT_FALSE(string_parse::ParseAsciiNumber("ASXD", result));
|
|
EXPECT_FALSE(string_parse::ParseAsciiNumber(" 100", result));
|
|
EXPECT_FALSE(string_parse::ParseAsciiNumber("-2147483649", result));
|
|
EXPECT_FALSE(string_parse::ParseAsciiNumber("2147483648", result));
|
|
}
|
|
|
|
} // namespace openscreen::string_parse
|