uca_test.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. if (i > 1) then
  123. p := PUCA_PropItemRec(PtrUInt(p) + SizeOf(UInt24));
  124. for k := 0 to kc do begin
  125. if (AWord[i] = p^.CodePoint) then begin
  126. ok := True;
  127. Break;
  128. end;
  129. p := PUCA_PropItemRec(PtrUInt(p) + p^.Size);
  130. end;
  131. if not ok then
  132. exit;
  133. end;
  134. Result := p;
  135. end;
  136. function DumpCodePoints(const AValues : array of Cardinal) : string;
  137. var
  138. i : Integer;
  139. begin
  140. Result := '';
  141. for i := 0 to Length(AValues) - 1 do
  142. Result := Format('%s %x',[Result,AValues[i]]);
  143. Result := Trim(Result);
  144. end;
  145. procedure uca_CheckProp_x(
  146. ABook : TUCA_DataBook;
  147. APropBook : PUCA_PropBook
  148. );
  149. var
  150. i, c, k : Integer;
  151. line : PUCA_LineRec;
  152. uc : Cardinal;
  153. p, q : PUCA_PropItemRec;
  154. begin
  155. WriteLn('uca_CheckProp_x Start ... ');
  156. line := @ABook.Lines[0];
  157. c := Length(ABook.Lines);
  158. for i := 0 to c - 1 do begin
  159. if line^.Stored and (Length(line^.CodePoints) > 1) then begin
  160. //WriteLn(' Code Point sequence : ' + DumpCodePoints(line^.CodePoints));
  161. uc := line^.CodePoints[0];
  162. k := IndexOf(uc,APropBook);
  163. if (k = -1) then begin
  164. WriteLn(' Property not found for Code Point : ' + Format('%x',[uc]));
  165. end else begin
  166. q := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+APropBook^.Index[k].Position);
  167. p := FindWord(line^.CodePoints,q);
  168. if (p = nil) then
  169. WriteLn(' Data not found for Code Point sequence : ' + DumpCodePoints(line^.CodePoints))
  170. else if not CompareWeigth(line,p) then
  171. WriteLn(' CompareWeigth fail for Code Point sequence : ' + DumpCodePoints(line^.CodePoints));
  172. end;
  173. end;
  174. Inc(line);
  175. end;
  176. WriteLn('uca_CheckProp_x End');
  177. end;
  178. function GetPropPosition(
  179. const ABMPCodePoint : Word;
  180. const AFirstTable : PucaBmpFirstTable;
  181. const ASecondTable : PucaBmpSecondTable
  182. ) : Integer; inline;overload;
  183. begin
  184. Result:=
  185. ASecondTable^[AFirstTable^[WordRec(ABMPCodePoint).Hi]][WordRec(ABMPCodePoint).Lo] - 1
  186. end;
  187. procedure uca_CheckProp_1y(
  188. const ABook : TUCA_DataBook;
  189. const APropBook : PUCA_PropBook;
  190. const AFirstTable : PucaBmpFirstTable;
  191. const ASecondTable : PucaBmpSecondTable
  192. );
  193. var
  194. i, c, k : Integer;
  195. line : PUCA_LineRec;
  196. uc : Cardinal;
  197. p : PUCA_PropItemRec;
  198. ucw : Word;
  199. begin
  200. WriteLn('uca_CheckProp_1y Start (BMP) ... ');
  201. line := @ABook.Lines[0];
  202. c := Length(ABook.Lines);
  203. for i := 0 to c - 1 do begin
  204. if line^.Stored and (Length(line^.CodePoints) = 1) then begin
  205. uc := line^.CodePoints[0];
  206. if (uc <= High(Word)) then begin
  207. ucw := uc;
  208. k := GetPropPosition(ucw,AFirstTable,ASecondTable);
  209. if (k = -1) then begin
  210. WriteLn('Property not found for Code Point : ' + Format('%x',[uc]));
  211. end else begin
  212. p := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+k);
  213. if not CompareWeigth(line,p) then
  214. WriteLn('CompareWeigth fail for Code Point : ' + Format('%x',[uc]));
  215. end;
  216. end;
  217. end;
  218. Inc(line);
  219. end;
  220. WriteLn('uca_CheckProp_1y End');
  221. end;
  222. procedure uca_CheckProp_2y(
  223. const ABook : TUCA_DataBook;
  224. const APropBook : PUCA_PropBook;
  225. const AFirstTable : PucaOBmpFirstTable;
  226. const ASecondTable : PucaOBmpSecondTable
  227. );
  228. var
  229. i, c, k : Integer;
  230. line : PUCA_LineRec;
  231. uc : Cardinal;
  232. p : PUCA_PropItemRec;
  233. uchs, ucls : Word;
  234. begin
  235. WriteLn('uca_CheckProp_2y Start (>BMP) ... ');
  236. line := @ABook.Lines[0];
  237. c := Length(ABook.Lines);
  238. for i := 0 to c - 1 do begin
  239. if line^.Stored and (Length(line^.CodePoints) = 1) then begin
  240. uc := line^.CodePoints[0];
  241. if (uc > High(Word)) then begin
  242. FromUCS4(uc,uchs,ucls);
  243. k := GetPropPosition(uchs,ucls,AFirstTable,ASecondTable);
  244. if (k = -1) then begin
  245. WriteLn('Property not found for Code Point : ' + Format('%x',[uc]));
  246. end else begin
  247. p := PUCA_PropItemRec(PtrUInt(APropBook^.Items)+k);
  248. if not CompareWeigth(line,p) then
  249. WriteLn('CompareWeigth fail for Code Point : ' + Format('%x',[uc]));
  250. end;
  251. end;
  252. end;
  253. Inc(line);
  254. end;
  255. WriteLn('uca_CheckProp_2y End');
  256. end;
  257. end.