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
use crate::test::drop_tracker::DropTracker;
use crate::test::types::Three;
use enumoid::EnumMap;
use enumoid::EnumOptionMap;
use std::cell::Cell;

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

  // Test that all values are default initialized
  assert_eq!(*map.get(Three::A), 0, "Expected default value for Three::A");
  assert_eq!(*map.get(Three::B), 0, "Expected default value for Three::B");
  assert_eq!(*map.get(Three::C), 0, "Expected default value for Three::C");

  // Test iteration shows all default values
  let collected: Vec<_> = map.iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, &0), (Three::B, &0), (Three::C, &0)],
    "Expected iteration to yield all key-value pairs with default values"
  );
}

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

  map[Three::B] = 200;
  assert_eq!(
    *map.get(Three::B),
    200,
    "Expected value after index assignment"
  );
  assert_eq!(*map.get(Three::A), 0, "Expected unchanged default value");
  assert_eq!(*map.get(Three::C), 0, "Expected unchanged default value");
}

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

  *map.get_mut(Three::C) += 1;
  assert_eq!(
    *map.get(Three::C),
    1,
    "Expected value after mutable reference modification"
  );
  assert_eq!(*map.get(Three::A), 0, "Expected unchanged default value");
  assert_eq!(*map.get(Three::B), 0, "Expected unchanged default value");
}

#[test]
fn test_iteration_all_elements() {
  let mut map = EnumMap::<Three, u16>::new();
  map[Three::A] = 10;
  map[Three::B] = 20;
  map[Three::C] = 30;

  let collected: Vec<_> = map.iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, &10), (Three::B, &20), (Three::C, &30)],
    "Expected iteration to yield all key-value pairs in order"
  );
}

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

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

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

#[test]
fn test_iterator_exact_size() {
  let mut map = EnumMap::<Three, u16>::new();
  map[Three::A] = 10;
  map[Three::B] = 20;
  map[Three::C] = 30;

  let iter = map.iter();

  // Test ExactSizeIterator
  assert_eq!(iter.len(), 3, "Expected iterator length to be 3");

  // Test size_hint
  assert_eq!(iter.size_hint(), (3, Some(3)), "Expected correct size hint");

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

#[test]
fn test_iterator_double_ended() {
  let mut map = EnumMap::<Three, u16>::new();
  map[Three::A] = 10;
  map[Three::B] = 20;
  map[Three::C] = 30;

  // Test DoubleEndedIterator
  let mut iter = map.iter();
  assert_eq!(
    iter.next_back(),
    Some((Three::C, &30)),
    "Expected next_back() to return last element"
  );
  assert_eq!(
    iter.next_back(),
    Some((Three::B, &20)),
    "Expected next_back() to return second-to-last element"
  );
  assert_eq!(
    iter.next(),
    Some((Three::A, &10)),
    "Expected next() to return first element"
  );
  assert_eq!(iter.next(), None, "Expected iterator to be exhausted");
}

#[test]
fn test_into_iterator() {
  let mut map = EnumMap::<Three, u16>::new();
  map[Three::A] = 10;
  map[Three::B] = 20;
  map[Three::C] = 30;

  // Test IntoIterator for &EnumMap
  let collected: Vec<_> = (&map).into_iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, &10), (Three::B, &20), (Three::C, &30)],
    "Expected IntoIterator to work for &EnumMap"
  );

  // Test IntoIterator for &mut EnumMap
  let collected_mut: Vec<_> =
    (&mut map).into_iter().map(|(k, v)| (k, *v)).collect();
  assert_eq!(
    collected_mut,
    vec![(Three::A, 10), (Three::B, 20), (Three::C, 30)],
    "Expected IntoIterator to work for &mut EnumMap"
  );

  // Test for loop syntax
  let mut sum = 0;
  for (_, value) in &map {
    sum += *value;
  }
  assert_eq!(sum, 60, "Expected for loop to sum all values");
}

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

  // Test set method returns old value
  let old_value = map.set(Three::A, 42);
  assert_eq!(old_value, 0, "Expected set() to return old default value");
  assert_eq!(*map.get(Three::A), 42, "Expected new value after set()");

  // Test set method with existing value
  let old_value = map.set(Three::A, 100);
  assert_eq!(old_value, 42, "Expected set() to return previous value");
  assert_eq!(
    *map.get(Three::A),
    100,
    "Expected updated value after second set()"
  );

  // Test set_by_index method
  let old_value = map.set_by_index(Three::B.into(), 200);
  assert_eq!(
    old_value, 0,
    "Expected set_by_index() to return old default value"
  );
  assert_eq!(
    *map.get(Three::B),
    200,
    "Expected new value after set_by_index()"
  );
}

