Name and path changes to help track down {CompileTimeError} issue

This commit is contained in:
Bruce Eckel 2016-08-23 09:33:05 -06:00
parent b947d10cbe
commit e5071c5175
17 changed files with 217 additions and 214 deletions

View File

@ -1,4 +1,4 @@
// arrays/Prefix1.java
// arrays/ParallelPrefix1.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
@ -6,7 +6,7 @@ import java.util.*;
import onjava.*;
import static onjava.ArrayShow.*;
public class Prefix1 {
public class ParallelPrefix1 {
public static void main(String[] args) {
int[] nums = new Count.int_().array(10);
show(nums);

View File

@ -1,4 +1,4 @@
// arrays/Prefix2.java
// arrays/ParallelPrefix2.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
@ -6,7 +6,7 @@ import java.util.*;
import onjava.*;
import static onjava.ArrayShow.*;
public class Prefix2 {
public class ParallelPrefix2 {
public static void main(String[] args) {
String[] strings = new Rand.String(1).array(8);
show(strings);

View File

@ -1,10 +1,10 @@
// arrays/ParallelPrefix.java
// arrays/ParallelPrefix3.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import java.util.*;
public class ParallelPrefix {
public class ParallelPrefix3 {
static final int SIZE = 10_000_000;
public static void main(String[] args) {
long[] nums = new long[SIZE];

View File

@ -1,26 +0,0 @@
// arrays/ParallelSort.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import java.util.*;
import java.time.*;
import onjava.*;
import static onjava.TimeIt.*;
public class ParallelSort {
static final int SZ = 10_000_000;
public static void main(String[] args) {
int[] ia1 = new Rand.int_().array(SZ);
int[] ia2 = Arrays.copyOf(ia1, ia1.length);
System.out.print("sort(): ");
long millis1 = timeIt(() -> Arrays.sort(ia1));
System.out.print("parallelSort(): ");
long millis2 = timeIt(() -> Arrays.parallelSort(ia2));
System.out.println(millis1/millis2);
}
}
/* Output:
sort(): 1237
parallelSort(): 344
3
*/

View File

@ -14,7 +14,7 @@ aSlice = aList[2:4]
print(aSlice) # [3, 4]
class MyList(list): # Inherit from list
# Define a method, 'this' pointer is explicit:
# Define a method; 'this' pointer is explicit:
def getReversed(self):
reversed = self[:] # Copy list using slices
reversed.reverse() # Built-in list method

View File

@ -0,0 +1,25 @@
// arrays/jmh/ParallelSort.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package arrays.jmh;
import java.util.*;
import onjava.*;
import org.openjdk.jmh.annotations.*;
@State(Scope.Thread)
public class ParallelSort {
private long[] la;
@Setup
public void setup() {
la = new Rand.long_().array(100_000);
}
@Benchmark
public void sort() {
Arrays.sort(la);
}
@Benchmark
public void parallelSort() {
Arrays.parallelSort(la);
}
}

View File

@ -229,6 +229,7 @@ subprojects {
// Exclude java sources that will not compile
if (tags.compileTimeError) {
println ">>> Excluding " + file.name
sourceSets.main.java.excludes.add(file.name)
} else {
JavaExec javaTask = null

View File

@ -3,16 +3,15 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import java.util.*;
import static onjava.TimeIt.*;
public class BadMicroBenchmark {
static final int SIZE = 20_000_000;
public static void main(String[] args) {
long[] la = new long[SIZE];
System.out.print("setAll: ");
timeIt(() -> Arrays.setAll(la, n -> n));
Time.it(() -> Arrays.setAll(la, n -> n));
System.out.print("parallelSetAll: ");
timeIt(() -> Arrays.parallelSetAll(la, n -> n));
Time.it(() -> Arrays.parallelSetAll(la, n -> n));
}
}
/* Output:

View File

@ -4,16 +4,15 @@
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Reversing the test order
import java.util.*;
import static onjava.TimeIt.*;
public class BadMicroBenchmark2 {
static final int SIZE = 20_000_000;
public static void main(String[] args) {
long[] la = new long[SIZE];
System.out.print("parallelSetAll: ");
timeIt(() -> Arrays.parallelSetAll(la, n -> n));
Time.it(() -> Arrays.parallelSetAll(la, n -> n));
System.out.print("setAll: ");
timeIt(() -> Arrays.setAll(la, n -> n));
Time.it(() -> Arrays.setAll(la, n -> n));
}
}
/* Output:

View File

@ -4,7 +4,6 @@
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Relying on a common resource
import java.util.*;
import static onjava.TimeIt.*;
public class BadMicroBenchmark3 {
static final int SIZE = 20_000_000;
@ -12,17 +11,17 @@ public class BadMicroBenchmark3 {
long[] la = new long[SIZE];
Random r = new Random();
System.out.print("parallelSetAll: ");
timeIt(() ->
Time.it(() ->
Arrays.parallelSetAll(la, n -> r.nextLong()));
System.out.print("setAll: ");
timeIt(() ->
Time.it(() ->
Arrays.setAll(la, n -> r.nextLong()));
SplittableRandom sr = new SplittableRandom();
System.out.print("parallelSetAll: ");
timeIt(() ->
Time.it(() ->
Arrays.parallelSetAll(la, n -> sr.nextLong()));
System.out.print("setAll: ");
timeIt(() ->
Time.it(() ->
Arrays.setAll(la, n -> sr.nextLong()));
}
}

View File

@ -3,13 +3,8 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Demonstration of Design by Contract (DbC)
// combined with white-box unit testing
package verifying;
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class Queue {
private Object[] data;
@ -33,6 +28,7 @@ public class Queue {
public boolean full() {
return wrapped && in == out;
}
public boolean isWrapped() { return wrapped; }
public void put(Object item) {
precondition(item != null, "put() null item");
precondition(!full(), "put() into full Queue");
@ -84,153 +80,10 @@ public class Queue {
"non-null outside of queue range: " + dump());
return true;
}
private String dump() {
public String dump() {
return "in = " + in +
", out = " + out +
", full() = " + full() +
", empty() = " + empty() +
", queue = " + Arrays.asList(data);
}
// JUnit testing.
// As an inner class, this has access to privates:
public static class WhiteBoxTest {
private Queue queue = new Queue(10);
private int i = 0;
public WhiteBoxTest() {
while(i < 5) // Preload with some data
queue.put(Integer.toString(i++));
}
// Support methods:
private void showFullness() {
assertTrue(queue.full());
assertFalse(queue.empty());
// Dump is private, white-box testing allows access:
System.out.println(queue.dump());
}
private void showEmptiness() {
assertFalse(queue.full());
assertTrue(queue.empty());
System.out.println(queue.dump());
}
@Test
public void full() {
System.out.println("testFull");
System.out.println(queue.dump());
System.out.println(queue.get());
System.out.println(queue.get());
while(!queue.full())
queue.put(Integer.toString(i++));
String msg = "";
try {
queue.put("");
} catch(QueueException e) {
msg = e.getMessage();
System.out.println(msg);
}
assertEquals(msg, "put() into full Queue");
showFullness();
}
@Test
public void empty() {
System.out.println("testEmpty");
while(!queue.empty())
System.out.println(queue.get());
String msg = "";
try {
queue.get();
} catch(QueueException e) {
msg = e.getMessage();
System.out.println(msg);
}
assertEquals(msg, "get() from empty Queue");
showEmptiness();
}
@Test
public void nullPut() {
System.out.println("testNullPut");
String msg = "";
try {
queue.put(null);
} catch(QueueException e) {
msg = e.getMessage();
System.out.println(msg);
}
assertEquals(msg, "put() null item");
}
@Test
public void circularity() {
System.out.println("testCircularity");
while(!queue.full())
queue.put(Integer.toString(i++));
showFullness();
// White-box testing accesses private field:
assertTrue(queue.wrapped);
while(!queue.empty())
System.out.println(queue.get());
showEmptiness();
while(!queue.full())
queue.put(Integer.toString(i++));
showFullness();
while(!queue.empty())
System.out.println(queue.get());
showEmptiness();
}
}
public static void main(String[] args) {
org.junit.runner.JUnitCore.runClasses(
Queue.WhiteBoxTest.class);
}
}
/* Output:
testNullPut
put() null item
testCircularity
in = 0, out = 0, full() = true, empty() = false, queue =
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
1
2
3
4
5
6
7
8
9
in = 0, out = 0, full() = false, empty() = true, queue =
[null, null, null, null, null, null, null, null, null,
null]
in = 0, out = 0, full() = true, empty() = false, queue =
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
10
11
12
13
14
15
16
17
18
19
in = 0, out = 0, full() = false, empty() = true, queue =
[null, null, null, null, null, null, null, null, null,
null]
testFull
in = 5, out = 0, full() = false, empty() = false, queue =
[0, 1, 2, 3, 4, null, null, null, null, null]
0
1
put() into full Queue
in = 2, out = 2, full() = true, empty() = false, queue =
[10, 11, 2, 3, 4, 5, 6, 7, 8, 9]
testEmpty
0
1
2
3
4
get() from empty Queue
in = 5, out = 5, full() = false, empty() = true, queue =
[null, null, null, null, null, null, null, null, null,
null]
*/

144
verifying/QueueTest.java Normal file
View File

@ -0,0 +1,144 @@
// verifying/QueueTest.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package verifying;
import verifying.Queue;
import org.junit.Test;
import static org.junit.Assert.*;
public class QueueTest {
private Queue queue = new Queue(10);
private int i = 0;
@Before
public void initialize() {
while(i < 5) // Preload with some data
queue.put(Integer.toString(i++));
}
// Support methods:
private void showFullness() {
assertTrue(queue.full());
assertFalse(queue.empty());
System.out.println(queue.dump());
}
private void showEmptiness() {
assertFalse(queue.full());
assertTrue(queue.empty());
System.out.println(queue.dump());
}
@Test
public void full() {
System.out.println("testFull");
System.out.println(queue.dump());
System.out.println(queue.get());
System.out.println(queue.get());
while(!queue.full())
queue.put(Integer.toString(i++));
String msg = "";
try {
queue.put("");
} catch(QueueException e) {
msg = e.getMessage();
System.out.println(msg);
}
assertEquals(msg, "put() into full Queue");
showFullness();
}
@Test
public void empty() {
System.out.println("testEmpty");
while(!queue.empty())
System.out.println(queue.get());
String msg = "";
try {
queue.get();
} catch(QueueException e) {
msg = e.getMessage();
System.out.println(msg);
}
assertEquals(msg, "get() from empty Queue");
showEmptiness();
}
@Test
public void nullPut() {
System.out.println("testNullPut");
String msg = "";
try {
queue.put(null);
} catch(QueueException e) {
msg = e.getMessage();
System.out.println(msg);
}
assertEquals(msg, "put() null item");
}
@Test
public void circularity() {
System.out.println("testCircularity");
while(!queue.full())
queue.put(Integer.toString(i++));
showFullness();
assertTrue(queue.isWrapped());
while(!queue.empty())
System.out.println(queue.get());
showEmptiness();
while(!queue.full())
queue.put(Integer.toString(i++));
showFullness();
while(!queue.empty())
System.out.println(queue.get());
showEmptiness();
}
}
/* Output:
testNullPut
put() null item
testCircularity
in = 0, out = 0, full() = true, empty() = false, queue =
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
1
2
3
4
5
6
7
8
9
in = 0, out = 0, full() = false, empty() = true, queue =
[null, null, null, null, null, null, null, null, null,
null]
in = 0, out = 0, full() = true, empty() = false, queue =
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
10
11
12
13
14
15
16
17
18
19
in = 0, out = 0, full() = false, empty() = true, queue =
[null, null, null, null, null, null, null, null, null,
null]
testFull
in = 5, out = 0, full() = false, empty() = false, queue =
[0, 1, 2, 3, 4, null, null, null, null, null]
0
1
put() into full Queue
in = 2, out = 2, full() = true, empty() = false, queue =
[10, 11, 2, 3, 4, 5, 6, 7, 8, 9]
testEmpty
0
1
2
3
4
get() from empty Queue
in = 5, out = 5, full() = false, empty() = true, queue =
[null, null, null, null, null, null, null, null, null,
null]
*/

View File

@ -5,19 +5,25 @@
import org.slf4j.*;
public class SLF4JLevels {
private static final Logger logger =
private static final Logger log =
LoggerFactory.getLogger(SLF4JLevels.class);
public static void main(String[] args) {
logger.trace("level: trace");
logger.debug("level: debug");
logger.info("level: info");
logger.warn("level: warn");
logger.error("level: error");
log.trace("Hello");
log.debug("Logging");
log.info("Using");
log.warn("the SLF4J");
log.error("Facade");
}
}
/* Output:
20:09:34.499 [main] DEBUG SLF4JLevels - level: debug
20:09:34.502 [main] INFO SLF4JLevels - level: info
20:09:34.502 [main] WARN SLF4JLevels - level: warn
20:09:34.502 [main] ERROR SLF4JLevels - level: error
2016-08-22T14:33:01.951
[main] TRACE SLF4JLevels - Hello
2016-08-22T14:33:01.954
[main] DEBUG SLF4JLevels - Logging
2016-08-22T14:33:01.954
[main] INFO SLF4JLevels - Using
2016-08-22T14:33:01.955
[main] WARN SLF4JLevels - the SLF4J
2016-08-22T14:33:01.955
[main] ERROR SLF4JLevels - Facade
*/

View File

@ -5,12 +5,13 @@
import org.slf4j.*;
public class SLF4JLogging {
private static final Logger logger =
private static final Logger log =
LoggerFactory.getLogger(SLF4JLogging.class);
public static void main(String[] args) {
logger.info("hello logging");
log.info("hello logging");
}
}
/* Output:
17:58:42.798 [main] INFO SLF4JLogging - hello logging
2016-08-22T14:42:44.177
[main] INFO SLF4JLogging - hello logging
*/

View File

@ -82,11 +82,11 @@ public class SimpleJUnit {
compare(list, new String[] { "0", "1", "2",
"An", "African", "Swallow" });
}
public static void main(String[] args) {
/* public static void main(String[] args) {
// Invoke JUnit on the class:
org.junit.runner.JUnitCore.runClasses(
SimpleJUnit.class);
}
} */
}
/* Output:
CountedList #0

View File

@ -1,12 +1,11 @@
// onjava/TimeIt.java
// verifying/Time.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package onjava;
import java.util.concurrent.*;
public interface TimeIt {
static long timeIt(Runnable test) {
public interface Time {
static long it(Runnable test) {
long start = System.nanoTime();
test.run();
long delta = System.nanoTime() - start;

View File

@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
<pattern>
%d{yyyy-MM-dd'T'HH:mm:ss.SSS}
[%thread] %-5level %logger - %msg%n
</pattern>
</encoder>
</appender>