Beispiel #1
0
        public void TestGetFreeBlockWithSpare()
        {
            NPOIFSFileSystem fs        = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi"));
            NPOIFSMiniStore  ministore = fs.GetMiniStore();

            // Our 2nd SBAT block has spares
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(true, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);

            // First free one at 181
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(181));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(182));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(183));
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(184));

            // Ask, will get 181
            Assert.AreEqual(181, ministore.GetFreeBlock());

            // Ask again, will still get 181 as not written to
            Assert.AreEqual(181, ministore.GetFreeBlock());

            // Allocate it, then ask again
            ministore.SetNextBlock(181, POIFSConstants.END_OF_CHAIN);
            Assert.AreEqual(182, ministore.GetFreeBlock());

            fs.Close();
        }
Beispiel #2
0
        public void TestGetFreeBlockWithNonSpare()
        {
            NPOIFSFileSystem fs        = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi"));
            NPOIFSMiniStore  ministore = fs.GetMiniStore();

            // We've spare ones from 181 to 255
            for (int i = 181; i < 256; i++)
            {
                Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i));
            }

            // Check our SBAT free stuff is correct
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(true, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);

            // Allocate all the spare ones
            for (int i = 181; i < 256; i++)
            {
                ministore.SetNextBlock(i, POIFSConstants.END_OF_CHAIN);
            }

            // SBAT are now full, but there's only the two
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);
            try
            {
                Assert.AreEqual(false, ministore.GetBATBlockAndIndex(256).Block.HasFreeSectors);
                Assert.Fail("Should only be two SBATs");
            }
            catch (ArgumentOutOfRangeException) { }

            // Now ask for a free one, will need to extend the SBAT chain
            Assert.AreEqual(256, ministore.GetFreeBlock());

            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors);
            Assert.AreEqual(false, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors);
            Assert.AreEqual(true, ministore.GetBATBlockAndIndex(256).Block.HasFreeSectors);
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(254)); // 2nd SBAT
            Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(255)); // 2nd SBAT
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(256)); // 3rd SBAT
            Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(257)); // 3rd SBAT

            fs.Close();
        }