Created
January 24, 2016 16:43
-
-
Save airbreather/7128d4dc74a76b098636 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//****************************** | |
// Written by Peter Golde | |
// Copyright (c) 2004-2005, Wintellect | |
// | |
// Use and restribution of this code is subject to the license agreement | |
// contained in the file "License.txt" accompanying this file. | |
//****************************** | |
#region Using directives | |
using System; | |
using System.Collections.Generic; | |
using System.Collections; | |
using NUnit.Framework; | |
#endregion | |
namespace Wintellect.PowerCollections.Tests | |
{ | |
[TestFixture] | |
public class UtilTests | |
{ | |
#pragma warning disable 649 | |
struct StructType { | |
public int i; | |
} | |
class ClassType { | |
public int i; | |
} | |
public struct CloneableStruct : ICloneable | |
{ | |
public int value; | |
public int tweak; | |
public CloneableStruct(int v) | |
{ | |
value = v; | |
tweak = 1; | |
} | |
public object Clone() | |
{ | |
CloneableStruct newStruct; | |
newStruct.value = value; | |
newStruct.tweak = tweak + 1; | |
return newStruct; | |
} | |
public bool Identical(CloneableStruct other) | |
{ | |
return value == other.value && tweak == other.tweak; | |
} | |
public override bool Equals(object other) | |
{ | |
if (! (other is CloneableStruct)) | |
return false; | |
CloneableStruct o = (CloneableStruct)other; | |
return (o.value == value); | |
} | |
public override int GetHashCode() | |
{ | |
return value.GetHashCode(); | |
} | |
} | |
#pragma warning restore 649 | |
[Test] | |
public void IsCloneableType() | |
{ | |
bool isCloneable, isValue; | |
isCloneable = Util.IsCloneableType(typeof(int), out isValue); | |
Assert.IsTrue(isCloneable); Assert.IsTrue(isValue); | |
isCloneable = Util.IsCloneableType(typeof(ICloneable), out isValue); | |
Assert.IsTrue(isCloneable); Assert.IsFalse(isValue); | |
isCloneable = Util.IsCloneableType(typeof(StructType), out isValue); | |
Assert.IsTrue(isCloneable); Assert.IsTrue(isValue); | |
isCloneable = Util.IsCloneableType(typeof(ClassType), out isValue); | |
Assert.IsFalse(isCloneable); Assert.IsFalse(isValue); | |
isCloneable = Util.IsCloneableType(typeof(ArrayList), out isValue); | |
Assert.IsTrue(isCloneable); Assert.IsFalse(isValue); | |
isCloneable = Util.IsCloneableType(typeof(CloneableStruct), out isValue); | |
Assert.IsTrue(isCloneable); Assert.IsFalse(isValue); | |
isCloneable = Util.IsCloneableType(typeof(OrderedDictionary<int, double>), out isValue); | |
Assert.IsTrue(isCloneable); Assert.IsFalse(isValue); | |
} | |
[Test] | |
public void WrapEnumerable() | |
{ | |
IEnumerable<int> enum1 = new List<int>(new int[] { 1, 4, 5, 6, 9, 1 }); | |
IEnumerable<int> enum2 = Util.CreateEnumerableWrapper(enum1); | |
InterfaceTests.TestEnumerableElements(enum2, new int[] { 1, 4, 5, 6, 9, 1 }); | |
} | |
[Test] | |
public void TestGetHashCode() | |
{ | |
int r1, r2, result; | |
result = Util.GetHashCode("foo", EqualityComparer<string>.Default); | |
Assert.AreEqual(result, "foo".GetHashCode()); | |
result = Util.GetHashCode(null, EqualityComparer<string>.Default); | |
Assert.AreEqual(result, 0x1786E23C); | |
r1 = Util.GetHashCode("Banana", StringComparer.InvariantCultureIgnoreCase); | |
r2 = Util.GetHashCode("banANA", StringComparer.InvariantCultureIgnoreCase); | |
Assert.AreEqual(r1, r2); | |
} | |
[Test] | |
public void TestLogBase2() | |
{ | |
TestPair[] expected = | |
{ | |
new TestPair( 0, 0), | |
new TestPair( 1, 0), | |
new TestPair( 2, 1), | |
new TestPair( 3, 1), | |
new TestPair( 4, 2), | |
new TestPair( 5, 2), | |
new TestPair( 6, 2), | |
new TestPair( 7, 2), | |
new TestPair( 8, 3), | |
new TestPair( 9, 3), | |
new TestPair( 15, 3), | |
new TestPair( 16, 4), | |
new TestPair( 17, 4), | |
new TestPair( 31, 4), | |
new TestPair( 32, 5), | |
new TestPair( 33, 5), | |
new TestPair( 63, 5), | |
new TestPair( 64, 6), | |
new TestPair( 65, 6), | |
new TestPair(127, 6), | |
new TestPair(128, 7), | |
new TestPair(129, 7), | |
new TestPair(255, 7), | |
new TestPair(256, 8), | |
new TestPair(257, 8), | |
new TestPair(511, 8), | |
new TestPair(512, 9), | |
new TestPair(513, 9), | |
new TestPair(1u << 10, 10), | |
new TestPair(1u << 11, 11), | |
new TestPair(1u << 12, 12), | |
new TestPair(1u << 13, 13), | |
new TestPair(1u << 14, 14), | |
new TestPair(1u << 15, 15), | |
new TestPair(1u << 16, 16), | |
new TestPair(1u << 17, 17), | |
new TestPair(1u << 18, 18), | |
new TestPair(1u << 19, 19), | |
new TestPair(1u << 20, 20), | |
new TestPair(1u << 21, 21), | |
new TestPair(1u << 22, 22), | |
new TestPair(1u << 23, 23), | |
new TestPair(1u << 24, 24), | |
new TestPair(1u << 25, 25), | |
new TestPair(1u << 26, 26), | |
new TestPair(1u << 27, 27), | |
new TestPair(1u << 28, 28), | |
new TestPair(1u << 29, 29), | |
new TestPair(1u << 30, 30), | |
new TestPair(1u << 31, 31), | |
new TestPair(UInt32.MaxValue, 31), | |
}; | |
TestPair[] actual = Array.ConvertAll(expected, pair => new TestPair(pair.Input, Util.LogBase2(pair.Input))); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
private struct TestPair : IEquatable<TestPair> | |
{ | |
internal uint Input; | |
internal int Output; | |
internal TestPair(uint input, int output) | |
{ | |
this.Input = input; | |
this.Output = output; | |
} | |
public static bool Equals(TestPair first, TestPair second) => first.Input == second.Input && first.Output == second.Output; | |
public static int GetHashCode(TestPair val) => (((17 * 31) + val.Input.GetHashCode()) * 31) + val.Output; | |
public static string ToString(TestPair val) => "Input: " + val.Input + ", Output: " + val.Output; | |
public bool Equals(TestPair other) => Equals(this, other); | |
public override bool Equals(object obj) => obj is TestPair && Equals(this, (TestPair)obj); | |
public override int GetHashCode() => GetHashCode(this); | |
public override string ToString() => ToString(this); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment