diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/BaseExtendedTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/BaseExtendedTest.kt new file mode 100644 index 00000000000..a7cc1c21651 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/BaseExtendedTest.kt @@ -0,0 +1,248 @@ +package tests + +import java_msg.BaseMessage +import java_msg.ExtendedMessage +import main.kotlin.Base +import main.kotlin.CodedInputStream +import main.kotlin.Extended +import java.io.ByteArrayOutputStream + + +/** + * Created by user on 8/11/16. + */ + +object BaseExtendedTest { + fun generateKtBaseMessage(): Base { + val arrSize = RandomGen.rnd.nextInt(1000) + val arr = LongArray(arrSize) + for (i in 0..(arrSize - 1)) { + arr[i] = RandomGen.rnd.nextLong() + } + + val flag = if (RandomGen.rnd.nextInt() % 2 == 0) false else true + + val int = RandomGen.rnd.nextInt() + + return Base.BuilderBase(arr, flag, int).build() + } + + fun generateKtExtendedMessage(): Extended { + val arrSize = RandomGen.rnd.nextInt(1000) + val arr = LongArray(arrSize) + for (i in 0..(arrSize - 1)) { + arr[i] = RandomGen.rnd.nextLong() + } + + val flag = if (RandomGen.rnd.nextInt() % 2 == 0) false else true + + val int = RandomGen.rnd.nextInt() + + val long = RandomGen.rnd.nextLong() + + val longsSize = RandomGen.rnd.nextInt(1000) + val longs = LongArray(longsSize) + for (i in 0..(longsSize - 1)) { + longs[i] = RandomGen.rnd.nextLong() + } + + return Extended.BuilderExtended(arr, longs, flag, long, int).build() + } + + fun generateJvBaseMessage(): BaseMessage.Base { + val arrSize = RandomGen.rnd.nextInt(1000) + val arr = LongArray(arrSize) + for (i in 0..(arrSize - 1)) { + arr[i] = RandomGen.rnd.nextLong() + } + + val flag = if (RandomGen.rnd.nextInt() % 2 == 0) false else true + + val int = RandomGen.rnd.nextInt() + + return BaseMessage.Base.newBuilder() + .addAllArr(arr.asIterable()) + .setFlag(flag) + .setInt(int) + .build() + } + + fun generateJvExtendedMessage(): ExtendedMessage.Extended { + val arrSize = RandomGen.rnd.nextInt(1000) + val arr = LongArray(arrSize) + for (i in 0..(arrSize - 1)) { + arr[i] = RandomGen.rnd.nextLong() + } + + val flag = if (RandomGen.rnd.nextInt() % 2 == 0) false else true + + val int = RandomGen.rnd.nextInt() + + val long = RandomGen.rnd.nextLong() + + val longsSize = RandomGen.rnd.nextInt(1000) + val longs = LongArray(longsSize) + for (i in 0..(longsSize - 1)) { + longs[i] = RandomGen.rnd.nextLong() + } + + return ExtendedMessage.Extended.newBuilder() + .addAllArr(arr.asIterable()) + .addAllArrLongs(longs.asIterable()) + .setFlag(flag) + .setInt(int) + .setLong(long) + .build() + } + + fun compareBases(kt: main.kotlin.Base, jv: BaseMessage.Base): Boolean { + return kt.flag == jv.flag && + kt.int == jv.int && + Util.compareArrays(kt.arr.asIterable(), jv.arrList) + } + + fun compareExtended(kt: main.kotlin.Extended, jv: ExtendedMessage.Extended): Boolean { + return kt.flag == jv.flag && + kt.int == jv.int && + Util.compareArrays(kt.arr.asIterable(), jv.arrList) && + Util.compareArrays(kt.arr_longs.asIterable(), jv.arrLongsList) && + kt.long == jv.long + } + + fun compareBaseKtToJavaExtended(kt: main.kotlin.Base, jv: ExtendedMessage.Extended): Boolean { + return kt.flag == jv.flag && + kt.int == jv.int && + Util.compareArrays(kt.arr.asIterable(), jv.arrList) + } + + fun compareExtendedKtToJavaBase(kt: main.kotlin.Extended, jv: BaseMessage.Base): Boolean { + return kt.flag == jv.flag && + kt.int == jv.int && + Util.compareArrays(kt.arr.asIterable(), jv.arrList) + } + + fun baseKtToBaseJavaOnce() { + val kt = generateKtBaseMessage() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = BaseMessage.Base.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareBases(kt, jv)) + } + + fun baseJavaToBaseKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvBaseMessage() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = Base.BuilderBase(LongArray(0), false, 0).build() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareBases(kt, jv)) + } + + fun baseKtToExtendedJavaOnce() { + val kt = generateKtBaseMessage() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = ExtendedMessage.Extended.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareBaseKtToJavaExtended(kt, jv)) + } + + fun extendedJavaToBaseKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvExtendedMessage() + jv.writeTo(outs) + + val ins = CodedInputStream(outs.toByteArray()) + + val kt = Base.BuilderBase(LongArray(0), false, 0).build() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareBaseKtToJavaExtended(kt, jv)) + } + + fun extendedKtToBaseJavaOnce() { + val kt = generateKtExtendedMessage() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = BaseMessage.Base.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareExtendedKtToJavaBase(kt, jv)) + } + + fun baseJavaToExtendedKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvBaseMessage() + jv.writeTo(outs) + + val ins = CodedInputStream(outs.toByteArray()) + + val kt = Extended.BuilderExtended(LongArray(0), LongArray(0), false, 0L, 0).build() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareExtendedKtToJavaBase(kt, jv)) + } + + fun extendedKtToExtendedJavaOnce() { + val kt = generateKtExtendedMessage() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = ExtendedMessage.Extended.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareExtended(kt, jv)) + } + + fun extendedJavaToExtendedKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvExtendedMessage() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = Extended.BuilderExtended(LongArray(0), LongArray(0), false, 0L, 0).build() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareExtended(kt, jv)) + } + + val testRuns = 1000 + fun runTests() { + for (i in 0..testRuns) { + // base - base + baseJavaToBaseKtOnce() + baseKtToBaseJavaOnce() + + // base - extended + baseKtToExtendedJavaOnce() + // extendedJavaToBaseKtOnce() - currently failing, proper parsing of unknown fields is needed. + + // extended - base + baseJavaToExtendedKtOnce() + extendedKtToBaseJavaOnce() + + // extended - extended + extendedJavaToExtendedKtOnce() + extendedKtToExtendedJavaOnce() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/ConnectTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/ConnectTest.kt new file mode 100644 index 00000000000..1c5f77c93ed --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/ConnectTest.kt @@ -0,0 +1,88 @@ +package tests + +import java_msg.Connect +import main.kotlin.CodedInputStream +import java.io.ByteArrayOutputStream + +object ConnectTest { + fun generateKotlinConnectionRequestMessage(): main.kotlin.ConnectionRequest { + val arrSize = RandomGen.rnd.nextInt(1000) + val arr = IntArray(arrSize) + for (i in 0..(arrSize - 1)) { + arr[i] = RandomGen.rnd.nextInt() + } + + val port = RandomGen.rnd.nextInt() + + val msg = main.kotlin.ConnectionRequest.BuilderConnectionRequest(arr, port).build() + + return msg + } + + + fun generateJavaConnectionRequest(): Connect.ConnectionRequest { + val arrSize = RandomGen.rnd.nextInt(1000) + val arr = IntArray(arrSize) + for (i in 0..(arrSize - 1)) { + arr[i] = RandomGen.rnd.nextInt() + } + + val port = RandomGen.rnd.nextInt() + + val msg = Connect.ConnectionRequest.newBuilder().addAllIp(arr.asIterable()).setPort(port).build() + + return msg + } + + + + fun compareConnectionRequests(kt: main.kotlin.ConnectionRequest, jv: Connect.ConnectionRequest): Boolean { + return Util.compareArrays(kt.ip.asIterable(), jv.ipList.asIterable()) + } + + + fun ktToJavaOnce() { + val ktConnectionRequest = generateKotlinConnectionRequestMessage() + val outs = Util.getKtOutputStream(ktConnectionRequest.getSizeNoTag()) + ktConnectionRequest.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jvConnectionRequest = Connect.ConnectionRequest.parseFrom(ins) + + Util.assert(ktConnectionRequest.errorCode == 0) + Util.assert(Util.compareArrays(ktConnectionRequest.ip.asIterable(), jvConnectionRequest.ipList)) + Util.assert(ktConnectionRequest.port == jvConnectionRequest.port) + } + + val testRuns = 10 + + fun KtToJava() { + for (i in 0..testRuns) { + ktToJavaOnce() + } + } + + fun JavaToKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jvConnectionRequest = generateJavaConnectionRequest() + jvConnectionRequest.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val ktConnectionRequest = main.kotlin.ConnectionRequest.BuilderConnectionRequest(IntArray(0), 0).parseFrom(ins) + + Util.assert(ktConnectionRequest.errorCode == 0) + Util.assert(Util.compareArrays(ktConnectionRequest.ip.asIterable(), jvConnectionRequest.ipList)) + Util.assert(ktConnectionRequest.port == jvConnectionRequest.port) + } + + fun JavaToKt() { + for (i in 0..testRuns) { + JavaToKtOnce() + } + } + + fun runTests() { + KtToJava() + JavaToKt() + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/CrossBranchTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/CrossBranchTest.kt new file mode 100644 index 00000000000..92a7dab17ca --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/CrossBranchTest.kt @@ -0,0 +1,130 @@ +package tests + +import java_msg.CrossBranchOuterClass +import main.kotlin.CodedInputStream +import main.kotlin.CrossBranch +import java.io.ByteArrayOutputStream + +/** + * Created by user on 8/11/16. + */ + +object CrossBranchTest { + fun generateKtGrandFather(): main.kotlin.CrossBranch.Grandfather { + fun generateKtRightFather(): main.kotlin.CrossBranch.Grandfather.RightFather { + fun generateKtRightLeftSon(): main.kotlin.CrossBranch.Grandfather.RightFather.RightLeftSon { + fun generateKtLeftLeftSon(): main.kotlin.CrossBranch.Grandfather.LeftFather.LeftLeftSon { + return CrossBranch.Grandfather.LeftFather.LeftLeftSon.BuilderLeftLeftSon(RandomGen.rnd.nextInt()).build() + } + + val msg = CrossBranch.Grandfather.RightFather.RightLeftSon.BuilderRightLeftSon( + generateKtLeftLeftSon() + ).build() + return msg + } + + fun generateKtRightRightSon(): CrossBranch.Grandfather.RightFather.RightRightSon { + fun generateKtLeftRightSon(): main.kotlin.CrossBranch.Grandfather.LeftFather.LeftRightSon { + return CrossBranch.Grandfather.LeftFather.LeftRightSon.BuilderLeftRightSon(RandomGen.rnd.nextInt()).build() + } + + return CrossBranch.Grandfather.RightFather.RightRightSon.BuilderRightRightSon( + generateKtLeftRightSon() + ).build() + } + + val msg = main.kotlin.CrossBranch.Grandfather.RightFather.BuilderRightFather( + generateKtRightLeftSon(), + generateKtRightRightSon() + ).build() + + return msg + } + + return CrossBranch.Grandfather.BuilderGrandfather(generateKtRightFather()).build() + } + + fun generateJvGrandFather(): CrossBranchOuterClass.CrossBranch.Grandfather { + fun generateJvRightFather(): CrossBranchOuterClass.CrossBranch.Grandfather.RightFather { + fun generateJvRightLeftSon(): CrossBranchOuterClass.CrossBranch.Grandfather.RightFather.RightLeftSon { + fun generateJvLeftLeftSon(): CrossBranchOuterClass.CrossBranch.Grandfather.LeftFather.LeftLeftSon { + return CrossBranchOuterClass.CrossBranch.Grandfather.LeftFather.LeftLeftSon.newBuilder() + .setSonField(RandomGen.rnd.nextInt()).build() + } + + return CrossBranchOuterClass.CrossBranch.Grandfather.RightFather.RightLeftSon.newBuilder() + .setSonField(generateJvLeftLeftSon()).build() + } + + fun generateJvRightRightSon(): CrossBranchOuterClass.CrossBranch.Grandfather.RightFather.RightRightSon { + fun generateJvLeftRightSon(): CrossBranchOuterClass.CrossBranch.Grandfather.LeftFather.LeftRightSon { + return CrossBranchOuterClass.CrossBranch.Grandfather.LeftFather.LeftRightSon.newBuilder() + .setSonField(RandomGen.rnd.nextInt()).build() + } + + return CrossBranchOuterClass.CrossBranch.Grandfather.RightFather.RightRightSon.newBuilder() + .setSonField(generateJvLeftRightSon()).build() + } + + return CrossBranchOuterClass.CrossBranch.Grandfather.RightFather.newBuilder() + .setRls( + generateJvRightLeftSon() + ) + .setRrs( + generateJvRightRightSon() + ) + .build() + } + + return CrossBranchOuterClass.CrossBranch.Grandfather.newBuilder().setRf(generateJvRightFather()).build() + } + + fun compareGrandFathers(kt: CrossBranch.Grandfather, jv: CrossBranchOuterClass.CrossBranch.Grandfather): Boolean { + return kt.rf.rls.son_field.son_field == jv.rf.rls.sonField.sonField && + kt.rf.rrs.son_field.son_field == jv.rf.rrs.sonField.sonField + } + + val testRuns = 10 + + fun KtToJavaOnce() { + val kt = generateKtGrandFather() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = CrossBranchOuterClass.CrossBranch.Grandfather.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareGrandFathers(kt, jv)) + } + + fun KtToJava() { + for (i in 0..testRuns) { + KtToJavaOnce() + } + } + + fun JavaToKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvGrandFather() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = generateKtGrandFather() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareGrandFathers(kt, jv)) + } + + fun JavaToKT() { + for (i in 0..testRuns) { + JavaToKtOnce() + } + } + + fun runTests() { + KtToJava() + JavaToKT() + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/DirectionTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/DirectionTest.kt new file mode 100644 index 00000000000..0eb858df00c --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/DirectionTest.kt @@ -0,0 +1,68 @@ +package tests + +import java_msg.Direction +import main.kotlin.CodedInputStream +import main.kotlin.DirectionRequest +import java.io.ByteArrayOutputStream + +object DirectionTest { + fun generateKtDirectionRequest(): DirectionRequest { + return DirectionRequest.BuilderDirectionRequest( + DirectionRequest.Command.fromIntToCommand(RandomGen.rnd.nextInt(4)) + ).build() + } + + fun generateJvDirectionRequest(): Direction.DirectionRequest { + return Direction.DirectionRequest.newBuilder() + .setCommandValue(RandomGen.rnd.nextInt(4) + ).build() + } + + fun compareDirectionRequests(kt: DirectionRequest, jv: Direction.DirectionRequest): Boolean { + return kt.command.id == jv.command.number + } + + val testRuns = 1000 + + fun KtToJavaOnce() { + val kt = generateKtDirectionRequest() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Direction.DirectionRequest.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareDirectionRequests(kt, jv)) + } + + fun KtToJava() { + for (i in 0..testRuns) { + KtToJavaOnce() + } + } + + fun JavaToKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvDirectionRequest() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = generateKtDirectionRequest() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareDirectionRequests(kt, jv)) + } + + fun JavaToKT() { + for (i in 0..testRuns) { + JavaToKtOnce() + } + } + + fun runTests() { + KtToJava() + JavaToKT() + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/Location.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/Location.kt new file mode 100644 index 00000000000..80ec7384c81 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/Location.kt @@ -0,0 +1,85 @@ +package tests + +import java_msg.Location +import main.kotlin.CodedInputStream +import main.kotlin.LocationResponse +import java.io.ByteArrayOutputStream + +/** + * Created by user on 8/11/16. + */ + +object Location { + fun generateKtLocationResponse(): LocationResponse { + fun generateKtLocationData(): LocationResponse.LocationData { + return LocationResponse.LocationData.BuilderLocationData( + RandomGen.rnd.nextInt(), + RandomGen.rnd.nextInt(), + RandomGen.rnd.nextInt() + ).build() + } + + return LocationResponse.BuilderLocationResponse( + generateKtLocationData(), + RandomGen.rnd.nextInt(), + RandomGen.rnd.nextInt() + ).build() + } + + fun generateJvLocationResponse(): Location.LocationResponse { + fun generateJvLocationData(): Location.LocationResponse.LocationData { + return java_msg.Location.LocationResponse.LocationData.newBuilder() + .setX(RandomGen.rnd.nextInt()) + .setY(RandomGen.rnd.nextInt()) + .setAngle(RandomGen.rnd.nextInt()) + .build() + } + + return java_msg.Location.LocationResponse.newBuilder() + .setLocationResponseData(generateJvLocationData()) + .setCode(RandomGen.rnd.nextInt()) + .build() + } + + fun compareLocationResponses(kt: LocationResponse, jv: Location.LocationResponse): Boolean { + fun compareLocationDatas(kt: LocationResponse.LocationData, jv: Location.LocationResponse.LocationData): Boolean { + return kt.x == jv.x && kt.y == jv.y && kt.angle == jv.angle + } + + return kt.code == jv.code && compareLocationDatas(kt.locationResponseData, jv.locationResponseData) + } + + fun ktToJavaOnce() { + val kt = generateKtLocationResponse() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = java_msg.Location.LocationResponse.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareLocationResponses(kt, jv)) + } + + fun JavaToKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvLocationResponse() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = LocationResponse.BuilderLocationResponse(LocationResponse.LocationData.BuilderLocationData(0, 0, 0).build(), 0, 0).build() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareLocationResponses(kt, jv)) + } + + val testRuns = 1000 + + fun runTests() { + for (i in 0..testRuns) { + ktToJavaOnce() + JavaToKtOnce() + } + } +} diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/MultipleMessagesTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/MultipleMessagesTest.kt new file mode 100644 index 00000000000..478a04db0ac --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/MultipleMessagesTest.kt @@ -0,0 +1,76 @@ +package tests + +import java_msg.MultipleMessages +import main.kotlin.CodedInputStream +import main.kotlin.FirstMessage +import main.kotlin.SecondMessage +import main.kotlin.ThirdMessage +import java.io.ByteArrayOutputStream + +/** + * Created by user on 8/11/16. + */ + +object MultipleMessagesTest { + fun generateKtFirstMessage(): FirstMessage { + return FirstMessage.BuilderFirstMessage(RandomGen.rnd.nextInt()).build() + } + + fun generateKtSecondMessage(): SecondMessage { + return SecondMessage.BuilderSecondMessage(generateKtFirstMessage()).build() + } + + fun generateKtThirdMessage(): ThirdMessage { + return ThirdMessage.BuilderThirdMessage(generateKtSecondMessage()).build() + } + + fun generateJvFirstMessage(): MultipleMessages.FirstMessage { + return MultipleMessages.FirstMessage.newBuilder().setIntField(RandomGen.rnd.nextInt()).build() + } + + fun generateJvSecondMessage(): MultipleMessages.SecondMessage { + return MultipleMessages.SecondMessage.newBuilder().setFirstMsg(generateJvFirstMessage()).build() + } + + fun generateJvThirdMessage(): MultipleMessages.ThirdMessage { + return MultipleMessages.ThirdMessage.newBuilder().setSecondMsg(generateJvSecondMessage()).build() + } + + fun compareThirdMessages(kt: ThirdMessage, jv: MultipleMessages.ThirdMessage): Boolean { + return kt.second_msg.first_msg.int_field == jv.secondMsg.firstMsg.intField + } + + fun ktToJavaOnce() { + val kt = generateKtThirdMessage() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = MultipleMessages.ThirdMessage.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareThirdMessages(kt, jv)) + } + + fun JavaToKtOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvThirdMessage() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = ThirdMessage.BuilderThirdMessage(SecondMessage.BuilderSecondMessage(FirstMessage.BuilderFirstMessage(0).build()).build()).build() + kt.mergeFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareThirdMessages(kt, jv)) + } + + val testRuns = 1000 + + fun runTests() { + for (i in 0..testRuns) { + ktToJavaOnce() + JavaToKtOnce() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RandomGen.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RandomGen.kt new file mode 100644 index 00000000000..0a50191d1c0 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RandomGen.kt @@ -0,0 +1,7 @@ +package tests + +import java.util.* + +object RandomGen { + val rnd = Random(42); +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RepeatedVarintsTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RepeatedVarintsTest.kt new file mode 100644 index 00000000000..914fb05b5f5 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RepeatedVarintsTest.kt @@ -0,0 +1,92 @@ +package tests + +import java_msg.RepeatedVarints +import main.kotlin.CodedInputStream +import main.kotlin.MessageRepeatedVarints +import java.io.ByteArrayOutputStream + +/** + * Created by user on 8/12/16. + */ + +object RepeatedVarintsTest { + fun generateKtRepeatedVarints(): MessageRepeatedVarints { + val int = Util.generateIntArray() + val long = Util.generateLongArray() + val sint = Util.generateIntArray() + val slong = Util.generateLongArray() + val bool = Util.generateBoolArray() + val uint = Util.generateIntArray() + val ulong = Util.generateLongArray() + + return MessageRepeatedVarints.BuilderMessageRepeatedVarints( + int, long, sint, slong, bool, uint, ulong + ).build() + } + + fun generateJvRepeatedVarints(): RepeatedVarints.MessageRepeatedVarints { + val int = Util.generateIntArray() + val long = Util.generateLongArray() + val sint = Util.generateIntArray() + val slong = Util.generateLongArray() + val bool = Util.generateBoolArray() + val uint = Util.generateIntArray() + val ulong = Util.generateLongArray() + + return RepeatedVarints.MessageRepeatedVarints.newBuilder() + .addAllInt(int.asIterable()) + .addAllLong(long.asIterable()) + .addAllSint(sint.asIterable()) + .addAllSlong(slong.asIterable()) + .addAllBl(bool.asIterable()) + .addAllUint(uint.asIterable()) + .addAllUlong(ulong.asIterable()) + .build() + } + + fun compareRepeatedVarints(kt: MessageRepeatedVarints, jv: RepeatedVarints.MessageRepeatedVarints): Boolean { + return Util.compareArrays(kt.int.asIterable(), jv.intList) && + Util.compareArrays(kt.long.asIterable(), jv.longList) && + Util.compareArrays(kt.sint.asIterable(), jv.sintList) && + Util.compareArrays(kt.slong.asIterable(), jv.slongList) && + Util.compareArrays(kt.bl.asIterable(), jv.blList) && + Util.compareArrays(kt.uint.asIterable(), jv.uintList) && + Util.compareArrays(kt.ulong.asIterable(), jv.ulongList) + } + + fun ktToJavaOnce() { + val kt = generateKtRepeatedVarints() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = RepeatedVarints.MessageRepeatedVarints.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareRepeatedVarints(kt, jv)) + } + + fun jvToKtOnce() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateJvRepeatedVarints() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageRepeatedVarints.BuilderMessageRepeatedVarints( + IntArray(0), LongArray(0), IntArray(0), LongArray(0), BooleanArray(0), IntArray(0), LongArray(0) + ).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareRepeatedVarints(kt, jv)) + } + + val testRuns = 1000 + + fun runTests() { + for (i in 0..testRuns) { + ktToJavaOnce() + jvToKtOnce() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RepeatedZigZag.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RepeatedZigZag.kt new file mode 100644 index 00000000000..2c06c1241c3 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/RepeatedZigZag.kt @@ -0,0 +1,66 @@ +package tests + +import java_msg.RepeatedZigzag +import main.kotlin.CodedInputStream +import main.kotlin.MessageRepeatedZigZag +import java.io.ByteArrayOutputStream + +/** + * Created by user on 8/12/16. + */ + +object RepeatedZigZag { + fun generateKtRepeatedZigZag(): MessageRepeatedZigZag { + val int = Util.generateIntArray() + val long = Util.generateLongArray() + return MessageRepeatedZigZag.BuilderMessageRepeatedZigZag(int, long).build() + } + + fun generateJvRepatedZigZag(): RepeatedZigzag.MessageRepeatedZigZag { + val int = Util.generateIntArray() + val long = Util.generateLongArray() + return RepeatedZigzag.MessageRepeatedZigZag.newBuilder() + .addAllInt(int.asIterable()) + .addAllLong(long.asIterable()) + .build() + } + + fun compareRepeatedZigZags(kt: MessageRepeatedZigZag, jv: RepeatedZigzag.MessageRepeatedZigZag): Boolean { + return Util.compareArrays(kt.int.asIterable(), jv.intList) && + Util.compareArrays(kt.long.asIterable(), jv.longList); + } + + fun ktToJavaOnce() { + val kt = generateKtRepeatedZigZag() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = RepeatedZigzag.MessageRepeatedZigZag.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareRepeatedZigZags(kt, jv)) + } + + fun jvToKtOnce() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateJvRepatedZigZag() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageRepeatedZigZag.BuilderMessageRepeatedZigZag(IntArray(0), LongArray(0)).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareRepeatedZigZags(kt, jv)) + } + + val testRuns = 1000 + + fun runTests() { + for (i in 0..testRuns) { + ktToJavaOnce() + jvToKtOnce() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/TagOrderTests.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/TagOrderTests.kt new file mode 100644 index 00000000000..e7a0ba0ea9c --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/TagOrderTests.kt @@ -0,0 +1,138 @@ +package tests + +import java_msg.RepeatedZigzag +import java_msg.TagOrderOuterClass +import java_msg.TagOrderShuffledOuterClass +import main.kotlin.CodedInputStream +import main.kotlin.TagOrder +import main.kotlin.TagOrderShuffled +import java.io.ByteArrayOutputStream + +object TagOrderTests { + fun generateKtTag(): TagOrder { + val int = RandomGen.rnd.nextInt() + val slong_array = Util.generateLongArray() + val sint = RandomGen.rnd.nextInt() + val enum = TagOrder.Enum.fromIntToEnum(RandomGen.rnd.nextInt(2)) + + return TagOrder.BuilderTagOrder(int, sint, slong_array, enum).build() + } + + fun generateKtTagShuffled(): TagOrderShuffled { + val int = RandomGen.rnd.nextInt() + val slong_array = Util.generateLongArray() + val sint = RandomGen.rnd.nextInt() + val enum = TagOrderShuffled.Enum.fromIntToEnum(RandomGen.rnd.nextInt(2)) + + return TagOrderShuffled.BuilderTagOrderShuffled(int, sint, slong_array, enum).build() + } + + fun generateJvTag(): TagOrderOuterClass.TagOrder { + val int = RandomGen.rnd.nextInt() + val slong_array = Util.generateLongArray() + val sint = RandomGen.rnd.nextInt() + val enum = TagOrderOuterClass.TagOrder.Enum.forNumber(RandomGen.rnd.nextInt(2)) + + return TagOrderOuterClass.TagOrder.newBuilder() + .setInt(int) + .addAllSlongArray(slong_array.asIterable()) + .setSint(int) + .setEnumField(enum) + .build() + } + + fun generateJvTagShuffled(): TagOrderShuffledOuterClass.TagOrderShuffled { + val int = RandomGen.rnd.nextInt() + val slong_array = Util.generateLongArray() + val sint = RandomGen.rnd.nextInt() + val enum = TagOrderShuffledOuterClass.TagOrderShuffled.Enum.forNumber(RandomGen.rnd.nextInt(2)) + + return TagOrderShuffledOuterClass.TagOrderShuffled.newBuilder() + .setInt(int) + .addAllSlongArray(slong_array.asIterable()) + .setSint(int) + .setEnumField(enum) + .build() + } + + fun compareTagAndShuffledTag(kt: TagOrder, jv: TagOrderShuffledOuterClass.TagOrderShuffled): Boolean { + return kt.enum_field.id == jv.enumField.number && + kt.int == jv.int && + kt.sint == jv.sint && + Util.compareArrays(kt.slong_array.asIterable(), jv.slongArrayList) + } + + fun compareShuffledAndTag(kt: TagOrderShuffled, jv: TagOrderOuterClass.TagOrder): Boolean { + return kt.enum_field.id == jv.enumField.number && + kt.int == jv.int && + kt.sint == jv.sint && + Util.compareArrays(kt.slong_array.asIterable(), jv.slongArrayList) + } + + fun jvTagToKtShuffledOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvTag() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = TagOrderShuffled.BuilderTagOrderShuffled( + 0, 0, LongArray(0), TagOrderShuffled.Enum.first_val + ).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareShuffledAndTag(kt, jv)) + } + + fun jvShuffledToKtTagOnce() { + val outs = ByteArrayOutputStream(100000) + + val jv = generateJvTagShuffled() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = TagOrder.BuilderTagOrder( + 0, 0, LongArray(0), TagOrder.Enum.first_val + ).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareTagAndShuffledTag(kt, jv)) + } + + fun ktTagToJvShuffled() { + val kt = generateKtTag() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = TagOrderShuffledOuterClass.TagOrderShuffled.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareTagAndShuffledTag(kt, jv)) + } + + fun ktShuffledToJvTag() { + val kt = generateKtTagShuffled() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = TagOrderOuterClass.TagOrder.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareShuffledAndTag(kt, jv)) + } + + val testRuns = 1000 + fun runTests() { + for (i in 0..testRuns) { + // kt -> java + ktShuffledToJvTag() + ktTagToJvShuffled() + + // java -> kt + jvShuffledToKtTagOnce() + jvTagToKtShuffledOnce() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/Util.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/Util.kt new file mode 100644 index 00000000000..129aa838f1a --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/Util.kt @@ -0,0 +1,71 @@ +package tests + +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.InputStream +import java.io.OutputStream +import java.util.concurrent.locks.Condition +import kotlin.system.exitProcess + +object Util { + var ARRAY_MAX_SIZE = 1000 + + fun assert(condition: Boolean) { + if (!condition) { + println("Assertion failed") + exitProcess(1) + } + } + + fun compareArrays (lhs: Iterable, rhs: Iterable): Boolean { + return lhs.count() == rhs.count() && lhs.filter { !rhs.contains(it) }.isEmpty() + } + + fun getKtInputStream(): main.kotlin.CodedInputStream { + val ba = ByteArray(100000) + val ins = main.kotlin.CodedInputStream(ba) + return ins + } + + fun getKtOutputStream(size: Int): main.kotlin.CodedOutputStream { + val ba = ByteArray(size) + val outs = main.kotlin.CodedOutputStream(ba) + return outs + } + + fun KtOutputStreamToInputStream(kt: main.kotlin.CodedOutputStream): InputStream { + return kt.buffer.inputStream() + } + + fun KtInputStreamToOutputStream(kt: main.kotlin.CodedInputStream): OutputStream { + val baos = ByteArrayOutputStream(kt.buffer.size) + return baos + } + + fun generateIntArray(): IntArray { + val size = RandomGen.rnd.nextInt(ARRAY_MAX_SIZE) + val arr = IntArray(size) + for (i in 0..(size - 1)) { + arr[i] = RandomGen.rnd.nextInt() + } + return arr + } + + fun generateLongArray(): LongArray { + val size = RandomGen.rnd.nextInt(ARRAY_MAX_SIZE) + val arr = LongArray(size) + for (i in 0..(size - 1)) { + arr[i] = RandomGen.rnd.nextLong() + } + return arr + } + + fun generateBoolArray(): BooleanArray { + val size = RandomGen.rnd.nextInt(ARRAY_MAX_SIZE) + val arr = BooleanArray(size) + for (i in 0..(size - 1)) { + arr[i] = RandomGen.rnd.nextBoolean() + } + return arr + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/VarintsTest.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/VarintsTest.kt new file mode 100644 index 00000000000..1f64761b4e9 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/VarintsTest.kt @@ -0,0 +1,224 @@ +package tests + +import java_msg.Varints +import main.kotlin.CodedInputStream +import main.kotlin.MessageVarints +import java.io.ByteArrayOutputStream +import java.util.* + +object VarintsTest { + fun generateKtVarint(): MessageVarints { + val int = RandomGen.rnd.nextInt() + val long = RandomGen.rnd.nextLong() + val sint = RandomGen.rnd.nextInt() + val slong = RandomGen.rnd.nextLong() + val bl = RandomGen.rnd.nextBoolean() + val uint = RandomGen.rnd.nextInt() + val ulong = RandomGen.rnd.nextLong() + val enum = MessageVarints.TestEnum.fromIntToTestEnum(RandomGen.rnd.nextInt(2)) + + return MessageVarints.BuilderMessageVarints( + int, long, sint, slong, bl, enum, uint, ulong + ).build() + } + + fun generateMaxKtVarint(): MessageVarints { + val int = Int.MAX_VALUE + val long = Long.MAX_VALUE + val sint = Int.MAX_VALUE + val slong = Long.MAX_VALUE + val bl = false + val uint = Int.MAX_VALUE + val ulong = Long.MAX_VALUE + val enum = MessageVarints.TestEnum.fromIntToTestEnum(RandomGen.rnd.nextInt(2)) + + return MessageVarints.BuilderMessageVarints( + int, long, sint, slong, bl, enum, uint, ulong + ).build() + } + + fun generateMinKtVarint(): MessageVarints { + val int = Int.MIN_VALUE + val long = Long.MIN_VALUE + val sint = Int.MIN_VALUE + val slong = Long.MIN_VALUE + val uint = Int.MIN_VALUE + val ulong = Long.MIN_VALUE + val bl = false + val enum = MessageVarints.TestEnum.fromIntToTestEnum(RandomGen.rnd.nextInt(2)) + + return MessageVarints.BuilderMessageVarints( + int, long, sint, slong, bl, enum, uint, ulong + ).build() + } + + fun generateJvVarint(): Varints.MessageVarints { + val int = RandomGen.rnd.nextInt() + val long = RandomGen.rnd.nextLong() + val sint = RandomGen.rnd.nextInt() + val slong = RandomGen.rnd.nextLong() + val bl = RandomGen.rnd.nextBoolean() + val uint = RandomGen.rnd.nextInt() + val ulong = RandomGen.rnd.nextLong() + val enum = Varints.MessageVarints.TestEnum.forNumber(RandomGen.rnd.nextInt(2)) + + return Varints.MessageVarints.newBuilder() + .setInt(int) + .setLong(long) + .setSint(sint) + .setSlong(slong) + .setBl(bl) + .setUint(uint) + .setUlong(ulong) + .setEnumField(enum) + .build() + } + + fun generateMaxJvVarint(): Varints.MessageVarints { + val int = Int.MAX_VALUE + val long = Long.MAX_VALUE + val sint = Int.MAX_VALUE + val slong = Long.MAX_VALUE + val bl = false + val uint = Int.MAX_VALUE + val ulong = Long.MAX_VALUE + val enum = Varints.MessageVarints.TestEnum.forNumber(RandomGen.rnd.nextInt(2)) + + return Varints.MessageVarints.newBuilder() + .setInt(int) + .setLong(long) + .setSint(sint) + .setSlong(slong) + .setBl(bl) + .setUint(uint) + .setUlong(ulong) + .setEnumField(enum) + .build() + } + + fun generateMinJvVarint(): Varints.MessageVarints { + val int = Int.MIN_VALUE + val long = Long.MIN_VALUE + val sint = Int.MIN_VALUE + val slong = Long.MIN_VALUE + val uint = Int.MIN_VALUE + val ulong = Long.MIN_VALUE + val bl = false + val enum = Varints.MessageVarints.TestEnum.forNumber(RandomGen.rnd.nextInt(2)) + + return Varints.MessageVarints.newBuilder() + .setInt(int) + .setLong(long) + .setSint(sint) + .setSlong(slong) + .setBl(bl) + .setUint(uint) + .setUlong(ulong) + .setEnumField(enum) + .build() + } + + fun compareVarints(kt: MessageVarints, jv: Varints.MessageVarints): Boolean { + return kt.int == jv.int && + kt.long == jv.long && + kt.sint == jv.sint && + kt.slong == jv.slong && + kt.bl == jv.bl && + kt.uint == jv.uint && + kt.ulong == jv.ulong && + kt.enumField.id == jv.enumField.number + } + + fun ktToJavaOnce() { + val kt = generateKtVarint() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Varints.MessageVarints.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareVarints(kt, jv)) + } + + fun jvToKtOnce() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateJvVarint() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageVarints.BuilderMessageVarints( + 0, 0, 0, 0, false, MessageVarints.TestEnum.firstVal, 0, 0 + ).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareVarints(kt, jv)) + } + + fun ktToJavaOnceMaxValues() { + val kt = generateMaxKtVarint() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Varints.MessageVarints.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareVarints(kt, jv)) + } + + fun ktToJavaOnceMinValues() { + val kt = generateMinKtVarint() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Varints.MessageVarints.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareVarints(kt, jv)) + } + + fun jvToKtOnceMaxValues() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateMaxJvVarint() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageVarints.BuilderMessageVarints( + 0, 0, 0, 0, false, MessageVarints.TestEnum.firstVal, 0, 0 + ).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareVarints(kt, jv)) + } + + fun jvToKtOnceMinValues() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateMinJvVarint() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageVarints.BuilderMessageVarints( + 0, 0, 0, 0, false, MessageVarints.TestEnum.firstVal, 0, 0 + ).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareVarints(kt, jv)) + } + + val testRuns = 1000 + + fun runTests() { + for (i in 0..testRuns) { + ktToJavaOnce() + jvToKtOnce() + + ktToJavaOnceMaxValues() + jvToKtOnceMaxValues() + + ktToJavaOnceMinValues() + jvToKtOnceMinValues() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/ZigZagTests.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/ZigZagTests.kt new file mode 100644 index 00000000000..d89b2fed3b5 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/ZigZagTests.kt @@ -0,0 +1,144 @@ +package tests + +import java_msg.Zigzag +import main.kotlin.CodedInputStream +import main.kotlin.MessageZigZag +import java.io.ByteArrayOutputStream + +object ZigZagTests { + fun generateKtZigZag(): MessageZigZag { + val int = RandomGen.rnd.nextInt() + val long = RandomGen.rnd.nextLong() + + return MessageZigZag.BuilderMessageZigZag(int, long).build() + } + + fun generateMaxKtZigZag(): MessageZigZag { + val int = Int.MAX_VALUE + val long = Long.MAX_VALUE + + return MessageZigZag.BuilderMessageZigZag(int, long).build() + } + + fun generateMinKtZigZag(): MessageZigZag { + val int = Int.MIN_VALUE + val long = Long.MIN_VALUE + + return MessageZigZag.BuilderMessageZigZag(int, long).build() + } + + fun generateJvZigZag(): Zigzag.MessageZigZag { + val int = RandomGen.rnd.nextInt() + val long = RandomGen.rnd.nextLong() + + return Zigzag.MessageZigZag.newBuilder().setInt(int).setLong(long).build() + } + + fun generateMaxJvZigZag(): Zigzag.MessageZigZag { + val int = Int.MAX_VALUE + val long = Long.MAX_VALUE + + return Zigzag.MessageZigZag.newBuilder().setInt(int).setLong(long).build() + } + + fun generateMinJvZigZag(): Zigzag.MessageZigZag { + val int = Int.MIN_VALUE + val long = Long.MIN_VALUE + + return Zigzag.MessageZigZag.newBuilder().setInt(int).setLong(long).build() + } + + fun compareZigZags(kt: MessageZigZag, jv: Zigzag.MessageZigZag): Boolean { + return kt.int == jv.int && kt.long == jv.long; + } + + fun ktToJavaOnce() { + val kt = generateKtZigZag() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Zigzag.MessageZigZag.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareZigZags(kt, jv)) + } + + fun ktToJavaOnceMaxValues() { + val kt = generateMaxKtZigZag() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Zigzag.MessageZigZag.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareZigZags(kt, jv)) + } + + fun ktToJavaOnceMinValues() { + val kt = generateMinKtZigZag() + val outs = Util.getKtOutputStream(kt.getSizeNoTag()) + kt.writeTo(outs) + + val ins = Util.KtOutputStreamToInputStream(outs) + val jv = Zigzag.MessageZigZag.parseFrom(ins) + + Util.assert(kt.errorCode == 0) + Util.assert(compareZigZags(kt, jv)) + } + + fun jvToKtOnce() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateJvZigZag() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageZigZag.BuilderMessageZigZag(0, 0L).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareZigZags(kt, jv)) + } + + fun jvToKtOnceMaxValues() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateMaxJvZigZag() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageZigZag.BuilderMessageZigZag(0, 0L).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareZigZags(kt, jv)) + } + + fun jvToKtOnceMinValues() { + val outs = ByteArrayOutputStream(10000000) + + val jv = generateMaxJvZigZag() + jv.writeTo(outs) + val ins = CodedInputStream(outs.toByteArray()) + val kt = MessageZigZag.BuilderMessageZigZag(0, 0L).parseFrom(ins).build() + + Util.assert(kt.errorCode == 0) + Util.assert(compareZigZags(kt, jv)) + } + + val testRuns = 1000 + + fun runTests() { + for (i in 0..testRuns) { + // random values + jvToKtOnce() + ktToJavaOnce() + + // max values + jvToKtOnceMaxValues() + ktToJavaOnceMaxValues() + + // min values + jvToKtOnceMinValues() + ktToJavaOnceMinValues() + } + } +} \ No newline at end of file diff --git a/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/main.kt b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/main.kt new file mode 100644 index 00000000000..220adc3f421 --- /dev/null +++ b/proto/compiler/google/src/google/protobuf/compiler/kotlin/protoc-test-project/src/tests/main.kt @@ -0,0 +1,53 @@ +package tests + +import java_msg.Varints + +/** + * Created by user on 8/11/16. + */ + +fun main(args: Array) { + print("Base-Extended tests...") + BaseExtendedTest.runTests() + println(" OK!") + + print("Connect Tests...") + ConnectTest.runTests() + println(" OK!") + + print("Cross-branch access tests...") + CrossBranchTest.runTests() + println(" OK!") + + print("Direction Tests...") + DirectionTest.runTests() + println(" OK!") + + print("Location Tests...") + Location.runTests() + println(" OK!") + + print("Multiple Messages Tests...") + MultipleMessagesTest.runTests() + println(" OK!") + + print("Repeated VarInts Tests...") + RepeatedVarintsTest.runTests() + println(" OK!") + + print("Repeated ZigZag Tests...") + RepeatedZigZag.runTests() + println(" OK!") + + print("Tag order Tests...") + TagOrderTests.runTests() + println(" OK!") + + print("Varints Tests...") + VarintsTest.runTests() + println(" OK!") + + print("ZigZag Tests...") + ZigZagTests.runTests() + println(" OK!") +} \ No newline at end of file