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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
use crate::helpers::range::{range_end, range_start};
use crate::FixedVec;
use core::cmp::Ordering;
use core::iter::Rev;
use core::ops::RangeBounds;
use orx_pinned_vec::utils::slice;
use orx_pinned_vec::{CapacityState, PinnedVec};
use orx_pseudo_default::PseudoDefault;

impl<T> PseudoDefault for FixedVec<T> {
    fn pseudo_default() -> Self {
        Self {
            data: Default::default(),
        }
    }
}

impl<T> PinnedVec<T> for FixedVec<T> {
    type Iter<'a> = core::slice::Iter<'a, T> where T: 'a, Self: 'a;
    type IterMut<'a> = core::slice::IterMut<'a, T> where T: 'a, Self: 'a;
    type IterRev<'a> = Rev<core::slice::Iter<'a, T>> where T: 'a, Self: 'a;
    type IterMutRev<'a> = Rev<core::slice::IterMut<'a, T>> where T: 'a, Self: 'a;
    type SliceIter<'a> = Option<&'a [T]> where T: 'a, Self: 'a;
    type SliceMutIter<'a> = Option<&'a mut [T]> where T: 'a, Self: 'a;

    /// Returns the index of the `element` with the given reference.
    /// This method has *O(1)* time complexity.
    ///
    /// Note that `T: Eq` is not required; reference equality is used.
    ///
    /// # Safety
    ///
    /// Since `FixedVec` implements `PinnedVec`, the underlying memory
    /// of the vector stays pinned; i.e., is not carried to different memory
    /// locations.
    /// Therefore, it is possible and safe to compare an element's reference
    /// to find its position in the vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_fixed_vec::prelude::*;
    ///
    /// let mut vec = FixedVec::new(4);
    /// for i in 0..4 {
    ///     vec.push(10 * i);
    /// }
    ///
    /// assert_eq!(Some(0), vec.index_of(&vec[0]));
    /// assert_eq!(Some(1), vec.index_of(&vec[1]));
    /// assert_eq!(Some(2), vec.index_of(&vec[2]));
    /// assert_eq!(Some(3), vec.index_of(&vec[3]));
    ///
    /// // num certainly does not belong to `vec`
    /// let num = 42;
    /// assert_eq!(None, vec.index_of(&num));
    ///
    /// // even if its value belongs
    /// let num = 20;
    /// assert_eq!(None, vec.index_of(&num));
    ///
    /// // as expected, querying elements of another vector will also fail
    /// let eq_vec = vec![0, 10, 20, 30];
    /// for i in 0..4 {
    ///     assert_eq!(None, vec.index_of(&eq_vec[i]));
    /// }
    /// ```
    #[inline(always)]
    fn index_of(&self, element: &T) -> Option<usize> {
        slice::index_of(&self.data, element)
    }

    fn index_of_ptr(&self, element_ptr: *const T) -> Option<usize> {
        slice::index_of_ptr(&self.data, element_ptr)
    }

    fn push_get_ptr(&mut self, value: T) -> *const T {
        let idx = self.data.len();
        self.data.push(value);
        unsafe { self.data.as_ptr().add(idx) }
    }

