예제 #1
0
        public static void GetBytes_ToDecimal(decimal value)
        {
            // Arrange

            // Act
            var result = DecimalHelper.ToDecimal(DecimalHelper.GetBytes(value), 0);

            // Assert
            Assert.Equal(value, result);
        }
예제 #2
0
        public static void GetBytes(string expected, string s)
        {
            // Arrange
            var value = decimal.Parse(s, CultureInfo.InvariantCulture);

            // Act
            var result = DecimalHelper.GetBytes(value);

            // Assert
            Assert.Equal(expected, Convert.ToBase64String(result));
        }
        public void Deserialize_Decimal()
        {
            // Arrange
            var formatter = new SimpleObjectFormatter(Encoding.UTF8, m_mockInner.Object);
            var flags     = (int)TypeCode.Decimal;
            var value     = RandomHelper.NextInt(g_random, -10000, 10000) / 100M;
            var data      = new ArraySegment <byte>(DecimalHelper.GetBytes(value));

            // Act
            var result = formatter.Deserialize <decimal>(data, flags);

            // Assert
            Assert.Equal(value, result);
        }
        public void Serialize_Decimal()
        {
            // Arrange
            var formatter = new SimpleObjectFormatter(Encoding.UTF8, m_mockInner.Object);
            var flags     = -1;
            var value     = 100M;

            // Act
            var result = formatter.Serialize <decimal>(value, out flags);

            // Assert
            Assert.Equal((int)TypeCode.Decimal, flags);
            Assert.NotNull(result.Array);
            Assert.Equal(DecimalHelper.GetBytes(value), result.Array);
        }
예제 #5
0
 /// <summary>
 /// Writes <see cref="decimal"/> value to the given <see cref="StringBuilder"/>.
 /// </summary>
 /// <param name="sb"><see cref="StringBuilder"/> to write to.</param>
 /// <param name="value">Value to write.</param>
 public static void WriteDecimal(StringBuilder sb, decimal value)
 {
     writeBytesAsChars(sb, DecimalHelper.GetBytes(value), 16);
 }
예제 #6
0
        public override ArraySegment <byte> Serialize <T>(T value, out int flags)
        {
            TypeCode typeCode = Type.GetTypeCode(typeof(T));

            byte[] data;
            switch (typeCode)
            {
            case TypeCode.Int32:
                data = BitConverter.GetBytes((int)(object)value);
                break;

            case TypeCode.Boolean:
                data = BitConverter.GetBytes((bool)(object)value);
                break;

            case TypeCode.String:
                data = m_encoding.GetBytes((string)(object)value);
                break;

            case TypeCode.DateTime:
                data = BitConverter.GetBytes(((DateTime)(object)value).Ticks);
                break;

            case TypeCode.Int64:
                data = BitConverter.GetBytes((long)(object)value);
                break;

            case TypeCode.Decimal:
                data = DecimalHelper.GetBytes(((decimal)(object)value));
                break;

            case TypeCode.Byte:
                data = new byte[] { ((byte)(object)value) };
                break;

            case TypeCode.Char:
                data = BitConverter.GetBytes((char)(object)value);
                break;

            case TypeCode.DBNull:
                data = Empty;
                break;

            case TypeCode.Double:
                data = BitConverter.GetBytes((double)(object)value);
                break;

            case TypeCode.Int16:
                data = BitConverter.GetBytes((short)(object)value);
                break;

            case TypeCode.Single:
                data = BitConverter.GetBytes((float)(object)value);
                break;

            case TypeCode.UInt16:
                data = BitConverter.GetBytes((ushort)(object)value);
                break;

            case TypeCode.UInt32:
                data = BitConverter.GetBytes((uint)(object)value);
                break;

            case TypeCode.UInt64:
                data = BitConverter.GetBytes((ulong)(object)value);
                break;

            default:
                return(base.Serialize <T>(value, out flags));
            }

            flags = (int)typeCode;
            return(new ArraySegment <byte>(data, 0, data.Length));
        }