enumoid 0.5.0

Enum Indexed Containers
Documentation
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
use crate::test::drop_tracker::DropTracker;
use crate::test::types::Three;
use enumoid::EnumOptionMap;
use enumoid::EnumSize;
use std::cell::Cell;

#[test]
fn test_empty_state() {
  let map = EnumOptionMap::<Three, u16>::new();

  assert!(map.is_empty(), "Expected new map to be empty");
  assert!(!map.is_full(), "Expected new map to not be full");
  assert_eq!(map.count(), 0, "Expected new map to have count of 0");
  assert_eq!(
    map.is_vec(),
    EnumSize::from_usize(0),
    "Expected empty map to be representable as 0-length vector"
  );

  // Test that all keys return None
  assert_eq!(
    map.get(Three::A),
    None,
    "Expected None for Three::A in empty map"
  );
  assert_eq!(
    map.get(Three::B),
    None,
    "Expected None for Three::B in empty map"
  );
  assert_eq!(
    map.get(Three::C),
    None,
    "Expected None for Three::C in empty map"
  );

  // Test contains
  assert!(
    !map.contains(Three::A),
    "Expected empty map to not contain Three::A"
  );
  assert!(
    !map.contains(Three::B),
    "Expected empty map to not contain Three::B"
  );
  assert!(
    !map.contains(Three::C),
    "Expected empty map to not contain Three::C"
  );
}

#[test]
fn test_set_and_get() {
  let mut map = EnumOptionMap::<Three, u16>::new();

  // Test setting a value
  let old_value = map.set(Three::B, Some(200));
  assert_eq!(
    old_value, None,
    "Expected set() to return None for previously empty key"
  );
  assert!(
    !map.is_empty(),
    "Expected map to not be empty after setting a value"
  );
  assert_eq!(
    map.count(),
    1,
    "Expected map to have count of 1 after setting one value"
  );

  // Test getting the value
  assert_eq!(
    map.get(Three::B),
    Some(&200),
    "Expected get() to return the set value"
  );
  assert_eq!(
    map.get(Three::A),
    None,
    "Expected get() to return None for unset key"
  );
  assert_eq!(
    map.get(Three::C),
    None,
    "Expected get() to return None for unset key"
  );

  // Test overwriting a value
  let old_value = map.set(Three::B, Some(300));
  assert_eq!(
    old_value,
    Some(200),
    "Expected set() to return previous value when overwriting"
  );
  assert_eq!(
    map.get(Three::B),
    Some(&300),
    "Expected get() to return the new value"
  );
  assert_eq!(
    map.count(),
    1,
    "Expected count to remain 1 after overwriting"
  );

  // Test setting to None (removal)
  let old_value = map.set(Three::B, None);
  assert_eq!(
    old_value,
    Some(300),
    "Expected set(None) to return previous value"
  );
  assert_eq!(
    map.get(Three::B),
    None,
    "Expected get() to return None after setting to None"
  );
  assert!(
    map.is_empty(),
    "Expected map to be empty after setting only value to None"
  );
  assert_eq!(
    map.count(),
    0,
    "Expected count to be 0 after removing only value"
  );
}

#[test]
fn test_insert_and_remove() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test insert
  let old_value = map.insert(Three::A, 100);
  assert_eq!(
    old_value, None,
    "Expected insert() to return None for new key"
  );
  assert_eq!(
    map.get(Three::A),
    Some(&100),
    "Expected get() to return inserted value"
  );
  assert_eq!(map.count(), 1, "Expected count to be 1 after insert");

  // Test insert with overwrite
  let old_value = map.insert(Three::A, 200);
  assert_eq!(
    old_value,
    Some(100),
    "Expected insert() to return previous value when overwriting"
  );
  assert_eq!(
    map.get(Three::A),
    Some(&200),
    "Expected get() to return new inserted value"
  );
  assert_eq!(
    map.count(),
    1,
    "Expected count to remain 1 after overwriting"
  );

  // Test remove
  let removed_value = map.remove(Three::A);
  assert_eq!(
    removed_value,
    Some(200),
    "Expected remove() to return the removed value"
  );
  assert_eq!(
    map.get(Three::A),
    None,
    "Expected get() to return None after removal"
  );
  assert_eq!(map.count(), 0, "Expected count to be 0 after removal");
  assert!(
    map.is_empty(),
    "Expected map to be empty after removing only value"
  );

  // Test remove non-existent key
  let removed_value = map.remove(Three::B);
  assert_eq!(
    removed_value, None,
    "Expected remove() to return None for non-existent key"
  );
}

