1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
|
package org.shedlang.compiler.patio.serialization;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.patio.*;
import org.shedlang.compiler.patio.instructions.Patio;
import org.shedlang.compiler.patio.instructions.PatioInstruction;
import org.shedlang.compiler.patio.operands.PatioOperand;
import org.shedlang.compiler.patio.types.*;
import org.shedlang.compiler.patio.values.PatioDataPtrValue;
import org.shedlang.compiler.patio.values.PatioIntValue;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
public abstract class PatioSerializationTests {
// == Compilation unit ==
protected abstract PatioCompilationUnit deserializeCompilationUnit(byte[] buffer) throws IOException;
protected abstract byte[] serializeCompilationUnit(PatioCompilationUnit value) throws IOException;
@Test
public void canSerializeCompilationUnit() throws IOException {
var element = PatioFunctionDefinition.builder()
.withName("f")
.build();
var compilationUnit = Patio.compilationUnit(List.of(element));
var deserializedCompilationUnit = deserializeCompilationUnit(
serializeCompilationUnit(compilationUnit)
);
assertThat(deserializedCompilationUnit, equalTo(compilationUnit));
}
// == Compilation unit elements ==
protected abstract PatioCompilationUnitElement deserializeCompilationUnitElement(byte[] buffer) throws IOException;
protected abstract byte[] serializeCompilationUnitElement(PatioCompilationUnitElement value) throws IOException;
@Test
public void canSerializeDataElementImmutableDefinition() throws IOException {
var element = Patio.dataElementImmutableDefinition(
"x",
PatioVisibility.PRIVATE,
PatioDataValues.array(
PatioDataLayouts.BYTE,
List.of(PatioDataValues.byte_(0x01), PatioDataValues.byte_(0x02))
)
);
var deserializedElement = deserializeCompilationUnitElement(
serializeCompilationUnitElement(element)
);
assertThat(deserializedElement, equalTo(element));
}
@Test
public void canSerializeDataElementMutableDeclaration() throws IOException {
var element = Patio.dataElementMutableDeclaration(
"x",
PatioDataLayouts.INT
);
var deserializedElement = deserializeCompilationUnitElement(
serializeCompilationUnitElement(element)
);
assertThat(deserializedElement, equalTo(element));
}
@Test
public void canSerializeDataElementMutableDefinition() throws IOException {
var element = Patio.dataElementMutableDefinition(
"x",
PatioVisibility.PRIVATE,
PatioDataValues.int_(42)
);
var deserializedElement = deserializeCompilationUnitElement(
serializeCompilationUnitElement(element)
);
assertThat(deserializedElement, equalTo(element));
}
@Test
public void canSerializeExceptionTypeDefinition() throws IOException {
var element = Patio.exceptionTypeDefinition("Error", PatioTypes.INT);
var deserializedElement = deserializeCompilationUnitElement(
serializeCompilationUnitElement(element)
);
assertThat(deserializedElement, equalTo(element));
}
@Test
public void canSerializeFunctionDeclaration() throws IOException {
var element = PatioFunctionDeclaration.builder()
.withName("f")
.addParam(Patio.param("param1", PatioTypes.INT))
.addParam(Patio.param("param2", PatioTypes.DATA_PTR))
.addReturnType(PatioTypes.INT32)
.build();
var deserializedElement = deserializeCompilationUnitElement(
serializeCompilationUnitElement(element)
);
assertThat(deserializedElement, equalTo(element));
}
@Test
public void canSerializeFunctionDefinition() throws IOException {
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))
.addReturnType(PatioTypes.INT32)
.addBodyInstruction(Patio.localStore(Patio.localRef("local2"), Patio.intConst(2)))
.build();
var deserializedElement = deserializeCompilationUnitElement(
serializeCompilationUnitElement(element)
);
assertThat(deserializedElement, equalTo(element));
}
// == Instructions ==
protected abstract PatioInstruction deserializeInstruction(byte[] buffer) throws IOException;
protected abstract byte[] serializeInstruction(PatioInstruction value) throws IOException;
@Test
public void canSerializeBlock() throws IOException {
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 = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeBoolToInt() throws IOException {
var instruction = Patio.boolToInt(Patio.localRef("destination"), Patio.localRef("value"));
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeCallWithoutDestination() throws IOException {
var instruction = Patio.call(
Optional.empty(),
"f",
List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c"))
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeCallWithDestination() throws IOException {
var instruction = Patio.call(
Optional.of(Patio.localRef("destination")),
"f",
List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c"))
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeCallPointer() throws IOException {
var instruction = Patio.callPointer(
Patio.localRef("destination"),
Patio.localRef("f"),
List.of(Patio.localRef("a"), Patio.localRef("b"), Patio.localRef("c"))
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeDataPtrToInt() throws IOException {
var instruction = Patio.dataPtrToInt(Patio.localRef("destination"), Patio.localRef("value"));
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIf() throws IOException {
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 = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIntAdd() throws IOException {
var instruction = Patio.intAdd(
Patio.localRef("destination"),
Patio.localRef("left"),
Patio.localRef("right")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIntEquals() throws IOException {
var instruction = Patio.intEquals(
Patio.localRef("destination"),
Patio.localRef("left"),
Patio.localRef("right")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIntNotEqual() throws IOException {
var instruction = Patio.intNotEqual(
Patio.localRef("destination"),
Patio.localRef("left"),
Patio.localRef("right")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIntSub() throws IOException {
var instruction = Patio.intSub(
Patio.localRef("destination"),
Patio.localRef("left"),
Patio.localRef("right")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIntToBool() throws IOException {
var instruction = Patio.intToBool(Patio.localRef("destination"), Patio.localRef("value"));
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeIntToDataPtr() throws IOException {
var instruction = Patio.intToDataPtr(
Patio.localRef("destination"),
Patio.localRef("value")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeLocalStore() throws IOException {
var instruction = Patio.localStore(
Patio.localRef("destination"),
Patio.localRef("x")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeLoop() throws IOException {
var instruction = Patio.loop(
List.of(
Patio.localStore(Patio.localRef("x"), Patio.intConst(1))
)
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeLoopBreak() throws IOException {
var instruction = Patio.loopBreak();
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeLoopContinue() throws IOException {
var instruction = Patio.loopContinue();
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeMemoryLoad() throws IOException {
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 = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeMemoryStore() throws IOException {
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 = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeReturn() throws IOException {
var instruction = Patio.return_(Patio.localRef("value"));
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeReturnVoid() throws IOException {
var instruction = Patio.returnVoid();
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeSwitch() throws IOException {
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 = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeThrow() throws IOException {
var instruction = Patio.throw_("Error", Patio.localRef("value"));
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeTry() throws IOException {
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 = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeUnreachable() throws IOException {
var instruction = Patio.unreachable();
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeVarLoad() throws IOException {
var instruction = Patio.varLoad(
Patio.localRef("destination"),
Patio.localRef("v")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
@Test
public void canSerializeVarStore() throws IOException {
var instruction = Patio.varStore(
Patio.localRef("destination"),
Patio.localRef("value")
);
var deserializedInstruction = deserializeInstruction(
serializeInstruction(instruction)
);
assertThat(deserializedInstruction, equalTo(instruction));
}
// == Operands ==
protected abstract PatioOperand deserializeOperand(byte[] buffer) throws IOException;
protected abstract byte[] serializeOperand(PatioOperand value) throws IOException;
@Test
public void canSerializeBoolConst() throws IOException {
var operand = Patio.boolConst(true);
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
@Test
public void canSerializeByteConst() throws IOException {
var operand = Patio.byteConst((byte) 42);
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
@Test
public void canSerializeDataPtrConst() throws IOException {
var operand = Patio.dataPtrConst("x");
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
@Test
public void canSerializeFunctionPtrConst() throws IOException {
var operand = Patio.functionPtrConst("x");
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
@Test
public void canSerializeIntConst() throws IOException {
var operand = Patio.intConst(42);
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
@Test
public void canSerializeInt32Const() throws IOException {
var operand = Patio.int32Const(42);
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
@Test
public void canSerializeLocalRef() throws IOException {
var operand = Patio.localRef("x");
var deserializedOperand = deserializeOperand(
serializeOperand(operand)
);
assertThat(deserializedOperand, equalTo(operand));
}
// == Data ptr values ==
protected abstract PatioDataPtrValue deserializeDataPtrValue(byte[] buffer) throws IOException;
protected abstract byte[] serializeDataPtrValue(PatioDataPtrValue value) throws IOException;
@Test
public void canSerializeDataPtrValueDataElement() throws IOException {
var dataPtrValue = Patio.dataPtrValueDataElement("string");
var deserializedDataPtrValue = deserializeDataPtrValue(
serializeDataPtrValue(dataPtrValue)
);
assertThat(deserializedDataPtrValue, equalTo(dataPtrValue));
}
@Test
public void canSerializeDataPtrValueNull() throws IOException {
var dataPtrValue = Patio.dataPtrValueNull();
var deserializedDataPtrValue = deserializeDataPtrValue(
serializeDataPtrValue(dataPtrValue)
);
assertThat(deserializedDataPtrValue, equalTo(dataPtrValue));
}
// == Int values ==
protected abstract PatioIntValue deserializeIntValue(byte[] buffer) throws IOException;
protected abstract byte[] serializeIntValue(PatioIntValue value) throws IOException;
@Test
public void canSerializeIntValueByteOffsetOf() throws IOException {
var intValue = Patio.intValueByteOffsetOf(
PatioDataLayouts.struct(List.of(
PatioDataLayouts.INT,
PatioDataLayouts.BYTE
)),
List.of(1, 2)
);
var deserializedIntValue = deserializeIntValue(
serializeIntValue(intValue)
);
assertThat(deserializedIntValue, equalTo(intValue));
}
@Test
public void canSerializeIntValueByteSizeOf() throws IOException {
var intValue = Patio.intValueByteSizeOf(
PatioDataLayouts.struct(List.of(
PatioDataLayouts.INT,
PatioDataLayouts.BYTE
))
);
var deserializedIntValue = deserializeIntValue(
serializeIntValue(intValue)
);
assertThat(deserializedIntValue, equalTo(intValue));
}
@Test
public void canSerializeIntValueLiteral() throws IOException {
var intValue = Patio.intValueLiteral(42);
var deserializedIntValue = deserializeIntValue(
serializeIntValue(intValue)
);
assertThat(deserializedIntValue, equalTo(intValue));
}
// == Types ==
protected abstract PatioType deserializeType(byte[] buffer) throws IOException;
protected abstract byte[] serializeType(PatioType value) throws IOException;
@Test
public void canSerializeBoolType() throws IOException {
var type = PatioTypes.BOOL;
var deserializedType = deserializeType(
serializeType(type)
);
assertThat(deserializedType, equalTo(type));
}
@Test
public void canSerializeByteType() throws IOException {
var type = PatioTypes.BYTE;
var deserializedType = deserializeType(
serializeType(type)
);
assertThat(deserializedType, equalTo(type));
}
@Test
public void canSerializeDataPtrType() throws IOException {
var type = PatioTypes.DATA_PTR;
var deserializedType = deserializeType(
serializeType(type)
);
assertThat(deserializedType, equalTo(type));
}
@Test
public void canSerializeFunctionPtrType() throws IOException {
var type = PatioTypes.FUNCTION_PTR;
var deserializedType = deserializeType(
serializeType(type)
);
assertThat(deserializedType, equalTo(type));
}
@Test
public void canSerializeIntType() throws IOException {
var type = PatioTypes.INT;
var deserializedType = deserializeType(
serializeType(type)
);
assertThat(deserializedType, equalTo(type));
}
@Test
public void canSerializeInt32Type() throws IOException {
var type = PatioTypes.INT32;
var deserializedType = deserializeType(
serializeType(type)
);
assertThat(deserializedType, equalTo(type));
}
// == Data layouts ==
protected abstract PatioDataLayout deserializeDataLayout(byte[] buffer) throws IOException;
protected abstract byte[] serializeDataLayout(PatioDataLayout value) throws IOException;
@Test
public void canSerializeDataLayoutArray() throws IOException {
var layout = PatioDataLayouts.array(PatioDataLayouts.INT, 4);
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutBool() throws IOException {
var layout = PatioDataLayouts.BOOL;
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutByte() throws IOException {
var layout = PatioDataLayouts.BYTE;
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutDataPtr() throws IOException {
var layout = PatioDataLayouts.DATA_PTR;
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutFunctionPtr() throws IOException {
var layout = PatioDataLayouts.FUNCTION_PTR;
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutInt() throws IOException {
var layout = PatioDataLayouts.INT;
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutInt32() throws IOException {
var layout = PatioDataLayouts.INT32;
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
@Test
public void canSerializeDataLayoutStruct() throws IOException {
var layout = PatioDataLayouts.struct(List.of(PatioDataLayouts.INT, PatioDataLayouts.INT, PatioDataLayouts.BYTE));
var deserializedLayout = deserializeDataLayout(
serializeDataLayout(layout)
);
assertThat(deserializedLayout, equalTo(layout));
}
// == Data values ==
protected abstract PatioDataValue deserializeDataValue(byte[] buffer) throws IOException;
protected abstract byte[] serializeDataValue(PatioDataValue value) throws IOException;
@Test
public void canSerializeDataValueArray() throws IOException {
var value = PatioDataValues.array(
PatioDataLayouts.BYTE,
List.of(
PatioDataValues.byte_(0x01),
PatioDataValues.byte_(0x02),
PatioDataValues.byte_(0x03)
)
);
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
@Test
public void canSerializeDataValueBool() throws IOException {
var value = PatioDataValues.bool(true);
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
@Test
public void canSerializeDataValueByte() throws IOException {
var value = PatioDataValues.byte_(0xc8);
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
@Test
public void canSerializeDataValueDataPtr() throws IOException {
var value = PatioDataValues.dataPtrNull();
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
@Test
public void canSerializeDataValueInt() throws IOException {
var value = PatioDataValues.int_(42);
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
@Test
public void canSerializeDataValueInt32() throws IOException {
var value = PatioDataValues.int32(42);
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
@Test
public void canSerializeDataValueStruct() throws IOException {
var value = PatioDataValues.struct(List.of(
PatioDataValues.byte_(0x01),
PatioDataValues.byte_(0x02),
PatioDataValues.byte_(0x03)
));
var deserializedValue = deserializeDataValue(
serializeDataValue(value)
);
assertThat(deserializedValue, equalTo(value));
}
}
|