Bry_split__tst.java 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.core.tests.Gftest;
  14. import gplx.objects.primitives.BoolUtl;
  15. import gplx.objects.strings.AsciiByte;
  16. import org.junit.Test;
  17. public class Bry_split__tst {
  18. private final Bry_split__fxt fxt = new Bry_split__fxt();
  19. @Test public void Split() {
  20. fxt.Test_split("a" , AsciiByte.Pipe, BoolUtl.N, "a"); // no trim
  21. fxt.Test_split("a|" , AsciiByte.Pipe, BoolUtl.N, "a");
  22. fxt.Test_split("|a" , AsciiByte.Pipe, BoolUtl.N, "", "a");
  23. fxt.Test_split("|" , AsciiByte.Pipe, BoolUtl.N, "");
  24. fxt.Test_split("" , AsciiByte.Pipe, BoolUtl.N);
  25. fxt.Test_split("a|b|c" , AsciiByte.Pipe, BoolUtl.N, "a", "b", "c");
  26. fxt.Test_split(" a " , AsciiByte.Pipe, BoolUtl.Y, "a"); // trim
  27. fxt.Test_split(" a |" , AsciiByte.Pipe, BoolUtl.Y, "a");
  28. fxt.Test_split("| a " , AsciiByte.Pipe, BoolUtl.Y, "", "a");
  29. fxt.Test_split(" | " , AsciiByte.Pipe, BoolUtl.Y, "");
  30. fxt.Test_split(" " , AsciiByte.Pipe, BoolUtl.Y);
  31. fxt.Test_split(" a | b | c " , AsciiByte.Pipe, BoolUtl.Y, "a", "b", "c");
  32. fxt.Test_split(" a b | c d " , AsciiByte.Pipe, BoolUtl.Y, "a b", "c d");
  33. fxt.Test_split(" a \n b " , AsciiByte.Nl , BoolUtl.N, " a ", " b "); // ws as dlm
  34. fxt.Test_split(" a \n b " , AsciiByte.Nl , BoolUtl.Y, "a", "b"); // ws as dlm; trim
  35. fxt.Test_split("a|extend|b" , AsciiByte.Pipe, BoolUtl.Y, "a", "extend|b"); // extend
  36. fxt.Test_split("extend|a" , AsciiByte.Pipe, BoolUtl.Y, "extend|a"); // extend
  37. fxt.Test_split("a|cancel|b" , AsciiByte.Pipe, BoolUtl.Y, "a"); // cancel
  38. }
  39. @Test public void Split__bry() {
  40. fxt.Test_split("a|b|c|d" , 2, 6, "|", "b", "c");
  41. fxt.Test_split("a|b|c|d" , 2, 4, "|", "b");
  42. }
  43. @Test public void Empty() {
  44. fxt.Test_split("a\n\nb" , AsciiByte.Nl, BoolUtl.N, "a", "", "b");
  45. }
  46. @Test public void Split_w_max() {
  47. fxt.Test__split_w_max("a|b|c|d" , AsciiByte.Pipe, 2, "a", "b"); // max is less
  48. fxt.Test__split_w_max("a" , AsciiByte.Pipe, 2, "a", null); // max is more
  49. fxt.Test__split_w_max("|" , AsciiByte.Pipe, 2, "", ""); // empty itms
  50. }
  51. @Test public void Split_ws() {
  52. fxt.Test__split_ws("a b", "a", "b");
  53. fxt.Test__split_ws(" a ", "a");
  54. fxt.Test__split_ws(" abc def ", "abc", "def");
  55. }
  56. }
  57. class Bry_split__fxt {
  58. private final Bry_split_wkr__example wkr = new Bry_split_wkr__example();
  59. public void Test_split(String raw_str, byte dlm, boolean trim, String... expd) {
  60. byte[] src = Bry_.new_a7(raw_str);
  61. Bry_split_.Split(src, 0, src.length, dlm, trim, wkr);
  62. byte[][] actl_ary = wkr.To_ary();
  63. Tfds.Eq_ary_str(expd, String_.Ary(actl_ary));
  64. }
  65. public void Test_split(String src, int src_bgn, int src_end, String dlm, String... expd) {
  66. Tfds.Eq_ary_str(Bry_.Ary(expd), Bry_split_.Split(Bry_.new_u8(src), src_bgn, src_end, Bry_.new_u8(dlm)));
  67. }
  68. public void Test__split_w_max(String src, byte dlm, int max, String... expd) {
  69. Gftest.Eq__ary(expd, String_.Ary(Bry_split_.Split_w_max(Bry_.new_u8(src), dlm, max)));
  70. }
  71. public void Test__split_ws(String raw, String... expd) {
  72. byte[][] actl = Bry_split_.Split_ws(Bry_.new_u8(raw));
  73. Gftest.Eq__ary(Bry_.Ary(expd), actl, raw);
  74. }
  75. }
  76. class Bry_split_wkr__example implements gplx.core.brys.Bry_split_wkr {
  77. private final List_adp list = List_adp_.New();
  78. public int Split(byte[] src, int itm_bgn, int itm_end) {
  79. byte[] bry = itm_end == itm_bgn ? Bry_.Empty : Bry_.Mid(src, itm_bgn, itm_end);
  80. if (Bry_.Eq(bry, Bry_.new_a7("extend"))) return Bry_split_.Rv__extend;
  81. else if (Bry_.Eq(bry, Bry_.new_a7("cancel"))) return Bry_split_.Rv__cancel;
  82. list.Add(bry);
  83. return Bry_split_.Rv__ok;
  84. }
  85. public byte[][] To_ary() {
  86. return (byte[][])list.ToAryAndClear(byte[].class);
  87. }
  88. }