public void TestFromDateTime_UtcNow_AsUnixEpoc() { Assert.AreEqual( checked (( long )DateTime.UtcNow.Subtract(_utcEpoc).TotalMilliseconds), MessagePackConvert.FromDateTime(DateTime.UtcNow) ); }
private void DecodeIfNeeded() { if (this._decoded != null) { return; } if (this._encoded == null) { return; } if (this._type != BinaryType.Unknwon) { return; } try { this._decoded = MessagePackConvert.DecodeStringStrict(this._encoded); this._type = BinaryType.String; } catch (DecoderFallbackException ex) { this._decodingError = ex; this._type = BinaryType.Blob; } }
public void TestDecodeStringStrict_WithBom_Success() { var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); byte[] value = encoding.GetBytes(_testValue); Assert.AreEqual(_testValue, MessagePackConvert.DecodeStringStrict(value)); }
public void TestFromDateTime_MaxValue_AsUnixEpoc() { Assert.AreEqual( checked (( long )(DateTime.MaxValue.ToUniversalTime().Subtract(_utcEpoc).TotalMilliseconds)), MessagePackConvert.FromDateTime(DateTime.MaxValue.ToUniversalTime()) ); }
public void TestFromDateTime_UtcEpoc_Zero() { Assert.AreEqual( 0L, MessagePackConvert.FromDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)) ); }
public void TestFromDateTimeOffset_UtcEpoc_Zero() { Assert.AreEqual( 0L, MessagePackConvert.FromDateTimeOffset(new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)) ); }
public void TestFromDateTimeOffset_Now_AsUtcUnixEpoc() { // LocalTime will be converted to UtcTime Assert.AreEqual( checked (DateTime.UtcNow.Subtract(UtcEpoc).Ticks / TicksToMilliseconds), MessagePackConvert.FromDateTimeOffset(DateTimeOffset.Now) ); }
public void TestFromDateTimeOffset_UtcNow_AsUnixEpoc() { var utcNow = DateTimeOffset.UtcNow; Assert.AreEqual( checked (utcNow.DateTime.Subtract(UtcEpoc).Ticks / TicksToMilliseconds), MessagePackConvert.FromDateTimeOffset(utcNow) ); }
public void TestToday() { var before = new DateTimeOffset(DateTimeOffset.UtcNow.Date, TimeSpan.Zero); var target = Timestamp.Today; var after = new DateTimeOffset(DateTimeOffset.UtcNow.Date, TimeSpan.Zero); // Assert before <= today <= after Assert.That(target.UnixEpochSecondsPart, Is.EqualTo(MessagePackConvert.FromDateTimeOffset(before) / 1000).Or.GreaterThan(MessagePackConvert.FromDateTimeOffset(before))); Assert.That(target.UnixEpochSecondsPart, Is.EqualTo(MessagePackConvert.FromDateTimeOffset(after) / 1000).Or.LessThan(MessagePackConvert.FromDateTimeOffset(after))); }
public void TestUtcNow() { var before = DateTimeOffset.UtcNow; var target = Timestamp.UtcNow; var after = DateTimeOffset.UtcNow; // Assert before <= now <= after Assert.That(target.UnixEpochSecondsPart, Is.EqualTo(MessagePackConvert.FromDateTimeOffset(before) / 1000).Or.GreaterThan(MessagePackConvert.FromDateTimeOffset(before))); Assert.That(target.UnixEpochSecondsPart, Is.EqualTo(MessagePackConvert.FromDateTimeOffset(after) / 1000).Or.LessThan(MessagePackConvert.FromDateTimeOffset(after))); }
public void TestFromDateTime_Now_AsUtcUnixEpoc() { // LocalTime will be converted to UtcTime var now = DateTime.Now; Assert.AreEqual( checked (now.ToUniversalTime().Subtract(UtcEpoc).Ticks / TicksToMilliseconds), MessagePackConvert.FromDateTime(now) ); }
public void TestIssue43() { var expected = new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc); var actual = new DateTime(3155378975999999999L, DateTimeKind.Utc); Assert.AreEqual( MessagePackConvert.ToDateTime(MessagePackConvert.FromDateTime(expected)), MessagePackConvert.ToDateTime(MessagePackConvert.FromDateTime(actual)) ); }
public void TestExplicitDateTimeOffset_UnderflowSeconds() { var source = new Timestamp(MessagePackConvert.FromDateTimeOffset(DateTimeOffset.MinValue) - 1L, checked (( int )(DateTimeOffset.MinValue.Ticks % 10000000 * 100))); Assert.Throws <InvalidOperationException>(() => { var x = ( DateTimeOffset )source; }); }
public void TestToDateTimeOffset_UnderflowNanoseconds() { var source = new Timestamp(MessagePackConvert.FromDateTimeOffset(DateTimeOffset.MinValue) - 1L, 999999999); Assert.Throws <InvalidOperationException>(() => source.ToDateTimeOffset()); }
public void TestToDateTimeOffset_OverflowSeconds() { var source = new Timestamp(MessagePackConvert.FromDateTimeOffset(DateTimeOffset.MaxValue) + 1L, checked (( int )(DateTimeOffset.MaxValue.Ticks % 10000000 * 100))); Assert.Throws <InvalidOperationException>(() => source.ToDateTimeOffset()); }
private static Tuple <double, double, double, double> TestEqualsCore() { Assert.IsTrue( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false) ), "Binary-Binary-True" ); Assert.IsTrue( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals( new MessagePackString("ABC") ), "Binary-String-True" ); Assert.IsTrue( new MessagePackString("ABC").Equals( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false) ), "String-Binary-True" ); Assert.IsTrue( new MessagePackString("ABC").Equals( new MessagePackString("ABC") ), "String-String-True" ); Assert.IsFalse( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'D' }, false) ), "Binary-Binary-False" ); Assert.IsFalse( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false).Equals( new MessagePackString("ABD") ), "Binary-String-False" ); Assert.IsFalse( new MessagePackString("ABD").Equals( new MessagePackString(new byte[] { ( byte )'A', ( byte )'B', ( byte )'C' }, false) ), "String-Binary-False" ); Assert.IsFalse( new MessagePackString("ABC").Equals( new MessagePackString("ABD") ), "String-String-False" ); var values = new[] { new MessagePackString(new byte[0], false), new MessagePackString(new byte[] { 0x20 }, false), new MessagePackString(new byte[] { 0xff }, false), new MessagePackString(new byte[] { 1, 2, 3 }, false), new MessagePackString(new byte[] { 3, 2, 1 }, false) }; const int iteration = 10; double tinyAvg = Double.MaxValue; double smallAvg = Double.MaxValue; double mediumAvg = Double.MaxValue; double largeAvg = Double.MaxValue; var sw = new Stopwatch(); for (int i = 0; i < iteration; i++) { sw.Restart(); for (int x = 0; x < values.Length; x++) { Assert.That(values[x].Equals(null), Is.False); for (int y = 0; y < values.Length; y++) { Assert.That(values[x].Equals(values[y]), Is.EqualTo(x == y)); } } sw.Stop(); tinyAvg = Math.Min(tinyAvg, sw.Elapsed.Ticks * 10.0 / (values.Length * values.Length)); } var smallX = new MessagePackString(new String('A', 16)); var smallY = new MessagePackString(MessagePackConvert.EncodeString(new String('A', 16)), false); for (int i = 0; i < iteration; i++) { sw.Restart(); Assert.That(smallX.Equals(smallY), Is.True); sw.Stop(); smallAvg = Math.Min(smallAvg, sw.Elapsed.Ticks * 10.0); } var mediumX = new MessagePackString(new String('A', 1000)); var mediumY = new MessagePackString(MessagePackConvert.EncodeString(new String('A', 1000)), false); for (int i = 0; i < iteration; i++) { sw.Restart(); Assert.That(mediumX.Equals(mediumY), Is.True); sw.Stop(); mediumAvg = Math.Min(mediumAvg, sw.Elapsed.Ticks * 10.0); } var largeX = new MessagePackString(new String('A', 100000)); var largeY = new MessagePackString(MessagePackConvert.EncodeString(new String('A', 100000)), false); for (int i = 0; i < iteration; i++) { sw.Restart(); Assert.That(largeX.Equals(largeY), Is.True); sw.Stop(); largeAvg = Math.Min(largeAvg, sw.Elapsed.Ticks * 10.0); } return(Tuple.Create(tinyAvg, smallAvg, mediumAvg, largeAvg)); }
public void TestDecodeStringStrict_Empty_Empty() { Assert.That(MessagePackConvert.DecodeStringStrict(new byte[0]), Is.Empty); }
public void TestToDateTimeRoundTrip_Minimum_IsUtcEpoc() { var offset = checked (UtcMinValue.Subtract(UtcEpoc).Ticks / TicksToMilliseconds); AssertIsUnixEpocDateTime(DateTime.MinValue, MessagePackConvert.ToDateTime(offset)); }
private static MessagePackObject?TryValidateObjectArgument(object value) { if (value == null) { return(new MessagePackObject?(MessagePackObject.Nil)); } if (value is MessagePackObject) { return(new MessagePackObject?((MessagePackObject)value)); } if (value is MessagePackObject?) { MessagePackObject?nullable2 = (MessagePackObject?)value; return(new MessagePackObject?(nullable2.HasValue ? nullable2.GetValueOrDefault() : MessagePackObject.Nil)); } byte[] buffer = value as byte[]; if (buffer != null) { return(new MessagePackObject?(buffer)); } string str = value as string; if (str != null) { return(new MessagePackObject?(str)); } MessagePackString messagePackString = value as MessagePackString; if (messagePackString != null) { return(new MessagePackObject(messagePackString)); } switch (Type.GetTypeCode(value.GetType())) { case TypeCode.Empty: case TypeCode.DBNull: return(new MessagePackObject?(MessagePackObject.Nil)); case TypeCode.Boolean: return(new MessagePackObject?((bool)value)); case TypeCode.SByte: return(new MessagePackObject?((sbyte)value)); case TypeCode.Byte: return(new MessagePackObject?((byte)value)); case TypeCode.Int16: return(new MessagePackObject?((short)value)); case TypeCode.UInt16: return(new MessagePackObject?((ushort)value)); case TypeCode.Int32: return(new MessagePackObject?((int)value)); case TypeCode.UInt32: return(new MessagePackObject?((uint)value)); case TypeCode.Int64: return(new MessagePackObject?((long)value)); case TypeCode.UInt64: return(new MessagePackObject?((ulong)value)); case TypeCode.Single: return(new MessagePackObject?((float)value)); case TypeCode.Double: return(new MessagePackObject?((double)value)); case TypeCode.DateTime: return(new MessagePackObject?(MessagePackConvert.FromDateTime((DateTime)value))); case TypeCode.String: return(new MessagePackObject?(value.ToString())); } return(null); }
public void TestEncodeString_Empty_EncodedAsEmpty() { Assert.That(MessagePackConvert.EncodeString(String.Empty), Is.Empty); }
public void TestEncodeString_Normal_EncodedAsUtf8NonBom() { var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); Assert.AreEqual(encoding.GetBytes(_testValue), MessagePackConvert.EncodeString(_testValue)); }
public void TestToDateTime_Maximum_IsUtcEpoc() { var offset = checked (UtcMaxValue.Subtract(UtcEpoc).Ticks / TicksToMilliseconds); AssertIsUnixEpocDateTime(MessagePackConvert.ToDateTime(offset), offset); }
public void TestToDateTimeRoundTrip_Zero_IsUtcEpoc() { AssertIsUnixEpocDateTime(UtcEpoc, MessagePackConvert.ToDateTime(0)); }
public void TestToDateTime_MinimumMinusOne_IsUtcEpoc() { var offset = checked (UtcMinValue.Subtract(UtcEpoc).Ticks / TicksToMilliseconds - 1L); Assert.Throws <ArgumentOutOfRangeException>(() => MessagePackConvert.ToDateTime(offset)); }
public void TestExplicitDateTimeOffset_UnderflowNanoseconds() { var source = new Timestamp(MessagePackConvert.FromDateTimeOffset(DateTimeOffset.MinValue) - 1, 999999999); Assert.Throws <InvalidOperationException>(() => { var x = ( DateTimeOffset )source; }); }
public void TestEncodeString_Null() { Assert.Throws <ArgumentNullException>(() => MessagePackConvert.EncodeString(null)); }
private static MessagePackObject?TryValidateObjectArgument(object value) { if (value == null) { return(MessagePackObject.Nil); } if (value is MessagePackObject) { return(( MessagePackObject )value); } if (value is MessagePackObject? ) { return(( MessagePackObject? )value ?? MessagePackObject.Nil); } byte[] asBytes; if ((asBytes = value as byte[]) != null) { return(asBytes); } string asString; if ((asString = value as string) != null) { return(asString); } MessagePackString asMessagePackString; if ((asMessagePackString = value as MessagePackString) != null) { return(new MessagePackObject(asMessagePackString)); } #if NETFX_CORE switch (WinRTCompatibility.GetTypeCode(value.GetType())) #else switch (Type.GetTypeCode(value.GetType())) #endif { case TypeCode.Boolean: { return(( bool )value); } case TypeCode.Byte: { return(( byte )value); } case TypeCode.DateTime: { return(MessagePackConvert.FromDateTime(( DateTime )value)); } case TypeCode.DBNull: case TypeCode.Empty: { return(MessagePackObject.Nil); } case TypeCode.Double: { return(( double )value); } case TypeCode.Int16: { return(( short )value); } case TypeCode.Int32: { return(( int )value); } case TypeCode.Int64: { return(( long )value); } case TypeCode.SByte: { return(( sbyte )value); } case TypeCode.Single: { return(( float )value); } case TypeCode.String: { return(value.ToString()); } case TypeCode.UInt16: { return(( ushort )value); } case TypeCode.UInt32: { return(( uint )value); } case TypeCode.UInt64: { return(( ulong )value); } case TypeCode.Char: case TypeCode.Decimal: case TypeCode.Object: default: { return(null); } } }
public void TestDecodeStringStrict_Invalid() { byte[] value = Encoding.Unicode.GetBytes(_testValue); Assert.Throws <DecoderFallbackException>(() => MessagePackConvert.DecodeStringStrict(value)); }
private static MessagePackObject?TryValidateObjectArgument(object value) { if (value == null) { return(MessagePackObject.Nil); } if (value is MessagePackObject) { return(( MessagePackObject )value); } byte[] asBytes; if ((asBytes = value as byte[]) != null) { return(asBytes); } string asString; if ((asString = value as string) != null) { return(asString); } MessagePackString asMessagePackString; if ((asMessagePackString = value as MessagePackString) != null) { return(new MessagePackObject(asMessagePackString)); } #if (NETSTANDARD1_1 || NETSTANDARD1_3) switch (NetStandardCompatibility.GetTypeCode(value.GetType())) #else switch (Type.GetTypeCode(value.GetType())) #endif // NETSTANDARD1_1 || NETSTANDARD1_3 { case TypeCode.Boolean: { return(( bool )value); } case TypeCode.Byte: { return(( byte )value); } case TypeCode.DateTime: { return(MessagePackConvert.FromDateTime(( DateTime )value)); } case TypeCode.DBNull: case TypeCode.Empty: { return(MessagePackObject.Nil); } case TypeCode.Double: { return(( double )value); } case TypeCode.Int16: { return(( short )value); } case TypeCode.Int32: { return(( int )value); } case TypeCode.Int64: { return(( long )value); } case TypeCode.SByte: { return(( sbyte )value); } case TypeCode.Single: { return(( float )value); } case TypeCode.String: { return(value.ToString()); } case TypeCode.UInt16: { return(( ushort )value); } case TypeCode.UInt32: { return(( uint )value); } case TypeCode.UInt64: { return(( ulong )value); } // ReSharper disable RedundantCaseLabel case TypeCode.Char: case TypeCode.Decimal: case TypeCode.Object: // ReSharper restore RedundantCaseLabel default: { return(null); } } }
public void TestToDateTimeRoundTrip_MinusOne_IsUtcEpoc() { AssertIsUnixEpocDateTime(UtcEpoc.AddMilliseconds(-1), MessagePackConvert.ToDateTime(-1)); }