UstringUtlTest.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2021 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.objects.strings.unicodes;
  13. import gplx.frameworks.tests.GfoTstr;
  14. import gplx.types.basics.strings.unicodes.Ustring;
  15. import gplx.types.basics.strings.unicodes.UstringUtl;
  16. import org.junit.Test;
  17. public class UstringUtlTest {
  18. private final UstringTstr fxt = new UstringTstr();
  19. @Test public void Empty() {
  20. fxt.Init("");
  21. fxt.TestLen(0, 0);
  22. }
  23. @Test public void Blank() {
  24. fxt.Init("");
  25. fxt.TestLen(0, 0);
  26. }
  27. @Test public void Single() {
  28. fxt.Init("Abc");
  29. fxt.TestLen(3, 3);
  30. fxt.TestGetCode(65, 98, 99);
  31. fxt.TestMapCodeToChar(0, 1, 2, 3);
  32. fxt.TestMapCharToCode(0, 1, 2, 3);
  33. }
  34. @Test public void Multi() {
  35. fxt.Init("a¢€𤭢b");
  36. fxt.TestLen(5, 6);
  37. fxt.TestGetCode(97, 162, 8364, 150370, 98);
  38. fxt.TestMapCodeToChar(0, 1, 2, 3, 5, 6);
  39. fxt.TestMapCharToCode(0, 1, 2, 3, -1, 4, 5);
  40. }
  41. @Test public void IndexOf() {
  42. fxt.TestIndexOf("abc", "b", 0, 1); // basic
  43. fxt.TestIndexOf("ab", "bc", 0, -1); // out-of-bounds
  44. fxt.TestIndexOf("a¢e", "¢", 0, 1); // check UTF-8 strings still match at byte-level
  45. }
  46. @Test public void Substring() {
  47. fxt.TestSubstring("abc", 1, 2, "b"); // basic
  48. fxt.TestSubstring("¢bc", 1, 2, "b"); // check UTF-8 strings don't get lopped off
  49. }
  50. }
  51. class UstringTstr {
  52. private Ustring under;
  53. public void Init(String src) {
  54. this.under = UstringUtl.NewCodepoints(src);
  55. }
  56. public void TestLen(int expdCodes, int expdChars) {
  57. GfoTstr.Eq(expdCodes, under.LenInData(), "codes");
  58. GfoTstr.Eq(expdChars, under.LenInChars(), "chars");
  59. }
  60. public void TestGetCode(int... expd) {
  61. int actlLen = under.LenInData();
  62. int[] actl = new int[actlLen];
  63. for (int i = 0; i < actlLen; i++)
  64. actl[i] = under.GetData(i);
  65. GfoTstr.EqAry(expd, actl);
  66. }
  67. public void TestMapCodeToChar(int... expd) {
  68. int actlLen = under.LenInData() + 1;
  69. int[] actl = new int[actlLen];
  70. for (int i = 0; i < actlLen; i++)
  71. actl[i] = under.MapDataToChar(i);
  72. GfoTstr.EqAry(expd, actl);
  73. }
  74. public void TestMapCharToCode(int... expd) {
  75. int actlLen = under.LenInChars() + 1;
  76. int[] actl = new int[actlLen];
  77. for (int i = 0; i < actlLen; i++) {
  78. int val = 0;
  79. try {
  80. val = under.MapCharToData(i);
  81. }
  82. catch (Exception exc) {
  83. val = -1;
  84. }
  85. actl[i] = val;
  86. }
  87. GfoTstr.EqAry(expd, actl);
  88. }
  89. public void TestIndexOf(String srcStr, String findStr, int bgn, int expd) {
  90. Ustring src = UstringUtl.NewCodepoints(srcStr);
  91. Ustring find = UstringUtl.NewCodepoints(findStr);
  92. int actl = src.IndexOf(find, bgn);
  93. GfoTstr.Eq(expd, actl);
  94. }
  95. public void TestSubstring(String srcStr, int bgn, int end, String expd) {
  96. Ustring src = UstringUtl.NewCodepoints(srcStr);
  97. String actl = src.Substring(bgn, end);
  98. GfoTstr.Eq(expd, actl);
  99. }
  100. }