OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 484 |
485 TEST(StringUtilTest, LowerCaseEqualsASCII) { | 485 TEST(StringUtilTest, LowerCaseEqualsASCII) { |
486 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(lowercase_cases); ++i) { | 486 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(lowercase_cases); ++i) { |
487 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_w, | 487 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_w, |
488 lowercase_cases[i].dst)); | 488 lowercase_cases[i].dst)); |
489 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_a, | 489 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_a, |
490 lowercase_cases[i].dst)); | 490 lowercase_cases[i].dst)); |
491 } | 491 } |
492 } | 492 } |
493 | 493 |
494 TEST(StringUtilTest, GetByteDisplayUnits) { | 494 TEST(StringUtilTest, FormatBytesUnlocalized) { |
495 static const struct { | 495 static const struct { |
496 int64 bytes; | 496 int64 bytes; |
497 DataUnits expected; | |
498 } cases[] = { | |
499 {0, DATA_UNITS_BYTE}, | |
500 {512, DATA_UNITS_BYTE}, | |
501 {10*1024, DATA_UNITS_KIBIBYTE}, | |
502 {10*1024*1024, DATA_UNITS_MEBIBYTE}, | |
503 {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE}, | |
504 {~(1LL<<63), DATA_UNITS_GIBIBYTE}, | |
505 #ifdef NDEBUG | |
506 {-1, DATA_UNITS_BYTE}, | |
507 #endif | |
508 }; | |
509 | |
510 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) | |
511 EXPECT_EQ(cases[i].expected, GetByteDisplayUnits(cases[i].bytes)); | |
512 } | |
513 | |
514 TEST(StringUtilTest, FormatBytes) { | |
515 static const struct { | |
516 int64 bytes; | |
517 DataUnits units; | |
518 const char* expected; | 497 const char* expected; |
519 const char* expected_with_units; | |
520 } cases[] = { | 498 } cases[] = { |
521 // Expected behavior: we show one post-decimal digit when we have | 499 // Expected behavior: we show one post-decimal digit when we have |
522 // under two pre-decimal digits, except in cases where it makes no | 500 // under two pre-decimal digits, except in cases where it makes no |
523 // sense (zero or bytes). | 501 // sense (zero or bytes). |
524 // Since we switch units once we cross the 1000 mark, this keeps | 502 // Since we switch units once we cross the 1000 mark, this keeps |
525 // the display of file sizes or bytes consistently around three | 503 // the display of file sizes or bytes consistently around three |
526 // digits. | 504 // digits. |
527 {0, DATA_UNITS_BYTE, "0", "0 B"}, | 505 {0, "0 B"}, |
528 {512, DATA_UNITS_BYTE, "512", "512 B"}, | 506 {512, "512 B"}, |
529 {512, DATA_UNITS_KIBIBYTE, "0.5", "0.5 kB"}, | 507 {1024*1024, "1.0 MB"}, |
530 {1024*1024, DATA_UNITS_KIBIBYTE, "1024", "1024 kB"}, | 508 {1024*1024*1024, "1.0 GB"}, |
531 {1024*1024, DATA_UNITS_MEBIBYTE, "1.0", "1.0 MB"}, | 509 {10LL*1024*1024*1024, "10.0 GB"}, |
532 {1024*1024*1024, DATA_UNITS_GIBIBYTE, "1.0", "1.0 GB"}, | 510 {99LL*1024*1024*1024, "99.0 GB"}, |
533 {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"}, | 511 {105LL*1024*1024*1024, "105 GB"}, |
534 {99LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "99.0", "99.0 GB"}, | 512 {105LL*1024*1024*1024 + 500LL*1024*1024, "105 GB"}, |
535 {105LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "105", "105 GB"}, | 513 {~(1LL<<63), "8192 PB"}, |
536 {105LL*1024*1024*1024 + 500LL*1024*1024, DATA_UNITS_GIBIBYTE, | |
537 "105", "105 GB"}, | |
538 {~(1LL<<63), DATA_UNITS_GIBIBYTE, "8589934592", "8589934592 GB"}, | |
539 | 514 |
540 {99*1024 + 103, DATA_UNITS_KIBIBYTE, "99.1", "99.1 kB"}, | 515 {99*1024 + 103, "99.1 kB"}, |
541 {1024*1024 + 103, DATA_UNITS_KIBIBYTE, "1024", "1024 kB"}, | 516 {1024*1024 + 103, "1.0 MB"}, |
542 {1024*1024 + 205 * 1024, DATA_UNITS_MEBIBYTE, "1.2", "1.2 MB"}, | 517 {1024*1024 + 205 * 1024, "1.2 MB"}, |
543 {1024*1024*1024 + (927 * 1024*1024), DATA_UNITS_GIBIBYTE, | 518 {1024*1024*1024 + (927 * 1024*1024), "1.9 GB"}, |
544 "1.9", "1.9 GB"}, | 519 {10LL*1024*1024*1024, "10.0 GB"}, |
545 {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"}, | 520 {100LL*1024*1024*1024, "100 GB"}, |
546 {100LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "100", "100 GB"}, | |
547 #ifdef NDEBUG | |
548 {-1, DATA_UNITS_BYTE, "", ""}, | |
549 #endif | |
550 }; | 521 }; |
551 | 522 |
552 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 523 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
553 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), | 524 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), |
554 FormatBytes(cases[i].bytes, cases[i].units, false)); | 525 FormatBytesUnlocalized(cases[i].bytes)); |
555 EXPECT_EQ(ASCIIToUTF16(cases[i].expected_with_units), | |
556 FormatBytes(cases[i].bytes, cases[i].units, true)); | |
557 } | 526 } |
558 } | 527 } |
559 | |
560 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { | 528 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { |
561 static const struct { | 529 static const struct { |
562 const char* str; | 530 const char* str; |
563 string16::size_type start_offset; | 531 string16::size_type start_offset; |
564 const char* find_this; | 532 const char* find_this; |
565 const char* replace_with; | 533 const char* replace_with; |
566 const char* expected; | 534 const char* expected; |
567 } cases[] = { | 535 } cases[] = { |
568 {"aaa", 0, "a", "b", "bbb"}, | 536 {"aaa", 0, "a", "b", "bbb"}, |
569 {"abb", 0, "ab", "a", "ab"}, | 537 {"abb", 0, "ab", "a", "ab"}, |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1106 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); | 1074 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); |
1107 | 1075 |
1108 EXPECT_TRUE(ContainsOnlyChars("", "1234")); | 1076 EXPECT_TRUE(ContainsOnlyChars("", "1234")); |
1109 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); | 1077 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); |
1110 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); | 1078 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); |
1111 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); | 1079 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); |
1112 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); | 1080 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); |
1113 } | 1081 } |
1114 | 1082 |
1115 } // namespace base | 1083 } // namespace base |
OLD | NEW |