コード例 #1
0
        public void ConvertBack(object source, byte[] destination, ushort position, ushort length, object parameter)
        {
            if (source == null)
            {
                throw new ArgumentNullException();
            }
            var scale = MathExtentions.PowerOf10((parameter as int?) ?? 0);

            switch (length)
            {
            case 1:
                destination[position] = (byte)(System.Convert.ToDecimal(source) * scale);
                return;

            case 2:
                Array.Copy(BitConverter.GetBytes((ushort)(System.Convert.ToDecimal(source) * scale)), 0, destination, position, length);
                return;

            case 4:
                Array.Copy(BitConverter.GetBytes((uint)(System.Convert.ToDecimal(source) * scale)), 0, destination, position, length);
                return;

            case 8:
                Array.Copy(BitConverter.GetBytes((ulong)(System.Convert.ToDecimal(source) * scale)), 0, destination, position, length);
                return;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #2
0
        public void Max_WhenCalled_ReturnTheGreaterArgument(int a, int b, int expectedResult)
        {
            //Act
            var result = MathExtentions.Max(a, b);

            //Assert
            Assert.That(result, Is.EqualTo(expectedResult));
        }
コード例 #3
0
        //[Ignore("Because I wanted to!")]
        public void Add_WhenCalled_ReturnSumOfArguments()
        {
            //Act
            var result = MathExtentions.Add(1, 2);

            //Assert
            Assert.That(result, Is.EqualTo(3));
        }
コード例 #4
0
        public void GetOddNumbers_LimitIsGreaterThanZero_ReturnOddNumbersUpToLimit()
        {
            var result = MathExtentions.GetOddNumbers(5);

            //Assert.That(result, Is.Not.Empty);
            //Assert.That(result.Count(), Is.EqualTo(3));
            //Assert.That(result, Does.Contain(1));
            //Assert.That(result, Does.Contain(3));
            //Assert.That(result, Does.Contain(5));

            Assert.That(result, Is.EquivalentTo(new[] { 1, 3, 5 }));
            //Assert.That(result, Is.Ordered);
            //Assert.That(result, Is.Unique);
        }
コード例 #5
0
        private void CalculateBTN_Click(object sender, EventArgs e)
        {
            var customer = new Customer();

            try
            {
                var result =
                    MathExtentions.CalculatePercentOfGoalSteps(this.StepGoalTxt.Text, this.TotalStepsTxt.Text);
                ResultLabel.Text = $"You reached {result}% of your goal!";
            }
            catch (ArgumentException exception)
            {
                MessageBox.Show("Your entry was not valid: " + exception.Message);
                ResultLabel.Text = string.Empty;
            }
        }
コード例 #6
0
        public void ConvertBack(object source, byte[] destination, ushort position, ushort length, object parameter = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException();
            }
            var signible = (byte)0x0f;
            var scale    = MathExtentions.PowerOf10((parameter as int?) ?? 0);
            var value    = Math.Truncate(System.Convert.ToDecimal(source) * scale);

            if (value < 0)
            {
                value    = -value;
                signible = (byte)0x0d;
            }
            var valueString = value.ToString();

            if (valueString.Length > length * 2 - 1)
            {
                throw new ArgumentException();
            }
            var zerofillCount = length * 2 - 1 - valueString.Length;

            if (zerofillCount != 0)
            {
                valueString = new string('0', zerofillCount) + valueString;
            }
            for (var i = 0; i < length; i++)
            {
                var digit = 0;
                digit |= byte.Parse(valueString.Substring(i * 2, 1)) << 4;
                if (i == length - 1)
                {
                    digit |= signible;
                }
                else
                {
                    digit |= byte.Parse(valueString.Substring(i * 2, 1));
                }
                destination[position + i] = (byte)digit;
            }
        }
コード例 #7
0
        public object Convert(byte[] source, ushort position, ushort length, object parameter)
        {
            var scale = MathExtentions.PowerOf10((parameter as int?) ?? 0);

            switch (length)
            {
            case 1:
                return(source[position] / scale);

            case 2:
                return(BitConverter.ToUInt16(source, position) / scale);

            case 4:
                return(BitConverter.ToUInt32(source, position) / scale);

            case 8:
                return(BitConverter.ToUInt64(source, position) / scale);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #8
0
        public object Convert(byte[] source, ushort position, ushort length, object parameter)
        {
            var scale  = MathExtentions.PowerOf10((parameter as int?) ?? 0);
            var result = 0m;

            for (var i = 0; i < length; i++)
            {
                if (i == length - 1)
                {
                    result += 1m * (source[position + i] >> 4);
                    if ((source[position + i] & 0x0f) == 0x0d)
                    {
                        result = -result;
                    }
                }
                else
                {
                    result += MathExtentions.PowerOf10((length - i) * 2 - 2) * (source[position + i] >> 4);
                    result += MathExtentions.PowerOf10((length - i) * 2 - 3) * (source[position + i] & 0x0f);
                }
            }
            return(result / scale);
        }
コード例 #9
0
 public void CalculatePercentOfGoalStepsTest(string goalSteps, string actualSteps)
 {
     //var actual = MathExtentions.CalculatePercentOfGoalSteps(goalSteps, actualSteps);
     Assert.That(() => MathExtentions.CalculatePercentOfGoalSteps(goalSteps, actualSteps), Throws.ArgumentException);
 }
コード例 #10
0
        public void CalculatePercentOfGoalStepsTest(string goalSteps, string actualSteps, float expectedResult)
        {
            var actual = MathExtentions.CalculatePercentOfGoalSteps(goalSteps, actualSteps);

            Assert.AreEqual(expectedResult, actual);
        }