1
1
using System ;
2
+ using System . Buffers ;
2
3
using System . Collections . Generic ;
3
4
using System . Dynamic ;
4
5
using System . IO ;
5
6
using System . Linq ;
7
+ using System . Runtime . InteropServices ;
6
8
using System . Text ;
7
9
using System . Threading . Tasks ;
8
10
9
11
namespace MemoryPack . Tests ;
10
12
11
- public class DeserializeTest
13
+ public partial class DeserializeTest
12
14
{
13
15
[ Fact ]
14
16
public async Task StreamTest ( )
@@ -30,6 +32,106 @@ public async Task StreamTest()
30
32
result . Should ( ) . Equal ( expected ) ;
31
33
}
32
34
35
+ [ Fact ]
36
+ public void GenericValueStructTest ( )
37
+ {
38
+ GenericStruct < int > value = new ( ) { Id = 75 , Value = 23 } ;
39
+
40
+ RunMultiSegmentTest ( value ) ;
41
+ }
42
+
43
+ [ Fact ]
44
+ public void LargeGenericValueStructTest ( )
45
+ {
46
+ GenericStruct < PrePaddedInt > value = new ( ) { Id = 75 , Value = new PrePaddedInt ( ) { Value = 23 } } ;
47
+
48
+ RunMultiSegmentTest ( value ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void GenericReferenceStructTest ( )
53
+ {
54
+ GenericStruct < string > value = new GenericStruct < string > ( ) { Id = 75 , Value = "Hello World!" } ;
55
+
56
+ RunMultiSegmentTest ( value ) ;
57
+ }
58
+
59
+ [ Fact ]
60
+ public void LargeGenericReferenceStructTest ( )
61
+ {
62
+ GenericStruct < PrePaddedString > value = new ( ) { Id = 75 , Value = new PrePaddedString ( ) { Value = "Hello World!" } } ;
63
+
64
+ RunMultiSegmentTest ( value ) ;
65
+ }
66
+
67
+ private void RunMultiSegmentTest < T > ( T value )
68
+ {
69
+ byte [ ] bytes = MemoryPackSerializer . Serialize ( value ) ;
70
+
71
+ byte [ ] firstHalf = new byte [ bytes . Length / 2 ] ;
72
+ Array . Copy ( bytes , 0 , firstHalf , 0 , firstHalf . Length ) ;
73
+
74
+ int secondHalfLength = bytes . Length / 2 ;
75
+ if ( bytes . Length % 2 != 0 )
76
+ {
77
+ secondHalfLength ++ ;
78
+ }
79
+
80
+ byte [ ] secondHalf = new byte [ secondHalfLength ] ;
81
+
82
+ Array . Copy ( bytes , firstHalf . Length , secondHalf , 0 , secondHalfLength ) ;
83
+
84
+ ReadOnlySequence < byte > sequence = ReadOnlySequenceBuilder . Create ( firstHalf , secondHalf ) ;
85
+
86
+ T ? result = MemoryPackSerializer . Deserialize < T > ( sequence ) ;
87
+ result . Should ( ) . Be ( value ) ;
88
+ }
89
+
90
+ [ MemoryPackable ]
91
+ public partial struct GenericStruct < T >
92
+ {
93
+ public int Id ;
94
+ public T Value ;
95
+
96
+ public override string ToString ( )
97
+ {
98
+ return $ "{ Id } , { Value } ";
99
+ }
100
+ }
101
+
102
+ [ StructLayout ( LayoutKind . Explicit , Size = 516 ) ]
103
+ struct PrePaddedInt
104
+ {
105
+ [ FieldOffset ( 512 ) ]
106
+ public int Value ;
107
+ }
108
+
109
+ [ MemoryPackable ]
110
+ private partial class PrePaddedString : IEquatable < PrePaddedString >
111
+ {
112
+ private PrePaddedInt _padding ;
113
+ public string Value { get ; set ; } = "" ;
114
+
115
+ public bool Equals ( PrePaddedString ? other )
116
+ {
117
+ if ( other is null )
118
+ return false ;
119
+
120
+ return Value . Equals ( other . Value ) ;
121
+ }
122
+
123
+ public override bool Equals ( object ? obj )
124
+ {
125
+ if ( obj is PrePaddedString other )
126
+ return Equals ( other ) ;
127
+ return false ;
128
+ }
129
+
130
+ public override int GetHashCode ( )
131
+ {
132
+ return Value . GetHashCode ( ) ;
133
+ }
134
+ }
33
135
34
136
class RandomStream : Stream
35
137
{
0 commit comments