public void TestCreateBlockIfNeeded() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); NPOIFSMiniStore ministore = fs.GetMiniStore(); // 178 -> 179 -> 180, 181+ is free Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); for (int i = 181; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } // However, the ministore data only covers blocks to 183 for (int i = 0; i <= 183; i++) { ministore.GetBlockAt(i); } try { ministore.GetBlockAt(184); Assert.Fail("No block at 184"); } catch (IndexOutOfRangeException) { } // The ministore itself is made up of 23 big blocks IEnumerator<ByteBuffer> it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator(); int count = 0; while (it.MoveNext()) { count++; //it.MoveNext(); } Assert.AreEqual(23, count); // Ask it to get block 184 with creating, it will do ministore.CreateBlockIfNeeded(184); // The ministore should be one big block bigger now it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator(); count = 0; while (it.MoveNext()) { count++; //it.MoveNext(); } Assert.AreEqual(24, count); // The mini block block counts now run to 191 for (int i = 0; i <= 191; i++) { ministore.GetBlockAt(i); } try { ministore.GetBlockAt(192); Assert.Fail("No block at 192"); } catch (IndexOutOfRangeException) { } // Now try writing through to 192, check that the SBAT and blocks are there byte[] data = new byte[15 * 64]; NPOIFSStream stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); // Check now Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(181, ministore.GetNextBlock(180)); Assert.AreEqual(182, ministore.GetNextBlock(181)); Assert.AreEqual(183, ministore.GetNextBlock(182)); Assert.AreEqual(184, ministore.GetNextBlock(183)); Assert.AreEqual(185, ministore.GetNextBlock(184)); Assert.AreEqual(186, ministore.GetNextBlock(185)); Assert.AreEqual(187, ministore.GetNextBlock(186)); Assert.AreEqual(188, ministore.GetNextBlock(187)); Assert.AreEqual(189, ministore.GetNextBlock(188)); Assert.AreEqual(190, ministore.GetNextBlock(189)); Assert.AreEqual(191, ministore.GetNextBlock(190)); Assert.AreEqual(192, ministore.GetNextBlock(191)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(192)); for (int i = 193; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } fs.Close(); }
public void TestCreateBlockIfNeeded() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); NPOIFSMiniStore ministore = fs.GetMiniStore(); // 178 -> 179 -> 180, 181+ is free Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); for (int i = 181; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } // However, the ministore data only covers blocks to 183 for (int i = 0; i <= 183; i++) { ministore.GetBlockAt(i); } try { ministore.GetBlockAt(184); Assert.Fail("No block at 184"); } catch (IndexOutOfRangeException) { } // The ministore itself is made up of 23 big blocks IEnumerator <ByteBuffer> it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator(); int count = 0; while (it.MoveNext()) { count++; //it.MoveNext(); } Assert.AreEqual(23, count); // Ask it to get block 184 with creating, it will do ministore.CreateBlockIfNeeded(184); // The ministore should be one big block bigger now it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator(); count = 0; while (it.MoveNext()) { count++; //it.MoveNext(); } Assert.AreEqual(24, count); // The mini block block counts now run to 191 for (int i = 0; i <= 191; i++) { ministore.GetBlockAt(i); } try { ministore.GetBlockAt(192); Assert.Fail("No block at 192"); } catch (IndexOutOfRangeException) { } // Now try writing through to 192, check that the SBAT and blocks are there byte[] data = new byte[15 * 64]; NPOIFSStream stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); // Check now Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(181, ministore.GetNextBlock(180)); Assert.AreEqual(182, ministore.GetNextBlock(181)); Assert.AreEqual(183, ministore.GetNextBlock(182)); Assert.AreEqual(184, ministore.GetNextBlock(183)); Assert.AreEqual(185, ministore.GetNextBlock(184)); Assert.AreEqual(186, ministore.GetNextBlock(185)); Assert.AreEqual(187, ministore.GetNextBlock(186)); Assert.AreEqual(188, ministore.GetNextBlock(187)); Assert.AreEqual(189, ministore.GetNextBlock(188)); Assert.AreEqual(190, ministore.GetNextBlock(189)); Assert.AreEqual(191, ministore.GetNextBlock(190)); Assert.AreEqual(192, ministore.GetNextBlock(191)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(192)); for (int i = 193; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } fs.Close(); }