xref: /aosp_15_r20/external/libtextclassifier/native/utils/strings/numbers_test.cc (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
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 
TestParseInt32(const char * c_str,bool expected_parsing_success,int32 expected_parsed_value=0)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 
TEST(ParseInt32Test,Normal)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   TestParseInt32("04", true, 4);
43   TestParseInt32("07", true, 7);
44   TestParseInt32("08", true, 8);
45   TestParseInt32("09", true, 9);
46 }
47 
TEST(ParseInt32Test,ErrorCases)48 TEST(ParseInt32Test, ErrorCases) {
49   TestParseInt32("", false);
50   TestParseInt32("  ", false);
51   TestParseInt32("not-a-number", false);
52   TestParseInt32("123a", false);
53 }
54 
TestParseInt64(const char * c_str,bool expected_parsing_success,int64 expected_parsed_value=0)55 void TestParseInt64(const char *c_str, bool expected_parsing_success,
56                     int64 expected_parsed_value = 0) {
57   int64 parsed_value = 0;
58   EXPECT_EQ(expected_parsing_success, ParseInt64(c_str, &parsed_value));
59   if (expected_parsing_success) {
60     EXPECT_EQ(expected_parsed_value, parsed_value);
61   }
62 }
63 
TEST(ParseInt64Test,Normal)64 TEST(ParseInt64Test, Normal) {
65   TestParseInt64("2", true, 2);
66   TestParseInt64("-357", true, -357);
67   TestParseInt64("7", true, 7);
68   TestParseInt64("+7", true, 7);
69   TestParseInt64("  +7", true, 7);
70   TestParseInt64("-23", true, -23);
71   TestParseInt64("  -23", true, -23);
72   TestParseInt64("07", true, 7);
73   TestParseInt64("08", true, 8);
74 }
75 
TEST(ParseInt64Test,ErrorCases)76 TEST(ParseInt64Test, ErrorCases) {
77   TestParseInt64("", false);
78   TestParseInt64("  ", false);
79   TestParseInt64("not-a-number", false);
80   TestParseInt64("23z", false);
81 }
82 
TestParseDouble(const char * c_str,bool expected_parsing_success,double expected_parsed_value=0.0)83 void TestParseDouble(const char *c_str, bool expected_parsing_success,
84                      double expected_parsed_value = 0.0) {
85   double parsed_value = 0.0;
86   EXPECT_EQ(expected_parsing_success, ParseDouble(c_str, &parsed_value));
87   if (expected_parsing_success) {
88     EXPECT_NEAR(expected_parsed_value, parsed_value, 0.00001);
89   }
90 }
91 
TEST(ParseDoubleTest,Normal)92 TEST(ParseDoubleTest, Normal) {
93   TestParseDouble("2", true, 2.0);
94   TestParseDouble("-357.023", true, -357.023);
95   TestParseDouble("7.04", true, 7.04);
96   TestParseDouble("+7.2", true, 7.2);
97   TestParseDouble("  +7.236", true, 7.236);
98   TestParseDouble("-23.4", true, -23.4);
99   TestParseDouble("  -23.4", true, -23.4);
100 }
101 
TEST(ParseDoubleTest,ErrorCases)102 TEST(ParseDoubleTest, ErrorCases) {
103   TestParseDouble("", false);
104   TestParseDouble("  ", false);
105   TestParseDouble("not-a-number", false);
106   TestParseDouble("23.5a", false);
107 }
108 }  // namespace
109 }  // namespace libtextclassifier3
110