-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.d
More file actions
766 lines (635 loc) · 21 KB
/
flags.d
File metadata and controls
766 lines (635 loc) · 21 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/**
Author: Era Scarecrow <rtcvb32@yahoo.com>
Date: 14 April 2012
License: GPLv3
Description: Basic handling of flags and their types. Since you can't go int to ENUM, this
structure will set, and test flags appropriately. As there's only one state (integral)
you can (hopefully) forcibly cast an int into HandleFlags so long as the size is the same.
---
enum ETEST {none = 0, one = 1, two = 2, three = 3, four = 4}
HandleFlags!(ETEST, int) ftest;
ftest.setFlag(ETEST.two); //check flag setting
assert(ftest.state == 2); //int and enum compares.
assert(ftest.state == ftest.Enum.two); //ftest.Enum.one accessible
//shortcuts avaliable.
assert(ftest.Enum.one == ETEST.one);
ftest.setFlag(ETEST.four); //set a second flag
assert(ftest.state == 6); //2+4, no 6 enum.
//Flags can also encompass multiple bits. in this case all bits returned with an AND must match the flag.
//using checkAll, all of the flags MUST be true in order to be true.
with(ftest.Enum) {
ftest.state = 0;
ftest.setFlag(one);
assert(!ftest.checkAll(one, two, three)); //can't be true, only 1 is there
ftest.setFlag(two);
assert(ftest.checkAll(one, two, three)); //must be true, since 1+2 includes 3.
}
//binary operations/shortcuts are possible.
with(ftest.Enum) {
ftest.state = 0;
assert(!ftest);
ftest = ftest | two;
assert(ftest && ftest.state == 2);
ftest ^= [one, two];
assert(ftest && ftest.state == 1);
assert(ftest & one);
assert(!(ftest & two));
assert(!(ftest - one));
// | and + are the same, so 1 + 1 = 1. (Setting the flag with + doesn't equal arithmetic)
ftest += one;
assert(ftest && ftest.state == 1);
assert(ftest == one);
assert(ftest == [one]);
}
---
*/
module smartmerged.flags;
import std.traits;
import std.exception;
import std.traits;
import std.stdio : writeln;
import std.conv;
@trusted:
//duplicate of above documentation, for correctness.
unittest {
enum ETEST {none = 0, one = 1, two = 2, three = 3, four = 4}
HandleFlags!(ETEST, int) ftest;
ftest.setFlag(ETEST.two); //check flag setting
assert(ftest.state == 2); //int and enum compares.
assert(ftest.state == ftest.Enum.two); //ftest.Enum.one accessible
//shortcuts avaliable.
assert(ftest.Enum.one == ETEST.one);
ftest.setFlag(ETEST.four); //set a second flag
assert(ftest.state == 6); //2+4, no 6 enum.
//Flags can also encompass multiple bits. in this case all bits returned with an AND must match the flag.
//using checkAll, all of the flags MUST be true in order to be true.
with(ftest.Enum) {
ftest.state = 0;
ftest.setFlag(one);
assert(!ftest.checkAll(one, two, three)); //can't be true, only 1 is there
ftest.setFlag(two);
assert(ftest.checkAll(one, two, three)); //must be true, since 1+2 includes 3.
}
//binary operations possible and even tests.
with(ftest.Enum) {
ftest.state = 0;
assert(!ftest);
ftest = ftest | two;
assert(ftest && ftest.state == 2);
ftest ^= [one, two];
assert(ftest && ftest.state == 1);
assert(ftest & one);
assert(!(ftest & two));
assert(!(ftest - one));
// | and + are the same, so 1 + 1 = 1. (Setting the flag with + doesn't equal arithmetic)
ftest += one;
assert(ftest && ftest.state == 1);
assert(ftest == one);
assert(ftest == [one]);
}
}
///
class OverlapError : Error {
this(string msg = null) {
if (msg !is null)
super("Warning! Overlap possible, This could be order specific for flags behavior.\n\n" ~ msg);
else
super("Warning! Overlap possible, This could be order specific for flags behavior.");
}
}
/**
* Returns any potential pairs of overlapping enum flags.
*/
auto getOverlaps(E)(const E[] flags ...)
if (is(E == enum)) {
E[2][] pairs;
if (flags.length > 1) {
foreach(i, l; flags) {
foreach(r; flags[i+1 .. $]) {
if (l & r) {
pairs ~= [l,r];
}
}
}
}
return pairs;
}
/**
* Checks enum contents for any of them that overlap. So an enum containing 1,2 & 3 would overlap, while 1, 2 & 4 wouldn't
* This can help detect potential flag issues later.
*/
template enumBitsOverlap(alias E)
if (is(E == enum)) {
enum enumBitsOverlap = getOverlaps(EnumMembers!(E));
}
unittest {
enum Overlap {none, one, two, three }
enum NoOverlap {none, one, two, four = 4 }
assert(enumBitsOverlap!(Overlap) !is null);
assert(enumBitsOverlap!(NoOverlap) is null);
}
///
struct HandleFlags(E, I)
if (is(E == enum) && isIntegral!(I) && isFloatingPoint!(I) == false) {
I state = 0; ///Holds state.
alias E Enum; ///
alias convToBool this;
/**simple bool conversion, prevents treating the whole thing as an numeric type
and opOpBinary operations won't have unwanted effects.*/
bool convToBool() @safe pure nothrow const {
return state != 0;
}
enum containsOverlaps = enumBitsOverlap!(E); ///
enum overlapsString = to!string(containsOverlaps); ///
///Start with flags set
this(const E[] flags ...) {
setFlag(flags);
}
///Convert numeric's value into a flag
this(I state) {
this.state = state;
}
/**
* checks to see if two flags may interfere with eachother, specifically clearing or xoring.
* This can cause problems where you may get a completely unexpected or invalid flag combination,
* Then it's order specific or shouldn't encompass all the flags supplied. By only using non-overlapping
* flags do you ensure behavior doesn't change.
*
* In the following example, we have C set, if we flip A and C we think A is on and C is off, while
* in reality B is set.
---
enum FLAGS { A = 1, B, C }
auto t = HandleFlags!(FLAGS, int)(C);
with (FLAGS) {
t.flipFlag(A, C);
}
//logically not having specific knowledge of how flags interact (as most of the time they shouldn't),
//then this normally should be true
assert(t.check(A)); //fails
assert(!t.check(C)); //fails
//when in reality
assert(!t.check(A));
assert(t.check(B));
assert(!t.check(C));
//has our enum been this, it would have worked.
enum FLAGS { A = 1, B = 2, C = 4 }
with (FLAGS) {
t.flipFlag(A, C);
}
assert(t.check(A)); //passes as expected
assert(!t.check(C)); //passes
---
* The reason multiple bits will be allowed for flags is mostly for checks, consider
---
enum FLAGS { A = 1, B = 2, Both = A|B }
---
* In this case Both is only true if A & B are on. Forcing a checkAll for multiple flags or specific states
*/
static bool canOverlap(const E[] flag...) @safe pure nothrow {
for(int i; i < (flag.length-1); i++)
foreach(fl; flag[i+1 .. $]) {
if (flag[i] & fl)
return true;
}
return false;
}
///checks if all bits in one enum flag are present.
///an enums with the value 0 are always false
bool isFlagSet(E flag) const @safe pure nothrow {
return (flag && (state & flag) == flag);
}
///Returns true/false if any specified flags has been set.
bool check(const E[] flags ...) const @safe pure nothrow
in {
assert(flags.length, "Empty flags list"); //logically not an error, but we aren't checking anything otherwise.
//overlap not an issue
}
body {
foreach(fl; flags) {
if (isFlagSet(fl))
return true;
}
return false;
}
///Returns true if (and only if) all supplied flags are true/on.
bool checkAll(const E[] flags ...) const @safe pure nothrow
in {
assert(flags.length, "Empty flags list");
//overlap not an issue
}
body {
foreach(fl; flags) {
if (!isFlagSet(fl))
return false;
}
return true;
}
/** Checks if a flag has been set, returning the first matching flag,
otherwise returning the Else flag.*/
E checkElse(E Else, const E[] flags ...) const @safe pure nothrow
in {
assert(flags.length, "Empty flags list");
//overlap not an issue
}
body {
foreach(fl; flags) {
if (isFlagSet(fl))
return fl;
}
return Else;
}
///Sets specific flag(s) on
void setFlag(const E[] flags ...) @safe pure nothrow
in {
assert(flags.length, "Empty flags list");
//overlap not an issue
}
body {
foreach(fl; flags) {
state |= fl;
}
}
///turns listed flags off.
void clearFlag(const E[] flags ...) @safe pure nothrow
in {
assert(flags.length, "Empty flags list");
static if (containsOverlaps) {
if (canOverlap(flags)) {
throw new OverlapError(overlapsString);
}
}
}
body {
foreach(fl; flags) {
state &= (~fl);
}
}
///reverses the state of a specific flag.
void flipFlag(const E[] flags ...) @safe pure nothrow
in {
assert(flags.length, "Empty flags list");
static if (containsOverlaps) {
if (canOverlap(flags))
throw new OverlapError(overlapsString);
}
}
body {
foreach(fl; flags) {
state ^= fl;
}
}
///keeps (ands) only specified flags
void keepOnly(const E[] flags ...) @safe pure nothrow
in {
assert(flags.length, "Empty flags list");
static if (containsOverlaps) {
if (canOverlap(flags))
throw new OverlapError(overlapsString);
}
}
body {
I mask;
foreach(fl; flags){
mask |= fl;
}
state &= mask;
}
//NOTE on binary flag operations. Checks regarding overlapping types not tested (as of yet)
///Binary operators for those that want to treat it like that.
// not currently working without explicit calling.
ref HandleFlags opOpAssign(string op)(const HandleFlags rhs) @trusted pure nothrow
if (op == "+" || op == "-" || op == "^" || op == "&" || op == "|") {
switch(op) {
case "+", "|": state |= rhs.state; break;
case "-": state &= (~rhs.state); break;
case "^": state ^= rhs.state; break;
case "&": state &= rhs.state; break;
default:
}
return this;
}
// not currently working without explicit calling.
ref HandleFlags opOpAssign(string op)(const E[] rhs ...) @trusted pure nothrow
if (op == "+" || op == "-" || op == "^" || op == "&" || op == "|") {
switch(op) {
case "+", "|": setFlag(rhs); break;
case "-": clearFlag(rhs); break;
case "^": flipFlag(rhs); break;
case "&": keepOnly(rhs); break;
default:
}
return this;
}
HandleFlags opBinary(string op)(const HandleFlags rhs) @trusted pure nothrow const
if (op == "+" || op == "-" || op == "^" || op == "&" || op == "|") {
HandleFlags tmp = this;
return tmp.opOpAssign!op(rhs);
}
///Binary operators for those that want to treat it like that. Single flags and whole Flag groups
HandleFlags opBinary(string op)(const E[] rhs ...) @trusted pure nothrow const
if (op == "+" || op == "-" || op == "^" || op == "&" || op == "|") {
HandleFlags tmp = this;
return tmp.opOpAssign!op(rhs);
}
///
bool opEquals(const HandleFlags rhs) const {
return rhs.state == state;
}
///
bool opEquals(const E[] flags ...) const {
HandleFlags tmp = HandleFlags(flags);
return tmp.state == state;
}
//basically converts to an array of all the flags. Thought it may be useful.
E[] opSlice() const {
E[] slice;
foreach(e; EnumMembers!(E)) {
if (isFlagSet(e))
slice ~= e;
}
return slice;
}
}
unittest {
enum ETEST {
none = 0,
one = 1,
two = 2,
three = 3,
four = 4
}
with(ETEST) {
HandleFlags!(ETEST, int) ftest;
//test all flags off. Uses int checks.
assert(ftest.state == 0); //start empty.
//set 2 flag
ftest.setFlag(two);
assert(ftest.state == 2);
//set 4, should now be 6
ftest.setFlag(four);
assert(ftest.state == 6);
//turn off 2 bit
ftest.clearFlag(two);
assert(ftest.state == 4);
//flip 1
ftest.flipFlag(one, two); //4+1+2 = 7
assert(ftest.state == 7);
ftest.flipFlag(two); //4+1
assert(ftest.state == 5);
//check both flags are present
assert(ftest.checkAll(one, four));
assert(ftest.check(one, four));
///check failure of flags
assert(!ftest.checkAll(two, four));
//flip 1 again.
ftest.flipFlag(one); //4+0
assert(ftest.state == 4);
//check truth/else
ETEST x = ftest.checkElse(none, four);
assert(x == four);
x = ftest.checkElse(none, one);
assert(x == none);
assert(ftest.check(four) == true);
assert(ftest.check(two) == false);
ftest = HandleFlags!(ETEST, int)(one, two);
assert(ftest.checkAll(one, two));
assert(ftest.state == 3);
assertThrown!Error(ftest.check(), "Not throwing an error/warning");
assertThrown!Error(ftest.checkAll(), "Not throwing an error/warning");
assertThrown!Error(ftest.checkElse(ETEST.none), "Not throwing an error/warning");
assertThrown!Error(ftest.setFlag(), "Not throwing an error/warning");
assertThrown!Error(ftest.clearFlag(), "Not throwing an error/warning");
assertThrown!Error(ftest.flipFlag(), "Not throwing an error/warning");
assertThrown!Error(ftest.keepOnly(), "Not throwing an error/warning");
assertThrown!Error(ftest.check([]), "Not throwing an error/warning");
assertThrown!Error(ftest.checkAll([]), "Not throwing an error/warning");
assertThrown!Error(ftest.checkElse(ETEST.none,[]), "Not throwing an error/warning");
assertThrown!Error(ftest.setFlag([]), "Not throwing an error/warning");
assertThrown!Error(ftest.clearFlag([]), "Not throwing an error/warning");
assertThrown!Error(ftest.flipFlag([]), "Not throwing an error/warning");
assertThrown!Error(ftest.keepOnly([]), "Not throwing an error/warning");
ftest.keepOnly(one);
assert(ftest.state == 1);
//Flags can also encompass multiple bits. in this case all bits returned with an AND must match the flag.
assert(!ftest.checkAll(one, two, three)); //can't be true, only 1 is.
ftest.state = 3;
assert(ftest.checkAll(one, two, three)); //must be true, since 1+2 includes 3.
//canOverlap tests. Important for multiple bitset check modificaions regarding removing flags
//setting/checking on the other hand is safe.
assert(ftest.canOverlap(one,two,four) == false);
assert(ftest.canOverlap(three,four) == false);
assert(ftest.canOverlap(one,three) == true);
assert(ftest.canOverlap(two,three) == true);
assert(ftest.canOverlap(one,two,three) == true);
//ensure such an overlap throws
assertThrown!OverlapError(ftest.flipFlag(one, three), "Not throwing Overlapping error");
assertThrown!OverlapError(ftest.clearFlag(one, three), "Not throwing Overlapping error");
assertThrown!OverlapError(ftest.keepOnly(one, three), "Not throwing Overlapping error");
assertThrown!OverlapError(ftest.flipFlag(one, three), "Not throwing Overlapping error");
assertThrown!OverlapError(ftest.clearFlag(one, three), "Not throwing Overlapping error");
assertThrown!OverlapError(ftest.keepOnly(one, three), "Not throwing Overlapping error");
assertNotThrown!()(ftest.flipFlag(one, two), "Should not throw");
assertNotThrown!()(ftest.clearFlag(one, two), "Should not throw");
assertNotThrown!()(ftest.keepOnly(one, two), "Should not throw");
assertNotThrown!()(ftest.flipFlag(one, two), "Should not throw");
assertNotThrown!()(ftest.clearFlag(one, two), "Should not throw");
assertNotThrown!()(ftest.keepOnly(one, two), "Should not throw");
//removed: alias Enum this. Was replaced by alias convToBool this.
assert(HandleFlags!(ETEST, int).Enum.four == ETEST.four);
alias HandleFlags!(ETEST, int) Etest; //as a new aliased type
assert(Etest.Enum.four == ETEST.four);
assert(is(Etest.Enum == ETEST));
// ftest.flipFlag(one, three); //intentionally to see the output message
ftest = Etest(one, two);
Etest ftest2 = Etest(two, four);
Etest noChanges, noChangesHF, changes, changesHF;
Etest resultsNoChanges, resultsChanges;
//grouped for folding and separation. TODO: Clean up binary tests
////+, |
{
noChanges = ftest + two; //no change
noChangesHF = ftest + ftest; //no change, all same flags
changes = ftest + four; //add flag
changesHF = ftest + ftest2; //or/add all flags missing.
resultsNoChanges = Etest(one, two);
resultsChanges = Etest(one, two, four);
assert(noChanges == resultsNoChanges);
assert(noChangesHF == resultsNoChanges);
assert(changes == resultsChanges);
assert(changesHF == resultsChanges);
////array version, to be finished later.
noChanges = ftest + [two]; //no change
changes = ftest + [two, four]; //add flag
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
noChanges = ftest | two; //no change
noChangesHF = ftest | ftest; //no change, all same flags
changes = ftest | four; //add flag
changesHF = ftest | ftest2; //or/add all flags missing.
// resultsNoChanges = Etest(one, two);
// resultsChanges = Etest(one, two, four);
assert(noChanges == resultsNoChanges);
assert(noChangesHF == resultsNoChanges);
assert(changes == resultsChanges);
assert(changesHF == resultsChanges);
//array
noChanges = ftest | [two]; //no change
changes = ftest | [two, four]; //add flag
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
//opOpAssign
ftest2 = Etest(four);
noChanges = ftest; changes = ftest;
noChanges += two;
changes += four;
noChangesHF += ftest;
changesHF += ftest2;
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
assert(noChangesHF == resultsNoChanges);
assert(changesHF == resultsChanges);
noChanges = ftest; changes = ftest;
noChanges += [two];
changes += [two, four];
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
//opOpAssign |
ftest2 = Etest(four);
noChanges = ftest; changes = ftest;
noChanges |= two;
changes |= four;
noChangesHF |= ftest;
changesHF |= ftest2;
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
assert(noChangesHF == resultsNoChanges);
assert(changesHF == resultsChanges);
noChanges = ftest; changes = ftest;
noChanges |= [two];
changes |= [two, four];
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
}
////-
{
ftest2 = Etest(four);
noChanges = ftest - four;
noChangesHF = ftest - ftest2;
ftest2 = Etest(two, four);
changes = ftest - two;
changesHF = ftest - ftest2;
resultsNoChanges = Etest(one, two);
resultsChanges = Etest(one);
assert(noChanges == resultsNoChanges);
assert(noChangesHF == resultsNoChanges);
assert(changes == resultsChanges);
assert(changesHF == resultsChanges);
//array
noChanges = ftest; changes = ftest;
noChanges = ftest - [four]; //no change
changes = ftest - [two, four]; //add flag
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
//opOpAssign -
ftest = Etest(one, two);
ftest2 = Etest(four);
noChanges = ftest; changes = ftest;
noChanges -= four;
changes -= two;
noChangesHF -= ftest2;
changesHF -= ftest;
resultsNoChanges = Etest(one, two);
resultsChanges = Etest(one);
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
assert(noChangesHF == resultsNoChanges);
assert(!changesHF);
noChanges = ftest; changes = ftest;
noChanges -= [four];
changes -= [two, four];
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
}
////&
{
ftest2 = Etest(one, two, four);
// noChanges = ftest & four; //only one flag, so we can't keep both flags.
noChangesHF = ftest & ftest2;
ftest2 = Etest(two, four); //overlapping 2
changes = ftest & two; //keep only 2
changesHF = ftest & ftest2;
resultsNoChanges = Etest(one, two);
resultsChanges = Etest(two);
// assert(noChanges == resultsNoChanges);
assert(noChangesHF == resultsNoChanges);
assert(changes == resultsChanges);
assert(changesHF == resultsChanges);
//array
noChanges = ftest; changes = ftest;
noChanges = ftest & [one, two]; //no change
changes = ftest & [two]; //add flag
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
//opOpAssign &
ftest = Etest(one, two);
ftest2 = Etest(two, four);
changesHF = ftest; changes = ftest; noChanges = ftest;
changes &= two;
changesHF &= ftest2;
resultsChanges = Etest(two);
assert(noChanges == resultsNoChanges);
assert(changes == resultsChanges);
assert(changesHF == resultsChanges);
changes &= [two, four];
assert(changes == resultsChanges);
}
////^
{
ftest = Etest(one, two);
ftest2 = Etest(two, four); //1 overlapping
changes = ftest ^ two; //remove flag
changesHF = ftest ^ ftest2; //swap 2
resultsChanges = Etest(one);
assert(changes == resultsChanges);
resultsChanges = Etest(one, four);
assert(changesHF == resultsChanges);
//second xor check for changes
changes = ftest ^ four; //remove flag
resultsChanges = Etest(one, two, four);
assert(changes == resultsChanges);
//array
changes = ftest ^ [two, four]; //add flag
resultsChanges = Etest(one, four);
assert(changes == resultsChanges);
//opOpAssign ^
ftest = Etest(one, two);
ftest2 = Etest(two, four);
changesHF = ftest; changes = ftest;
changes ^= two;
changes ^= four;
changesHF ^= ftest2;
resultsChanges = Etest(one, four);
assert(changes == resultsChanges);
assert(changesHF == resultsChanges);
changes = ftest;
changes ^= [two, four];
assert(changes == resultsChanges);
}
//convert to bool tests
assert(ftest);
assert(ftest & two);
assert(!(ftest & four));
ftest2 = Etest();
assert(!ftest2);
assert(!(ftest2 & ftest));
assert(ftest2 + ftest);
//test compare against flag & arrays
ftest = Etest(two);
assert(ftest == [two]);
assert(ftest != [four]);
assert(ftest == two);
assert(ftest != four);
auto slice = ftest[];
assert(slice == [two]);
}
}