public void GetPathToNameTest() { string fileName = "GetPathToNameTest"; string path = FileWithHeader.GetPathToName(fileName); DirectoryInfo dirInfo = Directory.GetParent(path); Assert.IsTrue(dirInfo.Exists); }
private static void GetPutTestAssert(byte expectedValue, int expectedIndex, FileWithHeader target) { int nextIndexBefore = target.GetNextIndex(); target.Put(expectedIndex, expectedValue); if (expectedIndex >= nextIndexBefore) { Assert.AreEqual(expectedIndex + 1, target.GetNextIndex()); } Assert.AreEqual(expectedValue, target.Get(expectedIndex)); }
private static void GetPutArrayTestAssert(FileWithHeader fwh, int index, byte[] data) { int nextIndexBefore = fwh.GetNextIndex(); fwh.Put(index, data); if (index + data.Length >= nextIndexBefore) { Assert.AreEqual(index + data.Length, fwh.GetNextIndex()); } byte[] actual = fwh.GetAmount(index, data.Length); TestHelper.AssertByteArraysAreSame(data, actual); }
public PersistentNextSpaceArray(String arrayName, int elementSize, int userHeaderSize) { if (elementSize <= 0) { throw new InvalidElementSizeException("The element size must be greater than zero"); } if (userHeaderSize < 0) { throw new InvalidUserHeaderException("User Header size must be positive"); } _fileWithHeader = new FileWithHeader(arrayName, userHeaderSize + GetElementSizeSize()); PutElementSize(elementSize); PutUserHeader(new byte[0]); }
private void GetUserHeaderSizeTestAssert(int expected) { FileWithHeader target = InitFWH("GetFWHUserHeaderSizeTest", expected); try { int actual = target.GetUserHeaderSize(); Assert.AreEqual(expected, actual); } finally { target.Close(); } }
public void CloseTest() { FileWithHeader fwh = InitFWH("FWHCloseTest", 5); try { fwh.Close(); fwh = InitFWH("FWHCloseTest", 3); } finally { fwh.Close(); } }
private void GetNextIndexTestAssert(int expected) { FileWithHeader target = InitFWH("GetFWHNextIndexTest", 16); try { target.Put(expected - 1, 0); int actual = target.GetNextIndex(); Assert.AreEqual(expected, actual); } finally { target.Close(); } }
private void GetPutUserHeaderTest(byte[] uHeader) { FileWithHeader target = InitFWH("GetPutUserHeaderTest", 16); try { target.PutUserHeader(uHeader); byte[] actual = target.GetUserHeader(); TestHelper.AssertByteArraysAreSame(uHeader, actual); } finally { target.Close(); } }
public FileWithHeader InitFWH(string fileName, int userHeaderSize) { FileWithHeader fwh; try { fwh = new FileWithHeader(fileName, userHeaderSize); } catch (FileNameConflictException) { fwh = new FileWithHeader(fileName); fwh.Delete(); fwh = new FileWithHeader(fileName, userHeaderSize); } return(fwh); }
public void GetPutTest() { FileWithHeader target = InitFWH("GetPutFWHTest", 5); try { GetPutTestAssert(8, 44, target); GetPutTestAssert(0, 44, target); GetPutTestAssert(99, 0, target); GetPutTestAssert(20, byte.MaxValue, target); GetPutTestAssert(4, 4, target); } finally { target.Close(); } }
public void GetRangeTest() { FileWithHeader fwh = InitFWH("FWHGetRangeTest", 5); try { GetRangeTestAssert(new byte[] { 1, 2, 3, 4 }, 0, fwh); GetRangeTestAssert(new byte[] { }, 6, fwh); GetRangeTestAssert(new byte[] { 0 }, 8, fwh); GetRangeTestAssert(new byte[] { byte.MaxValue, byte.MaxValue, byte.MaxValue }, 10, fwh); GetRangeTestAssert(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, 20, fwh); } finally { fwh.Close(); } }
public void PutGetAmountTest() { FileWithHeader fwh = InitFWH("FWHPutGetAmountTest", 5); try { GetPutArrayTestAssert(fwh, 9, new byte[] { }); GetPutArrayTestAssert(fwh, 3, new byte[] { byte.MaxValue, byte.MinValue }); GetPutArrayTestAssert(fwh, 50, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }); GetPutArrayTestAssert(fwh, 10, new byte[] { 7, 7, 7 }); GetPutArrayTestAssert(fwh, 30, new byte[] { 12, 34, 56, 78, 90 }); GetPutArrayTestAssert(fwh, 25, new byte[] { 0, 0, 0, 0, 0, 0, 0 }); } finally { fwh.Close(); } }
public void FileAlreadyExistsTest() { string fileName = "AlreadyExists"; FileWithHeader fwh = InitFWH(fileName, 5); FileWithHeader fwh2 = null; try { fwh2 = new FileWithHeader(fileName, 5); Assert.Fail("Should throw exception"); } catch (FileNameConflictException) { } finally { fwh.Close(); if (fwh2 != null) //if for some reason it actuall worked, { fwh2.Close(); // still close the file } } }
public void ReopenTest() { string fileName = "ReopenFileWithHeaderTest"; FileWithHeader fwh = InitFWH(fileName, 5); try { //write values int expectedIndex1 = 0; byte expectedValue1 = 6; fwh.Put(expectedIndex1, expectedValue1); int expectedIndex2 = 55; byte expectedValue2 = 200; fwh.Put(expectedIndex2, expectedValue2); //get next index int expectedNextIndex = fwh.GetNextIndex(); byte[] expectedUserHeader = new byte[5] { 1, 2, 3, 4, 5 }; fwh.PutUserHeader(expectedUserHeader); fwh.Close(); fwh = new FileWithHeader(fileName); Assert.AreEqual(expectedValue1, fwh.Get(expectedIndex1)); Assert.AreEqual(expectedValue2, fwh.Get(expectedIndex2)); Assert.AreEqual(expectedNextIndex, fwh.GetNextIndex()); TestHelper.AssertByteArraysAreSame(expectedUserHeader, fwh.GetUserHeader()); } finally { fwh.Close(); } }
public PersistentNextSpaceArray(String arrayName) { _fileWithHeader = new FileWithHeader(arrayName); byte[] userHeaderBytes = _fileWithHeader.GetUserHeader(); }
public PersistentHeap(String heapName) { _file = new FileWithHeader(heapName); }
public PersistentHeap(String heapName, int userHeaderSize) { _file = new FileWithHeader(heapName, userHeaderSize + GetFreeSpaceHeadSize()); PutFreeSpaceHead(FreeLinkedListNullPointer); }
private static void GetRangeTestAssert(byte[] bytes, int startIndex, FileWithHeader fwh) { fwh.Put(startIndex, bytes); byte[] actual = fwh.GetRange(startIndex, startIndex + bytes.Length - 1); TestHelper.AssertByteArraysAreSame(bytes, actual); }