Пример #1
0
        public void RingedQueueStressTest()
        {
            RingedQueue <BinaryArray>        queue  = new RingedQueue <BinaryArray>();
            BufferedLinkedList <BinaryArray> etalon = new BufferedLinkedList <BinaryArray>();

            Random rand       = new Random(55);
            int    iterations = 1000000;

            for (int i = 0; i < iterations; ++i)
            {
                int type = rand.Next() % 3;
                if (Math.Abs(type) == 0 && queue.Count > 0)
                {
                    Assert.AreEqual(true, queue.Pop().Equals(etalon.First));
                    etalon.RemoveFirst();
                }
                else
                {
                    BinaryArray array = new BinaryArray();
                    array.Append(i);
                    queue.Add(array);
                    etalon.AddLast(array);
                }

                if (i % 100000 == 0)
                {
                    int index    = 0;
                    int iterator = etalon.FirstKey;
                    Assert.AreEqual(queue.Count, etalon.Count);
                    foreach (BinaryArray element in queue)
                    {
                        Assert.AreEqual(true, element.Equals(etalon[iterator]));
                        Assert.AreEqual(true, element == queue[index]);
                        index++;
                        iterator = etalon.Next(iterator);
                    }
                }
            }
        }
Пример #2
0
        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());
        }