Test_String.dpr 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2019 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program Test_String;
  26. {$I Tests.inc}
  27. uses
  28. SysUtils,
  29. libsagui,
  30. BrookLibraryLoader,
  31. BrookString,
  32. Test;
  33. type
  34. TLocalString = class(TBrookString)
  35. public
  36. destructor Destroy; override;
  37. end;
  38. destructor TLocalString.Destroy;
  39. begin
  40. inherited Destroy;
  41. SgLib.Check;
  42. { checks if handle was really freed and 'nilified'. }
  43. Assert(not Assigned(Handle));
  44. Assert(sg_str_clear(Handle) <> 0);
  45. end;
  46. procedure Test_StringCreate;
  47. var
  48. VStr: TBrookString;
  49. begin
  50. VStr := TBrookString.Create(nil);
  51. try
  52. Assert(Assigned(VStr.Handle));
  53. finally
  54. VStr.Destroy;
  55. end;
  56. VStr := TBrookString.Create(Pointer(123));
  57. try
  58. Assert(VStr.Handle = Pointer(123));
  59. finally
  60. VStr.Destroy;
  61. end;
  62. end;
  63. procedure Test_StringHandle(AStr: TBrookString);
  64. var
  65. VStr: TBrookString;
  66. begin
  67. Assert(Assigned(AStr.Handle));
  68. VStr := TBrookString.Create(AStr.Handle);
  69. try
  70. Assert(VStr.Handle = AStr.Handle);
  71. finally
  72. VStr.Destroy;
  73. end;
  74. VStr := TBrookString.Create(nil);
  75. try
  76. Assert(Assigned(VStr.Handle));
  77. Assert(VStr.Handle <> AStr.Handle);
  78. finally
  79. VStr.Destroy;
  80. end;
  81. end;
  82. procedure Test_StringOwnsHandle;
  83. var
  84. Vhandle: Psg_str;
  85. VStr: TBrookString;
  86. begin
  87. SgLib.Check;
  88. Vhandle := sg_str_new;
  89. Assert(Assigned(Vhandle));
  90. VStr := TBrookString.Create(Vhandle);
  91. try
  92. Assert(Assigned(VStr.Handle));
  93. Assert(VStr.Handle = Vhandle);
  94. finally
  95. VStr.Destroy;
  96. sg_str_free(Vhandle);
  97. end;
  98. VStr := TLocalString.Create(nil);
  99. try
  100. Assert(Assigned(VStr.Handle));
  101. finally
  102. VStr.Destroy;
  103. end;
  104. end;
  105. procedure DoStringWriteBytes1(const AArgs: array of const);
  106. begin
  107. Assert(TBrookString(AArgs[0].VObject).WriteBytes(nil, AArgs[1].VInteger) = 0);
  108. end;
  109. procedure DoStringWriteBytes2(const AArgs: array of const);
  110. begin
  111. Assert(TBrookString(AArgs[0].VObject).WriteBytes(
  112. TBytes(AArgs[1].VPointer^), 0) = 0);
  113. end;
  114. procedure Test_StringWriteBytes(AStr: TBrookString; const AVal: TBytes;
  115. ALen: NativeUInt);
  116. begin
  117. AssertOSExcept(DoStringWriteBytes1, [AStr, ALen]);
  118. AssertOSExcept(DoStringWriteBytes2, [AStr, @AVal]);
  119. AStr.Clear;
  120. Assert(AStr.WriteBytes(AVal, ALen) = ALen);
  121. Assert(AStr.Length = ALen);
  122. end;
  123. procedure DoStringWrite1(const AArgs: array of const);
  124. begin
  125. TBrookString(AArgs[0].VObject).Write('', TEncoding.UTF8);
  126. end;
  127. procedure DoStringWrite2(const AArgs: array of const);
  128. begin
  129. TBrookString(AArgs[0].VObject).Write(string(AArgs[1].VString^), nil);
  130. end;
  131. procedure Test_StringWrite(AStr: TBrookString; const AVal: string;
  132. ALen: NativeUInt);
  133. begin
  134. AssertOSExcept(DoStringWrite1, [AStr]);
  135. AssertExcept(DoStringWrite2, EArgumentNilException, [AStr, @AVal]);
  136. AStr.Clear;
  137. AStr.Write(AVal, TEncoding.UTF8);
  138. Assert(AStr.Length = ALen);
  139. end;
  140. procedure Test_StringToString(AStr: TBrookString; const AVal: string);
  141. begin
  142. AStr.Clear;
  143. Assert(AStr.Text.IsEmpty);
  144. AStr.Text := AVal;
  145. Assert(AStr.ToString = AVal);
  146. end;
  147. procedure Test_StringClear(AStr: TBrookString; const AVal: TBytes;
  148. ALen: NativeUInt);
  149. begin
  150. AStr.Clear;
  151. Assert(AStr.Length = 0);
  152. AStr.WriteBytes(AVal, ALen);
  153. Assert(AStr.Length > 0);
  154. Assert(AStr.Length = ALen);
  155. end;
  156. procedure Test_StrincContent(AStr: TBrookString; const AVal: TBytes;
  157. ALen: NativeUInt);
  158. begin
  159. AStr.Clear;
  160. Assert(Length(AStr.Content) = 0);
  161. AStr.WriteBytes(AVal, ALen);
  162. Assert(CompareMem(@AStr.Content[0], @AVal[0], ALen));
  163. end;
  164. procedure Test_StringLength(AStr: TBrookString; const AVal: TBytes;
  165. ALen: NativeUInt);
  166. begin
  167. AStr.Clear;
  168. Assert(AStr.Length = 0);
  169. AStr.WriteBytes(AVal, ALen);
  170. Assert(AStr.Length = ALen);
  171. end;
  172. procedure Test_StringText(AStr: TBrookString; const AVal: string);
  173. begin
  174. AStr.Clear;
  175. Assert(AStr.Text.IsEmpty);
  176. AStr.Text := AVal;
  177. Assert(AStr.Text = AVal);
  178. end;
  179. procedure Test_StringExtra(AStr: TBrookString);
  180. var
  181. VStr: TBrookString;
  182. begin
  183. AStr.Clear;
  184. AStr.Write('abc');
  185. Assert(AStr.Text = 'abc');
  186. VStr := TBrookString.Create(AStr.Handle);
  187. try
  188. VStr.Write('123');
  189. finally
  190. VStr.Destroy;
  191. end;
  192. Assert(AStr.Text = 'abc123');
  193. end;
  194. const
  195. VAL = 'abc123def456';
  196. LEN: NativeUInt = Length(VAL);
  197. var
  198. VValB: TBytes;
  199. VStr: TBrookString;
  200. begin
  201. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  202. ReportMemoryLeaksOnShutdown := True;
  203. {$ENDIF}
  204. TBrookLibraryLoader.Load;
  205. VValB := TEncoding.UTF8.GetBytes(VAL);
  206. VStr := TBrookString.Create(nil);
  207. try
  208. Test_StringCreate;
  209. //Test_StringDestroy - not required
  210. Test_StringHandle(VStr);
  211. Test_StringOwnsHandle;
  212. Test_StringWriteBytes(VStr, VValB, LEN);
  213. Test_StringWrite(VStr, VAL, LEN);
  214. Test_StringToString(VStr, VAL);
  215. Test_StringClear(VStr, VValB, LEN);
  216. Test_StrincContent(VStr, VValB, LEN);
  217. Test_StringLength(VStr, VValB, LEN);
  218. Test_StringText(VStr, VAL);
  219. Test_StringExtra(VStr);
  220. finally
  221. VStr.Destroy;
  222. end;
  223. end.