1 /* 2 * Copyright (C) 2018 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 17 #include "utils/strings/numbers.h" 18 19 #include "utils/base/integral_types.h" 20 #include "gtest/gtest.h" 21 22 namespace libtextclassifier3 { 23 namespace { 24 25 void TestParseInt32(const char *c_str, bool expected_parsing_success, 26 int32 expected_parsed_value = 0) { 27 int32 parsed_value = 0; 28 EXPECT_EQ(expected_parsing_success, ParseInt32(c_str, &parsed_value)); 29 if (expected_parsing_success) { 30 EXPECT_EQ(expected_parsed_value, parsed_value); 31 } 32 } 33 34 TEST(ParseInt32Test, Normal) { 35 TestParseInt32("2", true, 2); 36 TestParseInt32("-357", true, -357); 37 TestParseInt32("7", true, 7); 38 TestParseInt32("+7", true, 7); 39 TestParseInt32(" +7", true, 7); 40 TestParseInt32("-23", true, -23); 41 TestParseInt32(" -23", true, -23); 42 } 43 44 TEST(ParseInt32Test, ErrorCases) { 45 TestParseInt32("", false); 46 TestParseInt32(" ", false); 47 TestParseInt32("not-a-number", false); 48 TestParseInt32("123a", false); 49 } 50 51 void TestParseInt64(const char *c_str, bool expected_parsing_success, 52 int64 expected_parsed_value = 0) { 53 int64 parsed_value = 0; 54 EXPECT_EQ(expected_parsing_success, ParseInt64(c_str, &parsed_value)); 55 if (expected_parsing_success) { 56 EXPECT_EQ(expected_parsed_value, parsed_value); 57 } 58 } 59 60 TEST(ParseInt64Test, Normal) { 61 TestParseInt64("2", true, 2); 62 TestParseInt64("-357", true, -357); 63 TestParseInt64("7", true, 7); 64 TestParseInt64("+7", true, 7); 65 TestParseInt64(" +7", true, 7); 66 TestParseInt64("-23", true, -23); 67 TestParseInt64(" -23", true, -23); 68 } 69 70 TEST(ParseInt64Test, ErrorCases) { 71 TestParseInt64("", false); 72 TestParseInt64(" ", false); 73 TestParseInt64("not-a-number", false); 74 TestParseInt64("23z", false); 75 } 76 77 void TestParseDouble(const char *c_str, bool expected_parsing_success, 78 double expected_parsed_value = 0.0) { 79 double parsed_value = 0.0; 80 EXPECT_EQ(expected_parsing_success, ParseDouble(c_str, &parsed_value)); 81 if (expected_parsing_success) { 82 EXPECT_NEAR(expected_parsed_value, parsed_value, 0.00001); 83 } 84 } 85 86 TEST(ParseDoubleTest, Normal) { 87 TestParseDouble("2", true, 2.0); 88 TestParseDouble("-357.023", true, -357.023); 89 TestParseDouble("7.04", true, 7.04); 90 TestParseDouble("+7.2", true, 7.2); 91 TestParseDouble(" +7.236", true, 7.236); 92 TestParseDouble("-23.4", true, -23.4); 93 TestParseDouble(" -23.4", true, -23.4); 94 } 95 96 TEST(ParseDoubleTest, ErrorCases) { 97 TestParseDouble("", false); 98 TestParseDouble(" ", false); 99 TestParseDouble("not-a-number", false); 100 TestParseDouble("23.5a", false); 101 } 102 } // namespace 103 } // namespace libtextclassifier3 104