public void TestNormalizeCapacity() { // A few sanity checks Assert.AreEqual(Int32.MaxValue, IoBuffer.NormalizeCapacity(-10)); Assert.AreEqual(0, IoBuffer.NormalizeCapacity(0)); Assert.AreEqual(Int32.MaxValue, IoBuffer.NormalizeCapacity(Int32.MaxValue)); Assert.AreEqual(Int32.MaxValue, IoBuffer.NormalizeCapacity(Int32.MinValue)); Assert.AreEqual(Int32.MaxValue, IoBuffer.NormalizeCapacity(Int32.MaxValue - 10)); // A sanity check test for all the powers of 2 for (int i = 0; i < 30; i++) { int n = 1 << i; Assert.AreEqual(n, IoBuffer.NormalizeCapacity(n)); if (i > 1) { // test that n - 1 will be normalized to n (notice that n = 2^i) Assert.AreEqual(n, IoBuffer.NormalizeCapacity(n - 1)); } // test that n + 1 will be normalized to 2^(i + 1) Assert.AreEqual(n << 1, IoBuffer.NormalizeCapacity(n + 1)); } // The first performance test measures the time to normalize integers // from 0 to 2^27 (it tests 2^27 integers) DateTime time = DateTime.Now; for (int i = 0; i < 1 << 27; i++) { IoBuffer.NormalizeCapacity(i); } DateTime time2 = DateTime.Now; //Console.WriteLine("Time for performance test 1: " + (time2 - time).TotalMilliseconds + "ms"); // The second performance test measures the time to normalize integers // from Int32.MaxValue to Int32.MaxValue - 2^27 (it tests 2^27 // integers) time = DateTime.Now; for (int i = Int32.MaxValue; i > Int32.MaxValue - (1 << 27); i--) { IoBuffer.NormalizeCapacity(i); } time2 = DateTime.Now; //Console.WriteLine("Time for performance test 2: " + (time2 - time).TotalMilliseconds + "ms"); }