#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Text;
using NUnit.Framework;
using Ingenious.Mvc;
namespace Ingenious.Mvc.UnitTest.Ids{
/// <summary>
/// Unit tests the <see cref="IdCollection"/> type.
/// </summary>
public sealed class IdCollectionTest : UnitTestBase
{
private IdCollection<int> _ids;
private bool _added;
private bool _removed;
protected override void SetUp()
{
base.SetUp();
_ids = new IdCollection<int>();
_added = false;
_removed = false;
_ids.IdAdded += delegate
{
_added = true;
};
_ids.IdRemoved += delegate
{
_removed = true;
};
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestIndexerInvalidIndex()
{
_ids[1] = int.MinValue;
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestIndexerNegativeIndex()
{
_ids[-1] = int.MinValue;
}
[Test]
public void TestIndexer()
{
int id = int.MaxValue;
Assert.AreEqual(0, _ids.Count);
_ids.Add(id);
Assert.IsTrue(_added);
Assert.AreEqual(1, _ids.Count);
Assert.AreEqual(id, _ids[0]);
Assert.IsFalse(_removed);
_ids[0] = int.MinValue;
Assert.IsTrue(_removed);
Assert.AreEqual(int.MinValue, _ids[0]);
}
[Test]
public void TestCount()
{
Assert.AreEqual(0, _ids.Count);
for (int i = 0; i < 100; ++i)
{
_ids.Add(i);
Assert.AreEqual(i + 1, _ids.Count);
}
}
[Test]
public void TestReadOnly()
{
Assert.IsFalse(_ids.IsReadOnly);
ICollection<int> ids = _ids.AsReadOnly();
Assert.IsTrue(ids.IsReadOnly);
}
[Test]
public void TestAdd()
{
Assert.AreEqual(0, _ids.Count);
for (int i = 0; i < 100; ++i)
{
_ids.Add(i);
}
Assert.AreEqual(100, _ids.Count);
Assert.IsTrue(_added);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestAddRangeNull()
{
_ids.AddRange(null);
}
[Test]
public void TestAddRange()
{
List<int> ids = new List<int>();
for (int i = 0; i < 100; ++i)
{
ids.Add(i);
}
_ids.AddRange(ids);
Assert.AreEqual(100, _ids.Count);
}
[Test]
public void TestClear()
{
Assert.AreEqual(0, _ids.Count);
_ids.Clear();
Assert.AreEqual(0, _ids.Count);
for (int i = 0; i < 100; ++i)
{
_ids.Add(i);
}
Assert.AreEqual(100, _ids.Count);
_ids.Clear();
Assert.AreEqual(0, _ids.Count);
}
[Test]
public void TestContains()
{
Assert.IsFalse(_ids.Contains(int.MinValue));
_ids.Add(int.MinValue);
Assert.IsTrue(_ids.Contains(int.MinValue));
Assert.IsFalse(_ids.Contains(int.MaxValue));
_ids.Add(int.MaxValue);
Assert.IsTrue(_ids.Contains(int.MaxValue));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestCopyToArrayNull()
{
_ids.CopyTo(null, 0);
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException), "Number was less than the array's lower bound in the first dimension.\r\nParameter name: dstIndex")]
public void TestCopyToIndexNegative()
{
_ids.CopyTo(new int[2], -1);
}
[Test]
[ExpectedException(typeof(ArgumentException), "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.")]
public void TestCopyToArrayTooSmall()
{
for (int i = 0; i < 100; ++i)
{
_ids.Add(i);
}
int[] ids = new int[0];
_ids.CopyTo(ids, 0);
}
[Test]
public void TestCopyTo()
{
for (int i = 0; i < 100; ++i)
{
_ids.Add(i);
}
int[] ids = new int[100];
_ids.CopyTo(ids, 0);
for (int i = 0; i < ids.Length; ++i)
{
Assert.AreEqual(i, ids[i]);
}
}
[Test]
public void TestIndexOf()
{
for (int i = 0; i < 100; ++i)
{
_ids.Add(1000 - i);
}
for (int i = 0; i < _ids.Count; ++i)
{
Assert.AreEqual(i, _ids.IndexOf(1000 - i));
}
Assert.AreEqual(-1, _ids.IndexOf(int.MinValue));
Assert.AreEqual(-1, _ids.IndexOf(int.MaxValue));
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException), "Index must be within the bounds of the List.\r\nParameter name: index")]
public void TestInsertInvalidIndex()
{
_ids.Insert(-1, 2);
}
[Test]
public void TestInsert()
{
_ids.Insert(0, int.MinValue);
Assert.AreEqual(int.MinValue, _ids[0]);
Assert.IsTrue(_added);
_ids.Insert(0, int.MaxValue);
Assert.AreEqual(int.MaxValue, _ids[0]);
Assert.AreEqual(int.MinValue, _ids[1]);
Assert.IsFalse(_removed);
}
[Test]
public void TestRemove()
{
Assert.IsFalse(_ids.Remove(int.MinValue));
Assert.IsFalse(_ids.Remove(int.MaxValue));
Assert.IsFalse(_removed);
_ids.Add(int.MinValue);
Assert.IsTrue(_ids.Remove(int.MinValue));
Assert.IsFalse(_ids.Remove(int.MaxValue));
Assert.IsTrue(_removed);
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException), "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index")]
public void TestRemoveAtInvalidIndex()
{
_ids.RemoveAt(-1);
}
[Test]
public void TestRemoveAt()
{
_ids.Add(int.MinValue);
_ids.Add(int.MaxValue);
Assert.IsFalse(_removed);
_ids.RemoveAt(0);
Assert.IsTrue(_removed);
Assert.AreEqual(1, _ids.Count);
Assert.AreEqual(int.MaxValue, _ids[0]);
}
[Test]
public void TestEnumeratorGeneric()
{
for (int i = 0; i < 100; ++i)
{
_ids.Add(i + 1);
}
foreach (int id in _ids)
{
Assert.IsTrue(id > 0);
Assert.IsTrue(id < 101);
}
}
[Test]
public void TestEnumerator()
{
for (int i = 0; i < 100; ++i)
{
_ids.Add(i + 1);
}
System.Collections.IEnumerator enumerator = (_ids as System.Collections.IEnumerable).GetEnumerator();
while (enumerator.MoveNext())
{
Assert.IsTrue((int) enumerator.Current > 0);
Assert.IsTrue((int) enumerator.Current < 101);
}
}
}
}
|