Exemple #1
0
        public void TestInitialization()
        {
            var a = DoubleMeasurement.Provider.Zero;

            a = new DoubleMeasurement(45);

            Assert.NotNull(a.MeasurementProvider);

            var units = DoubleMeasurement.Provider.ParsableUnits.ToList();
        }
Exemple #2
0
        public static void TestImplicitOperator()
        {
            Random rand = new Random();

            for (int i = 1; i < 1000; i++)
            {
                double            j    = rand.NextDouble() * 1000 * i;
                DoubleMeasurement meas = j;
                double            newJ = meas;

                Assert.Equal(j, newJ);
            }
        }
Exemple #3
0
        public static void TestMeasurementMultiplication()
        {
            Random rand = new Random();

            for (int i = 1; i < 1000; i++)
            {
                DoubleMeasurement j = rand.NextDouble() * 1000;

                foreach (var unit in Distance.Provider.ParsableUnits)
                {
                    double d = j.Multiply(new Distance(i, unit)).Divide(new Distance(i, unit));
                    Assert.Equal(Math.Round(d, 5), Math.Round(j.ToDouble(), 5));
                }
            }
        }
Exemple #4
0
        private bool Atom(out object success, out ParseException failure)
        {
            if (this.CurrentToken.Type == TokenType.Number)
            {
                NumberToken tok = this.CurrentToken as NumberToken;

                if (!this.Advance(TokenType.Number, out failure))
                {
                    success = null;
                    return(false);
                }
                else
                {
                    success = new DoubleMeasurement(tok.Value);
                    failure = null;
                    return(true);
                }
            }
            else if (this.CurrentToken.Type == TokenType.Id)
            {
                IdToken tok = this.CurrentToken as IdToken;

                if (!this.Advance(TokenType.Id, out failure))
                {
                    success = null;
                    return(false);
                }
                else
                {
                    if (this.Units.ContainsKey(tok.StringValue))
                    {
                        success = this.Units[tok.StringValue];
                        return(true);
                    }
                    else
                    {
                        success = null;
                        failure = ParseException.UndefinedUnitDiscovered(tok.StringValue);
                        return(false);
                    }
                }
            }
            else if (this.CurrentToken.Type == TokenType.OpenParen)
            {
                if (!this.Advance(TokenType.OpenParen, out failure))
                {
                    success = null;
                    return(false);
                }
                else if (!this.AddExpression(out success, out failure))
                {
                    return(false);
                }
                else if (!this.Advance(TokenType.CloseParen, out failure))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else if (this.CurrentToken.Type == TokenType.OpenBracket)
            {
                if (!this.Advance(TokenType.OpenBracket, out failure))
                {
                    success = null;
                    return(false);
                }
                else if (!this.AddExpression(out success, out failure))
                {
                    return(false);
                }
                else if (!this.Advance(TokenType.CloseBracket, out failure))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else if (this.CurrentToken.Type == TokenType.Plus || this.CurrentToken.Type == TokenType.Minus)
            {
                if (!this.Advance(this.CurrentToken.Type, out failure))
                {
                    success = null;
                    return(false);
                }
                else if (!this.AddExpression(out success, out failure))
                {
                    return(false);
                }
                else
                {
                    return(Evaluation.ApplyUrnaryOperator(
                               this.AllOperators,
                               this.CurrentToken.Type == TokenType.Plus ? UrnaryOperatorType.Positation : UrnaryOperatorType.Negation,
                               success,
                               out success,
                               out failure
                               ));
                }
            }
            else
            {
                success = null;
                failure = ParseException.UnexpectedCharactersError(this.CurrentToken.StringValue);
                return(false);
            }
        }