1
0
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:
gnosygnu
2021-12-19 16:19:19 -05:00
parent 48559edffe
commit 0e80d7ef6d
7999 changed files with 1375876 additions and 1365947 deletions

View File

@@ -0,0 +1,15 @@
package gplx.apis.types;
import gplx.frameworks.tests.GfoTstr;
import gplx.types.basics.utls.ArrayLni;
import org.junit.Test;
public class ArrayLniTest {
private final ArrayApiTstr tstr = new ArrayApiTstr();
@Test public void ResizeAddAry() {
tstr.TestResizeAddAry("basic" , new String[] {"a", "b", "c"}, new String[] {"d", "e"}, new String[] {"a", "b", "c", "d", "e"});
tstr.TestResizeAddAry("add.empty", new String[] {"a", "b", "c"}, new String[0], new String[] {"a", "b", "c"});
tstr.TestResizeAddAry("add.null" , new String[] {"a", "b", "c"}, null, new String[] {"a", "b", "c"});
}
}
class ArrayApiTstr {
public void TestResizeAddAry(String note, String[] src, String[] add, String[] expd) {GfoTstr.EqLines(expd, (String[])ArrayLni.ResizeAddAry(src, add), note);}
}

View 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));}
}

View File

@@ -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;}
}

View File

@@ -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));}
}

View 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
}

View File

@@ -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));}
}

View File

@@ -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)));}
}

