1
1
use orx_linked_list:: * ;
2
2
3
- #[ test]
4
- fn iter_from_singly ( ) {
5
- let mut list = SinglyList :: new ( ) ;
6
- let mut idx42 = None ;
7
- for i in 0 ..324 {
8
- let idx = list. push_front ( i. to_string ( ) ) ;
9
- if i == 42 {
10
- idx42 = Some ( idx)
11
- }
12
- }
13
-
14
- let idx42 = idx42. unwrap ( ) ;
15
-
16
- let vec: Vec < _ > = list. iter ( ) . cloned ( ) . collect ( ) ;
17
- let index_of_42 = vec
3
+ fn test_singly_iter_from (
4
+ list : & SinglyList < String > ,
5
+ vec : & [ String ] ,
6
+ value : usize ,
7
+ idx : NodeIdx < Singly < String > > ,
8
+ ) {
9
+ let position = vec
18
10
. iter ( )
19
11
. enumerate ( )
20
- . find ( |( _, x) | x == & & 42 . to_string ( ) )
12
+ . find ( |( _, x) | x == & & value . to_string ( ) )
21
13
. unwrap ( )
22
14
. 0 ;
23
15
24
- let vec_slice = & vec[ index_of_42 ..] ;
25
- let mut list_slice = list. iter_from ( & idx42 ) ;
16
+ let vec_slice = & vec[ position ..] ;
17
+ let mut list_slice = list. iter_from ( & idx ) ;
26
18
#[ cfg( feature = "validation" ) ]
27
19
list. validate ( ) ;
28
20
@@ -34,37 +26,51 @@ fn iter_from_singly() {
34
26
assert ! ( list_slice. next( ) . is_none( ) ) ;
35
27
}
36
28
37
- #[ test]
38
- fn iter_from_doubly ( ) {
39
- let mut list = DoublyList :: new ( ) ;
40
- let mut idx42 = None ;
41
- for i in 0 ..324 {
42
- let idx = match i % 3 == 0 {
43
- true => list. push_back ( i. to_string ( ) ) ,
44
- false => list. push_front ( i. to_string ( ) ) ,
45
- } ;
29
+ fn test_doubly_iter_from (
30
+ list : & DoublyList < String > ,
31
+ vec : & [ String ] ,
32
+ value : usize ,
33
+ idx : NodeIdx < Doubly < String > > ,
34
+ ) {
35
+ let position = vec
36
+ . iter ( )
37
+ . enumerate ( )
38
+ . find ( |( _, x) | x == & & value. to_string ( ) )
39
+ . unwrap ( )
40
+ . 0 ;
46
41
47
- if i == 42 {
48
- idx42 = Some ( idx)
49
- }
42
+ let vec_slice = & vec[ position..] ;
43
+ let mut list_slice = list. iter_from ( & idx) ;
44
+ #[ cfg( feature = "validation" ) ]
45
+ list. validate ( ) ;
46
+
47
+ for x in vec_slice {
48
+ let value = list_slice. next ( ) . unwrap ( ) ;
49
+ assert_eq ! ( x, value) ;
50
50
}
51
51
52
- let idx42 = idx42. unwrap ( ) ;
52
+ assert ! ( list_slice. next( ) . is_none( ) ) ;
53
+ }
53
54
54
- let vec: Vec < _ > = list. iter ( ) . cloned ( ) . collect ( ) ;
55
- let index_of_42 = vec
55
+ fn test_doubly_iter_backward_from (
56
+ list : & DoublyList < String > ,
57
+ vec : & [ String ] ,
58
+ value : usize ,
59
+ idx : NodeIdx < Doubly < String > > ,
60
+ ) {
61
+ let position = vec
56
62
. iter ( )
57
63
. enumerate ( )
58
- . find ( |( _, x) | x == & & 42 . to_string ( ) )
64
+ . find ( |( _, x) | x == & & value . to_string ( ) )
59
65
. unwrap ( )
60
66
. 0 ;
61
67
62
- let vec_slice = & vec[ index_of_42.. ] ;
63
- let mut list_slice = list. iter_from ( & idx42 ) ;
68
+ let vec_slice = & vec[ 0 ..=position ] ;
69
+ let mut list_slice = list. iter_backward_from ( & idx ) ;
64
70
#[ cfg( feature = "validation" ) ]
65
71
list. validate ( ) ;
66
72
67
- for x in vec_slice {
73
+ for x in vec_slice. iter ( ) . rev ( ) {
68
74
let value = list_slice. next ( ) . unwrap ( ) ;
69
75
assert_eq ! ( x, value) ;
70
76
}
@@ -73,39 +79,72 @@ fn iter_from_doubly() {
73
79
}
74
80
75
81
#[ test]
76
- fn iter_backward_from_doubly ( ) {
82
+ fn iter_from_singly ( ) {
83
+ let mut list = SinglyList :: new ( ) ;
84
+ let [ mut idx_first, mut idx_last, mut idx42] = [ None , None , None ] ;
85
+ for i in 0 ..324 {
86
+ let idx = list. push_front ( i. to_string ( ) ) ;
87
+ match i {
88
+ 0 => idx_first = Some ( idx) ,
89
+ 42 => idx42 = Some ( idx) ,
90
+ 323 => idx_last = Some ( idx) ,
91
+ _ => { }
92
+ } ;
93
+ }
94
+
95
+ let vec: Vec < _ > = list. iter ( ) . cloned ( ) . collect ( ) ;
96
+
97
+ test_singly_iter_from ( & list, & vec, 0 , idx_first. unwrap ( ) ) ;
98
+ test_singly_iter_from ( & list, & vec, 323 , idx_last. unwrap ( ) ) ;
99
+ test_singly_iter_from ( & list, & vec, 42 , idx42. unwrap ( ) ) ;
100
+ }
101
+
102
+ #[ test]
103
+ fn iter_from_doubly ( ) {
77
104
let mut list = DoublyList :: new ( ) ;
78
- let mut idx42 = None ;
105
+ let [ mut idx_first , mut idx_last , mut idx42] = [ None , None , None ] ;
79
106
for i in 0 ..324 {
80
107
let idx = match i % 3 == 0 {
81
108
true => list. push_back ( i. to_string ( ) ) ,
82
109
false => list. push_front ( i. to_string ( ) ) ,
83
110
} ;
84
111
85
- if i == 42 {
86
- idx42 = Some ( idx)
87
- }
112
+ match i {
113
+ 0 => idx_first = Some ( idx) ,
114
+ 42 => idx42 = Some ( idx) ,
115
+ 323 => idx_last = Some ( idx) ,
116
+ _ => { }
117
+ } ;
88
118
}
89
119
90
- let idx42 = idx42. unwrap ( ) ;
91
-
92
120
let vec: Vec < _ > = list. iter ( ) . cloned ( ) . collect ( ) ;
93
- let index_of_42 = vec
94
- . iter ( )
95
- . enumerate ( )
96
- . find ( |( _, x) | x == & & 42 . to_string ( ) )
97
- . unwrap ( )
98
- . 0 ;
99
121
100
- let vec_slice = & vec[ 0 ..=index_of_42 ] ;
101
- let mut list_slice = list . iter_backward_from ( & idx42 ) ;
102
- # [ cfg ( feature = "validation" ) ]
103
- list . validate ( ) ;
122
+ test_doubly_iter_from ( & list , & vec, 0 , idx_first . unwrap ( ) ) ;
123
+ test_doubly_iter_from ( & list , & vec , 323 , idx_last . unwrap ( ) ) ;
124
+ test_doubly_iter_from ( & list , & vec , 42 , idx42 . unwrap ( ) ) ;
125
+ }
104
126
105
- for x in vec_slice. iter ( ) . rev ( ) {
106
- let value = list_slice. next ( ) . unwrap ( ) ;
107
- assert_eq ! ( x, value) ;
127
+ #[ test]
128
+ fn iter_backward_from_doubly ( ) {
129
+ let mut list = DoublyList :: new ( ) ;
130
+ let [ mut idx_first, mut idx_last, mut idx42] = [ None , None , None ] ;
131
+ for i in 0 ..324 {
132
+ let idx = match i % 3 == 0 {
133
+ true => list. push_back ( i. to_string ( ) ) ,
134
+ false => list. push_front ( i. to_string ( ) ) ,
135
+ } ;
136
+
137
+ match i {
138
+ 0 => idx_first = Some ( idx) ,
139
+ 42 => idx42 = Some ( idx) ,
140
+ 323 => idx_last = Some ( idx) ,
141
+ _ => { }
142
+ } ;
108
143
}
109
144
110
- assert ! ( list_slice. next( ) . is_none( ) ) ;
145
+ let vec: Vec < _ > = list. iter ( ) . cloned ( ) . collect ( ) ;
146
+
147
+ test_doubly_iter_backward_from ( & list, & vec, 0 , idx_first. unwrap ( ) ) ;
148
+ test_doubly_iter_backward_from ( & list, & vec, 323 , idx_last. unwrap ( ) ) ;
149
+ test_doubly_iter_backward_from ( & list, & vec, 42 , idx42. unwrap ( ) ) ;
111
150
}
0 commit comments