Example #1
0
        public void ToLengthReturnsExpectedValue()
        {
            var value = _fixture.Create <int>().AsDbaseDecimalCountValue();
            var sut   = new DbaseDecimalCount(value);

            var result = sut.ToLength();

            Assert.Equal(new DbaseFieldLength(sut.ToInt32()), result);
        }
Example #2
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);
            }
        }