private static void MergeAfterTestAssert(int sizeIndex1, int endIndex1, int sizeIndex2, int endIndex2)
        {
            PersistentHeapFreeSpace freeSpace1 = new PersistentHeapFreeSpace(sizeIndex1, endIndex1, 5);
            PersistentHeapFreeSpace freeSpace2 = new PersistentHeapFreeSpace(sizeIndex2, endIndex2, 6);

            freeSpace1.MergeAfter(freeSpace2);

            Assert.AreEqual(endIndex2, freeSpace1.EndIndex);
            Assert.AreEqual(endIndex2 - sizeIndex1 - PersistentHeapSpace.GetUserSizeSize() + 1, freeSpace1.UserSize);
        }
        private static void SplitTestAssert(int sizeIndex, int userSize, int newUserSize)
        {
            PersistentHeapFreeSpace freeSpace = new PersistentHeapFreeSpace(sizeIndex, sizeIndex + userSize + PersistentHeapSpace.GetUserSizeSize() - 1, 29);

            PersistentHeapUsedSpace usedSpace = freeSpace.Split(newUserSize);

            Assert.AreEqual(newUserSize, freeSpace.UserSize);
            Assert.AreEqual(sizeIndex, freeSpace.SizeIndex);
            Assert.AreEqual(sizeIndex + newUserSize + PersistentHeapSpace.GetUserSizeSize() - 1, freeSpace.EndIndex);

            Assert.AreEqual(userSize - newUserSize - PersistentHeapSpace.GetUserSizeSize(), usedSpace.UserSize);
            Assert.AreEqual(sizeIndex + newUserSize + PersistentHeapSpace.GetUserSizeSize() - 1 + 1, usedSpace.SizeIndex);
        }
Beispiel #3
0
        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());
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        private PersistentHeapFreeSpace GetFreeSpaceThatIsAtLeast(int userSize)
        {
            PersistentHeapFreeSpace lastFreeSpace = null;

            //try to find a space in the free spaces list
            foreach (PersistentHeapFreeSpace persistentHeapFreeSpace in GetFreeSpaces())
            {
                if (persistentHeapFreeSpace.UserSize >= userSize)
                {
                    return(persistentHeapFreeSpace);
                }
                lastFreeSpace = persistentHeapFreeSpace;
            }


            //couldn't freespace available that is big enough, going to make our own

            int openIndex = _file.GetNextIndex();
            int fullSize  = userSize + PersistentHeapSpace.GetUserSizeSize();
            PersistentHeapFreeSpace freeSpaceOnTheEnd = new PersistentHeapFreeSpace(openIndex, openIndex + fullSize - 1, FreeLinkedListNullPointer);

            //update the free linked list
            if (lastFreeSpace == null) //there are no nodes in the free space linked list
            {
                Debug.Assert(GetFreeSpaceHead() == FreeLinkedListNullPointer);
                PutFreeSpaceHead(freeSpaceOnTheEnd.StartIndex);
            }
            else //there are nodes in the free space linked list
            {
                Debug.Assert(lastFreeSpace != null);
                lastFreeSpace.NextFreeSpace = freeSpaceOnTheEnd.StartIndex;
                PutFullSpace(lastFreeSpace);
            }

            PutFullSpace(freeSpaceOnTheEnd);
            return(freeSpaceOnTheEnd);
        }
        private static void StartIndexTestAssert(int startIndex, int userSize)
        {
            PersistentHeapSpace space = InitHeapSpace(startIndex - PersistentHeapSpace.GetUserSizeSize(), startIndex + userSize, new byte[0]);

            Assert.AreEqual(startIndex, space.StartIndex);
        }
        private static void UserSizeTestAssert(int userSize, int sizeIndex)
        {
            PersistentHeapSpace space = InitHeapSpace(sizeIndex, sizeIndex + userSize + PersistentHeapSpace.GetUserSizeSize() - 1, new byte[0]);

            Assert.AreEqual(userSize, space.UserSize);
        }