String__tst.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2017 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx;
  13. import gplx.objects.lists.CompareAbleUtl;
  14. import org.junit.*;
  15. public class String__tst {
  16. @Test public void LimitToFirst() {
  17. tst_LimitToFirst("abc", 0, "");
  18. tst_LimitToFirst("abc", 1, "a");
  19. tst_LimitToFirst("abc", 2, "ab");
  20. tst_LimitToFirst("abc", 3, "abc");
  21. tst_LimitToFirst("abc", 4, "abc");
  22. err_LimitToFirst("abc", -1);
  23. }
  24. void tst_LimitToFirst(String s, int v, String expd) {Tfds.Eq(expd, String_.LimitToFirst(s, v));}
  25. void err_LimitToFirst(String s, int v) {try {String_.LimitToFirst(s, v);} catch (Exception exc) {Tfds.Err_classMatch(exc, Err.class); return;} Tfds.Fail_expdError();}
  26. @Test public void LimitToLast() {
  27. tst_LimitToLast("abc", 0, "");
  28. tst_LimitToLast("abc", 1, "c");
  29. tst_LimitToLast("abc", 2, "bc");
  30. tst_LimitToLast("abc", 3, "abc");
  31. tst_LimitToLast("abc", 4, "abc");
  32. err_LimitToLast("abc", -1);
  33. }
  34. void tst_LimitToLast(String s, int v, String expd) {Tfds.Eq(expd, String_.LimitToLast(s, v));}
  35. void err_LimitToLast(String s, int v) {try {String_.LimitToLast(s, v);} catch (Exception exc) {Tfds.Err_classMatch(exc, Err.class); return;} Tfds.Fail_expdError();}
  36. @Test public void DelBgn() {
  37. tst_DelBgn("abc", 0, "abc");
  38. tst_DelBgn("abc", 1, "bc");
  39. tst_DelBgn("abc", 2, "c");
  40. tst_DelBgn("abc", 3, "");
  41. err_DelBgn(null, 0);
  42. err_DelBgn("abc", 4);
  43. }
  44. void tst_DelBgn(String s, int v, String expd) {Tfds.Eq(expd, String_.DelBgn(s, v));}
  45. void err_DelBgn(String s, int v) {try {String_.DelBgn(s, v);} catch (Exception exc) {Tfds.Err_classMatch(exc, Err.class); return;} Tfds.Fail_expdError();}
  46. @Test public void DelBgnIf() {
  47. tst_DelBgnIf("abc", "", "abc");
  48. tst_DelBgnIf("abc", "a", "bc");
  49. tst_DelBgnIf("abc", "ab", "c");
  50. tst_DelBgnIf("abc", "abc", "");
  51. tst_DelBgnIf("abc", "abcd", "abc");
  52. tst_DelBgnIf("abc", "bcd", "abc");
  53. err_DelBgnIf(null, "abc");
  54. err_DelBgnIf("abc", null);
  55. }
  56. void tst_DelBgnIf(String s, String v, String expd) {Tfds.Eq(expd, String_.DelBgnIf(s, v));}
  57. void err_DelBgnIf(String s, String v) {try {String_.DelBgnIf(s, v);} catch (Exception exc) {Tfds.Err_classMatch(exc, Err.class); return;} Tfds.Fail_expdError();}
  58. @Test public void DelEnd() {
  59. tst_DelEnd("abc", 0, "abc");
  60. tst_DelEnd("abc", 1, "ab");
  61. tst_DelEnd("abc", 2, "a");
  62. tst_DelEnd("abc", 3, "");
  63. err_DelEnd(null, 0);
  64. err_DelEnd("abc", 4);
  65. }
  66. void tst_DelEnd(String s, int v, String expd) {Tfds.Eq(expd, String_.DelEnd(s, v));}
  67. void err_DelEnd(String s, int v) {try {String_.DelEnd(s, v);} catch (Exception exc) {Tfds.Err_classMatch(exc, Err.class); return;} Tfds.Fail_expdError();}
  68. @Test public void DelEndIf() {
  69. tst_DelEndIf("abc", "", "abc");
  70. tst_DelEndIf("abc", "c", "ab");
  71. tst_DelEndIf("abc", "bc", "a");
  72. tst_DelEndIf("abc", "abc", "");
  73. tst_DelEndIf("abc", "abcd", "abc");
  74. tst_DelEndIf("abc", "ab", "abc");
  75. err_DelEndIf(null, "");
  76. err_DelEndIf("", null);
  77. }
  78. void tst_DelEndIf(String s, String v, String expd) {Tfds.Eq(expd, String_.DelEndIf(s, v));}
  79. void err_DelEndIf(String s, String v) {try {String_.DelEndIf(s, v);} catch (Exception exc) {Tfds.Err_classMatch(exc, Err.class); return;} Tfds.Fail_expdError();}
  80. @Test public void MidByPos() {
  81. tst_MidByPos("abc", 0, 0, "");
  82. tst_MidByPos("abc", 0, 1, "a");
  83. tst_MidByPos("abc", 0, 2, "ab");
  84. tst_MidByPos("abc", 0, 3, "abc");
  85. tst_MidByPos("abc", 2, 3, "c");
  86. err_MidByPos("abc", 1, 5);
  87. // err_MidByPos("abc", 0, 4);
  88. }
  89. void tst_MidByPos(String s, int bgn, int end, String expd) {Tfds.Eq(expd, String_.Mid(s, bgn, end));}
  90. void err_MidByPos(String s, int bgn, int end) {try {String_.Mid(s, bgn, end);} catch (Exception e) {Tfds.Err_classMatch(e, Err.class); return;} Tfds.Fail_expdError();}
  91. @Test public void TrimEnd() {
  92. tst_TrimEnd("a", "a");
  93. tst_TrimEnd("a ", "a");
  94. tst_TrimEnd("a\t", "a");
  95. tst_TrimEnd("a\n", "a");
  96. tst_TrimEnd("a\r", "a");
  97. tst_TrimEnd("a\r\n \t", "a");
  98. tst_TrimEnd(" a", " a");
  99. tst_TrimEnd(null, null);
  100. }
  101. void tst_TrimEnd(String s, String expd) {Tfds.Eq(expd, String_.TrimEnd(s));}
  102. @Test public void Count() {
  103. String text = "0 0 0";
  104. Tfds.Eq(3, String_.Count(text, "0"));
  105. }
  106. @Test public void Has() {
  107. String text = "find word";
  108. Tfds.Eq_true(String_.Has(text, "word"));
  109. Tfds.Eq_false(String_.Has(text, "nothing"));
  110. }
  111. @Test public void Repeat() {
  112. Tfds.Eq("333", String_.Repeat("3", 3));
  113. }
  114. @Test public void Split() {
  115. tst_Split("ab", " ", "ab"); // no match -> return array with original input
  116. tst_Split("ab cd", " ", "ab", "cd"); // separator.length = 1
  117. tst_Split("ab+!cd", "+!", "ab", "cd"); // separator.length = 2
  118. tst_Split("ab+!cd+!ef", "+!", "ab", "cd", "ef"); // terms = 3
  119. tst_Split("ab+!cd+!", "+!", "ab", "cd", ""); // closing separator
  120. tst_Split("+!ab", "+!", "", "ab"); // opening separator
  121. tst_Split("ab+cd+!ef", "+!", "ab+cd", "ef"); // ignore partial matches
  122. tst_Split("ab+!cd+", "+!", "ab", "cd+"); // ignore partial matches; end of String
  123. // boundary
  124. tst_Split("ab", "", "ab"); // separator.length = 0 -> return array with input as only member
  125. tst_Split("", " ", ""); // empty input -> return array with empty input
  126. // acceptance
  127. tst_Split("this\r\nis\na\rtest\r\n.", "\r\n", "this", "is\na\rtest", ".");
  128. } void tst_Split(String text, String separator, String... expd) {Tfds.Eq_ary(expd, String_.Split(text, separator));}
  129. @Test public void Concat_with_obj() {
  130. tst_ConcatWith_any("a|b", "|", "a", "b"); // do not append final delimiter
  131. tst_ConcatWith_any("a||c", "|", "a", null, "c"); // null
  132. tst_ConcatWith_any("a|b", "|", Object_.Ary("a", "b")); // pass array as arg
  133. } void tst_ConcatWith_any(String expd, String delimiter, Object... array) {Tfds.Eq(expd, String_.Concat_with_obj(delimiter, array));}
  134. @Test public void Compare_byteAry() {
  135. tst_Compare_byteAry("a", "a", CompareAbleUtl.Same);
  136. tst_Compare_byteAry("a", "b", CompareAbleUtl.Less);
  137. tst_Compare_byteAry("b", "a", CompareAbleUtl.More);
  138. tst_Compare_byteAry("ab", "ac", CompareAbleUtl.Less);
  139. tst_Compare_byteAry("ac", "ab", CompareAbleUtl.More);
  140. tst_Compare_byteAry("a", "ab", CompareAbleUtl.Less);
  141. tst_Compare_byteAry("ab", "a", CompareAbleUtl.More);
  142. tst_Compare_byteAry("101", "1-0-1", CompareAbleUtl.More); // NOTE: regular String_.Compare_as_ordinals returns Less in .NET, More in Java
  143. tst_Compare_byteAry("1-0-1", "101 (album)", CompareAbleUtl.Less);
  144. } void tst_Compare_byteAry(String lhs, String rhs, int expd) {Tfds.Eq(expd, String_.Compare_byteAry(lhs, rhs));}
  145. @Test public void FindBwd() { // WORKAROUND.CS:String.LastIndexOf returns -1 for multi-chars;
  146. tst_FindRev("abc", "a", 0, 0);
  147. tst_FindRev("abc", "ab", 0, 0); // 2 chars
  148. tst_FindRev("abc", "abc", 0, 0); // 3 chars
  149. tst_FindRev("ab", "abc", 0, -1); // out of index error
  150. tst_FindRev("ababab", "ab", 2, 2); // make sure cs implementation doesn't pick up next
  151. } void tst_FindRev(String s, String find, int pos, int expd) {Tfds.Eq(expd, String_.FindBwd(s, find, pos));}
  152. @Test public void Extract_after_bwd() {
  153. Extract_after_bwd_tst("a/b", "/", "b");
  154. Extract_after_bwd_tst("a/", "/", "");
  155. Extract_after_bwd_tst("a", "/", "");
  156. } void Extract_after_bwd_tst(String src, String dlm, String expd) {Tfds.Eq(expd, String_.Extract_after_bwd(src, dlm));}
  157. }