@@ -693,218 +693,3 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
693
693
}
694
694
}
695
695
}
696
-
697
- #[ cfg( test) ]
698
- mod tests {
699
- use prelude:: * ;
700
-
701
- use super :: BinaryHeap ;
702
-
703
- #[ test]
704
- fn test_iterator ( ) {
705
- let data = vec ! [ 5 , 9 , 3 ] ;
706
- let iterout = [ 9 , 5 , 3 ] ;
707
- let heap = BinaryHeap :: from_vec ( data) ;
708
- let mut i = 0 ;
709
- for el in & heap {
710
- assert_eq ! ( * el, iterout[ i] ) ;
711
- i += 1 ;
712
- }
713
- }
714
-
715
- #[ test]
716
- fn test_iterator_reverse ( ) {
717
- let data = vec ! [ 5 , 9 , 3 ] ;
718
- let iterout = vec ! [ 3 , 5 , 9 ] ;
719
- let pq = BinaryHeap :: from_vec ( data) ;
720
-
721
- let v: Vec < _ > = pq. iter ( ) . rev ( ) . cloned ( ) . collect ( ) ;
722
- assert_eq ! ( v, iterout) ;
723
- }
724
-
725
- #[ test]
726
- fn test_move_iter ( ) {
727
- let data = vec ! [ 5 , 9 , 3 ] ;
728
- let iterout = vec ! [ 9 , 5 , 3 ] ;
729
- let pq = BinaryHeap :: from_vec ( data) ;
730
-
731
- let v: Vec < _ > = pq. into_iter ( ) . collect ( ) ;
732
- assert_eq ! ( v, iterout) ;
733
- }
734
-
735
- #[ test]
736
- fn test_move_iter_size_hint ( ) {
737
- let data = vec ! [ 5 , 9 ] ;
738
- let pq = BinaryHeap :: from_vec ( data) ;
739
-
740
- let mut it = pq. into_iter ( ) ;
741
-
742
- assert_eq ! ( it. size_hint( ) , ( 2 , Some ( 2 ) ) ) ;
743
- assert_eq ! ( it. next( ) , Some ( 9 ) ) ;
744
-
745
- assert_eq ! ( it. size_hint( ) , ( 1 , Some ( 1 ) ) ) ;
746
- assert_eq ! ( it. next( ) , Some ( 5 ) ) ;
747
-
748
- assert_eq ! ( it. size_hint( ) , ( 0 , Some ( 0 ) ) ) ;
749
- assert_eq ! ( it. next( ) , None ) ;
750
- }
751
-
752
- #[ test]
753
- fn test_move_iter_reverse ( ) {
754
- let data = vec ! [ 5 , 9 , 3 ] ;
755
- let iterout = vec ! [ 3 , 5 , 9 ] ;
756
- let pq = BinaryHeap :: from_vec ( data) ;
757
-
758
- let v: Vec < _ > = pq. into_iter ( ) . rev ( ) . collect ( ) ;
759
- assert_eq ! ( v, iterout) ;
760
- }
761
-
762
- #[ test]
763
- fn test_peek_and_pop ( ) {
764
- let data = vec ! [ 2 , 4 , 6 , 2 , 1 , 8 , 10 , 3 , 5 , 7 , 0 , 9 , 1 ] ;
765
- let mut sorted = data. clone ( ) ;
766
- sorted. sort ( ) ;
767
- let mut heap = BinaryHeap :: from_vec ( data) ;
768
- while !heap. is_empty ( ) {
769
- assert_eq ! ( heap. peek( ) . unwrap( ) , sorted. last( ) . unwrap( ) ) ;
770
- assert_eq ! ( heap. pop( ) . unwrap( ) , sorted. pop( ) . unwrap( ) ) ;
771
- }
772
- }
773
-
774
- #[ test]
775
- fn test_push ( ) {
776
- let mut heap = BinaryHeap :: from_vec ( vec ! [ 2 , 4 , 9 ] ) ;
777
- assert_eq ! ( heap. len( ) , 3 ) ;
778
- assert ! ( * heap. peek( ) . unwrap( ) == 9 ) ;
779
- heap. push ( 11 ) ;
780
- assert_eq ! ( heap. len( ) , 4 ) ;
781
- assert ! ( * heap. peek( ) . unwrap( ) == 11 ) ;
782
- heap. push ( 5 ) ;
783
- assert_eq ! ( heap. len( ) , 5 ) ;
784
- assert ! ( * heap. peek( ) . unwrap( ) == 11 ) ;
785
- heap. push ( 27 ) ;
786
- assert_eq ! ( heap. len( ) , 6 ) ;
787
- assert ! ( * heap. peek( ) . unwrap( ) == 27 ) ;
788
- heap. push ( 3 ) ;
789
- assert_eq ! ( heap. len( ) , 7 ) ;
790
- assert ! ( * heap. peek( ) . unwrap( ) == 27 ) ;
791
- heap. push ( 103 ) ;
792
- assert_eq ! ( heap. len( ) , 8 ) ;
793
- assert ! ( * heap. peek( ) . unwrap( ) == 103 ) ;
794
- }
795
-
796
- #[ test]
797
- fn test_push_unique ( ) {
798
- let mut heap = BinaryHeap :: < Box < _ > > :: from_vec ( vec ! [ box 2 , box 4 , box 9 ] ) ;
799
- assert_eq ! ( heap. len( ) , 3 ) ;
800
- assert ! ( * heap. peek( ) . unwrap( ) == box 9 ) ;
801
- heap. push ( box 11 ) ;
802
- assert_eq ! ( heap. len( ) , 4 ) ;
803
- assert ! ( * heap. peek( ) . unwrap( ) == box 11 ) ;
804
- heap. push ( box 5 ) ;
805
- assert_eq ! ( heap. len( ) , 5 ) ;
806
- assert ! ( * heap. peek( ) . unwrap( ) == box 11 ) ;
807
- heap. push ( box 27 ) ;
808
- assert_eq ! ( heap. len( ) , 6 ) ;
809
- assert ! ( * heap. peek( ) . unwrap( ) == box 27 ) ;
810
- heap. push ( box 3 ) ;
811
- assert_eq ! ( heap. len( ) , 7 ) ;
812
- assert ! ( * heap. peek( ) . unwrap( ) == box 27 ) ;
813
- heap. push ( box 103 ) ;
814
- assert_eq ! ( heap. len( ) , 8 ) ;
815
- assert ! ( * heap. peek( ) . unwrap( ) == box 103 ) ;
816
- }
817
-
818
- #[ test]
819
- fn test_push_pop ( ) {
820
- let mut heap = BinaryHeap :: from_vec ( vec ! [ 5 , 5 , 2 , 1 , 3 ] ) ;
821
- assert_eq ! ( heap. len( ) , 5 ) ;
822
- assert_eq ! ( heap. push_pop( 6 ) , 6 ) ;
823
- assert_eq ! ( heap. len( ) , 5 ) ;
824
- assert_eq ! ( heap. push_pop( 0 ) , 5 ) ;
825
- assert_eq ! ( heap. len( ) , 5 ) ;
826
- assert_eq ! ( heap. push_pop( 4 ) , 5 ) ;
827
- assert_eq ! ( heap. len( ) , 5 ) ;
828
- assert_eq ! ( heap. push_pop( 1 ) , 4 ) ;
829
- assert_eq ! ( heap. len( ) , 5 ) ;
830
- }
831
-
832
- #[ test]
833
- fn test_replace ( ) {
834
- let mut heap = BinaryHeap :: from_vec ( vec ! [ 5 , 5 , 2 , 1 , 3 ] ) ;
835
- assert_eq ! ( heap. len( ) , 5 ) ;
836
- assert_eq ! ( heap. replace( 6 ) . unwrap( ) , 5 ) ;
837
- assert_eq ! ( heap. len( ) , 5 ) ;
838
- assert_eq ! ( heap. replace( 0 ) . unwrap( ) , 6 ) ;
839
- assert_eq ! ( heap. len( ) , 5 ) ;
840
- assert_eq ! ( heap. replace( 4 ) . unwrap( ) , 5 ) ;
841
- assert_eq ! ( heap. len( ) , 5 ) ;
842
- assert_eq ! ( heap. replace( 1 ) . unwrap( ) , 4 ) ;
843
- assert_eq ! ( heap. len( ) , 5 ) ;
844
- }
845
-
846
- fn check_to_vec ( mut data : Vec < i32 > ) {
847
- let heap = BinaryHeap :: from_vec ( data. clone ( ) ) ;
848
- let mut v = heap. clone ( ) . into_vec ( ) ;
849
- v. sort ( ) ;
850
- data. sort ( ) ;
851
-
852
- assert_eq ! ( v, data) ;
853
- assert_eq ! ( heap. into_sorted_vec( ) , data) ;
854
- }
855
-
856
- #[ test]
857
- fn test_to_vec ( ) {
858
- check_to_vec ( vec ! [ ] ) ;
859
- check_to_vec ( vec ! [ 5 ] ) ;
860
- check_to_vec ( vec ! [ 3 , 2 ] ) ;
861
- check_to_vec ( vec ! [ 2 , 3 ] ) ;
862
- check_to_vec ( vec ! [ 5 , 1 , 2 ] ) ;
863
- check_to_vec ( vec ! [ 1 , 100 , 2 , 3 ] ) ;
864
- check_to_vec ( vec ! [ 1 , 3 , 5 , 7 , 9 , 2 , 4 , 6 , 8 , 0 ] ) ;
865
- check_to_vec ( vec ! [ 2 , 4 , 6 , 2 , 1 , 8 , 10 , 3 , 5 , 7 , 0 , 9 , 1 ] ) ;
866
- check_to_vec ( vec ! [ 9 , 11 , 9 , 9 , 9 , 9 , 11 , 2 , 3 , 4 , 11 , 9 , 0 , 0 , 0 , 0 ] ) ;
867
- check_to_vec ( vec ! [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) ;
868
- check_to_vec ( vec ! [ 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] ) ;
869
- check_to_vec ( vec ! [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 0 , 0 , 1 , 2 ] ) ;
870
- check_to_vec ( vec ! [ 5 , 4 , 3 , 2 , 1 , 5 , 4 , 3 , 2 , 1 , 5 , 4 , 3 , 2 , 1 ] ) ;
871
- }
872
-
873
- #[ test]
874
- fn test_empty_pop ( ) {
875
- let mut heap = BinaryHeap :: < i32 > :: new ( ) ;
876
- assert ! ( heap. pop( ) . is_none( ) ) ;
877
- }
878
-
879
- #[ test]
880
- fn test_empty_peek ( ) {
881
- let empty = BinaryHeap :: < i32 > :: new ( ) ;
882
- assert ! ( empty. peek( ) . is_none( ) ) ;
883
- }
884
-
885
- #[ test]
886
- fn test_empty_replace ( ) {
887
- let mut heap = BinaryHeap :: new ( ) ;
888
- assert ! ( heap. replace( 5 ) . is_none( ) ) ;
889
- }
890
-
891
- #[ test]
892
- fn test_from_iter ( ) {
893
- let xs = vec ! [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ] ;
894
-
895
- let mut q: BinaryHeap < _ > = xs. iter ( ) . rev ( ) . cloned ( ) . collect ( ) ;
896
-
897
- for & x in & xs {
898
- assert_eq ! ( q. pop( ) . unwrap( ) , x) ;
899
- }
900
- }
901
-
902
- #[ test]
903
- fn test_drain ( ) {
904
- let mut q: BinaryHeap < _ > = [ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ] . iter ( ) . cloned ( ) . collect ( ) ;
905
-
906
- assert_eq ! ( q. drain( ) . take( 5 ) . count( ) , 5 ) ;
907
-
908
- assert ! ( q. is_empty( ) ) ;
909
- }
910
- }
0 commit comments