test_suite_utils.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. {$INCLUDE sdo_global.inc}
  2. unit test_suite_utils;
  3. interface
  4. uses
  5. SysUtils, Classes,
  6. {$IFDEF FPC}
  7. fpcunit,
  8. {$ENDIF}
  9. {$IFNDEF FPC}
  10. TestFrameWork,
  11. {$ENDIF}
  12. sdo, sdo_types;
  13. const
  14. TestFilesPath = '..' + PathDelim + '..' + PathDelim + 'files' + PathDelim;
  15. XsdTestFilesPath = '..' + PathDelim + '..' + PathDelim + 'files' + PathDelim + 'xsd' + PathDelim;
  16. type
  17. { TWstBaseTest }
  18. TWstBaseTest = class(TTestCase)
  19. protected
  20. procedure CheckEquals(expected, actual: TSDOBytes; msg: string = ''; const AStrict : Boolean = True); overload;
  21. procedure CheckEqualsCurrency(expected, actual: Currency; const delta: Currency; const msg: string = ''); overload;
  22. procedure CheckEqualsCurrency(expected, actual: Currency; const msg: string = ''); overload;
  23. {$IFDEF FPC}
  24. procedure CheckEquals(expected, actual: Int64; msg: string = ''; const AStrict : Boolean = True); overload;
  25. procedure CheckEquals(expected, actual: QWord; msg: string = ''; const AStrict : Boolean = True); overload;
  26. {$ENDIF FPC}
  27. end;
  28. //{$IFDEF FPC}
  29. // {$IF not Defined(RandomRange)}
  30. //function RandomRange(const AFrom, ATo : Integer) : Integer ;overload;
  31. // {$IFEND}
  32. //{$ENDIF}
  33. function RandomRange(const AFrom, ATo : Int64) : Int64 ;overload;
  34. function RandomString(const AMaxLen : PtrInt) : TSDOString;
  35. function RandomDate() : TSDODateTime ;
  36. function RandomBytes(const AMaxLen : Integer) : TSDOBytes;
  37. function BytesToString(const AValue: TSDOBytes): TSDOString;overload;
  38. function BytesToString(const AValue: array of Byte): TSDOString;overload;
  39. function RandomDouble(const AFrom, ATo: Integer): TSDODouble;
  40. function RandomFloat(const AFrom, ATo: Integer): TSDOFloat;
  41. function sdoExpandLocalFileName(const AFileName : string) : string;
  42. implementation
  43. uses
  44. Math;
  45. //{$IFDEF FPC}
  46. // {$IF not Defined(RandomRange)}
  47. {function RandomRange(const AFrom, ATo : Integer) : Integer ;
  48. var
  49. a : Integer;
  50. begin
  51. if ( AFrom <= ATo ) then
  52. a := AFrom
  53. else
  54. a := ATo;
  55. Result := a + Random(Abs(ATo - AFrom));
  56. end; }
  57. // {$IFEND}
  58. //{$ENDIF}
  59. function RandomRange(const AFrom, ATo : Int64) : Int64 ;
  60. var
  61. a : Int64;
  62. begin
  63. if ( AFrom <= ATo ) then
  64. a := AFrom
  65. else
  66. a := ATo;
  67. Result := a + Random(Abs(ATo - AFrom));
  68. end;
  69. function RandomString(const AMaxLen : PtrInt) : TSDOString;
  70. var
  71. i, l, j : PtrInt;
  72. begin
  73. l := RandomRange(0,AMaxLen);
  74. j := RandomRange(1, 3);
  75. SetLength(Result,l);
  76. for i := 1 to l do begin
  77. case j of
  78. 1 : Result[i] := AnsiChar(RandomRange(Ord('a'),Ord('z')));
  79. 2 : Result[i] := AnsiChar(RandomRange(Ord('0'),Ord('9')));
  80. 3 : Result[i] := AnsiChar(RandomRange(Ord('A'),Ord('Z')));
  81. end;
  82. j := ( j + 1 );
  83. if ( j > 3 ) then
  84. j := 1;
  85. end;
  86. end;
  87. function BytesToString(const AValue: TSDOBytes): TSDOString;
  88. var
  89. locRes : AnsiString;
  90. begin
  91. if ( Length(AValue) > 0 ) then begin
  92. SetLength(locRes, ( 2 * Length(AValue) ) );
  93. BinToHex(PAnsiChar(@(AValue[0])),PAnsiChar(@(locRes[1])),Length(AValue));
  94. Result := locRes;
  95. end else begin
  96. Result := '';
  97. end;
  98. end;
  99. function BytesToString(const AValue: array of Byte): TSDOString;
  100. var
  101. locRes : AnsiString;
  102. begin
  103. if ( Length(AValue) > 0 ) then begin
  104. SetLength(locRes, ( 2 * Length(AValue) ) );
  105. BinToHex(PAnsiChar(@(AValue[0])),PAnsiChar(@(locRes[1])),Length(AValue));
  106. Result := locRes;
  107. end else begin
  108. Result := '';
  109. end;
  110. end;
  111. function RandomDate() : TSDODateTime ;
  112. begin
  113. Result.Date := RandomRange(0,50000);
  114. Result.HourOffset := RandomRange(-12,12);
  115. Result.MinuteOffset := RandomRange(-59,59)
  116. end;
  117. function sdoExpandLocalFileName(const AFileName : string) : string;
  118. begin
  119. Result := ExtractFilePath(ParamStr(0)) + AFileName;
  120. end;
  121. function RandomBytes(const AMaxLen : Integer) : TSDOBytes;
  122. var
  123. i : Integer;
  124. begin
  125. if ( AMaxLen > 0 ) then begin
  126. SetLength(Result,AMaxLen);
  127. for i:= 0 to Pred(AMaxLen) do
  128. Result[i] := RandomRange(Low(Byte),High(Byte));
  129. end else begin
  130. Result := nil;
  131. end;
  132. end;
  133. function RandomDouble(const AFrom, ATo: Integer): TSDODouble;
  134. begin
  135. Result := RandomRange(AFrom,ATo)
  136. end;
  137. function RandomFloat(const AFrom, ATo: Integer): TSDOFloat;
  138. begin
  139. Result := RandomRange(AFrom,ATo)
  140. end;
  141. { TWstBaseTest }
  142. {$IFDEF FPC}
  143. procedure TWstBaseTest.CheckEquals(expected, actual: Int64; msg: string;
  144. const AStrict: Boolean);
  145. begin
  146. if (expected <> actual) then
  147. FailNotEquals(IntToStr(expected), IntToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF WST_DELPHI});
  148. end;
  149. procedure TWstBaseTest.CheckEquals(expected, actual: QWord; msg: string;
  150. const AStrict: Boolean);
  151. begin
  152. if (expected <> actual) then
  153. FailNotEquals(IntToStr(expected), IntToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF WST_DELPHI});
  154. end;
  155. {$ENDIF FPC}
  156. procedure TWstBaseTest.CheckEquals(expected, actual: TSDOBytes;
  157. msg: string; const AStrict: Boolean
  158. );
  159. begin
  160. if ( expected = nil ) then begin
  161. Check(actual = nil, msg);
  162. end else begin
  163. CheckEquals(Length(expected),Length(actual),msg);
  164. if ( Length(expected) > 0 ) then
  165. Check(CompareMem(Pointer(expected), Pointer(actual),Length(expected)),msg);
  166. end;
  167. end;
  168. procedure TWstBaseTest.CheckEqualsCurrency(expected, actual: Currency; const delta: Currency; const msg: string);
  169. begin
  170. if ( delta = 0 ) then begin
  171. if (expected <> actual) then
  172. FailNotEquals(CurrToStr(expected), CurrToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF});
  173. end else begin
  174. if ( Abs(expected-actual) > delta ) then
  175. FailNotEquals(CurrToStr(expected), CurrToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF});
  176. end;
  177. end;
  178. procedure TWstBaseTest.CheckEqualsCurrency(expected, actual: Currency;
  179. const msg: string);
  180. begin
  181. CheckEqualsCurrency(expected,actual,0,msg);
  182. end;
  183. end.