Beispiel #1
0
        public DbaseSingle(DbaseField field, float?value = null) : base(field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if (field.FieldType != DbaseFieldType.Float)
            {
                throw new ArgumentException(
                          $"The field {field.Name} 's type must be float to use it as a single field.", nameof(field));
            }

            if (field.Length < MinimumLength || field.Length > MaximumLength)
            {
                throw new ArgumentException(
                          $"The field {field.Name} 's length ({field.Length}) must be between {MinimumLength} and {MaximumLength}.",
                          nameof(field));
            }

            Provider = new NumberFormatInfo
            {
                NumberDecimalDigits    = DbaseDecimalCount.Min(MaximumDecimalCount, field.DecimalCount).ToInt32(),
                NumberDecimalSeparator = "."
            };

            Value = value;
        }
        public bool AcceptsValue(float value)
        {
            if (Field.DecimalCount.ToInt32() == 0)
            {
                return(((float)Math.Truncate(value)).ToString("F", Provider).Length <= Field.Length.ToInt32());
            }

            var digits  = DbaseDecimalCount.Min(MaximumDecimalCount, Field.DecimalCount).ToInt32();
            var rounded = (float)Math.Round(value, digits);

            return(rounded.ToString("F", Provider).Length <= Field.Length.ToInt32());
        }
Beispiel #3
0
        public bool AcceptsValue(double?value)
        {
            if (!value.HasValue)
            {
                return(true);
            }

            if (Field.DecimalCount.ToInt32() == 0)
            {
                return(Math.Truncate(value.Value).ToString("F", Provider).Length <= Field.Length.ToInt32());
            }

            var digits  = DbaseDecimalCount.Min(MaximumDecimalCount, Field.DecimalCount).ToInt32();
            var rounded = Math.Round(value.Value, digits);

            return(rounded.ToString("F", Provider).Length <= Field.Length.ToInt32());
        }
Beispiel #4
0
        public void CanReadWriteWithMaxDecimalCount()
        {
            var length       = DbaseSingle.MaximumLength;
            var decimalCount = DbaseDecimalCount.Min(DbaseSingle.MaximumDecimalCount,
                                                     new DbaseDecimalCount(length.ToInt32() - 2));
            var sut =
                new DbaseSingle(
                    new DbaseField(
                        _fixture.Create <DbaseFieldName>(),
                        DbaseFieldType.Float,
                        _fixture.Create <ByteOffset>(),
                        length,
                        decimalCount
                        )
                    );

            using (var random = new PooledRandom())
            {
                sut.Value =
                    new DbaseFieldNumberGenerator(random)
                    .GenerateAcceptableValue(sut);

                using (var stream = new MemoryStream())
                {
                    using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                    {
                        sut.Write(writer);
                        writer.Flush();
                    }

                    stream.Position = 0;

                    using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                    {
                        var result = new DbaseSingle(sut.Field);
                        result.Read(reader);

                        Assert.Equal(sut, result, new DbaseFieldValueEqualityComparer());
                    }
                }
            }
        }
Beispiel #5
0
        public void MinReturnsExpectedResult(int left, int right, int expected)
        {
            var result = DbaseDecimalCount.Min(new DbaseDecimalCount(left), new DbaseDecimalCount(right));

            Assert.Equal(new DbaseDecimalCount(expected), result);
        }
Beispiel #6
0
            bool IEqualityComparer <object> .Equals(object left, object right)
            {
                if (left == null && right == null)
                {
                    return(true);
                }
                if (left == null || right == null)
                {
                    return(false);
                }

                if (left is double leftDouble && right is double rightDouble)
                {
                    var doubleDigits = DbaseDecimalCount.Min(_digits, DbaseDouble.MaximumDecimalCount).ToInt32();
                    var leftRounded  = Math.Round(leftDouble, doubleDigits);
                    var rightRounded = Math.Round(rightDouble, doubleDigits);
                    return(Math.Abs(leftRounded - rightRounded) <= Math.Pow(10, -doubleDigits));
                }

                if (left is double? && right is double?)
                {
                    var nullableLeft  = (double?)left;
                    var nullableRight = (double?)right;
                    if (!nullableLeft.HasValue && !nullableRight.HasValue)
                    {
                        return(true);
                    }
                    if (nullableLeft.HasValue || nullableRight.HasValue)
                    {
                        return(false);
                    }
                    var doubleDigits = DbaseDecimalCount.Min(_digits, DbaseDouble.MaximumDecimalCount).ToInt32();
                    var leftRounded  = Math.Round((double)nullableLeft.Value, doubleDigits);
                    var rightRounded = Math.Round((double)nullableRight.Value, doubleDigits);
                    return(Math.Abs(leftRounded - rightRounded) <= Math.Pow(10, -doubleDigits));
                }

                if (left is float leftSingle && right is float rightSingle)
                {
                    var singleDigits = DbaseDecimalCount.Min(_digits, DbaseSingle.MaximumDecimalCount).ToInt32();
                    var leftRounded  = Math.Round(leftSingle, singleDigits);
                    var rightRounded = Math.Round(rightSingle, singleDigits);
                    return(Math.Abs(leftRounded - rightRounded) <= Math.Pow(10, -singleDigits));
                }

                if (left is float? && right is float?)
                {
                    var nullableLeft  = (float?)left;
                    var nullableRight = (float?)right;
                    if (!nullableLeft.HasValue && !nullableRight.HasValue)
                    {
                        return(true);
                    }
                    if (nullableLeft.HasValue || nullableRight.HasValue)
                    {
                        return(false);
                    }
                    var singleDigits = DbaseDecimalCount.Min(_digits, DbaseSingle.MaximumDecimalCount).ToInt32();
                    var leftRounded  = Math.Round((float)nullableLeft.Value, singleDigits);
                    var rightRounded = Math.Round((float)nullableRight.Value, singleDigits);
                    return(Math.Abs(leftRounded - rightRounded) <= Math.Pow(10, -singleDigits));
                }

                return(Equals(left, right));
            }