Exemplo n.º 1
0
        public static void ContainsTest()
        {
            var array = new UnmanagedArray <int>(3);

            array[0] = 1;
            array[1] = 2;
            array[2] = 3;

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: true))
            {
                Assert.That(() => valueQueue.Contains(1),
                            Is.True
                            );

                Assert.That(() => valueQueue.Contains(4),
                            Is.False
                            );

                _ = valueQueue.Dequeue();
                valueQueue.Enqueue(4);

                Assert.That(() => valueQueue.Contains(1),
                            Is.False
                            );

                Assert.That(() => valueQueue.Contains(4),
                            Is.True
                            );

                _ = valueQueue.Dequeue();
                valueQueue.Enqueue(5);
                valueQueue.Enqueue(6);

                Assert.That(() => valueQueue.Contains(2),
                            Is.False
                            );

                Assert.That(() => valueQueue.Contains(5),
                            Is.True
                            );

                Assert.That(() => valueQueue.Contains(6),
                            Is.True
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>())
            {
                Assert.That(() => valueQueue.Contains(0),
                            Is.False
                            );
            }
        }
Exemplo n.º 2
0
        public static void PeekTest()
        {
            var array = new UnmanagedArray <int>(3);

            array[0] = 1;
            array[1] = 2;
            array[2] = 3;

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: true))
            {
                Assert.That(() => valueQueue.Peek(),
                            Is.EqualTo(1)
                            );

                valueQueue.Enqueue(4);

                Assert.That(() => valueQueue.Peek(),
                            Is.EqualTo(1)
                            );

                _ = valueQueue.Dequeue();
                valueQueue.Enqueue(5);

                Assert.That(() => valueQueue.Peek(),
                            Is.EqualTo(2)
                            );

                _ = valueQueue.Dequeue();
                valueQueue.Enqueue(6);
                valueQueue.Enqueue(7);

                Assert.That(() => valueQueue.Peek(),
                            Is.EqualTo(3)
                            );

                Assert.That(() => valueQueue,
                            Has.Property("Capacity").EqualTo((nuint)6)
                            .And.Count.EqualTo((nuint)5)
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>())
            {
                Assert.That(() => valueQueue.Peek(),
                            Throws.InvalidOperationException
                            );
            }
        }
Exemplo n.º 3
0
        public static void ClearTest()
        {
            var array = new UnmanagedArray <int>(3);

            array[0] = 1;
            array[1] = 2;
            array[2] = 3;

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: false))
            {
                valueQueue.Clear();

                Assert.That(() => valueQueue,
                            Has.Property("Capacity").EqualTo((nuint)3)
                            .And.Count.EqualTo((nuint)0)
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: false))
            {
                _ = valueQueue.Dequeue();

                valueQueue.Enqueue(4);
                valueQueue.Clear();

                Assert.That(() => valueQueue,
                            Has.Property("Capacity").EqualTo((nuint)3)
                            .And.Count.EqualTo((nuint)0)
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: true))
            {
                _ = valueQueue.Dequeue();

                valueQueue.Enqueue(4);
                valueQueue.Enqueue(5);

                valueQueue.Clear();

                Assert.That(() => valueQueue,
                            Has.Property("Capacity").EqualTo((nuint)6)
                            .And.Count.EqualTo((nuint)0)
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>())
            {
                valueQueue.Clear();

                Assert.That(() => valueQueue,
                            Has.Property("Capacity").EqualTo((nuint)0)
                            .And.Count.EqualTo((nuint)0)
                            );
            }
        }
