forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePackConvertTest.cs
More file actions
304 lines (262 loc) · 9.13 KB
/
MessagePackConvertTest.cs
File metadata and controls
304 lines (262 loc) · 9.13 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
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2012 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.Text;
#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
{
/// <summary>
///Tests the Message Pack Convert
/// </summary>
[TestFixture()]
public class MessagePackConvertTest
{
// 1byte, 3bytes, 2bytes chars.
private const string _testValue = "A\u3000\u00C0";
private static readonly DateTime _utcEpoc = new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc );
[Test]
public void TestEncodeString_Normal_EncodedAsUtf8NonBom()
{
var encoding = new UTF8Encoding( encoderShouldEmitUTF8Identifier: false );
Assert.AreEqual( encoding.GetBytes( _testValue ), MessagePackConvert.EncodeString( _testValue ) );
}
[Test]
public void TestEncodeString_Empty_EncodedAsEmpty()
{
Assert.That( MessagePackConvert.EncodeString( String.Empty ), Is.Empty );
}
[Test]
public void TestEncodeString_Null()
{
Assert.Throws<ArgumentNullException>( () => MessagePackConvert.EncodeString( null ) );
}
[Test]
public void TestDecodeStringStrict_Normal_Success()
{
var encoding = new UTF8Encoding( encoderShouldEmitUTF8Identifier: false );
byte[] value = encoding.GetBytes( _testValue );
Assert.AreEqual( _testValue, MessagePackConvert.DecodeStringStrict( value ) );
}
[Test]
public void TestDecodeStringStrict_WithBom_Success()
{
var encoding = new UTF8Encoding( encoderShouldEmitUTF8Identifier: true );
byte[] value = encoding.GetBytes( _testValue );
Assert.AreEqual( _testValue, MessagePackConvert.DecodeStringStrict( value ) );
}
[Test]
public void TestDecodeStringStrict_Invalid()
{
byte[] value = Encoding.Unicode.GetBytes( _testValue );
Assert.Throws<DecoderFallbackException>( () => MessagePackConvert.DecodeStringStrict( value ) );
}
[Test]
public void TestDecodeStringStrict_Empty_Empty()
{
Assert.That( MessagePackConvert.DecodeStringStrict( new byte[ 0 ] ), Is.Empty );
}
[Test]
public void TestDecodeStringStrict_Null()
{
Assert.Throws<ArgumentNullException>( () => MessagePackConvert.DecodeStringStrict( null ) );
}
private static void AssertIsUnixEpocDateTimeOffset( DateTimeOffset actual, long millisecondsOffset )
{
Assert.That( actual.Offset, Is.EqualTo( TimeSpan.Zero ) );
var epoc = new DateTimeOffset( 1970, 1, 1, 0, 0, 0, TimeSpan.Zero );
Assert.That( actual, Is.EqualTo( epoc.AddMilliseconds( millisecondsOffset ) ) );
}
[Test]
public void TestToDateTimeOffset_Zero_IsUtcEpoc()
{
AssertIsUnixEpocDateTimeOffset( MessagePackConvert.ToDateTimeOffset( 0 ), 0 );
}
[Test]
public void TestToDateTimeOffset_One_IsUtcEpoc()
{
AssertIsUnixEpocDateTimeOffset( MessagePackConvert.ToDateTimeOffset( 1 ), 1 );
}
[Test]
public void TestToDateTimeOffset_MinuOne_IsUtcEpoc()
{
AssertIsUnixEpocDateTimeOffset( MessagePackConvert.ToDateTimeOffset( -1 ), -1 );
}
[Test]
public void TestToDateTimeOffset_Maximum_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MaxValue.Subtract( _utcEpoc ).TotalMilliseconds ) - 1L;
AssertIsUnixEpocDateTimeOffset( MessagePackConvert.ToDateTimeOffset( offset ), offset );
}
[Test]
public void TestToDateTimeOffset_Minimum_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MinValue.Subtract( _utcEpoc ).TotalMilliseconds );
AssertIsUnixEpocDateTimeOffset( MessagePackConvert.ToDateTimeOffset( offset ), offset );
}
[Test]
public void TestToDateTimeOffset_MaximumPlusOne_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MaxValue.Subtract( _utcEpoc ).TotalMilliseconds + 1L );
Assert.Throws<ArgumentOutOfRangeException>( () => MessagePackConvert.ToDateTimeOffset( offset ) );
}
[Test]
public void TestToDateTimeOffset_MinimumMinusOne_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MinValue.Subtract( _utcEpoc ).TotalMilliseconds - 1L );
Assert.Throws<ArgumentOutOfRangeException>( () => MessagePackConvert.ToDateTimeOffset( offset ) );
}
private static void AssertIsUnixEpocDateTime( DateTime actual, long millisecondsOffset )
{
Assert.That( actual.Kind, Is.EqualTo( DateTimeKind.Utc ) );
var epoc = new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc );
Assert.That( actual, Is.EqualTo( epoc.AddMilliseconds( millisecondsOffset ) ) );
}
[Test]
public void TestToDateTime_Zero_IsUtcEpoc()
{
AssertIsUnixEpocDateTime( MessagePackConvert.ToDateTime( 0 ), 0 );
}
[Test]
public void TestToDateTime_One_IsUtcEpoc()
{
AssertIsUnixEpocDateTime( MessagePackConvert.ToDateTime( 1 ), 1 );
}
[Test]
public void TestToDateTime_MinuOne_IsUtcEpoc()
{
AssertIsUnixEpocDateTime( MessagePackConvert.ToDateTime( -1 ), -1 );
}
[Test]
public void TestToDateTime_Maximum_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MaxValue.Subtract( _utcEpoc ).TotalMilliseconds ) - 1L;
AssertIsUnixEpocDateTime( MessagePackConvert.ToDateTime( offset ), offset );
}
[Test]
public void TestToDateTime_Minimum_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MinValue.Subtract( _utcEpoc ).TotalMilliseconds );
AssertIsUnixEpocDateTime( MessagePackConvert.ToDateTime( offset ), offset );
}
[Test]
public void TestToDateTime_MaximumPlusOne_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MaxValue.Subtract( _utcEpoc ).TotalMilliseconds + 1L );
Assert.Throws<ArgumentOutOfRangeException>( () => MessagePackConvert.ToDateTime( offset ) );
}
[Test]
public void TestToDateTime_MinimumMinusOne_IsUtcEpoc()
{
var offset = checked( ( long )DateTime.MinValue.Subtract( _utcEpoc ).TotalMilliseconds - 1L );
Assert.Throws<ArgumentOutOfRangeException>( () => MessagePackConvert.ToDateTime( offset ) );
}
[Test]
public void TestFromDateTimeOffset_UtcNow_AsUnixEpoc()
{
Assert.AreEqual(
checked( ( long )DateTime.UtcNow.Subtract( _utcEpoc ).TotalMilliseconds ),
MessagePackConvert.FromDateTimeOffset( DateTimeOffset.UtcNow )
);
}
[Test]
public void TestFromDateTimeOffset_Now_AsUtcUnixEpoc()
{
// LocalTime will be converted to UtcTime
Assert.AreEqual(
checked( ( long )DateTime.UtcNow.Subtract( _utcEpoc ).TotalMilliseconds ),
MessagePackConvert.FromDateTimeOffset( DateTimeOffset.Now )
);
}
[Test]
public void TestFromDateTimeOffset_UtcEpoc_Zero()
{
Assert.AreEqual(
0L,
MessagePackConvert.FromDateTimeOffset( new DateTimeOffset( 1970, 1, 1, 0, 0, 0, TimeSpan.Zero ) )
);
}
[Test]
public void TestFromDateTimeOffset_MinValue_AsUnixEpoc()
{
Assert.AreEqual(
checked( ( long )( DateTimeOffset.MinValue.ToUniversalTime().Subtract( _utcEpoc ).TotalMilliseconds ) ),
MessagePackConvert.FromDateTimeOffset( DateTimeOffset.MinValue.ToUniversalTime() )
);
}
[Test]
public void TestFromDateTimeOffset_MaxValue_AsUnixEpoc()
{
Assert.AreEqual(
checked( ( long )( DateTimeOffset.MaxValue.ToUniversalTime().Subtract( _utcEpoc ).TotalMilliseconds ) ),
MessagePackConvert.FromDateTimeOffset( DateTimeOffset.MaxValue.ToUniversalTime() )
);
}
[Test]
public void TestFromDateTime_UtcNow_AsUnixEpoc()
{
Assert.AreEqual(
checked( ( long )DateTime.UtcNow.Subtract( _utcEpoc ).TotalMilliseconds ),
MessagePackConvert.FromDateTime( DateTime.UtcNow )
);
}
[Test]
public void TestFromDateTime_Now_AsUtcUnixEpoc()
{
// LocalTime will be converted to UtcTime
var now = DateTime.Now;
Assert.AreEqual(
checked( ( long )now.ToUniversalTime().Subtract( _utcEpoc ).TotalMilliseconds ),
MessagePackConvert.FromDateTime( now )
);
}
[Test]
public void TestFromDateTime_UtcEpoc_Zero()
{
Assert.AreEqual(
0L,
MessagePackConvert.FromDateTime( new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc ) )
);
}
[Test]
public void TestFromDateTime_MinValue_AsUnixEpoc()
{
Assert.AreEqual(
checked( ( long )( DateTime.MinValue.ToUniversalTime().Subtract( _utcEpoc ).TotalMilliseconds ) ),
MessagePackConvert.FromDateTime( DateTime.MinValue.ToUniversalTime() )
);
}
[Test]
public void TestFromDateTime_MaxValue_AsUnixEpoc()
{
Assert.AreEqual(
checked( ( long )( DateTime.MaxValue.ToUniversalTime().Subtract( _utcEpoc ).TotalMilliseconds ) ),
MessagePackConvert.FromDateTime( DateTime.MaxValue.ToUniversalTime() )
);
}
}
}