    unsafe fn iter_ptr<'v, 'i>(&'v self) -> impl Iterator<Item = *const T> + 'i
    where
        T: 'i,
    {
        let ptr = self.data.as_ptr();
        (0..self.data.len()).map(move |i| unsafe { ptr.add(i) })
    }

    unsafe fn iter_ptr_rev<'v, 'i>(&'v self) -> impl Iterator<Item = *const T> + 'i
    where
        T: 'i,
    {
        let ptr = self.data.as_ptr();
        (0..self.data.len())
            .rev()
            .map(move |i| unsafe { ptr.add(i) })
    }

    /// Returns whether or not the `element` with the given reference belongs to the vector.
    /// This method has *O(1)* time complexity.
    ///
    /// Note that `T: Eq` is not required; memory address is used.
    ///
    /// # Safety
    ///
    /// Since `FixedVec` implements `PinnedVec`, the underlying memory
    /// of the vector stays pinned; i.e., is not carried to different memory
    /// locations.
    /// Therefore, it is possible and safe to compare an element's reference
    /// to find its position in the vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_fixed_vec::prelude::*;
    ///
    /// let mut vec = FixedVec::new(4);
    /// for i in 0..4 {
    ///     vec.push(10 * i);
    /// }
    ///
    /// assert!(vec.contains_reference(&vec[0]));
    /// assert!(vec.contains_reference(&vec[1]));
    /// assert!(vec.contains_reference(&vec[2]));
    /// assert!(vec.contains_reference(&vec[3]));
    ///
    /// // num certainly does not belong to `vec`
    /// let num = 42;
    /// assert!(!vec.contains_reference(&num));
    ///
    /// // even if its value belongs
    /// let num = 20;
    /// assert!(!vec.contains_reference(&num));
    ///
    /// // as expected, querying elements of another vector will also fail
    /// let eq_vec = vec![0, 10, 20, 30];
    /// for i in 0..4 {
    ///     assert!(!vec.contains_reference(&eq_vec[i]));
    /// }
    /// ```
    #[inline(always)]
    fn contains_reference(&self, element: &T) -> bool {
        slice::contains_reference(self.data.as_slice(), element)
    }

    /// Returns whether or not the `element` with the given reference belongs to the vector.
    /// This method has *O(1)* time complexity.
    ///
    /// Note that `T: Eq` is not required; memory pointer is used.
    ///
    /// # Safety
    ///
    /// Since `FixedVec` implements `PinnedVec`, the underlying memory
    /// of the vector stays pinned; i.e., is not carried to different memory
    /// locations.
    /// Therefore, it is possible and safe to compare an element's reference
    /// to find its position in the vector.
    #[inline(always)]
    fn contains_ptr(&self, element_ptr: *const T) -> bool {
        slice::contains_ptr(self.data.as_slice(), element_ptr)
    }

    fn clear(&mut self) {
        self.data.clear();
    }

    fn capacity(&self) -> usize {
        self.data.capacity()
    }

    fn capacity_state(&self) -> CapacityState {
        CapacityState::FixedCapacity(self.capacity())
    }

    /// Clones and appends all elements in a slice to the Vec.
    ///
    /// Iterates over the slice other, clones each element, and then appends it to this Vec. The other slice is traversed in-order.
    ///
    /// Note that this function is same as extend except that it is specialized to work with slices instead. If and when Rust gets specialization this function will likely be deprecated (but still available).
    ///
    /// # Panics
    ///
    /// Panics if there is not enough room in the vector for the elements in `other`;
    /// i.e., `self.room() < other.len()`.
    fn extend_from_slice(&mut self, other: &[T])
    where
        T: Clone,
    {
        self.panic_if_not_enough_room_for(other.len());
        self.data.extend_from_slice(other);
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Option<&T> {
        self.data.get(index)
    }

    #[inline(always)]
    fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        self.data.get_mut(index)
    }

    #[inline(always)]
    unsafe fn get_unchecked(&self, index: usize) -> &T {
        self.data.get_unchecked(index)
    }

    #[inline(always)]
    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
        self.data.get_unchecked_mut(index)
    }

    #[inline(always)]
    fn first(&self) -> Option<&T> {
        self.data.first()
    }

    #[inline(always)]
    fn last(&self) -> Option<&T> {
        self.data.last()
    }

    #[inline(always)]
    unsafe fn first_unchecked(&self) -> &T {
        self.data.get_unchecked(0)
    }

    #[inline(always)]
    unsafe fn last_unchecked(&self) -> &T {
        self.data.get_unchecked(self.data.len() - 1)
    }

    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    #[inline(always)]
    fn len(&self) -> usize {
        self.data.len()
    }

    /// Appends an element to the back of a collection.
    ///
    /// # Panics
    ///
    /// Panics if there is no available room in the vector;
    /// i.e., `self.is_full()` or equivalently `self.len() == self.capacity()`.
    #[inline(always)]
    fn push(&mut self, value: T) {
        self.push_or_panic(value)
    }

    /// Inserts an element at position index within the vector, shifting all elements after it to the right.
    ///
    /// # Panics
    /// Panics if `index >= len`.
    ///
    /// Panics also if there is no available room in the vector;
    /// i.e., `self.is_full()` or equivalently `self.len() == self.capacity()`.
    #[inline(always)]
    fn insert(&mut self, index: usize, element: T) {
        self.panic_if_not_enough_room_for(1);
        self.data.insert(index, element)
    }

    #[inline(always)]
    fn remove(&mut self, index: usize) -> T {
        self.data.remove(index)
    }

    #[inline(always)]
    fn pop(&mut self) -> Option<T> {
        self.data.pop()
    }

    #[inline(always)]
    fn swap(&mut self, a: usize, b: usize) {
        self.data.swap(a, b)
    }

    #[inline(always)]
    fn truncate(&mut self, len: usize) {
        self.data.truncate(len)
    }

    #[inline(always)]
    fn iter(&self) -> Self::Iter<'_> {
        self.data.iter()
    }

