-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathByteCountFormatStyleTests.swift
More file actions
261 lines (230 loc) · 10.4 KB
/
ByteCountFormatStyleTests.swift
File metadata and controls
261 lines (230 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Testing
#if canImport(FoundationInternationalization)
import FoundationEssentials
import FoundationInternationalization
#else
import Foundation
#endif
@Suite("ByteCountFormatStyle")
private struct ByteCountFormatStyleTests {
static let locales = [Locale(identifier: "en_US"), .init(identifier: "fr_FR"), .init(identifier: "zh_TW"), .init(identifier: "zh_CN"), .init(identifier: "ar")]
@Test(arguments: locales)
func zeroSpelledOutKb(locale: Locale) {
let localizedZerosSpelledOutKb: [Locale: String] = [
Locale(identifier: "en_US"): "Zero kB",
Locale(identifier: "fr_FR"): "Zéro ko",
Locale(identifier: "zh_TW"): "0 kB",
Locale(identifier: "zh_CN"): "0 kB",
Locale(identifier: "ar"): "صفر كيلوبايت",
]
#expect(0.formatted(.byteCount(style: .memory, spellsOutZero: true).locale(locale)) == localizedZerosSpelledOutKb[locale])
}
@Test(arguments: locales)
func zeroSpelledOutBytes(locale: Locale) {
let localizedZerosSpelledOutBytes: [Locale: String] = [
Locale(identifier: "en_US"): "Zero bytes",
Locale(identifier: "fr_FR"): "Zéro octet",
Locale(identifier: "zh_TW"): "0 byte",
Locale(identifier: "zh_CN"): "0字节",
Locale(identifier: "ar"): "صفر بايت",
]
#expect(0.formatted(.byteCount(style: .memory, allowedUnits: .bytes, spellsOutZero: true).locale(locale)) == localizedZerosSpelledOutBytes[locale], "locale: \(locale.identifier) failed expectation")
}
let localizedSingular: [Locale: [String]] = [
Locale(identifier: "en_US"): [
"1 byte",
"1 kB",
"1 MB",
"1 GB",
"1 TB",
"1 PB",
],
Locale(identifier: "fr_FR"): [
"1 octet",
"1 ko",
"1 Mo",
"1 Go",
"1 To",
"1 Po",
],
Locale(identifier: "zh_TW"): [
"1 byte",
"1 kB",
"1 MB",
"1 GB",
"1 TB",
"1 PB",
],
Locale(identifier: "zh_CN"): [
"1 byte",
"1 kB",
"1 MB",
"1 GB",
"1 TB",
"1 PB",
],
Locale(identifier: "ar"): [
"١ بايت",
"١ كيلوبايت",
"١ ميغابايت",
"١ غيغابايت",
"١ تيرابايت",
"١ بيتابايت",
]
]
#if FIXED_86386674
@Test(arguments: locales)
func singularUnitsBinary(locale: Locale) {
for i in 0...5 {
let value: Int64 = (1 << (i*10))
#expect((value).formatted(.byteCount(style: .memory).locale(locale)) == localizedSingular[locale]![i])
}
}
#endif
#if FIXED_86386684
@Test(arguments: locales)
func singularUnitsDecimal(locale: Locale) {
for i in 0...5 {
#expect(Int64(pow(10.0, Double(i*3))).formatted(.byteCount(style: .file).locale(locale)) == localizedSingular[locale]![i])
}
}
#endif
@Test func localizedParens() {
#expect(1024.formatted(.byteCount(style: ByteCountFormatStyle.Style.binary, includesActualByteCount: true).locale(.init(identifier: "zh_TW"))) == "1 kB(1,024 byte)")
#expect(1024.formatted(.byteCount(style: ByteCountFormatStyle.Style.binary, includesActualByteCount: true).locale(.init(identifier: "en_US"))) == "1 kB (1,024 bytes)")
}
@Test func actualByteCount() {
#expect(1024.formatted(.byteCount(style: ByteCountFormatStyle.Style.file, includesActualByteCount: true).locale(.init(identifier: "en_US"))) == "1 kB (1,024 bytes)")
}
@Test func rtl() {
#expect(1024.formatted(.byteCount(style: ByteCountFormatStyle.Style.binary, includesActualByteCount: true).locale(.init(identifier: "ar_SA"))) == "١ كيلوبايت (١٬٠٢٤ بايت)")
}
@Test func attributed() {
var expected: [Segment]
// Zero kB
expected = [
.init(string: "Zero", number: nil, symbol: nil, byteCount: .spelledOutValue),
.space,
.init(string: "kB", number: nil, symbol: nil, byteCount: .unit(.kb))]
#expect(0.formatted(.byteCount(style: .file, spellsOutZero: true).locale(.init(identifier: "en_US")).attributed) == expected.attributedString)
// 1 byte
expected = [
.init(string: "1", number: .integer, symbol: nil, byteCount: .value),
.space,
.init(string: "byte", number: nil, symbol: nil, byteCount: .unit(.byte))]
#expect(1.formatted(.byteCount(style: .file).locale(.init(identifier: "en_US")).attributed) == expected.attributedString)
// 1,000 bytes
expected = [
.init(string: "1", number: .integer, symbol: nil, byteCount: .value),
.init(string: ",", number: .integer, symbol: .groupingSeparator, byteCount: .value),
.init(string: "000", number: .integer, symbol: nil, byteCount: .value),
.space,
.init(string: "bytes", number: nil, symbol: nil, byteCount: .unit(.byte))]
#expect(1000.formatted(.byteCount(style: .memory).locale(.init(identifier: "en_US")).attributed) == expected.attributedString)
// 1,016 kB
expected = [
.init(string: "1", number: .integer, symbol: nil, byteCount: .value),
.init(string: ",", number: .integer, symbol: .groupingSeparator, byteCount: .value),
.init(string: "016", number: .integer, symbol: nil, byteCount: .value),
.space,
.init(string: "kB", number: nil, symbol: nil, byteCount: .unit(.kb))]
#expect(1_040_000.formatted(.byteCount(style: .memory).locale(.init(identifier: "en_US")).attributed) == expected.attributedString)
// 1.1 MB
expected = [
.init(string: "1", number: .integer, symbol: nil, byteCount: .value),
.init(string: ".", number: nil, symbol: .decimalSeparator, byteCount: .value),
.init(string: "1", number: .fraction, symbol: nil, byteCount: .value),
.space,
.init(string: "MB", number: nil, symbol: nil, byteCount: .unit(.mb))]
#expect(1_100_000.formatted(.byteCount(style: .file).locale(.init(identifier: "en_US")).attributed) == expected.attributedString)
// 4.2 GB (4,200,000 bytes)
expected = [
.init(string: "4", number: .integer, symbol: nil, byteCount: .value),
.init(string: ".", number: nil, symbol: .decimalSeparator, byteCount: .value),
.init(string: "2", number: .fraction, symbol: nil, byteCount: .value),
.space,
.init(string: "GB", number: nil, symbol: nil, byteCount: .unit(.gb)),
.space,
.openParen,
.init(string: "4", number: .integer, symbol: nil, byteCount: .actualByteCount),
.init(string: ",", number: .integer, symbol: .groupingSeparator, byteCount: .actualByteCount),
.init(string: "200", number: .integer, symbol: nil, byteCount: .actualByteCount),
.init(string: ",", number: .integer, symbol: .groupingSeparator, byteCount: .actualByteCount),
.init(string: "000", number: .integer, symbol: nil, byteCount: .actualByteCount),
.init(string: ",", number: .integer, symbol: .groupingSeparator, byteCount: .actualByteCount),
.init(string: "000", number: .integer, symbol: nil, byteCount: .actualByteCount),
.space,
.init(string: "bytes", number: nil, symbol: nil, byteCount: .unit(.byte)),
.closedParen]
#expect(Int64(4_200_000_000).formatted(.byteCount(style: .file, includesActualByteCount: true).locale(.init(identifier: "en_US")).attributed) == expected.attributedString)
}
@Test func concurrentZeroFormatting() async {
await withTaskGroup(of: Void.self) { group in
for _ in 0..<20 {
group.addTask {
for _ in 0..<50 {
_ = 0.formatted(.byteCount(style: .binary).locale(Locale(identifier: "en_US")))
}
}
}
}
}
#if !_pointerBitWidth(_32)
@Test func testEveryAllowedUnit() {
// 84270854: The largest unit supported currently is pb
let expectations: [ByteCountFormatStyle.Units: String] = [
.bytes: "10,000,000,000,000,000 bytes",
.kb: "10,000,000,000,000 kB",
.mb: "10,000,000,000 MB",
.gb: "10,000,000 GB",
.tb: "10,000 TB",
.pb: "10 PB",
.eb: "10 PB",
.zb: "10 PB",
.ybOrHigher: "10 PB"
]
for (units, expectation) in expectations {
#expect(10_000_000_000_000_000.formatted(.byteCount(style: .file, allowedUnits: units).locale(Locale(identifier: "en_US"))) == expectation)
}
}
#endif
}
fileprivate struct Segment {
let string: String
let number: AttributeScopes.FoundationAttributes.NumberFormatAttributes.NumberPartAttribute.NumberPart?
let symbol: AttributeScopes.FoundationAttributes.NumberFormatAttributes.SymbolAttribute.Symbol?
let byteCount: AttributeScopes.FoundationAttributes.ByteCountAttribute.Component?
static var space: Self {
return .init(string: " ", number: nil, symbol: nil, byteCount: nil)
}
static var openParen: Self {
return .init(string: "(", number: nil, symbol: nil, byteCount: nil)
}
static var closedParen: Self {
return .init(string: ")", number: nil, symbol: nil, byteCount: nil)
}
}
extension Sequence where Element == Segment {
var attributedString: AttributedString {
self.map { segment in
var attributed = AttributedString(segment.string)
if let symbol = segment.symbol {
attributed.numberSymbol = symbol
}
if let number = segment.number {
attributed.numberPart = number
}
if let byteCount = segment.byteCount {
attributed.byteCount = byteCount
}
return attributed
}.reduce(into: AttributedString()) { $0 += $1 }
}
}