THRIFT-2886 Integrate binary type in standard Thrift cross test
Client: Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/test/TestClient.pas b/lib/delphi/test/TestClient.pas
index 9b091ae..6f155a2 100644
--- a/lib/delphi/test/TestClient.pas
+++ b/lib/delphi/test/TestClient.pas
@@ -25,7 +25,7 @@
 interface
 
 uses
-  Windows, SysUtils, Classes,
+  Windows, SysUtils, Classes, Math,
   DateUtils,
   Generics.Collections,
   TestConstants,
@@ -77,10 +77,11 @@
     procedure StartTestGroup( const aGroup : string; const aTest : TTestGroup);
     procedure Expect( aTestResult : Boolean; const aTestInfo : string);
     procedure ReportResults;
-    function CalculateExitCode : Byte;
+    function  CalculateExitCode : Byte;
 
     procedure ClientTest;
     procedure JSONProtocolReadWriteTest;
+    function  PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
     {$IFDEF StressTest}
     procedure StressTest(const client : TThriftTest.Iface);
     {$ENDIF}
@@ -394,6 +395,7 @@
   i8 : ShortInt;
   i32 : Integer;
   i64 : Int64;
+  binOut,binIn : TBytes;
   dub : Double;
   o : IXtruct;
   o2 : IXtruct2;
@@ -524,6 +526,18 @@
   i64 := client.testI64(-34359738368);
   Expect( i64 = -34359738368, 'testI64(-34359738368) = ' + IntToStr( i64));
 
+  binOut := PrepareBinaryData( TRUE);
+  Console.WriteLine('testBinary('+BytesToHex(binOut)+')');
+  try
+    binIn := client.testBinary(binOut);
+    Expect( Length(binOut) = Length(binIn), 'testBinary(): length '+IntToStr(Length(binOut))+' = '+IntToStr(Length(binIn)));
+    i32 := Min( Length(binOut), Length(binIn));
+    Expect( CompareMem( binOut, binIn, i32), 'testBinary('+BytesToHex(binOut)+') = '+BytesToHex(binIn));
+  except
+    on e:TApplicationException do Console.WriteLine('testBinary(): '+e.Message);
+    on e:Exception do Expect( FALSE, 'testBinary(): Unexpected exception "'+e.ClassName+'": '+e.Message);
+  end;
+
   Console.WriteLine('testDouble(5.325098235)');
   dub := client.testDouble(5.325098235);
   Expect( abs(dub-5.325098235) < 1e-14, 'testDouble(5.325098235) = ' + FloatToStr( dub));
@@ -961,6 +975,34 @@
 end;
 {$ENDIF}
 
+
+function TClientThread.PrepareBinaryData( aRandomDist : Boolean = FALSE) : TBytes;
+var i, nextPos : Integer;
+begin
+  SetLength( result, $100);
+  ASSERT( Low(result) = 0);
+
+  // linear distribution, unless random is requested
+  if not aRandomDist then begin
+    for i := Low(result) to High(result) do begin
+      result[i] := i;
+    end;
+    Exit;
+  end;
+
+  // random distribution of all 256 values
+  FillChar( result[0], Length(result) * SizeOf(result[0]), $0);
+  i := 1;
+  while i < Length(result) do begin
+    nextPos := Byte( Random($100));
+    if result[nextPos] = 0 then begin  // unused?
+      result[nextPos] := i;
+      Inc(i);
+    end;
+  end;
+end;
+
+
 procedure TClientThread.JSONProtocolReadWriteTest;
 // Tests only then read/write procedures of the JSON protocol
 // All tests succeed, if we can read what we wrote before
@@ -991,8 +1033,7 @@
     StartTestGroup( 'JsonProtocolTest', test_Unknown);
 
     // prepare binary data
-    SetLength( binary, $100);
-    for i := Low(binary) to High(binary) do binary[i] := i;
+    binary := PrepareBinaryData( FALSE);
 
     // output setup
     prot := TJSONProtocolImpl.Create(
diff --git a/lib/delphi/test/TestConstants.pas b/lib/delphi/test/TestConstants.pas
index e5aa6c5..37969dc 100644
--- a/lib/delphi/test/TestConstants.pas
+++ b/lib/delphi/test/TestConstants.pas
@@ -21,6 +21,8 @@
 
 interface
 
+uses SysUtils;
+
 type
   TKnownProtocol = (
     prot_Binary,  // default binary protocol
@@ -140,8 +142,21 @@
                    + 'diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet '
                    + 'clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ';
 
+
+function BytesToHex( const bytes : TBytes) : string;
+
+
 implementation
 
-// nothing
+
+function BytesToHex( const bytes : TBytes) : string;
+var i : Integer;
+begin
+  result := '';
+  for i := Low(bytes) to High(bytes) do begin
+    result := result + IntToHex(bytes[i],2);
+  end;
+end;
+
 
 end.
diff --git a/lib/delphi/test/TestServer.pas b/lib/delphi/test/TestServer.pas
index 35f1ac8..ad4823f 100644
--- a/lib/delphi/test/TestServer.pas
+++ b/lib/delphi/test/TestServer.pas
@@ -62,6 +62,7 @@
         function testI32(thing: Integer): Integer;
         function testI64(const thing: Int64): Int64;
         function testDouble(const thing: Double): Double;
+        function testBinary(const thing: TBytes): TBytes;
         function testStruct(const thing: IXtruct): IXtruct;
         function testNest(const thing: IXtruct2): IXtruct2;
         function testMap(const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
@@ -138,6 +139,12 @@
   Result := thing;
 end;
 
+function TTestServer.TTestHandlerImpl.testBinary(const thing: TBytes): TBytes;
+begin
+  Console.WriteLine('testBinary("' + BytesToHex( thing ) + '")');
+  Result := thing;
+end;
+
 function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz;
 begin
   Console.WriteLine('testEnum(' + IntToStr( Integer( thing)) + ')');
diff --git a/lib/delphi/test/codegen/run-Pascal-Codegen-Tests.bat.tmpl b/lib/delphi/test/codegen/run-Pascal-Codegen-Tests.bat.tmpl
index 4398545..dbab0ae 100644
--- a/lib/delphi/test/codegen/run-Pascal-Codegen-Tests.bat.tmpl
+++ b/lib/delphi/test/codegen/run-Pascal-Codegen-Tests.bat.tmpl
@@ -76,7 +76,7 @@
 echo.
 echo Generating code, please wait ...
 cd "%TARGET%"
-for %%a in (*.thrift) do "%BIN%\thrift.exe" -v --gen delphi:ansistr_binary,register_types,constprefix,events,xmldoc "%%a" 2>> "%LOGFILE%"
+for %%a in (*.thrift) do "%BIN%\thrift.exe" -v --gen delphi:register_types,constprefix,events,xmldoc "%%a" 2>> "%LOGFILE%"
 REM * for %%a in (*.thrift) do "%BIN%\thrift.exe" -v --gen cpp "%%a" >> NUL:
 cmd /c start notepad "%LOGFILE%"
 cd ..