achieved 100% test coverage for the read only collection wrapper; optimized the indexer of the license key class; increased test coverage for the license key class to 100%
git-svn-id: file:///srv/devel/repo-conversion/nusu@95 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
c43bfd47c8
commit
de7c28fa84
|
@ -76,6 +76,9 @@
|
|||
<DependentUpon>PriorityQueue.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Collections\ReadOnlyCollection.cs" />
|
||||
<Compile Include="Source\Collections\ReadOnlyCollection.Test.cs">
|
||||
<DependentUpon>ReadOnlyCollection.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Collections\ReadOnlyDictionary.cs" />
|
||||
<Compile Include="Source\Collections\ReadOnlyList.cs" />
|
||||
<Compile Include="Source\Collections\ReverseComparer.cs" />
|
||||
|
|
157
Source/Collections/ReadOnlyCollection.Test.cs
Normal file
157
Source/Collections/ReadOnlyCollection.Test.cs
Normal file
|
@ -0,0 +1,157 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 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;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#if UNITTEST
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Nuclex.Support.Collections {
|
||||
|
||||
/// <summary>Unit Test for the read only collection wrapper</summary>
|
||||
[TestFixture]
|
||||
public class ReadOnlyCollectionTest {
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the copy constructor of the read only collection works
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestCopyConstructor() {
|
||||
int[] integers = new int[] { 12, 34, 56, 78 };
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(integers);
|
||||
|
||||
CollectionAssert.AreEqual(integers, testCollection);
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the IsReadOnly property returns true</summary>
|
||||
[Test]
|
||||
public void TestIsReadOnly() {
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(new int[0]);
|
||||
|
||||
Assert.IsTrue(testCollection.IsReadOnly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the CopyTo() of the read only collection works
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestCopyToArray() {
|
||||
int[] inputIntegers = new int[] { 12, 34, 56, 78 };
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(inputIntegers);
|
||||
|
||||
int[] outputIntegers = new int[testCollection.Count];
|
||||
testCollection.CopyTo(outputIntegers, 0);
|
||||
|
||||
CollectionAssert.AreEqual(inputIntegers, outputIntegers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the Contains() method of the read only collection is able to
|
||||
/// determine if the collection contains an item
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestContains() {
|
||||
int[] integers = new int[] { 1234, 6789 };
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(integers);
|
||||
|
||||
Assert.IsTrue(testCollection.Contains(1234));
|
||||
Assert.IsFalse(testCollection.Contains(4321));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the Add() method of the read only collection throws an exception
|
||||
/// </summary>
|
||||
[Test, ExpectedException(typeof(NotSupportedException))]
|
||||
public void TestThrowOnAdd() {
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(new int[0]);
|
||||
(testCollection as ICollection<int>).Add(123);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the Remove() method of the read only collection throws an exception
|
||||
/// </summary>
|
||||
[Test, ExpectedException(typeof(NotSupportedException))]
|
||||
public void TestThrowOnRemove() {
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(new int[0]);
|
||||
(testCollection as ICollection<int>).Remove(123);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the Clear() method of the read only collection throws an exception
|
||||
/// </summary>
|
||||
[Test, ExpectedException(typeof(NotSupportedException))]
|
||||
public void TestThrowOnClear() {
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(new int[0]);
|
||||
(testCollection as ICollection<int>).Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the typesafe enumerator of the read only collection is working
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestTypesafeEnumerator() {
|
||||
int[] inputIntegers = new int[] { 12, 34, 56, 78 };
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(inputIntegers);
|
||||
|
||||
List<int> outputIntegers = new List<int>();
|
||||
foreach(int value in testCollection) {
|
||||
outputIntegers.Add(value);
|
||||
}
|
||||
|
||||
CollectionAssert.AreEqual(inputIntegers, outputIntegers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the CopyTo() of the read only collection works if invoked via
|
||||
/// the ICollection interface
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestCopyToArrayViaICollection() {
|
||||
int[] inputIntegers = new int[] { 12, 34, 56, 78 };
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(inputIntegers);
|
||||
|
||||
int[] outputIntegers = new int[testCollection.Count];
|
||||
(testCollection as ICollection).CopyTo(outputIntegers, 0);
|
||||
|
||||
CollectionAssert.AreEqual(inputIntegers, outputIntegers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the IsSynchronized property and the SyncRoot property are working
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestSynchronization() {
|
||||
ReadOnlyCollection<int> testCollection = new ReadOnlyCollection<int>(new int[0]);
|
||||
|
||||
if(!(testCollection as ICollection).IsSynchronized) {
|
||||
lock((testCollection as ICollection).SyncRoot) {
|
||||
int count = testCollection.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Collections
|
||||
|
||||
#endif // UNITTEST
|
|
@ -115,17 +115,17 @@ namespace Nuclex.Support.Collections {
|
|||
/// Starting index at which to begin filling the destination array
|
||||
/// </param>
|
||||
void ICollection.CopyTo(Array array, int index) {
|
||||
throw new NotImplementedException();
|
||||
this.objectCollection.CopyTo(array, index);
|
||||
}
|
||||
|
||||
/// <summary>Whether the List is synchronized for multi-threaded usage</summary>
|
||||
bool ICollection.IsSynchronized {
|
||||
get { throw new NotImplementedException(); }
|
||||
get { return this.objectCollection.IsSynchronized; }
|
||||
}
|
||||
|
||||
/// <summary>Synchronization root on which the List locks</summary>
|
||||
object ICollection.SyncRoot {
|
||||
get { throw new NotImplementedException(); }
|
||||
get { return this.objectCollection.SyncRoot; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -31,6 +31,13 @@ namespace Nuclex.Support.Licensing {
|
|||
[TestFixture]
|
||||
public class LicenseKeyTest {
|
||||
|
||||
/// <summary>Tests the default constructor of the license key class</summary>
|
||||
[Test]
|
||||
public void TestDefaultConstructor() {
|
||||
new LicenseKey();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>Validates the correct translation of keys to GUIDs and back</summary>
|
||||
[Test]
|
||||
public void TestGuidKeyConversion() {
|
||||
|
@ -88,6 +95,43 @@ namespace Nuclex.Support.Licensing {
|
|||
|
||||
}
|
||||
|
||||
/// <summary>Tests whether license keys can be modified without destroying them</summary>
|
||||
[Test, ExpectedException(typeof(ArgumentException))]
|
||||
public void TestParseInvalidLicenseKey() {
|
||||
LicenseKey.Parse("hello world");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether an exception is thrown if the indexer of a license key is used
|
||||
/// with an invalid index to retrieve a component of the key
|
||||
/// </summary>
|
||||
[Test, ExpectedException(typeof(IndexOutOfRangeException))]
|
||||
public void TestGetByIndexerWithInvalidIndex() {
|
||||
LicenseKey key = new LicenseKey();
|
||||
int indexMinusOne = key[-1];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether an exception is thrown if the indexer of a license key is used
|
||||
/// with an invalid index to set a component of the key
|
||||
/// </summary>
|
||||
[Test, ExpectedException(typeof(IndexOutOfRangeException))]
|
||||
public void TestSetByIndexerWithInvalidIndex() {
|
||||
LicenseKey key = new LicenseKey();
|
||||
key[-1] = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a license key can be converted into a byte array
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestToByteArray() {
|
||||
Guid someGuid = Guid.NewGuid();
|
||||
LicenseKey someKey = new LicenseKey(someGuid);
|
||||
|
||||
CollectionAssert.AreEqual(someGuid.ToByteArray(), someKey.ToByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Licensing
|
||||
|
|
|
@ -113,12 +113,18 @@ namespace Nuclex.Support.Licensing {
|
|||
if((index < 0) || (index > 3))
|
||||
throw new IndexOutOfRangeException("Index out of range");
|
||||
|
||||
using(MemoryStream guidBytes = new MemoryStream(this.guid.ToByteArray())) {
|
||||
guidBytes.Position = index * 4;
|
||||
new BinaryWriter(guidBytes).Write(value);
|
||||
// Convert the GUID into binary data so we can replace one of its values
|
||||
byte[] guidBytes = this.guid.ToByteArray();
|
||||
|
||||
this.guid = new Guid(guidBytes.ToArray());
|
||||
}
|
||||
// Overwrite the section at the index specified by the user with the new value
|
||||
Array.Copy(
|
||||
BitConverter.GetBytes(value), 0, // source and start index
|
||||
guidBytes, index * 4, // destination and start index
|
||||
4 // length
|
||||
);
|
||||
|
||||
// Replacement finished, now we can reconstruct our guid
|
||||
this.guid = new Guid(guidBytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user