Beispiel #1
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()
                );
        }
Beispiel #2
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));
        }
Beispiel #3
0
        public void ReadSpanningTwoAdjacentMemories()
        {
            var mem  = new VirtualMemory();
            var ram1 = CreateFixedMemory(10);
            var ram2 = CreateFixedMemory(10);

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

            var buf = new byte[5];

            mem.Read(8, buf);
            Assert.Equal(new byte[] { 9, 10, 1, 2, 3 }, buf);
        }
Beispiel #4
0
        public void ReadOutsideOfAnyMemoryProducesAllZeros()
        {
            var mem  = new VirtualMemory();
            var ram1 = CreateFixedMemory(5);
            var ram2 = CreateFixedMemory(5);

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

            var buf = new byte[5];

            mem.Read(10, buf);
            Assert.True(buf.All(b => b == 0));
        }
Beispiel #5
0
        public void WriteOverDetachedMemoryThrows()
        {
            var mem  = new VirtualMemory();
            var ram1 = CreateFixedMemory(10);
            var ram2 = CreateFixedMemory(10);

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

            var buf = CreateData(15);

            Assert.Throws <InvalidOperationException>(() => mem.Write(5, buf.Span));
        }
Beispiel #6
0
        public void WriteThatGoesOverTheEndThrows()
        {
            var mem = new VirtualMemory();
            var buf = CreateData(20);

            Assert.Throws <IndexOutOfRangeException>(() => mem.Write(0, buf.Span));

            mem.Attach(0, new FixedMemory(5));
            mem.Attach(5, new FixedMemory(5));

            Assert.Throws <IndexOutOfRangeException>(() => mem.Write(0, buf.Span));
            Assert.Throws <IndexOutOfRangeException>(() => mem.Write(7, buf.Span));
            Assert.Throws <IndexOutOfRangeException>(() => mem.Write(15, buf.Span));
        }
Beispiel #7
0
        public void ReadThatGoesOverTheEndThrows()
        {
            var mem = new VirtualMemory();
            var buf = new byte[20];

            Assert.Throws <IndexOutOfRangeException>(() => mem.Read(0, buf));

            mem.Attach(0, new FixedMemory(5));
            mem.Attach(5, new FixedMemory(5));

            Assert.Throws <IndexOutOfRangeException>(() => mem.Read(0, buf));
            Assert.Throws <IndexOutOfRangeException>(() => mem.Read(7, buf));
            Assert.Throws <IndexOutOfRangeException>(() => mem.Read(15, buf));
        }
Beispiel #8
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);
        }
Beispiel #9
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));
        }
Beispiel #10
0
        public void ReadSpanningTwoMemoriesWithAGap()
        {
            var mem  = new VirtualMemory();
            var ram1 = CreateFixedMemory(10);
            var ram2 = CreateFixedMemory(10);

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

            var buf = new byte[15];

            mem.Read(5, buf);
            Assert.Equal(new byte[] {
                6, 7, 8, 9, 10,
                0, 0, 0, 0, 0,
                1, 2, 3, 4, 5
            }, buf);
        }
Beispiel #11
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());
        }
Beispiel #12
0
        public void ReadDoesNotIncludeDetachedMemory()
        {
            var mem  = new VirtualMemory();
            var ram1 = CreateFixedMemory(10);
            var ram2 = CreateFixedMemory(10);

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

            var buf = new byte[15];

            mem.Read(5, buf);
            Assert.Equal(new byte[] {
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                1, 2, 3, 4, 5
            }, buf);
        }
Beispiel #13
0
        public void ReadContainedInOneMemory()
        {
            var mem  = new VirtualMemory();
            var ram1 = CreateFixedMemory(10);

            mem.Attach(0, ram1);

            var buf = new byte[5];

            mem.Read(1, buf);
            Assert.Equal(new byte[] { 2, 3, 4, 5, 6 }, buf);
        }
Beispiel #14
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());
        }