Reformat whole codebase using Android Studio

This commit is contained in:
Manuel Fuhr 2022-07-11 06:30:17 +02:00
parent d5322667d5
commit c15913c1ab
161 changed files with 15124 additions and 18537 deletions

View file

@ -3,61 +3,49 @@ package btools.util;
import org.junit.Assert;
import org.junit.Test;
public class BitCoderContextTest
{
public class BitCoderContextTest {
@Test
public void varBitsEncodeDecodeTest()
{
public void varBitsEncodeDecodeTest() {
byte[] ab = new byte[581969];
BitCoderContext ctx = new BitCoderContext( ab );
for ( int i = 0; i < 31; i++ )
{
ctx.encodeVarBits( (1<<i)+3 );
BitCoderContext ctx = new BitCoderContext(ab);
for (int i = 0; i < 31; i++) {
ctx.encodeVarBits((1 << i) + 3);
}
for ( int i = 0; i < 100000; i+=13 )
{
ctx.encodeVarBits( i );
for (int i = 0; i < 100000; i += 13) {
ctx.encodeVarBits(i);
}
ctx.closeAndGetEncodedLength();
ctx = new BitCoderContext( ab );
ctx = new BitCoderContext(ab);
for ( int i = 0; i < 31; i++ )
{
for (int i = 0; i < 31; i++) {
int value = ctx.decodeVarBits();
int v0 = (1<<i)+3;
Assert.assertTrue( "value mismatch value=" + value + "v0=" + v0, v0 == value );
int v0 = (1 << i) + 3;
Assert.assertTrue("value mismatch value=" + value + "v0=" + v0, v0 == value);
}
for ( int i = 0; i < 100000; i+=13 )
{
for (int i = 0; i < 100000; i += 13) {
int value = ctx.decodeVarBits();
Assert.assertTrue( "value mismatch i=" + i + "v=" + value, value == i );
Assert.assertTrue("value mismatch i=" + i + "v=" + value, value == i);
}
}
@Test
public void boundedEncodeDecodeTest()
{
public void boundedEncodeDecodeTest() {
byte[] ab = new byte[581969];
BitCoderContext ctx = new BitCoderContext( ab );
for ( int max = 1; max < 1000; max++ )
{
for ( int val = 0; val <= max; val++ )
{
ctx.encodeBounded( max, val );
BitCoderContext ctx = new BitCoderContext(ab);
for (int max = 1; max < 1000; max++) {
for (int val = 0; val <= max; val++) {
ctx.encodeBounded(max, val);
}
}
ctx.closeAndGetEncodedLength();
ctx = new BitCoderContext( ab );
ctx = new BitCoderContext(ab);
for ( int max = 1; max < 1000; max++ )
{
for ( int val = 0; val <= max; val++ )
{
int valDecoded = ctx.decodeBounded( max );
if ( valDecoded != val )
{
Assert.fail( "mismatch at max=" + max + " " + valDecoded + "<>" + val );
for (int max = 1; max < 1000; max++) {
for (int val = 0; val <= max; val++) {
int valDecoded = ctx.decodeBounded(max);
if (valDecoded != val) {
Assert.fail("mismatch at max=" + max + " " + valDecoded + "<>" + val);
}
}
}

View file

@ -6,23 +6,19 @@ import java.util.HashSet;
import org.junit.Assert;
import org.junit.Test;
public class ByteDataIOTest
{
public class ByteDataIOTest {
@Test
public void varLengthEncodeDecodeTest()
{
byte[] ab = new byte[4000];
ByteDataWriter w = new ByteDataWriter( ab );
for( int i=0; i<1000; i++ )
{
w.writeVarLengthUnsigned( i );
public void varLengthEncodeDecodeTest() {
byte[] ab = new byte[4000];
ByteDataWriter w = new ByteDataWriter(ab);
for (int i = 0; i < 1000; i++) {
w.writeVarLengthUnsigned(i);
}
ByteDataReader r = new ByteDataReader( ab );
for( int i=0; i<1000; i++ )
{
ByteDataReader r = new ByteDataReader(ab);
for (int i = 0; i < 1000; i++) {
int value = r.readVarLengthUnsigned();
Assert.assertTrue( "value mismatch", value == i );
Assert.assertTrue("value mismatch", value == i);
}
}
}

View file

@ -10,11 +10,11 @@ import org.junit.Test;
public class CheapAngleMeterTest {
static int toOsmLon(double lon) {
return (int)( ( lon + 180. ) / CheapRuler.ILATLNG_TO_LATLNG + 0.5);
return (int) ((lon + 180.) / CheapRuler.ILATLNG_TO_LATLNG + 0.5);
}
static int toOsmLat(double lat) {
return (int)( ( lat + 90. ) / CheapRuler.ILATLNG_TO_LATLNG + 0.5);
return (int) ((lat + 90.) / CheapRuler.ILATLNG_TO_LATLNG + 0.5);
}
@Test
@ -79,40 +79,38 @@ public class CheapAngleMeterTest {
@Test
public void testCalcAngle2() {
CheapAngleMeter am = new CheapAngleMeter();
int lon1 = 8500000;
int lon1 = 8500000;
int lat1 = 49500000;
double[] lonlat2m = CheapRuler.getLonLatToMeterScales( lat1 );
double[] lonlat2m = CheapRuler.getLonLatToMeterScales(lat1);
double lon2m = lonlat2m[0];
double lat2m = lonlat2m[1];
for ( double afrom = -175.; afrom < 180.; afrom += 10. )
{
double sf = Math.sin( afrom * Math.PI / 180. );
double cf = Math.cos( afrom * Math.PI / 180. );
int lon0 = (int)(0.5+lon1 - cf*150./lon2m );
int lat0 = (int)(0.5+lat1 - sf*150./lat2m );
for ( double ato = -177.; ato < 180.; ato += 10. )
{
double st = Math.sin( ato * Math.PI / 180. );
double ct = Math.cos( ato * Math.PI / 180. );
int lon2 = (int)(0.5+lon1 + ct*250./lon2m);
int lat2 = (int)(0.5+lat1 + st*250./lat2m);
for (double afrom = -175.; afrom < 180.; afrom += 10.) {
double sf = Math.sin(afrom * Math.PI / 180.);
double cf = Math.cos(afrom * Math.PI / 180.);
int lon0 = (int) (0.5 + lon1 - cf * 150. / lon2m);
int lat0 = (int) (0.5 + lat1 - sf * 150. / lat2m);
for (double ato = -177.; ato < 180.; ato += 10.) {
double st = Math.sin(ato * Math.PI / 180.);
double ct = Math.cos(ato * Math.PI / 180.);
int lon2 = (int) (0.5 + lon1 + ct * 250. / lon2m);
int lat2 = (int) (0.5 + lat1 + st * 250. / lat2m);
double a1 = afrom - ato;
if ( a1 > 180. ) a1 -= 360.;
if ( a1 < -180. ) a1 += 360.;
double a2 = am.calcAngle( lon0, lat0, lon1, lat1, lon2, lat2 );
double c1 = Math.cos( a1 * Math.PI / 180. );
if (a1 > 180.) a1 -= 360.;
if (a1 < -180.) a1 += 360.;
double a2 = am.calcAngle(lon0, lat0, lon1, lat1, lon2, lat2);
double c1 = Math.cos(a1 * Math.PI / 180.);
double c2 = am.getCosAngle();
assertEquals( "angle mismatch for afrom=" + afrom + " ato=" + ato, a1, a2, 0.2 );
assertEquals( "cosinus mismatch for afrom=" + afrom + " ato=" + ato, c1, c2, 0.001 );
assertEquals("angle mismatch for afrom=" + afrom + " ato=" + ato, a1, a2, 0.2);
assertEquals("cosinus mismatch for afrom=" + afrom + " ato=" + ato, c1, c2, 0.001);
}
}
}
}
}

View file

@ -6,62 +6,54 @@ import java.util.HashMap;
import org.junit.Assert;
import org.junit.Test;
public class CompactMapTest
{
public class CompactMapTest {
@Test
public void hashMapComparisonTest()
{
hashMapComparison( 0, 1 );
hashMapComparison( 1, 1 );
hashMapComparison( 2, 2 );
hashMapComparison( 3, 3 );
hashMapComparison( 4, 4 );
hashMapComparison( 5, 5 );
hashMapComparison( 7, 10 );
hashMapComparison( 8, 10 );
hashMapComparison( 10000, 20000 );
public void hashMapComparisonTest() {
hashMapComparison(0, 1);
hashMapComparison(1, 1);
hashMapComparison(2, 2);
hashMapComparison(3, 3);
hashMapComparison(4, 4);
hashMapComparison(5, 5);
hashMapComparison(7, 10);
hashMapComparison(8, 10);
hashMapComparison(10000, 20000);
}
private void hashMapComparison( int mapsize, int trycount )
{
Random rand = new Random( 12345 );
HashMap<Long,String> hmap = new HashMap<Long,String>();
private void hashMapComparison(int mapsize, int trycount) {
Random rand = new Random(12345);
HashMap<Long, String> hmap = new HashMap<Long, String>();
CompactLongMap<String> cmap_slow = new CompactLongMap<String>();
CompactLongMap<String> cmap_fast = new CompactLongMap<String>();
for( int i=0; i<mapsize; i++ )
{
for (int i = 0; i < mapsize; i++) {
String s = "" + i;
long k = mapsize < 10 ? i : rand.nextInt( 20000 );
Long KK = new Long( k );
long k = mapsize < 10 ? i : rand.nextInt(20000);
Long KK = new Long(k);
if ( !hmap.containsKey( KK ) )
{
hmap.put( KK, s );
cmap_slow.put( k, s );
cmap_fast.fastPut( k, s );
if (!hmap.containsKey(KK)) {
hmap.put(KK, s);
cmap_slow.put(k, s);
cmap_fast.fastPut(k, s);
}
}
for( int i=0; i<trycount*2; i++ )
{
if ( i == trycount )
{
cmap_slow = new FrozenLongMap<String>( cmap_slow );
cmap_fast = new FrozenLongMap<String>( cmap_fast );
for (int i = 0; i < trycount * 2; i++) {
if (i == trycount) {
cmap_slow = new FrozenLongMap<String>(cmap_slow);
cmap_fast = new FrozenLongMap<String>(cmap_fast);
}
long k = mapsize < 10 ? i : rand.nextInt( 20000 );
Long KK = new Long( k );
String s = hmap.get( KK );
long k = mapsize < 10 ? i : rand.nextInt(20000);
Long KK = new Long(k);
String s = hmap.get(KK);
boolean contained = hmap.containsKey( KK );
Assert.assertTrue( "containsKey missmatch (slow)", contained == cmap_slow.contains( k ) );
Assert.assertTrue( "containsKey missmatch (fast)", contained == cmap_fast.contains( k ) );
if ( contained )
{
Assert.assertEquals( "object missmatch (fast)", s, cmap_fast.get( k ) );
Assert.assertEquals( "object missmatch (slow)", s, cmap_slow.get( k ) );
boolean contained = hmap.containsKey(KK);
Assert.assertTrue("containsKey missmatch (slow)", contained == cmap_slow.contains(k));
Assert.assertTrue("containsKey missmatch (fast)", contained == cmap_fast.contains(k));
if (contained) {
Assert.assertEquals("object missmatch (fast)", s, cmap_fast.get(k));
Assert.assertEquals("object missmatch (slow)", s, cmap_slow.get(k));
}
}
}

View file

@ -6,55 +6,48 @@ import java.util.HashSet;
import org.junit.Assert;
import org.junit.Test;
public class CompactSetTest
{
public class CompactSetTest {
@Test
public void hashSetComparisonTest()
{
hashSetComparison( 0, 1 );
hashSetComparison( 1, 1 );
hashSetComparison( 2, 2 );
hashSetComparison( 3, 3 );
hashSetComparison( 4, 4 );
hashSetComparison( 5, 5 );
hashSetComparison( 7, 10 );
hashSetComparison( 8, 10 );
hashSetComparison( 10000, 20000 );
public void hashSetComparisonTest() {
hashSetComparison(0, 1);
hashSetComparison(1, 1);
hashSetComparison(2, 2);
hashSetComparison(3, 3);
hashSetComparison(4, 4);
hashSetComparison(5, 5);
hashSetComparison(7, 10);
hashSetComparison(8, 10);
hashSetComparison(10000, 20000);
}
private void hashSetComparison( int setsize, int trycount )
{
Random rand = new Random( 12345 );
private void hashSetComparison(int setsize, int trycount) {
Random rand = new Random(12345);
HashSet<Long> hset = new HashSet<Long>();
CompactLongSet cset_slow = new CompactLongSet();
CompactLongSet cset_fast = new CompactLongSet();
for( int i=0; i<setsize; i++ )
{
long k = setsize < 10 ? i : rand.nextInt( 20000 );
Long KK = new Long( k );
for (int i = 0; i < setsize; i++) {
long k = setsize < 10 ? i : rand.nextInt(20000);
Long KK = new Long(k);
if ( !hset.contains( KK ) )
{
hset.add( KK );
cset_slow.add( k );
cset_fast.fastAdd( k );
if (!hset.contains(KK)) {
hset.add(KK);
cset_slow.add(k);
cset_fast.fastAdd(k);
}
}
for( int i=0; i<trycount*2; i++ )
{
if ( i == trycount )
{
cset_slow = new FrozenLongSet( cset_slow );
cset_fast = new FrozenLongSet( cset_fast );
for (int i = 0; i < trycount * 2; i++) {
if (i == trycount) {
cset_slow = new FrozenLongSet(cset_slow);
cset_fast = new FrozenLongSet(cset_fast);
}
long k = setsize < 10 ? i : rand.nextInt( 20000 );
Long KK = new Long( k );
long k = setsize < 10 ? i : rand.nextInt(20000);
Long KK = new Long(k);
boolean contained = hset.contains( KK );
Assert.assertTrue( "contains missmatch (slow)", contained == cset_slow.contains( k ) );
Assert.assertTrue( "contains missmatch (fast)", contained == cset_fast.contains( k ) );
boolean contained = hset.contains(KK);
Assert.assertTrue("contains missmatch (slow)", contained == cset_slow.contains(k));
Assert.assertTrue("contains missmatch (fast)", contained == cset_fast.contains(k));
}
}
}

View file

@ -7,107 +7,94 @@ import java.util.HashSet;
import org.junit.Assert;
import org.junit.Test;
public class DenseLongMapTest
{
public class DenseLongMapTest {
@Test
public void hashMapComparisonTest()
{
hashMapComparison( 100000, 100000, 100000 );
hashMapComparison( 100000, 100000, 13000000 );
public void hashMapComparisonTest() {
hashMapComparison(100000, 100000, 100000);
hashMapComparison(100000, 100000, 13000000);
}
private void hashMapComparison( int mapsize, int trycount, long keyrange )
{
Random rand = new Random( 12345 );
HashMap<Long,Integer> hmap = new HashMap<Long,Integer>();
DenseLongMap dmap = new DenseLongMap( 512 );
private void hashMapComparison(int mapsize, int trycount, long keyrange) {
Random rand = new Random(12345);
HashMap<Long, Integer> hmap = new HashMap<Long, Integer>();
DenseLongMap dmap = new DenseLongMap(512);
for( int i=0; i<mapsize; i++ )
{
int value = i%255;
long k = (long)(rand.nextDouble()*keyrange);
Long KK = new Long( k );
for (int i = 0; i < mapsize; i++) {
int value = i % 255;
long k = (long) (rand.nextDouble() * keyrange);
Long KK = new Long(k);
hmap.put( KK, new Integer ( value ) );
dmap.put( k, value ); // duplicate puts allowed!
hmap.put(KK, new Integer(value));
dmap.put(k, value); // duplicate puts allowed!
}
for( int i=0; i<trycount; i++ )
{
long k = (long)(rand.nextDouble()*keyrange);
Long KK = new Long( k );
Integer VV = hmap.get( KK );
for (int i = 0; i < trycount; i++) {
long k = (long) (rand.nextDouble() * keyrange);
Long KK = new Long(k);
Integer VV = hmap.get(KK);
int hvalue = VV == null ? -1 : VV.intValue();
int dvalue = dmap.getInt( k );
int dvalue = dmap.getInt(k);
if ( hvalue != dvalue )
{
Assert.fail( "value missmatch for key " + k + " hashmap=" + hvalue + " densemap=" + dvalue );
if (hvalue != dvalue) {
Assert.fail("value missmatch for key " + k + " hashmap=" + hvalue + " densemap=" + dvalue);
}
}
}
@Test
public void oneBitTest()
{
public void oneBitTest() {
int keyrange = 300000;
int mapputs = 100000;
int trycount = 100000;
Random rand = new Random( 12345 );
Random rand = new Random(12345);
HashSet<Long> hset = new HashSet<Long>();
DenseLongMap dmap = new DenseLongMap( 512 );
for( int i=0; i<mapputs; i++ )
{
long k = (long)(rand.nextDouble()*keyrange);
hset.add( new Long( k ) );
dmap.put( k, 0 );
DenseLongMap dmap = new DenseLongMap(512);
for (int i = 0; i < mapputs; i++) {
long k = (long) (rand.nextDouble() * keyrange);
hset.add(new Long(k));
dmap.put(k, 0);
}
for( int i=0; i<trycount; i++ )
{
long k = (long)(rand.nextDouble()*keyrange);
boolean hcontains = hset.contains( new Long( k ) );
boolean dcontains = dmap.getInt( k ) == 0;
for (int i = 0; i < trycount; i++) {
long k = (long) (rand.nextDouble() * keyrange);
boolean hcontains = hset.contains(new Long(k));
boolean dcontains = dmap.getInt(k) == 0;
if ( hcontains != dcontains )
{
Assert.fail( "value missmatch for key " + k + " hashset=" + hcontains + " densemap=" + dcontains );
if (hcontains != dcontains) {
Assert.fail("value missmatch for key " + k + " hashset=" + hcontains + " densemap=" + dcontains);
}
}
}
// @Test - memory test disabled for load reasons
public void memoryUsageTest()
{
public void memoryUsageTest() {
int keyrange = 32000000;
int mapputs = keyrange * 2;
Random rand = new Random( 12345 );
DenseLongMap dmap = new DenseLongMap( 6 );
Random rand = new Random(12345);
DenseLongMap dmap = new DenseLongMap(6);
System.gc();
long mem1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
for( int i=0; i<mapputs; i++ )
{
int value = i%63;
long k = (long)(rand.nextDouble()*keyrange);
dmap.put( k, value );
for (int i = 0; i < mapputs; i++) {
int value = i % 63;
long k = (long) (rand.nextDouble() * keyrange);
dmap.put(k, value);
}
System.gc();
long mem2 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long memusage = mem2-mem1;
long memusage = mem2 - mem1;
if ( memusage > (keyrange/8)*7 )
{
Assert.fail( "memory usage too high: " + memusage + " for keyrange " + keyrange );
if (memusage > (keyrange / 8) * 7) {
Assert.fail("memory usage too high: " + memusage + " for keyrange " + keyrange);
}
// need to use the map again for valid memory measure
Assert.assertTrue( "out of range test", dmap.getInt(-1) == -1 );
Assert.assertTrue("out of range test", dmap.getInt(-1) == -1);
}
}

View file

@ -6,57 +6,44 @@ import java.io.*;
import org.junit.Assert;
import org.junit.Test;
public class MixCoderTest
{
public class MixCoderTest {
@Test
public void mixEncodeDecodeTest() throws IOException
{
public void mixEncodeDecodeTest() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MixCoderDataOutputStream mco = new MixCoderDataOutputStream( baos );
MixCoderDataOutputStream mco = new MixCoderDataOutputStream(baos);
MixCoderDataInputStream mci = null;
for(;;)
{
Random rnd = new Random( 1234 );
for( int i=0; i<1500; i++ )
{
checkEncodeDecode( rnd.nextInt( 3800 ), mco, mci );
for (; ; ) {
Random rnd = new Random(1234);
for (int i = 0; i < 1500; i++) {
checkEncodeDecode(rnd.nextInt(3800), mco, mci);
}
for( int i=0; i<1500; i++ )
{
checkEncodeDecode( rnd.nextInt( 35 ), mco, mci );
for (int i = 0; i < 1500; i++) {
checkEncodeDecode(rnd.nextInt(35), mco, mci);
}
for( int i=0; i<1500; i++ )
{
checkEncodeDecode( 0, mco, mci );
for (int i = 0; i < 1500; i++) {
checkEncodeDecode(0, mco, mci);
}
for( int i=0; i<1500; i++ )
{
checkEncodeDecode( 1000, mco, mci );
for (int i = 0; i < 1500; i++) {
checkEncodeDecode(1000, mco, mci);
}
if ( mco != null )
{
if (mco != null) {
mco.close();
mco = null;
mci = new MixCoderDataInputStream( new ByteArrayInputStream( baos.toByteArray() ) );
}
else break;
mci = new MixCoderDataInputStream(new ByteArrayInputStream(baos.toByteArray()));
} else break;
}
}
private void checkEncodeDecode( int v, MixCoderDataOutputStream mco, MixCoderDataInputStream mci ) throws IOException
{
if ( mco != null )
{
mco.writeMixed( v );
private void checkEncodeDecode(int v, MixCoderDataOutputStream mco, MixCoderDataInputStream mci) throws IOException {
if (mco != null) {
mco.writeMixed(v);
}
if ( mci != null )
{
if (mci != null) {
long vv = mci.readMixed();
if ( vv != v )
{
Assert.assertTrue( "value mismatch: v=" + v + " vv=" + vv, false );
if (vv != v) {
Assert.assertTrue("value mismatch: v=" + v + " vv=" + vv, false);
}
}
}

View file

@ -6,41 +6,38 @@ import java.io.*;
import org.junit.Assert;
import org.junit.Test;
public class ReducedMedianFilterTest
{
public class ReducedMedianFilterTest {
@Test
public void reducedMedianFilterTest() throws IOException
{
ReducedMedianFilter f = new ReducedMedianFilter( 10 );
public void reducedMedianFilterTest() throws IOException {
ReducedMedianFilter f = new ReducedMedianFilter(10);
f.reset();
f.addSample( .2, 10 );
f.addSample( .2, 10 );
f.addSample( .2, 10 );
f.addSample( .2, 15 );
f.addSample( .2, 20 );
double m = f.calcEdgeReducedMedian( 0.5 );
Assert.assertTrue( "median1 mismatch m=" + m + " expected 11.5", doubleEquals( m, 11.5 ) );
f.addSample(.2, 10);
f.addSample(.2, 10);
f.addSample(.2, 10);
f.addSample(.2, 15);
f.addSample(.2, 20);
double m = f.calcEdgeReducedMedian(0.5);
Assert.assertTrue("median1 mismatch m=" + m + " expected 11.5", doubleEquals(m, 11.5));
f.reset();
f.addSample( .2, 10 );
f.addSample( .2, 10 );
f.addSample( .2, 10 );
f.addSample( .2, 10 );
f.addSample( .2, 20 );
f.addSample(.2, 10);
f.addSample(.2, 10);
f.addSample(.2, 10);
f.addSample(.2, 10);
f.addSample(.2, 20);
m = f.calcEdgeReducedMedian( 1. );
Assert.assertTrue( "median1 mismatch m=" + m + " expected 12", doubleEquals( m, 12. ) );
m = f.calcEdgeReducedMedian(1.);
Assert.assertTrue("median1 mismatch m=" + m + " expected 12", doubleEquals(m, 12.));
f.reset();
f.addSample( .5, -10 );
f.addSample( .5, 10 );
m = f.calcEdgeReducedMedian( 0.5 );
Assert.assertTrue( "median2 mismatch m=" + m + " expected 0", doubleEquals( m, 0. ) );
f.addSample(.5, -10);
f.addSample(.5, 10);
m = f.calcEdgeReducedMedian(0.5);
Assert.assertTrue("median2 mismatch m=" + m + " expected 0", doubleEquals(m, 0.));
}
private boolean doubleEquals( double d1, double d2 )
{
private boolean doubleEquals(double d1, double d2) {
double d = d1 - d2;
return d < 1e-9 && d > -1e-9;
}

View file

@ -7,59 +7,52 @@ import java.util.HashSet;
import org.junit.Assert;
import org.junit.Test;
public class SortedHeapTest
{
public class SortedHeapTest {
@Test
public void sortedHeapTest1()
{
SortedHeap<String> sh = new SortedHeap<String>();
Random rnd = new Random();
for( int i = 0; i< 100000; i++ )
{
int val = rnd.nextInt( 1000000 );
sh.add( val, "" + val );
val = rnd.nextInt( 1000000 );
sh.add( val, "" + val );
sh.popLowestKeyValue();
}
public void sortedHeapTest1() {
SortedHeap<String> sh = new SortedHeap<String>();
Random rnd = new Random();
for (int i = 0; i < 100000; i++) {
int val = rnd.nextInt(1000000);
sh.add(val, "" + val);
val = rnd.nextInt(1000000);
sh.add(val, "" + val);
sh.popLowestKeyValue();
}
int cnt = 0;
int lastval = 0;
for(;;)
{
String s = sh.popLowestKeyValue();
if ( s == null ) break;
cnt ++;
int val = Integer.parseInt( s );
Assert.assertTrue( "sorting test", val >= lastval );
lastval = val;
}
Assert.assertTrue( "total count test", cnt == 100000 );
int cnt = 0;
int lastval = 0;
for (; ; ) {
String s = sh.popLowestKeyValue();
if (s == null) break;
cnt++;
int val = Integer.parseInt(s);
Assert.assertTrue("sorting test", val >= lastval);
lastval = val;
}
Assert.assertTrue("total count test", cnt == 100000);
}
@Test
public void sortedHeapTest2()
{
SortedHeap<String> sh = new SortedHeap<String>();
Random rnd = new Random();
for( int i = 0; i< 100000; i++ )
{
sh.add( i, "" + i );
}
public void sortedHeapTest2() {
SortedHeap<String> sh = new SortedHeap<String>();
Random rnd = new Random();
for (int i = 0; i < 100000; i++) {
sh.add(i, "" + i);
}
int cnt = 0;
int expected = 0;
for(;;)
{
String s = sh.popLowestKeyValue();
if ( s == null ) break;
cnt ++;
int val = Integer.parseInt( s );
Assert.assertTrue( "sequence test", val == expected );
expected++;
}
Assert.assertTrue( "total count test", cnt == 100000 );
int cnt = 0;
int expected = 0;
for (; ; ) {
String s = sh.popLowestKeyValue();
if (s == null) break;
cnt++;
int val = Integer.parseInt(s);
Assert.assertTrue("sequence test", val == expected);
expected++;
}
Assert.assertTrue("total count test", cnt == 100000);
}
}

View file

@ -3,27 +3,22 @@ package btools.util;
import org.junit.Assert;
import org.junit.Test;
public class StringUtilsTest
{
private static String[] raw = new String[] { "hallo", "is 1<2 ?", "or 4>5 ?", "or 1<>2 ?", "\"hi\" 'there'" };
private static String[] xml = new String[] { "hallo", "is 1&lt;2 ?", "or 4&gt;5 ?", "or 1&lt;&gt;2 ?", "&quot;hi&quot; &apos;there&apos;" };
private static String[] jsn = new String[] { "hallo", "is 1<2 ?", "or 4>5 ?", "or 1<>2 ?", "\\\"hi\\\" \\'there\\'" };
public class StringUtilsTest {
private static String[] raw = new String[]{"hallo", "is 1<2 ?", "or 4>5 ?", "or 1<>2 ?", "\"hi\" 'there'"};
private static String[] xml = new String[]{"hallo", "is 1&lt;2 ?", "or 4&gt;5 ?", "or 1&lt;&gt;2 ?", "&quot;hi&quot; &apos;there&apos;"};
private static String[] jsn = new String[]{"hallo", "is 1<2 ?", "or 4>5 ?", "or 1<>2 ?", "\\\"hi\\\" \\'there\\'"};
@Test
public void xmlEncodingTest()
{
for( int i=0; i<raw.length; i++ )
{
Assert.assertEquals( "xml encoding mismatch for raw: " + raw[i], xml[i], StringUtils.escapeXml10( raw[i] ) );
}
public void xmlEncodingTest() {
for (int i = 0; i < raw.length; i++) {
Assert.assertEquals("xml encoding mismatch for raw: " + raw[i], xml[i], StringUtils.escapeXml10(raw[i]));
}
}
@Test
public void jsonEncodingTest()
{
for( int i=0; i<raw.length; i++ )
{
Assert.assertEquals( "json encoding mismatch for raw: " + raw[i], jsn[i], StringUtils.escapeJson( raw[i] ) );
public void jsonEncodingTest() {
for (int i = 0; i < raw.length; i++) {
Assert.assertEquals("json encoding mismatch for raw: " + raw[i], jsn[i], StringUtils.escapeJson(raw[i]));
}
}
}