01: /*
02: * $Id: TestFreeLogicalRowIdPage.java,v 1.3 2003/09/21 15:49:02 boisvert Exp $
03: *
04: * Unit tests for FreeLogicalRowIdPage class
05: *
06: * Simple db toolkit
07: * Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
08: *
09: * This library is free software; you can redistribute it and/or
10: * modify it under the terms of the GNU Library General Public License
11: * as published by the Free Software Foundation; either version 2
12: * of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: * Library General Public License for more details.
18: *
19: * You should have received a copy of the GNU Library General Public License
20: * along with this library; if not, write to the Free Software Foundation,
21: * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22: */
23: package jdbm.recman;
24:
25: import junit.framework.TestCase;
26: import junit.framework.TestSuite;
27:
28: /**
29: * This class contains all Unit tests for {@link FreeLogicalRowIdPage}.
30: */
31: public class TestFreeLogicalRowIdPage extends TestCase {
32:
33: public TestFreeLogicalRowIdPage(String name) {
34: super (name);
35: }
36:
37: /**
38: * Test constructor - create a page
39: */
40: public void testCtor() throws Exception {
41: byte[] data = new byte[RecordFile.BLOCK_SIZE];
42: BlockIo test = new BlockIo(0, data);
43: new PageHeader(test, Magic.FREELOGIDS_PAGE);
44: FreeLogicalRowIdPage page = new FreeLogicalRowIdPage(test);
45: }
46:
47: /**
48: * Test basics
49: */
50: public void testBasics() throws Exception {
51: byte[] data = new byte[RecordFile.BLOCK_SIZE];
52: BlockIo test = new BlockIo(0, data);
53: new PageHeader(test, Magic.FREELOGIDS_PAGE);
54: FreeLogicalRowIdPage page = new FreeLogicalRowIdPage(test);
55:
56: // we have a completely empty page.
57: assertEquals("zero count", 0, page.getCount());
58:
59: // three allocs
60: PhysicalRowId id = page.alloc(0);
61: id = page.alloc(1);
62: id = page.alloc(2);
63: assertEquals("three count", 3, page.getCount());
64:
65: // setup last id (2)
66: id.setBlock(1);
67: id.setOffset((short) 2);
68:
69: // two frees
70: page.free(0);
71: page.free(1);
72: assertEquals("one left count", 1, page.getCount());
73: assertTrue("isfree 0", page.isFree(0));
74: assertTrue("isfree 1", page.isFree(1));
75: assertTrue("isalloc 2", page.isAllocated(2));
76:
77: // now, create a new page over the data and check whether
78: // it's all the same.
79: page = new FreeLogicalRowIdPage(test);
80:
81: assertEquals("2: one left count", 1, page.getCount());
82: assertTrue("2: isfree 0", page.isFree(0));
83: assertTrue("2: isfree 1", page.isFree(1));
84: assertTrue("2: isalloc 2", page.isAllocated(2));
85:
86: id = page.get(2);
87: assertEquals("block", 1, id.getBlock());
88: assertEquals("offset", 2, id.getOffset());
89:
90: }
91:
92: /**
93: * Runs all tests in this class
94: */
95: public static void main(String[] args) {
96: junit.textui.TestRunner.run(new TestSuite(
97: TestFreeLogicalRowIdPage.class));
98: }
99: }
|