Example #1
0
        public void ReadThrowsIfWriteOnly()
        {
            var mem = new FixedMemory(10, canRead: false, canWrite: true);
            var buf = new byte[5];

            Assert.Throws <InvalidOperationException>(() => mem.Read(0, buf));
        }
Example #2
0
        public void WriteThrowsIfWouldGoOutOfBounds()
        {
            var mem = new FixedMemory(10);
            var buf = new byte[20];

            Assert.Throws <IndexOutOfRangeException>(() => mem.Write(0, buf));
        }
Example #3
0
        public void WriteSpanningTwoMemoriesWithAGapThrowsButDoesWriteEverythingBeforeTheGap()
        {
            var mem  = new VirtualMemory();
            var ram1 = new FixedMemory(10);
            var ram2 = new FixedMemory(10);

            mem.Attach(0, ram1);
            mem.Attach(15, ram2);

            var buf = CreateData(15);

            Assert.Throws <InvalidOperationException>(() => mem.Write(5, buf.Span));

            // Part of our contract is that we WILL write everything we could before the gap!
            Assert.Equal(
                new byte[] { 0, 0, 0, 0, 0, 1, 2, 3, 4, 5 },
                ram1.Data.ToArray()
                );

            // But not after the gap
            Assert.Equal(
                new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                ram2.Data.ToArray()
                );
        }
Example #4
0
        public void WriteThrowsIfOffsetIsNegative()
        {
            var mem = new FixedMemory(10);
            var buf = new byte[5];
            var ex  = Assert.Throws <ArgumentOutOfRangeException>(() => mem.Write(-1, buf));

            Assert.Equal("offset", ex.ParamName);
        }
Example #5
0
        public void InitializesWithAllZeros()
        {
            var mem = new FixedMemory(10);

            var buf = new byte[10];

            mem.Read(0, buf);
            Assert.True(buf.All(b => b == 0));
        }
Example #6
0
        public void WriteThrowsIfReadOnly()
        {
            var mem = new FixedMemory(10, canRead: true, canWrite: false);
            var buf = new byte[5] {
                1, 2, 3, 4, 5
            };

            Assert.Throws <InvalidOperationException>(() => mem.Write(0, buf));
        }
Example #7
0
        public void CanBeInitializedWithData()
        {
            var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var mem  = new FixedMemory(data);

            var buf = new byte[10];

            mem.Read(0, buf);
            Assert.Equal(data, buf);
        }
Example #8
0
        public void WriteUpdatesData()
        {
            var mem = new FixedMemory(10);
            var buf = new byte[5] {
                1, 2, 3, 4, 5
            };

            mem.Write(2, buf.AsSpan());

            Assert.Equal(new byte[] { 0, 0, 1, 2, 3, 4, 5, 0, 0, 0 }, mem.Data.ToArray());
        }
Example #9
0
        public void WriteMirrorsAsManyTimesAsNecessaryToFillWrite()
        {
            var ram = new FixedMemory(5);
            var mem = new MirroredMemory(20, ram);
            var buf = CreateData(15);

            mem.Write(3, buf.Span);

            Assert.Equal(
                new byte[] { 13, 14, 15, 11, 12 },
                ram.Data.ToArray());
        }
Example #10
0
        public void WriteContainedInOneMemory()
        {
            var mem  = new VirtualMemory();
            var ram1 = new FixedMemory(10);

            mem.Attach(0, ram1);

            var buf = CreateData(5);

            mem.Write(1, buf.Span);
            Assert.Equal(new byte[] { 0, 1, 2, 3, 4, 5, 0, 0, 0, 0 }, ram1.Data.ToArray());
        }
Example #11
0
        public void WriteOutsideOfAnyMemoryThrows()
        {
            var mem  = new VirtualMemory();
            var ram1 = new FixedMemory(5);
            var ram2 = new FixedMemory(5);

            mem.Attach(0, ram1);
            mem.Attach(20, ram2);

            var buf = CreateData(5);

            Assert.Throws <InvalidOperationException>(() => mem.Write(10, buf.Span));
        }
Example #12
0
        public void AttachAndDetachUpdateLength()
        {
            var mem  = new VirtualMemory();
            var ram1 = new FixedMemory(10);
            var ram2 = new FixedMemory(5);

            mem.Attach(10, ram1);
            mem.Attach(20, ram2);

            Assert.Equal(25, mem.Length);

            mem.Detach(ram2);

            Assert.Equal(20, mem.Length);
        }
Example #13
0
        public void AttachThrowsIfMemoriesWouldOverlap()
        {
            var mem  = new VirtualMemory();
            var ram1 = new FixedMemory(10);
            var ram2 = new FixedMemory(5);

            mem.Attach(10, ram1);

            // Dangling off the end
            Assert.Throws <InvalidOperationException>(() => mem.Attach(18, ram2));

            // Fully contained within
            Assert.Throws <InvalidOperationException>(() => mem.Attach(11, ram2));

            // Dangling off the start
            Assert.Throws <InvalidOperationException>(() => mem.Attach(9, ram2));
        }
Example #14
0
        public void WriteSpanningTwoAdjacentMemories()
        {
            var mem  = new VirtualMemory();
            var ram1 = new FixedMemory(10);
            var ram2 = new FixedMemory(10);

            mem.Attach(0, ram1);
            mem.Attach(10, ram2);

            var buf = CreateData(10);

            mem.Write(5, buf.Span);
            Assert.Equal(
                new byte[] { 0, 0, 0, 0, 0, 1, 2, 3, 4, 5 },
                ram1.Data.ToArray());
            Assert.Equal(
                new byte[] { 6, 7, 8, 9, 10, 0, 0, 0, 0, 0 },
                ram2.Data.ToArray());
        }