From 255ccadf5d665fdeb3a6f6da86c4fd5ac45af36b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Mar 2026 11:36:56 +0000 Subject: Replace patio.protobuf with patio.serialization --- .../patio/protobuf/PatioToProtobufTests.java | 1050 -------------------- .../patio/serialization/PatioToProtobufTests.java | 1050 ++++++++++++++++++++ 2 files changed, 1050 insertions(+), 1050 deletions(-) delete mode 100644 src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/serialization/PatioToProtobufTests.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java b/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java deleted file mode 100644 index 9eaf4ef3..00000000 --- a/src/test/java/org/shedlang/compiler/patio/protobuf/PatioToProtobufTests.java +++ /dev/null @@ -1,1050 +0,0 @@ -package org.shedlang.compiler.patio.protobuf; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.patio.PatioFunctionDeclaration; -import org.shedlang.compiler.patio.PatioFunctionDefinition; -import org.shedlang.compiler.patio.PatioVisibility; -import org.shedlang.compiler.patio.instructions.Patio; -import org.shedlang.compiler.patio.types.PatioDataLayouts; -import org.shedlang.compiler.patio.types.PatioDataValues; -import org.shedlang.compiler.patio.types.PatioTypes; -import org.shedlang.compiler.sources.Sources; - -import java.util.List; -import java.util.Optional; - -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.equalTo; - -public class PatioToProtobufTests { - // == Compilation unit == - - @Test - public void canSerializeCompilationUnit() { - var element = PatioFunctionDefinition.builder() - .withName("f") - .withReturnType(PatioTypes.VOID) - .build(); - var compilationUnit = Patio.compilationUnit(List.of(element)); - - var deserializedCompilationUnit = PatioToProtobuf.deserializeCompilationUnit( - PatioToProtobuf.serializeCompilationUnit(compilationUnit) - ); - - assertThat(deserializedCompilationUnit, equalTo(compilationUnit)); - } - - // == Compilation unit elements == - - @Test - public void canSerializeDataElementImmutableDefinition() { - var element = Patio.dataElementImmutableDefinition( - "x", - PatioVisibility.PRIVATE, - PatioDataValues.array( - PatioDataLayouts.BYTE, - List.of(PatioDataValues.byte_(0x01), PatioDataValues.byte_(0x02)) - ) - ); - - var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( - PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedElement, equalTo(element)); - } - - @Test - public void canSerializeDataElementMutableDeclaration() { - var element = Patio.dataElementMutableDeclaration( - "x", - PatioDataLayouts.INT - ); - - var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( - PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedElement, equalTo(element)); - } - - @Test - public void canSerializeDataElementMutableDefinition() { - var element = Patio.dataElementMutableDefinition( - "x", - PatioVisibility.PRIVATE, - PatioDataValues.int_(42) - ); - - var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( - PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedElement, equalTo(element)); - } - - @Test - public void canSerializeExceptionTypeDefinition() { - var element = Patio.exceptionTypeDefinition("Error", PatioTypes.INT); - - var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( - PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedElement, equalTo(element)); - } - - @Test - public void canSerializeFunctionDeclaration() { - var element = PatioFunctionDeclaration.builder() - .withName("f") - .addParam(Patio.param("param1", PatioTypes.INT)) - .addParam(Patio.param("param2", PatioTypes.DATA_PTR)) - .withReturnType(PatioTypes.VOID) - .build(); - - var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( - PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedElement, equalTo(element)); - } - - @Test - public void canSerializeFunctionDefinition() { - var element = PatioFunctionDefinition.builder() - .withName("f") - .withVisibility(PatioVisibility.PRIVATE) - .addLocal(Patio.param("param1", PatioTypes.INT)) - .addLocal(Patio.param("param2", PatioTypes.DATA_PTR)) - .addLocal(Patio.val("local1", PatioTypes.FUNCTION_PTR)) - .addLocal(Patio.val("local2", PatioTypes.INT)) - .withReturnType(PatioTypes.VOID) - .addBodyInstruction(Patio.localStore(Patio.localRef("local2"), Patio.intConst(2))) - .build(); - - var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( - PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedElement, equalTo(element)); - } - - // == Instructions == - - @Test - public void canSerializeBlock() { - var instruction = Patio.block( - List.of( - Patio.localStore(Patio.localRef("x1"), Patio.intConst(1)), - Patio.localStore(Patio.localRef("y1"), Patio.intConst(2)) - ), - List.of( - Patio.localRef("x1"), - Patio.localRef("y1") - ) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeBoolToInt() { - var instruction = Patio.boolToInt(Patio.localRef("destination"), Patio.localRef("value")); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeCallWithoutDestination() { - var instruction = Patio.call( - Optional.empty(), - "f", - List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c")) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeCallWithDestination() { - var instruction = Patio.call( - Optional.of(Patio.localRef("destination")), - "f", - List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c")) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeCallPointer() { - var instruction = Patio.callPointer( - Patio.localRef("destination"), - Patio.localRef("f"), - List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c")) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeDataPtrToInt() { - var instruction = Patio.dataPtrToInt(Patio.localRef("destination"), Patio.localRef("value")); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIf() { - var instruction = Patio.if_( - List.of( - Patio.localRef("x"), - Patio.localRef("y") - ), - Patio.localRef("condition"), - Patio.block( - List.of( - Patio.localStore(Patio.localRef("x1"), Patio.intConst(1)), - Patio.localStore(Patio.localRef("y1"), Patio.intConst(2)) - ), - List.of( - Patio.localRef("x1"), - Patio.localRef("y1") - ) - ), - Patio.block( - List.of( - Patio.localStore(Patio.localRef("x2"), Patio.intConst(11)), - Patio.localStore(Patio.localRef("y2"), Patio.intConst(12)) - ), - List.of( - Patio.localRef("x2"), - Patio.localRef("y2") - ) - ) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIntAdd() { - var instruction = Patio.intAdd( - Patio.localRef("destination"), - Patio.localRef("left"), - Patio.localRef("right") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIntEquals() { - var instruction = Patio.intEquals( - Patio.localRef("destination"), - Patio.localRef("left"), - Patio.localRef("right") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIntNotEqual() { - var instruction = Patio.intNotEqual( - Patio.localRef("destination"), - Patio.localRef("left"), - Patio.localRef("right") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIntSub() { - var instruction = Patio.intSub( - Patio.localRef("destination"), - Patio.localRef("left"), - Patio.localRef("right") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIntToBool() { - var instruction = Patio.intToBool(Patio.localRef("destination"), Patio.localRef("value")); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeIntToDataPtr() { - var instruction = Patio.intToDataPtr( - Patio.localRef("destination"), - Patio.localRef("value") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeLocalStore() { - var instruction = Patio.localStore( - Patio.localRef("destination"), - Patio.localRef("x") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeLoop() { - var instruction = Patio.loop( - List.of( - Patio.localStore(Patio.localRef("x"), Patio.intConst(1)) - ) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeLoopBreak() { - var instruction = Patio.loopBreak(); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeLoopContinue() { - var instruction = Patio.loopContinue(); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeMemoryLoad() { - var instruction = Patio.memoryLoad( - Patio.localRef("destination"), - Patio.localRef("pointer"), - PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE)), - List.of(1) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeMemoryStore() { - var instruction = Patio.memoryStore( - Patio.localRef("pointer"), - PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE)), - List.of(1), - Patio.localRef("value") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeResume() { - var instruction = Patio.resume(Patio.localRef("value")); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeReturn() { - var instruction = Patio.return_(Patio.localRef("value")); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeReturnVoid() { - var instruction = Patio.returnVoid(); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeSwitch() { - var instruction = Patio.switch_( - List.of( - Patio.localRef("x"), - Patio.localRef("y") - ), - Patio.localRef("value"), - List.of( - Patio.switchCase( - 1, - Patio.block( - List.of( - Patio.localStore(Patio.localRef("x1"), Patio.intConst(11)), - Patio.localStore(Patio.localRef("y1"), Patio.intConst(12)) - ), - List.of( - Patio.localRef("x1"), - Patio.localRef("y1") - ) - ) - ), - Patio.switchCase( - 2, - Patio.block( - List.of( - Patio.localStore(Patio.localRef("x2"), Patio.intConst(21)), - Patio.localStore(Patio.localRef("y2"), Patio.intConst(22)) - ), - List.of( - Patio.localRef("x2"), - Patio.localRef("y2") - ) - ) - ) - ), - Patio.block( - List.of( - Patio.localStore(Patio.localRef("x3"), Patio.intConst(31)), - Patio.localStore(Patio.localRef("y3"), Patio.intConst(32)) - ), - List.of( - Patio.localRef("x3"), - Patio.localRef("y3") - ) - ) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeThrow() { - var instruction = Patio.throw_("Error", Patio.localRef("value")); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeTry() { - var instruction = Patio.try_( - List.of(Patio.localRef("x"), Patio.localRef("y")), - List.of( - Patio.catchClause( - "Error1", - Patio.localRef("a1"), - Patio.block( - List.of( - Patio.localStore(Patio.localRef("xCatch1"), Patio.intConst(11)), - Patio.localStore(Patio.localRef("yCatch1"), Patio.intConst(12)) - ), - List.of(Patio.localRef("xCatch1"), Patio.localRef("yCatch1")) - ) - ), - Patio.catchClause( - "Error2", - Patio.localRef("a2"), - Patio.block( - List.of( - Patio.localStore(Patio.localRef("xCatch2"), Patio.intConst(21)), - Patio.localStore(Patio.localRef("yCatch2"), Patio.intConst(22)) - ), - List.of(Patio.localRef("xCatch2"), Patio.localRef("yCatch2")) - ) - ) - ), - Patio.block( - List.of( - Patio.localStore(Patio.localRef("xTry"), Patio.intConst(31)), - Patio.localStore(Patio.localRef("yTry"), Patio.intConst(32)) - ), - List.of(Patio.localRef("xTry"), Patio.localRef("yTry")) - ) - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeUnreachable() { - var instruction = Patio.unreachable(); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeVarLoad() { - var instruction = Patio.varLoad( - Patio.localRef("destination"), - Patio.localRef("v") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - @Test - public void canSerializeVarStore() { - var instruction = Patio.varStore( - Patio.localRef("destination"), - Patio.localRef("value") - ); - - var deserializedInstruction = PatioToProtobuf.deserializeInstruction( - PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), - Sources.NULL - ); - - assertThat(deserializedInstruction, equalTo(instruction)); - } - - // == Operands == - - @Test - public void canSerializeBoolConst() { - var operand = Patio.boolConst(true); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - @Test - public void canSerializeByteConst() { - var operand = Patio.byteConst((byte) 42); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - @Test - public void canSerializeDataPtrConst() { - var operand = Patio.dataPtrConst("x"); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - @Test - public void canSerializeFunctionPtrConst() { - var operand = Patio.functionPtrConst("x"); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - @Test - public void canSerializeIntConst() { - var operand = Patio.intConst(42); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - @Test - public void canSerializeInt32Const() { - var operand = Patio.int32Const(42); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - @Test - public void canSerializeLocalRef() { - var operand = Patio.localRef("x"); - - var deserializedOperand = PatioToProtobuf.deserializeOperand( - PatioToProtobuf.serializeOperand(operand) - ); - - assertThat(deserializedOperand, equalTo(operand)); - } - - // == Data ptr values == - - @Test - public void canSerializeDataPtrValueDataElement() { - var dataPtrValue = Patio.dataPtrValueDataElement("string"); - - var deserializedDataPtrValue = PatioToProtobuf.deserializeDataPtrValue( - PatioToProtobuf.serializeDataPtrValue(dataPtrValue) - ); - - assertThat(deserializedDataPtrValue, equalTo(dataPtrValue)); - } - - @Test - public void canSerializeDataPtrValueNull() { - var dataPtrValue = Patio.dataPtrValueNull(); - - var deserializedDataPtrValue = PatioToProtobuf.deserializeDataPtrValue( - PatioToProtobuf.serializeDataPtrValue(dataPtrValue) - ); - - assertThat(deserializedDataPtrValue, equalTo(dataPtrValue)); - } - - // == Int values == - - @Test - public void canSerializeIntValueByteOffsetOf() { - var intValue = Patio.intValueByteOffsetOf( - PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT, - PatioDataLayouts.BYTE - )), - List.of(1, 2) - ); - - var deserializedIntValue = PatioToProtobuf.deserializeIntValue( - PatioToProtobuf.serializeIntValue(intValue) - ); - - assertThat(deserializedIntValue, equalTo(intValue)); - } - - @Test - public void canSerializeIntValueByteSizeOf() { - var intValue = Patio.intValueByteSizeOf( - PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT, - PatioDataLayouts.BYTE - )) - ); - - var deserializedIntValue = PatioToProtobuf.deserializeIntValue( - PatioToProtobuf.serializeIntValue(intValue) - ); - - assertThat(deserializedIntValue, equalTo(intValue)); - } - - @Test - public void canSerializeIntValueLiteral() { - var intValue = Patio.intValueLiteral(42); - - var deserializedIntValue = PatioToProtobuf.deserializeIntValue( - PatioToProtobuf.serializeIntValue(intValue) - ); - - assertThat(deserializedIntValue, equalTo(intValue)); - } - - // == Types == - - @Test - public void canSerializeBoolType() { - var type = PatioTypes.BOOL; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - @Test - public void canSerializeByteType() { - var type = PatioTypes.BYTE; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - @Test - public void canSerializeDataPtrType() { - var type = PatioTypes.DATA_PTR; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - @Test - public void canSerializeFunctionPtrType() { - var type = PatioTypes.FUNCTION_PTR; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - @Test - public void canSerializeIntType() { - var type = PatioTypes.INT; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - @Test - public void canSerializeInt32Type() { - var type = PatioTypes.INT32; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - @Test - public void canSerializeVoidType() { - var type = PatioTypes.VOID; - - var deserializedType = PatioToProtobuf.deserializeType( - PatioToProtobuf.serializeType(type) - ); - - assertThat(deserializedType, equalTo(type)); - } - - // == Data layouts == - - @Test - public void canSerializeDataLayoutArray() { - var layout = PatioDataLayouts.array(PatioDataLayouts.INT, 4); - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutBool() { - var layout = PatioDataLayouts.BOOL; - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutByte() { - var layout = PatioDataLayouts.BYTE; - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutDataPtr() { - var layout = PatioDataLayouts.DATA_PTR; - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutFunctionPtr() { - var layout = PatioDataLayouts.FUNCTION_PTR; - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutInt() { - var layout = PatioDataLayouts.INT; - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutInt32() { - var layout = PatioDataLayouts.INT32; - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - @Test - public void canSerializeDataLayoutStruct() { - var layout = PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE)); - - var deserializedLayout = PatioToProtobuf.deserializeDataLayout( - PatioToProtobuf.serializeDataLayout(layout) - ); - - assertThat(deserializedLayout, equalTo(layout)); - } - - // == Data values == - - @Test - public void canSerializeDataValueArray() { - var value = PatioDataValues.array( - PatioDataLayouts.BYTE, - List.of( - PatioDataValues.byte_(0x01), - PatioDataValues.byte_(0x02), - PatioDataValues.byte_(0x03) - ) - ); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } - - @Test - public void canSerializeDataValueBool() { - var value = PatioDataValues.bool(true); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } - - @Test - public void canSerializeDataValueByte() { - var value = PatioDataValues.byte_(0xc8); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } - - @Test - public void canSerializeDataValueDataPtr() { - var value = PatioDataValues.dataPtrNull(); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } - - @Test - public void canSerializeDataValueInt() { - var value = PatioDataValues.int_(42); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } - - @Test - public void canSerializeDataValueInt32() { - var value = PatioDataValues.int32(42); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } - - @Test - public void canSerializeDataValueStruct() { - var value = PatioDataValues.struct(List.of( - PatioDataValues.byte_(0x01), - PatioDataValues.byte_(0x02), - PatioDataValues.byte_(0x03) - )); - - var deserializedValue = PatioToProtobuf.deserializeDataValue( - PatioToProtobuf.serializeDataValue(value) - ); - - assertThat(deserializedValue, equalTo(value)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/serialization/PatioToProtobufTests.java b/src/test/java/org/shedlang/compiler/patio/serialization/PatioToProtobufTests.java new file mode 100644 index 00000000..396bed72 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/serialization/PatioToProtobufTests.java @@ -0,0 +1,1050 @@ +package org.shedlang.compiler.patio.serialization; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.patio.PatioFunctionDeclaration; +import org.shedlang.compiler.patio.PatioFunctionDefinition; +import org.shedlang.compiler.patio.PatioVisibility; +import org.shedlang.compiler.patio.instructions.Patio; +import org.shedlang.compiler.patio.types.PatioDataLayouts; +import org.shedlang.compiler.patio.types.PatioDataValues; +import org.shedlang.compiler.patio.types.PatioTypes; +import org.shedlang.compiler.sources.Sources; + +import java.util.List; +import java.util.Optional; + +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class PatioToProtobufTests { + // == Compilation unit == + + @Test + public void canSerializeCompilationUnit() { + var element = PatioFunctionDefinition.builder() + .withName("f") + .withReturnType(PatioTypes.VOID) + .build(); + var compilationUnit = Patio.compilationUnit(List.of(element)); + + var deserializedCompilationUnit = PatioToProtobuf.deserializeCompilationUnit( + PatioToProtobuf.serializeCompilationUnit(compilationUnit) + ); + + assertThat(deserializedCompilationUnit, equalTo(compilationUnit)); + } + + // == Compilation unit elements == + + @Test + public void canSerializeDataElementImmutableDefinition() { + var element = Patio.dataElementImmutableDefinition( + "x", + PatioVisibility.PRIVATE, + PatioDataValues.array( + PatioDataLayouts.BYTE, + List.of(PatioDataValues.byte_(0x01), PatioDataValues.byte_(0x02)) + ) + ); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + + @Test + public void canSerializeDataElementMutableDeclaration() { + var element = Patio.dataElementMutableDeclaration( + "x", + PatioDataLayouts.INT + ); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + + @Test + public void canSerializeDataElementMutableDefinition() { + var element = Patio.dataElementMutableDefinition( + "x", + PatioVisibility.PRIVATE, + PatioDataValues.int_(42) + ); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + + @Test + public void canSerializeExceptionTypeDefinition() { + var element = Patio.exceptionTypeDefinition("Error", PatioTypes.INT); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + + @Test + public void canSerializeFunctionDeclaration() { + var element = PatioFunctionDeclaration.builder() + .withName("f") + .addParam(Patio.param("param1", PatioTypes.INT)) + .addParam(Patio.param("param2", PatioTypes.DATA_PTR)) + .withReturnType(PatioTypes.VOID) + .build(); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + + @Test + public void canSerializeFunctionDefinition() { + var element = PatioFunctionDefinition.builder() + .withName("f") + .withVisibility(PatioVisibility.PRIVATE) + .addLocal(Patio.param("param1", PatioTypes.INT)) + .addLocal(Patio.param("param2", PatioTypes.DATA_PTR)) + .addLocal(Patio.val("local1", PatioTypes.FUNCTION_PTR)) + .addLocal(Patio.val("local2", PatioTypes.INT)) + .withReturnType(PatioTypes.VOID) + .addBodyInstruction(Patio.localStore(Patio.localRef("local2"), Patio.intConst(2))) + .build(); + + var deserializedElement = PatioToProtobuf.deserializeCompilationUnitElement( + PatioToProtobuf.serializeCompilationUnitElement(element, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedElement, equalTo(element)); + } + + // == Instructions == + + @Test + public void canSerializeBlock() { + var instruction = Patio.block( + List.of( + Patio.localStore(Patio.localRef("x1"), Patio.intConst(1)), + Patio.localStore(Patio.localRef("y1"), Patio.intConst(2)) + ), + List.of( + Patio.localRef("x1"), + Patio.localRef("y1") + ) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeBoolToInt() { + var instruction = Patio.boolToInt(Patio.localRef("destination"), Patio.localRef("value")); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeCallWithoutDestination() { + var instruction = Patio.call( + Optional.empty(), + "f", + List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c")) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeCallWithDestination() { + var instruction = Patio.call( + Optional.of(Patio.localRef("destination")), + "f", + List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c")) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeCallPointer() { + var instruction = Patio.callPointer( + Patio.localRef("destination"), + Patio.localRef("f"), + List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c")) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeDataPtrToInt() { + var instruction = Patio.dataPtrToInt(Patio.localRef("destination"), Patio.localRef("value")); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIf() { + var instruction = Patio.if_( + List.of( + Patio.localRef("x"), + Patio.localRef("y") + ), + Patio.localRef("condition"), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x1"), Patio.intConst(1)), + Patio.localStore(Patio.localRef("y1"), Patio.intConst(2)) + ), + List.of( + Patio.localRef("x1"), + Patio.localRef("y1") + ) + ), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x2"), Patio.intConst(11)), + Patio.localStore(Patio.localRef("y2"), Patio.intConst(12)) + ), + List.of( + Patio.localRef("x2"), + Patio.localRef("y2") + ) + ) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIntAdd() { + var instruction = Patio.intAdd( + Patio.localRef("destination"), + Patio.localRef("left"), + Patio.localRef("right") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIntEquals() { + var instruction = Patio.intEquals( + Patio.localRef("destination"), + Patio.localRef("left"), + Patio.localRef("right") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIntNotEqual() { + var instruction = Patio.intNotEqual( + Patio.localRef("destination"), + Patio.localRef("left"), + Patio.localRef("right") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIntSub() { + var instruction = Patio.intSub( + Patio.localRef("destination"), + Patio.localRef("left"), + Patio.localRef("right") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIntToBool() { + var instruction = Patio.intToBool(Patio.localRef("destination"), Patio.localRef("value")); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeIntToDataPtr() { + var instruction = Patio.intToDataPtr( + Patio.localRef("destination"), + Patio.localRef("value") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeLocalStore() { + var instruction = Patio.localStore( + Patio.localRef("destination"), + Patio.localRef("x") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeLoop() { + var instruction = Patio.loop( + List.of( + Patio.localStore(Patio.localRef("x"), Patio.intConst(1)) + ) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeLoopBreak() { + var instruction = Patio.loopBreak(); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeLoopContinue() { + var instruction = Patio.loopContinue(); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeMemoryLoad() { + var instruction = Patio.memoryLoad( + Patio.localRef("destination"), + Patio.localRef("pointer"), + PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE)), + List.of(1) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeMemoryStore() { + var instruction = Patio.memoryStore( + Patio.localRef("pointer"), + PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE)), + List.of(1), + Patio.localRef("value") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeResume() { + var instruction = Patio.resume(Patio.localRef("value")); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeReturn() { + var instruction = Patio.return_(Patio.localRef("value")); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeReturnVoid() { + var instruction = Patio.returnVoid(); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeSwitch() { + var instruction = Patio.switch_( + List.of( + Patio.localRef("x"), + Patio.localRef("y") + ), + Patio.localRef("value"), + List.of( + Patio.switchCase( + 1, + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x1"), Patio.intConst(11)), + Patio.localStore(Patio.localRef("y1"), Patio.intConst(12)) + ), + List.of( + Patio.localRef("x1"), + Patio.localRef("y1") + ) + ) + ), + Patio.switchCase( + 2, + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x2"), Patio.intConst(21)), + Patio.localStore(Patio.localRef("y2"), Patio.intConst(22)) + ), + List.of( + Patio.localRef("x2"), + Patio.localRef("y2") + ) + ) + ) + ), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x3"), Patio.intConst(31)), + Patio.localStore(Patio.localRef("y3"), Patio.intConst(32)) + ), + List.of( + Patio.localRef("x3"), + Patio.localRef("y3") + ) + ) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeThrow() { + var instruction = Patio.throw_("Error", Patio.localRef("value")); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeTry() { + var instruction = Patio.try_( + List.of(Patio.localRef("x"), Patio.localRef("y")), + List.of( + Patio.catchClause( + "Error1", + Patio.localRef("a1"), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("xCatch1"), Patio.intConst(11)), + Patio.localStore(Patio.localRef("yCatch1"), Patio.intConst(12)) + ), + List.of(Patio.localRef("xCatch1"), Patio.localRef("yCatch1")) + ) + ), + Patio.catchClause( + "Error2", + Patio.localRef("a2"), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("xCatch2"), Patio.intConst(21)), + Patio.localStore(Patio.localRef("yCatch2"), Patio.intConst(22)) + ), + List.of(Patio.localRef("xCatch2"), Patio.localRef("yCatch2")) + ) + ) + ), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("xTry"), Patio.intConst(31)), + Patio.localStore(Patio.localRef("yTry"), Patio.intConst(32)) + ), + List.of(Patio.localRef("xTry"), Patio.localRef("yTry")) + ) + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeUnreachable() { + var instruction = Patio.unreachable(); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeVarLoad() { + var instruction = Patio.varLoad( + Patio.localRef("destination"), + Patio.localRef("v") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + @Test + public void canSerializeVarStore() { + var instruction = Patio.varStore( + Patio.localRef("destination"), + Patio.localRef("value") + ); + + var deserializedInstruction = PatioToProtobuf.deserializeInstruction( + PatioToProtobuf.serializeInstruction(instruction, Sources.NULL), + Sources.NULL + ); + + assertThat(deserializedInstruction, equalTo(instruction)); + } + + // == Operands == + + @Test + public void canSerializeBoolConst() { + var operand = Patio.boolConst(true); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + @Test + public void canSerializeByteConst() { + var operand = Patio.byteConst((byte) 42); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + @Test + public void canSerializeDataPtrConst() { + var operand = Patio.dataPtrConst("x"); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + @Test + public void canSerializeFunctionPtrConst() { + var operand = Patio.functionPtrConst("x"); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + @Test + public void canSerializeIntConst() { + var operand = Patio.intConst(42); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + @Test + public void canSerializeInt32Const() { + var operand = Patio.int32Const(42); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + @Test + public void canSerializeLocalRef() { + var operand = Patio.localRef("x"); + + var deserializedOperand = PatioToProtobuf.deserializeOperand( + PatioToProtobuf.serializeOperand(operand) + ); + + assertThat(deserializedOperand, equalTo(operand)); + } + + // == Data ptr values == + + @Test + public void canSerializeDataPtrValueDataElement() { + var dataPtrValue = Patio.dataPtrValueDataElement("string"); + + var deserializedDataPtrValue = PatioToProtobuf.deserializeDataPtrValue( + PatioToProtobuf.serializeDataPtrValue(dataPtrValue) + ); + + assertThat(deserializedDataPtrValue, equalTo(dataPtrValue)); + } + + @Test + public void canSerializeDataPtrValueNull() { + var dataPtrValue = Patio.dataPtrValueNull(); + + var deserializedDataPtrValue = PatioToProtobuf.deserializeDataPtrValue( + PatioToProtobuf.serializeDataPtrValue(dataPtrValue) + ); + + assertThat(deserializedDataPtrValue, equalTo(dataPtrValue)); + } + + // == Int values == + + @Test + public void canSerializeIntValueByteOffsetOf() { + var intValue = Patio.intValueByteOffsetOf( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT, + PatioDataLayouts.BYTE + )), + List.of(1, 2) + ); + + var deserializedIntValue = PatioToProtobuf.deserializeIntValue( + PatioToProtobuf.serializeIntValue(intValue) + ); + + assertThat(deserializedIntValue, equalTo(intValue)); + } + + @Test + public void canSerializeIntValueByteSizeOf() { + var intValue = Patio.intValueByteSizeOf( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT, + PatioDataLayouts.BYTE + )) + ); + + var deserializedIntValue = PatioToProtobuf.deserializeIntValue( + PatioToProtobuf.serializeIntValue(intValue) + ); + + assertThat(deserializedIntValue, equalTo(intValue)); + } + + @Test + public void canSerializeIntValueLiteral() { + var intValue = Patio.intValueLiteral(42); + + var deserializedIntValue = PatioToProtobuf.deserializeIntValue( + PatioToProtobuf.serializeIntValue(intValue) + ); + + assertThat(deserializedIntValue, equalTo(intValue)); + } + + // == Types == + + @Test + public void canSerializeBoolType() { + var type = PatioTypes.BOOL; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + @Test + public void canSerializeByteType() { + var type = PatioTypes.BYTE; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + @Test + public void canSerializeDataPtrType() { + var type = PatioTypes.DATA_PTR; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + @Test + public void canSerializeFunctionPtrType() { + var type = PatioTypes.FUNCTION_PTR; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + @Test + public void canSerializeIntType() { + var type = PatioTypes.INT; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + @Test + public void canSerializeInt32Type() { + var type = PatioTypes.INT32; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + @Test + public void canSerializeVoidType() { + var type = PatioTypes.VOID; + + var deserializedType = PatioToProtobuf.deserializeType( + PatioToProtobuf.serializeType(type) + ); + + assertThat(deserializedType, equalTo(type)); + } + + // == Data layouts == + + @Test + public void canSerializeDataLayoutArray() { + var layout = PatioDataLayouts.array(PatioDataLayouts.INT, 4); + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutBool() { + var layout = PatioDataLayouts.BOOL; + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutByte() { + var layout = PatioDataLayouts.BYTE; + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutDataPtr() { + var layout = PatioDataLayouts.DATA_PTR; + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutFunctionPtr() { + var layout = PatioDataLayouts.FUNCTION_PTR; + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutInt() { + var layout = PatioDataLayouts.INT; + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutInt32() { + var layout = PatioDataLayouts.INT32; + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + @Test + public void canSerializeDataLayoutStruct() { + var layout = PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE)); + + var deserializedLayout = PatioToProtobuf.deserializeDataLayout( + PatioToProtobuf.serializeDataLayout(layout) + ); + + assertThat(deserializedLayout, equalTo(layout)); + } + + // == Data values == + + @Test + public void canSerializeDataValueArray() { + var value = PatioDataValues.array( + PatioDataLayouts.BYTE, + List.of( + PatioDataValues.byte_(0x01), + PatioDataValues.byte_(0x02), + PatioDataValues.byte_(0x03) + ) + ); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } + + @Test + public void canSerializeDataValueBool() { + var value = PatioDataValues.bool(true); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } + + @Test + public void canSerializeDataValueByte() { + var value = PatioDataValues.byte_(0xc8); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } + + @Test + public void canSerializeDataValueDataPtr() { + var value = PatioDataValues.dataPtrNull(); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } + + @Test + public void canSerializeDataValueInt() { + var value = PatioDataValues.int_(42); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } + + @Test + public void canSerializeDataValueInt32() { + var value = PatioDataValues.int32(42); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } + + @Test + public void canSerializeDataValueStruct() { + var value = PatioDataValues.struct(List.of( + PatioDataValues.byte_(0x01), + PatioDataValues.byte_(0x02), + PatioDataValues.byte_(0x03) + )); + + var deserializedValue = PatioToProtobuf.deserializeDataValue( + PatioToProtobuf.serializeDataValue(value) + ); + + assertThat(deserializedValue, equalTo(value)); + } +} -- cgit v1.2.3