Roll src/third_party/WebKit 33ba8ad:8d0ce3d (svn 202514:202517)
[chromium-blink-merge.git] / ui / base / text / bytes_formatting_unittest.cc
blob308996c8f3c8858a5635aaec4c249896da3dbcd4
1 // Copyright (c) 2012 The Chromium 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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/base/text/bytes_formatting.h"
9 namespace ui {
11 TEST(BytesFormattingTest, GetByteDisplayUnits) {
12 static const struct {
13 int64 bytes;
14 DataUnits expected;
15 } cases[] = {
16 {0, DATA_UNITS_BYTE},
17 {512, DATA_UNITS_BYTE},
18 {10*1024, DATA_UNITS_KIBIBYTE},
19 {10*1024*1024, DATA_UNITS_MEBIBYTE},
20 {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE},
21 {10LL*1024*1024*1024*1024, DATA_UNITS_TEBIBYTE},
22 {~(1LL<<63), DATA_UNITS_PEBIBYTE},
23 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
24 {-1, DATA_UNITS_BYTE},
25 #endif
28 for (size_t i = 0; i < arraysize(cases); ++i)
29 EXPECT_EQ(cases[i].expected, GetByteDisplayUnits(cases[i].bytes));
32 TEST(BytesFormattingTest, FormatBytes) {
33 static const struct {
34 int64 bytes;
35 DataUnits units;
36 const char* expected;
37 const char* expected_with_units;
38 } cases[] = {
39 // Expected behavior: we show one post-decimal digit when we have
40 // under two pre-decimal digits, except in cases where it makes no
41 // sense (zero or bytes).
42 // Since we switch units once we cross the 1000 mark, this keeps
43 // the display of file sizes or bytes consistently around three
44 // digits.
45 {0, DATA_UNITS_BYTE, "0", "0 B"},
46 {512, DATA_UNITS_BYTE, "512", "512 B"},
47 {512, DATA_UNITS_KIBIBYTE, "0.5", "0.5 KB"},
48 {1024*1024, DATA_UNITS_KIBIBYTE, "1,024", "1,024 KB"},
49 {1024*1024, DATA_UNITS_MEBIBYTE, "1.0", "1.0 MB"},
50 {1024*1024*1024, DATA_UNITS_GIBIBYTE, "1.0", "1.0 GB"},
51 {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"},
52 {99LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "99.0", "99.0 GB"},
53 {105LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "105", "105 GB"},
54 {105LL*1024*1024*1024 + 500LL*1024*1024, DATA_UNITS_GIBIBYTE,
55 "105", "105 GB"},
56 {~(1LL<<63), DATA_UNITS_GIBIBYTE, "8,589,934,592", "8,589,934,592 GB"},
57 {~(1LL<<63), DATA_UNITS_PEBIBYTE, "8,192", "8,192 PB"},
59 {99*1024 + 103, DATA_UNITS_KIBIBYTE, "99.1", "99.1 KB"},
60 {1024*1024 + 103, DATA_UNITS_KIBIBYTE, "1,024", "1,024 KB"},
61 {1024*1024 + 205 * 1024, DATA_UNITS_MEBIBYTE, "1.2", "1.2 MB"},
62 {1024*1024*1024 + (927 * 1024*1024), DATA_UNITS_GIBIBYTE,
63 "1.9", "1.9 GB"},
64 {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"},
65 {100LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "100", "100 GB"},
66 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
67 {-1, DATA_UNITS_BYTE, "", ""},
68 #endif
71 for (size_t i = 0; i < arraysize(cases); ++i) {
72 EXPECT_EQ(base::ASCIIToUTF16(cases[i].expected),
73 FormatBytesWithUnits(cases[i].bytes, cases[i].units, false));
74 EXPECT_EQ(base::ASCIIToUTF16(cases[i].expected_with_units),
75 FormatBytesWithUnits(cases[i].bytes, cases[i].units, true));
79 } // namespace ui