    #[inline(always)]
    fn iter_mut(&mut self) -> Self::IterMut<'_> {
        self.data.iter_mut()
    }

    #[inline(always)]
    fn iter_rev(&self) -> Self::IterRev<'_> {
        self.data.iter().rev()
    }

    #[inline(always)]
    fn iter_mut_rev(&mut self) -> Self::IterMutRev<'_> {
        self.data.iter_mut().rev()
    }

    /// Returns the view on the required `range` as an Option of slice:
    ///
    /// * returns None if the range is out of bounds;
    /// * returns Some of the slice when the range is within bounds of the fixed vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_fixed_vec::prelude::*;
    ///
    /// let mut vec = FixedVec::new(10);
    ///
    /// vec.extend_from_slice(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    ///
    /// assert_eq!(vec.as_slice(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    ///
    /// // within bounds
    /// assert_eq!(vec.slices(0..4).unwrap(), &[0, 1, 2, 3]);
    /// assert_eq!(vec.slices(5..7).unwrap(), &[5, 6]);
    /// assert_eq!(vec.slices(8..10).unwrap(), &[8, 9]);
    ///
    /// // OutOfBounds
    /// assert!(vec.slices(5..12).is_none());
    /// assert!(vec.slices(10..11).is_none());
    /// ```
    fn slices<R: RangeBounds<usize>>(&self, range: R) -> Self::SliceIter<'_> {
        let a = range_start(&range);
        let b = range_end(&range, self.len());

        match b.saturating_sub(a) {
            0 => Some(&[]),
            _ => match (a.cmp(&self.len()), b.cmp(&self.len())) {
                (Ordering::Equal | Ordering::Greater, _) => None,
                (_, Ordering::Greater) => None,
                _ => Some(&self.data[a..b]),
            },
        }
    }

    /// Returns a mutable view on the required `range` as an Option of slice:
    ///
    /// * returns None if the range is out of bounds;
    /// * returns Some of the slice when the range is within bounds of the fixed vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_fixed_vec::prelude::*;
    ///
    /// let mut vec = FixedVec::new(10);
    ///
    /// vec.extend_from_slice(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    ///
    /// assert_eq!(vec.as_slice(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    ///
    /// // within bounds
    /// let mut slice = vec.slices_mut(0..4).unwrap();
    /// assert_eq!(slice, &[0, 1, 2, 3]);
    /// slice[1] *= 10;
    /// assert_eq!(vec.as_slice(), &[0, 10, 2, 3, 4, 5, 6, 7, 8, 9]);
    ///
    ///
    /// let mut slice = vec.slices_mut(5..7).unwrap();
    /// assert_eq!(slice, &[5, 6]);
    /// slice[0] *= 10;
    /// assert_eq!(vec.as_slice(), &[0, 10, 2, 3, 4, 50, 6, 7, 8, 9]);
    ///
    /// // OutOfBounds
    /// assert!(vec.slices_mut(5..12).is_none());
    /// assert!(vec.slices_mut(10..11).is_none());
    /// ```
    #[inline(always)]
    fn slices_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Self::SliceMutIter<'_> {
        let a = range_start(&range);
        let b = range_end(&range, self.len());

        match b.saturating_sub(a) {
            0 => Some(&mut []),
            _ => match (a.cmp(&self.len()), b.cmp(&self.len())) {
                (Ordering::Equal | Ordering::Greater, _) => None,
                (_, Ordering::Greater) => None,
                _ => Some(&mut self.data[a..b]),
            },
        }
    }

    #[inline(always)]
    fn get_ptr(&self, index: usize) -> Option<*const T> {
        (index < self.data.capacity()).then(|| unsafe { self.data.as_ptr().add(index) })
    }

    #[inline(always)]
    fn get_ptr_mut(&mut self, index: usize) -> Option<*mut T> {
        (index < self.data.capacity()).then(|| unsafe { self.data.as_mut_ptr().add(index) })
    }

    #[inline(always)]
    unsafe fn set_len(&mut self, new_len: usize) {
        self.data.set_len(new_len)
    }

    fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
    where
        F: FnMut(&T) -> Ordering,
    {
        self.data.binary_search_by(f)
    }

    fn sort(&mut self)
    where
        T: Ord,
    {
        self.data.sort();
    }

    fn sort_by<F>(&mut self, compare: F)
    where
        F: FnMut(&T, &T) -> Ordering,
    {
        self.data.sort_by(compare)
    }

    fn sort_by_key<K, F>(&mut self, f: F)
    where
        F: FnMut(&T) -> K,
        K: Ord,
    {
        self.data.sort_by_key(f)
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use alloc::string::String;
    use alloc::vec::Vec;
    use orx_pinned_vec::*;
    use orx_pseudo_default::PseudoDefault;

    #[test]
    fn pinned_vec_exact_capacity() {
        for cap in [0, 124, 5421] {
            test_pinned_vec(FixedVec::new(cap), cap);
        }
    }

    #[test]
    fn pinned_vec_loose_capacity() {
        for cap in [0, 124, 5421] {
            test_pinned_vec(FixedVec::new(cap * 2), cap);
        }
    }

    #[test]
    fn into_inner() {
        let fixed: FixedVec<_> = (0..16).collect();
        let vec = fixed.into_inner();
        assert_eq!(vec, (0..16).collect::<Vec<_>>())
    }

    #[test]
    fn index_of_and_contains() {
        fn test(mut vec: FixedVec<usize>) {
            let mut another_vec = Vec::new();
            for i in 0..42 {
                vec.push(i);
                another_vec.push(i);
            }
            for i in 0..vec.len() {
                assert_eq!(Some(i), vec.index_of(&vec[i]));
                assert!(vec.contains_reference(&vec[i]));

                assert_eq!(None, vec.index_of(&another_vec[i]));
                assert!(!vec.contains_reference(&another_vec[i]));

                let scalar = another_vec[i];
                assert_eq!(None, vec.index_of(&scalar));
                assert!(!vec.contains_reference(&scalar));
            }
        }
        test(FixedVec::new(42));
        test(FixedVec::new(1000));
    }

    #[test]
    fn len_and_is_empty() {
        fn test_len(mut vec: FixedVec<usize>) {
            for i in 0..42 {
                assert_eq!(i, vec.len());
                vec.push(i);
            }
            assert_eq!(42, vec.len());

            vec.clear();
            assert_eq!(0, vec.len());

            vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
            assert_eq!(42, vec.len());

            for i in 0..42 {
                assert_eq!(42 - i, vec.len());
                vec.pop();
            }
            assert_eq!(0, vec.len());

            vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
            for i in 0..42 {
                assert_eq!(42 - i, vec.len());
                vec.remove(vec.len() / 2);
            }
            assert_eq!(0, vec.len());

            vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
            for i in 0..42 {
                assert_eq!(42 - i, vec.len());
                vec.remove(0);
            }
            assert_eq!(0, vec.len());

            vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
            for i in 0..42 {
                assert_eq!(42 - i, vec.len());
                vec.remove(vec.len() - 1);
            }
            assert_eq!(0, vec.len());

            vec.clear();
            for i in 0..42 {
                assert_eq!(i, vec.len());
                vec.insert(i, i);
            }
            assert_eq!(42, vec.len());

            vec.clear();
            for i in 0..42 {
                assert_eq!(i, vec.len());
                vec.insert(0, i);
            }
            assert_eq!(42, vec.len());
        }

        test_len(FixedVec::new(42));
        test_len(FixedVec::new(1_000));
    }

    #[test]
    fn capacity() {
        let vec: FixedVec<char> = FixedVec::new(44);
        assert_eq!(44, vec.capacity());
        assert_eq!(CapacityState::FixedCapacity(44), vec.capacity_state());
    }

    #[test]
    fn clear() {
        fn clear_is_empty(mut vec: FixedVec<usize>) {
            vec.clear();
            assert!(vec.is_empty());
            assert_eq!(0, vec.len());

            vec.push(1);
            assert!(!vec.is_empty());
            for i in 0..42 {
                vec.push(i);
            }
            assert!(!vec.is_empty());

            vec.clear();
            assert!(vec.is_empty());
            assert_eq!(0, vec.len());
        }
        clear_is_empty(FixedVec::new(43));
        clear_is_empty(FixedVec::new(1000));
    }

    #[test]
    fn get() {
        fn test_get(mut vec: FixedVec<usize>) {
            assert!(vec.is_empty());

            for i in 0..53 {
                vec.push(i);

                assert_eq!(vec.get(i), Some(&i));
                assert_eq!(vec.get(i + 1), None);

                *vec.get_mut(i).expect("is-some") += 100;
                *unsafe { vec.get_unchecked_mut(i) } += 10;
            }

            for i in 0..53 {
                assert_eq!(vec.get(i), Some(&(110 + i)));
                assert_eq!(unsafe { vec.get_unchecked(i) }, &(110 + i));
            }
        }
        test_get(FixedVec::new(53));
        test_get(FixedVec::new(1000));
    }

    #[test]
    fn first_last() {
        let mut vec = FixedVec::new(6);

        assert!(vec.first().is_none());
        assert!(vec.last().is_none());

        vec.push(42);

        assert_eq!(vec.first(), Some(&42));
        assert_eq!(vec.last(), Some(&42));

        unsafe {
            assert_eq!(vec.first_unchecked(), &42);
            assert_eq!(vec.last_unchecked(), &42);
        }

        vec.push(7);

        assert_eq!(vec.first(), Some(&42));
        assert_eq!(vec.last(), Some(&7));

        unsafe {
            assert_eq!(vec.first_unchecked(), &42);
            assert_eq!(vec.last_unchecked(), &7);
        }

        vec.insert(1, 56421);

        assert_eq!(vec.first(), Some(&42));
        assert_eq!(vec.last(), Some(&7));

        unsafe {
            assert_eq!(vec.first_unchecked(), &42);
            assert_eq!(vec.last_unchecked(), &7);
        }

        vec.clear();

        assert!(vec.first().is_none());
        assert!(vec.last().is_none());
    }

    #[test]
    fn extend_from_slice() {
        fn test(mut vec: FixedVec<usize>) {
            vec.extend_from_slice(&(0..42).collect::<Vec<_>>());
            vec.extend_from_slice(&(42..63).collect::<Vec<_>>());
            vec.extend_from_slice(&(63..100).collect::<Vec<_>>());

            assert_eq!(100, vec.len());
            for i in 0..100 {
                assert_eq!(Some(&i), vec.get(i));
            }
        }
        test(FixedVec::new(100));
        test(FixedVec::new(1000));
    }

    #[test]
    fn grow() {
        fn test(mut vec: FixedVec<usize>) {
            for i in 0..42 {
                vec.push(i);
            }
            for i in 0..42 {
                vec.insert(i, 100 + i);
            }

            for i in 0..42 {
                assert_eq!(Some(&i), vec.get(42 + i));
                assert_eq!(Some(&(100 + i)), vec.get(i));
            }
        }
        test(FixedVec::new(84));
        test(FixedVec::new(1000));
    }

    #[test]
    fn shrink() {
        fn test(mut vec: FixedVec<usize>) {
            for i in 0..42 {
                vec.push(i);
                assert_eq!(i, vec.remove(0));
                assert!(vec.is_empty());
            }

            for i in 0..42 {
                vec.push(i);
            }
            for i in 0..42 {
                assert_eq!(i, vec.remove(0));
            }
            assert!(vec.is_empty());

            for i in 0..42 {
                vec.push(i);
            }
            for i in (0..42).rev() {
                assert_eq!(Some(i), vec.pop());
            }
            assert_eq!(None, vec.pop());
            assert!(vec.is_empty());

            for i in 0..42 {
                vec.push(i);
            }
            for _ in 0..42 {
                vec.remove(vec.len() / 2);
            }
            assert!(vec.is_empty());
        }
        test(FixedVec::new(42));
        test(FixedVec::new(1000));
    }

    #[test]
    fn swap() {
        fn test(mut vec: FixedVec<usize>) {
            for i in 0..42 {
                vec.push(i);
            }

            for i in 0..21 {
                vec.swap(i, 21 + i);
            }

            for i in 0..21 {
                assert_eq!(21 + i, vec[i]);
            }
            for i in 21..42 {
                assert_eq!(i - 21, vec[i]);
            }
        }
        test(FixedVec::new(42));
        test(FixedVec::new(1000));
    }
    #[test]
    fn truncate() {
        fn test(mut vec: FixedVec<usize>) {
            for i in 0..42 {
                vec.push(i);
            }

            vec.truncate(100);
            assert_eq!(vec, (0..42).collect::<Vec<_>>());

            vec.truncate(21);
            assert_eq!(vec, (0..21).collect::<Vec<_>>());
        }
        test(FixedVec::new(42));
        test(FixedVec::new(1000));
    }

    #[test]
    fn eq() {
        fn test(mut vec: FixedVec<usize>) {
            let slice = &(0..42).collect::<Vec<usize>>();
            for i in 0..42 {
                vec.push(i);
            }
            assert!(vec.eq(slice));

            vec.remove(7);
            assert!(!vec.eq(slice));
        }

        test(FixedVec::new(142));
        test(FixedVec::new(1000));
    }

    #[test]
    fn clone() {
        fn test(mut vec: FixedVec<usize>) {
            assert!(vec.is_empty());

            for i in 0..53 {
                vec.push(i);
            }

            let clone = vec.clone();
            assert_eq!(vec, clone);
        }

        test(FixedVec::new(53));
        test(FixedVec::new(1000));
    }

    #[test]
    fn slices() {
        #![allow(for_loops_over_fallibles)]
        let mut vec = FixedVec::new(184);

        for i in 0..184 {
            assert!(vec.slices(i..i + 1).is_none());
            assert!(vec.slices(0..i + 1).is_none());
            vec.push(i);
        }

        let slice = vec.slices(0..vec.len());
        let mut combined = Vec::new();
        for s in slice {
            combined.extend_from_slice(s);
        }
        for i in 0..184 {
            assert_eq!(i, vec[i]);
            assert_eq!(i, combined[i]);
        }

        let begin = vec.len() / 4;
        let end = 3 * vec.len() / 4;
        let slice = vec.slices(begin..end);
        let mut combined = Vec::new();
        for s in slice {
            combined.extend_from_slice(s);
        }
        for i in begin..end {
            assert_eq!(i, vec[i]);
            assert_eq!(i, combined[i - begin]);
        }
    }

    #[test]
    fn slices_mut() {
        #![allow(for_loops_over_fallibles)]
        let mut vec = FixedVec::new(184);

        for i in 0..184 {
            assert!(vec.slices_mut(i..i + 1).is_none());
            assert!(vec.slices_mut(0..i + 1).is_none());
            vec.push(i);
        }

        let slice = vec.slices_mut(0..vec.len());
        let mut combined = Vec::new();
        for s in slice {
            combined.extend_from_slice(s);
        }
        for i in 0..184 {
            assert_eq!(i, vec[i]);
            assert_eq!(i, combined[i]);
        }

        let begin = vec.len() / 4;
        let end = 3 * vec.len() / 4;
        let slice = vec.slices_mut(begin..end);
        let mut combined = Vec::new();
        for s in slice {
            combined.extend_from_slice(s);
        }
        for i in begin..end {
            assert_eq!(i, vec[i]);
            assert_eq!(i, combined[i - begin]);
        }

        vec.clear();

        for _ in 0..184 {
            vec.push(0);
        }

        fn update(slice: Option<&mut [usize]>, begin: usize) {
            let mut val = begin;
            for s in slice {
                for x in s {
                    *x = val;
                    val += 1;
                }
            }
        }
        let mut fill = |begin: usize, end: usize| {
            let range = begin..end;
            update(vec.slices_mut(range), begin);
        };

        fill(0, 14);
        fill(14, 56);
        fill(56, 77);
        fill(77, 149);
        fill(149, 182);
        fill(182, 184);
        for i in 0..184 {
            assert_eq!(vec.get(i), Some(&i));
        }
    }

    #[test]
    fn iter_iter_mut() {
        let mut vec = FixedVec::new(4);
        vec.push('a');
        vec.push('b');

        let mut iter = vec.iter();
        assert_eq!(Some(&'a'), iter.next());
        assert_eq!(Some(&'b'), iter.next());
        assert_eq!(None, iter.next());

        for x in vec.iter_mut() {
            *x = 'x';
        }

        let mut iter = vec.iter();
        assert_eq!(Some(&'x'), iter.next());
        assert_eq!(Some(&'x'), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn rev_iter_iter_mut() {
        let mut vec = FixedVec::new(4);
        vec.push('a');
        vec.push('b');

        let mut iter = vec.iter_rev();
        assert_eq!(Some(&'b'), iter.next());
        assert_eq!(Some(&'a'), iter.next());
        assert_eq!(None, iter.next());

        for x in vec.iter_mut_rev() {
            *x = 'x';
        }

        let mut iter = vec.iter_rev();
        assert_eq!(Some(&'x'), iter.next());
        assert_eq!(Some(&'x'), iter.next());
        assert_eq!(None, iter.next());
    }

    #[derive(Debug, PartialEq, Clone)]
    struct Num(usize);
    #[test]
    fn insert() {
        fn test(mut vec: FixedVec<Num>) {
            for i in 0..42 {
                vec.push(Num(i));
            }
            for i in 0..42 {
                vec.insert(i, Num(100 + i));
            }

            for i in 0..42 {
                assert_eq!(Some(&Num(i)), vec.get(42 + i));
                assert_eq!(Some(&Num(100 + i)), vec.get(i));
            }
        }
        test(FixedVec::new(84));
        test(FixedVec::new(1000));
    }

    #[test]
    fn pseudo_default() {
        let vec = FixedVec::<String>::pseudo_default();
        assert_eq!(0, vec.capacity());
        assert_eq!(0, vec.len());
    }
}