Esempio n. 1
0
        public void TryParseFloat_FromDecimalAndNullableDecimal()
        {
            #region float - full method
            var success = ConvertibleDecimalValue.TryParseFloat(DefaultValue);

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting decimal to float");
            #endregion

            #region float - without DefaultValue param
            success = ConvertibleDecimalValue.TryParseFloat();

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting decimal to float");
            #endregion

            #region float? - full method
            success = ConvertibleNullableDecimalValue.TryParseFloat(DefaultValue);
            var invalid1 = ((decimal?)null).TryParseFloat(DefaultValue);

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting decimal? to float");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting decimal? to float");
            #endregion

            #region float? - without DefaultValue param
            success  = ConvertibleNullableDecimalValue.TryParseFloat();
            invalid1 = ((decimal?)null).TryParseFloat();

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting decimal? to float");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultFloatConversionValue(), "Error converting decimal? to float");
            #endregion
        }
Esempio n. 2
0
        public void TryParseFloat_FromObject()
        {
            var objectValid   = (object)1;
            var objectInvalid = (object)"a";

            Assert.AreEqual(objectValid.TryParseFloat(), Double.Parse(objectValid.ToString()), "Error converting object value to float");
            Assert.AreEqual(objectInvalid.TryParseFloat(), BasePrimitivesExtensions.GetDefaultFloatConversionValue(), "Error converting object value to float");

            Assert.AreEqual(objectValid.TryParseFloat(2), Double.Parse(objectValid.ToString()), "Error converting object value to float");
            Assert.AreEqual(objectInvalid.TryParseFloat(2), 2, "Error converting object value to float");
        }
Esempio n. 3
0
        public void TryParseFloat_FromDoubleAndNullableDouble()
        {
            const double invalidValue1         = double.MaxValue;
            double?      nullableInvalidValue1 = double.MaxValue;
            const double invalidValue2         = double.MinValue;
            double?      nullableInvalidValue2 = double.MinValue;

            #region double - full method
            var success  = ConvertibleDoubleValue.TryParseFloat(DefaultValue);
            var invalid1 = invalidValue1.TryParseFloat(DefaultValue);
            var invalid2 = invalidValue2.TryParseFloat(DefaultValue);

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting double to float");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting double to float");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting double to float");
            #endregion

            #region double - without DefaultValue param
            success  = ConvertibleDoubleValue.TryParseFloat();
            invalid1 = invalidValue1.TryParseFloat();
            invalid2 = invalidValue2.TryParseFloat();

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting double to byte");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultFloatConversionValue(), "Error converting double to float");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultFloatConversionValue(), "Error converting double to float");
            #endregion

            #region double? - full method
            success  = ConvertibleNullableDoubleValue.TryParseFloat(DefaultValue);
            invalid1 = nullableInvalidValue1.TryParseFloat(DefaultValue);
            invalid2 = nullableInvalidValue2.TryParseFloat(DefaultValue);

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting double? to float");
            Assert.AreEqual(invalid1, DefaultValue, "Error converting double? to float");
            Assert.AreEqual(invalid2, DefaultValue, "Error converting double? to float");
            #endregion

            #region double? - without DefaultValue param
            success  = ConvertibleNullableDoubleValue.TryParseFloat();
            invalid1 = nullableInvalidValue1.TryParseFloat();
            invalid2 = nullableInvalidValue2.TryParseFloat();

            Assert.AreEqual(success, ConvertibleFloatValue, "Error converting double? to float");
            Assert.AreEqual(invalid1, BasePrimitivesExtensions.GetDefaultFloatConversionValue(), "Error converting double? to float");
            Assert.AreEqual(invalid2, BasePrimitivesExtensions.GetDefaultFloatConversionValue(), "Error converting double? to float");
            #endregion
        }
