uca_test.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. { Unicode Collation Algorithm test routines for generated data.
  2. Copyright (c) 2012 by Inoussa OUEDRAOGO
  3. The source code is distributed under the Library GNU
  4. General Public License with the following modification:
  5. - object files and libraries linked into an application may be
  6. distributed without source code.
  7. If you didn't receive a copy of the file COPYING, contact:
  8. Free Software Foundation
  9. 675 Mass Ave
  10. Cambridge, MA 02139
  11. USA
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. }
  16. unit uca_test;
  17. {$mode objfpc}{$H+}
  18. interface
  19. uses
  20. SysUtils,
  21. helper;
  22. procedure uca_CheckProp_1(
  23. ABook : TUCA_DataBook;
  24. APropBook : PUCA_PropBook
  25. );
  26. procedure uca_CheckProp_x(
  27. ABook : TUCA_DataBook;
  28. APropBook : PUCA_PropBook
  29. );
  30. procedure uca_CheckProp_1y(
  31. const ABook : TUCA_DataBook;
  32. const APropBook : PUCA_PropBook;
  33. const AFirstTable : PucaBmpFirstTable;
  34. const ASecondTable : PucaBmpSecondTable
  35. );
  36. procedure uca_CheckProp_2y(
  37. const ABook : TUCA_DataBook;
  38. const APropBook : PUCA_PropBook;
  39. const AFirstTable : PucaOBmpFirstTable;
  40. const ASecondTable : PucaOBmpSecondTable
  41. );
  42. implementation
  43. function IndexOf(const ACodePoint : Cardinal; APropBook : PUCA_PropBook): Integer;
  44. var
  45. i : Integer;
  46. begin
  47. for i := 0 to Length(APropBook^.Index) - 1 do begin
  48. if (ACodePoint = APropBook^.Index[i].CodePoint) then
  49. exit(i);
  50. end;
  51. Result := -1;
  52. end;
  53. function CompareWeigth(AExpect : PUCA_LineRec; AActual : PUCA_PropItemRec) : Boolean;
  54. var
  55. i, k : Integer;
  56. p : PUCA_PropWeights;
  57. pw : array of TUCA_PropWeights;
  58. begin
  59. Result := False;
  60. if (Length(AExpect^.Weights) <> AActual^.WeightLength) then
  61. exit;
  62. //p := PUCA_PropWeights(PtrUInt(AActual) + SizeOf(TUCA_PropItemRec));
  63. SetLength(pw,AActual^.WeightLength);
  64. p := @pw[0];
  65. AActual^.GetWeightArray(p);
  66. for i := 0 to Length(AExpect^.Weights) - 1 do begin
  67. //if (BoolToByte(AExpect^.Weights[i].Variable) <> p^.Variable) then
  68. //exit;
  69. for k := 0 to 3 - 1 do begin
  70. if (AExpect^.Weights[i].Weights[k] <> p^.Weights[k]) then
  71. exit;
  72. end;
  73. Inc(p);
  74. end;
  75. Result := True;
  76. end;
  77. procedure uca_CheckProp_1(
  78. ABook : TUCA_DataBook;
  79. APropBook : PUCA_PropBook
  80. );
  81. var
  82. i, c, k : Integer;
  83. line : PUCA_LineRec;
  84. uc : Cardinal;
  85. p : PUCA_PropItemRec;
  86. begin
  87. WriteLn('uca_CheckProp_1 Start ... ');
  88. line := @ABook.Lines[0];
  89. c := Length(ABook.Lines);
  90. for i := 0 to c - 1 do begin
  91. if line^.Stored and (Length(line^.CodePoints) = 1) then begin
  92. uc := line^.CodePoints[0];
  93. k := IndexOf(uc,APropBook);
  94. if (k = -1) then begin
  95. WriteLn('Property not found for Code Point : ' + Format('%x',[uc]));
  96. end else begin
  97. p := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+APropBook^.Index[k].Position);
  98. if not CompareWeigth(line,p) then
  99. WriteLn('CompareWeigth fail for Code Point : ' + Format('%x',[uc]));
  100. end;
  101. end;
  102. Inc(line);
  103. end;
  104. WriteLn('uca_CheckProp_1 End');
  105. end;
  106. function FindWord(
  107. const AWord : array of Cardinal;
  108. const APropBook : PUCA_PropItemRec
  109. ) : PUCA_PropItemRec;
  110. var
  111. cc : Cardinal;
  112. p : PUCA_PropItemRec;
  113. i, k, kc : Integer;
  114. ok : Boolean;
  115. begin
  116. Result := nil;
  117. p := APropBook;
  118. for i := 1 to Length(AWord) - 1 do begin
  119. ok := False;
  120. kc := p^.ChildCount - 1;
  121. p := PUCA_PropItemRec(PtrUInt(p) + p^.GetSelfOnlySize());
  122. for k := 0 to kc do begin
  123. if (AWord[i] = p^.CodePoint) then begin
  124. ok := True;
  125. Break;
  126. end;
  127. p := PUCA_PropItemRec(PtrUInt(p) + p^.Size);
  128. end;
  129. if not ok then
  130. exit;
  131. end;
  132. Result := p;
  133. end;
  134. function DumpCodePoints(const AValues : array of Cardinal) : string;
  135. var
  136. i : Integer;
  137. begin
  138. Result := '';
  139. for i := 0 to Length(AValues) - 1 do
  140. Result := Format('%s %x',[Result,AValues[i]]);
  141. Result := Trim(Result);
  142. end;
  143. procedure uca_CheckProp_x(
  144. ABook : TUCA_DataBook;
  145. APropBook : PUCA_PropBook
  146. );
  147. var
  148. i, c, k : Integer;
  149. line : PUCA_LineRec;
  150. uc : Cardinal;
  151. p, q : PUCA_PropItemRec;
  152. begin
  153. WriteLn('uca_CheckProp_x Start ... ');
  154. line := @ABook.Lines[0];
  155. c := Length(ABook.Lines);
  156. for i := 0 to c - 1 do begin
  157. if line^.Stored and (Length(line^.CodePoints) > 1) then begin
  158. //WriteLn(' Code Point sequence : ' + DumpCodePoints(line^.CodePoints));
  159. uc := line^.CodePoints[0];
  160. k := IndexOf(uc,APropBook);
  161. if (k = -1) then begin
  162. WriteLn(' Property not found for Code Point : ' + Format('%x',[uc]));
  163. end else begin
  164. q := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+APropBook^.Index[k].Position);
  165. p := FindWord(line^.CodePoints,q);
  166. if (p = nil) then
  167. WriteLn(' Data not found for Code Point sequence : ' + DumpCodePoints(line^.CodePoints))
  168. else if not CompareWeigth(line,p) then
  169. WriteLn(' CompareWeigth fail for Code Point sequence : ' + DumpCodePoints(line^.CodePoints));
  170. end;
  171. end;
  172. Inc(line);
  173. end;
  174. WriteLn('uca_CheckProp_x End');
  175. end;
  176. function GetPropPosition(
  177. const ABMPCodePoint : Word;
  178. const AFirstTable : PucaBmpFirstTable;
  179. const ASecondTable : PucaBmpSecondTable
  180. ) : Integer; inline;overload;
  181. begin
  182. Result:=
  183. ASecondTable^[AFirstTable^[WordRec(ABMPCodePoint).Hi]][WordRec(ABMPCodePoint).Lo] - 1
  184. end;
  185. procedure uca_CheckProp_1y(
  186. const ABook : TUCA_DataBook;
  187. const APropBook : PUCA_PropBook;
  188. const AFirstTable : PucaBmpFirstTable;
  189. const ASecondTable : PucaBmpSecondTable
  190. );
  191. var
  192. i, c, k : Integer;
  193. line : PUCA_LineRec;
  194. uc : Cardinal;
  195. p : PUCA_PropItemRec;
  196. ucw : Word;
  197. begin
  198. WriteLn('uca_CheckProp_1y Start (BMP) ... ');
  199. line := @ABook.Lines[0];
  200. c := Length(ABook.Lines);
  201. for i := 0 to c - 1 do begin
  202. if line^.Stored and (Length(line^.CodePoints) = 1) then begin
  203. uc := line^.CodePoints[0];
  204. if (uc <= High(Word)) then begin
  205. ucw := uc;
  206. k := GetPropPosition(ucw,AFirstTable,ASecondTable);
  207. if (k = -1) then begin
  208. WriteLn('Property not found for Code Point : ' + Format('%x',[uc]));
  209. end else begin
  210. p := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+k);
  211. if not CompareWeigth(line,p) then
  212. WriteLn('CompareWeigth fail for Code Point : ' + Format('%x',[uc]));
  213. end;
  214. end;
  215. end;
  216. Inc(line);
  217. end;
  218. WriteLn('uca_CheckProp_1y End');
  219. end;
  220. procedure uca_CheckProp_2y(
  221. const ABook : TUCA_DataBook;
  222. const APropBook : PUCA_PropBook;
  223. const AFirstTable : PucaOBmpFirstTable;
  224. const ASecondTable : PucaOBmpSecondTable
  225. );
  226. var
  227. i, c, k : Integer;
  228. line : PUCA_LineRec;
  229. uc : Cardinal;
  230. p : PUCA_PropItemRec;
  231. uchs, ucls : Word;
  232. begin
  233. WriteLn('uca_CheckProp_2y Start (>BMP) ... ');
  234. line := @ABook.Lines[0];
  235. c := Length(ABook.Lines);
  236. for i := 0 to c - 1 do begin
  237. if line^.Stored and (Length(line^.CodePoints) = 1) then begin
  238. uc := line^.CodePoints[0];
  239. if (uc > High(Word)) then begin
  240. FromUCS4(uc,uchs,ucls);
  241. k := GetPropPosition(uchs,ucls,AFirstTable,ASecondTable);
  242. if (k = -1) then begin
  243. WriteLn('Property not found for Code Point : ' + Format('%x',[uc]));
  244. end else begin
  245. p := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+k);
  246. if not CompareWeigth(line,p) then
  247. WriteLn('CompareWeigth fail for Code Point : ' + Format('%x',[uc]));
  248. end;
  249. end;
  250. end;
  251. Inc(line);
  252. end;
  253. WriteLn('uca_CheckProp_2y End');
  254. end;
  255. end.