|
9 | 9 |
|
10 | 10 | class TestDecimal: XCTestCase {
|
11 | 11 |
|
12 |
| - static var allTests : [(String, (TestDecimal) -> () throws -> Void)] { |
13 |
| - return [ |
14 |
| - ("test_NSDecimalNumberInit", test_NSDecimalNumberInit), |
15 |
| - ("test_AdditionWithNormalization", test_AdditionWithNormalization), |
16 |
| - ("test_BasicConstruction", test_BasicConstruction), |
17 |
| - ("test_Constants", test_Constants), |
18 |
| - ("test_Description", test_Description), |
19 |
| - ("test_ExplicitConstruction", test_ExplicitConstruction), |
20 |
| - ("test_Maths", test_Maths), |
21 |
| - ("test_Misc", test_Misc), |
22 |
| - ("test_MultiplicationOverflow", test_MultiplicationOverflow), |
23 |
| - ("test_NaNInput", test_NaNInput), |
24 |
| - ("test_NegativeAndZeroMultiplication", test_NegativeAndZeroMultiplication), |
25 |
| - ("test_Normalise", test_Normalise), |
26 |
| - ("test_NSDecimal", test_NSDecimal), |
27 |
| - ("test_PositivePowers", test_PositivePowers), |
28 |
| - ("test_RepeatingDivision", test_RepeatingDivision), |
29 |
| - ("test_Round", test_Round), |
30 |
| - ("test_ScanDecimal", test_ScanDecimal), |
31 |
| - ("test_SimpleMultiplication", test_SimpleMultiplication), |
32 |
| - ("test_SmallerNumbers", test_SmallerNumbers), |
33 |
| - ("test_ZeroPower", test_ZeroPower), |
34 |
| - ("test_doubleValue", test_doubleValue), |
35 |
| - ("test_NSDecimalNumberValues", test_NSDecimalNumberValues), |
36 |
| - ("test_bridging", test_bridging), |
37 |
| - ("test_stringWithLocale", test_stringWithLocale), |
38 |
| - ] |
39 |
| - } |
40 |
| - |
41 | 12 | func test_NSDecimalNumberInit() {
|
42 | 13 | XCTAssertEqual(NSDecimalNumber(mantissa: 123456789000, exponent: -2, isNegative: true), -1234567890)
|
43 | 14 | XCTAssertEqual(NSDecimalNumber(decimal: Decimal()).decimalValue, Decimal(0))
|
@@ -203,8 +174,43 @@ class TestDecimal: XCTestCase {
|
203 | 174 | XCTAssertEqual("-5", Decimal(signOf: Decimal(-3), magnitudeOf: Decimal(5)).description)
|
204 | 175 | XCTAssertEqual("5", Decimal(signOf: Decimal(3), magnitudeOf: Decimal(-5)).description)
|
205 | 176 | XCTAssertEqual("-5", Decimal(signOf: Decimal(-3), magnitudeOf: Decimal(-5)).description)
|
206 |
| - XCTAssertEqual("5", NSDecimalNumber(decimal:Decimal(5)).description) |
207 |
| - XCTAssertEqual("-5", NSDecimalNumber(decimal:Decimal(-5)).description) |
| 177 | + XCTAssertEqual("5", NSDecimalNumber(decimal: Decimal(5)).description) |
| 178 | + XCTAssertEqual("-5", NSDecimalNumber(decimal: Decimal(-5)).description) |
| 179 | + XCTAssertEqual("-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Decimal.leastFiniteMagnitude.description) |
| 180 | + XCTAssertEqual("3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Decimal.greatestFiniteMagnitude.description) |
| 181 | + XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNormalMagnitude.description) |
| 182 | + XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", Decimal.leastNonzeroMagnitude.description) |
| 183 | + |
| 184 | + let fr = Locale(identifier: "fr_FR") |
| 185 | + let leastFiniteMagnitude = "-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 186 | + let greatestFiniteMagnitude = "3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 187 | + |
| 188 | + XCTAssertEqual("0", NSDecimalNumber(decimal: Decimal()).description(withLocale: fr)) |
| 189 | + XCTAssertEqual("1000", NSDecimalNumber(decimal: Decimal(1000)).description(withLocale: fr)) |
| 190 | + XCTAssertEqual("10", NSDecimalNumber(decimal: Decimal(10)).description(withLocale: fr)) |
| 191 | + XCTAssertEqual("123,458", NSDecimalNumber(decimal: Decimal(123.458)).description(withLocale: fr)) |
| 192 | + XCTAssertEqual("123", NSDecimalNumber(decimal: Decimal(UInt8(123))).description(withLocale: fr)) |
| 193 | + XCTAssertEqual("3,14159265358979323846264338327950288419", NSDecimalNumber(decimal: Decimal.pi).description(withLocale: fr)) |
| 194 | + XCTAssertEqual("-30000000000", NSDecimalNumber(decimal: Decimal(sign: .minus, exponent: 10, significand: Decimal(3))).description(withLocale: fr)) |
| 195 | + XCTAssertEqual("123456,789", NSDecimalNumber(decimal: Decimal(string: "123456.789")!).description(withLocale: fr)) |
| 196 | + XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: fr)) |
| 197 | + XCTAssertEqual(greatestFiniteMagnitude, NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude).description(withLocale: fr)) |
| 198 | + XCTAssertEqual("0,0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: fr)) |
| 199 | + XCTAssertEqual("0,0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: fr)) |
| 200 | + |
| 201 | + let en = Locale(identifier: "en_GB") |
| 202 | + XCTAssertEqual("0", NSDecimalNumber(decimal: Decimal()).description(withLocale: en)) |
| 203 | + XCTAssertEqual("1000", NSDecimalNumber(decimal: Decimal(1000)).description(withLocale: en)) |
| 204 | + XCTAssertEqual("10", NSDecimalNumber(decimal: Decimal(10)).description(withLocale: en)) |
| 205 | + XCTAssertEqual("123.458", NSDecimalNumber(decimal: Decimal(123.458)).description(withLocale: en)) |
| 206 | + XCTAssertEqual("123", NSDecimalNumber(decimal: Decimal(UInt8(123))).description(withLocale: en)) |
| 207 | + XCTAssertEqual("3.14159265358979323846264338327950288419", NSDecimalNumber(decimal: Decimal.pi).description(withLocale: en)) |
| 208 | + XCTAssertEqual("-30000000000", NSDecimalNumber(decimal: Decimal(sign: .minus, exponent: 10, significand: Decimal(3))).description(withLocale: en)) |
| 209 | + XCTAssertEqual("123456.789", NSDecimalNumber(decimal: Decimal(string: "123456.789")!).description(withLocale: en)) |
| 210 | + XCTAssertEqual(leastFiniteMagnitude, NSDecimalNumber(decimal: Decimal.leastFiniteMagnitude).description(withLocale: en)) |
| 211 | + XCTAssertEqual(greatestFiniteMagnitude, NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude).description(withLocale: en)) |
| 212 | + XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNormalMagnitude).description(withLocale: en)) |
| 213 | + XCTAssertEqual("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", NSDecimalNumber(decimal: Decimal.leastNonzeroMagnitude).description(withLocale: en)) |
208 | 214 | }
|
209 | 215 |
|
210 | 216 | func test_ExplicitConstruction() {
|
@@ -1116,4 +1122,55 @@ class TestDecimal: XCTestCase {
|
1116 | 1122 | XCTAssertEqual(Decimal(string: s2, locale: en_US)?.description, "1234")
|
1117 | 1123 | XCTAssertEqual(Decimal(string: s2, locale: fr_FR)?.description, s1)
|
1118 | 1124 | }
|
| 1125 | + |
| 1126 | + func test_NSDecimalString() { |
| 1127 | + var decimal = Decimal(string: "-123456.789")! |
| 1128 | + XCTAssertEqual(NSDecimalString(&decimal, nil), "-123456.789") |
| 1129 | + let en = NSDecimalString(&decimal, Locale(identifier: "en_GB")) |
| 1130 | + XCTAssertEqual(en, "-123456.789") |
| 1131 | + let fr = NSDecimalString(&decimal, Locale(identifier: "fr_FR")) |
| 1132 | + XCTAssertEqual(fr, "-123456,789") |
| 1133 | + |
| 1134 | + let d1: [NSLocale.Key: String] = [ .decimalSeparator: "@@@"] |
| 1135 | + XCTAssertEqual(NSDecimalString(&decimal, d1), "-123456@@@789") |
| 1136 | + let d2: [NSLocale.Key: String] = [ .decimalSeparator: "()"] |
| 1137 | + XCTAssertEqual(NSDecimalString(&decimal, NSDictionary(dictionary: d2)), "-123456()789") |
| 1138 | + let d3: [String: String] = [ "kCFLocaleDecimalSeparatorKey": "X"] |
| 1139 | + XCTAssertEqual(NSDecimalString(&decimal, NSDictionary(dictionary: d3)), "-123456X789") |
| 1140 | + |
| 1141 | + // Input is ignored |
| 1142 | + let d4: [Int: String] = [123: "X"] |
| 1143 | + XCTAssertEqual(NSDecimalString(&decimal, NSDictionary(dictionary: d4)), "-123456.789") |
| 1144 | + } |
| 1145 | + |
| 1146 | + |
| 1147 | + static var allTests : [(String, (TestDecimal) -> () throws -> Void)] { |
| 1148 | + return [ |
| 1149 | + ("test_NSDecimalNumberInit", test_NSDecimalNumberInit), |
| 1150 | + ("test_AdditionWithNormalization", test_AdditionWithNormalization), |
| 1151 | + ("test_BasicConstruction", test_BasicConstruction), |
| 1152 | + ("test_Constants", test_Constants), |
| 1153 | + ("test_Description", test_Description), |
| 1154 | + ("test_ExplicitConstruction", test_ExplicitConstruction), |
| 1155 | + ("test_Maths", test_Maths), |
| 1156 | + ("test_Misc", test_Misc), |
| 1157 | + ("test_MultiplicationOverflow", test_MultiplicationOverflow), |
| 1158 | + ("test_NaNInput", test_NaNInput), |
| 1159 | + ("test_NegativeAndZeroMultiplication", test_NegativeAndZeroMultiplication), |
| 1160 | + ("test_Normalise", test_Normalise), |
| 1161 | + ("test_NSDecimal", test_NSDecimal), |
| 1162 | + ("test_PositivePowers", test_PositivePowers), |
| 1163 | + ("test_RepeatingDivision", test_RepeatingDivision), |
| 1164 | + ("test_Round", test_Round), |
| 1165 | + ("test_ScanDecimal", test_ScanDecimal), |
| 1166 | + ("test_SimpleMultiplication", test_SimpleMultiplication), |
| 1167 | + ("test_SmallerNumbers", test_SmallerNumbers), |
| 1168 | + ("test_ZeroPower", test_ZeroPower), |
| 1169 | + ("test_doubleValue", test_doubleValue), |
| 1170 | + ("test_NSDecimalNumberValues", test_NSDecimalNumberValues), |
| 1171 | + ("test_bridging", test_bridging), |
| 1172 | + ("test_stringWithLocale", test_stringWithLocale), |
| 1173 | + ("test_NSDecimalString", test_NSDecimalString), |
| 1174 | + ] |
| 1175 | + } |
1119 | 1176 | }
|
0 commit comments