@@ -929,53 +929,75 @@ fn test_append() {
929
929
}
930
930
931
931
#[ test]
932
- fn test_append_advanced ( ) {
933
- fn check (
934
- a_push_back : usize ,
935
- a_pop_back : usize ,
936
- b_push_back : usize ,
937
- b_pop_back : usize ,
938
- a_push_front : usize ,
939
- a_pop_front : usize ,
940
- b_push_front : usize ,
941
- b_pop_front : usize
942
- ) {
943
- let mut taken = 0 ;
944
- let mut a = VecDeque :: new ( ) ;
945
- let mut b = VecDeque :: new ( ) ;
946
- for n in ( taken..) . take ( a_push_back) {
947
- a. push_back ( n) ;
932
+ fn test_append_permutations ( ) {
933
+ fn construct_vec_deque (
934
+ push_back : usize ,
935
+ pop_back : usize ,
936
+ push_front : usize ,
937
+ pop_front : usize ,
938
+ ) -> VecDeque < usize > {
939
+ let mut out = VecDeque :: new ( ) ;
940
+ for a in 0 ..push_back {
941
+ out. push_back ( a) ;
948
942
}
949
- taken += a_push_back;
950
- for n in ( taken..) . take ( a_push_front) {
951
- a. push_front ( n) ;
943
+ for b in 0 ..push_front {
944
+ out. push_front ( push_back + b) ;
952
945
}
953
- taken += a_push_front;
954
- for n in ( taken..) . take ( b_push_back) {
955
- b. push_back ( n) ;
946
+ for _ in 0 ..pop_back {
947
+ out. pop_back ( ) ;
956
948
}
957
- taken += b_push_back;
958
- for n in ( taken..) . take ( b_push_front) {
959
- b. push_front ( n) ;
949
+ for _ in 0 ..pop_front {
950
+ out. pop_front ( ) ;
960
951
}
961
-
962
- a. drain ( ..a_pop_back) ;
963
- a. drain ( a_pop_front..) ;
964
- b. drain ( ..b_pop_back) ;
965
- b. drain ( b_pop_front..) ;
966
- let checked = a. iter ( ) . chain ( b. iter ( ) ) . map ( |& x| x) . collect :: < Vec < usize > > ( ) ;
967
- a. append ( & mut b) ;
968
- assert_eq ! ( a, checked) ;
969
- assert ! ( b. is_empty( ) ) ;
952
+ out
970
953
}
971
- for a_push in 0 ..17 {
972
- for a_pop in 0 ..a_push {
973
- for b_push in 0 ..17 {
974
- for b_pop in 0 ..b_push {
975
- check ( a_push, a_pop, b_push, b_pop, 0 , 0 , 0 , 0 ) ;
976
- check ( a_push, a_pop, b_push, b_pop, a_push, 0 , 0 , 0 ) ;
977
- check ( a_push, a_pop, b_push, b_pop, 0 , 0 , b_push, 0 ) ;
978
- check ( 0 , 0 , 0 , 0 , a_push, a_pop, b_push, b_pop) ;
954
+
955
+ const MAX : usize = 5 ;
956
+
957
+ // Many different permutations of both the `VecDeque` getting appended to
958
+ // and the one getting appended are generated to check `append`.
959
+ // This ensures all 6 code paths of `append` are tested.
960
+ for src_push_back in 0 ..MAX {
961
+ for src_push_front in 0 ..MAX {
962
+ // doesn't pop more values than are pushed
963
+ for src_pop_back in 0 ..( src_push_back + src_push_front) {
964
+ for src_pop_front in 0 ..( src_push_back + src_push_front - src_pop_back) {
965
+
966
+ let src = construct_vec_deque (
967
+ src_push_back,
968
+ src_pop_back,
969
+ src_push_front,
970
+ src_pop_front,
971
+ ) ;
972
+
973
+ for dst_push_back in 0 ..MAX {
974
+ for dst_push_front in 0 ..MAX {
975
+ for dst_pop_back in 0 ..( dst_push_back + dst_push_front) {
976
+ for dst_pop_front
977
+ in 0 ..( dst_push_back + dst_push_front - dst_pop_back)
978
+ {
979
+ let mut dst = construct_vec_deque (
980
+ dst_push_back,
981
+ dst_pop_back,
982
+ dst_push_front,
983
+ dst_pop_front,
984
+ ) ;
985
+ let mut src = src. clone ( ) ;
986
+
987
+ // Assert that appending `src` to `dst` gives the same order
988
+ // of values as iterating over both in sequence.
989
+ let correct = dst
990
+ . iter ( )
991
+ . chain ( src. iter ( ) )
992
+ . cloned ( )
993
+ . collect :: < Vec < usize > > ( ) ;
994
+ dst. append ( & mut src) ;
995
+ assert_eq ! ( dst, correct) ;
996
+ assert ! ( src. is_empty( ) ) ;
997
+ }
998
+ }
999
+ }
1000
+ }
979
1001
}
980
1002
}
981
1003
}
0 commit comments