Esempio n. 1
0
        public void Test_When_M_Then_1000()
        {
            var roman  = new RomanNumber("M");
            var result = roman.ToDecimal();

            Assert.Equal(1000, result);
        }
        /// <summary>
        /// Parse input
        /// </summary>
        /// <param name="input">input from file</param>
        /// <returns>length</returns>
        public override bool Parse(string input)
        {
            var lexers = input.Split(new[] { " is " }, StringSplitOptions.RemoveEmptyEntries);

            if (lexers.Count() != 2)
            {
                return(false);
            }

            if (lexers[1].Length > 1)
            {
                return(false);
            }

            var roman = RomanNumber.Parse(lexers[1][0]);

            if (roman == null)
            {
                throw new Exception("syntex error.");
            }

            var name = lexers[0].Trim();

            this.Context.Primitives[name] = roman;
            return(true);
        }
Esempio n. 3
0
        public void Test_When_ValuesAreGreaterToLower_Then_SumAll()
        {
            var roman  = new RomanNumber("MDCLXVI");
            var result = roman.ToDecimal();

            Assert.Equal(1666, result);
        }
Esempio n. 4
0
        /// <summary>
        /// Parses the specified input string.
        /// </summary>
        /// <param name="inputStr">The input string.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">inputStr cannot be null - Unable to parse input string</exception>
        public bool Parse(string inputStr)
        {
            var retval = false;

            if (string.IsNullOrEmpty(inputStr))
            {
                throw new ArgumentNullException("inputStr cannot be null - Unable to parse input string");
            }

            var parts = inputStr.Split(new[] { " is " }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 2)
            {
                var leftPart = parts[0].Split(' ');

                if (leftPart.Length > 2)
                {
                    var value = int.Parse(parts[1].Split(' ')[0]);
                    var unit  = parts[1].Split(' ')[1];
                    Transaction.SelectedTransaction = unit;
                    var romanNumber = new RomanNumber();
                    romanNumber = (RomanNumber)romanNumber.Parse(string.Join(" ", leftPart.Take(leftPart.Length - 1)), Transaction.Symbols);
                    var result    = romanNumber.Calculate();
                    var lastUnit  = leftPart.Last();
                    var finalUnit = value / result;
                    Transaction.Units[lastUnit] = finalUnit;
                    retval = true;
                }
            }

            return(retval);
        }
Esempio n. 5
0
        public void Test_When_EmptyString_Then_ReturnZero()
        {
            var roman  = new RomanNumber("");
            var result = roman.ToDecimal();

            Assert.Equal(0, result);
        }
Esempio n. 6
0
        public void Test_When_V_Then_5()
        {
            var roman  = new RomanNumber("V");
            var result = roman.ToDecimal();

            Assert.Equal(5, result);
        }
 public int Parse(RomanNumber romanNumber)
 {
     sum = 0;
     rn  = romanNumber.Number;
     for (var i = 0; i < romanNumber.Number.Length; i++)
     {
         if (i + 1 == romanNumber.Number.Length)
         {
             sum = sum + RomanNumberEquivalent(rn[i]);
             return(sum);
         }
         else
         {
             if (RomanNumberEquivalent(rn[i]) == RomanNumberEquivalent(rn[i + 1]))
             {
                 sum = sum + RomanNumberEquivalent(rn[i]);
             }
             if (RomanNumberEquivalent(rn[i]) > RomanNumberEquivalent(rn[i + 1]))
             {
                 sum = sum + RomanNumberEquivalent(rn[i]);
             }
             if (RomanNumberEquivalent(rn[i]) < RomanNumberEquivalent(rn[i + 1]))
             {
                 sum = sum - RomanNumberEquivalent(rn[i]) + RomanNumberEquivalent(rn[i + 1]);
                 i++;
             }
         }
     }
     return(sum);
 }
Esempio n. 8
0
        public void Test_When_IncorrectValues_Then_ReturnZero()
        {
            var roman  = new RomanNumber("QWERTYUOPASFGHJKÑZBN");
            var result = roman.ToDecimal();

            Assert.Equal(0, result);
        }
