示例#1
0
        private char[] GeneratePositiveNumber(DbaseIntegerDigits digits, DbaseDecimalCount decimals)
        {
            var data = new char[digits.ToInt32() + 1 + decimals.ToInt32()];

            for (var index = 0; index < digits.ToInt32(); index++)
            {
                data[index] = _random.Next(0, 10).ToString()[0];
            }
            data[digits.ToInt32()] = '.';
            for (var index = 0; index < decimals.ToInt32(); index++)
            {
                data[digits.ToInt32() + 1 + index] = _random.Next(0, 10).ToString()[0];
            }
            return(data);
        }
示例#2
0
 public static void CustomizeDbaseDecimalCount(this IFixture fixture, DbaseDecimalCount maximum)
 {
     fixture.Customize <DbaseDecimalCount>(
         customization =>
         customization.FromFactory <int>(
             value => new DbaseDecimalCount(value.AsDbaseDecimalCountValue(maximum.ToInt32()))
             ));
 }
示例#3
0
        public void ToInt32ReturnsExpectedValue()
        {
            var value = _fixture.Create <int>().AsDbaseDecimalCountValue();
            var sut   = new DbaseDecimalCount(value);

            var result = sut.ToInt32();

            Assert.Equal(value, result);
        }
示例#4
0
 public bool Equals(DbaseDouble left, DbaseDouble right)
 {
     if (left == null && right == null)
     {
         return(true);
     }
     if (left == null || right == null)
     {
         return(false);
     }
     return(left.Field.Equals(right.Field) &&
            Math.Abs(left.Value - right.Value) < Math.Pow(10, -_precision.ToInt32()));
 }
示例#5
0
        public static DbaseDecimalCount GenerateDbaseDoubleDecimalCount(this IFixture fixture,
                                                                        DbaseDecimalCount minimum, DbaseFieldLength length)
        {
            if (length > DbaseDouble.MaximumLength)
            {
                throw new ArgumentOutOfRangeException(nameof(length),
                                                      $"The length ({length}) can not exceed the maximum length ({DbaseDouble.MaximumLength}).");
            }

            using (var random = new PooledRandom())
            {
                return(new DbaseDecimalCount(random.Next(minimum.ToInt32(),
                                                         Math.Min(DbaseDouble.MaximumDecimalCount.ToInt32() + 1, length.ToInt32()) - 2)));
            }
        }
