Beispiel #1
0
        public void CompareToArrayTest()
        {
            var intArrayRef = new int[1000];
            var intArray    = new NativeMemoryArray <int>(DefaultUnmanagedMemoryAllocator.Instance, 1000);

            var randomGenerator = new System.Random(8675309); // make this deterministic

            for (var idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    intArrayRef[idx] = idx;
                    intArray[idx]    = idx;
                }
                else
                {
                    intArrayRef[idx] = 0;
                    intArray[idx]    = 0;
                }
            }

            for (var idx = 0; idx < 1000; idx++)
            {
                Assert.AreEqual(intArrayRef[idx], intArray[idx]);
            }

            intArray.Dispose();
        }
Beispiel #2
0
        public void RoundTripTest()
        {
            var inputBytes        = new byte[1024 * 1024];
            var inputBytesAsInt32 = MemoryMarshal.Cast <byte, int>(inputBytes);
            var randomGenerator   = new System.Random(8675309); // make this deterministic

            for (int i = 0; i < inputBytesAsInt32.Length; i++)
            {
                inputBytesAsInt32[i] = randomGenerator.Next();
            }

            Span <byte> outputBytes;

            using (var arr1 = new NativeMemoryArray <int>(DefaultUnmanagedMemoryAllocator.Instance, inputBytesAsInt32.Length))
                using (var arr2 = new NativeMemoryArray <int>(DefaultUnmanagedMemoryAllocator.Instance, inputBytesAsInt32.Length))
                {
                    // load the data in from a stream
                    using (var ms = new MemoryStream(inputBytes, false))
                    {
                        arr1.CopyFrom(ms);
                    }

                    // copy the data to another array
                    arr2.CopyFrom(arr1);

                    // save the data out to another stream
                    using (var ms = new MemoryStream(inputBytes.Length))
                    {
                        arr2.CopyTo(ms);
                        outputBytes = ms.ToArray();
                    }
                }

            Assert.True(outputBytes.SequenceEqual(inputBytes));
        }
Beispiel #3
0
        public void ResizeTest()
        {
            var intArrayRef = new int[1000];
            var intArray    = new NativeMemoryArray <int>(DefaultUnmanagedMemoryAllocator.Instance, 1000);

            var randomGenerator = new System.Random(8675309); // make this deterministic

            for (int idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    intArrayRef[idx] = idx;
                    intArray[idx]    = idx;
                }
                else
                {
                    intArrayRef[idx] = 0;
                    intArray[idx]    = 0;
                }
            }

            Array.Resize(ref intArrayRef, 335);
            intArray.Resize(335);

            Assert.AreEqual(intArrayRef.Length, intArray.Length);
            for (int idx = 0; idx < intArrayRef.Length; idx++)
            {
                Assert.AreEqual(intArrayRef[idx], intArray[idx]);
            }

            intArrayRef = new int[1000];
            intArray.Dispose();
            intArray = new NativeMemoryArray <int>(DefaultUnmanagedMemoryAllocator.Instance, 1000);

            for (int idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    intArrayRef[idx] = idx;
                    intArray[idx]    = idx;
                }
                else
                {
                    intArrayRef[idx] = 0;
                    intArray[idx]    = 0;
                }
            }

            Array.Resize(ref intArrayRef, 1235);
            intArray.Resize(1235);

            Assert.AreEqual(intArrayRef.Length, intArray.Length);
            for (int idx = 0; idx < intArrayRef.Length; idx++)
            {
                Assert.AreEqual(intArrayRef[idx], intArray[idx]);
            }

            intArray.Dispose();
        }
Beispiel #4
0
        public void ThrowIfDisposedTest()
        {
            var array = new NativeMemoryArray <int>(DefaultUnmanagedMemoryAllocator.Instance, 1);

            array.Dispose();

            // should be able to dispose twice and do a "resize" to current size
            Assert.DoesNotThrow(() => array.Dispose());
            Assert.DoesNotThrow(() => array.Resize(0));

            // but should not be able to resurrect
            Assert.Throws <ObjectDisposedException>(() => array.Resize(1));
        }