#[test]
fn test_new_with_constructor() {
  let map = EnumMap::<Three, i32>::new_with(|variant| match variant {
    Three::A => 10,
    Three::B => 20,
    Three::C => 30,
  });

  assert_eq!(*map.get(Three::A), 10, "Expected custom value for Three::A");
  assert_eq!(*map.get(Three::B), 20, "Expected custom value for Three::B");
  assert_eq!(*map.get(Three::C), 30, "Expected custom value for Three::C");
}

#[test]
fn test_swap_methods() {
  let mut map = EnumMap::<Three, i32>::new_with(|variant| match variant {
    Three::A => 1,
    Three::B => 2,
    Three::C => 3,
  });

  // Test swap method
  map.swap(Three::A, Three::C);
  assert_eq!(*map.get(Three::A), 3, "Expected swapped value for Three::A");
  assert_eq!(*map.get(Three::C), 1, "Expected swapped value for Three::C");
  assert_eq!(
    *map.get(Three::B),
    2,
    "Expected unchanged value for Three::B"
  );

  // Test swap_by_index method
  map.swap_by_index(Three::A.into(), Three::B.into());
  assert_eq!(
    *map.get(Three::A),
    2,
    "Expected swapped value for Three::A after swap_by_index"
  );
  assert_eq!(
    *map.get(Three::B),
    3,
    "Expected swapped value for Three::B after swap_by_index"
  );
}

#[test]
fn test_slice_access() {
  let mut map = EnumMap::<Three, i32>::new_with(|variant| match variant {
    Three::A => 10,
    Three::B => 20,
    Three::C => 30,
  });

  // Test as_slice
  let slice = map.as_slice();
  assert_eq!(
    slice,
    &[10, 20, 30],
    "Expected slice to contain all values in order"
  );

  // Test as_slice_mut
  let slice_mut = map.as_slice_mut();
  slice_mut[1] = 99;
  assert_eq!(
    *map.get(Three::B),
    99,
    "Expected value change through mutable slice"
  );
}

#[test]
fn test_get_by_index() {
  let map = EnumMap::<Three, i32>::new_with(|variant| match variant {
    Three::A => 100,
    Three::B => 200,
    Three::C => 300,
  });

  assert_eq!(
    *map.get_by_index(Three::A.into()),
    100,
    "Expected correct value for index A"
  );
  assert_eq!(
    *map.get_by_index(Three::B.into()),
    200,
    "Expected correct value for index B"
  );
  assert_eq!(
    *map.get_by_index(Three::C.into()),
    300,
    "Expected correct value for index C"
  );
}

#[test]
fn test_get_by_index_mut() {
  let mut map = EnumMap::<Three, i32>::new_with(|variant| match variant {
    Three::A => 100,
    Three::B => 200,
    Three::C => 300,
  });

  // Test mutable access by index
  *map.get_by_index_mut(Three::B.into()) += 50;
  assert_eq!(
    *map.get_by_index(Three::B.into()),
    250,
    "Expected value to be modified through get_by_index_mut"
  );

  // Test that other values remain unchanged
  assert_eq!(
    *map.get_by_index(Three::A.into()),
    100,
    "Expected unchanged value for index A"
  );
  assert_eq!(
    *map.get_by_index(Three::C.into()),
    300,
    "Expected unchanged value for index C"
  );
}

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

  let map =
    EnumMap::try_from(opt).expect("Expected Ok when option map is full");
  assert_eq!(*map.get(Three::A), 10, "Expected converted value for A");
  assert_eq!(*map.get(Three::B), 20, "Expected converted value for B");
  assert_eq!(*map.get(Three::C), 30, "Expected converted value for C");
}

#[test]
fn test_try_from_partial_option_map_fails() {
  let mut opt = EnumOptionMap::<Three, i32>::new();
  opt.insert(Three::A, 10);
  opt.insert(Three::C, 30); // Missing Three::B, so not full

  assert_eq!(
    EnumMap::try_from(opt),
    Err(()),
    "Expected Err when option map is not full"
  );
}

#[test]
fn test_from_iterator() {
  // Collecting key-value pairs builds a total map.
  let map: EnumMap<Three, i32> =
    [(Three::A, 10), (Three::B, 20), (Three::C, 30)]
      .into_iter()
      .collect();

  assert_eq!(*map.get(Three::A), 10, "Expected collected value for A");
  assert_eq!(*map.get(Three::B), 20, "Expected collected value for B");
  assert_eq!(*map.get(Three::C), 30, "Expected collected value for C");
}

