test_suite_utils.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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] := Char(RandomRange(Ord('a'),Ord('z')));
  79. 2 : Result[i] := Char(RandomRange(Ord('0'),Ord('9')));
  80. 3 : Result[i] := Char(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;
  96. end;
  97. function BytesToString(const AValue: array of Byte): TSDOString;
  98. var
  99. locRes : AnsiString;
  100. begin
  101. if ( Length(AValue) > 0 ) then begin
  102. SetLength(locRes, ( 2 * Length(AValue) ) );
  103. BinToHex(PAnsiChar(@(AValue[0])),PAnsiChar(@(locRes[1])),Length(AValue));
  104. Result := locRes;
  105. end;
  106. end;
  107. function RandomDate() : TSDODateTime ;
  108. begin
  109. Result.Date := RandomRange(0,50000);
  110. Result.HourOffset := RandomRange(-12,12);
  111. Result.MinuteOffset := RandomRange(-59,59)
  112. end;
  113. function sdoExpandLocalFileName(const AFileName : string) : string;
  114. begin
  115. Result := ExtractFilePath(ParamStr(0)) + AFileName;
  116. end;
  117. function RandomBytes(const AMaxLen : Integer) : TSDOBytes;
  118. var
  119. i : Integer;
  120. begin
  121. if ( AMaxLen > 0 ) then begin
  122. SetLength(Result,AMaxLen);
  123. for i:= 0 to Pred(AMaxLen) do
  124. Result[i] := RandomRange(Low(Byte),High(Byte));
  125. end else begin
  126. Result := nil;
  127. end;
  128. end;
  129. function RandomDouble(const AFrom, ATo: Integer): TSDODouble;
  130. begin
  131. Result := RandomRange(AFrom,ATo)
  132. end;
  133. function RandomFloat(const AFrom, ATo: Integer): TSDOFloat;
  134. begin
  135. Result := RandomRange(AFrom,ATo)
  136. end;
  137. { TWstBaseTest }
  138. {$IFDEF FPC}
  139. procedure TWstBaseTest.CheckEquals(expected, actual: Int64; msg: string;
  140. const AStrict: Boolean);
  141. begin
  142. if (expected <> actual) then
  143. FailNotEquals(IntToStr(expected), IntToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF WST_DELPHI});
  144. end;
  145. procedure TWstBaseTest.CheckEquals(expected, actual: QWord; msg: string;
  146. const AStrict: Boolean);
  147. begin
  148. if (expected <> actual) then
  149. FailNotEquals(IntToStr(expected), IntToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF WST_DELPHI});
  150. end;
  151. {$ENDIF FPC}
  152. procedure TWstBaseTest.CheckEquals(expected, actual: TSDOBytes;
  153. msg: string; const AStrict: Boolean
  154. );
  155. begin
  156. if ( expected = nil ) then begin
  157. Check(actual = nil, msg);
  158. end else begin
  159. CheckEquals(Length(expected),Length(actual),msg);
  160. if ( Length(expected) > 0 ) then
  161. Check(CompareMem(Pointer(expected), Pointer(actual),Length(expected)),msg);
  162. end;
  163. end;
  164. procedure TWstBaseTest.CheckEqualsCurrency(expected, actual: Currency; const delta: Currency; const msg: string);
  165. begin
  166. if ( delta = 0 ) then begin
  167. if (expected <> actual) then
  168. FailNotEquals(CurrToStr(expected), CurrToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF});
  169. end else begin
  170. if ( Abs(expected-actual) > delta ) then
  171. FailNotEquals(CurrToStr(expected), CurrToStr(actual), msg{$IFDEF WST_DELPHI}, CallerAddr{$ENDIF});
  172. end;
  173. end;
  174. procedure TWstBaseTest.CheckEqualsCurrency(expected, actual: Currency;
  175. const msg: string);
  176. begin
  177. CheckEqualsCurrency(expected,actual,0,msg);
  178. end;
  179. end.