PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/specs/data/queue.ds

http://github.com/wilkie/djehuty
Unknown | 245 lines | 185 code | 60 blank | 0 comment | 0 complexity | 40ba3ad56e63547236a01798ed33910f MD5 | raw file
  1module specs.data.queue;
  2
  3import data.queue;
  4
  5describe queue() {
  6	describe creation() {
  7		it should_work_as_expected() {
  8			Queue!(int) list = new Queue!(int)();
  9			shouldNot(list is null);
 10			should(list.length == 0);
 11		}
 12	}
 13
 14	describe add() {
 15		it should_add_item_to_the_head() {
 16			Queue!(int) list = new Queue!(int)();
 17			int item = 42;
 18			list.add(item);
 19			
 20			should(list.length == 1);
 21			should(list.peek() == item);
 22		}
 23
 24		it should_an_a_list_to_list() {
 25			Queue!(int) list1 = new Queue!(int)();
 26			Queue!(int) list2 = new Queue!(int)();
 27			int item = 33;
 28
 29			list2.add(item);
 30			list1.add(list2);
 31			
 32			should(list1.length == 1);
 33			should(list1.peek() == item);
 34
 35		}
 36
 37		it should_add_an_array_to_list() {
 38			int[3] arr = 1;
 39			Queue!(int) list = new Queue!(int)();
 40
 41			list.add(arr);
 42
 43			should(list.length == 3);
 44			should(list.peek() == arr[2]);
 45		}
 46	}
 47
 48	describe peek() {
 49		it should_return_the_head() {
 50			Queue!(int) list = new Queue!(int)();
 51			
 52			int item1 = 1;
 53			int item2 = 2;
 54			int item3 = 3;
 55
 56			list.add(item1);
 57			list.add(item2);
 58			list.add(item3);
 59
 60			should(list.peek() == item3);
 61		}
 62
 63		it should_return_the_item_at_index() {
 64			Queue!(int) list = new Queue!(int)();
 65			
 66			int item1 = 1;
 67			int item2 = 2;
 68			int item3 = 3;
 69
 70			list.add(item1);
 71			list.add(item2);
 72			list.add(item3);
 73
 74			should(list.peekAt(0) == item3);
 75			should(list.peekAt(1) == item2);
 76			should(list.peekAt(2) == item1);
 77		}
 78	}
 79
 80	describe remove() {
 81		it should_remove_the_tail() {
 82			Queue!(int) list = new Queue!(int)();
 83			int item = 1;
 84			list.add(item);
 85			
 86			should(list.remove() == item);
 87			should(list.length == 0);
 88		}
 89
 90		it should_remove_by_data() {
 91			Queue!(int) list = new Queue!(int)();
 92			int item = 1;
 93			list.add(item);
 94
 95			should(list.remove(item) == item);
 96			should(list.length == 0);
 97		}
 98
 99		it should_remove_at_index(){
100			Queue!(int) list = new Queue!(int)();
101			
102			int item1 = 1;
103			int item2 = 2;
104			int item3 = 3;
105
106			list.add(item1);
107			list.add(item2);
108			list.add(item3);
109
110			should(list.removeAt(2) == item1);
111			should(list.length == 2);
112			should(list.removeAt(1) == item2);
113			should(list.length == 1);
114			should(list.removeAt(0) == item3);
115			should(list.length == 0);
116		}
117	}
118
119	describe clear() {
120		it should_work_as_expected() {
121			Queue!(int) list = new Queue!(int)();
122
123			list.add(1);
124			list.add(2);
125			list.add(3);
126
127			list.clear();
128
129			should(list.length == 0);
130		}
131	}
132
133	describe empty() {
134		it should_work_as_expected() {
135			Queue!(int) list = new Queue!(int)();
136
137			should(list.empty());
138
139			list.add(1);
140
141			shouldNot(list.empty());
142
143		}
144	}
145
146	describe operations() {
147		it should_peek_at_the_index() {
148			Queue!(int) list = new Queue!(int)();
149			int entry = 1;
150			list.add(entry);
151
152			should(list[0] == entry);
153		}
154	}
155
156	describe array() {
157		it should_return_an_array_of_the_list() {
158			Queue!(int) list = new Queue!(int)();
159			
160			int entry1 = 1;
161			int entry2 = 2;
162			int entry3 = 3;
163
164			list.add(entry1);
165			list.add(entry2);
166			list.add(entry3);
167
168			int[] listArr = list.array();
169
170			shouldNot(listArr is null);
171			should(listArr[0] == entry1);
172			should(listArr[1] == entry2);
173			should(listArr[2] == entry3);
174		}
175	}
176
177	describe duplication() {
178		it should_work_as_expected() {
179			Queue!(int) list = new Queue!(int)();
180			int entry = 1;
181			list.add(entry);
182
183			Queue!(int) dupList = list.dup();
184
185			shouldNot(dupList is null);
186			should(dupList.peek() == entry);
187
188		}
189	}
190
191	describe slicing() {
192		it should_work_as_expected() {
193			Queue!(int) list = new Queue!(int)();
194
195			int entry1 = 1;
196			int entry2 = 2; 
197			int entry3 = 3;
198
199			list.add(entry3);
200			list.add(entry2);
201			list.add(entry1);
202
203			Queue!(int) sliceList = list.slice(0,2);
204
205			shouldNot(sliceList is null);
206			should(sliceList.length == 2);
207			should(sliceList.remove() == entry3);
208			should(sliceList.remove() == entry2);
209		}
210	}
211
212	describe reverse() {
213		it should_work_as_expected {
214			Queue!(int) list = new Queue!(int)();
215
216			int entry1 = 1;
217			int entry2 = 2; 
218			int entry3 = 3;
219
220			list.add(entry1);
221			list.add(entry2);
222			list.add(entry3);
223
224			Queue!(int) revList = list.reverse();
225
226			shouldNot(revList is null);
227			should(revList.length == 3);
228			should(revList.remove() == entry1);
229			should(revList.remove() == entry2);
230			should(revList.remove() == entry3);
231		}
232	}
233	
234	describe string() {
235		it should_work_as_expected {
236			Queue!(int) list = new Queue!(int)();
237			
238			list.add(1);
239			list.add(2);
240			list.add(3);
241
242			should(list.toString() == "[1, 2, 3]");
243		}
244	}
245}