1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Core
.Tests
19 using NUnit
.Framework
;
21 using Castle
.Core
.Internal
;
24 public class LinkedListTestCase
27 public void Enumerable()
29 LinkedList list
= new LinkedList();
31 list
.AddFirst( "third" );
32 list
.AddFirst( "second" );
33 list
.AddFirst( "first" );
37 foreach(String
value in list
)
42 Assert
.AreEqual( "first", value );
45 Assert
.AreEqual( "second", value );
48 Assert
.AreEqual( "third", value );
55 public void AddFirst()
57 LinkedList list
= new LinkedList();
59 list
.AddFirst( "third" );
60 Assert
.AreEqual( "third", list
.Head
);
61 Assert
.AreEqual( 1, list
.Count
);
63 list
.AddFirst( "second" );
64 Assert
.AreEqual( "second", list
.Head
);
65 Assert
.AreEqual( 2, list
.Count
);
67 list
.AddFirst( "first" );
68 Assert
.AreEqual( "first", list
.Head
);
69 Assert
.AreEqual( 3, list
.Count
);
75 LinkedList list
= new LinkedList();
78 Assert
.AreEqual( "1", list
.Head
);
79 Assert
.AreEqual( 1, list
.Count
);
82 Assert
.AreEqual( "1", list
.Head
);
83 Assert
.AreEqual( 2, list
.Count
);
86 Assert
.AreEqual( "1", list
.Head
);
87 Assert
.AreEqual( 3, list
.Count
);
91 public void RemoveBoundary1()
93 LinkedList list
= new LinkedList();
103 Assert
.AreEqual( "2", list
.Head
);
104 Assert
.AreEqual( 2, list
.Count
);
106 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
107 Assert
.AreEqual( "2,3", String
.Join(",", array
) );
111 public void RemoveBoundary2()
113 LinkedList list
= new LinkedList();
123 Assert
.AreEqual( "1", list
.Head
);
124 Assert
.AreEqual( 2, list
.Count
);
126 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
127 Assert
.AreEqual( "1,2", String
.Join(",", array
) );
131 public void RemoveBoundary3()
133 LinkedList list
= new LinkedList();
140 Assert
.AreEqual( "1", list
.Head
);
141 Assert
.AreEqual( 1, list
.Count
);
143 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
144 Assert
.AreEqual( "1", String
.Join(",", array
) );
148 public void RemoveMiddle1()
150 LinkedList list
= new LinkedList();
160 Assert
.AreEqual( "1", list
.Head
);
161 Assert
.AreEqual( 2, list
.Count
);
163 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
164 Assert
.AreEqual( "1,3", String
.Join(",", array
) );
168 public void RemoveMiddle2()
170 LinkedList list
= new LinkedList();
180 Assert
.AreEqual( "1", list
.Head
);
181 Assert
.AreEqual( 4, list
.Count
);
183 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
184 Assert
.AreEqual( "1,2,4,5", String
.Join(",", array
) );
188 public void IndexOf1()
190 LinkedList list
= new LinkedList();
198 Assert
.AreEqual( 0, list
.IndexOf("1") );
199 Assert
.AreEqual( -1, list
.IndexOf("10") );
203 public void IndexOf2()
205 LinkedList list
= new LinkedList();
213 Assert
.AreEqual( 4, list
.IndexOf("5") );
214 Assert
.AreEqual( -1, list
.IndexOf("10") );
218 public void Insert0()
220 LinkedList list
= new LinkedList();
227 Assert
.AreEqual( 4, list
.Count
);
229 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
230 Assert
.AreEqual( "x,1,2,3", String
.Join(",", array
) );
234 public void Insert1()
236 LinkedList list
= new LinkedList();
243 Assert
.AreEqual( 4, list
.Count
);
245 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
246 Assert
.AreEqual( "1,x,2,3", String
.Join(",", array
) );
250 public void Insert2()
252 LinkedList list
= new LinkedList();
259 Assert
.AreEqual( 4, list
.Count
);
261 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
262 Assert
.AreEqual( "1,2,x,3", String
.Join(",", array
) );
267 public void Insert2bis()
269 LinkedList list
= new LinkedList();
279 Assert
.AreEqual( 7, list
.Count
);
281 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
282 Assert
.AreEqual( "0,1,x,2,3,4,5", String
.Join(",", array
) );
287 public void Replace1()
289 LinkedList list
= new LinkedList();
293 Assert
.IsTrue( list
.Replace("0", "x") );
295 Assert
.AreEqual( 2, list
.Count
);
297 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
298 Assert
.AreEqual( "x,1", String
.Join(",", array
) );
302 public void Replace2()
304 LinkedList list
= new LinkedList();
308 Assert
.IsTrue( list
.Replace("1", "x") );
310 Assert
.AreEqual( 2, list
.Count
);
312 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
313 Assert
.AreEqual( "0,x", String
.Join(",", array
) );
317 public void Replace3()
319 LinkedList list
= new LinkedList();
323 Assert
.IsFalse( list
.Replace("11", "x") );
325 Assert
.AreEqual( 2, list
.Count
);
327 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
328 Assert
.AreEqual( "0,1", String
.Join(",", array
) );
332 public void Replace4()
334 LinkedList list
= new LinkedList();
340 Assert
.IsTrue( list
.Replace("2", "x") );
342 Assert
.AreEqual( 4, list
.Count
);
344 String
[] array
= (String
[]) list
.ToArray( typeof(String
) );
345 Assert
.AreEqual( "0,1,x,3", String
.Join(",", array
) );
349 public void ToArray()
351 LinkedList list
= new LinkedList();
353 list
.AddFirst( "third" );
354 list
.AddFirst( "second" );
355 list
.AddFirst( "first" );
357 String
[] values
= (String
[]) list
.ToArray( typeof(String
) );
358 Assert
.AreEqual( "first", values
[0] );
359 Assert
.AreEqual( "second", values
[1] );
360 Assert
.AreEqual( "third", values
[2] );