Exemplo n.º 4
0
        public static void PeekNUIntTest()
        {
            var array = new UnmanagedArray <int>(3);

            array[0] = 1;
            array[1] = 2;
            array[2] = 3;

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: true))
            {
                Assert.That(() => valueQueue.Peek(0),
                            Is.EqualTo(1)
                            );

                valueQueue.Enqueue(4);

                Assert.That(() => valueQueue.Peek(0),
                            Is.EqualTo(1)
                            );

                Assert.That(() => valueQueue.Peek(3),
                            Is.EqualTo(4)
                            );

                _ = valueQueue.Dequeue();
                valueQueue.Enqueue(5);

                Assert.That(() => valueQueue.Peek(0),
                            Is.EqualTo(2)
                            );

                Assert.That(() => valueQueue.Peek(1),
                            Is.EqualTo(3)
                            );

                _ = valueQueue.Dequeue();
                valueQueue.Enqueue(6);
                valueQueue.Enqueue(7);

                Assert.That(() => valueQueue.Peek(0),
                            Is.EqualTo(3)
                            );

                Assert.That(() => valueQueue.Peek(2),
                            Is.EqualTo(5)
                            );

                Assert.That(() => valueQueue,
                            Has.Property("Capacity").EqualTo((nuint)6)
                            .And.Count.EqualTo((nuint)5)
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>())
            {
                Assert.That(() => valueQueue.Peek(0),
                            Throws.InstanceOf <ArgumentOutOfRangeException>()
                            .And.Property("ActualValue").EqualTo((nuint)0)
                            .And.Property("ParamName").EqualTo("index")
                            );
            }
        }
Exemplo n.º 5
0
        public static void CopyToUnmanagedSpanTest()
        {
            var array = new UnmanagedArray <int>(3);

            array[0] = 1;
            array[1] = 2;
            array[2] = 3;

            using (var valueQueue = new UnmanagedValueQueue <int>(array, takeOwnership: true))
            {
                using (var destination = new UnmanagedArray <int>(3))
                {
                    valueQueue.CopyTo(destination);

                    Assert.That(() => destination[0],
                                Is.EqualTo(1)
                                );

                    Assert.That(() => destination[1],
                                Is.EqualTo(2)
                                );

                    Assert.That(() => destination[2],
                                Is.EqualTo(3)
                                );

                    _ = valueQueue.Dequeue();
                    valueQueue.Enqueue(4);

                    valueQueue.CopyTo(destination);

                    Assert.That(() => destination[0],
                                Is.EqualTo(2)
                                );

                    Assert.That(() => destination[1],
                                Is.EqualTo(3)
                                );

                    Assert.That(() => destination[2],
                                Is.EqualTo(4)
                                );
                }

                using (var destination = new UnmanagedArray <int>(6))
                {
                    valueQueue.CopyTo(destination);

                    Assert.That(() => destination[0],
                                Is.EqualTo(2)
                                );

                    Assert.That(() => destination[1],
                                Is.EqualTo(3)
                                );

                    Assert.That(() => destination[2],
                                Is.EqualTo(4)
                                );

                    Assert.That(() => destination[3],
                                Is.EqualTo(0)
                                );

                    Assert.That(() => destination[4],
                                Is.EqualTo(0)
                                );

                    Assert.That(() => destination[5],
                                Is.EqualTo(0)
                                );

                    _ = valueQueue.Dequeue();
                    valueQueue.Enqueue(5);
                    valueQueue.Enqueue(6);

                    valueQueue.CopyTo(destination);

                    Assert.That(() => destination[0],
                                Is.EqualTo(3)
                                );

                    Assert.That(() => destination[1],
                                Is.EqualTo(4)
                                );

                    Assert.That(() => destination[2],
                                Is.EqualTo(5)
                                );

                    Assert.That(() => destination[3],
                                Is.EqualTo(6)
                                );

                    Assert.That(() => destination[4],
                                Is.EqualTo(0)
                                );

                    Assert.That(() => destination[5],
                                Is.EqualTo(0)
                                );
                }

                Assert.That(() => valueQueue.CopyTo(UnmanagedArray <int> .Empty),
                            Throws.InstanceOf <ArgumentOutOfRangeException>()
                            .And.Property("ActualValue").EqualTo((nuint)4)
                            .And.Property("ParamName").EqualTo("Count")
                            );
            }

            using (var valueQueue = new UnmanagedValueQueue <int>())
            {
                Assert.That(() => valueQueue.CopyTo(UnmanagedArray <int> .Empty),
                            Throws.Nothing
                            );
            }
        }