THRIFT-2886 Integrate binary type in standard Thrift cross test
Client: Haxe
Patch: Jens Geyer
diff --git a/test/haxe/src/TestClient.hx b/test/haxe/src/TestClient.hx
index 276cd0f..f77620f 100644
--- a/test/haxe/src/TestClient.hx
+++ b/test/haxe/src/TestClient.hx
@@ -21,6 +21,7 @@
import haxe.Int32;
import haxe.Int64;
+import haxe.io.Bytes;
import haxe.Timer;
import haxe.ds.IntMap;
import haxe.ds.StringMap;
@@ -307,6 +308,43 @@
}
+ public static function BytesToHex(data : Bytes) : String {
+ var hex = "";
+ for ( i in 0 ... data.length) {
+ hex += StringTools.hex( data.get(i), 2);
+ }
+ return hex;
+ }
+
+ public static function PrepareTestData(randomDist : Bool) : Bytes {
+ var retval = Bytes.alloc(0x100);
+ var initLen : Int = (retval.length > 0x100 ? 0x100 : retval.length);
+
+ // linear distribution, unless random is requested
+ if (!randomDist) {
+ for (i in 0 ... initLen) {
+ retval.set(i, i % 0x100);
+ }
+ return retval;
+ }
+
+ // random distribution
+ for (i in 0 ... initLen) {
+ retval.set(i, 0);
+ }
+ for (i in 1 ... initLen) {
+ while( true) {
+ var nextPos = Std.random(initLen);
+ if (retval.get(nextPos) == 0) {
+ retval.set( nextPos, i % 0x100);
+ break;
+ }
+ }
+ }
+ return retval;
+ }
+
+
public static function ClientTest( transport : TTransport, protocol : TProtocol,
args : Arguments, rslt : TestResults) : Void
{
@@ -417,6 +455,23 @@
trace(' = $dub');
rslt.Expect(dub == 5.325098235, '$dub == 5.325098235');
+ var binOut = PrepareTestData(true);
+ trace('testBinary('+BytesToHex(binOut)+')');
+ try {
+ var binIn = client.testBinary(binOut);
+ trace('testBinary() = '+BytesToHex(binIn));
+ rslt.Expect( binIn.length == binOut.length, '${binIn.length} == ${binOut.length}');
+ var len = ((binIn.length < binOut.length) ? binIn.length : binOut.length);
+ for (ofs in 0 ... len) {
+ if (binIn.get(ofs) != binOut.get(ofs)) {
+ rslt.Expect( false, 'testBinary('+BytesToHex(binOut)+'): content mismatch at offset $ofs');
+ }
+ }
+ }
+ catch (e : TApplicationException) {
+ trace('testBinary('+BytesToHex(binOut)+'): '+e.errorMsg); // may not be supported by the server
+ }
+
rslt.StartTestGroup( TestResults.EXITCODE_FAILBIT_STRUCTS);
diff --git a/test/haxe/src/TestServerHandler.hx b/test/haxe/src/TestServerHandler.hx
index 9fc7d14..9e2a633 100644
--- a/test/haxe/src/TestServerHandler.hx
+++ b/test/haxe/src/TestServerHandler.hx
@@ -117,6 +117,23 @@
}
/**
+ * Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
+ * @param binary thing - the binary data to print
+ * @return binary - returns the binary 'thing'
+ *
+ * @param thing
+ */
+ public function testBinary(thing : haxe.io.Bytes) : haxe.io.Bytes
+ {
+ var hex = "";
+ for ( i in 0 ... thing.length) {
+ hex += StringTools.hex( thing.get(i), 2);
+ }
+ trace('testBinary($hex)');
+ return thing;
+ }
+
+ /**
* Prints 'testStruct("{%s}")' where thing has been formatted
* into a string of comma separated values
* @param Xtruct thing - the Xtruct to print