001: /*
002: * $Id: TestFreePhysicalRowIdPage.java,v 1.3 2003/09/21 15:49:02 boisvert Exp $
003: *
004: * Unit tests for FreePhysicalRowIdPage class
005: *
006: * Simple db toolkit
007: * Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Library General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Library General Public License for more details.
018: *
019: * You should have received a copy of the GNU Library General Public License
020: * along with this library; if not, write to the Free Software Foundation,
021: * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
022: */
023: package jdbm.recman;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: /**
029: * This class contains all Unit tests for {@link FreePhysicalRowIdPage}.
030: */
031: public class TestFreePhysicalRowIdPage extends TestCase {
032:
033: public TestFreePhysicalRowIdPage(String name) {
034: super (name);
035: }
036:
037: /**
038: * Test constructor - create a page
039: */
040: public void testCtor() throws Exception {
041: byte[] data = new byte[RecordFile.BLOCK_SIZE];
042: BlockIo test = new BlockIo(0, data);
043: new PageHeader(test, Magic.FREEPHYSIDS_PAGE);
044: FreePhysicalRowIdPage page = new FreePhysicalRowIdPage(test);
045: }
046:
047: /**
048: * Test basics
049: */
050: public void testBasics() throws Exception {
051: byte[] data = new byte[RecordFile.BLOCK_SIZE];
052: BlockIo test = new BlockIo(0, data);
053: new PageHeader(test, Magic.FREEPHYSIDS_PAGE);
054: FreePhysicalRowIdPage page = new FreePhysicalRowIdPage(test);
055:
056: // we have a completely empty page.
057: assertEquals("zero count", 0, page.getCount());
058:
059: // three allocs
060: FreePhysicalRowId id = page.alloc(0);
061: id = page.alloc(1);
062: id = page.alloc(2);
063: assertEquals("three count", 3, page.getCount());
064:
065: // setup last id (2)
066: id.setBlock(1);
067: id.setOffset((short) 2);
068: id.setSize(3);
069:
070: // two frees
071: page.free(0);
072: page.free(1);
073: assertEquals("one left count", 1, page.getCount());
074: assertTrue("isfree 0", page.isFree(0));
075: assertTrue("isfree 1", page.isFree(1));
076: assertTrue("isalloc 2", page.isAllocated(2));
077:
078: // now, create a new page over the data and check whether
079: // it's all the same.
080: page = new FreePhysicalRowIdPage(test);
081:
082: assertEquals("2: one left count", 1, page.getCount());
083: assertTrue("2: isfree 0", page.isFree(0));
084: assertTrue("2: isfree 1", page.isFree(1));
085: assertTrue("2: isalloc 2", page.isAllocated(2));
086:
087: id = page.get(2);
088: assertEquals("block", 1, id.getBlock());
089: assertEquals("offset", 2, id.getOffset());
090: assertEquals("size", 3, id.getSize());
091:
092: }
093:
094: /**
095: * Runs all tests in this class
096: */
097: public static void main(String[] args) {
098: junit.textui.TestRunner.run(new TestSuite(
099: TestFreePhysicalRowIdPage.class));
100: }
101: }
|