Example #1
0
        public static DbaseFieldLength GenerateDbaseSingleLengthBetween(this IFixture fixture,
                                                                        DbaseFieldLength minLength, DbaseFieldLength maxLength)
        {
            if (minLength < DbaseSingle.MinimumLength || minLength > DbaseSingle.MaximumLength)
            {
                throw new ArgumentOutOfRangeException(nameof(minLength),
                                                      $"The minimum length needs to be between {DbaseSingle.MinimumLength} and {DbaseSingle.MaximumLength}.");
            }

            if (maxLength < DbaseSingle.MinimumLength || maxLength > DbaseSingle.MaximumLength)
            {
                throw new ArgumentOutOfRangeException(nameof(maxLength),
                                                      $"The maximum length needs to be between {DbaseSingle.MinimumLength} and {DbaseSingle.MaximumLength}.");
            }

            if (minLength > maxLength)
            {
                throw new ArgumentOutOfRangeException(nameof(minLength),
                                                      $"The minimum length {minLength} needs to be less than or equal to the maximum length {maxLength}.");
            }

            using (var random = new PooledRandom())
            {
                return(new DbaseFieldLength(random.Next(minLength.ToInt32(), maxLength.ToInt32())));
            }
        }
Example #2
0
 public static void CustomizeDbaseString(this IFixture fixture)
 {
     fixture.Customize <DbaseString>(
         customization =>
         customization
         .FromFactory <int>(
             value =>
     {
         using (var random = new PooledRandom(value))
         {
             var length = new DbaseFieldLength(random.Next(0, 255));
             return(new DbaseString(
                        new DbaseField(
                            fixture.Create <DbaseFieldName>(),
                            DbaseFieldType.Character,
                            fixture.Create <ByteOffset>(),
                            length,
                            new DbaseDecimalCount(0)
                            ),
                        new string('a', random.Next(0, length.ToInt32()))));
         }
     }
             )
         .OmitAutoProperties());
 }
Example #3
0
 public static void CustomizeDbaseFieldLength(this IFixture fixture, DbaseFieldLength maxLength)
 {
     fixture.Customize <DbaseFieldLength>(
         customization =>
         customization.FromFactory <int>(
             value => new DbaseFieldLength(value.AsDbaseFieldLengthValue(maxLength.ToInt32()))
             ));
 }
Example #4
0
 public static DbaseFieldLength GenerateDbaseInt32LengthLessThan(this IFixture fixture,
                                                                 DbaseFieldLength maxLength)
 {
     using (var random = new PooledRandom())
     {
         return(new DbaseFieldLength(random.Next(maxLength.ToInt32())));
     }
 }
        public void ToInt32ReturnsExpectedValue()
        {
            var value = _fixture.Create <int>().AsDbaseFieldLengthValue();
            var sut   = new DbaseFieldLength(value);

            var result = sut.ToInt32();

            Assert.Equal(value, result);
        }
Example #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);
            }
        }
Example #7
0
        public static DbaseDecimalCount GenerateDbaseSingleDecimalCount(this IFixture fixture, DbaseFieldLength length)
        {
            if (length < DbaseSingle.MinimumLength || length > DbaseSingle.MaximumLength)
            {
                throw new ArgumentOutOfRangeException(nameof(length),
                                                      $"The length needs to be between {DbaseSingle.MinimumLength} and {DbaseSingle.MaximumLength}.");
            }

            using (var random = new PooledRandom())
            {
                return(new DbaseDecimalCount(random.Next(1,
                                                         Math.Min(DbaseSingle.MaximumDecimalCount.ToInt32(), length.ToInt32()) - 2)));
            }
        }
Example #8
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)));
            }
        }
Example #9
0
        public static DbaseFieldLength GenerateDbaseDoubleLengthLessThan(this IFixture fixture,
                                                                         DbaseFieldLength maxLength)
        {
            if (maxLength < DbaseDouble.MinimumLength || maxLength > DbaseDouble.MaximumLength)
            {
                throw new ArgumentOutOfRangeException(nameof(maxLength),
                                                      $"The maximum length needs to be between {DbaseDouble.MinimumLength} and {DbaseDouble.MaximumLength}.");
            }

            using (var random = new PooledRandom())
            {
                return(new DbaseFieldLength(random.Next(DbaseDouble.MinimumLength.ToInt32(), maxLength.ToInt32() + 1)));
            }
        }
Example #10
0
 public DbaseIntegerDigits Minus(DbaseFieldLength other) => new DbaseIntegerDigits(_value - other.ToInt32());
Example #11
0
 public DbaseIntegerDigits Plus(DbaseFieldLength other)
 {
     return(new DbaseIntegerDigits(_value + other.ToInt32()));
 }
Example #12
0
 public ByteOffset Plus(DbaseFieldLength other)
 {
     return(new ByteOffset(_value + other.ToInt32()));
 }
Example #13
0
 public DbaseRecordLength Plus(DbaseFieldLength other)
 {
     return(new DbaseRecordLength(_value + other.ToInt32()));
 }