-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashtableTest.java
More file actions
154 lines (137 loc) · 4.2 KB
/
HashtableTest.java
File metadata and controls
154 lines (137 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import junit.framework.TestCase;
import org.junit.Before;
// -------------------------------------------------------------------------
/**
* memmory manager class test.
*
* @author wenfeng ren
* @version Sep 15, 2014
*/
public class HashtableTest
extends TestCase
{
private Hashtable songTable;
private MemManager mem;
/**
*
*/
private String name1 = "Long Lonesome Blues";
/**
*
*/
private String name2 = "Ma Rainey's Black Bottom";
/**
*
*/
private String name3 = "Mississippi Boweavil Blues";
/**
*
*/
private Handle handle1 = new Handle(1);
private Handle handle2 = new Handle(2);
@Before
public void setUp()
throws Exception
{
// initialize
songTable = new Hashtable(5);
mem = new MemManager(10);
}
// ----------------------------------------------------------
/**
* Place a description of your method here.
*/
// ----------------------------------------------------------
/**
* test findindex method.
*/
public void testFindIndex()
{
//
long key = songTable.hash(name1);
long keyy = songTable.hash(name2);
assertEquals(0, songTable.findIndex(key));
songTable.insert(name1, handle1);
assertEquals(1, songTable.findIndex(key));
songTable.insert(name2, handle2);
songTable.remove(name1);
assertEquals(2, songTable.findIndex(keyy));
}
// ----------------------------------------------------------
/**
* test insert method.
*/
public void testInsert()
{
assertEquals(true, songTable.insert(name1, handle1));
assertFalse(songTable.rehashNeed(name1));
assertEquals(true, songTable.insert(name2, handle2));
assertTrue(songTable.rehashNeed(name3));
assertEquals(true, songTable.insert(name3, handle2));
songTable.remove(name3);
songTable.insert(name3, handle2);
assertFalse(songTable.rehashNeed(name1));
assertEquals(false, songTable.insert(name2, handle2));
assertEquals(3, songTable.usedSize());
}
// ----------------------------------------------------------
/**
* test remove method.
*/
public void testRemove()
{
songTable.insert(name1, handle1);
songTable.insert(name2, handle2);
songTable.insert(name3, handle2);
songTable.remove(name3);
songTable.remove(name2);
assertEquals(1, songTable.usedSize());
}
// ----------------------------------------------------------
/**
* test search method.
*/
public void testSearch()
{
long key = songTable.hash(name2);
long keyy = songTable.hash(name3);
assertEquals(false, songTable.search(key));
songTable.insert(name1, handle1);
songTable.insert(name2, handle2);
assertEquals(true, songTable.search(key));
songTable.insert(name3, handle2);
assertEquals(true, songTable.search(keyy));
}
// ----------------------------------------------------------
/**
* test get method.
*/
public void testGet()
{
long key = songTable.hash(name2);
long keyy = songTable.hash(name3);
songTable.insert(name1, handle1);
songTable.insert(name2, handle2);
songTable.insert(name3, handle2);
assertEquals(6, songTable.get(key));
assertEquals(2, songTable.get(keyy));
}
// ----------------------------------------------------------
/**
* test print method.
*/
public void testPrint()
{
assertEquals("total ", songTable.print(mem));
byte[] space = Client.stringToByte(name1);
Handle handle3 = mem.insert(space, space.length);
songTable.insert(name1, handle3);
String string = "|Long Lonesome Blues| " + 0 + "\n";
assertEquals(string + "total ", songTable.print(mem));
byte[] space2 = Client.stringToByte(name2);
Handle handle4 = mem.insert(space2, space2.length);
songTable.insert(name2, handle4);
songTable.remove((name2));
assertEquals(string + "total ", songTable.print(mem));
}
}