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