1 // Copyright 2016 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "core/fxcrt/fx_system.h" 6 #include "testing/gtest/include/gtest/gtest.h" 7 8 TEST(fxcrt, FXSYS_atoi) { 9 EXPECT_EQ(0, FXSYS_atoi("")); 10 EXPECT_EQ(0, FXSYS_atoi("0")); 11 EXPECT_EQ(-1, FXSYS_atoi("-1")); 12 EXPECT_EQ(2345, FXSYS_atoi("2345")); 13 EXPECT_EQ(-2147483647, FXSYS_atoi("-2147483647")); 14 // Handle the sign. 15 EXPECT_EQ(-2345, FXSYS_atoi("-2345")); 16 EXPECT_EQ(2345, FXSYS_atoi("+2345")); 17 // The max value. 18 EXPECT_EQ(2147483647, FXSYS_atoi("2147483647")); 19 // The min value. Written in -1 format to avoid "unary minus operator applied 20 // to unsigned type" warning. 21 EXPECT_EQ(-2147483647 - 1, FXSYS_atoi("-2147483648")); 22 // With invalid char. 23 EXPECT_EQ(9, FXSYS_atoi("9x9")); 24 25 // Out of range values. 26 EXPECT_EQ(2147483647, FXSYS_atoi("2147483623423412348")); 27 EXPECT_EQ(2147483647, FXSYS_atoi("2147483648")); 28 EXPECT_EQ(-2147483647 - 1, FXSYS_atoi("-2147483650")); 29 } 30 31 TEST(fxcrt, FXSYS_atoi64) { 32 EXPECT_EQ(0, FXSYS_atoi64("")); 33 EXPECT_EQ(0, FXSYS_atoi64("0")); 34 EXPECT_EQ(-1, FXSYS_atoi64("-1")); 35 EXPECT_EQ(2345, FXSYS_atoi64("2345")); 36 EXPECT_EQ(-9223372036854775807LL, FXSYS_atoi64("-9223372036854775807")); 37 // Handle the sign. 38 EXPECT_EQ(-2345, FXSYS_atoi64("-2345")); 39 EXPECT_EQ(2345, FXSYS_atoi64("+2345")); 40 // The max value. 41 EXPECT_EQ(9223372036854775807LL, FXSYS_atoi64("9223372036854775807")); 42 // The min value. Written in -1 format to avoid implicit unsigned warning. 43 EXPECT_EQ(-9223372036854775807LL - 1LL, FXSYS_atoi64("-9223372036854775808")); 44 // With invalid char. 45 EXPECT_EQ(9, FXSYS_atoi64("9x9")); 46 47 // Out of range values. 48 EXPECT_EQ(9223372036854775807LL, FXSYS_atoi64("922337203685471234123475807")); 49 EXPECT_EQ(9223372036854775807LL, FXSYS_atoi64("9223372036854775808")); 50 EXPECT_EQ(-9223372036854775807LL - 1LL, FXSYS_atoi64("-9223372036854775810")); 51 } 52 53 TEST(fxcrt, FXSYS_wtoi) { 54 EXPECT_EQ(0, FXSYS_wtoi(L"")); 55 EXPECT_EQ(0, FXSYS_wtoi(L"0")); 56 EXPECT_EQ(-1, FXSYS_wtoi(L"-1")); 57 EXPECT_EQ(2345, FXSYS_wtoi(L"2345")); 58 EXPECT_EQ(-2147483647, FXSYS_wtoi(L"-2147483647")); 59 // The max value. 60 EXPECT_EQ(2147483647, FXSYS_wtoi(L"2147483647")); 61 // The min value. 62 EXPECT_EQ(-2147483647 - 1, FXSYS_wtoi(L"-2147483648")); 63 EXPECT_EQ(9, FXSYS_wtoi64(L"9x9")); 64 65 // Out of range values. 66 EXPECT_EQ(2147483647, FXSYS_wtoi(L"2147483623423412348")); 67 EXPECT_EQ(2147483647, FXSYS_wtoi(L"2147483648")); 68 EXPECT_EQ(-2147483647 - 1, FXSYS_wtoi(L"-2147483652")); 69 } 70 71 TEST(fxcrt, FXSYS_wtoi64) { 72 EXPECT_EQ(0, FXSYS_wtoi64(L"")); 73 EXPECT_EQ(0, FXSYS_wtoi64(L"0")); 74 EXPECT_EQ(-1, FXSYS_wtoi64(L"-1")); 75 EXPECT_EQ(2345, FXSYS_wtoi64(L"2345")); 76 EXPECT_EQ(-9223372036854775807LL, FXSYS_wtoi64(L"-9223372036854775807")); 77 // Handle the sign. 78 EXPECT_EQ(-2345, FXSYS_wtoi64(L"-2345")); 79 EXPECT_EQ(2345, FXSYS_wtoi64(L"+2345")); 80 // The max value. 81 EXPECT_EQ(9223372036854775807LL, FXSYS_wtoi64(L"9223372036854775807")); 82 // The min value. 83 EXPECT_EQ(-9223372036854775807LL - 1LL, 84 FXSYS_wtoi64(L"-9223372036854775808")); 85 // With invalid char. 86 EXPECT_EQ(9, FXSYS_wtoi64(L"9x9")); 87 88 // Out of range values. 89 EXPECT_EQ(9223372036854775807LL, 90 FXSYS_wtoi64(L"922337203685471234123475807")); 91 EXPECT_EQ(9223372036854775807LL, FXSYS_wtoi64(L"9223372036854775808")); 92 EXPECT_EQ(-9223372036854775807LL - 1LL, 93 FXSYS_wtoi64(L"-9223372036854775810")); 94 } 95 96 TEST(fxcrt, FXSYS_atoui) { 97 EXPECT_EQ(0u, FXSYS_atoui("")); 98 EXPECT_EQ(0u, FXSYS_atoui("0")); 99 EXPECT_EQ(4294967295, FXSYS_atoui("-1")); 100 EXPECT_EQ(2345u, FXSYS_atoui("2345")); 101 // Handle the sign. 102 EXPECT_EQ(4294964951, FXSYS_atoui("-2345")); 103 EXPECT_EQ(2345u, FXSYS_atoui("+2345")); 104 // The max value. 105 EXPECT_EQ(4294967295, FXSYS_atoui("4294967295")); 106 EXPECT_EQ(9u, FXSYS_atoui("9x9")); 107 108 // Out of range values. 109 EXPECT_EQ(4294967295, FXSYS_atoui("2147483623423412348")); 110 EXPECT_EQ(4294967295, FXSYS_atoui("4294967296")); 111 EXPECT_EQ(4294967295, FXSYS_atoui("-4294967345")); 112 } 113