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);
        }
Exemple #3
0
        public int GetNextIndex()
        {
            double actualNextIndex = _fileWithHeader.GetNextIndex();
            double nextElement     = actualNextIndex / GetElementSize();
            double roundedUp       = Math.Ceiling(nextElement);

            return((int)roundedUp);
        }
        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();
            }
        }
        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();
            }
        }
Exemple #6
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);
        }