public void AllocateTooSmallSize()
        {
            PersistentHeap heap = InitHeap("HeapAllocateInARowTest", 5);

            try
            {
                try
                {
                    heap.Allocate(-1);
                    Assert.Fail("Should throw exception");
                }
                catch (InvalidElementSizeException) { }

                heap.Allocate(4);

                try
                {
                    heap.Allocate(3);
                    Assert.Fail("Should throw exception");
                }
                catch (InvalidElementSizeException) { }
            }
            finally
            {
                heap.Close();
            }
        }
        private static void PutGetTestAssert(byte[] buffer, PersistentHeap heap)
        {
            int token = heap.Allocate(buffer.Length);

            heap.Put(token, buffer);
            TestHelper.AssertByteArraysAreSame(buffer, heap.Get(token));
        }
        public void FreeMiddleSpaceTest()
        {
            PersistentHeap heap = InitHeap("HeapFreeMiddleSpaceTest", 5);

            try
            {
                heap.Allocate(4);
                int middle = heap.Allocate(6);
                heap.Allocate(7);

                heap.Free(middle);
            }
            finally
            {
                heap.Close();
            }
        }
        public void FreeLastSpaceTest()
        {
            PersistentHeap heap = InitHeap("HeapFreeLastSpaceTest", 5);

            try
            {
                heap.Allocate(4);
                heap.Allocate(6);
                int lastSpace = heap.Allocate(7);

                heap.Free(lastSpace);
            }
            finally
            {
                heap.Close();
            }
        }
        private static int PutGetAssert(byte[] bytes, PersistentHeap heap)
        {
            Debug.Assert(bytes.Length >= 4, "Your byte[] is too small to be put");
            int space1 = heap.Allocate(bytes.Length);

            heap.Put(space1, bytes);
            TestHelper.AssertByteArraysAreSame(bytes, heap.Get(space1));
            return(space1);
        }
        public void AllocateSplitTest()
        {
            PersistentHeap heap = InitHeap("HeapAllocateSplitTest", 9);

            try
            {
                int space1 = PutGetAssert(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, heap);
                int space2 = PutGetAssert(new byte[] { 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 }, heap);
                int space3 = PutGetAssert(new byte[] { 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 }, heap);
                int space4 = PutGetAssert(new byte[] { 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48 }, heap);

                heap.Free(space2);

                int space2_3 = heap.Allocate(4);

                Assert.IsTrue(space2 < space2_3);
                Assert.IsTrue(space2_3 < space3);
            }
            finally
            {
                heap.Close();
            }
        }
        public void FreeMergeAfterTest()
        {
            PersistentHeap heap = InitHeap("HeapFreeMergeAfterTest", 5);

            try
            {
                heap.Allocate(4);
                heap.Allocate(6);
                heap.Allocate(7);
                int previous = heap.Allocate(8);
                int after    = heap.Allocate(10);
                heap.Allocate(5);

                heap.Free(after);
                heap.Free(previous);
            }
            finally
            {
                heap.Close();
            }
        }
        public void FreeMergeBeforeTest()
        {
            PersistentHeap heap = InitHeap("HeapFreeMergeBeforeTest", 5);

            try
            {
                heap.Allocate(4);
                heap.Allocate(6);
                int before = heap.Allocate(7);
                int next   = heap.Allocate(8);
                heap.Allocate(10);
                heap.Allocate(5);

                heap.Free(before);
                heap.Free(next);
            }
            finally
            {
                heap.Close();
            }
        }
        public void FreeAddSpaceToEndOfFreeListTest()
        {
            PersistentHeap heap = InitHeap("HeapFreeAddSpaceToEndOfFreeListTest", 5);

            try
            {
                heap.Allocate(4);
                heap.Allocate(6);
                int toFree1 = heap.Allocate(7);
                heap.Allocate(8);
                heap.Allocate(10);
                int last = heap.Allocate(5);

                heap.Free(toFree1);
                heap.Free(last);
            }
            finally
            {
                heap.Close();
            }
        }
        public void FreeMergeBothTest()
        {
            PersistentHeap heap = InitHeap("HeapFreeMergeBothTest", 5);

            try
            {
                heap.Allocate(4);
                heap.Allocate(6);
                int before = heap.Allocate(7);
                int middle = heap.Allocate(8);
                int after  = heap.Allocate(10);
                heap.Allocate(5);

                heap.Free(after);
                heap.Free(before);
                heap.Free(middle);
            }
            finally
            {
                heap.Close();
            }
        }