Skip to content

Commit cb0a9b5

Browse files
committed
Improvements to "Validating" chapter
1 parent c17d52b commit cb0a9b5

5 files changed

Lines changed: 177 additions & 21 deletions

File tree

validating/BadMicroBenchmark.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
public class BadMicroBenchmark {
88
static final int SIZE = 250_000_000;
99
public static void main(String[] args) {
10-
long[] la = new long[SIZE];
11-
System.out.print("setAll: ");
12-
Time.it(() -> Arrays.setAll(la, n -> n));
13-
System.out.print("parallelSetAll: ");
14-
Time.it(() -> Arrays.parallelSetAll(la, n -> n));
10+
try { // For machines with insufficient memory
11+
long[] la = new long[SIZE];
12+
System.out.print("setAll: ");
13+
Time.it(() -> Arrays.setAll(la, n -> n));
14+
System.out.print("parallelSetAll: ");
15+
Time.it(() -> Arrays.parallelSetAll(la, n -> n));
16+
} catch(OutOfMemoryError e) {
17+
System.out.println("Insufficient memory");
18+
System.exit(0);
19+
}
1520
}
1621
}
1722
/* Output:
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
// validating/Queue.java
1+
// validating/CircularQueue.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
// Demonstration of Design by Contract (DbC)
66
package validating;
77
import java.util.*;
88

9-
public class Queue {
9+
public class CircularQueue {
1010
private Object[] data;
1111
private int
1212
in = 0, // Next available storage space
1313
out = 0; // Next gettable object
1414
// Has it wrapped around the circular queue?
1515
private boolean wrapped = false;
16-
public Queue(int size) {
16+
public CircularQueue(int size) {
1717
data = new Object[size];
1818
// Must be true after construction:
1919
assert invariant();
@@ -27,7 +27,8 @@ public boolean full() {
2727
public boolean isWrapped() { return wrapped; }
2828
public void put(Object item) {
2929
precondition(item != null, "put() null item");
30-
precondition(!full(), "put() into full Queue");
30+
precondition(!full(),
31+
"put() into full CircularQueue");
3132
assert invariant();
3233
data[in++] = item;
3334
if(in >= data.length) {
@@ -37,7 +38,8 @@ public void put(Object item) {
3738
assert invariant();
3839
}
3940
public Object get() {
40-
precondition(!empty(), "get() from empty Queue");
41+
precondition(!empty(),
42+
"get() from empty CircularQueue");
4143
assert invariant();
4244
Object returnVal = data[out];
4345
data[out] = null;
@@ -47,40 +49,43 @@ public Object get() {
4749
wrapped = false;
4850
}
4951
assert postcondition(
50-
returnVal != null, "Null item in Queue");
52+
returnVal != null,
53+
"Null item in CircularQueue");
5154
assert invariant();
5255
return returnVal;
5356
}
5457
// Design-by-contract support methods:
5558
private static void
5659
precondition(boolean cond, String msg) {
57-
if(!cond) throw new QueueException(msg);
60+
if(!cond) throw new CircularQueueException(msg);
5861
}
5962
private static boolean
6063
postcondition(boolean cond, String msg) {
61-
if(!cond) throw new QueueException(msg);
64+
if(!cond) throw new CircularQueueException(msg);
6265
return true;
6366
}
6467
private boolean invariant() {
6568
// Guarantee that no null values are in the
6669
// region of 'data' that holds objects:
6770
for(int i = out; i != in; i = (i + 1) % data.length)
6871
if(data[i] == null)
69-
throw new QueueException("null in queue");
72+
throw new CircularQueueException(
73+
"null in CircularQueue");
7074
// Guarantee that only null values are outside the
7175
// region of 'data' that holds objects:
7276
if(full()) return true;
7377
for(int i = in; i != out; i = (i + 1) % data.length)
7478
if(data[i] != null)
75-
throw new QueueException(
76-
"non-null outside of queue range: " + dump());
79+
throw new CircularQueueException(
80+
"non-null outside of CircularQueue range: "
81+
+ dump());
7782
return true;
7883
}
7984
public String dump() {
8085
return "in = " + in +
8186
", out = " + out +
8287
", full() = " + full() +
8388
", empty() = " + empty() +
84-
", queue = " + Arrays.asList(data);
89+
", CircularQueue = " + Arrays.asList(data);
8590
}
8691
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
// validating/QueueException.java
1+
// validating/CircularQueueException.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
package validating;
66

7-
public class QueueException extends RuntimeException {
8-
public QueueException(String why) { super(why); }
7+
public class
8+
CircularQueueException extends RuntimeException {
9+
public CircularQueueException(String why) {
10+
super(why);
11+
}
912
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// validating/tests/CircularQueueTest.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package validating;
6+
import org.junit.jupiter.api.*;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
public class CircularQueueTest {
10+
private CircularQueue queue = new CircularQueue(10);
11+
private int i = 0;
12+
@BeforeEach
13+
public void initialize() {
14+
while(i < 5) // Preload with some data
15+
queue.put(Integer.toString(i++));
16+
}
17+
// Support methods:
18+
private void showFullness() {
19+
assertTrue(queue.full());
20+
assertFalse(queue.empty());
21+
System.out.println(queue.dump());
22+
}
23+
private void showEmptiness() {
24+
assertFalse(queue.full());
25+
assertTrue(queue.empty());
26+
System.out.println(queue.dump());
27+
}
28+
@Test
29+
public void full() {
30+
System.out.println("testFull");
31+
System.out.println(queue.dump());
32+
System.out.println(queue.get());
33+
System.out.println(queue.get());
34+
while(!queue.full())
35+
queue.put(Integer.toString(i++));
36+
String msg = "";
37+
try {
38+
queue.put("");
39+
} catch(CircularQueueException e) {
40+
msg = e.getMessage();
41+
System.out.println(msg);
42+
}
43+
assertEquals(msg, "put() into full CircularQueue");
44+
showFullness();
45+
}
46+
@Test
47+
public void empty() {
48+
System.out.println("testEmpty");
49+
while(!queue.empty())
50+
System.out.println(queue.get());
51+
String msg = "";
52+
try {
53+
queue.get();
54+
} catch(CircularQueueException e) {
55+
msg = e.getMessage();
56+
System.out.println(msg);
57+
}
58+
assertEquals(msg, "get() from empty CircularQueue");
59+
showEmptiness();
60+
}
61+
@Test
62+
public void nullPut() {
63+
System.out.println("testNullPut");
64+
String msg = "";
65+
try {
66+
queue.put(null);
67+
} catch(CircularQueueException e) {
68+
msg = e.getMessage();
69+
System.out.println(msg);
70+
}
71+
assertEquals(msg, "put() null item");
72+
}
73+
@Test
74+
public void circularity() {
75+
System.out.println("testCircularity");
76+
while(!queue.full())
77+
queue.put(Integer.toString(i++));
78+
showFullness();
79+
assertTrue(queue.isWrapped());
80+
while(!queue.empty())
81+
System.out.println(queue.get());
82+
showEmptiness();
83+
while(!queue.full())
84+
queue.put(Integer.toString(i++));
85+
showFullness();
86+
while(!queue.empty())
87+
System.out.println(queue.get());
88+
showEmptiness();
89+
}
90+
}
91+
/* Output:
92+
testNullPut
93+
put() null item
94+
testCircularity
95+
in = 0, out = 0, full() = true, empty() = false, CircularQueue =
96+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
97+
0
98+
1
99+
2
100+
3
101+
4
102+
5
103+
6
104+
7
105+
8
106+
9
107+
in = 0, out = 0, full() = false, empty() = true, CircularQueue =
108+
[null, null, null, null, null, null, null, null, null,
109+
null]
110+
in = 0, out = 0, full() = true, empty() = false, CircularQueue =
111+
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
112+
10
113+
11
114+
12
115+
13
116+
14
117+
15
118+
16
119+
17
120+
18
121+
19
122+
in = 0, out = 0, full() = false, empty() = true, CircularQueue =
123+
[null, null, null, null, null, null, null, null, null,
124+
null]
125+
testFull
126+
in = 5, out = 0, full() = false, empty() = false, CircularQueue =
127+
[0, 1, 2, 3, 4, null, null, null, null, null]
128+
0
129+
1
130+
put() into full CircularQueue
131+
in = 2, out = 2, full() = true, empty() = false, CircularQueue =
132+
[10, 11, 2, 3, 4, 5, 6, 7, 8, 9]
133+
testEmpty
134+
0
135+
1
136+
2
137+
3
138+
4
139+
get() from empty CircularQueue
140+
in = 5, out = 5, full() = false, empty() = true, CircularQueue =
141+
[null, null, null, null, null, null, null, null, null,
142+
null]
143+
*/

validating/tests/CountedListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void replace() {
4646
assertEquals(list.get(1), "Replace");
4747
}
4848
// A helper method to simplify the code. As
49-
// long as it isn't annotated with @Test, it will
49+
// long as it's not annotated with @Test, it will
5050
// not be automatically executed by JUnit.
5151
private
5252
void compare(List<String> lst, String[] strs) {

0 commit comments

Comments
 (0)