#[test]
fn test_extend() {
  // Extending overlays new pairs onto an existing map.
  let mut map = EnumMap::<Three, i32>::new();
  map.set(Three::A, 1);
  map.extend([(Three::B, 20), (Three::C, 30)]);

  assert_eq!(*map.get(Three::A), 1, "Expected preexisting value for A");
  assert_eq!(*map.get(Three::B), 20, "Expected extended value for B");
  assert_eq!(*map.get(Three::C), 30, "Expected extended value for C");
}

#[test]
fn test_extend_duplicate_keys_take_last() {
  // Later pairs for the same key override earlier ones.
  let mut map = EnumMap::<Three, i32>::new();
  map.extend([(Three::A, 10), (Three::A, 99)]);

  assert_eq!(
    *map.get(Three::A),
    99,
    "Expected the last value for a duplicated key"
  );
}

#[test]
fn test_from_iterator_partial_uses_defaults() {
  // Keys absent from the iterator fall back to the default value.
  let map: EnumMap<Three, i32> =
    [(Three::A, 10), (Three::C, 30)].into_iter().collect();

  assert_eq!(*map.get(Three::A), 10, "Expected collected value for A");
  assert_eq!(
    *map.get(Three::B),
    0,
    "Expected default value for absent key B"
  );
  assert_eq!(*map.get(Three::C), 30, "Expected collected value for C");
}

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

  assert_eq!(
    *map.get(Three::A),
    99,
    "Expected the last value for a duplicated key"
  );
}

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

  assert_eq!(*map.get(Three::A), 0, "Expected default value for A");
  assert_eq!(*map.get(Three::B), 0, "Expected default value for B");
  assert_eq!(*map.get(Three::C), 0, "Expected default value for C");
}

#[test]
fn test_into_iterator_owned() {
  // The consuming IntoIterator yields owned (key, value) pairs, mirroring
  // FromIterator<(T, V)>.
  let map: EnumMap<Three, i32> =
    [(Three::A, 10), (Three::B, 20), (Three::C, 30)]
      .into_iter()
      .collect();
  let collected: Vec<(Three, i32)> = map.into_iter().collect();
  assert_eq!(
    collected,
    vec![(Three::A, 10), (Three::B, 20), (Three::C, 30)],
    "Expected consuming iteration to yield all key-value pairs in order"
  );
}

#[test]
fn test_into_iterator_owned_roundtrips() {
  let map = EnumMap::<Three, i32>::new_with(|k| match k {
    Three::A => 10,
    Three::B => 20,
    Three::C => 30,
  });
  let roundtripped: EnumMap<Three, i32> = map.into_iter().collect();
  let expected = EnumMap::<Three, i32>::new_with(|k| match k {
    Three::A => 10,
    Three::B => 20,
    Three::C => 30,
  });
  assert_eq!(
    roundtripped, expected,
    "Expected into_iter().collect() to round-trip"
  );
}

#[test]
fn test_into_iterator_owned_exact_size() {
  let map = EnumMap::<Three, i32>::new();
  let mut iter = map.into_iter();
  assert_eq!(iter.len(), 3, "Expected ExactSizeIterator len of 3");
  assert_eq!(iter.size_hint(), (3, Some(3)), "Expected exact size hint");
  iter.next();
  assert_eq!(iter.len(), 2, "Expected len to decrease after next()");
}

#[test]
fn test_into_iterator_owned_drops_all_when_consumed() {
  let drops = Cell::new(0);
  let map = EnumMap::<Three, DropTracker>::new_with(|k| match k {
    Three::A => DropTracker::new(1, &drops),
    Three::B => DropTracker::new(2, &drops),
    Three::C => DropTracker::new(3, &drops),
  });

  let ids: Vec<i32> = map.into_iter().map(|(_, v)| v.id()).collect();
  assert_eq!(ids, vec![1, 2, 3], "Expected ids in key order");
  assert_eq!(
    drops.get(),
    3,
    "Expected each yielded value to be dropped exactly once"
  );
}

