コード例 #1
0
        public static void ClearTest()
        {
            var array = new UnmanagedArray <int>(3);

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

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

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

            using (var valueStack = new UnmanagedValueStack <int>(array, takeOwnership: false))
            {
                _ = valueStack.Pop();

                valueStack.Push(4);
                valueStack.Clear();

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

            using (var valueStack = new UnmanagedValueStack <int>(array, takeOwnership: true))
            {
                _ = valueStack.Pop();

                valueStack.Push(4);
                valueStack.Push(5);

                valueStack.Clear();

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

            using (var valueStack = new UnmanagedValueStack <int>())
            {
                valueStack.Clear();

                Assert.That(() => valueStack,
                            Has.Property("Capacity").EqualTo((nuint)0)
                            .And.Count.EqualTo((nuint)0)
                            );
            }
        }
コード例 #2
0
        public static void ContainsTest()
        {
            var array = new UnmanagedArray <int>(3);

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

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

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

                _ = valueStack.Pop();
                valueStack.Push(4);

                Assert.That(() => valueStack.Contains(3),
                            Is.False
                            );

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

                _ = valueStack.Pop();
                valueStack.Push(5);
                valueStack.Push(6);

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

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

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

            using (var valueStack = new UnmanagedValueStack <int>())
            {
                Assert.That(() => valueStack.Contains(0),
                            Is.False
                            );
            }
        }
コード例 #3
0
        public static void PeekTest()
        {
            var array = new UnmanagedArray <int>(3);

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

            using (var valueStack = new UnmanagedValueStack <int>(array, takeOwnership: true))
            {
                Assert.That(() => valueStack.Peek(),
                            Is.EqualTo(3)
                            );

                valueStack.Push(4);

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

                _ = valueStack.Pop();
                valueStack.Push(5);

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

                _ = valueStack.Pop();
                valueStack.Push(6);
                valueStack.Push(7);

                Assert.That(() => valueStack.Peek(),
                            Is.EqualTo(7)
                            );

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

            using (var valueStack = new UnmanagedValueStack <int>())
            {
                Assert.That(() => valueStack.Peek(),
                            Throws.InvalidOperationException
                            );
            }
        }
コード例 #4
0
        public static void TryPeekNUIntTest()
        {
            var array = new UnmanagedArray <int>(3);

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

            using (var valueStack = new UnmanagedValueStack <int>(array, takeOwnership: true))
            {
                var result = valueStack.TryPeek(0, out var value);

                Assert.That(() => result,
                            Is.True
                            );

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

                valueStack.Push(4);
                result = valueStack.TryPeek(0, out value);

                Assert.That(() => result,
                            Is.True
                            );

                Assert.That(() => value,
                            Is.EqualTo(4)
                            );

                result = valueStack.TryPeek(3, out value);

                Assert.That(() => result,
                            Is.True
                            );

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

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

            using (var valueStack = new UnmanagedValueStack <int>())
            {
                Assert.That(() => valueStack.TryPeek(0, out _),
                            Is.False
                            );
            }
        }
コード例 #5
0
        public static void PeekNUIntTest()
        {
            var array = new UnmanagedArray <int>(3);

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

            using (var valueStack = new UnmanagedValueStack <int>(array, takeOwnership: true))
            {
                Assert.That(() => valueStack.Peek(0),
                            Is.EqualTo(3)
                            );

                valueStack.Push(4);

                Assert.That(() => valueStack.Peek(0),
                            Is.EqualTo(4)
                            );

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

                _ = valueStack.Pop();
                valueStack.Push(5);

                Assert.That(() => valueStack.Peek(0),
                            Is.EqualTo(5)
                            );

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

                _ = valueStack.Pop();
                valueStack.Push(6);
                valueStack.Push(7);

                Assert.That(() => valueStack.Peek(0),
                            Is.EqualTo(7)
                            );

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

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

            using (var valueStack = new UnmanagedValueStack <int>())
            {
                Assert.That(() => valueStack.Peek(0),
                            Throws.InstanceOf <ArgumentOutOfRangeException>()
                            .And.Property("ActualValue").EqualTo((nuint)0)
                            .And.Property("ParamName").EqualTo("index")
                            );
            }
        }
コード例 #6
0
        public static void CopyToUnmanagedSpanTest()
        {
            var array = new UnmanagedArray <int>(3);

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

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

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

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

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

                    _ = valueStack.Pop();
                    valueStack.Push(4);

                    valueStack.CopyTo(destination);

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

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

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

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

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

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

                    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)
                                );

                    _ = valueStack.Pop();
                    valueStack.Push(5);
                    valueStack.Push(6);

                    valueStack.CopyTo(destination);

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

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

                    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(() => valueStack.CopyTo(UnmanagedArray <int> .Empty),
                            Throws.InstanceOf <ArgumentOutOfRangeException>()
                            .And.Property("ActualValue").EqualTo((nuint)4)
                            .And.Property("ParamName").EqualTo("Count")
                            );
            }

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