Commit 5b9733e5 authored by Stephanie Gawroriski's avatar Stephanie Gawroriski
Browse files

Pull in sorting and corrections as well.

parent 5963046f
Pipeline #39157 passed with stages
in 1 minute and 33 seconds
......@@ -327,7 +327,6 @@ public class Arrays
* @throws NullPointerException On null arguments.
* @since 2018/11/04
*/
@SuppressWarnings({"SuspiciousSystemArraycopy"})
public static <T, U> T[] copyOf(U[] __src, int __newLen,
Class<? extends T[]> __newType)
throws ArrayStoreException, NegativeArraySizeException,
......@@ -342,8 +341,8 @@ public class Arrays
T[] rv = ObjectShelf.<T[]>arrayNew(TypeShelf.classToType(__newType),
__newLen);
System.arraycopy(__src, 0, rv, 0,
Math.min(__newLen, __src.length));
System.arraycopy(__src, 0,
rv, 0, Math.min(__newLen, __src.length));
return rv;
}
......
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package squirreljme.util;
import cc.squirreljme.runtime.cldc.util.ShellSort;
import net.multiphasicapps.tac.TestRunnable;
/**
* Test that the {@link ShellSort} gaps make sense.
*
* @since 2021/07/12
*/
public class TestShellSortGaps
extends TestRunnable
{
/** Gaps used in shell sort, used as a base to determine the gap size. */
private static final int[] _GAPS =
new int[]{1750, 701, 301, 132, 57, 23, 10, 4, 1};
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@Override
public void test()
throws Throwable
{
int[] gaps = TestShellSortGaps._GAPS;
for (int i = 0, n = gaps.length; i < n; i++)
{
int len = gaps[i];
for (int d = -1; d <= 1; d++)
this.secondary(String.format("%04d", len + d),
ShellSort.gaps(len + d));
}
}
}
......@@ -40,15 +40,15 @@ public class TestSorting
@Override
public void test()
{
for (int c = 0, cn = _COUNTS.length; c < cn; c++)
for (int c = 0, cn = TestSorting._COUNTS.length; c < cn; c++)
{
Random rand;
int count = _COUNTS[c];
int count = TestSorting._COUNTS[c];
String cid = String.format("%02d", count);
// Byte
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
byte[] ab = new byte[count];
for (int i = 0; i < count; i++)
ab[i] = (byte)rand.nextInt();
......@@ -56,7 +56,7 @@ public class TestSorting
this.secondary("arraybyte" + cid, ab);
// Short
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
short[] as = new short[count];
for (int i = 0; i < count; i++)
as[i] = (short)rand.nextInt();
......@@ -64,7 +64,7 @@ public class TestSorting
this.secondary("arrayshort" + cid, as);
// Char
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
char[] ac = new char[count];
for (int i = 0; i < count; i++)
ac[i] = (char)rand.nextInt();
......@@ -72,7 +72,7 @@ public class TestSorting
this.secondary("arraychar" + cid, ac);
// Integer
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
int[] ai = new int[count];
for (int i = 0; i < count; i++)
ai[i] = rand.nextInt();
......@@ -80,7 +80,7 @@ public class TestSorting
this.secondary("arrayint" + cid, ai);
// Object (just integers)
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
Integer[] ao = new Integer[count];
for (int i = 0; i < count; i++)
ao[i] = rand.nextInt();
......@@ -88,7 +88,7 @@ public class TestSorting
this.secondary("arrayobject" + cid, ao);
// Array List
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
List<Integer> la = new ArrayList<>(count);
for (int i = 0; i < count; i++)
la.add(rand.nextInt());
......@@ -97,7 +97,7 @@ public class TestSorting
la.<Integer>toArray(new Integer[count]));
// Linked List
rand = new Random(SEED);
rand = new Random(TestSorting.SEED);
List<Integer> ll = new LinkedList<>();
for (int i = 0; i < count; i++)
ll.add(rand.nextInt());
......
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package util;
import cc.squirreljme.runtime.cldc.debug.Debugging;
import cc.squirreljme.runtime.cldc.util.IntegerArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import net.multiphasicapps.tac.TestRunnable;
/**
* Tests sorting a large number of items, determined by random.
*
* @since 2021/07/12
*/
public class TestSortingLots
extends TestRunnable
{
/** The number of items to test. */
public static final int NUM_ITEMS =
4321;
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@SuppressWarnings("CheckForOutOfMemoryOnLargeArrayAllocation")
@Override
public void test()
throws Throwable
{
// Use random seed, but not a fixed one so this has unique data
// every time!
Random rand = new Random(System.currentTimeMillis() ^
System.nanoTime());
// Test with primitive integers first
int[] ints = new int[TestSortingLots.NUM_ITEMS];
for (int i = 0; i < TestSortingLots.NUM_ITEMS; i++)
ints[i] = rand.nextInt();
// Do sort!
long intsStart = System.nanoTime();
Arrays.sort(ints);
Debugging.debugNote("ints: %d in %dms",
TestSortingLots.NUM_ITEMS,
(System.nanoTime() - intsStart) / 1_000_000L);
// Make sure everything is sorted
this.secondary("ints",
TestSortingLots.isSorted(new IntegerArrayList(ints)));
// Then with integer objects
Integer[] objects = new Integer[TestSortingLots.NUM_ITEMS];
for (int i = 0; i < TestSortingLots.NUM_ITEMS; i++)
objects[i] = rand.nextInt();
// Do sort!
long objectsStart = System.nanoTime();
Arrays.sort(objects);
Debugging.debugNote("objects: %d in %dms",
TestSortingLots.NUM_ITEMS,
(System.nanoTime() - objectsStart) / 1_000_000L);
// Make sure is sorted!
this.secondary("objects",
TestSortingLots.isSorted(Arrays.asList(objects)));
}
/**
* Checks if this list is sorted.
*
* @param __list The list to check.
* @return If this is sorted.
* @since 2021/07/12
*/
public static boolean isSorted(List<Integer> __list)
{
// Each later number must be bigger than the earlier number
int last = Integer.MIN_VALUE;
for (int i = 0, n = __list.size(); i < n; i++)
{
int now = __list.get(i);
if (now < last)
return false;
last = now;
}
// Must be sorted
return true;
}
}
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
package util;
import cc.squirreljme.runtime.cldc.debug.Debugging;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import net.multiphasicapps.tac.TestRunnable;
/**
* Tests that sorting is stable, as required by Java.
*
* @since 2021/07/12
*/
public class TestStableSort
extends TestRunnable
{
/** The number of items to sort. */
public static final int COUNT =
128;
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@SuppressWarnings("RedundantCollectionOperation")
@Override
public void test()
throws Throwable
{
Random random = new Random(0xDEAD_BEEFL);
// Setup a bunch of items
List<Item> original = new ArrayList<>(TestStableSort.COUNT);
Set<Integer> taken = new HashSet<>();
for (int i = 0; i < TestStableSort.COUNT; i++)
{
// We could put extra values in order but that would not be
// a good test since it would be very linear and not random.
int extra;
for (;;)
{
// Make every value in here unique!
extra = random.nextInt(TestStableSort.COUNT);
if (taken.add(extra))
break;
}
// Add the item to be sorted
original.add(random.nextInt(1 + i),
new Item(i / 3, extra));
}
// Make as an array
Item[] array = original.toArray(new Item[original.size()]);
Item[] list = array.clone();
// Sort the lists
Arrays.sort(array);
Collections.sort(Arrays.asList(list));
// These must be equal
this.secondary("equal", Arrays.equals(array, list));
// Extra insurance to make sure everything is as expected
this.secondary("sorted",
TestStableSort.isSorted(Arrays.asList(array)));
// Extract all the extra values, which should be in a specific order
int[] extra = new int[TestStableSort.COUNT];
for (int i = 0; i < TestStableSort.COUNT; i++)
extra[i] = array[i].extra;
// Report these values
this.secondary("extra", extra);
}
/**
* Checks if this list is sorted.
*
* @param __list The list to check.
* @return If this is sorted.
* @since 2021/07/12
*/
public static boolean isSorted(List<Item> __list)
{
// Each later number must be bigger than the earlier number
Item last = null;
for (int i = 0, n = __list.size(); i < n; i++)
{
Item now = __list.get(i);
if (last != null && now.compareTo(last) < 0)
return false;
last = now;
}
// Must be sorted
return true;
}
/**
* The items to be check.
*
* @since 2021/07/12
*/
public static class Item
implements Comparable<Item>
{
/** The sorted value. */
public final int sorted;
/** The extra value. */
public final int extra;
/**
* Initializes the item.
*
* @param __sorted The sorted item.
* @param __extra The extra item.
* @since 2021/07/12
*/
public Item(int __sorted, int __extra)
{
this.sorted = __sorted;
this.extra = __extra;
}
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@Override
public int compareTo(Item __o)
{
return __o.sorted - this.sorted;
}
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@Override
public boolean equals(Object __o)
{
if (this == __o)
return true;
if (!(__o instanceof Item))
return false;
Item o = (Item)__o;
return this.sorted == o.sorted &&
this.extra == o.extra;
}
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@Override
public int hashCode()
{
return this.sorted ^ (~this.extra);
}
/**
* {@inheritDoc}
* @since 2021/07/12
*/
@Override
public String toString()
{
return String.format("[%d|%d]", this.sorted, this.extra);
}
}
}
// ---------------------------------------------------------------------------
// Multi-Phasic Applications: SquirrelJME
// Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------
/**
* Generic utilities test.
*
* @since 2021/07/12
*/
package util;
Manifest-Version: 1.0
result: NoResult
thrown: NoExceptionThrown
secondary-0000: int[1]:1
secondary-0001: int[1]:1
secondary-0002: int[1]:1
secondary-0003: int[1]:1
secondary-0004: int[1]:1
secondary-0005: int[2]:4,1
secondary-0009: int[2]:4,1
secondary-0010: int[2]:4,1
secondary-0011: int[3]:10,4,1
secondary-0022: int[3]:10,4,1
secondary-0023: int[3]:10,4,1
secondary-0024: int[4]:23,10,4,1
secondary-0056: int[4]:23,10,4,1
secondary-0057: int[4]:23,10,4,1
secondary-0058: int[5]:57,23,10,4,1
secondary-0131: int[5]:57,23,10,4,1
secondary-0132: int[5]:57,23,10,4,1
secondary-0133: int[6]:132,57,23,10,4,1
secondary-0300: int[6]:132,57,23,10,4,1
secondary-0301: int[6]:132,57,23,10,4,1
secondary-0302: int[7]:301,132,57,23,10,4,1
secondary-0700: int[7]:301,132,57,23,10,4,1
secondary-0701: int[7]:301,132,57,23,10,4,1
secondary-0702: int[8]:701,301,132,57,23,10,4,1
secondary-1749: int[8]:701,301,132,57,23,10,4,1
secondary-1750: int[8]:701,301,132,57,23,10,4,1
secondary-1751: int[9]:1750,701,301,132,57,23,10,4,1
......@@ -150,7 +150,7 @@ secondary-eldest99key: int:39
secondary-eldest99val: string:Value39
secondary-eldest9key: int:0
secondary-eldest9val: string:Value0
secondary-ordkeys: int[10]:40,41,42,43,44,45,46,47,48,49
secondary-ordkeys: int*[10]:40,41,42,43,44,45,46,47,48,49
secondary-ordvals: string[10]:Value40,Value41,Value42,Value43,Value44,
Value45,Value46,Value47,Value48,Value49
......@@ -35,17 +35,17 @@ secondary-arrayint26: int[26]:-2145218123,-1872816220,-1574936737,-144
21,505839579,823057922,930275108,1068715060,1094656688,1178377885,1543
966882,1677212580,1754641508,1827465615,1851145775,1962592967,20417093
29,2054142522,2092500449,2105985923
secondary-arrayobject01: int[1]:-1442945365
secondary-arrayobject02: int[2]:-1442945365,-1016548095
secondary-arrayobject03: int[3]:-1442945365,-1016548095,1962592967
secondary-arrayobject04: int[4]:-1442945365,-1016548095,1094656688,196
secondary-arrayobject01: int*[1]:-1442945365
secondary-arrayobject02: int*[2]:-1442945365,-1016548095
secondary-arrayobject03: int*[3]:-1442945365,-1016548095,1962592967
secondary-arrayobject04: int*[4]:-1442945365,-1016548095,1094656688,196
2592967
secondary-arrayobject08: int[8]:-1442945365,-1016548095,-458096230,930
secondary-arrayobject08: int*[8]:-1442945365,-1016548095,-458096230,930
275108,1094656688,1677212580,1827465615,1962592967
secondary-arrayobject13: int[13]:-1442945365,-1075280701,-1016548095,-
secondary-arrayobject13: int*[13]:-1442945365,-1075280701,-1016548095,-
458096230,930275108,1094656688,1543966882,1677212580,1827465615,185114
5775,1962592967,2041709329,2105985923
secondary-arrayobject26: int[26]:-2145218123,-1872816220,-1574936737,-
secondary-arrayobject26: int*[26]:-2145218123,-1872816220,-1574936737,-
1442945365,-1075280701,-1016548095,-458096230,-223996045,68659964,4926
38721,505839579,823057922,930275108,1068715060,1094656688,1178377885,1
543966882,1677212580,1754641508,1827465615,1851145775,1962592967,20417
......@@ -61,32 +61,32 @@ secondary-arrayshort13: short[13]:-31549,-19199,-13625,-13437,-8412,-5
secondary-arrayshort26: short[26]:-31549,-28235,-24931,-21764,-19356,-
19199,-17862,-13625,-13437,-8702,-8412,-5745,410,785,1505,4258,4609,60
03,6052,8880,15268,15919,19508,24415,26283,32731
secondary-listarray01: int[1]:-1442945365
secondary-listarray02: int[2]:-1442945365,-1016548095
secondary-listarray03: int[3]:-1442945365,-1016548095,1962592967
secondary-listarray04: int[4]:-1442945365,-1016548095,1094656688,19625
secondary-listarray01: int*[1]:-1442945365
secondary-listarray02: int*[2]:-1442945365,-1016548095
secondary-listarray03: int*[3]:-1442945365,-1016548095,1962592967
secondary-listarray04: int*[4]:-1442945365,-1016548095,1094656688,19625
92967
secondary-listarray08: int[8]:-1442945365,-1016548095,-458096230,93027
secondary-listarray08: int*[8]:-1442945365,-1016548095,-458096230,93027
5108,1094656688,1677212580,1827465615,1962592967
secondary-listarray13: int[13]:-1442945365,-1075280701,-1016548095,-45
secondary-listarray13: int*[13]:-1442945365,-1075280701,-1016548095,-45
8096230,930275108,1094656688,1543966882,1677212580,1827465615,18511457
75,1962592967,2041709329,2105985923
secondary-listarray26: int[26]:-2145218123,-1872816220,-1574936737,-14
secondary-listarray26: int*[26]:-2145218123,-1872816220,-1574936737,-14
42945365,-1075280701,-1016548095,-458096230,-223996045,68659964,492638
721,505839579,823057922,930275108,1068715060,1094656688,1178377885,154
3966882,1677212580,1754641508,1827465615,1851145775,1962592967,2041709
329,2054142522,2092500449,2105985923
secondary-listlinked01: int[1]:-1442945365
secondary-listlinked02: int[2]:-1442945365,-1016548095
secondary-listlinked03: int[3]:-1442945365,-1016548095,1962592967
secondary-listlinked04: int[4]:-1442945365,-1016548095,1094656688,1962
secondary-listlinked01: int*[1]:-1442945365
secondary-listlinked02: int*[2]:-1442945365,-1016548095
secondary-listlinked03: int*[3]:-1442945365,-1016548095,1962592967
secondary-listlinked04: int*[4]:-1442945365,-1016548095,1094656688,1962
592967
secondary-listlinked08: int[8]:-1442945365,-1016548095,-458096230,9302
secondary-listlinked08: int*[8]:-1442945365,-1016548095,-458096230,9302
75108,1094656688,1677212580,1827465615,1962592967
secondary-listlinked13: int[13]:-1442945365,-1075280701,-1016548095,-4
secondary-listlinked13: int*[13]:-1442945365,-1075280701,-1016548095,-4
58096230,930275108,1094656688,1543966882,1677212580,1827465615,1851145
775,1962592967,2041709329,2105985923
secondary-listlinked26: int[26]:-2145218123,-1872816220,-1574936737,-1
secondary-listlinked26: int*[26]:-2145218123,-1872816220,-1574936737,-1
442945365,-1075280701,-1016548095,-458096230,-223996045,68659964,49263
8721,505839579,823057922,930275108,1068715060,1094656688,1178377885,15
43966882,1677212580,1754641508,1827465615,1851145775,1962592967,204170
......
Manifest-Version: 1.0
result: NoResult
thrown: NoExceptionThrown
secondary-ints: true
secondary-objects: true
Manifest-Version: 1.0
result: NoResult
thrown: NoExceptionThrown
secondary-equal: true
secondary-sorted: true
secondary-extra: int[128]:104,109,60,53,42,95,106,101,56,40,74,75,30,63,22,
64,103,59,87,73,118,23,68,12,96,126,120,52,107,5,99,116,92,57,4,15,25,7,11,
102,94,26,100,79,123,105,24,88,78,85,14,50,21,29,83,112,76,19,18,38,114,98,
62,86,54,115,6,0,3,67,91,32,43,1,20,41,27,45,10,34,71,119,9,90,117,72,81,
111,84,49,113,31,46,37,28,65,124,80,2,122,39,13,8,61,127,55,110,58,82,93,
125,35,121,97,89,17,48,66,108,77,47,33,36,51,69,44,70,16
result: NoResult
thrown: NoExceptionThrown
secondary-array: int[17]:1234,200,100,0,1,2,3,4,5,6,7,8,9,300,400,500,600
secondary-array2: int[10]:2,3,4,5,6,7,8,9,300,400
secondary-array: int*[17]:1234,200,100,0,1,2,3,4,5,6,7,8,9,300,400,500,600
secondary-array2: int*[10]:2,3,4,5,6,7,8,9,300,400
secondary-element: int:1
secondary-getfirst: int:200
secondary-getlast: int:400
......@@ -61,13 +61,14 @@ secondary-other--add--9: true
secondary-has14: false
secondary-has500: false
secondary-has7: true
secondary-array3: int[20]:2,3,4,5,6,7,8,9,300,400,0,1,2,3,4,5,6,7,8,9
secondary-array4: int[15]:3,5,7,9,300,400,1,2,3,4,5,6,7,8,9
secondary-array5: int[10]:3,5,7,9,300,400,2,4,6,8
secondary-array6: int[0]:
secondary-arrayover: int[34]:1234,200,100,0,1,2,3,4,5,6,7,8,
9,300,400,500,600,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
secondary-arraygrow: int[17]:1234,200,100,0,1,2,3,4,5,6,7,8,
secondary-array3: int*[20]:2,3,4,5,6,7,8,9,300,400,0,1,2,3,4,5,6,7,8,9
secondary-array4: int*[15]:3,5,7,9,300,400,1,2,3,4,5,6,7,8,9