#[test]
fn test_into_iterator_owned_drops_remainder_when_abandoned() {
  // Abandoning a partially-consumed iterator drops every unyielded value
  // exactly once.
  let drops = Cell::new(0);
  let map = EnumMap::<Three, DropTracker>::new_with(|k| match k {
    Three::A => DropTracker::new(1, &drops),
    Three::B => DropTracker::new(2, &drops),
    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 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(),
    3,
    "Expected the two unyielded values to drop when abandoned"
  );
}

#[test]
fn test_into_iterator_owned_double_ended() {
  // next() and next_back() meet in the middle, each pair yielded once.
  let map: EnumMap<Three, i32> =
    [(Three::A, 10), (Three::B, 20), (Three::C, 30)]
      .into_iter()
      .collect();
  let mut iter = map.into_iter();
  assert_eq!(
    iter.next_back(),
    Some((Three::C, 30)),
    "Expected next_back() to yield the last pair"
  );
  assert_eq!(
    iter.next(),
    Some((Three::A, 10)),
    "Expected next() to yield the first pair"
  );
  assert_eq!(
    iter.next_back(),
    Some((Three::B, 20)),
    "Expected next_back() to yield the remaining middle pair"
  );
  assert_eq!(iter.next(), None, "Expected exhaustion from the front");
  assert_eq!(iter.next_back(), None, "Expected exhaustion from the back");
}

#[test]
fn test_into_iterator_owned_rev() {
  // rev() relies on DoubleEndedIterator and yields keys in reverse order.
  let map: EnumMap<Three, i32> =
    [(Three::A, 10), (Three::B, 20), (Three::C, 30)]
      .into_iter()
      .collect();
  let collected: Vec<(Three, i32)> = map.into_iter().rev().collect();
  assert_eq!(
    collected,
    vec![(Three::C, 30), (Three::B, 20), (Three::A, 10)],
    "Expected reversed iteration to yield pairs in reverse key order"
  );
}

#[test]
fn test_into_iterator_owned_double_ended_exact_size() {
  // len() stays accurate as the range is consumed from both ends.
  let map = EnumMap::<Three, i32>::new();
  let mut iter = map.into_iter();
  assert_eq!(iter.len(), 3, "Expected initial len of 3");
  iter.next();
  assert_eq!(iter.len(), 2, "Expected len 2 after next()");
  iter.next_back();
  assert_eq!(iter.len(), 1, "Expected len 1 after next_back()");
  iter.next();
  assert_eq!(iter.len(), 0, "Expected len 0 once the ends meet");
}

#[test]
fn test_into_iterator_owned_fused() {
  // Once exhausted, the iterator keeps returning None from both ends.
  let map = EnumMap::<Three, i32>::new();
  let mut iter = map.into_iter();
  for _ in 0..3 {
    assert!(
      iter.next().is_some(),
      "Expected three pairs before exhaustion"
    );
  }
  assert_eq!(iter.next(), None, "Expected None once exhausted");
  assert_eq!(iter.next(), None, "Expected fused None on repeat next()");
  assert_eq!(
    iter.next_back(),
    None,
    "Expected fused None from the back once exhausted"
  );
}

#[test]
fn test_into_iterator_owned_double_ended_drops_remainder() {
  // After taking one pair from each end, abandoning the iterator drops only
  // the unyielded middle value, exactly once, and never the yielded ends.
  let drops = Cell::new(0);
  let map = EnumMap::<Three, DropTracker>::new_with(|k| match k {
    Three::A => DropTracker::new(1, &drops),
    Three::B => DropTracker::new(2, &drops),
    Three::C => DropTracker::new(3, &drops),
  });

  let mut iter = map.into_iter();
  {
    let (front_key, front_value) = iter.next().expect("Expected a front pair");
    let (back_key, back_value) =
      iter.next_back().expect("Expected a back pair");
    assert_eq!(front_key, Three::A, "Expected front key A");
    assert_eq!(front_value.id(), 1, "Expected front value id");
    assert_eq!(back_key, Three::C, "Expected back key C");
    assert_eq!(back_value.id(), 3, "Expected back value id");
    assert_eq!(drops.get(), 0, "Expected no drops while values are held");
  }
  assert_eq!(drops.get(), 2, "Expected the two moved-out values to drop");

  drop(iter);
  assert_eq!(
    drops.get(),
    3,
    "Expected the single unyielded middle value to drop exactly once"
  );
}

#[test]
fn test_iter_mut_double_ended() {
  // EnumSliceIterMut is double-ended; mutations through next_back must land.
  let mut map = EnumMap::<Three, u16>::new();
  map[Three::A] = 10;
  map[Three::B] = 20;
  map[Three::C] = 30;

  {
    let mut iter = map.iter_mut();
    let (back_key, back_value) =
      iter.next_back().expect("Expected a back element");
    assert_eq!(back_key, Three::C, "Expected next_back() key to be C");
    *back_value += 1;
    let (front_key, front_value) =
      iter.next().expect("Expected a front element");
    assert_eq!(front_key, Three::A, "Expected next() key to be A");
    *front_value += 1;
    assert_eq!(
      iter.next_back().map(|(k, v)| (k, *v)),
      Some((Three::B, 20)),
      "Expected next_back() to yield the middle element"
    );
    assert!(iter.next().is_none(), "Expected the ends to have met");
    assert!(
      iter.next_back().is_none(),
      "Expected fused None from the back"
    );
  }

  assert_eq!(map[Three::A], 11, "Expected front mutation to persist");
  assert_eq!(map[Three::C], 31, "Expected back mutation to persist");
}