#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2017 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Collections {
/// Unit Test for the list segment class
[TestFixture]
internal class ListSegmentTest {
///
/// Tests whether the default constructor of the ListSegment class throws the
/// right exception when being passed 'null' instead of a list
///
[Test]
public void SimpleConstructorThrowsWhenListIsNull() {
Assert.Throws(
delegate() { new ListSegment(null); }
);
}
///
/// Tests whether the simple constructor of the ListSegment class accepts
/// an empty list
///
[Test]
public void SimpleConstructorAcceptsEmptyList() {
new ListSegment(new List());
}
///
/// Tests whether the full constructor of the ListSegment class throws the
/// right exception when being passed 'null' instead of a string
///
[Test]
public void ConstructorThrowsWhenListIsNull() {
Assert.Throws(
delegate() { new ListSegment(null, 0, 0); }
);
}
///
/// Tests whether the full constructor of the ListSegment class accepts
/// an empty string
///
[Test]
public void ConstructorAcceptsEmptyList() {
new ListSegment(new List(), 0, 0);
}
///
/// Tests whether the full constructor of the ListSegment class throws the
/// right exception when being passed an invalid start offset
///
[Test]
public void ConstructorThrowsOnInvalidOffset() {
Assert.Throws(
delegate() { new ListSegment(new List(), -1, 0); }
);
}
///
/// Tests whether the full constructor of the ListSegment class throws the
/// right exception when being passed an invalid element count
///
[Test]
public void ConstructorThrowsOnInvalidCount() {
Assert.Throws(
delegate() { new ListSegment(new List(), 0, -1); }
);
}
///
/// Tests whether the full constructor of the ListSegment class throws the
/// right exception when being passed a string length that's too large
///
[Test]
public void ConstructorThrowsOnListOverrun() {
var testList = new List(capacity: 5) { 1, 2, 3, 4, 5 };
Assert.Throws(
delegate() { new ListSegment(testList, 3, 3); }
);
}
/// Tests whether the 'Text' property works as expected
[Test]
public void ListPropertyStoresOriginalList() {
var testList = new List(capacity: 5) { 1, 2, 3, 4, 5 };
ListSegment testSegment = new ListSegment(testList, 1, 3);
Assert.AreSame(testList, testSegment.List);
}
/// Tests whether the 'Offset' property works as expected
[Test]
public void OffsetPropertyIsStored() {
var testList = new List(capacity: 5) { 1, 2, 3, 4, 5 };
ListSegment testSegment = new ListSegment(testList, 1, 3);
Assert.AreEqual(1, testSegment.Offset);
}
/// Tests whether the 'Count' property works as expected
[Test]
public void CountPropertyIsStored() {
var testList = new List(capacity: 5) { 1, 2, 3, 4, 5 };
ListSegment testSegment = new ListSegment(testList, 1, 3);
Assert.AreEqual(3, testSegment.Count);
}
///
/// Tests whether two differing instances produce different hash codes
///
[Test]
public void DifferentInstancesHaveDifferentHashCodes_Usually() {
var forwardCountSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
var reverseCountSegment = new ListSegment(
new List(capacity: 9) { 9, 8, 7, 6, 5, 4, 3, 2, 1 }, 1, 8
);
Assert.AreNotEqual(
forwardCountSegment.GetHashCode(), reverseCountSegment.GetHashCode()
);
}
///
/// Tests whether two equivalent instances produce an identical hash code
///
[Test]
public void EquivalentInstancesHaveSameHashcode() {
var testSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
var identicalSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
Assert.AreEqual(
testSegment.GetHashCode(), identicalSegment.GetHashCode()
);
}
/// Tests the equals method performing a comparison against null
[Test]
public void EqualsAgainstNullIsAlwaysFalse() {
var testList = new List(capacity: 5) { 1, 2, 3, 4, 5 };
ListSegment testSegment = new ListSegment(testList, 1, 3);
Assert.IsFalse(
testSegment.Equals(null)
);
}
/// Tests the equality operator with differing instances
[Test]
public void DifferingInstancesAreNotEqual() {
var forwardCountSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
var reverseCountSegment = new ListSegment(
new List(capacity: 9) { 9, 8, 7, 6, 5, 4, 3, 2, 1 }, 1, 8
);
Assert.IsFalse(forwardCountSegment == reverseCountSegment);
}
/// Tests the equality operator with equivalent instances
[Test]
public void EquivalentInstancesAreEqual() {
var testSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
var identicalSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
Assert.IsTrue(testSegment == identicalSegment);
}
/// Tests the inequality operator with differing instances
[Test]
public void DifferingInstancesAreUnequal() {
var forwardCountSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
var reverseCountSegment = new ListSegment(
new List(capacity: 9) { 9, 8, 7, 6, 5, 4, 3, 2, 1 }, 1, 8
);
Assert.IsTrue(forwardCountSegment != reverseCountSegment);
}
/// Tests the inequality operator with equivalent instances
[Test]
public void EquivalentInstancesAreNotUnequal() {
var testSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
var identicalSegment = new ListSegment(
new List(capacity: 9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2, 7
);
Assert.IsFalse(testSegment != identicalSegment);
}
/// Tests the ToString() method of the string segment
[Test]
public void TestToString() {
var testList = new List(capacity: 6) { 1, 2, 3, 4, 5, 6 };
ListSegment testSegment = new ListSegment(testList, 2, 2);
string stringRepresentation = testSegment.ToString();
StringAssert.Contains("3, 4", stringRepresentation);
StringAssert.DoesNotContain("2", stringRepresentation);
StringAssert.DoesNotContain("5", stringRepresentation);
}
/// Tests whether the 'Text' property works as expected
[Test]
public void ToListReturnsSubset() {
var testList = new List(capacity: 5) { 1, 2, 3, 4, 5 };
ListSegment testSegment = new ListSegment(testList, 1, 3);
CollectionAssert.AreEqual(
new List(capacity: 3) { 2, 3, 4 },
testSegment.ToList()
);
}
}
} // namespace Nuclex.Support.Collections
#endif // UNITTEST