public void SerializeTest()
        {
            int sizeIndex = 7;
            int endIndex = 20;
            byte[] data = new byte[]{1,2,3,4,5,6,};
            PersistentHeapUsedSpace usedSpace = new PersistentHeapUsedSpace(sizeIndex, endIndex, data);
            int userSize = (endIndex - sizeIndex - PersistentHeapSpace.GetUserSizeSize() + 1);
            byte[] userSizeBytes = userSize.ToBytes();
            byte[] expected = userSizeBytes.Append(data);

            TestHelper.AssertByteArraysAreSame(expected, usedSpace.Serialize());
        }
        public PersistentHeapUsedSpace Split(int newUserSize)
        {
            int newProposedBoundery = StartIndex + newUserSize-1;
            if(newProposedBoundery <= StartIndex)
                throw new InvalidIndexException("The new size is too small for a space to be created before it");
            if(newProposedBoundery >= EndIndex - GetUserSizeSize())
                throw new InvalidIndexException("The new size is too large for a space to be created after it");

            int usedSpaceStartIndex = newProposedBoundery + 1;
            int usedSpaceEndIndex = EndIndex;
            PersistentHeapUsedSpace usedSpace = new PersistentHeapUsedSpace(usedSpaceStartIndex, usedSpaceEndIndex, new byte[0]);

            EndIndex = newProposedBoundery;

            return usedSpace;
        }
        public void CtorTest()
        {
            byte[] data = new byte[]{1,2,3,4,5,6,7,8,9,0};
            PersistentHeapUsedSpace usedSpace = new PersistentHeapUsedSpace(7, 33, data);
            TestHelper.AssertByteArraysAreSame(data, usedSpace.Data);

            byte[] data2 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, };
            PersistentHeapUsedSpace usedSpace2 = new PersistentHeapUsedSpace(5, 18, data2);
            TestHelper.AssertByteArraysAreSame(data2, usedSpace2.Data);

            try {
                new PersistentHeapUsedSpace(7, 10, new byte[4]);
                Assert.Fail("Should throw exception");
            } catch (IndexOutOfRangeException) {}

            try {
                new PersistentHeapUsedSpace(7, 14, new byte[5]);
                Assert.Fail("Should throw exception");
            } catch (IndexOutOfRangeException) {}
        }
        public PersistentHeapUsedSpace Split(int newUserSize)
        {
            int newProposedBoundery = StartIndex + newUserSize - 1;

            if (newProposedBoundery <= StartIndex)
            {
                throw new InvalidIndexException("The new size is too small for a space to be created before it");
            }
            if (newProposedBoundery >= EndIndex - GetUserSizeSize())
            {
                throw new InvalidIndexException("The new size is too large for a space to be created after it");
            }

            int usedSpaceStartIndex           = newProposedBoundery + 1;
            int usedSpaceEndIndex             = EndIndex;
            PersistentHeapUsedSpace usedSpace = new PersistentHeapUsedSpace(usedSpaceStartIndex, usedSpaceEndIndex, new byte[0]);

            EndIndex = newProposedBoundery;

            return(usedSpace);
        }
        private PersistentHeapUsedSpace ToUsedSpace(PersistentHeapFreeSpace freeSpace)
        {
            // remove free space from linked list of free spaces
            PersistentHeapFreeSpace before = GetFreeSpaceBefore(freeSpace.StartIndex);

            if (before == null) //there isn't free space before freeSpace
            {
                Debug.Assert(GetFreeSpaceHead() == freeSpace.StartIndex); //the first free space better be the free space passed in
                PutFreeSpaceHead(freeSpace.NextFreeSpace);
            }
            else //there is a free space before freeSpace
            {
                Debug.Assert(before.NextFreeSpace == freeSpace.StartIndex);
                before.NextFreeSpace = freeSpace.NextFreeSpace;
                PutFullSpace(before);
            }

            int usedSpaceDataSize = freeSpace.EndIndex - freeSpace.StartIndex + 1;
            PersistentHeapUsedSpace usedSpace = new PersistentHeapUsedSpace(freeSpace.SizeIndex, freeSpace.EndIndex, new byte[usedSpaceDataSize]);
            PutFullSpace(usedSpace);
            return usedSpace;
        }
        private PersistentHeapUsedSpace GetSpaceAt(int token)
        {
            int sizeIndex = token - PersistentHeapSpace.GetUserSizeSize();
            byte[] sizeBytes = _file.GetAmount(sizeIndex, PersistentHeapSpace.GetUserSizeSize());
            int userSize = PersistentHeapSpace.GetUserSize(sizeBytes);

            int fullLength = userSize + PersistentHeapSpace.GetUserSizeSize();
            int endIndex = sizeIndex + fullLength-1;

            byte[] data = _file.GetRange(sizeIndex + PersistentHeapSpace.GetUserSizeSize(), endIndex);

            //let PersistentHeapUsedSpace be default
            PersistentHeapUsedSpace usedSpace = new PersistentHeapUsedSpace(sizeIndex, endIndex, data);
            return usedSpace;
        }