Compare commits

...

4 Commits

Author SHA1 Message Date
Robinson efcdbf9559
Version 1.47 2023-09-14 17:54:29 +02:00
Robinson e13205166a
updated unit tests 2023-09-14 17:54:10 +02:00
Robinson 0ce4bb2e2e
Rename .java to .kt 2023-09-14 17:54:10 +02:00
Robinson dc1bfb8371
Cleaned up getTimePretty() output 2023-09-14 17:54:00 +02:00
6 changed files with 392 additions and 296 deletions

View File

@ -17,7 +17,7 @@ Maven Info
<dependency>
<groupId>com.dorkbox</groupId>
<artifactId>Utilities</artifactId>
<version>1.46</version>
<version>1.47</version>
</dependency>
</dependencies>
```
@ -27,7 +27,7 @@ Gradle Info
```
dependencies {
...
compile "com.dorkbox:Utilities:1.46"
compile "com.dorkbox:Utilities:1.47"
}
```

View File

@ -35,7 +35,7 @@ object Extras {
// set for the project
const val description = "Utilities for use within Java projects"
const val group = "com.dorkbox"
const val version = "1.46"
const val version = "1.47"
// set as project.ext
const val name = "Utilities"

View File

@ -26,7 +26,7 @@ object Sys {
/**
* Gets the version number.
*/
val version = "1.46"
val version = "1.47"
init {
// Add this project to the updates system, which verifies this class + UUID + version information
@ -117,22 +117,22 @@ object Sys {
fun getTimePretty(nanoSeconds: Long): String {
val unit: TimeUnit
val text: String
if (TimeUnit.DAYS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
if (TimeUnit.DAYS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.DAYS
text = "d"
} else if (TimeUnit.HOURS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.HOURS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.HOURS
text = "h"
} else if (TimeUnit.MINUTES.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.MINUTES.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.MINUTES
text = "min"
} else if (TimeUnit.SECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
text = "m"
} else if (TimeUnit.SECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.SECONDS
text = "s"
} else if (TimeUnit.MILLISECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.MILLISECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.MILLISECONDS
text = "ms"
} else if (TimeUnit.MICROSECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.MICROSECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.MICROSECONDS
text = "\u03bcs" // μs
} else {
@ -142,7 +142,16 @@ object Sys {
// convert the unit into the largest time unit possible (since that is often what makes sense)
val value = nanoSeconds.toDouble() / TimeUnit.NANOSECONDS.convert(1, unit)
return String.format("%.4g$text", value)
return if (value < 10) {
String.format("%.1g $text", value)
} else if (value < 100) {
String.format("%.2g $text", value)
} else if (value < 1000) {
String.format("%.3g $text", value)
} else {
String.format("%.4g $text", value)
}
}
/**
@ -151,22 +160,22 @@ object Sys {
fun getTimePrettyFull(nanoSeconds: Long): String {
val unit: TimeUnit
var text: String
if (TimeUnit.DAYS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
if (TimeUnit.DAYS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.DAYS
text = "day"
} else if (TimeUnit.HOURS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.HOURS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.HOURS
text = "hour"
} else if (TimeUnit.MINUTES.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.MINUTES.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.MINUTES
text = "minute"
} else if (TimeUnit.SECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.SECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.SECONDS
text = "second"
} else if (TimeUnit.MILLISECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.MILLISECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.MILLISECONDS
text = "milli-second"
} else if (TimeUnit.MICROSECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0) {
} else if (TimeUnit.MICROSECONDS.convert(nanoSeconds, TimeUnit.NANOSECONDS) > 0L) {
unit = TimeUnit.MICROSECONDS
text = "micro-second"
} else {
@ -179,7 +188,16 @@ object Sys {
if (value > 1.0) {
text += "s"
}
return String.format("%.4g $text", value)
return if (value < 10) {
String.format("%.1g $text", value)
} else if (value < 100) {
String.format("%.2g $text", value)
} else if (value < 1000) {
String.format("%.3g $text", value)
} else {
String.format("%.4g $text", value)
}
}
private fun <T : Throwable> throwException0(t: Throwable) {

View File

@ -1,277 +0,0 @@
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util;
import java.io.IOException;
import java.util.Random;
import org.junit.Test;
public class MersenneTwisterFastTest {
@Test
public void mersenneTwisterTest() throws IOException {
int j;
MersenneTwisterFast r;
// CORRECTNESS TEST
// COMPARE WITH
// http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.out
r = new MersenneTwisterFast(new int[] {0x123,0x234,0x345,0x456});
// System.out.println("Output of MersenneTwisterFast with new (2002/1/26) seeding mechanism");
for (j = 0; j < 1000; j++) {
// first, convert the int from signed to "unsigned"
long l = r.nextInt();
if (l < 0) {
l += 4294967296L; // max int value
}
String s = String.valueOf(l);
while (s.length() < 10) {
s = " " + s; // buffer
}
System.out.print(s + " ");
if (j % 5 == 4) {
System.out.println();
}
}
// SPEED TEST
final long SEED = 4357;
int xx;
long ms;
System.out.println("\nTime to test grabbing 100000000 ints");
Random rr = new Random(SEED);
xx = 0;
ms = System.currentTimeMillis();
for (j = 0; j < 100000000; j++) {
xx += rr.nextInt();
}
System.out.println("java.util.Random: " + (System.currentTimeMillis() - ms) + " Ignore this: " + xx);
r = new MersenneTwisterFast(SEED);
ms = System.currentTimeMillis();
xx = 0;
for (j = 0; j < 100000000; j++) {
xx += r.nextInt();
}
System.out.println("Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: "
+ xx);
// TEST TO COMPARE TYPE CONVERSION BETWEEN
// MersenneTwisterFast.java AND MersenneTwister.java
boolean test = false;
System.out.println("\nGrab the first 1000 booleans");
ms = System.currentTimeMillis();
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
// System.out.print(r.nextBoolean() + " ");
test = r.nextBoolean();
if (j % 8 == 7) {
// System.out.println();
test = false;
}
}
if (!(j % 8 == 7)) {
// System.out.println();
test = true;
}
System.out.println("Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: "
+ xx + "" + test);
System.out.println("\nGrab 1000 booleans of increasing probability using nextBoolean(double)");
r = new MersenneTwisterFast(SEED);
ms = System.currentTimeMillis();
for (j = 0; j < 1000; j++) {
// System.out.print(r.nextBoolean(j / 999.0) + " ");
test = r.nextBoolean(j / 999.0);
if (j % 8 == 7) {
// System.out.println();
test = false;
}
}
if (!(j % 8 == 7)) {
// System.out.println();
test = true;
}
System.out.println("Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: "
+ xx + "" + test);
System.out.println("\nGrab 1000 booleans of increasing probability using nextBoolean(float)");
r = new MersenneTwisterFast(SEED);
ms = System.currentTimeMillis();
for (j = 0; j < 1000; j++) {
// System.out.print(r.nextBoolean(j / 999.0f) + " ");
test = r.nextBoolean(j / 999.0f);
if (j % 8 == 7) {
test = false;
System.out.println();
}
}
if (!(j % 8 == 7)) {
// System.out.println();
test = true;
}
System.out.println("Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: "
+ xx + "" + test);
byte[] bytes = new byte[1000];
System.out.println("\nGrab the first 1000 bytes using nextBytes");
r = new MersenneTwisterFast(SEED);
r.nextBytes(bytes);
for (j = 0; j < 1000; j++) {
System.out.print(bytes[j] + " ");
if (j % 16 == 15) {
System.out.println();
}
}
if (!(j % 16 == 15)) {
System.out.println();
}
byte b;
System.out.println("\nGrab the first 1000 bytes -- must be same as nextBytes");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print((b = r.nextByte()) + " ");
if (b != bytes[j]) {
System.out.print("BAD ");
}
if (j % 16 == 15) {
System.out.println();
}
}
if (!(j % 16 == 15)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 shorts");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print(r.nextShort() + " ");
if (j % 8 == 7) {
System.out.println();
}
}
if (!(j % 8 == 7)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 ints");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print(r.nextInt() + " ");
if (j % 4 == 3) {
System.out.println();
}
}
if (!(j % 4 == 3)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 ints of different sizes");
r = new MersenneTwisterFast(SEED);
int max = 1;
for (j = 0; j < 1000; j++) {
System.out.print(r.nextInt(max) + " ");
max *= 2;
if (max <= 0) {
max = 1;
}
if (j % 4 == 3) {
System.out.println();
}
}
if (!(j % 4 == 3)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 longs");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print(r.nextLong() + " ");
if (j % 3 == 2) {
System.out.println();
}
}
if (!(j % 3 == 2)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 longs of different sizes");
r = new MersenneTwisterFast(SEED);
long max2 = 1;
for (j = 0; j < 1000; j++) {
System.out.print(r.nextLong(max2) + " ");
max2 *= 2;
if (max2 <= 0) {
max2 = 1;
}
if (j % 4 == 3) {
System.out.println();
}
}
if (!(j % 4 == 3)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 floats");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print(r.nextFloat() + " ");
if (j % 4 == 3) {
System.out.println();
}
}
if (!(j % 4 == 3)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 doubles");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print(r.nextDouble() + " ");
if (j % 3 == 2) {
System.out.println();
}
}
if (!(j % 3 == 2)) {
System.out.println();
}
System.out.println("\nGrab the first 1000 gaussian doubles");
r = new MersenneTwisterFast(SEED);
for (j = 0; j < 1000; j++) {
System.out.print(r.nextGaussian() + " ");
if (j % 3 == 2) {
System.out.println();
}
}
if (!(j % 3 == 2)) {
System.out.println();
}
}
}

View File

@ -0,0 +1,292 @@
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util
import org.junit.Test
import java.io.IOException
import java.util.*
class MersenneTwisterFastTest {
@Test
@Throws(IOException::class)
fun mersenneTwisterTest() {
var j: Int
var r: MersenneTwisterFast
// CORRECTNESS TEST
// COMPARE WITH
// http://www.math.keio.ac.jp/matumoto/CODES/MT2002/mt19937ar.out
r = MersenneTwisterFast(intArrayOf(0x123, 0x234, 0x345, 0x456))
// System.out.println("Output of MersenneTwisterFast with new (2002/1/26) seeding mechanism");
j = 0
while (j < 1000) {
// first, convert the int from signed to "unsigned"
var l = r.nextInt().toLong()
if (l < 0) {
l += 4294967296L // max int value
}
var s = l.toString()
while (s.length < 10) {
s = " $s" // buffer
}
print("$s ")
if (j % 5 == 4) {
println()
}
j++
}
// SPEED TEST
val SEED: Long = 4357
var xx: Int
var ms: Long
println("\nTime to test grabbing 100000000 ints")
val rr = Random(SEED)
xx = 0
ms = System.currentTimeMillis()
j = 0
while (j < 100000000) {
xx += rr.nextInt()
j++
}
println("java.util.Random: " + (System.currentTimeMillis() - ms) + " Ignore this: " + xx)
r = MersenneTwisterFast(SEED)
ms = System.currentTimeMillis()
xx = 0
j = 0
while (j < 100000000) {
xx += r.nextInt()
j++
}
println(
"Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: " + xx
)
// TEST TO COMPARE TYPE CONVERSION BETWEEN
// MersenneTwisterFast.java AND MersenneTwister.java
var test = false
println("\nGrab the first 1000 booleans")
ms = System.currentTimeMillis()
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
// System.out.print(r.nextBoolean() + " ");
test = r.nextBoolean()
if (j % 8 == 7) {
// System.out.println();
test = false
}
j++
}
if (j % 8 != 7) {
// System.out.println();
test = true
}
println(
"Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: " + xx + "" + test
)
println("\nGrab 1000 booleans of increasing probability using nextBoolean(double)")
r = MersenneTwisterFast(SEED)
ms = System.currentTimeMillis()
j = 0
while (j < 1000) {
// System.out.print(r.nextBoolean(j / 999.0) + " ");
test = r.nextBoolean(j / 999.0)
if (j % 8 == 7) {
// System.out.println();
test = false
}
j++
}
if (j % 8 != 7) {
// System.out.println();
test = true
}
println(
"Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: " + xx + "" + test
)
println("\nGrab 1000 booleans of increasing probability using nextBoolean(float)")
r = MersenneTwisterFast(SEED)
ms = System.currentTimeMillis()
j = 0
while (j < 1000) {
// System.out.print(r.nextBoolean(j / 999.0f) + " ");
test = r.nextBoolean(j / 999.0f)
if (j % 8 == 7) {
test = false
println()
}
j++
}
if (j % 8 != 7) {
// System.out.println();
test = true
}
println(
"Mersenne Twister Fast: " + (System.currentTimeMillis() - ms) + " Ignore this: " + xx + "" + test
)
val bytes = ByteArray(1000)
println("\nGrab the first 1000 bytes using nextBytes")
r = MersenneTwisterFast(SEED)
r.nextBytes(bytes)
j = 0
while (j < 1000) {
print(bytes[j].toString() + " ")
if (j % 16 == 15) {
println()
}
j++
}
if (j % 16 != 15) {
println()
}
var b: Byte
println("\nGrab the first 1000 bytes -- must be same as nextBytes")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextByte().also { b = it }.toString() + " ")
if (b != bytes[j]) {
print("BAD ")
}
if (j % 16 == 15) {
println()
}
j++
}
if (j % 16 != 15) {
println()
}
println("\nGrab the first 1000 shorts")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextShort().toString() + " ")
if (j % 8 == 7) {
println()
}
j++
}
if (j % 8 != 7) {
println()
}
println("\nGrab the first 1000 ints")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextInt().toString() + " ")
if (j % 4 == 3) {
println()
}
j++
}
if (j % 4 != 3) {
println()
}
println("\nGrab the first 1000 ints of different sizes")
r = MersenneTwisterFast(SEED)
var max = 1
j = 0
while (j < 1000) {
print(r.nextInt(max).toString() + " ")
max *= 2
if (max <= 0) {
max = 1
}
if (j % 4 == 3) {
println()
}
j++
}
if (j % 4 != 3) {
println()
}
println("\nGrab the first 1000 longs")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextLong().toString() + " ")
if (j % 3 == 2) {
println()
}
j++
}
if (j % 3 != 2) {
println()
}
println("\nGrab the first 1000 longs of different sizes")
r = MersenneTwisterFast(SEED)
var max2: Long = 1
j = 0
while (j < 1000) {
print(r.nextLong(max2).toString() + " ")
max2 *= 2
if (max2 <= 0) {
max2 = 1
}
if (j % 4 == 3) {
println()
}
j++
}
if (j % 4 != 3) {
println()
}
println("\nGrab the first 1000 floats")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextFloat().toString() + " ")
if (j % 4 == 3) {
println()
}
j++
}
if (j % 4 != 3) {
println()
}
println("\nGrab the first 1000 doubles")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextDouble().toString() + " ")
if (j % 3 == 2) {
println()
}
j++
}
if (j % 3 != 2) {
println()
}
println("\nGrab the first 1000 gaussian doubles")
r = MersenneTwisterFast(SEED)
j = 0
while (j < 1000) {
print(r.nextGaussian().toString() + " ")
if (j % 3 == 2) {
println()
}
j++
}
if (j % 3 != 2) {
println()
}
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util
import org.junit.Assert
import org.junit.Test
import java.util.concurrent.*
class TimeTest {
@Test
fun time() {
TimeUnit.DAYS.toNanos(3).also {
Assert.assertEquals("3 days", Sys.getTimePrettyFull(it))
}
TimeUnit.DAYS.toNanos(30).also {
Assert.assertEquals("30 days", Sys.getTimePrettyFull(it))
}
TimeUnit.DAYS.toNanos(300).also {
Assert.assertEquals("300 days", Sys.getTimePrettyFull(it))
}
TimeUnit.DAYS.toNanos(3000).also {
Assert.assertEquals("3000 days", Sys.getTimePrettyFull(it))
}
TimeUnit.HOURS.toNanos(3).also {
Assert.assertEquals("3 hours", Sys.getTimePrettyFull(it))
}
TimeUnit.MINUTES.toNanos(3).also {
Assert.assertEquals("3 minutes", Sys.getTimePrettyFull(it))
}
TimeUnit.SECONDS.toNanos(3).also {
Assert.assertEquals("3 seconds", Sys.getTimePrettyFull(it))
}
TimeUnit.MILLISECONDS.toNanos(3).also {
Assert.assertEquals("3 milli-seconds", Sys.getTimePrettyFull(it))
}
TimeUnit.MICROSECONDS.toNanos(3).also {
Assert.assertEquals("3 micro-seconds", Sys.getTimePrettyFull(it))
}
TimeUnit.NANOSECONDS.toNanos(3).also {
Assert.assertEquals("3 nano-seconds", Sys.getTimePrettyFull(it))
}
TimeUnit.NANOSECONDS.toNanos(1).also {
Assert.assertEquals("1 nano-second", Sys.getTimePrettyFull(it))
}
}
}