public void TestIndirectBlocks()
        {
            var repository = new MockRepository();
            var mockDiskAccess = repository.DynamicMock<IDirectDiskAccess>();
            var mockAllocator = repository.StrictMock<IBlockAllocator>();

            const int blocksToAllocate = 2000;
            var list = new List<Address>();
            for (int i = 0; i < blocksToAllocate; ++i)
            {
                list.Add(new Address((ulong)i * Constants.BlockSizeBytes));
            }

            Expect.Call(mockAllocator.AllocateBlocks(blocksToAllocate)).IgnoreArguments().Return(list.ToArray());
            Expect.Call(mockAllocator.AllocateBlocks(1)).IgnoreArguments().Return(new[] { new Address(0) }).Repeat.Times(3); // indirect block allocation
            Expect.Call(() => mockAllocator.FreeBlocks(null)).IgnoreArguments().Repeat.Any();

            var storage = new BlockAddressStorage(mockDiskAccess, mockAllocator, new Address(0), new Address(0));

            repository.ReplayAll();

            storage.AddBlocks(2000);
            storage.GetBlockStartAddress(12);
            storage.GetBlockStartAddress(1024);
            storage.GetBlockStartAddress(1024 + 12);
            storage.GetBlockStartAddress(1999);
            Assert.Throws<IndexOutOfRangeException>(() => storage.GetBlockStartAddress(2000));

            repository.VerifyAll();
        }
Example #2
0
        private ulong _size; // will mean file size, bytes for file node, and number of entries for directory node.

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Node"/> class.
        /// </summary>
        /// <param name="blockAllocator">The block allocator.</param>
        /// <param name="diskAccess">The disk access.</param>
        /// <param name="address">The address.</param>
        /// <param name="globalBlockStartAddress">The global block start address.</param>
        public Node(
            [NotNull] IBlockAllocator blockAllocator,
            [NotNull] IDirectDiskAccess diskAccess,
            [NotNull] Address address,
            [NotNull] Address globalBlockStartAddress)
        {
            Validate.ArgumentNotNull(blockAllocator, "blockAllocator");
            Validate.ArgumentNotNull(diskAccess, "diskAccess");
            Validate.ArgumentNotNull(address, "address");
            Validate.ArgumentNotNull(globalBlockStartAddress, "globalBlockStartAddress");

            _address = address;
            DiskAccess = diskAccess;
            Storage = new BlockAddressStorage(
                diskAccess, blockAllocator, new Address(_address.Value + 25), globalBlockStartAddress);
        }
        public void TestBlockAllocation()
        {
            var repository = new MockRepository();
            var mockDiskAccess = repository.DynamicMock<IDirectDiskAccess>();
            var mockAllocator = repository.StrictMock<IBlockAllocator>();

            int count = 0;

            Expect.Call(mockAllocator.AllocateBlocks(1)).Return(new []{new Address(0)}).Repeat.Any().WhenCalled(f => count++);
            Expect.Call(() => mockAllocator.FreeBlocks(null)).IgnoreArguments().Repeat.Any().WhenCalled(f => count--);

            var storage = new BlockAddressStorage(mockDiskAccess, mockAllocator, new Address(0), new Address(0));

            repository.ReplayAll();

            for(int i = 0; i < 10000; ++i)
            {
                storage.AddBlocks(1);
            }

            Assert.AreEqual(10000, storage.NumBlocksAllocated);
            Assert.IsNotNull(storage.GetBlockStartAddress(0));
            Assert.IsNotNull(storage.GetBlockStartAddress(9999));

            storage.FreeLastBlocks(5000);
            Assert.AreEqual(5000, storage.NumBlocksAllocated);
            Assert.IsNotNull(storage.GetBlockStartAddress(4999));
            Assert.Throws<IndexOutOfRangeException>(() => storage.GetBlockStartAddress(5000));

            storage.FreeLastBlocks(5000);

            Assert.AreEqual(0, storage.NumBlocksAllocated);
            Assert.Throws<IndexOutOfRangeException>(() => storage.GetBlockStartAddress(0));
            Assert.Throws<ArgumentOutOfRangeException>(() => storage.FreeLastBlocks(1));
            Assert.AreEqual(0, count);
            repository.VerifyAll();
        }