mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Refactor: Refactor baselib; merge Array_ and Bool_
This commit is contained in:
43
baselib/tst/gplx/objects/arrays/ArrayUtlTest.java
Normal file
43
baselib/tst/gplx/objects/arrays/ArrayUtlTest.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
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;}
|
||||
}
|
||||
37
baselib/tst/gplx/objects/lists/ComparerAbleSorterTest.java
Normal file
37
baselib/tst/gplx/objects/lists/ComparerAbleSorterTest.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
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,70 +1,83 @@
|
||||
/*
|
||||
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.Gftest_fxt;
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
list.DelBy("A");
|
||||
|
||||
testIterate("b", "c");
|
||||
testIterate("b", "c");
|
||||
|
||||
list.DelBy("B");
|
||||
list.DelBy("B");
|
||||
|
||||
testIterate("c");
|
||||
testIterate("c");
|
||||
|
||||
list.DelBy("C");
|
||||
list.DelBy("C");
|
||||
|
||||
testIterate();
|
||||
}
|
||||
@Test public void DelBy_SameVal() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "a");
|
||||
testIterate();
|
||||
}
|
||||
@Test public void DelBySameVal() {
|
||||
list.Add("A", "a");
|
||||
list.Add("B", "b");
|
||||
list.Add("C", "a");
|
||||
|
||||
list.DelBy("C");
|
||||
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");
|
||||
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) {
|
||||
Gftest_fxt.Eq__str(expd, list.GetByOrFail(key));
|
||||
}
|
||||
private void testGetAt(int idx, String expd) {
|
||||
Gftest_fxt.Eq__str(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;
|
||||
}
|
||||
Gftest_fxt.Eq__ary(expd, actl);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
31
baselib/tst/gplx/objects/primitives/BoolUtlTest.java
Normal file
31
baselib/tst/gplx/objects/primitives/BoolUtlTest.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
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.objects.lists.CompareAbleUtl;
|
||||
import gplx.tests.GfoTstr;
|
||||
import org.junit.*;
|
||||
public class BoolUtlTest {
|
||||
private final BoolUtlTstr fxt = new BoolUtlTstr();
|
||||
@Test public void Compare() {
|
||||
fxt.TestCompare(BoolUtl.Y, BoolUtl.Y, CompareAbleUtl.Same);
|
||||
fxt.TestCompare(BoolUtl.N, BoolUtl.N, CompareAbleUtl.Same);
|
||||
fxt.TestCompare(BoolUtl.N, BoolUtl.Y, CompareAbleUtl.Less);
|
||||
fxt.TestCompare(BoolUtl.Y, BoolUtl.N, CompareAbleUtl.More);
|
||||
}
|
||||
}
|
||||
class BoolUtlTstr {
|
||||
public void TestCompare(boolean lhs, boolean rhs, int expd) {GfoTstr.EqInt(expd, BoolUtl.Compare(lhs, rhs));}
|
||||
}
|
||||
49
baselib/tst/gplx/objects/primitives/DoubleUtlTest.java
Normal file
49
baselib/tst/gplx/objects/primitives/DoubleUtlTest.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
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.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class DoubleUtlTest {
|
||||
private final DoubleUtlTstr tstr = new DoubleUtlTstr();
|
||||
|
||||
@Test
|
||||
public void TrimZeroes() {
|
||||
tstr.TestTrimZeroes("12.100" , "12.1");
|
||||
tstr.TestTrimZeroes("12.000" , "12");
|
||||
tstr.TestTrimZeroes("12.001" , "12.001");
|
||||
tstr.TestTrimZeroes("1020.00" , "1020");
|
||||
tstr.TestTrimZeroes("1020.00" , "1020");
|
||||
tstr.TestTrimZeroes("1.200e5" , "1.2E5");
|
||||
tstr.TestTrimZeroes("1.200e-05" , "1.2E-5");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ToStrByPrintF() {
|
||||
tstr.TestToStrByPrintF(1d / 2d , "0.5"); // fails with 0.50000000000000
|
||||
tstr.TestToStrByPrintF(5d / 100000000000000000d, "5E-17"); // fails with 5.0000000000000e-17
|
||||
tstr.TestToStrByPrintF(7538000d / 7773352000d , "0.00096972322879499"); // fails with 0; ISSUE#:697; DATE:2020-08-11
|
||||
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
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2020 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.tests.Gftest_fxt;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Double_Test {
|
||||
private final Double_Tstr tstr = new Double_Tstr();
|
||||
|
||||
@Test
|
||||
public void TrimZeroes() {
|
||||
tstr.Test_TrimZeroes("12.100" , "12.1");
|
||||
tstr.Test_TrimZeroes("12.000" , "12");
|
||||
tstr.Test_TrimZeroes("12.001" , "12.001");
|
||||
tstr.Test_TrimZeroes("1020.00" , "1020");
|
||||
tstr.Test_TrimZeroes("1020.00" , "1020");
|
||||
tstr.Test_TrimZeroes("1.200e5" , "1.2E5");
|
||||
tstr.Test_TrimZeroes("1.200e-05" , "1.2E-5");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ToStrByPrintF() {
|
||||
tstr.Test_ToStrByPrintF(1d / 2d , "0.5"); // fails with 0.50000000000000
|
||||
tstr.Test_ToStrByPrintF(5d / 100000000000000000d, "5E-17"); // fails with 5.0000000000000e-17
|
||||
tstr.Test_ToStrByPrintF(7538000d / 7773352000d , "0.00096972322879499"); // fails with 0; ISSUE#:697; DATE:2020-08-11
|
||||
tstr.Test_ToStrByPrintF(56225d / 7776747000d , "7.2298867379895E-06"); // fails with 0; ISSUE#:697; DATE:2020-08-11
|
||||
tstr.Test_ToStrByPrintF(35746d / 7805411000d , "4.5796435319037E-06"); // fails with 0; ISSUE#:697; DATE:2020-08-11
|
||||
}
|
||||
}
|
||||
class Double_Tstr {
|
||||
public void Test_ToStrByPrintF(double v, String expd) {
|
||||
Gftest_fxt.Eq__str(expd, Double_.ToStrByPrintF(v));
|
||||
}
|
||||
public void Test_TrimZeroes(String val, String expd) {
|
||||
Gftest_fxt.Eq__str(expd, Double_.TrimZeroes(val));
|
||||
}
|
||||
}
|
||||
91
baselib/tst/gplx/objects/primitives/IntUtlTest.java
Normal file
91
baselib/tst/gplx/objects/primitives/IntUtlTest.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
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.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class IntUtlTest {
|
||||
private final IntUtlTstr fxt = new IntUtlTstr();
|
||||
@Test public void ParseOr() {
|
||||
fxt.TestParseOr("123", 123); // basic
|
||||
fxt.TestParseOrMinValue(null); // null
|
||||
fxt.TestParseOrMinValue(""); // empty
|
||||
fxt.TestParseOrMinValue("1a"); // invalid number
|
||||
|
||||
fxt.TestParseOr("-123", -123); // negative
|
||||
fxt.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
|
||||
}
|
||||
@Test public void CountDigits() {
|
||||
fxt.TestCountDigits( 0, 1);
|
||||
fxt.TestCountDigits( 9, 1);
|
||||
fxt.TestCountDigits( 100, 3);
|
||||
fxt.TestCountDigits( -1, 2);
|
||||
fxt.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);
|
||||
}
|
||||
}
|
||||
class IntUtlTstr {
|
||||
public void TestParseOr(String raw, int expd) {
|
||||
GfoTstr.EqInt(expd, IntUtl.ParseOr(raw, -1));
|
||||
}
|
||||
public void TestParseOrMinValue(String raw) {
|
||||
GfoTstr.EqInt(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));
|
||||
}
|
||||
public void TestCountDigits(int val, int expd) {
|
||||
GfoTstr.EqInt(expd, IntUtl.CountDigits(val), IntUtl.ToStr(val));
|
||||
}
|
||||
public void TestLog10(int val, int expd) {
|
||||
GfoTstr.EqInt(expd, IntUtl.Log10(val));
|
||||
}
|
||||
}
|
||||
48
baselib/tst/gplx/objects/strings/StringUtlTest.java
Normal file
48
baselib/tst/gplx/objects/strings/StringUtlTest.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.strings;
|
||||
import gplx.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class StringUtlTest {
|
||||
private final StringUtlTstr fxt = new StringUtlTstr();
|
||||
@Test public void Len() {
|
||||
fxt.TestLen("" , 0);
|
||||
fxt.TestLen("abc", 3);
|
||||
}
|
||||
@Test public void Format() {
|
||||
fxt.TestFormat("empty fmt" , "" , "");
|
||||
fxt.TestFormat("empty fmt w/ args" , "" , "", "a");
|
||||
fxt.TestFormat("no args" , "a" , "a");
|
||||
fxt.TestFormat("args = 1" , "a" , "{0}", "a");
|
||||
fxt.TestFormat("args = n" , "a + b" , "{0} + {1}", "a", "b");
|
||||
fxt.TestFormat("escape {" , "{" , "{{", 0);
|
||||
fxt.TestFormat("escape }" , "}" , "}}", 0);
|
||||
fxt.TestFormat("nested" , "{a0c}" , "{a{0}c}", 0);
|
||||
fxt.TestFormat("nested; invalid" , "{a{b}c}" , "{a{b}c}", 0);
|
||||
fxt.TestFormat("out of bounds" , "{1}" , "{1}", "a");
|
||||
fxt.TestFormat("invalid arg" , "{a} {b}" , "{a} {b}", 0);
|
||||
fxt.TestFormat("invalid and valid args" , "{a}0{b}1", "{a}{0}{b}{1}", 0, 1);
|
||||
fxt.TestFormat("dangling" , "{0" , "{0", 0);
|
||||
}
|
||||
}
|
||||
class StringUtlTstr {
|
||||
public void TestLen(String v, int expd) {
|
||||
GfoTstr.EqInt(expd, StringUtl.Len(v));
|
||||
}
|
||||
public void TestFormat(String note, String expd, String fmt, Object... ary) {
|
||||
GfoTstr.EqStr(expd, StringUtl.Format(fmt, ary), note);
|
||||
}
|
||||
}
|
||||
105
baselib/tst/gplx/objects/strings/unicodes/UstringUtlTest.java
Normal file
105
baselib/tst/gplx/objects/strings/unicodes/UstringUtlTest.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
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.strings.unicodes;
|
||||
import gplx.objects.errs.ErrUtl;
|
||||
import gplx.tests.GfoTstr;
|
||||
import org.junit.Test;
|
||||
public class UstringUtlTest {
|
||||
private final UstringTstr fxt = new UstringTstr();
|
||||
@Test public void Empty() {
|
||||
fxt.Init("");
|
||||
fxt.TestLen(0, 0);
|
||||
}
|
||||
@Test public void Blank() {
|
||||
fxt.Init("");
|
||||
fxt.TestLen(0, 0);
|
||||
}
|
||||
@Test public void Single() {
|
||||
fxt.Init("Abc");
|
||||
fxt.TestLen(3, 3);
|
||||
fxt.TestGetCode(65, 98, 99);
|
||||
fxt.TestMapCodeToChar(0, 1, 2, 3);
|
||||
fxt.TestMapCharToCode(0, 1, 2, 3);
|
||||
}
|
||||
@Test public void Multi() {
|
||||
fxt.Init("a¢€𤭢b");
|
||||
fxt.TestLen(5, 6);
|
||||
fxt.TestGetCode(97, 162, 8364, 150370, 98);
|
||||
fxt.TestMapCodeToChar(0, 1, 2, 3, 5, 6);
|
||||
fxt.TestMapCharToCode(0, 1, 2, 3, -1, 4, 5);
|
||||
}
|
||||
@Test public void IndexOf() {
|
||||
fxt.TestIndexOf("abc", "b", 0, 1); // basic
|
||||
fxt.TestIndexOf("ab", "bc", 0, -1); // out-of-bounds
|
||||
fxt.TestIndexOf("a¢e", "¢", 0, 1); // check UTF-8 strings still match at byte-level
|
||||
}
|
||||
|
||||
@Test public void Substring() {
|
||||
fxt.TestSubstring("abc", 1, 2, "b"); // basic
|
||||
fxt.TestSubstring("¢bc", 1, 2, "b"); // check UTF-8 strings don't get lopped off
|
||||
}
|
||||
}
|
||||
class UstringTstr {
|
||||
private Ustring under;
|
||||
public void Init(String src) {
|
||||
this.under = UstringUtl.NewCodepoints(src);
|
||||
}
|
||||
public void TestLen(int expdCodes, int expdChars) {
|
||||
GfoTstr.EqInt(expdCodes, under.LenInData(), "codes");
|
||||
GfoTstr.EqInt(expdChars, under.LenInChars(), "chars");
|
||||
}
|
||||
public void TestGetCode(int... expd) {
|
||||
int actlLen = under.LenInData();
|
||||
int[] actl = new int[actlLen];
|
||||
for (int i = 0; i < actlLen; i++)
|
||||
actl[i] = under.GetData(i);
|
||||
GfoTstr.EqAry(expd, actl);
|
||||
}
|
||||
public void TestMapCodeToChar(int... expd) {
|
||||
int actlLen = under.LenInData() + 1;
|
||||
int[] actl = new int[actlLen];
|
||||
for (int i = 0; i < actlLen; i++)
|
||||
actl[i] = under.MapDataToChar(i);
|
||||
GfoTstr.EqAry(expd, actl);
|
||||
}
|
||||
public void TestMapCharToCode(int... expd) {
|
||||
int actlLen = under.LenInChars() + 1;
|
||||
int[] actl = new int[actlLen];
|
||||
for (int i = 0; i < actlLen; i++) {
|
||||
int val = 0;
|
||||
try {
|
||||
val = under.MapCharToData(i);
|
||||
}
|
||||
catch (Exception exc) {
|
||||
val = -1;
|
||||
ErrUtl.Noop(exc);
|
||||
}
|
||||
actl[i] = val;
|
||||
}
|
||||
GfoTstr.EqAry(expd, actl);
|
||||
}
|
||||
public void TestIndexOf(String srcStr, String findStr, int bgn, int expd) {
|
||||
Ustring src = UstringUtl.NewCodepoints(srcStr);
|
||||
Ustring find = UstringUtl.NewCodepoints(findStr);
|
||||
int actl = src.IndexOf(find, bgn);
|
||||
GfoTstr.EqInt(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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user