示例#6
0
        public DbaseField(DbaseFieldName name, DbaseFieldType fieldType, ByteOffset offset, DbaseFieldLength length,
                          DbaseDecimalCount decimalCount)
        {
            if (!Enum.IsDefined(typeof(DbaseFieldType), fieldType))
            {
                throw new ArgumentException(
                          $"The field type {fieldType} of field {name} is not supported.",
                          nameof(fieldType));
            }

            switch (fieldType)
            {
            case DbaseFieldType.Character:
                if (decimalCount.ToInt32() != 0)
                {
                    throw new ArgumentException(
                              $"The character field {name} decimal count ({decimalCount}) must be set to 0.",
                              nameof(decimalCount));
                }

                break;

            case DbaseFieldType.DateTime:
                if (length.ToInt32() != 15)
                {
                    throw new ArgumentException($"The datetime field {name} length ({length}) must be set to 15.",
                                                nameof(length));
                }

                if (decimalCount.ToInt32() != 0)
                {
                    throw new ArgumentException(
                              $"The datetime field {name} decimal count ({decimalCount}) must be set to 0.",
                              nameof(decimalCount));
                }

                break;

            case DbaseFieldType.Number:
                if (length > DbaseDouble.MaximumLength)
                {
                    throw new ArgumentException(
                              $"The number field {name} length ({length}) must be less than or equal to {DbaseDouble.MaximumLength}.",
                              nameof(length));
                }

                if (decimalCount.ToInt32() != 0)
                {
                    if (length < DbaseDouble.MinimumLength)
                    {
                        throw new ArgumentException(
                                  $"The number field {name} length ({length}) must be at least {DbaseDouble.MinimumLength}.",
                                  nameof(length));
                    }

                    if (decimalCount > DbaseDouble.MaximumDecimalCount)
                    {
                        throw new ArgumentException(
                                  $"The number field {name} decimal count ({decimalCount}) must be less than or equal to {DbaseDouble.MaximumDecimalCount}.",
                                  nameof(decimalCount));
                    }

                    if (decimalCount.ToInt32() > length.ToInt32() - 2)
                    {
                        throw new ArgumentException(
                                  $"The number field {name} decimal count ({decimalCount}) must be 2 less than its length ({length}).",
                                  nameof(decimalCount));
                    }
                }

                break;

            case DbaseFieldType.Float:
                if (length > DbaseSingle.MaximumLength)
                {
                    throw new ArgumentException(
                              $"The float field {name} length ({length}) must be less than or equal to {DbaseSingle.MaximumLength}.",
                              nameof(length));
                }

                if (decimalCount.ToInt32() != 0)
                {
                    if (length < DbaseSingle.MinimumLength)
                    {
                        throw new ArgumentException(
                                  $"The number field {name} length ({length}) must be at least {DbaseSingle.MinimumLength}.",
                                  nameof(length));
                    }

                    if (decimalCount > DbaseSingle.MaximumDecimalCount)
                    {
                        throw new ArgumentException(
                                  $"The float field {name} decimal count ({decimalCount}) must be less than or equal to {DbaseSingle.MaximumDecimalCount}.",
                                  nameof(decimalCount));
                    }

                    if (decimalCount.ToInt32() > length.ToInt32() - 2)
                    {
                        throw new ArgumentException(
                                  $"The float field {name} decimal count ({decimalCount}) must be 2 less than its length ({length}).",
                                  nameof(decimalCount));
                    }
                }

                break;

            case DbaseFieldType.Logical:
                if (decimalCount.ToInt32() != 0)
                {
                    throw new ArgumentException(
                              $"The logical field {name} decimal count ({decimalCount}) must be set to 0.",
                              nameof(decimalCount));
                }

                if (length.ToInt32() != 1)
                {
                    throw new ArgumentException(
                              $"The logical field {name} length ({length}) must be set to 1.",
                              nameof(length));
                }

                break;
            }

            Name         = name;
            FieldType    = fieldType;
            Offset       = offset;
            Length       = length;
            DecimalCount = decimalCount;

            if (FieldType == DbaseFieldType.Number || FieldType == DbaseFieldType.Float)
            {
                PositiveIntegerDigits =
                    DecimalCount.ToInt32() != 0
                        ? new DbaseIntegerDigits(
                        Length
                        .Minus(DecimalCount.ToLength())
                        .Minus(DecimalSeparatorLength)
                        .ToInt32()
                        )
                        : new DbaseIntegerDigits(Length.ToInt32());

                NegativeIntegerDigits =
                    PositiveIntegerDigits != new DbaseIntegerDigits(0)
                        ? PositiveIntegerDigits
                    .Minus(SignLength)
                        : new DbaseIntegerDigits(0);
            }
            else
            {
                PositiveIntegerDigits = new DbaseIntegerDigits(0);
                NegativeIntegerDigits = new DbaseIntegerDigits(0);
            }
        }
示例#7
0
 public bool Equals(DbaseDecimal left, DbaseDecimal right)
 {
     if (left == null && right == null)
     {
         return(true);
     }
     if (left == null || right == null)
     {
         return(false);
     }
     if (left.Value == null && right.Value == null)
     {
         return(left.Field.Equals(right.Field));
     }
     if (left.Value == null || right.Value == null)
     {
         return(false);
     }
     return(left.Field.Equals(right.Field) &&
            Math.Abs(left.Value.Value - right.Value.Value) < Convert.ToDecimal(Math.Pow(10, -_precision.ToInt32())));
 }