Esempio n. 4
0
        public void TryParseFloatArray()
        {
            const string successValue1 = "10";
            const string successValue2 = "20";
            var          errorValue1   = double.MaxValue.ToString(CultureInfo.InvariantCulture);
            var          errorValue2   = double.MinValue.ToString(CultureInfo.InvariantCulture);

            var successByteValues = new[] { successValue1.TryParseFloat(), successValue2.TryParseFloat() };
            var successValues     = new[] { successValue1, successValue2 };
            var errorValues       = new[] { errorValue1, errorValue2 };
            var mixedValues       = new[] { successValue1, successValue2, errorValue1, errorValue2 };

            #region full method (with number format), without default, dont return defult conversion
            var successConversion = string.Join(",", successValues).TryParseFloatArray(null, false,
                                                                                       BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                                                                                       BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to float array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to float array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to float array");

            var falseConversions = string.Join(",", errorValues).TryParseFloatArray(null, false,
                                                                                    BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                                                                                    BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to float array");
            Assert.IsFalse(falseConversions.Any(), "Error converting string to float array");

            var mixedConversions = string.Join(",", mixedValues).TryParseFloatArray(null, false,
                                                                                    BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                                                                                    BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(mixedConversions, "Error converting string to float array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(mixedConversions.Count() == successByteValues.Count(), "Error converting string to float array");
            #endregion

            #region partial method (with number format), without default
            successConversion = string.Join(",", successValues).TryParseFloatArray(null,
                                                                                   BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                                                                                   BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to float array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to float array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to float array");

            falseConversions = string.Join(",", errorValues).TryParseFloatArray(null,
                                                                                BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                                                                                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to float array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");

            mixedConversions = string.Join(",", mixedValues).TryParseFloatArray(null,
                                                                                BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                                                                                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(mixedConversions, "Error converting string to float array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to float array");
            #endregion

            #region simple method (with number format)
            successConversion = string.Join(",", successValues).TryParseFloatArray(
                BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(successConversion, "Error converting string to float array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to float array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to float array");

            falseConversions = string.Join(",", errorValues).TryParseFloatArray(
                BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(falseConversions, "Error converting string to float array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");

            mixedConversions = string.Join(",", mixedValues).TryParseFloatArray(
                BasePrimitivesExtensions.GetDefaultFloatNumberStyle(),
                BasePrimitivesExtensions.GetCurrentCulture());
            Assert.IsNotNull(mixedConversions, "Error converting string to float array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to float array");
            #endregion

            #region full method (without number format), without default, dont return defult conversion
            successConversion = string.Join(",", successValues).TryParseFloatArray(null, false);
            Assert.IsNotNull(successConversion, "Error converting string to float array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to float array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to float array");

            falseConversions = string.Join(",", errorValues).TryParseFloatArray(null, false);
            Assert.IsNotNull(falseConversions, "Error converting string to float array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");

            mixedConversions = string.Join(",", mixedValues).TryParseFloatArray(null, false);
            Assert.IsNotNull(mixedConversions, "Error converting string to float array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to float array");
            #endregion

            #region partial method (without number format), without default
            successConversion = string.Join(",", successValues).TryParseFloatArray(null);
            Assert.IsNotNull(successConversion, "Error converting string to float array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to float array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to float array");

            falseConversions = string.Join(",", errorValues).TryParseFloatArray(null);
            Assert.IsNotNull(falseConversions, "Error converting string to float array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");

            mixedConversions = string.Join(",", mixedValues).TryParseFloatArray(null);
            Assert.IsNotNull(mixedConversions, "Error converting string to float array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to float array");
            #endregion

            #region simple method (without number format)
            successConversion = string.Join(",", successValues).TryParseFloatArray();
            Assert.IsNotNull(successConversion, "Error converting string to float array");
            Assert.IsTrue(successConversion.Any(), "Error converting string to float array");
            Assert.IsFalse(successConversion.Any(a => !successByteValues.Contains(a)), "Error converting string to float array");
            Assert.IsTrue(successConversion.Count() == successByteValues.Count(), "Error converting string to float array");

            falseConversions = string.Join(",", errorValues).TryParseFloatArray();
            Assert.IsNotNull(falseConversions, "Error converting string to float array");
            Assert.IsFalse(falseConversions.Any(a => !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");

            mixedConversions = string.Join(",", mixedValues).TryParseFloatArray();
            Assert.IsNotNull(mixedConversions, "Error converting string to float array");
            Assert.IsFalse(mixedConversions.Any(a => !successByteValues.Contains(a) && !a.Equals(BasePrimitivesExtensions.GetDefaultFloatConversionValue())), "Error converting string to float array");
            Assert.IsTrue((mixedConversions.Count() - falseConversions.Count()) == successByteValues.Count(), "Error converting string to float array");
            #endregion
        }