Esempio n. 9
0
        public void Test_When_I_Then_One()
        {
            var roman  = new RomanNumber("I");
            var result = roman.ToDecimal();

            Assert.Equal(1, result);
        }
Esempio n. 10
0
        public void Test_When_MCMXLIV_Then_1944()
        {
            var roman  = new RomanNumber("MCMXLIV");
            var result = roman.ToDecimal();

            Assert.Equal(1944, result);
        }
Esempio n. 11
0
        public void Test_When_IV_Then_4()
        {
            var roman  = new RomanNumber("IV");
            var result = roman.ToDecimal();

            Assert.Equal(4, result);
        }
        public void CanHandleFalseRoman()
        {
            int  value  = 0;
            bool result = RomanNumber.TryParse("FF", out value);

            Assert.IsFalse(result);
        }
Esempio n. 13
0
        public void TryParse_UsingNonRomanSymbol_ReturnsFalse([ValueSource(typeof(SymbolGenerator), "GetNonRomanSymbols")] string nonRomanSymbol)
        {
            RomanNumber romanNumber;
            bool        parseResult = RomanNumber.TryParse(nonRomanSymbol, out romanNumber);

            Assert.IsFalse(parseResult);
        }
Esempio n. 14
0
        public void Test_When_D_Then_500()
        {
            var roman  = new RomanNumber("D");
            var result = roman.ToDecimal();

            Assert.Equal(500, result);
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            var rNuber = new RomanNumber();

            rNuber.Number = "MMMCDLXXVIII";

            var converter = new ParserService();
            int solution  = converter.Parse(rNuber);

            Console.WriteLine("This converter is working only with correct format of roman numbersгтвук 4000. It does not check the correctness of roman number.");
            Console.Write("Roman Number: "); Console.Write(rNuber.Number); Console.Write(" is "); Console.Write(solution); Console.WriteLine();

            var rNuber1 = new RomanNumber();

            rNuber1.Number = "MCMXCVII";

            var rNuber2 = new RomanNumber();

            rNuber2.Number = "MMCDXCVIII";
            int convertedNumber1;
            int convertedNumber2;

            convertedNumber2 = converter.Parse(rNuber2);
            convertedNumber1 = converter.Parse(rNuber1);

            if (convertedNumber1 == 1997)
            {
                Console.WriteLine("Unit Test with MCMXCVII=1997 is successfull");
            }

            if (convertedNumber2 == 2498)
            {
                Console.WriteLine("Unit Test with MMCDXCVIII=2498 is successfull");
            }
        }
Esempio n. 16
0
        public void Test_When_LowerChars_Then_SumAll()
        {
            var roman  = new RomanNumber("mdclxvi");
            var result = roman.ToDecimal();

            Assert.Equal(1666, result);
        }
Esempio n. 17
0
        /// <summary>
        /// StrToRomanNumberWithError
        /// </summary>
        public static RopResult <RomanNumber, string> StrToRomanNumberWithError(string input)
        {
            var digitsWithErrs =
                input
                .ToCharArray()
                .Select(CharToRomanDigitWithError);
            var errors =
                digitsWithErrs
                .Where(r => !r.IsSuccess)
                .Select(r => r.FailureValues.First());

            if (errors.Any())
            {
                return(Rop.Fail <RomanNumber, string>(errors));
            }
            else
            {
                var digits =
                    digitsWithErrs
                    .Where(r => r.IsSuccess)
                    .Select(r => r.SuccessValue)
                    .ToList();
                var romanNumber = new RomanNumber(digits);
                return(Rop.Succeed <RomanNumber, string>(romanNumber));
            }
        }
        public ActionResult DoSumming(SummingModel model)
        {
            if (ModelState.IsValid)
            {
                bool        hasErrors = false;
                RomanNumber leftOperand;
                RomanNumber rightOperand;

                if (!RomanNumber.TryParse(model.LeftOperand, out leftOperand))
                {
                    ModelState.AddModelError("LeftOperand", string.Format("'{0}' is not a valid Roman number", model.LeftOperand));
                    hasErrors = true;
                }

                if (!RomanNumber.TryParse(model.RightOperand, out rightOperand))
                {
                    ModelState.AddModelError("RightOperand", string.Format("'{0}' is not a valid Roman number", model.RightOperand));
                    hasErrors = true;
                }

                if (!hasErrors)
                {
                    Abacus abacus = new RomanAbacus(100); // Roman abacus wil be able to handle hundreds of thousands
                    ViewBag.SummingResult = abacus.PerformSumming(model.LeftOperand, model.RightOperand);
                }
            }

            InitSummingView();
            return(View("Index", model));
        }
