| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <math.h> | 5 #include <math.h> |
| 6 #include <stdarg.h> | 6 #include <stdarg.h> |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 strlen(convert_cases[i].utf8), | 304 strlen(convert_cases[i].utf8), |
| 305 &converted)); | 305 &converted)); |
| 306 std::wstring expected(convert_cases[i].wide); | 306 std::wstring expected(convert_cases[i].wide); |
| 307 EXPECT_EQ(expected, converted); | 307 EXPECT_EQ(expected, converted); |
| 308 } | 308 } |
| 309 | 309 |
| 310 // Manually test an embedded NULL. | 310 // Manually test an embedded NULL. |
| 311 std::wstring converted; | 311 std::wstring converted; |
| 312 EXPECT_TRUE(UTF8ToWide("\00Z\t", 3, &converted)); | 312 EXPECT_TRUE(UTF8ToWide("\00Z\t", 3, &converted)); |
| 313 ASSERT_EQ(3U, converted.length()); | 313 ASSERT_EQ(3U, converted.length()); |
| 314 #if defined(WCHAR_T_IS_UNSIGNED) |
| 315 EXPECT_EQ(0U, converted[0]); |
| 316 #else |
| 314 EXPECT_EQ(0, converted[0]); | 317 EXPECT_EQ(0, converted[0]); |
| 318 #endif |
| 315 EXPECT_EQ('Z', converted[1]); | 319 EXPECT_EQ('Z', converted[1]); |
| 316 EXPECT_EQ('\t', converted[2]); | 320 EXPECT_EQ('\t', converted[2]); |
| 317 | 321 |
| 318 // Make sure that conversion replaces, not appends. | 322 // Make sure that conversion replaces, not appends. |
| 319 EXPECT_TRUE(UTF8ToWide("B", 1, &converted)); | 323 EXPECT_TRUE(UTF8ToWide("B", 1, &converted)); |
| 320 ASSERT_EQ(1U, converted.length()); | 324 ASSERT_EQ(1U, converted.length()); |
| 321 EXPECT_EQ('B', converted[0]); | 325 EXPECT_EQ('B', converted[0]); |
| 322 } | 326 } |
| 323 | 327 |
| 324 #if defined(WCHAR_T_IS_UTF16) | 328 #if defined(WCHAR_T_IS_UTF16) |
| (...skipping 1177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1502 | 1506 |
| 1503 // Test dst_size == 0, nothing should be written to |dst| and we should | 1507 // Test dst_size == 0, nothing should be written to |dst| and we should |
| 1504 // have the equivalent of strlen(src). | 1508 // have the equivalent of strlen(src). |
| 1505 { | 1509 { |
| 1506 char dst[2] = {1, 2}; | 1510 char dst[2] = {1, 2}; |
| 1507 wchar_t wdst[2] = {1, 2}; | 1511 wchar_t wdst[2] = {1, 2}; |
| 1508 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", 0)); | 1512 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", 0)); |
| 1509 EXPECT_EQ(1, dst[0]); | 1513 EXPECT_EQ(1, dst[0]); |
| 1510 EXPECT_EQ(2, dst[1]); | 1514 EXPECT_EQ(2, dst[1]); |
| 1511 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", 0)); | 1515 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", 0)); |
| 1516 #if defined(WCHAR_T_IS_UNSIGNED) |
| 1517 EXPECT_EQ(1U, wdst[0]); |
| 1518 EXPECT_EQ(2U, wdst[1]); |
| 1519 #else |
| 1512 EXPECT_EQ(1, wdst[0]); | 1520 EXPECT_EQ(1, wdst[0]); |
| 1513 EXPECT_EQ(2, wdst[1]); | 1521 EXPECT_EQ(2, wdst[1]); |
| 1522 #endif |
| 1514 } | 1523 } |
| 1515 | 1524 |
| 1516 // Test the case were we _just_ competely fit including the null. | 1525 // Test the case were we _just_ competely fit including the null. |
| 1517 { | 1526 { |
| 1518 char dst[8]; | 1527 char dst[8]; |
| 1519 wchar_t wdst[8]; | 1528 wchar_t wdst[8]; |
| 1520 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", arraysize(dst))); | 1529 EXPECT_EQ(7U, base::strlcpy(dst, "abcdefg", arraysize(dst))); |
| 1521 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); | 1530 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); |
| 1522 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst))); | 1531 EXPECT_EQ(7U, base::wcslcpy(wdst, L"abcdefg", arraysize(wdst))); |
| 1523 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); | 1532 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1603 } | 1612 } |
| 1604 } | 1613 } |
| 1605 | 1614 |
| 1606 TEST(StringUtilTest, HexEncode) { | 1615 TEST(StringUtilTest, HexEncode) { |
| 1607 std::string hex(HexEncode(NULL, 0)); | 1616 std::string hex(HexEncode(NULL, 0)); |
| 1608 EXPECT_EQ(hex.length(), 0U); | 1617 EXPECT_EQ(hex.length(), 0U); |
| 1609 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 1618 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 1610 hex = HexEncode(bytes, sizeof(bytes)); | 1619 hex = HexEncode(bytes, sizeof(bytes)); |
| 1611 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 1620 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 1612 } | 1621 } |
| OLD | NEW |