forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNumberFormatter.swift
475 lines (413 loc) · 19.1 KB
/
TestNumberFormatter.swift
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
import CoreFoundation
class TestNumberFormatter: XCTestCase {
static var allTests: [(String, (TestNumberFormatter) -> () throws -> Void)] {
return [
("test_currencyCode", test_currencyCode),
("test_decimalSeparator", test_decimalSeparator),
("test_currencyDecimalSeparator", test_currencyDecimalSeparator),
("test_alwaysShowDecimalSeparator", test_alwaysShowDecimalSeparator),
("test_groupingSeparator", test_groupingSeparator),
("test_percentSymbol", test_percentSymbol),
("test_zeroSymbol", test_zeroSymbol),
("test_notANumberSymbol", test_notANumberSymbol),
("test_positiveInfinitySymbol", test_positiveInfinitySymbol),
("test_minusSignSymbol", test_minusSignSymbol),
("test_plusSignSymbol", test_plusSignSymbol),
("test_currencySymbol", test_currencySymbol),
("test_exponentSymbol", test_exponentSymbol),
("test_minimumIntegerDigits", test_minimumIntegerDigits),
("test_maximumIntegerDigits", test_maximumIntegerDigits),
("test_minimumFractionDigits", test_minimumFractionDigits),
("test_maximumFractionDigits", test_maximumFractionDigits),
("test_groupingSize", test_groupingSize),
("test_secondaryGroupingSize", test_secondaryGroupingSize),
("test_roundingMode", test_roundingMode),
("test_roundingIncrement", test_roundingIncrement),
("test_formatWidth", test_formatWidth),
("test_formatPosition", test_formatPosition),
("test_multiplier", test_multiplier),
("test_positivePrefix", test_positivePrefix),
("test_positiveSuffix", test_positiveSuffix),
("test_negativePrefix", test_negativePrefix),
("test_negativeSuffix", test_negativeSuffix),
("test_internationalCurrencySymbol", test_internationalCurrencySymbol),
("test_currencyGroupingSeparator", test_currencyGroupingSeparator),
("test_lenient", test_lenient),
("test_minimumSignificantDigits", test_minimumSignificantDigits),
("test_maximumSignificantDigits", test_maximumSignificantDigits),
("test_stringFor", test_stringFor),
("test_numberFrom", test_numberFrom),
//("test_en_US_initialValues", test_en_US_initialValues)
]
}
func test_currencyCode() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencyCode = "T"
numberFormatter.currencyDecimalSeparator = "_"
let formattedString = numberFormatter.stringFromNumber(42)
XCTAssertEqual(formattedString, "T 42_00")
*/
}
func test_decimalSeparator() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
let separator = "-"
numberFormatter.decimalSeparator = separator
XCTAssertEqual(numberFormatter.decimalSeparator, separator)
let formattedString = numberFormatter.string(from: 42.42)
XCTAssertEqual(formattedString, "42-42")
}
func test_currencyDecimalSeparator() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencyDecimalSeparator = "-"
numberFormatter.currencyCode = "T"
let formattedString = numberFormatter.stringFromNumber(42.42)
XCTAssertEqual(formattedString, "T 42-42")
*/
}
func test_alwaysShowDecimalSeparator() {
let numberFormatter = NumberFormatter()
numberFormatter.decimalSeparator = "-"
numberFormatter.alwaysShowsDecimalSeparator = true
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42-")
}
func test_groupingSeparator() {
let numberFormatter = NumberFormatter()
numberFormatter.usesGroupingSeparator = true
numberFormatter.groupingSeparator = "_"
let formattedString = numberFormatter.string(from: 42_000)
XCTAssertEqual(formattedString, "42_000")
}
func test_percentSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .percent
numberFormatter.percentSymbol = "💯"
let formattedString = numberFormatter.string(from: 0.42)
XCTAssertEqual(formattedString, "42💯")
}
func test_zeroSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.zeroSymbol = "⚽️"
let formattedString = numberFormatter.string(from: 0)
XCTAssertEqual(formattedString, "⚽️")
}
var unknownZero: Int = 0
func test_notANumberSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.notANumberSymbol = "👽"
let number: Double = -42
let numberObject = NSNumber(value: sqrt(number))
let formattedString = numberFormatter.string(from: numberObject)
XCTAssertEqual(formattedString, "👽")
}
func test_positiveInfinitySymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.positiveInfinitySymbol = "🚀"
let numberObject = NSNumber(value: Double(42.0) / Double(0))
let formattedString = numberFormatter.string(from: numberObject)
XCTAssertEqual(formattedString, "🚀")
}
func test_minusSignSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.minusSign = "👎"
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "👎42")
}
func test_plusSignSymbol() {
// ex. 1.0E+1 in scientific notation
let numberFormatter = NumberFormatter()
let format = "#E+0"
numberFormatter.format = format
XCTAssertEqual(numberFormatter.format, format)
let sign = "👍"
numberFormatter.plusSign = sign
XCTAssertEqual(numberFormatter.plusSign, sign)
let formattedString = numberFormatter.string(from: 420000000000000000)
XCTAssertNotNil(formattedString)
XCTAssertEqual(formattedString, "4.2E👍17")
// Verify a negative exponent does not have the 👍
let noPlusString = numberFormatter.string(from: -0.420)
XCTAssertNotNil(noPlusString)
if let fmt = noPlusString {
XCTAssertFalse(fmt.contains(sign), "Expected format of -0.420 (-4.2E-1) shouldn't have a plus sign which was set as \(sign)")
}
}
func test_currencySymbol() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencySymbol = "🍯"
numberFormatter.currencyDecimalSeparator = "_"
let formattedString = numberFormatter.stringFromNumber(42)
XCTAssertEqual(formattedString, "🍯 42_00")
*/
}
func test_exponentSymbol() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .scientific
numberFormatter.exponentSymbol = "⬆️"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "4.2⬆️1")
}
func test_minimumIntegerDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.minimumIntegerDigits = 3
var formattedString = numberFormatter.string(from: 0)
XCTAssertEqual(formattedString, "000")
numberFormatter.numberStyle = .decimal
formattedString = numberFormatter.string(from: 0.1)
XCTAssertEqual(formattedString, "0.1")
}
func test_maximumIntegerDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.maximumIntegerDigits = 3
let formattedString = numberFormatter.string(from: 1_000)
XCTAssertEqual(formattedString, "000")
}
func test_minimumFractionDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.minimumFractionDigits = 3
numberFormatter.decimalSeparator = "-"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42-000")
}
func test_maximumFractionDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 3
numberFormatter.decimalSeparator = "-"
let formattedString = numberFormatter.string(from: 42.4242)
XCTAssertEqual(formattedString, "42-424")
}
func test_groupingSize() {
let numberFormatter = NumberFormatter()
numberFormatter.groupingSize = 4
numberFormatter.groupingSeparator = "_"
numberFormatter.usesGroupingSeparator = true
let formattedString = numberFormatter.string(from: 42_000)
XCTAssertEqual(formattedString, "4_2000")
}
func test_secondaryGroupingSize() {
let numberFormatter = NumberFormatter()
numberFormatter.secondaryGroupingSize = 2
numberFormatter.groupingSeparator = "_"
numberFormatter.usesGroupingSeparator = true
let formattedString = numberFormatter.string(from: 42_000_000)
XCTAssertEqual(formattedString, "4_20_00_000")
}
func test_roundingMode() {
let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 0
numberFormatter.roundingMode = .ceiling
let formattedString = numberFormatter.string(from: 41.0001)
XCTAssertEqual(formattedString, "42")
}
func test_roundingIncrement() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.roundingIncrement = 0.2
let formattedString = numberFormatter.string(from: 4.25)
XCTAssertEqual(formattedString, "4.2")
}
func test_formatWidth() {
let numberFormatter = NumberFormatter()
numberFormatter.paddingCharacter = "_"
numberFormatter.formatWidth = 5
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "___42")
}
func test_formatPosition() {
let numberFormatter = NumberFormatter()
numberFormatter.paddingCharacter = "_"
numberFormatter.formatWidth = 5
numberFormatter.paddingPosition = .afterPrefix
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "-__42")
}
func test_multiplier() {
let numberFormatter = NumberFormatter()
numberFormatter.multiplier = 2
let formattedString = numberFormatter.string(from: 21)
XCTAssertEqual(formattedString, "42")
}
func test_positivePrefix() {
let numberFormatter = NumberFormatter()
numberFormatter.positivePrefix = "👍"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "👍42")
}
func test_positiveSuffix() {
let numberFormatter = NumberFormatter()
numberFormatter.positiveSuffix = "👍"
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42👍")
}
func test_negativePrefix() {
let numberFormatter = NumberFormatter()
numberFormatter.negativePrefix = "👎"
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "👎42")
}
func test_negativeSuffix() {
let numberFormatter = NumberFormatter()
numberFormatter.negativeSuffix = "👎"
let formattedString = numberFormatter.string(from: -42)
XCTAssertEqual(formattedString, "-42👎")
}
func test_internationalCurrencySymbol() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyPluralStyle
numberFormatter.internationalCurrencySymbol = "💵"
numberFormatter.currencyDecimalSeparator = "_"
let formattedString = numberFormatter.stringFromNumber(42)
XCTAssertEqual(formattedString, "💵 42_00")
*/
}
func test_currencyGroupingSeparator() {
// Disabled due to [SR-250]
/*
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencyGroupingSeparator = "_"
numberFormatter.currencyCode = "T"
numberFormatter.currencyDecimalSeparator = "/"
let formattedString = numberFormatter.stringFromNumber(42_000)
XCTAssertEqual(formattedString, "T 42_000/00")
*/
}
func test_lenient() {
let numberFormatter = NumberFormatter()
// Not lenient by default
XCTAssertFalse(numberFormatter.isLenient)
// Lenient allows wrong style -- not lenient here
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.numberStyle, .spellOut)
// let nilNumber = numberFormatter.number(from: "2.22")
// FIXME: Not nil on Linux?
//XCTAssertNil(nilNumber)
// Lenient allows wrong style
numberFormatter.isLenient = true
XCTAssertTrue(numberFormatter.isLenient)
let number = numberFormatter.number(from: "2.22")
XCTAssertEqual(number, 2.22)
// TODO: Add some tests with currency after [SR-250] resolved
// numberFormatter.numberStyle = .currency
// let nilNumberBeforeLenient = numberFormatter.number(from: "42")
//
// XCTAssertNil(nilNumberBeforeLenient)
// numberFormatter.isLenient = true
// let numberAfterLenient = numberFormatter.number(from: "42.42")
// XCTAssertEqual(numberAfterLenient, 42.42)
}
func test_minimumSignificantDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.minimumSignificantDigits = 3
let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "42.0")
}
func test_maximumSignificantDigits() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
var formattedString = numberFormatter.string(from: 987654321)
XCTAssertEqual(formattedString, "987,654,321")
numberFormatter.usesSignificantDigits = true
numberFormatter.maximumSignificantDigits = 3
formattedString = numberFormatter.string(from: 42.42424242)
XCTAssertEqual(formattedString, "42.4")
}
func test_stringFor() {
let numberFormatter = NumberFormatter()
XCTAssertEqual(numberFormatter.string(for: 10)!, "10")
XCTAssertEqual(numberFormatter.string(for: 3.14285714285714)!, "3")
XCTAssertEqual(numberFormatter.string(for: true)!, "1")
XCTAssertEqual(numberFormatter.string(for: false)!, "0")
XCTAssertNil(numberFormatter.string(for: [1,2]))
XCTAssertEqual(numberFormatter.string(for: NSNumber(value: 99.1))!, "99")
XCTAssertNil(numberFormatter.string(for: "NaN"))
XCTAssertNil(numberFormatter.string(for: NSString(string: "NaN")))
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.string(for: 234), "two hundred thirty-four")
XCTAssertEqual(numberFormatter.string(for: 2007), "two thousand seven")
XCTAssertEqual(numberFormatter.string(for: 3), "three")
XCTAssertEqual(numberFormatter.string(for: 0.3), "zero point three")
numberFormatter.numberStyle = .decimal
XCTAssertEqual(numberFormatter.string(for: 234.5678), "234.568")
numberFormatter.locale = Locale(identifier: "zh_CN")
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.string(from: 11.4), "十一点四")
numberFormatter.locale = Locale(identifier: "fr_FR")
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.string(from: 11.4), "onze virgule quatre")
}
func test_numberFrom() {
let numberFormatter = NumberFormatter()
XCTAssertEqual(numberFormatter.number(from: "10"), 10)
XCTAssertEqual(numberFormatter.number(from: "3.14"), 3.14)
XCTAssertEqual(numberFormatter.number(from: "0.01"), 0.01)
XCTAssertEqual(numberFormatter.number(from: ".01"), 0.01)
// These don't work unless lenient/style set
numberFormatter.numberStyle = .decimal
XCTAssertEqual(numberFormatter.number(from: "1,001"), 1001)
XCTAssertEqual(numberFormatter.number(from: "1,050,001"), 1050001)
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.number(from: "two thousand and seven"), 2007)
XCTAssertEqual(numberFormatter.number(from: "one point zero"), 1.0)
XCTAssertEqual(numberFormatter.number(from: "one hundred million"), 1E8)
numberFormatter.locale = Locale(identifier: "zh_CN")
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.number(from: "十一点四"), 11.4)
numberFormatter.locale = Locale(identifier: "fr_FR")
numberFormatter.numberStyle = .spellOut
XCTAssertEqual(numberFormatter.number(from: "onze virgule quatre"), 11.4)
}
func test_en_US_initialValues() {
// Symbols should be extractable
// At one point, none of this passed!
let numberFormatter = NumberFormatter();
numberFormatter.locale = Locale(identifier: "en_US")
// TODO: Check if this is true for all versions...
XCTAssertEqual(numberFormatter.format, "#;0;#")
XCTAssertEqual(numberFormatter.plusSign, "+")
XCTAssertEqual(numberFormatter.minusSign, "-")
XCTAssertEqual(numberFormatter.decimalSeparator, ".")
XCTAssertEqual(numberFormatter.groupingSeparator, ",")
XCTAssertEqual(numberFormatter.nilSymbol, "")
XCTAssertEqual(numberFormatter.notANumberSymbol, "NaN")
XCTAssertEqual(numberFormatter.positiveInfinitySymbol, "+∞")
XCTAssertEqual(numberFormatter.negativeInfinitySymbol, "-∞")
XCTAssertEqual(numberFormatter.positivePrefix, "")
XCTAssertEqual(numberFormatter.negativePrefix, "-")
XCTAssertEqual(numberFormatter.positiveSuffix, "")
XCTAssertEqual(numberFormatter.negativeSuffix, "")
XCTAssertEqual(numberFormatter.percentSymbol, "%")
XCTAssertEqual(numberFormatter.perMillSymbol, "‰")
XCTAssertEqual(numberFormatter.exponentSymbol, "E")
XCTAssertEqual(numberFormatter.groupingSeparator, ",")
XCTAssertEqual(numberFormatter.paddingCharacter, "*")
}
}