Esempio n. 19
0
        public void ParseTestForInvalidCharacterSequence()
        {//"I" can be subtracted from "V" and "X" only. "X" can be subtracted from "L" and "C" only. "C" can be subtracted from
         //  "D" and "M" only. "V", "L", and "D" can never be subtracted.
            List <string> invalidNumbers = new List <string>();

            invalidNumbers.Add("IC"); //I can be subtracted only from V and X
            invalidNumbers.Add("IM"); //I can be subtracted only from V and X
            invalidNumbers.Add("ID");
            invalidNumbers.Add("IL");
            invalidNumbers.Add("MXM"); //X can be subtracted from L and C only;
            invalidNumbers.Add("MVX"); //V must never be subtracted
            invalidNumbers.Add("MVC");
            invalidNumbers.Add("MVM");
            invalidNumbers.Add("MLC"); //L must never be subtracted
            invalidNumbers.Add("MLM");
            invalidNumbers.Add("MDM"); //D must never be subtracted
            invalidNumbers.Add("IIV"); //only 1 small value must be subtracted
            invalidNumbers.Add("IVX");
            foreach (var t in invalidNumbers)
            {
                try
                {
                    RomanNumber.Parse(t);
                    Assert.Fail(); //execution reaching this line implies successful parse.
                }
                catch (Exception exception)
                {
                    Assert.AreEqual(typeof(FormatException), exception.GetType());
                    Assert.AreEqual("Invalid number sequence", exception.Message);
                }
            }
        }
Esempio n. 20
0
        public void TryParse_UsingRomanBasicSymbol_ReturnsTrue([ValueSource(typeof(SymbolGenerator), "GetRomanBasicSymbols")] string basicRomanSymbol)
        {
            RomanNumber romanNumber;
            bool        parseResult = RomanNumber.TryParse(basicRomanSymbol, out romanNumber);

            Assert.IsTrue(parseResult);
        }
Esempio n. 21
0
        public void TryParse_UsingValidRomanNumeral_ReturnsTrue(
            [ValueSource(typeof(SymbolGenerator), "GenerateValidRomanNumerals")] string romanNumberAsString)
        {
            RomanNumber romanNumber;
            bool        parseResult = RomanNumber.TryParse(romanNumberAsString, out romanNumber);

            Assert.IsTrue(parseResult);
        }
Esempio n. 22
0
        public void TryParse_UsingGiganticAmountOfThousandRomanSymbols_ReturnsTrue()
        {
            string      giganticAmountOfThousandRomanSymbol = new string('M', 99999);
            RomanNumber romanNumber;
            bool        parseResult = RomanNumber.TryParse(giganticAmountOfThousandRomanSymbol, out romanNumber);

            Assert.IsTrue(parseResult);
        }
        public void CanConvertXIVFromRoman()
        {
            int  value  = 0;
            bool result = RomanNumber.TryParse("XIV", out value);

            Assert.IsTrue(result);
            Assert.AreEqual(14, value);
        }
Esempio n. 24
0
 private void SetStageInfoUI()
 {
     if (InGameManager.IsInGameScene())
     {
         GameObject.Find("Title").GetComponent <TextFader>().SetText(title);
         GameObject.Find("Chapter").GetComponent <TextFader>().
         SetText(("Stage " + RomanNumber.Roman(InGameManager.instance.StageIndex + 1)));
     }
 }