View 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));}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,60 @@
/*
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.types.basics.utls;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class MathUtlTest {
@Test public void Abs() {
TestAbs(1, 1);
TestAbs(-1, 1);
TestAbs(0, 0);
} void TestAbs(int val, int expd) {GfoTstr.Eq(expd, MathUtl.Abs(val));}
@Test public void Log10() {
TestLog10(0, IntUtl.MinValue);
TestLog10(9, 0);
TestLog10(10, 1);
TestLog10(99, 1);
TestLog10(100, 2);
} void TestLog10(int val, int expd) {GfoTstr.Eq(expd, MathUtl.Log10(val));}
@Test public void Min() {
TestMin(0, 1, 0);
TestMin(1, 0, 0);
TestMin(0, 0, 0);
} void TestMin(int val0, int val1, int expd) {GfoTstr.Eq(expd, MathUtl.Min(val0, val1));}
@Test public void Pow() {
TestPow(2, 0, 1);
TestPow(2, 1, 2);
TestPow(2, 2, 4);
} void TestPow(int val, int exponent, double expd) {GfoTstr.EqDouble(expd, MathUtl.Pow(val, exponent));}
@Test public void Mult() {
TestMult(100, .01f, 1);
} void TestMult(int val, float multiplier, int expd) {GfoTstr.Eq(expd, IntUtl.Mult(val, multiplier));}
@Test public void Base2Ary() {
TestBase2Ary( 1, 256, 1);
TestBase2Ary( 2, 256, 2);
TestBase2Ary( 3, 256, 1, 2);
TestBase2Ary( 4, 256, 4);
TestBase2Ary( 5, 256, 1, 4);
TestBase2Ary( 6, 256, 2, 4);
TestBase2Ary(511, 256, 1, 2, 4, 8, 16, 32, 64, 128, 256);
} void TestBase2Ary(int v, int max, int... expd) {GfoTstr.EqAry(expd, MathUtl.Base2Ary(v, max));}
@Test public void Round() {
TestRound(1.5 , 0, 2);
TestRound(2.5 , 0, 3);
TestRound(2.123 , 2, 2.12);
TestRound(21.1 , -1, 20);
} void TestRound(double v, int places, double expd) {GfoTstr.EqDouble(expd, MathUtl.Round(v, places));}
}

View 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.types.commons;
import gplx.types.basics.utls.BryUtl;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoDateParserTest {
private final GfoDateParserTstr tstr = new GfoDateParserTstr();
@Test public void Parse_gplx() {
tstr.TestParseIso8651Like("2000-01-02T03:04:05.006-05:00" , 2000, 1, 2, 3, 4, 5, 6);
tstr.TestParseIso8651Like("2000-01-02" , 2000, 1, 2, 0, 0, 0, 0);
}
}
class GfoDateParserTstr {
private int[] actl = new int[7]; // keep as member variable to test .Clear
public void TestParseIso8651Like(String s, int... expd) {
GfoDateParser parser = new GfoDateParser();
byte[] bry = BryUtl.NewA7(s);
parser.ParseIso8651Like(actl, bry, 0, bry.length);
GfoTstr.EqAry(expd, actl, s);
}
}

View File

@@ -0,0 +1,80 @@
/*
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.types.commons;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoDateUtlTest {
private final GfoDateUtlTstr tstr = new GfoDateUtlTstr();
@Test public void ParseGplx() {
tstr.TestParseGplx("99991231_235959.999" , "99991231_235959.999");
tstr.TestParseGplx("20090430_213200.123" , "20090430_213200.123");
tstr.TestParseGplx("20090430_213200" , "20090430_213200.000");
tstr.TestParseGplx("20090430" , "20090430_000000.000");
}
@Test public void ParseSeparators() {
tstr.TestParseGplx("2009-04-30 21:32:00.123", "20090430_213200.123");
tstr.TestParseGplx("2009-04-30 21:32:00" , "20090430_213200.000");
tstr.TestParseGplx("2009-04-30" , "20090430_000000.000");
}
@Test public void ParseUtc() {
tstr.TestParseGplx("2015-12-26T10:03:53Z" , "20151226_100353.000");
}
@Test public void DayOfWeek() {
tstr.TestDayOfWeek("2012-01-18", 3); //3=Wed
}
@Test public void WeekOfYear() {
tstr.TestWeekOfYear("2006-02-01", 5); // 1-1:Sun;2-1:Wed
tstr.TestWeekOfYear("2007-02-01", 5); // 1-1:Mon;2-1:Thu
tstr.TestWeekOfYear("2008-02-01", 5); // 1-1:Tue;2-1:Fri
tstr.TestWeekOfYear("2009-02-01", 6); // 1-1:Thu;2-1:Sun
tstr.TestWeekOfYear("2010-02-01", 6); // 1-1:Fri;2-1:Mon
tstr.TestWeekOfYear("2011-02-01", 6); // 1-1:Sat;2-1:Tue
}
@Test public void DayOfYear() {
tstr.TestDayOfYear("2012-01-01", 1);
tstr.TestDayOfYear("2012-02-29", 60);
tstr.TestDayOfYear("2012-12-31", 366);
}
@Test public void Timestamp_unix() {
tstr.TestTimestampUnix("1970-01-01 00:00:00", 0);
tstr.TestTimestampUnix("2012-01-01 00:00:00", 1325376000);
}
@Test public void DaysInMonth() {
tstr.TestDaysInMonth("2012-01-01", 31);
tstr.TestDaysInMonth("2012-02-01", 29);
tstr.TestDaysInMonth("2012-04-01", 30);
tstr.TestDaysInMonth("2011-02-01", 28);
}
@Test public void ToUtc() {
tstr.TestToUtc("2012-01-01 00:00", "2012-01-01 05:00"); //4=Wed
}
@Test public void TimezoneId() {
tstr.TestTimezoneId("2015-12-26T10:03:53Z", "UTC");
}
}
class GfoDateUtlTstr {
public void TestParseGplx(String raw, String expd) {GfoTstr.Eq(expd, GfoDateUtl.ParseGplx(raw).ToStrGplx());}
public void TestDayOfWeek(String raw, int expd) {GfoTstr.Eq(expd, GfoDateUtl.ParseGplx(raw).DayOfWeek());}
public void TestWeekOfYear(String raw, int expd) {GfoTstr.Eq(expd, GfoDateUtl.ParseGplx(raw).WeekOfYear());}
public void TestDayOfYear(String raw, int expd) {GfoTstr.Eq(expd, GfoDateUtl.ParseGplx(raw).DayOfYear());}
public void TestDaysInMonth(String raw, int expd) {
GfoDate date = GfoDateUtl.ParseGplx(raw);
GfoTstr.Eq(expd, GfoDateUtl.DaysInMonth(date.Year(), date.Month()));
}
public void TestTimestampUnix(String raw, long expd) {GfoTstr.EqLong(expd, GfoDateUtl.ParseGplx(raw).TimestampUnix());}
public void TestToUtc(String raw, String expd) {GfoTstr.Eq(expd, GfoDateUtl.ParseGplx(raw).ToUtc().ToStrFmt_yyyy_MM_dd_HH_mm());}
public void TestTimezoneId(String raw, String expd) {GfoTstr.Eq(expd, GfoDateUtl.ParseGplx(raw).ToUtc().TimezoneId());}
}

View File

@@ -0,0 +1,92 @@
/*
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.types.commons;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoDecimalTest {
private final GfoDecimalTstr tstr = new GfoDecimalTstr();
@Test public void Divide() {
tstr.TestDivide(1, 1000, "0.001");
tstr.TestDivide(1, 3, "0.33333333333333");
tstr.TestDivide(1, 7, "0.14285714285714");
}
@Test public void Base1000() {
tstr.TestBase1000(1000, "1");
tstr.TestBase1000(1234, "1.234");
tstr.TestBase1000(123, "0.123");
}
@Test public void Parts() {
tstr.TestParts(1, 0, "1");
tstr.TestParts(1, 2, "1.2");
tstr.TestParts(1, 23, "1.23");
tstr.TestParts(123, 4567, "123.4567");
}
@Test public void Parse() {
tstr.TestParse("1", "1");
tstr.TestParse("1.2", "1.2");
tstr.TestParse("0.1", "0.1");
tstr.TestParse("1.2E1", "12");
tstr.TestParse("1.2e1", "12"); // 2020-08-27|ISSUE#:565|Parse 'e' as 'E'; PAGE:en.w:Huntington_Plaza
}
@Test public void ParseDot() {
tstr.TestParse(".", "0"); // 2021-02-13|ISSUE#:838|Parse '.' as '0.'; PAGE:en.w:2019_FIVB_Volleyball_Women%27s_Challenger_Cup#Pool_A
}
@Test public void TruncateDecimal() {
tstr.TestTruncateDecimal("1", "1");
tstr.TestTruncateDecimal("1.1", "1");
tstr.TestTruncateDecimal("1.9", "1");
}
@Test public void Fraction1000() {
tstr.TestFrac1000(1, 1000, 1); // 0.001
tstr.TestFrac1000(1, 3, 333); // 0.33333333
tstr.TestFrac1000(1234, 1000, 234); // 1.234
tstr.TestFrac1000(12345, 10000, 234); // 1.2345
}
@Test public void Lt() {
tstr.TestCompLt(1,123, 2, true);
tstr.TestCompLt(1,99999999, 2, true);
}
@Test public void ToStrFmt() {
tstr.TestToStrFmt(1, 2, "0.0", "0.5");
tstr.TestToStrFmt(1, 3, "0.0", "0.3");
tstr.TestToStrFmt(10000, 7, "0,000.000", "1,428.571");
tstr.TestToStrFmt(1, 2, "00.00", "00.50");
}
@Test public void Round() {
tstr.TestRound("123.456", 3, "123.456");
tstr.TestRound("123.456", 2, "123.46");
tstr.TestRound("123.456", 1, "123.5");
tstr.TestRound("123.456", 0, "123");
tstr.TestRound("123.456", -1, "120");
tstr.TestRound("123.456", -2, "100");
tstr.TestRound("123.456", -3, "0");
tstr.TestRound("6", -1, "10");
tstr.TestRound("5", -1, "10");
tstr.TestRound("6", -2, "0");
}
}
class GfoDecimalTstr {
public void TestDivide(int lhs, int rhs, String expd) {GfoTstr.Eq(expd, GfoDecimalUtl.NewByDivide(lhs, rhs).ToStr());}
public void TestBase1000(int val, String expd) {GfoTstr.Eq(expd, GfoDecimalUtl.NewByBase1000(val).ToStr());}
public void TestParts(int num, int fracs, String expd) {GfoTstr.Eq(expd, GfoDecimalUtl.NewByParts(num, fracs).ToStr());}
public void TestParse(String raw, String expd) {GfoTstr.Eq(expd, GfoDecimalUtl.Parse(raw).ToStr());}
public void TestTruncateDecimal(String raw, String expd) {GfoTstr.Eq(GfoDecimalUtl.Parse(expd).ToStr(), GfoDecimalUtl.Parse(raw).Truncate().ToStr());}
public void TestFrac1000(int lhs, int rhs, int expd) {GfoTstr.Eq(expd, GfoDecimalUtl.NewByDivide(lhs, rhs).Frac1000());}
public void TestCompLt(int lhsNum, int lhsFrc, int rhs, boolean expd) {GfoTstr.Eq(expd, GfoDecimalUtl.NewByParts(lhsNum, lhsFrc).CompLt(rhs));}
public void TestToStrFmt(int l, int r, String fmt, String expd) {GfoTstr.Eq(expd, GfoDecimalUtl.NewByDivide(l, r).ToStr(fmt));}
public void TestRound(String raw, int places, String expd) {GfoTstr.Eq(expd, GfoDecimalUtl.Parse(raw).Round(places).ToStr(), "round");}
}

View File

@@ -0,0 +1,27 @@
/*
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.types.commons;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoGuidTest {
@Test public void parse() {
TestParse("467ffb41-cdfe-402f-b22b-be855425784b");
}
private void TestParse(String s) {
GfoGuid uuid = GfoGuidUtl.Parse(s);
GfoTstr.Eq(uuid.ToStr(), s);
}
}

View File

@@ -0,0 +1,87 @@
/*
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.types.commons;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoTimeSpanBasicTest {
@Test public void Seconds() {
GfoTimeSpan expd = GfoTimeSpanUtl.NewFracs(123987);
GfoTimeSpan actl = GfoTimeSpanUtl.NewSeconds(123.987);
GfoTstr.EqObj(expd, actl);
}
@Test public void TotalSecs() {
GfoTimeSpan val = GfoTimeSpanUtl.NewFracs(1987);
GfoTstr.EqObj(GfoDecimalUtl.NewByParts(1, 987), val.TotalSecs());
}
@Test public void Units() {
TestUnits("01:02:03.987", 1, 2, 3, 987);
TestUnits("01:00:03", 1, 0, 3, 0);
TestUnits("01:00:00.987", 1, 0, 0, 987);
TestUnits("02:00.987", 0, 2, 0, 987);
}
@Test public void Add() {
GfoTimeSpan val = GfoTimeSpanUtl.NewFracs(3);
GfoTimeSpan arg = GfoTimeSpanUtl.NewFracs(2);
GfoTimeSpan expd = GfoTimeSpanUtl.NewFracs(5);
GfoTimeSpan actl = val.Add(arg);
GfoTstr.EqObj(expd, actl);
}
@Test public void Subtract() {
GfoTimeSpan val = GfoTimeSpanUtl.NewFracs(3);
GfoTimeSpan arg = GfoTimeSpanUtl.NewFracs(2);
GfoTimeSpan expd = GfoTimeSpanUtl.NewFracs(1);
GfoTimeSpan actl = val.Subtract(arg);
GfoTstr.EqObj(expd, actl);
}
@Test public void Add_unit_identity() {
TestAddUnit("00:00:01.000", 0, 0, "00:00:01.000");
}
@Test public void Add_unit_basic() {
TestAddUnit("01:59:58.987", 0, 1013, "02:00:00.000");
TestAddUnit("01:59:58.987", 1, 2, "02:00:00.987");
TestAddUnit("01:59:58.987", 2, 1, "02:00:58.987");
TestAddUnit("01:59:58.987", 3, 1, "02:59:58.987");
}
@Test public void Add_unit_negative() {
TestAddUnit("01:00:00.00", 0, -1, "00:59:59.999");
TestAddUnit("01:00:00.00", 1, -1, "00:59:59.000");
TestAddUnit("01:00:00.00", 2, -1, "00:59:00.000");
TestAddUnit("01:00:00.00", 3, -1, "00:00:00.000");
}
@Test public void XtoStrUiAbbrv() {
TestToStrUiAbbrv("01:02:03.004", "1h 2m 3s 4f");
TestToStrUiAbbrv("00:00:03.004", "3s 4f");
TestToStrUiAbbrv("00:00:03.000", "3s 0f");
TestToStrUiAbbrv("11:22:33.444", "11h 22m 33s 444f");
TestToStrUiAbbrv("00:00:00.000", "0f");
}
private void TestToStrUiAbbrv(String raw, String expd) {GfoTstr.EqObj(expd, GfoTimeSpanUtl.Parse(raw).ToStrUiAbrv());}
private void TestAddUnit(String valRaw, int unitIdx, int delta, String expdRaw) {
GfoTimeSpan val = GfoTimeSpanUtl.Parse(valRaw);
GfoTimeSpan actl = val.AddUnit(unitIdx, delta);
GfoTstr.EqObj(GfoTimeSpanUtl.Parse(expdRaw), actl);
}
private void TestUnits(String text, int... expd) {
GfoTimeSpan val = GfoTimeSpanUtl.Parse(text);
int hour = 0, min = 0, sec = 0, frac = 0;
int[] ary = val.Units();
hour = ary[GfoTimeSpanUtl.Idx_Hour]; min = ary[GfoTimeSpanUtl.Idx_Min]; sec = ary[GfoTimeSpanUtl.Idx_Sec]; frac = ary[GfoTimeSpanUtl.Idx_Frac];
GfoTstr.EqObj(expd[0], hour);
GfoTstr.EqObj(expd[1], min);
GfoTstr.EqObj(expd[2], sec);
GfoTstr.EqObj(expd[3], frac);
}
}

View 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.types.commons;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoTimeSpanParseTest {
@Test public void Zero() {
TestParse("0", 0);
}
@Test public void Milliseconds() {
TestParse("0.987", 987);
TestParse("0.00199", 1); // do not parse as 199
TestParse("0.1", 100); // do not parse as 1
}
@Test public void Seconds() {
TestParse("1.987", 1987);
}
@Test public void Minutes() {
TestParse("1:02.987", 62987);
}
@Test public void MinuteSecondOnly() {
TestParse("1:02", 62000);
}
@Test public void Hour() {
TestParse("1:02:03.987", 3723987);
}
@Test public void Negative() {
TestParse("-1:02:03.987", -3723987);
}
@Test public void Loopholes() {
TestParse("001:02", 62000); // multiple leading zeroes
TestParse("1.2.3.4", 1200); // ignore all decimals except first
TestParse("60:60.9999", 3660999); // value does not need to be bounded to limits (except fracs, which is always < 1000)
TestParse(" 01 : 02 : 03 . 987", 3723987); // whitespace
}
private void TestParse(String text, long expd) {
GfoTimeSpan val = GfoTimeSpanUtl.Parse(text);
GfoTstr.EqLong(expd, val.Fracs());
}
}

View File

@@ -0,0 +1,43 @@
package gplx.types.commons;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoTimeSpanToStrTest {
@Test public void Zero() {
TestDefault(0, "0");
}
@Test public void MinuteSeconds() {
TestDefault(77000, "1:17");
}
@Test public void ZeroSuppression() {
TestDefault(660000, "11:00"); //fractional 0 and leading 0s are suppressed; i.e.: not 00:11:00.000
}
@Test public void HourTest() {
TestDefault(3723987, "1:02:03.987");
}
@Test public void NegSeconds() {
TestDefault(-2000, "-2");
}
@Test public void NegMins() {
TestDefault(-60000, "-1:00");
}
@Test public void NegHours() {
TestDefault(-3723981, "-1:02:03.981");
}
@Test public void ZeroPadding() {
TestZeroPadding("0", "00:00:00.000");
TestZeroPadding("1:02:03.123", "01:02:03.123");
TestZeroPadding("1", "00:00:01.000");
TestZeroPadding(".987", "00:00:00.987");
TestZeroPadding("2:01.456", "00:02:01.456");
}
private void TestDefault(long fractionals, String expd) {
GfoTimeSpan ts = GfoTimeSpanUtl.NewFracs(fractionals);
String actl = ts.ToStr(GfoTimeSpanUtl.Fmt_Default);
GfoTstr.Eq(expd, actl);
}
private void TestZeroPadding(String val, String expd) {
GfoTimeSpan timeSpan = GfoTimeSpanUtl.Parse(val);
String actl = timeSpan.ToStr(GfoTimeSpanUtl.Fmt_PadZeros);
GfoTstr.Eq(expd, actl);
}
}

View File

@@ -1,6 +1,6 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
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.
@@ -13,20 +13,20 @@ 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;
package gplx.types.commons.lists;
import gplx.types.basics.utls.ArrayUtl;
import gplx.frameworks.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));
GfoTstr.EqAryObjAry(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"});
GfoTstr.EqAryObjAry(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];

View File

@@ -13,8 +13,8 @@ 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;
package gplx.types.commons.lists;
import gplx.frameworks.tests.GfoTstr;
import org.junit.Test;
public class GfoIndexedListEntryTest {
private GfoIndexedList<String, String> list = new GfoIndexedList<>();
@@ -67,10 +67,10 @@ public class GfoIndexedListEntryTest {
testIterate("a", "bb", "c");
}
private void testGetByOrFail(String key, String expd) {
GfoTstr.EqStr(expd, list.GetByOrFail(key));
GfoTstr.Eq(expd, list.GetByOrFail(key));
}
private void testGetAt(int idx, String expd) {
GfoTstr.EqStr(expd, list.GetAt(idx));
GfoTstr.Eq(expd, list.GetAt(idx));
}
private void testIterate(String... expd) {
String[] actl = new String[expd.length];
@@ -78,6 +78,6 @@ public class GfoIndexedListEntryTest {
for (String itm : list) {
actl[i++] = itm;
}
GfoTstr.EqAry(expd, actl);
GfoTstr.EqLines(expd, actl);
}
}

View File

@@ -0,0 +1,94 @@
/*
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.types.custom.brys.rdrs;
import gplx.types.basics.utls.BryUtl;
import gplx.types.basics.constants.AsciiByte;
import gplx.frameworks.tests.GfoTstr;
import gplx.types.custom.brys.BryFind;
import org.junit.Test;
public class BryFindTest {
private BryFindTstr tstr = new BryFindTstr();
@Test public void FindFwd() {
tstr.TestFindFwd("abcba", "b", 0, 1);
tstr.TestFindFwd("abcba", "z", 0, -1);
tstr.TestFindFwd("abcba", "b", 1, 1);
tstr.TestFindFwd("abcba", "b", 2, 3);
tstr.TestFindFwd("abcba", "b", 4, -1);
tstr.TestFindFwd("abcba", "zb", 4, -1);
tstr.TestFindFwd("abcba", "a", 6, -1);
}
@Test public void FindBwd() {
tstr.TestFindBwd("abcba", "b", 4, 3);
tstr.TestFindBwd("abcba", "z", 4, -1);
tstr.TestFindBwd("abcba", "b", 3, 1);
tstr.TestFindBwd("abcba", "b", 2, 1);
tstr.TestFindBwd("abcba", "b", 0, -1);
tstr.TestFindBwd("abcba", "zb", 4, -1);
tstr.TestFindFwd("abcba", "a", -1, -1);
tstr.TestFindBwd("abcba", "ab", 4, 0);
}
@Test public void FindBwdLastWs() {
tstr.TestFindBwdLastWs("a b" , 2, 1); // basic
tstr.TestFindBwdLastWs("a b" , 3, 1); // multiple
tstr.TestFindBwdLastWs("ab" , 1, BryFind.NotFound); // none
}
@Test public void TrimFwdSpaceTab() {
tstr.TestTrimFwdSpaceTab(" a b" , 1);
tstr.TestTrimFwdSpaceTab("\ta b" , 1);
tstr.TestTrimFwdSpaceTab(" \ta b" , 2);
tstr.TestTrimFwdSpaceTab("a bc" , 0);
tstr.TestTrimFwdSpaceTab("" , 0);
tstr.TestTrimFwdSpaceTab(" \t" , 2);
}
@Test public void TrimBwdSpaceTab() {
tstr.TestTrimBwdSpaceTab("a b " , 3);
tstr.TestTrimBwdSpaceTab("a b\t" , 3);
tstr.TestTrimBwdSpaceTab("a b\t " , 3);
tstr.TestTrimBwdSpaceTab("a bc" , 4);
tstr.TestTrimBwdSpaceTab("" , 0);
tstr.TestTrimBwdSpaceTab(" \t" , 0);
}
@Test public void FindFwdWhileIn() {
boolean[] while_ary = tstr.InitFindFwdWhileIn(AsciiByte.Space, AsciiByte.Tab, AsciiByte.Nl);
tstr.TestFindFwdWhileIn(" \t\na", while_ary, 3);
}
}
class BryFindTstr {
public void TestFindFwd(String src, String lkp, int bgn, int expd) {GfoTstr.Eq(expd, BryFind.FindFwd(BryUtl.NewU8(src), BryUtl.NewU8(lkp), bgn));}
public void TestFindBwd(String src, String lkp, int bgn, int expd) {GfoTstr.Eq(expd, BryFind.FindBwd(BryUtl.NewU8(src), BryUtl.NewU8(lkp), bgn));}
public void TestFindBwdLastWs(String src, int pos, int expd) {GfoTstr.Eq(expd, BryFind.FindBwdLastWs(BryUtl.NewA7(src), pos));}
public void TestTrimBwdSpaceTab(String raw_str, int expd) {
byte[] raw_bry = BryUtl.NewU8(raw_str);
int actl = BryFind.TrimBwdSpaceTab(raw_bry, raw_bry.length, 0);
GfoTstr.Eq(expd, actl, raw_str);
}
public void TestTrimFwdSpaceTab(String raw_str, int expd) {
byte[] raw_bry = BryUtl.NewU8(raw_str);
int actl = BryFind.TrimFwdSpaceTab(raw_bry, 0, raw_bry.length);
GfoTstr.Eq(expd, actl, raw_str);
}
public boolean[] InitFindFwdWhileIn(byte... ary) {
boolean[] rv = new boolean[256];
int len = ary.length;
for (int i = 0; i < len; i++)
rv[ary[i]] = true;
return rv;
}
public void TestFindFwdWhileIn(String src, boolean[] ary, int expd) {
byte[] src_bry = BryUtl.NewU8(src);
GfoTstr.Eq(expd, BryFind.FindFwdWhileIn(src_bry, 0, src_bry.length, ary));
}
}