#[test]
fn test_insert_remove_by_index() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test insert_by_index
  let old_value = map.insert_by_index(Three::A.into(), 42);
  assert_eq!(
    old_value, None,
    "Expected insert_by_index() to return None for new index"
  );
  assert_eq!(
    map.get_by_index(Three::A.into()),
    Some(&42),
    "Expected get_by_index() to return inserted value"
  );

  // Test remove_by_index
  let removed_value = map.remove_by_index(Three::A.into());
  assert_eq!(
    removed_value,
    Some(42),
    "Expected remove_by_index() to return the removed value"
  );
  assert_eq!(
    map.get_by_index(Three::A.into()),
    None,
    "Expected get_by_index() to return None after removal"
  );

  // Test remove_by_index non-existent
  let removed_value = map.remove_by_index(Three::B.into());
  assert_eq!(
    removed_value, None,
    "Expected remove_by_index() to return None for non-existent index"
  );
}

#[test]
fn test_clear() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Add some values
  map.insert(Three::A, 10);
  map.insert(Three::B, 20);
  map.insert(Three::C, 30);

  assert!(!map.is_empty(), "Expected map to not be empty before clear");
  assert_eq!(map.count(), 3, "Expected count to be 3 before clear");

  // Clear the map
  map.clear();

  assert!(map.is_empty(), "Expected map to be empty after clear");
  assert_eq!(map.count(), 0, "Expected count to be 0 after clear");
  assert_eq!(
    map.get(Three::A),
    None,
    "Expected get() to return None after clear"
  );
  assert_eq!(
    map.get(Three::B),
    None,
    "Expected get() to return None after clear"
  );
  assert_eq!(
    map.get(Three::C),
    None,
    "Expected get() to return None after clear"
  );
}

#[test]
fn test_contains() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test contains on empty map
  assert!(
    !map.contains(Three::A),
    "Expected empty map to not contain Three::A"
  );
  assert!(
    !map.contains(Three::B),
    "Expected empty map to not contain Three::B"
  );
  assert!(
    !map.contains(Three::C),
    "Expected empty map to not contain Three::C"
  );

  // Add a value and test contains
  map.insert(Three::B, 100);
  assert!(
    !map.contains(Three::A),
    "Expected map to not contain Three::A"
  );
  assert!(map.contains(Three::B), "Expected map to contain Three::B");
  assert!(
    !map.contains(Three::C),
    "Expected map to not contain Three::C"
  );

  // Test contains_index
  assert!(
    !map.contains_index(Three::A.into()),
    "Expected map to not contain index A"
  );
  assert!(
    map.contains_index(Three::B.into()),
    "Expected map to contain index B"
  );
  assert!(
    !map.contains_index(Three::C.into()),
    "Expected map to not contain index C"
  );
}

#[test]
fn test_keys() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test keys on empty map
  let keys = map.keys();
  assert!(
    !keys.contains(Three::A),
    "Expected empty map keys to not contain Three::A"
  );
  assert!(
    !keys.contains(Three::B),
    "Expected empty map keys to not contain Three::B"
  );
  assert!(
    !keys.contains(Three::C),
    "Expected empty map keys to not contain Three::C"
  );

  // Add values and test keys
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  let keys = map.keys();
  assert!(keys.contains(Three::A), "Expected keys to contain Three::A");
  assert!(
    !keys.contains(Three::B),
    "Expected keys to not contain Three::B"
  );
  assert!(keys.contains(Three::C), "Expected keys to contain Three::C");
}

#[test]
fn test_full_state() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Gradually fill the map
  assert!(!map.is_full(), "Expected map to not be full initially");

  map.insert(Three::A, 10);
  assert!(
    !map.is_full(),
    "Expected map to not be full with 1/3 values"
  );

  map.insert(Three::B, 20);
  assert!(
    !map.is_full(),
    "Expected map to not be full with 2/3 values"
  );

  map.insert(Three::C, 30);
  assert!(map.is_full(), "Expected map to be full with 3/3 values");

  // Remove one value
  map.remove(Three::B);
  assert!(
    !map.is_full(),
    "Expected map to not be full after removing a value"
  );
}

