2008-07-28 19:58:15 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2014-07-19 09:13:36 +00:00
|
|
|
|
Copyright (C) 2002-2014 Nuclex Development Labs
|
2008-07-28 19:58:15 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2009-01-13 18:50:52 +00:00
|
|
|
|
#if UNITTEST
|
|
|
|
|
|
2008-07-28 19:58:15 +00:00
|
|
|
|
using System;
|
2008-07-17 21:19:41 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support {
|
|
|
|
|
|
|
|
|
|
/// <summary>Contains unit tests for the integer helper class</summary>
|
|
|
|
|
[TestFixture]
|
2012-02-29 16:27:43 +00:00
|
|
|
|
internal class IntegerHelperTest {
|
2008-07-17 21:19:41 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that the next power of 2 calculation works for long integers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNextPowerOf2ULong() {
|
|
|
|
|
Assert.AreEqual(1UL, IntegerHelper.NextPowerOf2(0UL));
|
|
|
|
|
Assert.AreEqual(1UL, IntegerHelper.NextPowerOf2(1UL));
|
|
|
|
|
Assert.AreEqual(2UL, IntegerHelper.NextPowerOf2(2UL));
|
|
|
|
|
Assert.AreEqual(4UL, IntegerHelper.NextPowerOf2(3UL));
|
|
|
|
|
Assert.AreEqual(4UL, IntegerHelper.NextPowerOf2(4UL));
|
2008-12-14 18:08:44 +00:00
|
|
|
|
Assert.AreEqual(
|
|
|
|
|
9223372036854775808UL, IntegerHelper.NextPowerOf2(4611686018427387905UL)
|
|
|
|
|
);
|
2008-07-17 21:19:41 +00:00
|
|
|
|
Assert.AreEqual(
|
|
|
|
|
9223372036854775808UL, IntegerHelper.NextPowerOf2(9223372036854775807UL)
|
|
|
|
|
);
|
|
|
|
|
Assert.AreEqual(
|
|
|
|
|
9223372036854775808UL, IntegerHelper.NextPowerOf2(9223372036854775808UL)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that the next power of 2 calculation works for long integers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNextPowerOf2Long() {
|
|
|
|
|
Assert.AreEqual(1L, IntegerHelper.NextPowerOf2(0L));
|
|
|
|
|
Assert.AreEqual(1L, IntegerHelper.NextPowerOf2(1L));
|
|
|
|
|
Assert.AreEqual(2L, IntegerHelper.NextPowerOf2(2L));
|
|
|
|
|
Assert.AreEqual(4L, IntegerHelper.NextPowerOf2(3L));
|
|
|
|
|
Assert.AreEqual(4L, IntegerHelper.NextPowerOf2(4L));
|
2008-12-14 18:08:44 +00:00
|
|
|
|
Assert.AreEqual(4611686018427387904L, IntegerHelper.NextPowerOf2(2305843009213693953L));
|
2008-07-17 21:19:41 +00:00
|
|
|
|
Assert.AreEqual(4611686018427387904L, IntegerHelper.NextPowerOf2(4611686018427387903L));
|
|
|
|
|
Assert.AreEqual(4611686018427387904L, IntegerHelper.NextPowerOf2(4611686018427387904L));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that the next power of 2 calculation works for integers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNextPowerOf2UInt() {
|
|
|
|
|
Assert.AreEqual(1U, IntegerHelper.NextPowerOf2(0U));
|
|
|
|
|
Assert.AreEqual(1U, IntegerHelper.NextPowerOf2(1U));
|
|
|
|
|
Assert.AreEqual(2U, IntegerHelper.NextPowerOf2(2U));
|
|
|
|
|
Assert.AreEqual(4U, IntegerHelper.NextPowerOf2(3U));
|
|
|
|
|
Assert.AreEqual(4U, IntegerHelper.NextPowerOf2(4U));
|
2008-12-14 18:08:44 +00:00
|
|
|
|
Assert.AreEqual(2147483648U, IntegerHelper.NextPowerOf2(1073741825U));
|
2008-07-17 21:19:41 +00:00
|
|
|
|
Assert.AreEqual(2147483648U, IntegerHelper.NextPowerOf2(2147483647U));
|
|
|
|
|
Assert.AreEqual(2147483648U, IntegerHelper.NextPowerOf2(2147483648U));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies that the next power of 2 calculation works for integers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNextPowerOf2Int() {
|
|
|
|
|
Assert.AreEqual(1, IntegerHelper.NextPowerOf2(0));
|
|
|
|
|
Assert.AreEqual(1, IntegerHelper.NextPowerOf2(1));
|
|
|
|
|
Assert.AreEqual(2, IntegerHelper.NextPowerOf2(2));
|
|
|
|
|
Assert.AreEqual(4, IntegerHelper.NextPowerOf2(3));
|
|
|
|
|
Assert.AreEqual(4, IntegerHelper.NextPowerOf2(4));
|
2008-12-14 18:08:44 +00:00
|
|
|
|
Assert.AreEqual(1073741824, IntegerHelper.NextPowerOf2(536870913));
|
2008-07-17 21:19:41 +00:00
|
|
|
|
Assert.AreEqual(1073741824, IntegerHelper.NextPowerOf2(1073741823));
|
|
|
|
|
Assert.AreEqual(1073741824, IntegerHelper.NextPowerOf2(1073741824));
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-22 18:05:55 +00:00
|
|
|
|
/// <summary>Verifies that the bit counting method for integers works</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestCountBitsInInteger() {
|
|
|
|
|
Assert.AreEqual(0, IntegerHelper.CountBits(0));
|
|
|
|
|
Assert.AreEqual(32, IntegerHelper.CountBits(-1));
|
|
|
|
|
Assert.AreEqual(16, IntegerHelper.CountBits(0x55555555));
|
|
|
|
|
Assert.AreEqual(16, IntegerHelper.CountBits(0xAAAAAAAA));
|
|
|
|
|
|
|
|
|
|
for (int bitIndex = 0; bitIndex < 32; ++bitIndex) {
|
|
|
|
|
Assert.AreEqual(1, IntegerHelper.CountBits(1 << bitIndex));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verifies that the bit counting method for long integers works</summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestCountBitsInLongInteger() {
|
|
|
|
|
Assert.AreEqual(0, IntegerHelper.CountBits(0L));
|
|
|
|
|
Assert.AreEqual(64, IntegerHelper.CountBits(-1L));
|
|
|
|
|
Assert.AreEqual(32, IntegerHelper.CountBits(0x5555555555555555));
|
|
|
|
|
Assert.AreEqual(32, IntegerHelper.CountBits(0xAAAAAAAAAAAAAAAA));
|
|
|
|
|
|
|
|
|
|
for (int bitIndex = 0; bitIndex < 64; ++bitIndex) {
|
|
|
|
|
Assert.AreEqual(1, IntegerHelper.CountBits(1 << bitIndex));
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-07-17 21:19:41 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support
|
2009-01-13 18:50:52 +00:00
|
|
|
|
|
2012-03-03 10:57:08 +00:00
|
|
|
|
#endif // UNITTEST
|