mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Refactor: Pull more classes into baselib
This commit is contained in:
38
baselib/tst/gplx/objects/ObjectUtlTest.java
Normal file
38
baselib/tst/gplx/objects/ObjectUtlTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2021 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.ObjectUtl;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
public class ObjectUtlTest {
|
||||
private ObjectUtlTstr tstr = new ObjectUtlTstr();
|
||||
@Before public void init() {}
|
||||
@Test public void Eq() {
|
||||
tstr.TestEq(null, null, true); // both null
|
||||
tstr.TestEq(5, 5, true); // both non-null
|
||||
tstr.TestEq(5, null, false); // rhs non-null
|
||||
tstr.TestEq(null, 5, false); // lhs non-null
|
||||
}
|
||||
@Test public void ToStrLooseOrNull() {
|
||||
tstr.TestToStrLooseOrNull(null, null);
|
||||
tstr.TestToStrLooseOrNull(2449.6000000000004d, "2449.6");
|
||||
}
|
||||
}
|
||||
class ObjectUtlTstr {
|
||||
public void TestEq(Object lhs, Object rhs, boolean expd) {GfoTstr.Eq(expd, ObjectUtl.Eq(lhs, rhs));}
|
||||
public void TestToStrLooseOrNull(Object v, String expd) {GfoTstr.Eq(expd, ObjectUtl.ToStrLooseOr(v, null));}
|
||||
}
|
||||
@@ -1,43 +1,44 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.arrays;
|
||||
import gplx.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class ArrayUtlTest {
|
||||
@Test public void Append() {
|
||||
TestAppend(AryInt(), AryInt(1), AryInt(1)); // 0 + 1 = 1
|
||||
TestAppend(AryInt(0), AryInt(), AryInt(0)); // 1 + 0 = 1
|
||||
TestAppend(AryInt(0), AryInt(1), AryInt(0, 1)); // 1 + 1 = 2
|
||||
}
|
||||
private void TestAppend(int[] source, int[] added, int[] expd) {
|
||||
GfoTstr.EqAry(expd, (int[])ArrayUtl.Append(source, added));
|
||||
}
|
||||
@Test public void Resize() {
|
||||
TestResize(AryInt(0), 0, AryInt()); // 1 -> 0
|
||||
TestResize(AryInt(0, 1), 1, AryInt(0)); // 2 -> 1
|
||||
}
|
||||
private void TestResize(int[] source, int length, int[] expd) {
|
||||
GfoTstr.EqAry(expd, (int[])ArrayUtl.Resize(source, length));
|
||||
}
|
||||
@Test public void Insert() {
|
||||
TestInsert(AryObj(0, 1, 4, 5), AryObj(2, 3), 2, AryObj(0, 1, 2, 3, 4, 5));
|
||||
}
|
||||
private void TestInsert(Object[] cur, Object[] add, int addPos, Object[] expd) {
|
||||
GfoTstr.EqAry(expd, ArrayUtl.Insert(cur, add, addPos));
|
||||
}
|
||||
private Object[] AryObj(Object... ary) {return ary;}
|
||||
private int[] AryInt(int... ary) {return ary;}
|
||||
}
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2021 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.arrays;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.ArrayUtl;
|
||||
import org.junit.Test;
|
||||
public class ArrayUtlTest {
|
||||
@Test public void Append() {
|
||||
TestAppend(AryInt(), AryInt(1), AryInt(1)); // 0 + 1 = 1
|
||||
TestAppend(AryInt(0), AryInt(), AryInt(0)); // 1 + 0 = 1
|
||||
TestAppend(AryInt(0), AryInt(1), AryInt(0, 1)); // 1 + 1 = 2
|
||||
}
|
||||
private void TestAppend(int[] source, int[] added, int[] expd) {
|
||||
GfoTstr.EqAry(expd, (int[])ArrayUtl.Append(source, added));
|
||||
}
|
||||
@Test public void Resize() {
|
||||
TestResize(AryInt(0), 0, AryInt()); // 1 -> 0
|
||||
TestResize(AryInt(0, 1), 1, AryInt(0)); // 2 -> 1
|
||||
}
|
||||
private void TestResize(int[] source, int length, int[] expd) {
|
||||
GfoTstr.EqAry(expd, (int[])ArrayUtl.Resize(source, length));
|
||||
}
|
||||
@Test public void Insert() {
|
||||
TestInsert(AryObj(0, 1, 4, 5), AryObj(2, 3), 2, AryObj(0, 1, 2, 3, 4, 5));
|
||||
}
|
||||
private void TestInsert(Object[] cur, Object[] add, int addPos, Object[] expd) {
|
||||
GfoTstr.EqAryObjAry(expd, ArrayUtl.Insert(cur, add, addPos));
|
||||
}
|
||||
private Object[] AryObj(Object... ary) {return ary;}
|
||||
private int[] AryInt(int... ary) {return ary;}
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.lists;
|
||||
import gplx.objects.arrays.ArrayUtl;
|
||||
import gplx.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class ComparerAbleSorterTest {
|
||||
@Test public void Basic() {
|
||||
Object[] src = new Object[] {0,8,1,7,2,6,3,5,4};
|
||||
new ComparerAbleSorter().Sort(src, src.length);
|
||||
GfoTstr.EqAry(src, Sequential(0, 8));
|
||||
}
|
||||
@Test public void Basic2() {
|
||||
Object[] src = new Object[] {"0","8","1","7","2","6","3","5","4"};
|
||||
new ComparerAbleSorter().Sort(src, src.length);
|
||||
GfoTstr.EqAry(src, new Object[] {"0","1","2","3","4","5","6","7","8"});
|
||||
}
|
||||
private Object[] Sequential(int bgn, int end) {
|
||||
Object[] rv = new Object[end - bgn + 1];
|
||||
for (int i = 0; i < ArrayUtl.Len(rv); i++)
|
||||
rv[i] = i + bgn;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2021 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.lists;
|
||||
import gplx.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class GfoIndexedListEntryTest {
|
||||
private GfoIndexedList<String, String> list = new GfoIndexedList<>();
|
||||
@Test public void Add() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "c");
|
||||
|
||||
testGetAt(0, "a");
|
||||
testGetAt(1, "b");
|
||||
testGetAt(2, "c");
|
||||
testGetByOrFail("A", "a");
|
||||
testGetByOrFail("B", "b");
|
||||
testGetByOrFail("C", "c");
|
||||
testIterate("a", "b", "c");
|
||||
}
|
||||
@Test public void DelBy() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "c");
|
||||
|
||||
list.DelBy("A");
|
||||
|
||||
testIterate("b", "c");
|
||||
|
||||
list.DelBy("B");
|
||||
|
||||
testIterate("c");
|
||||
|
||||
list.DelBy("C");
|
||||
|
||||
testIterate();
|
||||
}
|
||||
@Test public void DelBySameVal() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "a");
|
||||
|
||||
list.DelBy("C");
|
||||
|
||||
testIterate("a", "b"); // fails if "b", "a"
|
||||
}
|
||||
@Test public void Set() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "c");
|
||||
|
||||
list.Set("B", "bb");
|
||||
testGetByOrFail("B", "bb");
|
||||
testIterate("a", "bb", "c");
|
||||
}
|
||||
private void testGetByOrFail(String key, String expd) {
|
||||
GfoTstr.EqStr(expd, list.GetByOrFail(key));
|
||||
}
|
||||
private void testGetAt(int idx, String expd) {
|
||||
GfoTstr.EqStr(expd, list.GetAt(idx));
|
||||
}
|
||||
private void testIterate(String... expd) {
|
||||
String[] actl = new String[expd.length];
|
||||
int i = 0;
|
||||
for (String itm : list) {
|
||||
actl[i++] = itm;
|
||||
}
|
||||
GfoTstr.EqAry(expd, actl);
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,9 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.primitives;
|
||||
import gplx.objects.lists.CompareAbleUtl;
|
||||
import gplx.tests.GfoTstr;
|
||||
import gplx.types.commons.lists.CompareAbleUtl;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.BoolUtl;
|
||||
import org.junit.*;
|
||||
public class BoolUtlTest {
|
||||
private final BoolUtlTstr fxt = new BoolUtlTstr();
|
||||
@@ -27,5 +28,5 @@ public class BoolUtlTest {
|
||||
}
|
||||
}
|
||||
class BoolUtlTstr {
|
||||
public void TestCompare(boolean lhs, boolean rhs, int expd) {GfoTstr.EqInt(expd, BoolUtl.Compare(lhs, rhs));}
|
||||
public void TestCompare(boolean lhs, boolean rhs, int expd) {GfoTstr.Eq(expd, BoolUtl.Compare(lhs, rhs));}
|
||||
}
|
||||
|
||||
35
baselib/tst/gplx/objects/primitives/ByteUtlTest.java
Normal file
35
baselib/tst/gplx/objects/primitives/ByteUtlTest.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2021 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.primitives;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.ByteUtl;
|
||||
import org.junit.Test;
|
||||
public class ByteUtlTest {
|
||||
@Test public void int_() {
|
||||
tst_int_( 0, 0);
|
||||
tst_int_( 127, 127);
|
||||
tst_int_( 128, 128); // NOTE: JAVA defines byte as -128 -> 127
|
||||
tst_int_( 255, 255);
|
||||
tst_int_( 256, 0); // NOTE: 256 will cast to 1; (byte)256 works same in both JAVA/.NET
|
||||
} void tst_int_(int v, int expd) {GfoTstr.Eq((byte)expd, ByteUtl.ByInt(v));} // WORKAROUND/JAVA: expd is of type int b/c java promotes numbers to ints
|
||||
@Test public void To_int() {
|
||||
tst_XtoInt( 0, 0);
|
||||
tst_XtoInt( 127, 127);
|
||||
tst_XtoInt( 128, 128);
|
||||
tst_XtoInt( 255, 255);
|
||||
tst_XtoInt( 256, 0);
|
||||
} void tst_XtoInt(int v, int expd) {GfoTstr.Eq(expd, ByteUtl.ToInt((byte)v));} // WORKAROUND/JAVA: v is of type int b/c java promotes numbers to ints
|
||||
}
|
||||
@@ -14,7 +14,8 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.primitives;
|
||||
import gplx.tests.GfoTstr;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.DoubleUtl;
|
||||
import org.junit.Test;
|
||||
public class DoubleUtlTest {
|
||||
private final DoubleUtlTstr tstr = new DoubleUtlTstr();
|
||||
@@ -38,12 +39,13 @@ public class DoubleUtlTest {
|
||||
tstr.TestToStrByPrintF(56225d / 7776747000d , "7.2298867379895E-06"); // fails with 0; ISSUE#:697; DATE:2020-08-11
|
||||
tstr.TestToStrByPrintF(35746d / 7805411000d , "4.5796435319037E-06"); // fails with 0; ISSUE#:697; DATE:2020-08-11
|
||||
}
|
||||
@Test public void Xto_str_loose() {
|
||||
tstr.TestXtoStrLoose(2449.6000000d , "2449.6");
|
||||
tstr.TestXtoStrLoose(623.700d , "623.7");
|
||||
}
|
||||
}
|
||||
class DoubleUtlTstr {
|
||||
public void TestToStrByPrintF(double v, String expd) {
|
||||
GfoTstr.EqStr(expd, DoubleUtl.ToStrByPrintF(v));
|
||||
}
|
||||
public void TestTrimZeroes(String val, String expd) {
|
||||
GfoTstr.EqStr(expd, DoubleUtl.TrimZeroes(val));
|
||||
}
|
||||
public void TestToStrByPrintF(double v, String expd) {GfoTstr.Eq(expd, DoubleUtl.ToStrByPrintF(v));}
|
||||
public void TestTrimZeroes(String val, String expd) {GfoTstr.Eq(expd, DoubleUtl.TrimZeroes(val));}
|
||||
public void TestXtoStrLoose(double v, String expd) {GfoTstr.Eq(expd, DoubleUtl.ToStrLoose(v));}
|
||||
}
|
||||
|
||||
@@ -14,78 +14,99 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.primitives;
|
||||
import gplx.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.BryUtl;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.IntUtl;
|
||||
import org.junit.Test;
|
||||
public class IntUtlTest {
|
||||
private final IntUtlTstr fxt = new IntUtlTstr();
|
||||
private final IntUtlTstr tstr = new IntUtlTstr();
|
||||
@Test public void ParseOr() {
|
||||
fxt.TestParseOr("123", 123); // basic
|
||||
fxt.TestParseOrMinValue(null); // null
|
||||
fxt.TestParseOrMinValue(""); // empty
|
||||
fxt.TestParseOrMinValue("1a"); // invalid number
|
||||
tstr.TestParseOr("123", 123); // basic
|
||||
tstr.TestParseOrMinValue(null); // null
|
||||
tstr.TestParseOrMinValue(""); // empty
|
||||
tstr.TestParseOrMinValue("1a"); // invalid number
|
||||
|
||||
fxt.TestParseOr("-123", -123); // negative
|
||||
fxt.TestParseOrMinValue("1-23"); // negative at invalid position
|
||||
tstr.TestParseOr("-123", -123); // negative
|
||||
tstr.TestParseOrMinValue("1-23"); // negative at invalid position
|
||||
}
|
||||
@Test public void Between() {
|
||||
fxt.TestBetween(1, 0, 2, true); // simple true
|
||||
fxt.TestBetween(3, 0, 2, false); // simple false
|
||||
fxt.TestBetween(0, 0, 2, true); // bgn true
|
||||
fxt.TestBetween(2, 0, 2, true); // end true
|
||||
tstr.TestBetween(1, 0, 2, true); // simple true
|
||||
tstr.TestBetween(3, 0, 2, false); // simple false
|
||||
tstr.TestBetween(0, 0, 2, true); // bgn true
|
||||
tstr.TestBetween(2, 0, 2, true); // end true
|
||||
}
|
||||
@Test public void CountDigits() {
|
||||
fxt.TestCountDigits( 0, 1);
|
||||
fxt.TestCountDigits( 9, 1);
|
||||
fxt.TestCountDigits( 100, 3);
|
||||
fxt.TestCountDigits( -1, 2);
|
||||
fxt.TestCountDigits(-100, 4);
|
||||
tstr.TestCountDigits( 0, 1);
|
||||
tstr.TestCountDigits( 9, 1);
|
||||
tstr.TestCountDigits( 100, 3);
|
||||
tstr.TestCountDigits( -1, 2);
|
||||
tstr.TestCountDigits(-100, 4);
|
||||
}
|
||||
@Test public void Log10() {
|
||||
fxt.TestLog10( 0, 0);
|
||||
fxt.TestLog10( 1, 0);
|
||||
fxt.TestLog10( 2, 0);
|
||||
fxt.TestLog10( 10, 1);
|
||||
fxt.TestLog10( 12, 1);
|
||||
fxt.TestLog10( 100, 2);
|
||||
fxt.TestLog10( 123, 2);
|
||||
fxt.TestLog10( 1000, 3);
|
||||
fxt.TestLog10( 1234, 3);
|
||||
fxt.TestLog10( 10000, 4);
|
||||
fxt.TestLog10( 12345, 4);
|
||||
fxt.TestLog10( 100000, 5);
|
||||
fxt.TestLog10( 123456, 5);
|
||||
fxt.TestLog10( 1000000, 6);
|
||||
fxt.TestLog10( 1234567, 6);
|
||||
fxt.TestLog10( 10000000, 7);
|
||||
fxt.TestLog10( 12345678, 7);
|
||||
fxt.TestLog10( 100000000, 8);
|
||||
fxt.TestLog10( 123456789, 8);
|
||||
fxt.TestLog10( 1000000000, 9);
|
||||
fxt.TestLog10( 1234567890, 9);
|
||||
fxt.TestLog10(IntUtl.MaxValue, 9);
|
||||
fxt.TestLog10( -1, 0);
|
||||
fxt.TestLog10( -10, -1);
|
||||
fxt.TestLog10( -100, -2);
|
||||
fxt.TestLog10( -1000000, -6);
|
||||
fxt.TestLog10( -1000000000, -9);
|
||||
fxt.TestLog10(IntUtl.MinValue, -9);
|
||||
fxt.TestLog10(IntUtl.MinValue + 1, -9);
|
||||
tstr.TestLog10( 0, 0);
|
||||
tstr.TestLog10( 1, 0);
|
||||
tstr.TestLog10( 2, 0);
|
||||
tstr.TestLog10( 10, 1);
|
||||
tstr.TestLog10( 12, 1);
|
||||
tstr.TestLog10( 100, 2);
|
||||
tstr.TestLog10( 123, 2);
|
||||
tstr.TestLog10( 1000, 3);
|
||||
tstr.TestLog10( 1234, 3);
|
||||
tstr.TestLog10( 10000, 4);
|
||||
tstr.TestLog10( 12345, 4);
|
||||
tstr.TestLog10( 100000, 5);
|
||||
tstr.TestLog10( 123456, 5);
|
||||
tstr.TestLog10( 1000000, 6);
|
||||
tstr.TestLog10( 1234567, 6);
|
||||
tstr.TestLog10( 10000000, 7);
|
||||
tstr.TestLog10( 12345678, 7);
|
||||
tstr.TestLog10( 100000000, 8);
|
||||
tstr.TestLog10( 123456789, 8);
|
||||
tstr.TestLog10( 1000000000, 9);
|
||||
tstr.TestLog10( 1234567890, 9);
|
||||
tstr.TestLog10(IntUtl.MaxValue, 9);
|
||||
tstr.TestLog10( -1, 0);
|
||||
tstr.TestLog10( -10, -1);
|
||||
tstr.TestLog10( -100, -2);
|
||||
tstr.TestLog10( -1000000, -6);
|
||||
tstr.TestLog10( -1000000000, -9);
|
||||
tstr.TestLog10(IntUtl.MinValue, -9);
|
||||
tstr.TestLog10(IntUtl.MinValue + 1, -9);
|
||||
}
|
||||
@Test public void ToStrPadBgnZeroes() {
|
||||
tstr.TestToStrPadBgnZeroes(1 , 3, "001"); // pad
|
||||
tstr.TestToStrPadBgnZeroes(123 , 3, "123"); // no pad
|
||||
tstr.TestToStrPadBgnZeroes(1234 , 3, "1234"); // val exceeds pad; confirm noop
|
||||
tstr.TestToStrPadBgnZeroes(-1 , 3, "-01"); // negative
|
||||
tstr.TestToStrPadBgnZeroes(-12 , 3, "-12"); // negative
|
||||
tstr.TestToStrPadBgnZeroes(-123 , 3, "-123"); // negative
|
||||
tstr.TestToStrPadBgnZeroes(-1234 , 3, "-1234"); // negative
|
||||
}
|
||||
@Test public void ToStrFmt() {
|
||||
tstr.TestToStrFmt(1, "1");
|
||||
tstr.TestToStrFmt(1000, "1,000");
|
||||
}
|
||||
@Test public void ByHexBry() {
|
||||
tstr.TestByHexBry("007C", 124);
|
||||
}
|
||||
}
|
||||
class IntUtlTstr {
|
||||
public void TestParseOr(String raw, int expd) {
|
||||
GfoTstr.EqInt(expd, IntUtl.ParseOr(raw, -1));
|
||||
GfoTstr.Eq(expd, IntUtl.ParseOr(raw, -1));
|
||||
}
|
||||
public void TestParseOrMinValue(String raw) {
|
||||
GfoTstr.EqInt(IntUtl.MinValue, IntUtl.ParseOr(raw, IntUtl.MinValue));
|
||||
GfoTstr.Eq(IntUtl.MinValue, IntUtl.ParseOr(raw, IntUtl.MinValue));
|
||||
}
|
||||
public void TestBetween(int val, int lhs, int rhs, boolean expd) {
|
||||
GfoTstr.EqBool(expd, IntUtl.Between(val, lhs, rhs));
|
||||
GfoTstr.Eq(expd, IntUtl.Between(val, lhs, rhs));
|
||||
}
|
||||
public void TestCountDigits(int val, int expd) {
|
||||
GfoTstr.EqInt(expd, IntUtl.CountDigits(val), IntUtl.ToStr(val));
|
||||
GfoTstr.Eq(expd, IntUtl.CountDigits(val), IntUtl.ToStr(val));
|
||||
}
|
||||
public void TestLog10(int val, int expd) {
|
||||
GfoTstr.EqInt(expd, IntUtl.Log10(val));
|
||||
GfoTstr.Eq(expd, IntUtl.Log10(val));
|
||||
}
|
||||
public void TestToStrPadBgnZeroes(int val, int zeros, String expd) {GfoTstr.Eq(expd, IntUtl.ToStrPadBgnZero(val, zeros));}
|
||||
public void TestToStrFmt(int v, String expd) {GfoTstr.Eq(expd, IntUtl.ToStrFmt(v, "#,###"));}
|
||||
public void TestByHexBry(String raw, int expd) {GfoTstr.Eq(expd, IntUtl.ByHexBry(BryUtl.NewA7(raw)));}
|
||||
}
|
||||
|
||||
53
baselib/tst/gplx/objects/primitives/LongUtlTest.java
Normal file
53
baselib/tst/gplx/objects/primitives/LongUtlTest.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2021 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.primitives;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.LongUtl;
|
||||
import org.junit.Test;
|
||||
public class LongUtlTest {
|
||||
private LongUtlTstr tstr = new LongUtlTstr();
|
||||
@Test public void DigitCount() {
|
||||
tstr.TestDigitCount(0, 1);
|
||||
tstr.TestDigitCount(1, 1);
|
||||
tstr.TestDigitCount(9, 1);
|
||||
tstr.TestDigitCount(10, 2);
|
||||
tstr.TestDigitCount(100, 3);
|
||||
tstr.TestDigitCount(10000, 5);
|
||||
tstr.TestDigitCount(100000, 6);
|
||||
tstr.TestDigitCount(1000000, 7);
|
||||
tstr.TestDigitCount(1000000000, 10);
|
||||
tstr.TestDigitCount(10000000000L, 11);
|
||||
tstr.TestDigitCount(100000000000L, 12);
|
||||
tstr.TestDigitCount(10000000000000000L, 17);
|
||||
tstr.TestDigitCount(-1, 2);
|
||||
}
|
||||
@Test public void Int_merge() {
|
||||
tstr.TestInt_merge(123, 456, 528280977864L);
|
||||
tstr.TestInt_merge(123, 457, 528280977865L);
|
||||
}
|
||||
@Test public void parse_or() {
|
||||
tstr.parse_or_tst("10000000000", 10000000000L);
|
||||
}
|
||||
}
|
||||
class LongUtlTstr {
|
||||
public void TestDigitCount(long val, int expd) {GfoTstr.EqLong(expd, LongUtl.DigitCount(val));}
|
||||
public void TestInt_merge(int hi, int lo, long expd) {
|
||||
GfoTstr.EqLong(expd, LongUtl.IntMerge(hi, lo));
|
||||
GfoTstr.EqLong(hi, LongUtl.IntSplitHi(expd));
|
||||
GfoTstr.EqLong(lo, LongUtl.IntSplitLo(expd));
|
||||
}
|
||||
public void parse_or_tst(String raw, long expd) {GfoTstr.EqLong(expd, LongUtl.ParseOr(raw, -1));}
|
||||
}
|
||||
@@ -14,7 +14,9 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.strings;
|
||||
import gplx.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.ObjectUtl;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.utls.StringUtl;
|
||||
import org.junit.Test;
|
||||
public class StringUtlTest {
|
||||
private final StringUtlTstr fxt = new StringUtlTstr();
|
||||
@@ -22,6 +24,9 @@ public class StringUtlTest {
|
||||
fxt.TestLen("" , 0);
|
||||
fxt.TestLen("abc", 3);
|
||||
}
|
||||
@Test public void X() {
|
||||
GfoTstr.Write("k".compareTo("a"));
|
||||
}
|
||||
@Test public void Format() {
|
||||
fxt.TestFormat("empty fmt" , "" , "");
|
||||
fxt.TestFormat("empty fmt w/ args" , "" , "", "a");
|
||||
@@ -37,12 +42,112 @@ public class StringUtlTest {
|
||||
fxt.TestFormat("invalid and valid args" , "{a}0{b}1", "{a}{0}{b}{1}", 0, 1);
|
||||
fxt.TestFormat("dangling" , "{0" , "{0", 0);
|
||||
}
|
||||
@Test public void LimitToFirst() {
|
||||
TestLimitToFirst("abc", 0, "");
|
||||
TestLimitToFirst("abc", 1, "a");
|
||||
TestLimitToFirst("abc", 2, "ab");
|
||||
TestLimitToFirst("abc", 3, "abc");
|
||||
TestLimitToFirst("abc", 4, "abc");
|
||||
TestLimitToFirst("abc", -1);
|
||||
}
|
||||
void TestLimitToFirst(String s, int v, String expd) {GfoTstr.Eq(expd, StringUtl.LimitToFirst(s, v));}
|
||||
void TestLimitToFirst(String s, int v) {try {StringUtl.LimitToFirst(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
|
||||
@Test public void DelBgn() {
|
||||
TestDelBgn("abc", 0, "abc");
|
||||
TestDelBgn("abc", 1, "bc");
|
||||
TestDelBgn("abc", 2, "c");
|
||||
TestDelBgn("abc", 3, "");
|
||||
TestDelBgn(null, 0);
|
||||
TestDelBgn("abc", 4);
|
||||
}
|
||||
void TestDelBgn(String s, int v, String expd) {GfoTstr.Eq(expd, StringUtl.DelBgn(s, v));}
|
||||
void TestDelBgn(String s, int v) {try {StringUtl.DelBgn(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
|
||||
@Test public void DelEnd() {
|
||||
TestDelEnd("abc", 0, "abc");
|
||||
TestDelEnd("abc", 1, "ab");
|
||||
TestDelEnd("abc", 2, "a");
|
||||
TestDelEnd("abc", 3, "");
|
||||
TestDelEnd(null, 0);
|
||||
TestDelEnd("abc", 4);
|
||||
}
|
||||
void TestDelEnd(String s, int v, String expd) {GfoTstr.Eq(expd, StringUtl.DelEnd(s, v));}
|
||||
void TestDelEnd(String s, int v) {try {StringUtl.DelEnd(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
|
||||
@Test public void DelEndIf() {
|
||||
TestDelEndIf("abc", "", "abc");
|
||||
TestDelEndIf("abc", "c", "ab");
|
||||
TestDelEndIf("abc", "bc", "a");
|
||||
TestDelEndIf("abc", "abc", "");
|
||||
TestDelEndIf("abc", "abcd", "abc");
|
||||
TestDelEndIf("abc", "ab", "abc");
|
||||
TestDelEndIf(null, "");
|
||||
TestDelEndIf("", null);
|
||||
}
|
||||
void TestDelEndIf(String s, String v, String expd) {GfoTstr.Eq(expd, StringUtl.DelEndIf(s, v));}
|
||||
void TestDelEndIf(String s, String v) {try {StringUtl.DelEndIf(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
|
||||
@Test public void MidByPos() {
|
||||
TestMidByPos("abc", 0, 0, "");
|
||||
TestMidByPos("abc", 0, 1, "a");
|
||||
TestMidByPos("abc", 0, 2, "ab");
|
||||
TestMidByPos("abc", 0, 3, "abc");
|
||||
TestMidByPos("abc", 2, 3, "c");
|
||||
TestMidByPos("abc", 1, 5);
|
||||
// TestMidByPos("abc", 0, 4);
|
||||
}
|
||||
void TestMidByPos(String s, int bgn, int end, String expd) {GfoTstr.Eq(expd, StringUtl.Mid(s, bgn, end));}
|
||||
void TestMidByPos(String s, int bgn, int end) {try {StringUtl.Mid(s, bgn, end);} catch (Exception e) {GfoTstr.EqErr(e, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
|
||||
|
||||
@Test public void Count() {
|
||||
String text = "0 0 0";
|
||||
GfoTstr.Eq(3, StringUtl.Count(text, "0"));
|
||||
}
|
||||
@Test public void Has() {
|
||||
String text = "find word";
|
||||
GfoTstr.EqBoolY(StringUtl.Has(text, "word"));
|
||||
GfoTstr.EqBoolN(StringUtl.Has(text, "nothing"));
|
||||
}
|
||||
@Test public void Repeat() {
|
||||
GfoTstr.Eq("333", StringUtl.Repeat("3", 3));
|
||||
}
|
||||
@Test public void Split() {
|
||||
TestSplit("ab", " ", "ab"); // no match -> return array with original input
|
||||
TestSplit("ab cd", " ", "ab", "cd"); // separator.length = 1
|
||||
TestSplit("ab+!cd", "+!", "ab", "cd"); // separator.length = 2
|
||||
TestSplit("ab+!cd+!ef", "+!", "ab", "cd", "ef"); // terms = 3
|
||||
TestSplit("ab+!cd+!", "+!", "ab", "cd", ""); // closing separator
|
||||
TestSplit("+!ab", "+!", "", "ab"); // opening separator
|
||||
TestSplit("ab+cd+!ef", "+!", "ab+cd", "ef"); // ignore partial matches
|
||||
TestSplit("ab+!cd+", "+!", "ab", "cd+"); // ignore partial matches; end of String
|
||||
|
||||
// boundary
|
||||
TestSplit("ab", "", "ab"); // separator.length = 0 -> return array with input as only member
|
||||
TestSplit("", " ", ""); // empty input -> return array with empty input
|
||||
|
||||
// acceptance
|
||||
TestSplit("this\r\nis\na\rtest\r\n.", "\r\n", "this", "is\na\rtest", ".");
|
||||
} void TestSplit(String text, String separator, String... expd) {GfoTstr.EqLines(expd, StringUtl.Split(text, separator));}
|
||||
@Test public void Concat_with_obj() {
|
||||
TestConcatWith_any("a|b", "|", "a", "b"); // do not append final delimiter
|
||||
TestConcatWith_any("a||c", "|", "a", null, "c"); // null
|
||||
TestConcatWith_any("a|b", "|", ObjectUtl.Ary("a", "b")); // pass array as arg
|
||||
} void TestConcatWith_any(String expd, String delimiter, Object... array) {GfoTstr.Eq(expd, StringUtl.ConcatWithObj(delimiter, array));}
|
||||
@Test public void FindBwd() { // WORKAROUND.CS:String.LastIndexOf returns -1 for multi-chars;
|
||||
TestFindRev("abc", "a", 0, 0);
|
||||
TestFindRev("abc", "ab", 0, 0); // 2 chars
|
||||
TestFindRev("abc", "abc", 0, 0); // 3 chars
|
||||
TestFindRev("ab", "abc", 0, -1); // out of index error
|
||||
TestFindRev("ababab", "ab", 2, 2); // make sure cs implementation doesn't pick up next
|
||||
} void TestFindRev(String s, String find, int pos, int expd) {GfoTstr.Eq(expd, StringUtl.FindBwd(s, find, pos));}
|
||||
@Test public void Extract_after_bwd() {
|
||||
Extract_after_bwd_tst("a/b", "/", "b");
|
||||
Extract_after_bwd_tst("a/", "/", "");
|
||||
Extract_after_bwd_tst("a", "/", "");
|
||||
} void Extract_after_bwd_tst(String src, String dlm, String expd) {GfoTstr.Eq(expd, StringUtl.ExtractAfterBwd(src, dlm));}
|
||||
}
|
||||
class StringUtlTstr {
|
||||
public void TestLen(String v, int expd) {
|
||||
GfoTstr.EqInt(expd, StringUtl.Len(v));
|
||||
GfoTstr.Eq(expd, StringUtl.Len(v));
|
||||
}
|
||||
public void TestFormat(String note, String expd, String fmt, Object... ary) {
|
||||
GfoTstr.EqStr(expd, StringUtl.Format(fmt, ary), note);
|
||||
GfoTstr.Eq(expd, StringUtl.Format(fmt, ary), note);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.objects.strings.unicodes;
|
||||
import gplx.objects.errs.ErrUtl;
|
||||
import gplx.tests.GfoTstr;
|
||||
import gplx.frameworks.tests.GfoTstr;
|
||||
import gplx.types.basics.strings.unicodes.Ustring;
|
||||
import gplx.types.basics.strings.unicodes.UstringUtl;
|
||||
import org.junit.Test;
|
||||
public class UstringUtlTest {
|
||||
private final UstringTstr fxt = new UstringTstr();
|
||||
@@ -58,8 +59,8 @@ class UstringTstr {
|
||||
this.under = UstringUtl.NewCodepoints(src);
|
||||
}
|
||||
public void TestLen(int expdCodes, int expdChars) {
|
||||
GfoTstr.EqInt(expdCodes, under.LenInData(), "codes");
|
||||
GfoTstr.EqInt(expdChars, under.LenInChars(), "chars");
|
||||
GfoTstr.Eq(expdCodes, under.LenInData(), "codes");
|
||||
GfoTstr.Eq(expdChars, under.LenInChars(), "chars");
|
||||
}
|
||||
public void TestGetCode(int... expd) {
|
||||
int actlLen = under.LenInData();
|
||||
@@ -85,7 +86,6 @@ class UstringTstr {
|
||||
}
|
||||
catch (Exception exc) {
|
||||
val = -1;
|
||||
ErrUtl.Noop(exc);
|
||||
}
|
||||
actl[i] = val;
|
||||
}
|
||||
@@ -95,11 +95,11 @@ class UstringTstr {
|
||||
Ustring src = UstringUtl.NewCodepoints(srcStr);
|
||||
Ustring find = UstringUtl.NewCodepoints(findStr);
|
||||
int actl = src.IndexOf(find, bgn);
|
||||
GfoTstr.EqInt(expd, actl);
|
||||
GfoTstr.Eq(expd, actl);
|
||||
}
|
||||
public void TestSubstring(String srcStr, int bgn, int end, String expd) {
|
||||
Ustring src = UstringUtl.NewCodepoints(srcStr);
|
||||
String actl = src.Substring(bgn, end);
|
||||
GfoTstr.EqStr(expd, actl);
|
||||
GfoTstr.Eq(expd, actl);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user