Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_checkedint.d

raw
1 @safe unittest
2 {
3 import std.checkedint;
4
5 int[] concatAndAdd(int[] a, int[] b, int offset)
6 {
7 // Aborts on overflow on size computation
8 auto r = new int[(checked(a.length) + b.length).get];
9 // Aborts on overflow on element computation
10 foreach (i; 0 .. a.length)
11 r[i] = (a[i] + checked(offset)).get;
12 foreach (i; 0 .. b.length)
13 r[i + a.length] = (b[i] + checked(offset)).get;
14 return r;
15 }
16 assert(concatAndAdd([1, 2, 3], [4, 5], -1) == [0, 1, 2, 3, 4]);
17 }
18
19 @safe unittest
20 {
21 import std.checkedint;
22
23 auto x = (cast(byte) 127).checked!Saturate;
24 assert(x == 127);
25 x++;
26 assert(x == 127);
27 }
28
29 @safe unittest
30 {
31 import std.checkedint;
32
33 auto x = 100.checked!WithNaN;
34 assert(x == 100);
35 x /= 0;
36 assert(x.isNaN);
37 }
38
39 @safe unittest
40 {
41 import std.checkedint;
42
43 uint x = 1;
44 auto y = x.checked!ProperCompare;
45 assert(x < -1); // built-in comparison
46 assert(y > -1); // ProperCompare
47 }
48
49 @safe unittest
50 {
51 import std.checkedint;
52
53 import std.exception : assertThrown;
54 auto x = -1.checked!Throw;
55 assertThrown(x / 0);
56 assertThrown(x + int.min);
57 assertThrown(x == uint.max);
58 }
59
60 @safe unittest
61 {
62 import std.checkedint;
63
64 auto x = checked(ubyte(42));
65 static assert(is(typeof(x.get()) == ubyte));
66 assert(x.get == 42);
67 const y = checked(ubyte(42));
68 static assert(is(typeof(y.get()) == const ubyte));
69 assert(y.get == 42);
70
71 }
72
73 @safe unittest
74 {
75 import std.checkedint;
76
77 assert(Checked!short.min == -32768);
78 assert(Checked!(short, WithNaN).min == -32767);
79 assert(Checked!(uint, WithNaN).max == uint.max - 1);
80
81 }
82
83 @safe unittest
84 {
85 import std.checkedint;
86
87 auto a = checked(42L);
88 assert(a == 42);
89 auto b = Checked!long(4242); // convert 4242 to long
90 assert(b == 4242);
91
92 }
93
94 @safe unittest
95 {
96 import std.checkedint;
97
98 Checked!long a;
99 a = 42L;
100 assert(a == 42);
101 a = 4242;
102 assert(a == 4242);
103
104 }
105
106 @safe unittest
107 {
108 import std.checkedint;
109
110 Checked!long a, b;
111 a = b = 3;
112 assert(a == 3 && b == 3);
113
114 }
115
116 @system unittest
117 {
118 import std.checkedint;
119
120 import std.conv : to;
121
122 const a = to!long("1234");
123 const b = to!(Checked!long)("1234");
124 assert(a == b);
125
126 }
127
128 @safe unittest
129 {
130 import std.checkedint;
131
132 assert(cast(uint) checked(42) == 42);
133 assert(cast(uint) checked!WithNaN(-42) == uint.max);
134
135 }
136
137 @safe unittest
138 {
139 import std.checkedint;
140
141 import std.traits : isUnsigned;
142
143 static struct MyHook
144 {
145 static bool thereWereErrors;
146 static bool hookOpEquals(L, R)(L lhs, R rhs)
147 {
148 if (lhs != rhs) return false;
149 static if (isUnsigned!L && !isUnsigned!R)
150 {
151 if (lhs > 0 && rhs < 0) thereWereErrors = true;
152 }
153 else static if (isUnsigned!R && !isUnsigned!L)
154 if (lhs < 0 && rhs > 0) thereWereErrors = true;
155 // Preserve built-in behavior.
156 return true;
157 }
158 }
159 auto a = checked!MyHook(-42);
160 assert(a == uint(-42));
161 assert(MyHook.thereWereErrors);
162 MyHook.thereWereErrors = false;
163 assert(checked!MyHook(uint(-42)) == -42);
164 assert(MyHook.thereWereErrors);
165 static struct MyHook2
166 {
167 static bool hookOpEquals(L, R)(L lhs, R rhs)
168 {
169 return lhs == rhs;
170 }
171 }
172 MyHook.thereWereErrors = false;
173 assert(checked!MyHook2(uint(-42)) == a);
174 // Hook on left hand side takes precedence, so no errors
175 assert(!MyHook.thereWereErrors);
176
177 }
178
179 @system unittest
180 {
181 import std.checkedint;
182
183 import std.format;
184
185 assert(format("%04d", checked(15)) == "0015");
186 assert(format("0x%02x", checked(15)) == "0x0f");
187
188 }
189
190 @safe unittest
191 {
192 import std.checkedint;
193
194 import std.traits : isUnsigned;
195
196 static struct MyHook
197 {
198 static bool thereWereErrors;
199 static int hookOpCmp(L, R)(L lhs, R rhs)
200 {
201 static if (isUnsigned!L && !isUnsigned!R)
202 {
203 if (rhs < 0 && rhs >= lhs)
204 thereWereErrors = true;
205 }
206 else static if (isUnsigned!R && !isUnsigned!L)
207 {
208 if (lhs < 0 && lhs >= rhs)
209 thereWereErrors = true;
210 }
211 // Preserve built-in behavior.
212 return lhs < rhs ? -1 : lhs > rhs;
213 }
214 }
215 auto a = checked!MyHook(-42);
216 assert(a > uint(42));
217 assert(MyHook.thereWereErrors);
218 static struct MyHook2
219 {
220 static int hookOpCmp(L, R)(L lhs, R rhs)
221 {
222 // Default behavior
223 return lhs < rhs ? -1 : lhs > rhs;
224 }
225 }
226 MyHook.thereWereErrors = false;
227 assert(Checked!(uint, MyHook2)(uint(-42)) <= a);
228 //assert(Checked!(uint, MyHook2)(uint(-42)) >= a);
229 // Hook on left hand side takes precedence, so no errors
230 assert(!MyHook.thereWereErrors);
231 assert(a <= Checked!(uint, MyHook2)(uint(-42)));
232 assert(MyHook.thereWereErrors);
233
234 }
235
236 @safe unittest
237 {
238 import std.checkedint;
239
240 static struct MyHook
241 {
242 static bool thereWereErrors;
243 static L hookOpUnary(string x, L)(L lhs)
244 {
245 if (x == "-" && lhs == -lhs) thereWereErrors = true;
246 return -lhs;
247 }
248 }
249 auto a = checked!MyHook(long.min);
250 assert(a == -a);
251 assert(MyHook.thereWereErrors);
252 auto b = checked!void(42);
253 assert(++b == 43);
254
255 }
256
257 @safe unittest
258 {
259 import std.checkedint;
260
261 static struct MyHook
262 {
263 static bool thereWereErrors;
264 static T onLowerBound(Rhs, T)(Rhs rhs, T bound)
265 {
266 thereWereErrors = true;
267 return bound;
268 }
269 static T onUpperBound(Rhs, T)(Rhs rhs, T bound)
270 {
271 thereWereErrors = true;
272 return bound;
273 }
274 }
275 auto x = checked!MyHook(byte.min);
276 x -= 1;
277 assert(MyHook.thereWereErrors);
278 MyHook.thereWereErrors = false;
279 x = byte.max;
280 x += 1;
281 assert(MyHook.thereWereErrors);
282
283 }
284
285 @safe @nogc pure nothrow unittest
286 {
287 import std.checkedint;
288
289 // Hook that ignores all problems.
290 static struct Ignore
291 {
292 @nogc nothrow pure @safe static:
293 Dst onBadCast(Dst, Src)(Src src) { return cast(Dst) src; }
294 Lhs onLowerBound(Rhs, T)(Rhs rhs, T bound) { return cast(T) rhs; }
295 T onUpperBound(Rhs, T)(Rhs rhs, T bound) { return cast(T) rhs; }
296 bool hookOpEquals(Lhs, Rhs)(Lhs lhs, Rhs rhs) { return lhs == rhs; }
297 int hookOpCmp(Lhs, Rhs)(Lhs lhs, Rhs rhs) { return (lhs > rhs) - (lhs < rhs); }
298 typeof(~Lhs()) onOverflow(string x, Lhs)(ref Lhs lhs) { return mixin(x ~ "lhs"); }
299 typeof(Lhs() + Rhs()) onOverflow(string x, Lhs, Rhs)(Lhs lhs, Rhs rhs)
300 {
301 static if (x == "/")
302 return typeof(lhs / rhs).min;
303 else
304 return mixin("lhs" ~ x ~ "rhs");
305 }
306 }
307
308 auto x = Checked!(int, Ignore)(5) + 7;
309 }
310
311 @safe unittest
312 {
313 import std.checkedint;
314
315 static assert(is(typeof(checked(42)) == Checked!int));
316 assert(checked(42) == Checked!int(42));
317 static assert(is(typeof(checked!WithNaN(42)) == Checked!(int, WithNaN)));
318 assert(checked!WithNaN(42) == Checked!(int, WithNaN)(42));
319 }
320
321 @safe unittest
322 {
323 import std.checkedint;
324
325 void test(T)()
326 {
327 Checked!(int, Abort) x;
328 x = 42;
329 auto x1 = cast(T) x;
330 assert(x1 == 42);
331 //x1 += long(int.max);
332 }
333 test!short;
334 test!(const short);
335 test!(immutable short);
336 }
337
338 @safe unittest
339 {
340 import std.checkedint;
341
342 void test(T)()
343 {
344 Checked!(int, Throw) x;
345 x = 42;
346 auto x1 = cast(T) x;
347 assert(x1 == 42);
348 x = T.max + 1;
349 import std.exception : assertThrown, assertNotThrown;
350 assertThrown(cast(T) x);
351 x = x.max;
352 assertThrown(x += 42);
353 assertThrown(x += 42L);
354 x = x.min;
355 assertThrown(-x);
356 assertThrown(x -= 42);
357 assertThrown(x -= 42L);
358 x = -1;
359 assertNotThrown(x == -1);
360 assertThrown(x == uint(-1));
361 assertNotThrown(x <= -1);
362 assertThrown(x <= uint(-1));
363 }
364 test!short;
365 test!(const short);
366 test!(immutable short);
367 }
368
369 @safe unittest
370 {
371 import std.checkedint;
372
373 auto x = checked!Warn(-42);
374 // Passes
375 assert(x == -42);
376 // Passes but prints a warning
377 // assert(x == uint(-42));
378
379 }
380
381 @safe unittest
382 {
383 import std.checkedint;
384
385 auto x = checked!Warn(-42);
386 // Passes
387 assert(x <= -42);
388 // Passes but prints a warning
389 // assert(x <= uint(-42));
390
391 }
392
393 @safe unittest
394 {
395 import std.checkedint;
396
397 auto x = checked!Warn(42);
398 short x1 = cast(short) x;
399 //x += long(int.max);
400 auto y = checked!Warn(cast(const int) 42);
401 short y1 = cast(const byte) y;
402 }
403
404 @safe unittest
405 {
406 import std.checkedint;
407
408 alias opEqualsProper = ProperCompare.hookOpEquals;
409 assert(opEqualsProper(42, 42));
410 assert(opEqualsProper(42.0, 42.0));
411 assert(opEqualsProper(42u, 42));
412 assert(opEqualsProper(42, 42u));
413 assert(-1 == 4294967295u);
414 assert(!opEqualsProper(-1, 4294967295u));
415 assert(!opEqualsProper(const uint(-1), -1));
416 assert(!opEqualsProper(uint(-1), -1.0));
417 assert(3_000_000_000U == -1_294_967_296);
418 assert(!opEqualsProper(3_000_000_000U, -1_294_967_296));
419 }
420
421 @safe unittest
422 {
423 import std.checkedint;
424
425 auto x = checked!WithNaN(422);
426 assert((cast(ubyte) x) == 255);
427 x = checked!WithNaN(-422);
428 assert((cast(byte) x) == -128);
429 assert(cast(short) x == -422);
430 assert(cast(bool) x);
431 x = x.init; // set back to NaN
432 assert(x != true);
433 assert(x != false);
434
435 }
436
437 @safe unittest
438 {
439 import std.checkedint;
440
441 Checked!(int, WithNaN) x;
442 assert(!(x < 0) && !(x > 0) && !(x == 0));
443 x = 1;
444 assert(x > 0 && !(x < 0) && !(x == 0));
445
446 }
447
448 @safe unittest
449 {
450 import std.checkedint;
451
452 Checked!(int, WithNaN) x;
453 ++x;
454 assert(x.isNaN);
455 x = 1;
456 assert(!x.isNaN);
457 x = -x;
458 ++x;
459 assert(!x.isNaN);
460
461 }
462
463 @safe unittest
464 {
465 import std.checkedint;
466
467 Checked!(int, WithNaN) x;
468 assert((x + 1).isNaN);
469 x = 100;
470 assert(!(x + 1).isNaN);
471
472 }
473
474 @safe unittest
475 {
476 import std.checkedint;
477
478 Checked!(int, WithNaN) x;
479 assert((1 + x).isNaN);
480 x = 100;
481 assert(!(1 + x).isNaN);
482
483 }
484
485 @safe unittest
486 {
487 import std.checkedint;
488
489 Checked!(int, WithNaN) x;
490 x += 4;
491 assert(x.isNaN);
492 x = 0;
493 x += 4;
494 assert(!x.isNaN);
495 x += int.max;
496 assert(x.isNaN);
497
498 }
499
500 @safe unittest
501 {
502 import std.checkedint;
503
504 auto x1 = Checked!(int, WithNaN)();
505 assert(x1.isNaN);
506 assert(x1.get == int.min);
507 assert(x1 != x1);
508 assert(!(x1 < x1));
509 assert(!(x1 > x1));
510 assert(!(x1 == x1));
511 ++x1;
512 assert(x1.isNaN);
513 assert(x1.get == int.min);
514 --x1;
515 assert(x1.isNaN);
516 assert(x1.get == int.min);
517 x1 = 42;
518 assert(!x1.isNaN);
519 assert(x1 == x1);
520 assert(x1 <= x1);
521 assert(x1 >= x1);
522 static assert(x1.min == int.min + 1);
523 x1 += long(int.max);
524 }
525
526 @safe unittest
527 {
528 import std.checkedint;
529
530 auto x1 = Checked!(int, WithNaN)();
531 assert(x1.isNaN);
532 x1 = 1;
533 assert(!x1.isNaN);
534 x1 = x1.init;
535 assert(x1.isNaN);
536 }
537
538 @safe unittest
539 {
540 import std.checkedint;
541
542 auto x = checked!Saturate(short(100));
543 x += 33000;
544 assert(x == short.max);
545 x -= 70000;
546 assert(x == short.min);
547
548 }
549
550 @safe unittest
551 {
552 import std.checkedint;
553
554 assert(checked!Saturate(int.max) + 1 == int.max);
555 assert(checked!Saturate(100) ^^ 10 == int.max);
556 assert(checked!Saturate(-100) ^^ 10 == int.max);
557 assert(checked!Saturate(100) / 0 == int.max);
558 assert(checked!Saturate(100) << -1 == 0);
559 assert(checked!Saturate(100) << 33 == int.max);
560 assert(checked!Saturate(100) >> -1 == int.max);
561 assert(checked!Saturate(100) >> 33 == 0);
562
563 }
564
565 @safe unittest
566 {
567 import std.checkedint;
568
569 auto x = checked!Saturate(int.max);
570 ++x;
571 assert(x == int.max);
572 --x;
573 assert(x == int.max - 1);
574 x = int.min;
575 assert(-x == int.max);
576 x -= 42;
577 assert(x == int.min);
578 assert(x * -2 == int.max);
579 }
580
581 @safe unittest
582 {
583 import std.checkedint;
584
585 bool overflow;
586 assert(opChecked!"+"(const short(1), short(1), overflow) == 2 && !overflow);
587 assert(opChecked!"+"(1, 1, overflow) == 2 && !overflow);
588 assert(opChecked!"+"(1, 1u, overflow) == 2 && !overflow);
589 assert(opChecked!"+"(-1, 1u, overflow) == 0 && !overflow);
590 assert(opChecked!"+"(1u, -1, overflow) == 0 && !overflow);
591 }
592
593 @safe unittest
594 {
595 import std.checkedint;
596
597 bool overflow;
598 assert(opChecked!"-"(1, 1, overflow) == 0 && !overflow);
599 assert(opChecked!"-"(1, 1u, overflow) == 0 && !overflow);
600 assert(opChecked!"-"(1u, -1, overflow) == 2 && !overflow);
601 assert(opChecked!"-"(-1, 1u, overflow) == 0 && overflow);
602 }
603
604 @safe unittest
605 {
606 import std.checkedint;
607
608 struct MyHook
609 {
610 static size_t hookToHash(T)(const T payload) nothrow @trusted
611 {
612 return .hashOf(payload);
613 }
614 }
615
616 int[Checked!(int, MyHook)] aa;
617 Checked!(int, MyHook) var = 42;
618 aa[var] = 100;
619
620 assert(aa[var] == 100);
621
622 int[Checked!(int, Abort)] bb;
623 Checked!(int, Abort) var2 = 42;
624 bb[var2] = 100;
625
626 assert(bb[var2] == 100);
627 }
628