-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomizedQueueTest.java
92 lines (81 loc) · 2.37 KB
/
RandomizedQueueTest.java
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
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
public class RandomizedQueueTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testInsert() {
int[] arr = {1,2,3,4,5};
RandomizedQueue<Integer> rq = new RandomizedQueue<Integer>();
for (int i:arr) {
rq.enqueue(i);
assertEquals(i, rq.size());
}
assertEquals(5, rq.size());
}
@Test
public void oneElementInsertDelete() {
String value = "Hi";
RandomizedQueue<String> rq = new RandomizedQueue<String>();
rq.enqueue(value);
assertEquals(value,rq.dequeue());
}
@Test
public void oneElementInsertSample() {
String value = "Hi";
RandomizedQueue<String> rq = new RandomizedQueue<String>();
rq.enqueue(value);
assertEquals(value,rq.sample());
}
@Test
public void testInsertDelete() {
int[] arr = {1,2,3,4,5,6};
int[] brr = new int[6];
RandomizedQueue<Integer> rQ = new RandomizedQueue<Integer>();
for (int i:arr)
rQ.enqueue(i);
assertEquals(6, rQ.size());
for (int i = 0; i<6; i++)
brr[i] = rQ.dequeue();
assertEquals(0, rQ.size());
for (int i:arr)
rQ.enqueue(i);
assertEquals(6, rQ.size());
for (int i = 0; i<6; i++)
brr[i] = rQ.dequeue();
Arrays.sort(brr);
assertArrayEquals(brr, arr);
assertEquals(0, rQ.size());
}
@Test
public void testIterator() {
int[] arr = {1,2,3,4,5,6};
int[] brr = new int[6];
RandomizedQueue<Integer> rQ = new RandomizedQueue<Integer>();
for (int i:arr)
rQ.enqueue(i);
int idx = 0;
for (int i: rQ)
brr[idx++] = i;
Arrays.sort(brr);
assertArrayEquals(brr, arr);
}
@Test
public void testInsertDel() {
RandomizedQueue<Integer> rQ = new RandomizedQueue<Integer>();
rQ.enqueue(1);
rQ.enqueue(2);
rQ.dequeue();
rQ.dequeue();
rQ.enqueue(1);
assertEquals(1,rQ.size());
}
@Test(expected=java.util.NoSuchElementException.class)
public void emptyDelete() {
RandomizedQueue<Integer> rq = new RandomizedQueue<Integer>();
rq.dequeue();
}
}