#[test]
fn test_is_vec() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Empty map should be representable as 0-length vector
  assert_eq!(
    map.is_vec(),
    EnumSize::from_usize(0),
    "Expected empty map to be representable as 0-length vector"
  );

  // Add first value - should be representable as 1-length vector
  map.insert(Three::A, 10);
  assert_eq!(
    map.is_vec(),
    EnumSize::from_usize(1),
    "Expected [A] to be representable as 1-length vector"
  );

  // Add second contiguous value - should be representable as 2-length vector
  map.insert(Three::B, 20);
  assert_eq!(
    map.is_vec(),
    EnumSize::from_usize(2),
    "Expected [A,B] to be representable as 2-length vector"
  );

  // Add third contiguous value - should be representable as 3-length vector
  map.insert(Three::C, 30);
  assert_eq!(
    map.is_vec(),
    EnumSize::from_usize(3),
    "Expected [A,B,C] to be representable as 3-length vector"
  );

  // Remove middle value - should not be representable as vector
  map.remove(Three::B);
  assert_eq!(
    map.is_vec(),
    None,
    "Expected [A,_,C] to not be representable as vector"
  );

  // Remove first value, leaving only C - should not be representable as vector
  map.remove(Three::A);
  assert_eq!(
    map.is_vec(),
    None,
    "Expected [_,_,C] to not be representable as vector"
  );
}

#[test]
fn test_eq() {
  let mut a = EnumOptionMap::<Three, i32>::new();
  a.insert(Three::A, 10);
  a.insert(Three::C, 30);

  // Equal maps compare equal.
  let mut b = EnumOptionMap::<Three, i32>::new();
  b.insert(Three::A, 10);
  b.insert(Three::C, 30);
  assert_eq!(a, b, "Expected maps with identical entries to be equal");

  // A differing value at a shared key compares unequal.
  let mut differing_value = EnumOptionMap::<Three, i32>::new();
  differing_value.insert(Three::A, 10);
  differing_value.insert(Three::C, 99);
  assert_ne!(
    a, differing_value,
    "Expected maps with a differing value to be unequal"
  );

  // A differing presence (Some vs None) compares unequal.
  let mut differing_presence = EnumOptionMap::<Three, i32>::new();
  differing_presence.insert(Three::A, 10);
  assert_ne!(
    a, differing_presence,
    "Expected maps with differing presence to be unequal"
  );
}

#[test]
fn test_mutable_get() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test get_mut on empty map
  assert_eq!(
    map.get_mut(Three::A),
    None,
    "Expected get_mut() to return None for empty map"
  );

  // Insert a value and test get_mut
  map.insert(Three::A, 100);

  let value_mut = map
    .get_mut(Three::A)
    .expect("Expected get_mut to return Some for existing key");
  *value_mut += 50;

  assert_eq!(
    map.get(Three::A),
    Some(&150),
    "Expected value to be modified through get_mut()"
  );

  // Test get_by_index_mut
  let value_mut = map
    .get_by_index_mut(Three::A.into())
    .expect("Expected get_by_index_mut to return Some for existing key");
  *value_mut *= 2;

  assert_eq!(
    map.get(Three::A),
    Some(&300),
    "Expected value to be modified through get_by_index_mut()"
  );
}

#[test]
fn test_iteration_present_elements() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test iteration on empty map
  let collected: Vec<_> = map.iter().collect();
  assert_eq!(collected, vec![], "Expected empty iteration for empty map");

  // Add values non-contiguously
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  // Test immutable iteration
  let collected: Vec<_> = map.iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, &10), (Three::C, &30)],
    "Expected iteration to yield inserted values in order"
  );

  // Test mutable iteration
  for (_, value) in map.iter_mut() {
    *value *= 10;
  }

  let collected: Vec<_> = map.iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, &100), (Three::C, &300)],
    "Expected values to be modified through iter_mut()"
  );
}

#[test]
fn test_iterator_exact_size_and_double_ended() {
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  let mut iter = map.iter();

  // Test size_hint
  assert_eq!(
    iter.size_hint().0,
    2,
    "Expected correct size hint lower bound"
  );

  // Test Iterator::nth
  assert_eq!(
    iter.nth(1),
    Some((Three::C, &30)),
    "Expected nth(1) to return second element"
  );

  // Test iterator count
  assert_eq!(map.iter().count(), 2, "Expected iterator count to be 2");

  // Test empty iterator traits
  let empty_map = EnumOptionMap::<Three, i32>::new();
  let empty_iter = empty_map.iter();
  assert_eq!(
    empty_iter.size_hint().0,
    0,
    "Expected empty iterator lower bound to be 0"
  );
}