Esempio n. 25
0
 /// <summary>
 /// Преобразование к типу Int
 /// </summary>
 /// <param name="obj">Объект</param>
 /// <param name="defaultValue">Дефолтное значение</param>
 /// <returns></returns>
 public static int FromRomanToInt(this object obj, int defaultValue = 0)
 {
     try {
         var val = defaultValue;
         val = RomanNumber.RomanToArabic(obj + "");
         return(val);
     } catch {
         return(defaultValue);
     }
 }
Esempio n. 26
0
        public void Given_Roman_Number_M_When_Call_Conversion_ToInt_Should_Return_1000()
        {
            // Arrange

            // Act
            int actual = RomanNumber.ToInt("M");

            // Assert
            Assert.AreEqual(1000, actual);
        }
Esempio n. 27
0
        public void repetition()
        {
            IRomanNumber romanNumber = new RomanNumber();

            for (int i = RomanNumber.minRoman; i <= RomanNumber.maxRoman; ++i)
            {
                romanNumber.setRomanNumber(i);
                repetionViolation(romanNumber.getRoman());
            }
        }
Esempio n. 28
0
        void conversion(int number, string roman)
        {
            IRomanNumber romanNumber = new RomanNumber();

            romanNumber.setRomanNumber(number);
            Assert.AreEqual(roman, romanNumber.getRoman(), number + " becomes " + romanNumber.getRoman());

            romanNumber.setRomanNumber(roman);
            Assert.AreEqual(number, romanNumber.getInteger(), roman + " becomes " + romanNumber.getInteger());
        }
Esempio n. 29
0
        public void Given_Roman_Number_VII_When_Call_Conversion_ToInt_Should_Return_7()
        {
            // Arrange

            // Act
            int actual = RomanNumber.ToInt("VII");

            // Assert
            Assert.AreEqual(7, actual);
        }
Esempio n. 30
0
        public void String_constructor_should_assign_new_array_of_symbols_to_field()
        {
            //Arrange
            var expected = new[] { new RomanSymbol("I"), new RomanSymbol("D") };
            //Act
            var target = new RomanNumber("ID");

            //Assert
            target.Symbols.Should().ContainInOrder(expected);
        }
Esempio n. 31
0
 public void Given_Number_when_GetRomanNumber_Return_RomanNumebr(int givenNumebr, string expectedNumber)
 {
     RomanNumber romanNumber=new RomanNumber();
     string roman = romanNumber.GetRomanNumber(givenNumebr);
     Assert.That(roman, Is.EqualTo(expectedNumber));
 }
 /// <summary>
 /// RomanNumberToInt
 /// </summary>
 public static int RomanNumberToInt(RomanNumber number)
 {
     throw new Exception();
 }
 /// <summary>
 /// StrToRomanNumberWithError
 /// </summary>
 public static RopResult<RomanNumber, string> StrToRomanNumberWithError(string input)
 {
     var digitsWithErrs =
         input
             .ToCharArray()
             .Select(CharToRomanDigitWithError);
     var errors =
         digitsWithErrs
             .Where(r => !r.IsSuccess)
             .Select(r => r.FailureValues.First());
     if (errors.Any())
     {
         return Rop.Fail<RomanNumber, string>(errors);
     }
     else
     {
         var digits =
             digitsWithErrs
                 .Where(r => r.IsSuccess)
                 .Select(r => r.SuccessValue)
                 .ToList();
         var romanNumber = new RomanNumber(digits);
         return Rop.Succeed<RomanNumber, string>(romanNumber);
     }
 }
        public void TestRomanNumberToInt()
        {
            var roman = new RomanNumber(new[]
            {
                RomanDigit.X,
                RomanDigit.V,
                RomanDigit.X
            });

            var actual = RomanNumberToInt(roman);
            var expected = 16;
            Assert.AreEqual(expected, actual);
        }
 /// <summary>
 /// RomanNumberToInt
 /// </summary>
 public static int RomanNumberToInt(RomanNumber number)
 {
     return number.Digits.Select(DigitToInt).Sum();
 }