| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #if NET_2_0
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using NUnit.Framework;
- namespace MonoTests.System.Collections.Generic
- {
- [TestFixture]
- public class LinkedListTest
- {
- LinkedList <int> intlist;
- LinkedList <string> strings;
- [SetUp]
- public void Setup ()
- {
- intlist = new LinkedList <int> ();
-
- // 2 3 4
- intlist.AddLast (3);
- intlist.AddLast (4);
- intlist.AddFirst (2);
- string [] tmpStrings = new string [] { "foo", "bar", "baz" };
- // FIXME workaround for 74953
-
- List <string> workaround = new List <string> ();
- foreach (string s in tmpStrings)
- workaround.Add (s);
- // strings = new LinkedList <string> (tmpStrings);
- strings = new LinkedList <string> (workaround);
- }
- [Test]
- public void AddedTest ()
- {
- int i = 2;
- foreach (int current in intlist)
- {
- Assert.AreEqual (i, current);
- i++;
- }
- Assert.AreEqual (5, i);
- }
- [Test]
- public void CreatedTest ()
- {
- string [] values = new string [] { "foo", "bar", "baz" };
- int i = 0;
- foreach (string current in strings)
- {
- Assert.AreEqual (values [i], current);
- i++;
- }
- Assert.AreEqual (3, i);
- }
- [Test]
- public void NonCircularNodeTest ()
- {
- LinkedListNode <int> node = intlist.First;
- Assert.AreEqual (2, node.Value);
- LinkedListNode <int> previous = node.Previous;
- Assert.IsNull (previous);
- node = node.Next;
- Assert.IsNotNull (node);
- Assert.AreEqual (3, node.Value);
- node = node.Next;
- Assert.IsNotNull (node);
- Assert.AreEqual (4, node.Value);
- node = node.Next;
- Assert.IsNull (node);
- }
- [Test]
- public void ClearTest ()
- {
- intlist.Clear ();
- Assert.AreEqual (0, intlist.Count);
- }
- [Test]
- public void ContainsTest ()
- {
- Assert.IsTrue (intlist.Contains (3));
- Assert.IsFalse (intlist.Contains (5));
- }
- [Test]
- public void AddBeforeAndAfterTest ()
- {
- LinkedListNode <int> node = intlist.Find (3);
- intlist.AddAfter (node, new LinkedListNode <int> (5));
- LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
- LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
- intlist.AddBefore (node, new LinkedListNode <int> (8));
- Assert.AreEqual (6, sixNode.Value);
- Assert.AreEqual (7, sevenNode.Value);
- // 2 7 8 3 6 5 4
- int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
- int i = 0;
- foreach (int current in intlist)
- {
- Assert.AreEqual (values [i], current);
- i++;
- }
- for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
- Assert.AreSame (intlist, current.List);
- }
- [Test]
- public void CopyToTest ()
- {
- int [] values = new int [] { 2, 3, 4 };
- int [] output = new int [3];
- intlist.CopyTo (output, 0);
- for (int i = 0; i < 3; i++)
- Assert.AreEqual (values [i], output [i]);
- }
- [Test]
- public void FindTest ()
- {
- intlist.AddFirst (4);
- LinkedListNode <int> head, tail;
- head = intlist.Find (4);
- tail = intlist.FindLast (4);
- Assert.AreEqual (intlist.First, head);
- Assert.AreEqual (intlist.Last, tail);
- }
- [Test]
- public void RemoveTest ()
- {
- Assert.IsTrue (intlist.Remove (3));
- Assert.AreEqual (2, intlist.Count);
- int [] values = { 2, 4 };
- int i = 0;
- foreach (int current in intlist)
- {
- Assert.AreEqual (values [i], current);
- i++;
- }
- Assert.IsFalse (intlist.Remove (5));
- LinkedListNode <string> node = strings.Find ("baz");
- strings.Remove (node);
- Assert.IsNull (node.List);
- Assert.IsNull (node.Previous);
- Assert.IsNull (node.Next);
- string [] values2 = { "foo", "bar" };
- i = 0;
- foreach (string current in strings)
- {
- Assert.AreEqual (values2 [i], current);
- i++;
- }
- }
- [Test, ExpectedException (typeof (ArgumentNullException))]
- public void RemoveNullNodeTest ()
- {
- intlist.Remove (null);
- }
- [Test, ExpectedException (typeof (InvalidOperationException))]
- public void RemoveInvalidNodeTest ()
- {
- intlist.Remove (new LinkedListNode <int> (4));
- }
- [Test]
- public void RemoveFirstLastTest ()
- {
- strings.RemoveFirst ();
- strings.RemoveLast ();
- Assert.AreEqual (1, strings.Count);
- Assert.AreEqual ("bar", strings.First.Value);
- }
- [Test]
- public void ListSerializationTest ()
- {
- BinaryFormatter formatter = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- formatter.Serialize(stream, intlist);
- stream.Position = 0;
- object deserialized = formatter.Deserialize(stream);
- Assert.IsTrue(deserialized is LinkedList <int>);
- LinkedList <int> dlist = deserialized as LinkedList <int>;
- int [] values = { 2, 3, 4 };
- int i = 0;
- foreach (int value in dlist)
- {
- Assert.AreEqual (values [i], value);
- i++;
- }
- Assert.AreEqual(3, i);
- }
- /* FIXME: disabled pending fix for #75299
- [Test]
- */
- public void EnumeratorSerializationTest ()
- {
- BinaryFormatter formatter = new BinaryFormatter ();
- MemoryStream stream = new MemoryStream ();
- LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
- formatter.Serialize (stream, e);
- stream.Position = 0;
- object deserialized = formatter.Deserialize(stream);
- Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
- LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
- int [] values = { 2, 3, 4 };
- int i = 0;
- while (d.MoveNext ())
- {
- Assert.AreEqual (values [i], d.Current);
- i++;
- }
- Assert.AreEqual(3, i);
- }
- }
- }
- #endif
|