コード例 #1
0
        public void ToLengthReturnsExpectedValue()
        {
            var value = _fixture.Create <int>().AsDbaseFieldCompatibleDbaseIntegerDigitsValue();
            var sut   = new DbaseIntegerDigits(value);

            var result = sut.ToLength();

            Assert.Equal(new DbaseFieldLength(sut.ToInt32()), result);
        }
コード例 #2
0
        public void ToInt32ReturnsExpectedValue()
        {
            var value = _fixture.Create <int>().AsDbaseIntegerDigitsValue();
            var sut   = new DbaseIntegerDigits(value);

            var result = sut.ToInt32();

            Assert.Equal(value, result);
        }
コード例 #3
0
        public void LessThanOrEqualToDbaseFieldLengthOperatorReturnsExpectedValue(int left, int right, bool expected)
        {
            var sut   = new DbaseIntegerDigits(left);
            var other = new DbaseIntegerDigits(right);

            var result = sut <= other;

            Assert.Equal(expected, result);
        }
コード例 #4
0
        public void InequalityOperatorReturnsExpectedValue(int left, int right, bool expected)
        {
            var sut   = new DbaseIntegerDigits(left);
            var other = new DbaseIntegerDigits(right);

            var result = sut != other;

            Assert.Equal(expected, result);
        }
コード例 #5
0
        public void CompareToDbaseIntegerDigitsReturnsExpectedResult(int left, int right, int expected)
        {
            var sut   = new DbaseIntegerDigits(left);
            var other = new DbaseIntegerDigits(right);

            var result = sut.CompareTo(other);

            Assert.Equal(expected, result);
        }
コード例 #6
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);
        }
コード例 #7
0
        private float?GeneratePositiveSingleValue(DbaseField field)
        {
            if (field.Length < DbaseSingle.PositiveValueMinimumLength || !SingleSupportedFieldTypes.Contains(field.FieldType))
            {
                return(null);
            }

            var digits = DbaseIntegerDigits.Min(field.PositiveIntegerDigits, DbaseSingle.MaximumIntegerDigits);
            var number = GeneratePositiveNumber(digits, field.DecimalCount);

            return(Single.Parse(
                       number,
                       NumberStyles.AllowDecimalPoint,
                       new NumberFormatInfo
            {
                NumberDecimalDigits = field.DecimalCount.ToInt32(),
                NumberDecimalSeparator = "."
            }));
        }
コード例 #8
0
ファイル: DbaseFieldTests.cs プロジェクト: yreynhout/shaperon
        public void CreateInt16FieldReturnsExpectedResult()
        {
            var name   = _fixture.Create <DbaseFieldName>();
            var length = _fixture.Create <DbaseFieldLength>();
            var positiveIntegerDigits = new DbaseIntegerDigits(length.ToInt32());
            var negativeIntegerDigits = positiveIntegerDigits != new DbaseIntegerDigits(0)
                ? positiveIntegerDigits.Minus(new DbaseIntegerDigits(1))
                : new DbaseIntegerDigits(0);
            var result = DbaseField.CreateInt16Field(
                name,
                length);

            Assert.Equal(name, result.Name);
            Assert.Equal(DbaseFieldType.Number, result.FieldType);
            Assert.Equal(ByteOffset.Initial, result.Offset);
            Assert.Equal(length, result.Length);
            Assert.Equal(new DbaseDecimalCount(0), result.DecimalCount);
            Assert.Equal(positiveIntegerDigits, result.PositiveIntegerDigits);
            Assert.Equal(negativeIntegerDigits, result.NegativeIntegerDigits);
        }
コード例 #9
0
        private double?GenerateNegativeDoubleValue(DbaseField field)
        {
            if (field.Length < DbaseDouble.NegativeValueMinimumLength || !DoubleSupportedFieldTypes.Contains(field.FieldType))
            {
                return(null);
            }

            var digits = DbaseIntegerDigits.Min(field.NegativeIntegerDigits, DbaseDouble.MaximumIntegerDigits);
            var number = GenerateNegativeNumber(digits, field.DecimalCount);

            return(Double.Parse(
                       number,
                       NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
                       new NumberFormatInfo
            {
                NumberDecimalDigits = field.DecimalCount.ToInt32(),
                NumberDecimalSeparator = ".",
                NegativeSign = "-"
            }));
        }
コード例 #10
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);
            }
        }
コード例 #11
0
        public void ToLengthThrowsWhenNoDigits()
        {
            var sut = new DbaseIntegerDigits(0);

            Assert.Throws <InvalidOperationException>(() => sut.ToLength());
        }
コード例 #12
0
        public void MinReturnsExpectedResult(int left, int right, int expected)
        {
            var result = DbaseIntegerDigits.Min(new DbaseIntegerDigits(left), new DbaseIntegerDigits(right));

            Assert.Equal(new DbaseIntegerDigits(expected), result);
        }