public static void TestHashCodeMultithread(TestedClass testWhat) { Int32 threadCount = 2; Int32 iterationsCount = 1000000; BinaryArray a = new BinaryArray(); MutableString s = new MutableString(); Barrier beforeWrite = new Barrier(threadCount); Barrier beforeRead = new Barrier(threadCount); Action <int> action = (x) => { while (index < iterationsCount) { beforeWrite.SignalAndWait(); if ((previousIndex % threadCount) == x) { index++; expectedHash = index; switch (testWhat) { case TestedClass.BinaryArray: a.Assign(index); break; case TestedClass.MutableString: s.Assign(index); expectedHash = s.Clone().GetHashCode(); break; } a.Assign(index); if ((index % 100000) == 0) { Console.WriteLine("Index = " + index); } } beforeRead.SignalAndWait(); int currentHash = 0; switch (testWhat) { case TestedClass.BinaryArray: currentHash = a.GetHashCode(); break; case TestedClass.MutableString: currentHash = s.GetHashCode(); break; } Assert.AreEqual(currentHash, expectedHash); previousIndex = index; } }; Task[] t = new Task[threadCount]; for (int i = 0; i < threadCount; ++i) { int id = i; t[i] = new Task(() => action(id)); t[i].Start(); } Task.WaitAll(t); }
public void BinaryArraySpanTest() { byte[] byteArray = { 1, 2, 3, 4, 5 }; sbyte[] sbyteArray = { 1, 2, 3, 4, 5 }; char[] charArray = { 'a', 'b', 'c', 'd', 'e' }; Span <byte> testByteSpan = new Span <byte>(byteArray); Span <sbyte> testSByteSpan = new Span <sbyte>(sbyteArray); Span <char> testCharSpan = new Span <char>(charArray); BinaryArray ar = new BinaryArray(); ar.Assign((ReadOnlySpan <byte>)testByteSpan); Assert.AreEqual(5, ar.Count); for (int i = 0; i < ar.Count; ++i) { Assert.AreEqual(i + 1, ar[i]); } ar.Append(testByteSpan); Assert.AreEqual(10, ar.Count); for (int i = 0; i < ar.Count; ++i) { Assert.AreEqual(i % 5 + 1, ar[i]); } ar.Insert(5, testByteSpan); Assert.AreEqual(15, ar.Count); for (int i = 0; i < ar.Count; ++i) { Assert.AreEqual(i % 5 + 1, ar[i]); } ar.Assign(testSByteSpan); Assert.AreEqual(5, ar.Count); for (int i = 0; i < ar.Count; ++i) { Assert.AreEqual(i + 1, ar[i]); } ar.Append(testSByteSpan); Assert.AreEqual(10, ar.Count); for (int i = 0; i < ar.Count; ++i) { Assert.AreEqual(i % 5 + 1, ar[i]); } ar.Insert(5, testSByteSpan); Assert.AreEqual(15, ar.Count); for (int i = 0; i < ar.Count; ++i) { Assert.AreEqual(i % 5 + 1, ar[i]); } ar.Assign(testCharSpan); Assert.AreEqual(10, ar.Count); Assert.AreEqual("abcde", ar.ToString()); ar.Append(testCharSpan); Assert.AreEqual(20, ar.Count); Assert.AreEqual("abcdeabcde", ar.ToString()); ar.Insert(10, testCharSpan); Assert.AreEqual(30, ar.Count); Assert.AreEqual("abcdeabcdeabcde", ar.ToString()); Span <byte> span = new Span <byte>(new byte[30]); ar.ToByteArray(0, span); BinaryArray ar1 = new BinaryArray(span); Assert.AreEqual("abcdeabcdeabcde", ar1.ToString()); Span <byte> span1 = new Span <byte>(new byte[20]); ar1.ToByteArray(10, span1); BinaryArray ar2 = new BinaryArray(span1); Assert.AreEqual("abcdeabcde", ar2.ToString()); }