forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackerTest.Pack.tt
More file actions
387 lines (357 loc) · 14.7 KB
/
PackerTest.Pack.tt
File metadata and controls
387 lines (357 loc) · 14.7 KB
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
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly Name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Runtime.InteropServices" #>
<#
var __numerics =
new Tuple<string, Type, string, string>[]
{
// Label, Target type, Test value, Expected bytes
Tuple.Create( "DoubleMinValue", typeof( double ), "Double.MinValue", "new byte[] { 0xCB }.Concat( BitConverter.GetBytes( Double.MinValue ).Reverse() ).ToArray()" ),
Tuple.Create( "SingleMinValue", typeof( float ), "Single.MinValue", "new byte[] { 0xCA }.Concat( BitConverter.GetBytes( Single.MinValue ).Reverse() ).ToArray()" ),
Tuple.Create( "Int64MinValue", typeof( long ), "Int64.MinValue", "new byte[] { 0xD3 }.Concat( BitConverter.GetBytes( Int64.MinValue ).Reverse() ).ToArray()" ),
Tuple.Create( "Int32MinValue", typeof( int ), "Int32.MinValue", "new byte[] { 0xD2 }.Concat( BitConverter.GetBytes( Int32.MinValue ).Reverse() ).ToArray()" ),
Tuple.Create( "Int16MinValue", typeof( short ), "Int16.MinValue", "new byte[] { 0xD1 }.Concat( BitConverter.GetBytes( Int16.MinValue ).Reverse() ).ToArray()" ),
Tuple.Create( "SByteMinValue", typeof( sbyte ), "SByte.MinValue", "new byte[] { 0xD0, 0x80 }" ),
Tuple.Create( "DoubleMaxValue", typeof( double ),"Double.MaxValue" , "new byte[] { 0xCB }.Concat( BitConverter.GetBytes( Double.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "SingleMaxValue", typeof( float ), "Single.MaxValue" , "new byte[] { 0xCA }.Concat( BitConverter.GetBytes( Single.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "UInt64MaxValue", typeof( ulong ), "UInt64.MaxValue" , "new byte[] { 0xCF }.Concat( BitConverter.GetBytes( UInt64.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "Int64MaxValue", typeof( long ), "Int64.MaxValue" , "new byte[] { 0xD3 }.Concat( BitConverter.GetBytes( Int64.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "UInt32MaxValue", typeof( uint ), "UInt32.MaxValue" , "new byte[] { 0xCE }.Concat( BitConverter.GetBytes( UInt32.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "Int32MaxValue", typeof( int ), "Int32.MaxValue" , "new byte[] { 0xD2 }.Concat( BitConverter.GetBytes( Int32.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "UInt16MaxValue", typeof( ushort ), "UInt16.MaxValue" , "new byte[] { 0xCD }.Concat( BitConverter.GetBytes( UInt16.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "Int16MaxValue", typeof( short ), "Int16.MaxValue" , "new byte[] { 0xD1 }.Concat( BitConverter.GetBytes( Int16.MaxValue ).Reverse() ).ToArray()" ),
Tuple.Create( "ByteMaxValue", typeof( byte ), "Byte.MaxValue" , "new byte[] { 0xCC, 0xFF }" ),
Tuple.Create( "SByteMaxValue", typeof( sbyte ), "SByte.MaxValue" , "new byte[] { 0x7F }" ),
Tuple.Create( "NegativeFixNumMinValueMinusOne", typeof( sbyte ), "( ( sbyte )-33 )" , "new byte[] { 0xD0, unchecked( ( byte )( sbyte )-33 ) }" ),
Tuple.Create( "NegativeFixNumMinValue", typeof( int ), "( -32 )" , "new byte[] { 0xE0 }" ),
Tuple.Create( "MinusOne", typeof( int ), "( -1 )" , "new byte[] { 0xFF }" ),
Tuple.Create( "Zero", typeof( int ), "0", "new byte[] { 0x00 }" ),
Tuple.Create( "PlusOne", typeof( int ), "( 1 )" , "new byte[] { 0x01 }" ),
Tuple.Create( "PositiveFixNumMaxValue", typeof( int ), "( 127 )" , "new byte[] { 0x7F }" ),
Tuple.Create( "PositiveFixNumMaxValuePlusOne", typeof( byte ), "( ( byte )128 )" , "new byte[] { 0xCC, 0x80 }" ),
Tuple.Create( "True", typeof( bool ), "true", "new byte[] { 0xC3 }" ),
Tuple.Create( "False", typeof( bool ), "false", "new byte[] { 0xC2 }" ),
Tuple.Create( "Nil", typeof( object ), "default( object )" , "new byte[] { 0xC0 }" ),
Tuple.Create( "SingleEpsilon", typeof( float ), "Single.Epsilon" , "new byte[] { 0xCA }.Concat( BitConverter.GetBytes( Single.Epsilon ).Reverse() ).ToArray()" ),
Tuple.Create( "DoubleEpsilon", typeof( double ), "Double.Epsilon" , "new byte[] { 0xCB }.Concat( BitConverter.GetBytes( Double.Epsilon ).Reverse() ).ToArray()" ),
Tuple.Create( "SinglePositiveInfinity", typeof( float ), "Single.PositiveInfinity" , "new byte[] { 0xCA }.Concat( BitConverter.GetBytes( Single.PositiveInfinity ).Reverse() ).ToArray()" ),
Tuple.Create( "DoublePositiveInfinity", typeof( double ), "Double.PositiveInfinity" , "new byte[] { 0xCB }.Concat( BitConverter.GetBytes( Double.PositiveInfinity ).Reverse() ).ToArray()" ),
Tuple.Create( "SingleNegativeInfinity", typeof( float ), "Single.NegativeInfinity" , "new byte[] { 0xCA }.Concat( BitConverter.GetBytes( Single.NegativeInfinity ).Reverse() ).ToArray()" ),
Tuple.Create( "DoubleNegativeInfinity", typeof( double ), "Double.NegativeInfinity" , "new byte[] { 0xCB }.Concat( BitConverter.GetBytes( Double.NegativeInfinity ).Reverse() ).ToArray()" ),
};
#>
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2017 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
#if !MSTEST
using NUnit.Framework;
#else
using TestFixtureAttribute = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using TestAttribute = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
using TimeoutAttribute = NUnit.Framework.TimeoutAttribute;
using Assert = NUnit.Framework.Assert;
using Is = NUnit.Framework.Is;
#endif
namespace MsgPack
{
// This file was generated from PackerTest.Pack.tt and StreamingUnapkcerBase.ttinclude T4Template.
// Do not modify this file. Edit PackerTest.Pack.tt and StreamingUnapkcerBase.ttinclude instead.
partial class PackerTest
{
<#
foreach( var isAsync in new [] { false, true } )
{
if ( isAsync )
{
#>
#if FEATURE_TAP
<#
}
foreach( var __numeric in __numerics )
{
#>
[Test]
public <#= AsyncTest( "Pack", isAsync )#>_<#= __numeric.Item1 #>_AsValue()
{
using( var buffer = new MemoryStream() )
using( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.Pack", isAsync ) #>( <#= __numeric.Item3 #> );
Assert.AreEqual(
<#= __numeric.Item4 #>,
this.GetResult( packer )
);
}
}
<#
} // foreach( var __numeric
var __defined = new HashSet<string>();
foreach( var __numeric in __numerics )
{
if( __numeric.Item1 == "Nil" || !__defined.Add( __numeric.Item2.Name ) )
{
continue;
}
#>
[Test]
public <#= AsyncTest( "Pack", isAsync )#>_Nullable<#= __numeric.Item2.Name #>_NotNull_AsValue()
{
using( var buffer = new MemoryStream() )
using( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.Pack", isAsync ) #>( <#= __numeric.Item3 #> );
Assert.AreEqual(
<#= __numeric.Item4 #>,
this.GetResult( packer )
);
}
}
[Test]
public <#= AsyncTest( "Pack", isAsync )#>_Nullable<#= __numeric.Item2.Name #>_Null_AsNil()
{
using( var buffer = new MemoryStream() )
using( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.Pack", isAsync ) #>( default( <#= __numeric.Item2 #>? ) );
Assert.AreEqual(
new byte[]{ 0xC0 },
this.GetResult( packer )
);
}
}
<#
} // foreach( var __numeric
// PackArrayHeader
foreach( var testCase in
new []
{
new { Type = "Int32", ValueLabel = "Empty", Value = "0", Bytes = "0x90" },
new { Type = "Int32", ValueLabel = "15", Value = "15", Bytes = "0x9F" },
new { Type = "Int32", ValueLabel = "16", Value = "0x100", Bytes = "0xDC, 0x01, 0x00" },
new { Type = "Int32", ValueLabel = "0xFFFF", Value = "0xFFFF", Bytes = "0xDC, 0xFF, 0xFF" },
new { Type = "Int32", ValueLabel = "0x10000", Value = "0x10000", Bytes = "0xDD, 0x00, 0x01, 0x00, 0x00" },
new { Type = "IList", ValueLabel = "Null", Value = "default( IList<MessagePackObject> )", Bytes = "0xC0" },
new { Type = "IList", ValueLabel = "NotNull", Value = "new MessagePackObject[] { 1, 2, 3 }", Bytes = "0x93" },
}
)
{
#>
[Test]
public <#= AsyncTest( "PackArrayHeader", isAsync ) #><#= testCase.Type #>_<#= testCase.ValueLabel #>()
{
using ( var buffer = new MemoryStream() )
using ( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.PackArrayHeader", isAsync ) #>( <#= testCase.Value #> );
Assert.AreEqual(
new byte[] { <#= testCase.Bytes #> },
this.GetResult( packer )
);
}
}
<#
} // foreach( var testCase
#>
[Test]
public <#= AsyncLambdaTest( "PackArrayHeader", isAsync ) #>Int32_MinusOne()
{
using ( var buffer = new MemoryStream() )
using ( var packer = CreatePacker( buffer ) )
{
<#= Throws( isAsync ) #><ArgumentOutOfRangeException>( <#= AsyncLambda( "packer.PackArrayHeader", isAsync ) #>( -1 ) );
}
}
<#
// PackMapHeader
foreach( var testCase in
new []
{
new { Type = "Int32", ValueLabel = "Empty", Value = "0", Bytes = "0x80" },
new { Type = "Int32", ValueLabel = "15", Value = "15", Bytes = "0x8F" },
new { Type = "Int32", ValueLabel = "16", Value = "0x100", Bytes = "0xDE, 0x01, 0x00" },
new { Type = "Int32", ValueLabel = "0xFFFF", Value = "0xFFFF", Bytes = "0xDE, 0xFF, 0xFF" },
new { Type = "Int32", ValueLabel = "0x10000", Value = "0x10000", Bytes = "0xDF, 0x00, 0x01, 0x00, 0x00" },
new { Type = "IDictionary", ValueLabel = "Null", Value = "default( IDictionary<MessagePackObject, MessagePackObject> )", Bytes = "0xC0" },
new { Type = "IDictionary", ValueLabel = "NotNull", Value = "new MessagePackObjectDictionary() { { 1, 1 }, { 2, 2 }, { 3, 3 } }", Bytes = "0x83" },
}
)
{
#>
[Test]
public <#= AsyncTest( "PackMapHeader", isAsync ) #><#= testCase.Type #>_<#= testCase.ValueLabel #>()
{
using ( var buffer = new MemoryStream() )
using ( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.PackMapHeader", isAsync ) #>( <#= testCase.Value #> );
Assert.AreEqual(
new byte[] { <#= testCase.Bytes #> },
this.GetResult( packer )
);
}
}
<#
} // foreach( var testCase
#>
[Test]
public <#= AsyncLambdaTest( "PackMapHeader", isAsync ) #>Int32_MinusOne()
{
using ( var buffer = new MemoryStream() )
using ( var packer = CreatePacker( buffer ) )
{
<#= Throws( isAsync ) #><ArgumentOutOfRangeException>( <#= AsyncLambda( "packer.PackMapHeader", isAsync ) #>( -1 ) );
}
}
<#
// PackCollection
foreach( var testCase in
new []
{
new { Type = "Array", ValueLabel = "Null", Value = "default( MessagePackObject[] )", Bytes = "0xC0" },
new { Type = "Array", ValueLabel = "NotNull", Value = "new int[] { 1, 2, 3 }", Bytes = "0x93, 1, 2, 3" },
new { Type = "Enumerable", ValueLabel = "Null", Value = "default( IEnumerable<MessagePackObject> )", Bytes = "0xC0" },
new { Type = "Enumerable", ValueLabel = "NotNull", Value = "Enumerable.Range( 1, 3 )", Bytes = "0x93, 1, 2, 3" },
}
)
{
#>
[Test]
public <#= AsyncTest( "PackCollection", isAsync ) #><#= testCase.Type #>_<#= testCase.ValueLabel #>()
{
using ( var buffer = new MemoryStream() )
using ( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.PackCollection", isAsync ) #>( <#= testCase.Value #> );
Assert.AreEqual(
new byte[] { <#= testCase.Bytes #> },
this.GetResult( packer )
);
}
}
<#
} // foreach( var testCase
// Numeric boundaries
foreach( var testCase in
new []
{
new { Type = "SByte", ValueLabel = "0x7F", Value = "0x7F", Bytes = "0x7F" },
new { Type = "Int16", ValueLabel = "0x7F", Value = "0x7F", Bytes = "0x7F" },
new { Type = "Int32", ValueLabel = "0x7F", Value = "0x7F", Bytes = "0x7F" },
new { Type = "Int64", ValueLabel = "0x7F", Value = "0x7F", Bytes = "0x7F" },
new { Type = "Int32", ValueLabel = "Minus128", Value = "-128", Bytes = "0xD0, 0x80" },
new { Type = "UInt32", ValueLabel = "0x80", Value = "0x80", Bytes = "0xCC, 0x80" },
new { Type = "UInt32", ValueLabel = "0x100_AsUInt16", Value = "0x100", Bytes = "0xCD, 0x1, 0x0" },
new { Type = "Int64", ValueLabel = "0x1", Value = "0x1", Bytes = "0x1" },
new { Type = "Int64", ValueLabel = "Minus128", Value = "-128", Bytes = "0xD0, 0x80" },
new { Type = "Int64", ValueLabel = "0x100_AsInt16", Value = "0x100", Bytes = "0xD1, 0x1, 0x0" },
new { Type = "Int64", ValueLabel = "0x10000_AsInt32", Value = "0x10000", Bytes = "0xD2, 0x0, 0x1, 0x0, 0x0" },
new { Type = "UInt64", ValueLabel = "0x80_AsInt8", Value = "0x80", Bytes = "0xCC, 0x80" },
new { Type = "UInt64", ValueLabel = "0x100_AsInt16", Value = "0x100", Bytes = "0xCD, 0x1, 0x0" },
new { Type = "UInt64", ValueLabel = "0x10000_AsInt32", Value = "0x10000", Bytes = "0xCE, 0x0, 0x1, 0x0, 0x0" },
}
)
{
#>
[Test]
public <#= AsyncTest( "Pack", isAsync ) #><#= testCase.Type #>_<#= testCase.ValueLabel #>()
{
<#= testCase.Type #> value = <#= testCase.Value #>;
using ( var buffer = new MemoryStream() )
using ( var packer = CreatePacker( buffer ) )
{
<#= Await( "packer.Pack", isAsync ) #>( value );
Assert.AreEqual(
new byte[] { <#= testCase.Bytes #> },
this.GetResult( packer )
);
}
}
<#
} // foreach( var testCase
if ( isAsync )
{
#>
#endif // FEATURE_TAP
<#
}
} // foreach( var isAsync
#>
}
}
<#+
private static string AsyncTest( string method, bool isAsync )
{
return
String.Format(
CultureInfo.InvariantCulture,
"{0} Test{1}{2}",
isAsync ? "async Task" : "void",
method,
isAsync ? "Async" : String.Empty
);
}
private static string Await( string expression, bool isAsync )
{
return
String.Format(
CultureInfo.InvariantCulture,
"{0}{1}{2}",
isAsync ? "await " : String.Empty,
expression,
isAsync ? "Async" : String.Empty
);
}
private static string AsyncLambdaTest( string method, bool isAsync )
{
return
String.Format(
CultureInfo.InvariantCulture,
"void Test{0}{1}",
method,
isAsync ? "Async" : String.Empty
);
}
private static string Throws( bool isAsync )
{
return isAsync ? "AssertEx.ThrowsAsync" : "Assert.Throws";
}
private static string AsyncLambda( string expressionBody, bool isAsync )
{
return
String.Format(
CultureInfo.InvariantCulture,
"{0}() => {1}{2}{3}",
isAsync ? "async " : String.Empty,
isAsync ? "await " : String.Empty,
expressionBody,
isAsync ? "Async" : String.Empty
);
}
#>