| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.conv; |
| 4 |
|
| 5 |
import std.exception : assertThrown; |
| 6 |
assertThrown!ConvException(to!int("abc")); |
| 7 |
} |
| 8 |
|
| 9 |
@safe unittest |
| 10 |
{ |
| 11 |
import std.conv; |
| 12 |
|
| 13 |
import std.exception : assertThrown; |
| 14 |
assertThrown!ConvOverflowException(to!ubyte(1_000_000)); |
| 15 |
} |
| 16 |
|
| 17 |
@safe pure unittest |
| 18 |
{ |
| 19 |
import std.conv; |
| 20 |
|
| 21 |
int a = 42; |
| 22 |
int b = to!int(a); |
| 23 |
double c = to!double(3.14); // c is double with value 3.14 |
| 24 |
} |
| 25 |
|
| 26 |
@safe pure unittest |
| 27 |
{ |
| 28 |
import std.conv; |
| 29 |
|
| 30 |
import std.exception : assertThrown; |
| 31 |
|
| 32 |
int a = 420; |
| 33 |
assert(to!long(a) == a); |
| 34 |
assertThrown!ConvOverflowException(to!byte(a)); |
| 35 |
|
| 36 |
assert(to!int(4.2e6) == 4200000); |
| 37 |
assertThrown!ConvOverflowException(to!uint(-3.14)); |
| 38 |
assert(to!uint(3.14) == 3); |
| 39 |
assert(to!uint(3.99) == 3); |
| 40 |
assert(to!int(-3.99) == -3); |
| 41 |
} |
| 42 |
|
| 43 |
@safe pure unittest |
| 44 |
{ |
| 45 |
import std.conv; |
| 46 |
|
| 47 |
auto str = to!string(42, 16); |
| 48 |
assert(str == "2A"); |
| 49 |
auto i = to!int(str, 16); |
| 50 |
assert(i == 42); |
| 51 |
} |
| 52 |
|
| 53 |
@safe pure unittest |
| 54 |
{ |
| 55 |
import std.conv; |
| 56 |
|
| 57 |
// 2^24 - 1, largest proper integer representable as float |
| 58 |
int a = 16_777_215; |
| 59 |
assert(to!int(to!float(a)) == a); |
| 60 |
assert(to!int(to!float(-a)) == -a); |
| 61 |
} |
| 62 |
|
| 63 |
@safe pure unittest |
| 64 |
{ |
| 65 |
import std.conv; |
| 66 |
|
| 67 |
import std.exception : assertThrown; |
| 68 |
|
| 69 |
assert(to!char("a") == 'a'); |
| 70 |
assertThrown(to!char("ñ")); // 'ñ' does not fit into a char |
| 71 |
assert(to!wchar("ñ") == 'ñ'); |
| 72 |
assertThrown(to!wchar("😃")); // '😃' does not fit into a wchar |
| 73 |
assert(to!dchar("😃") == '😃'); |
| 74 |
|
| 75 |
// Using wstring or dstring as source type does not affect the result |
| 76 |
assert(to!char("a"w) == 'a'); |
| 77 |
assert(to!char("a"d) == 'a'); |
| 78 |
|
| 79 |
// Two code points cannot be converted to a single one |
| 80 |
assertThrown(to!char("ab")); |
| 81 |
} |
| 82 |
|
| 83 |
@safe pure unittest |
| 84 |
{ |
| 85 |
import std.conv; |
| 86 |
|
| 87 |
import std.string : split; |
| 88 |
|
| 89 |
int[] a = [1, 2, 3]; |
| 90 |
auto b = to!(float[])(a); |
| 91 |
assert(b == [1.0f, 2, 3]); |
| 92 |
string str = "1 2 3 4 5 6"; |
| 93 |
auto numbers = to!(double[])(split(str)); |
| 94 |
assert(numbers == [1.0, 2, 3, 4, 5, 6]); |
| 95 |
int[string] c; |
| 96 |
c["a"] = 1; |
| 97 |
c["b"] = 2; |
| 98 |
auto d = to!(double[wstring])(c); |
| 99 |
assert(d["a"w] == 1 && d["b"w] == 2); |
| 100 |
} |
| 101 |
|
| 102 |
@safe unittest |
| 103 |
{ |
| 104 |
import std.conv; |
| 105 |
|
| 106 |
int[string][double[int[]]] a; |
| 107 |
auto b = to!(short[wstring][string[double[]]])(a); |
| 108 |
} |
| 109 |
|
| 110 |
@safe pure unittest |
| 111 |
{ |
| 112 |
import std.conv; |
| 113 |
|
| 114 |
import std.exception : assertThrown; |
| 115 |
// Testing object conversions |
| 116 |
class A {} |
| 117 |
class B : A {} |
| 118 |
class C : A {} |
| 119 |
A a1 = new A, a2 = new B, a3 = new C; |
| 120 |
assert(to!B(a2) is a2); |
| 121 |
assert(to!C(a3) is a3); |
| 122 |
assertThrown!ConvException(to!B(a3)); |
| 123 |
} |
| 124 |
|
| 125 |
@system pure unittest |
| 126 |
{ |
| 127 |
import std.conv; |
| 128 |
|
| 129 |
// Conversion representing dynamic/static array with string |
| 130 |
long[] a = [ 1, 3, 5 ]; |
| 131 |
assert(to!string(a) == "[1, 3, 5]"); |
| 132 |
|
| 133 |
// Conversion representing associative array with string |
| 134 |
int[string] associativeArray = ["0":1, "1":2]; |
| 135 |
assert(to!string(associativeArray) == `["0":1, "1":2]` || |
| 136 |
to!string(associativeArray) == `["1":2, "0":1]`); |
| 137 |
|
| 138 |
// char* to string conversion |
| 139 |
assert(to!string(cast(char*) null) == ""); |
| 140 |
assert(to!string("foo\0".ptr) == "foo"); |
| 141 |
|
| 142 |
// Conversion reinterpreting void array to string |
| 143 |
auto w = "abcx"w; |
| 144 |
const(void)[] b = w; |
| 145 |
assert(b.length == 8); |
| 146 |
|
| 147 |
auto c = to!(wchar[])(b); |
| 148 |
assert(c == "abcx"); |
| 149 |
} |
| 150 |
|
| 151 |
@safe pure unittest |
| 152 |
{ |
| 153 |
import std.conv; |
| 154 |
|
| 155 |
import std.exception : assertThrown; |
| 156 |
|
| 157 |
enum E { a, b, c } |
| 158 |
assert(to!E("a") == E.a); |
| 159 |
assert(to!E("b") == E.b); |
| 160 |
assertThrown!ConvException(to!E("A")); |
| 161 |
} |
| 162 |
|
| 163 |
@safe unittest |
| 164 |
{ |
| 165 |
import std.conv; |
| 166 |
|
| 167 |
assert(roundTo!int(3.14) == 3); |
| 168 |
assert(roundTo!int(3.49) == 3); |
| 169 |
assert(roundTo!int(3.5) == 4); |
| 170 |
assert(roundTo!int(3.999) == 4); |
| 171 |
assert(roundTo!int(-3.14) == -3); |
| 172 |
assert(roundTo!int(-3.49) == -3); |
| 173 |
assert(roundTo!int(-3.5) == -4); |
| 174 |
assert(roundTo!int(-3.999) == -4); |
| 175 |
assert(roundTo!(const int)(to!(const double)(-3.999)) == -4); |
| 176 |
} |
| 177 |
|
| 178 |
@safe unittest |
| 179 |
{ |
| 180 |
import std.conv; |
| 181 |
|
| 182 |
import std.typecons : Flag, Yes, No; |
| 183 |
auto s = "true"; |
| 184 |
bool b = parse!bool(s); |
| 185 |
assert(b); |
| 186 |
auto s2 = "true"; |
| 187 |
bool b2 = parse!(bool, string, No.doCount)(s2); |
| 188 |
assert(b2); |
| 189 |
auto s3 = "true"; |
| 190 |
auto b3 = parse!(bool, string, Yes.doCount)(s3); |
| 191 |
assert(b3.data && b3.count == 4); |
| 192 |
auto s4 = "falSE"; |
| 193 |
auto b4 = parse!(bool, string, Yes.doCount)(s4); |
| 194 |
assert(!b4.data && b4.count == 5); |
| 195 |
} |
| 196 |
|
| 197 |
@safe pure unittest |
| 198 |
{ |
| 199 |
import std.conv; |
| 200 |
|
| 201 |
import std.typecons : Flag, Yes, No; |
| 202 |
string s = "123"; |
| 203 |
auto a = parse!int(s); |
| 204 |
assert(a == 123); |
| 205 |
|
| 206 |
string s1 = "123"; |
| 207 |
auto a1 = parse!(int, string, Yes.doCount)(s1); |
| 208 |
assert(a1.data == 123 && a1.count == 3); |
| 209 |
} |
| 210 |
|
| 211 |
@safe pure unittest |
| 212 |
{ |
| 213 |
import std.conv; |
| 214 |
|
| 215 |
import std.string : tr; |
| 216 |
import std.typecons : Flag, Yes, No; |
| 217 |
string test = "123 \t 76.14"; |
| 218 |
auto a = parse!uint(test); |
| 219 |
assert(a == 123); |
| 220 |
assert(test == " \t 76.14"); // parse bumps string |
| 221 |
test = tr(test, " \t\n\r", "", "d"); // skip ws |
| 222 |
assert(test == "76.14"); |
| 223 |
auto b = parse!double(test); |
| 224 |
assert(b == 76.14); |
| 225 |
assert(test == ""); |
| 226 |
|
| 227 |
string test2 = "123 \t 76.14"; |
| 228 |
auto a2 = parse!(uint, string, Yes.doCount)(test2); |
| 229 |
assert(a2.data == 123 && a2.count == 3); |
| 230 |
assert(test2 == " \t 76.14");// parse bumps string |
| 231 |
test2 = tr(test2, " \t\n\r", "", "d"); // skip ws |
| 232 |
assert(test2 == "76.14"); |
| 233 |
auto b2 = parse!(double, string, Yes.doCount)(test2); |
| 234 |
assert(b2.data == 76.14 && b2.count == 5); |
| 235 |
assert(test2 == ""); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
@safe unittest |
| 240 |
{ |
| 241 |
import std.conv; |
| 242 |
|
| 243 |
import std.typecons : Flag, Yes, No, tuple; |
| 244 |
enum EnumType : bool { a = true, b = false, c = a } |
| 245 |
|
| 246 |
auto str = "a"; |
| 247 |
assert(parse!EnumType(str) == EnumType.a); |
| 248 |
auto str2 = "a"; |
| 249 |
assert(parse!(EnumType, string, No.doCount)(str2) == EnumType.a); |
| 250 |
auto str3 = "a"; |
| 251 |
assert(parse!(EnumType, string, Yes.doCount)(str3) == tuple(EnumType.a, 1)); |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
@safe unittest |
| 256 |
{ |
| 257 |
import std.conv; |
| 258 |
|
| 259 |
import std.math.operations : isClose; |
| 260 |
import std.math.traits : isNaN, isInfinity; |
| 261 |
import std.typecons : Flag, Yes, No; |
| 262 |
auto str = "123.456"; |
| 263 |
assert(parse!double(str).isClose(123.456)); |
| 264 |
auto str2 = "123.456"; |
| 265 |
assert(parse!(double, string, No.doCount)(str2).isClose(123.456)); |
| 266 |
auto str3 = "123.456"; |
| 267 |
auto r = parse!(double, string, Yes.doCount)(str3); |
| 268 |
assert(r.data.isClose(123.456)); |
| 269 |
assert(r.count == 7); |
| 270 |
auto str4 = "-123.456"; |
| 271 |
r = parse!(double, string, Yes.doCount)(str4); |
| 272 |
assert(r.data.isClose(-123.456)); |
| 273 |
assert(r.count == 8); |
| 274 |
auto str5 = "+123.456"; |
| 275 |
r = parse!(double, string, Yes.doCount)(str5); |
| 276 |
assert(r.data.isClose(123.456)); |
| 277 |
assert(r.count == 8); |
| 278 |
auto str6 = "inf0"; |
| 279 |
r = parse!(double, string, Yes.doCount)(str6); |
| 280 |
assert(isInfinity(r.data) && r.count == 3 && str6 == "0"); |
| 281 |
auto str7 = "-0"; |
| 282 |
auto r2 = parse!(float, string, Yes.doCount)(str7); |
| 283 |
assert(r2.data.isClose(0.0) && r2.count == 2); |
| 284 |
auto str8 = "nan"; |
| 285 |
auto r3 = parse!(real, string, Yes.doCount)(str8); |
| 286 |
assert(isNaN(r3.data) && r3.count == 3); |
| 287 |
} |
| 288 |
|
| 289 |
@safe pure unittest |
| 290 |
{ |
| 291 |
import std.conv; |
| 292 |
|
| 293 |
import std.typecons : Flag, Yes, No; |
| 294 |
auto s = "Hello, World!"; |
| 295 |
char first = parse!char(s); |
| 296 |
assert(first == 'H'); |
| 297 |
assert(s == "ello, World!"); |
| 298 |
char second = parse!(char, string, No.doCount)(s); |
| 299 |
assert(second == 'e'); |
| 300 |
assert(s == "llo, World!"); |
| 301 |
auto third = parse!(char, string, Yes.doCount)(s); |
| 302 |
assert(third.data == 'l' && third.count == 1); |
| 303 |
assert(s == "lo, World!"); |
| 304 |
} |
| 305 |
|
| 306 |
@safe pure unittest |
| 307 |
{ |
| 308 |
import std.conv; |
| 309 |
|
| 310 |
import std.exception : assertThrown; |
| 311 |
import std.typecons : Flag, Yes, No; |
| 312 |
|
| 313 |
alias NullType = typeof(null); |
| 314 |
auto s1 = "null"; |
| 315 |
assert(parse!NullType(s1) is null); |
| 316 |
assert(s1 == ""); |
| 317 |
|
| 318 |
auto s2 = "NUll"d; |
| 319 |
assert(parse!NullType(s2) is null); |
| 320 |
assert(s2 == ""); |
| 321 |
|
| 322 |
auto s3 = "nuLlNULl"; |
| 323 |
assert(parse!(NullType, string, No.doCount)(s3) is null); |
| 324 |
auto r = parse!(NullType, string, Yes.doCount)(s3); |
| 325 |
assert(r.data is null && r.count == 4); |
| 326 |
|
| 327 |
auto m = "maybe"; |
| 328 |
assertThrown!ConvException(parse!NullType(m)); |
| 329 |
assertThrown!ConvException(parse!(NullType, string, Yes.doCount)(m)); |
| 330 |
assert(m == "maybe"); // m shouldn't change on failure |
| 331 |
|
| 332 |
auto s = "NULL"; |
| 333 |
assert(parse!(const NullType)(s) is null); |
| 334 |
} |
| 335 |
|
| 336 |
@safe pure unittest |
| 337 |
{ |
| 338 |
import std.conv; |
| 339 |
|
| 340 |
import std.typecons : Flag, Yes, No; |
| 341 |
auto s1 = `[['h', 'e', 'l', 'l', 'o'], "world"]`; |
| 342 |
auto a1 = parse!(string[])(s1); |
| 343 |
assert(a1 == ["hello", "world"]); |
| 344 |
|
| 345 |
auto s2 = `["aaa", "bbb", "ccc"]`; |
| 346 |
auto a2 = parse!(string[])(s2); |
| 347 |
assert(a2 == ["aaa", "bbb", "ccc"]); |
| 348 |
|
| 349 |
auto s3 = `[['h', 'e', 'l', 'l', 'o'], "world"]`; |
| 350 |
auto len3 = s3.length; |
| 351 |
auto a3 = parse!(string[], string, Yes.doCount)(s3); |
| 352 |
assert(a3.data == ["hello", "world"]); |
| 353 |
assert(a3.count == len3); |
| 354 |
} |
| 355 |
|
| 356 |
@safe pure unittest |
| 357 |
{ |
| 358 |
import std.conv; |
| 359 |
|
| 360 |
import std.typecons : Flag, Yes, No, tuple; |
| 361 |
import std.range.primitives : save; |
| 362 |
import std.array : assocArray; |
| 363 |
auto s1 = "[1:10, 2:20, 3:30]"; |
| 364 |
auto copyS1 = s1.save; |
| 365 |
auto aa1 = parse!(int[int])(s1); |
| 366 |
assert(aa1 == [1:10, 2:20, 3:30]); |
| 367 |
assert(tuple([1:10, 2:20, 3:30], copyS1.length) == parse!(int[int], string, Yes.doCount)(copyS1)); |
| 368 |
|
| 369 |
auto s2 = `["aaa":10, "bbb":20, "ccc":30]`; |
| 370 |
auto copyS2 = s2.save; |
| 371 |
auto aa2 = parse!(int[string])(s2); |
| 372 |
assert(aa2 == ["aaa":10, "bbb":20, "ccc":30]); |
| 373 |
assert(tuple(["aaa":10, "bbb":20, "ccc":30], copyS2.length) == |
| 374 |
parse!(int[string], string, Yes.doCount)(copyS2)); |
| 375 |
|
| 376 |
auto s3 = `["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]`; |
| 377 |
auto copyS3 = s3.save; |
| 378 |
auto aa3 = parse!(int[][string])(s3); |
| 379 |
assert(aa3 == ["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]); |
| 380 |
assert(tuple(["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]], copyS3.length) == |
| 381 |
parse!(int[][string], string, Yes.doCount)(copyS3)); |
| 382 |
|
| 383 |
auto s4 = `[]`; |
| 384 |
int[int] emptyAA; |
| 385 |
assert(tuple(emptyAA, s4.length) == parse!(int[int], string, Yes.doCount)(s4)); |
| 386 |
} |
| 387 |
|
| 388 |
@safe unittest |
| 389 |
{ |
| 390 |
import std.conv; |
| 391 |
|
| 392 |
assert( text(42, ' ', 1.5, ": xyz") == "42 1.5: xyz"c); |
| 393 |
assert(wtext(42, ' ', 1.5, ": xyz") == "42 1.5: xyz"w); |
| 394 |
assert(dtext(42, ' ', 1.5, ": xyz") == "42 1.5: xyz"d); |
| 395 |
} |
| 396 |
|
| 397 |
@safe unittest |
| 398 |
{ |
| 399 |
import std.conv; |
| 400 |
|
| 401 |
// Same as 0177 |
| 402 |
auto a = octal!177; |
| 403 |
// octal is a compile-time device |
| 404 |
enum b = octal!160; |
| 405 |
// Create an unsigned octal |
| 406 |
auto c = octal!"1_000_000u"; |
| 407 |
// Leading zeros are allowed when converting from a string |
| 408 |
auto d = octal!"0001_200_000"; |
| 409 |
} |
| 410 |
|
| 411 |
@safe unittest |
| 412 |
{ |
| 413 |
import std.conv; |
| 414 |
|
| 415 |
import std.traits : Unsigned; |
| 416 |
immutable int s = 42; |
| 417 |
auto u1 = unsigned(s); //not qualified |
| 418 |
static assert(is(typeof(u1) == uint)); |
| 419 |
Unsigned!(typeof(s)) u2 = unsigned(s); //same qualification |
| 420 |
static assert(is(typeof(u2) == immutable uint)); |
| 421 |
immutable u3 = unsigned(s); //explicitly qualified |
| 422 |
} |
| 423 |
|
| 424 |
@safe unittest |
| 425 |
{ |
| 426 |
import std.conv; |
| 427 |
|
| 428 |
import std.traits : Signed; |
| 429 |
|
| 430 |
immutable uint u = 42; |
| 431 |
auto s1 = signed(u); //not qualified |
| 432 |
static assert(is(typeof(s1) == int)); |
| 433 |
Signed!(typeof(u)) s2 = signed(u); //same qualification |
| 434 |
static assert(is(typeof(s2) == immutable int)); |
| 435 |
immutable s3 = signed(u); //explicitly qualified |
| 436 |
} |
| 437 |
|
| 438 |
@safe unittest |
| 439 |
{ |
| 440 |
import std.conv; |
| 441 |
|
| 442 |
enum A { a = 42 } |
| 443 |
static assert(is(typeof(A.a.asOriginalType) == int)); |
| 444 |
assert(A.a.asOriginalType == 42); |
| 445 |
enum B : double { a = 43 } |
| 446 |
static assert(is(typeof(B.a.asOriginalType) == double)); |
| 447 |
assert(B.a.asOriginalType == 43); |
| 448 |
} |
| 449 |
|
| 450 |
@system unittest |
| 451 |
{ |
| 452 |
import std.conv; |
| 453 |
|
| 454 |
// Regular cast, which has been verified to be legal by the programmer: |
| 455 |
{ |
| 456 |
long x; |
| 457 |
auto y = cast(int) x; |
| 458 |
} |
| 459 |
|
| 460 |
// However this will still compile if 'x' is changed to be a pointer: |
| 461 |
{ |
| 462 |
long* x; |
| 463 |
auto y = cast(int) x; |
| 464 |
} |
| 465 |
|
| 466 |
// castFrom provides a more reliable alternative to casting: |
| 467 |
{ |
| 468 |
long x; |
| 469 |
auto y = castFrom!long.to!int(x); |
| 470 |
} |
| 471 |
|
| 472 |
// Changing the type of 'x' will now issue a compiler error, |
| 473 |
// allowing bad casts to be caught before it's too late: |
| 474 |
{ |
| 475 |
long* x; |
| 476 |
static assert( |
| 477 |
!__traits(compiles, castFrom!long.to!int(x)) |
| 478 |
); |
| 479 |
|
| 480 |
// if cast is still needed, must be changed to: |
| 481 |
auto y = castFrom!(long*).to!int(x); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
@safe unittest |
| 486 |
{ |
| 487 |
import std.conv; |
| 488 |
|
| 489 |
// conversion at compile time |
| 490 |
auto string1 = hexString!"304A314B"; |
| 491 |
assert(string1 == "0J1K"); |
| 492 |
auto string2 = hexString!"304A314B"w; |
| 493 |
assert(string2 == "0J1K"w); |
| 494 |
auto string3 = hexString!"304A314B"d; |
| 495 |
assert(string3 == "0J1K"d); |
| 496 |
} |
| 497 |
|
| 498 |
@safe unittest |
| 499 |
{ |
| 500 |
import std.conv; |
| 501 |
|
| 502 |
import std.algorithm.comparison : equal; |
| 503 |
|
| 504 |
assert(toChars(1).equal("1")); |
| 505 |
assert(toChars(1_000_000).equal("1000000")); |
| 506 |
|
| 507 |
assert(toChars!(2)(2U).equal("10")); |
| 508 |
assert(toChars!(16)(255U).equal("ff")); |
| 509 |
assert(toChars!(16, char, LetterCase.upper)(255U).equal("FF")); |
| 510 |
} |
| 511 |
|
| 512 |
@safe unittest |
| 513 |
{ |
| 514 |
import std.conv; |
| 515 |
|
| 516 |
uint n = 0xDEADBEEF; |
| 517 |
|
| 518 |
version (LittleEndian) |
| 519 |
assert(n.bitCast!(ubyte[4]) == [0xEF, 0xBE, 0xAD, 0xDE]); |
| 520 |
version (BigEndian) |
| 521 |
assert(n.bitCast!(ubyte[4]) == [0xDE, 0xAD, 0xBE, 0xEF]); |
| 522 |
} |
| 523 |
|