#[test]
fn test_iterator_partial_consumption() {
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::A, 10);
  map.insert(Three::B, 20);
  map.insert(Three::C, 30);

  let mut iter = map.iter();

  // Consume first element
  assert_eq!(iter.next(), Some((Three::A, &10)), "Expected first element");

  // Test remaining elements
  let remaining: Vec<_> = iter.collect();
  assert_eq!(
    remaining,
    vec![(Three::B, &20), (Three::C, &30)],
    "Expected remaining elements after partial consumption"
  );
}

#[test]
fn test_iterator_single_element() {
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::B, 42);

  let collected: Vec<_> = map.iter().collect();
  assert_eq!(
    collected,
    vec![(Three::B, &42)],
    "Expected single element iteration"
  );

  let mut iter = map.iter();
  assert_eq!(
    iter.next(),
    Some((Three::B, &42)),
    "Expected single element"
  );
  assert_eq!(iter.next(), None, "Expected iterator to be exhausted");
}

#[test]
fn test_swap_present_with_present() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test swap with both values present
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  map.swap(Three::A, Three::C);
  assert_eq!(
    map.get(Three::A),
    Some(&30),
    "Expected swapped value for Three::A"
  );
  assert_eq!(
    map.get(Three::C),
    Some(&10),
    "Expected swapped value for Three::C"
  );
  assert_eq!(map.count(), 2, "Expected count to remain 2 after swap");
}

#[test]
fn test_swap_by_index_present_with_present() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test swap_by_index with both values present
  map.insert(Three::A, 15);
  map.insert(Three::B, 25);

  map.swap_by_index(Three::A.into(), Three::B.into());
  assert_eq!(
    map.get(Three::A),
    Some(&25),
    "Expected swapped value for Three::A after swap_by_index"
  );
  assert_eq!(
    map.get(Three::B),
    Some(&15),
    "Expected swapped value for Three::B after swap_by_index"
  );
  assert_eq!(
    map.count(),
    2,
    "Expected count to remain 2 after swap_by_index"
  );
}

#[test]
fn test_swap_present_with_absent() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test swap with one value present, one absent: the value moves into the
  // previously-absent slot, leaving the source slot empty.
  map.insert(Three::A, 100);

  map.swap(Three::A, Three::B);
  assert_eq!(
    map.get(Three::A),
    None,
    "Expected Three::A to become absent after swapping into an empty slot"
  );
  assert_eq!(
    map.get(Three::B),
    Some(&100),
    "Expected Three::B to receive the value after swap"
  );
  assert_eq!(map.count(), 1, "Expected count to remain 1 after swap");
}

#[test]
fn test_swap_by_index_present_with_absent() {
  let mut map = EnumOptionMap::<Three, i32>::new();

  // Test swap_by_index with one present, one absent: the value moves into the
  // previously-absent slot, leaving the source slot empty.
  map.insert(Three::C, 200);

  map.swap_by_index(Three::C.into(), Three::A.into());
  assert_eq!(
    map.get(Three::C),
    None,
    "Expected Three::C to become absent after swapping into an empty slot"
  );
  assert_eq!(
    map.get(Three::A),
    Some(&200),
    "Expected Three::A to receive the value after swap_by_index"
  );
  assert_eq!(
    map.count(),
    1,
    "Expected count to remain 1 after swap_by_index"
  );
}

#[test]
fn test_from_iterator() {
  // Collecting key-value pairs inserts each into the map.
  let map: EnumOptionMap<Three, i32> =
    [(Three::A, 10), (Three::C, 30)].into_iter().collect();

  assert_eq!(map.get(Three::A), Some(&10), "Expected value for A");
  assert_eq!(map.get(Three::B), None, "Expected absent key B");
  assert_eq!(map.get(Three::C), Some(&30), "Expected value for C");
  assert_eq!(map.count(), 2, "Expected count to be 2 after collecting");
}

#[test]
fn test_extend() {
  // Extending inserts additional pairs into an existing map.
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::A, 10);
  map.extend([(Three::A, 99), (Three::C, 30)]);

  assert_eq!(
    map.get(Three::A),
    Some(&99),
    "Expected overridden value for A"
  );
  assert_eq!(map.get(Three::B), None, "Expected absent key B");
  assert_eq!(
    map.get(Three::C),
    Some(&30),
    "Expected extended value for C"
  );
  assert_eq!(map.count(), 2, "Expected count to be 2 after extending");
}

