Ejemplo n.º 1
0
        public void TestSuccessIntegralConversion()
        {
            var culture   = CultureInfo.InvariantCulture;
            var converter = new NumberConverter();

            Assert.AreEqual(byte.MinValue, converter.ConvertFromString(byte.MinValue.ToString(culture), culture, typeof(byte)));
            Assert.AreEqual(byte.MaxValue, converter.ConvertFromString(byte.MaxValue.ToString(culture), culture, typeof(byte)));
            Assert.AreEqual(byte.MinValue, converter.ConvertFromString(byte.MinValue.ToString(culture), culture, typeof(byte?)));
            Assert.AreEqual(byte.MaxValue, converter.ConvertFromString(byte.MaxValue.ToString(culture), culture, typeof(byte?)));
            Assert.AreEqual((byte)0, converter.ConvertFromString("0", culture, typeof(byte)));
            Assert.AreEqual((byte)0, converter.ConvertFromString("0", culture, typeof(byte?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(byte?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(byte)));

            Assert.AreEqual(sbyte.MinValue, converter.ConvertFromString(sbyte.MinValue.ToString(culture), culture, typeof(sbyte)));
            Assert.AreEqual(sbyte.MaxValue, converter.ConvertFromString(sbyte.MaxValue.ToString(culture), culture, typeof(sbyte)));
            Assert.AreEqual(sbyte.MinValue, converter.ConvertFromString(sbyte.MinValue.ToString(culture), culture, typeof(sbyte?)));
            Assert.AreEqual(sbyte.MaxValue, converter.ConvertFromString(sbyte.MaxValue.ToString(culture), culture, typeof(sbyte?)));
            Assert.AreEqual((sbyte)0, converter.ConvertFromString("0", culture, typeof(sbyte)));
            Assert.AreEqual((sbyte)0, converter.ConvertFromString("0", culture, typeof(sbyte?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(sbyte?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(sbyte)));

            Assert.AreEqual(short.MinValue, converter.ConvertFromString(short.MinValue.ToString(culture), culture, typeof(short)));
            Assert.AreEqual(short.MaxValue, converter.ConvertFromString(short.MaxValue.ToString(culture), culture, typeof(short)));
            Assert.AreEqual(short.MinValue, converter.ConvertFromString(short.MinValue.ToString(culture), culture, typeof(short?)));
            Assert.AreEqual(short.MaxValue, converter.ConvertFromString(short.MaxValue.ToString(culture), culture, typeof(short?)));
            Assert.AreEqual((short)0, converter.ConvertFromString("0", culture, typeof(short)));
            Assert.AreEqual((short)0, converter.ConvertFromString("0", culture, typeof(short?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(short?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(short)));

            Assert.AreEqual(ushort.MinValue, converter.ConvertFromString(ushort.MinValue.ToString(culture), culture, typeof(ushort)));
            Assert.AreEqual(ushort.MaxValue, converter.ConvertFromString(ushort.MaxValue.ToString(culture), culture, typeof(ushort)));
            Assert.AreEqual(ushort.MinValue, converter.ConvertFromString(ushort.MinValue.ToString(culture), culture, typeof(ushort?)));
            Assert.AreEqual(ushort.MaxValue, converter.ConvertFromString(ushort.MaxValue.ToString(culture), culture, typeof(ushort?)));
            Assert.AreEqual((ushort)0, converter.ConvertFromString("0", culture, typeof(ushort)));
            Assert.AreEqual((ushort)0, converter.ConvertFromString("0", culture, typeof(ushort?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(ushort?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(ushort)));

            Assert.AreEqual(int.MinValue, converter.ConvertFromString(int.MinValue.ToString(culture), culture, typeof(int)));
            Assert.AreEqual(int.MaxValue, converter.ConvertFromString(int.MaxValue.ToString(culture), culture, typeof(int)));
            Assert.AreEqual(int.MinValue, converter.ConvertFromString(int.MinValue.ToString(culture), culture, typeof(int?)));
            Assert.AreEqual(int.MaxValue, converter.ConvertFromString(int.MaxValue.ToString(culture), culture, typeof(int?)));
            Assert.AreEqual((int)0, converter.ConvertFromString("0", culture, typeof(int)));
            Assert.AreEqual((int)0, converter.ConvertFromString("0", culture, typeof(int?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(int?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(int)));

            Assert.AreEqual(uint.MinValue, converter.ConvertFromString(uint.MinValue.ToString(culture), culture, typeof(uint)));
            Assert.AreEqual(uint.MaxValue, converter.ConvertFromString(uint.MaxValue.ToString(culture), culture, typeof(uint)));
            Assert.AreEqual(uint.MinValue, converter.ConvertFromString(uint.MinValue.ToString(culture), culture, typeof(uint?)));
            Assert.AreEqual(uint.MaxValue, converter.ConvertFromString(uint.MaxValue.ToString(culture), culture, typeof(uint?)));
            Assert.AreEqual((uint)0, converter.ConvertFromString("0", culture, typeof(uint)));
            Assert.AreEqual((uint)0, converter.ConvertFromString("0", culture, typeof(uint?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(uint?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(uint)));

            Assert.AreEqual(long.MinValue, converter.ConvertFromString(long.MinValue.ToString(culture), culture, typeof(long)));
            Assert.AreEqual(long.MaxValue, converter.ConvertFromString(long.MaxValue.ToString(culture), culture, typeof(long)));
            Assert.AreEqual(long.MinValue, converter.ConvertFromString(long.MinValue.ToString(culture), culture, typeof(long?)));
            Assert.AreEqual(long.MaxValue, converter.ConvertFromString(long.MaxValue.ToString(culture), culture, typeof(long?)));
            Assert.AreEqual((long)0, converter.ConvertFromString("0", culture, typeof(long)));
            Assert.AreEqual((long)0, converter.ConvertFromString("0", culture, typeof(long?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(long?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(long)));

            Assert.AreEqual(ulong.MinValue, converter.ConvertFromString(ulong.MinValue.ToString(culture), culture, typeof(ulong)));
            Assert.AreEqual(ulong.MaxValue, converter.ConvertFromString(ulong.MaxValue.ToString(culture), culture, typeof(ulong)));
            Assert.AreEqual(ulong.MinValue, converter.ConvertFromString(ulong.MinValue.ToString(culture), culture, typeof(ulong?)));
            Assert.AreEqual(ulong.MaxValue, converter.ConvertFromString(ulong.MaxValue.ToString(culture), culture, typeof(ulong?)));
            Assert.AreEqual((ulong)0, converter.ConvertFromString("0", culture, typeof(ulong)));
            Assert.AreEqual((ulong)0, converter.ConvertFromString("0", culture, typeof(ulong?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(ulong?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(ulong)));

            Assert.True(Math.Abs((float)converter.ConvertFromString("123.33", culture, typeof(float)) - ((float)123.33)) < (float)0.001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("0.0000002347", culture, typeof(float)) - ((float)0.0000002347)) < (float)0.00000000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("-0.0000002347", culture, typeof(float)) - ((float)-0.0000002347)) < (float)0.00000000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("-987347.2304757", culture, typeof(float)) - ((float)-987347.2304757)) < (float)0.00000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("123.33", culture, typeof(float?)) - ((float)123.33)) < (float)0.001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("0.0000002347", culture, typeof(float?)) - ((float)0.0000002347)) < (float)0.00000000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("-0.0000002347", culture, typeof(float?)) - ((float)-0.0000002347)) < (float)0.00000000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("-987347.2304757", culture, typeof(float?)) - ((float)-987347.2304757)) < (float)0.00000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("1.227e-4", culture, typeof(float)) - ((float)1.227e-4)) < (float)0.00000001);
            Assert.True(Math.Abs((float)converter.ConvertFromString("50", culture, typeof(float)) - ((float)50)) < (float)0.1);
            Assert.True(Math.Abs((float)converter.ConvertFromString("-7180", culture, typeof(float)) - ((float)-7180)) < (float)0.1);
            Assert.AreEqual((float)0, converter.ConvertFromString("0", culture, typeof(float)));
            Assert.AreEqual((float)0, converter.ConvertFromString("0", culture, typeof(float?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(float?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(float)));

            Assert.True(Math.Abs((double)converter.ConvertFromString("123.33", culture, typeof(double)) - ((double)123.33)) < (double)0.001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("0.0000002347", culture, typeof(double)) - ((double)0.0000002347)) < (double)0.00000000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("-0.0000002347", culture, typeof(double)) - ((double)-0.0000002347)) < (double)0.00000000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("-987347.2304757", culture, typeof(double)) - ((double)-987347.2304757)) < (double)0.00000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("123.33", culture, typeof(double?)) - ((double)123.33)) < (double)0.001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("0.0000002347", culture, typeof(double?)) - ((double)0.0000002347)) < (double)0.00000000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("-0.0000002347", culture, typeof(double?)) - ((double)-0.0000002347)) < (double)0.00000000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("-987347.2304757", culture, typeof(double?)) - ((double)-987347.2304757)) < (double)0.00000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("1.227e-4", culture, typeof(double)) - ((double)1.227e-4)) < (double)0.00000001);
            Assert.True(Math.Abs((double)converter.ConvertFromString("50", culture, typeof(double)) - ((double)50)) < (double)0.1);
            Assert.True(Math.Abs((double)converter.ConvertFromString("-7180", culture, typeof(double)) - ((double)-7180)) < (double)0.1);
            Assert.AreEqual((double)0, converter.ConvertFromString("0", culture, typeof(double)));
            Assert.AreEqual((double)0, converter.ConvertFromString("0", culture, typeof(double?)));
            Assert.Null(converter.ConvertFromString("", culture, typeof(double?)));
            Assert.Throws <FormatException>(() => converter.ConvertFromString("", culture, typeof(double)));
        }