public void ByteArrayComparer_SortRandomUInt32Test() { var comparer = new ByteArrayComparer(); var r = Randomness.NextRandom(); var list = new List <byte[]>(1000); var list2 = new List <uint>(1000); for (int i = 0; i < 1000; i++) { var bytes = new byte[4]; r.NextBytes(bytes); uint x = ((uint)bytes[0] << 24) | ((uint)bytes[1] << 16) | ((uint)bytes[2] << 8) | bytes[3] ; list.Add(bytes); list2.Add(x); } list.Sort(comparer); list2.Sort(); // use default comparer CollectionAssert.AreEqual(list2, list.Select(bytes => { uint x = ((uint)bytes[0] << 24) | ((uint)bytes[1] << 16) | ((uint)bytes[2] << 8) | bytes[3] ; return(x); }).ToList()); }
public void BitTwiddling_ByteSwapUInt64Test() { var r = Randomness.NextRandom(); var bytes = new byte[8]; for (int i = 0; i < 1000; i++) { r.NextBytes(bytes); var reversed = BitConverter.GetBytes(BitConverter.ToUInt64(bytes, 0).ByteSwap()); CollectionAssert.AreEqual(bytes.Reverse().ToArray(), reversed); } }