#[test]
fn test_from_iterator_duplicate_keys_take_last() {
  // Later pairs for the same key override earlier ones.
  let map: EnumOptionMap<Three, i32> =
    [(Three::A, 10), (Three::A, 99)].into_iter().collect();

  assert_eq!(
    map.get(Three::A),
    Some(&99),
    "Expected the last value for a duplicated key"
  );
  assert_eq!(
    map.count(),
    1,
    "Expected count to be 1 despite duplicate keys"
  );
}

#[test]
fn test_from_iterator_empty() {
  // An empty iterator yields an empty map.
  let map: EnumOptionMap<Three, i32> = std::iter::empty().collect();

  assert!(map.is_empty(), "Expected empty map from empty iterator");
  assert_eq!(map.count(), 0, "Expected count of 0 from empty iterator");
}

#[test]
fn test_into_iterator_owned() {
  // The consuming IntoIterator yields owned (key, value) pairs for populated
  // keys only, mirroring FromIterator<(T, V)>.
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  let collected: Vec<(Three, i32)> = map.into_iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, 10), (Three::C, 30)],
    "Expected consuming iteration to yield only populated pairs in order"
  );
}

#[test]
fn test_into_iterator_owned_roundtrips() {
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  let roundtripped: EnumOptionMap<Three, i32> = map.into_iter().collect();

  let mut expected = EnumOptionMap::<Three, i32>::new();
  expected.insert(Three::A, 10);
  expected.insert(Three::C, 30);
  assert_eq!(
    roundtripped, expected,
    "Expected into_iter().collect() to round-trip"
  );
}

#[test]
fn test_into_iterator_owned_empty() {
  let map = EnumOptionMap::<Three, i32>::new();
  let collected: Vec<(Three, i32)> = map.into_iter().collect();
  assert_eq!(
    collected,
    vec![],
    "Expected empty consuming iteration for empty map"
  );
}

#[test]
fn test_into_iterator_by_ref() {
  let mut map = EnumOptionMap::<Three, i32>::new();
  map.insert(Three::A, 10);
  map.insert(Three::C, 30);

  // IntoIterator for &EnumOptionMap
  let collected: Vec<(Three, &i32)> = (&map).into_iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, &10), (Three::C, &30)],
    "Expected &EnumOptionMap iteration to yield populated pairs"
  );

  // IntoIterator for &mut EnumOptionMap
  for (_, value) in &mut map {
    *value += 1;
  }
  assert_eq!(
    map.get(Three::A),
    Some(&11),
    "Expected value mutated via &mut"
  );
  assert_eq!(
    map.get(Three::C),
    Some(&31),
    "Expected value mutated via &mut"
  );
}

#[test]
fn test_into_iterator_owned_drops_only_populated_when_consumed() {
  // Only populated cells hold values; absent keys must never be dropped.
  let drops = Cell::new(0);
  let mut map = EnumOptionMap::<Three, DropTracker>::new();
  map.insert(Three::A, DropTracker::new(1, &drops));
  map.insert(Three::C, DropTracker::new(3, &drops));

  let ids: Vec<i32> = map.into_iter().map(|(_, v)| v.id()).collect();
  assert_eq!(ids, vec![1, 3], "Expected ids of populated keys in order");
  assert_eq!(
    drops.get(),
    2,
    "Expected only the two populated values to be dropped"
  );
}

#[test]
fn test_into_iterator_owned_drops_remainder_when_abandoned() {
  // Abandoning a partially-consumed iterator drops every unyielded populated
  // value exactly once, and nothing for the absent key.
  let drops = Cell::new(0);
  let mut map = EnumOptionMap::<Three, DropTracker>::new();
  map.insert(Three::A, DropTracker::new(1, &drops));
  map.insert(Three::C, DropTracker::new(3, &drops));

  let mut iter = map.into_iter();
  {
    let (key, value) = iter.next().expect("Expected a first pair");
    assert_eq!(key, Three::A, "Expected first populated key");
    assert_eq!(value.id(), 1, "Expected first value id");
    assert_eq!(drops.get(), 0, "Expected no drops while value is held");
  }
  assert_eq!(drops.get(), 1, "Expected the moved-out value to drop");

  drop(iter);
  assert_eq!(
    drops.get(),
    2,
    "Expected the remaining populated value to drop when abandoned"
  );
}