コード例 #1
0
        public void testPassingDividenumbers()
        {
            // Normal subtractions
            Assert.That("2", Is.EqualTo(CalculatorAppCore.dividenumbers(4, 2)));
            Assert.That("2,5", Is.EqualTo(CalculatorAppCore.dividenumbers(10, 4)));
            Assert.That("13", Is.EqualTo(CalculatorAppCore.dividenumbers(65, 5)));

            // Divide by zero
            Assert.Throws <DivideByZeroException>(() => CalculatorAppCore.dividenumbers(4, 0));

            /*try
             * {
             *  Assert.That("4", Is.EqualTo(CalculatorAppCore.dividenumbers(4, 0)));
             *  Assert.That(true, Is.EqualTo(false));
             * }
             * catch (DivideByZeroException){}*/



            // Edge cases [-1, 0, 1, maxvalue, minvalue]
            // cases of 1
            Assert.That("0,2", Is.EqualTo(CalculatorAppCore.dividenumbers(1, 5)));                                                             // On left
            Assert.That("3", Is.EqualTo(CalculatorAppCore.dividenumbers(3, 1)));                                                               // On right
            Assert.That("1", Is.EqualTo(CalculatorAppCore.dividenumbers(1, 1)));                                                               // On both
            Assert.That("-1", Is.EqualTo(CalculatorAppCore.dividenumbers(1, -1)));                                                             // with -1
            Assert.That(((float)1 / (float)Int32.MaxValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(1, Int32.MaxValue)));        // with maxvalue
            Assert.That(((float)1 / (float)Int32.MinValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(1, Int32.MinValue)));        // with minvalue

            // -1
            Assert.That("0,25", Is.EqualTo(CalculatorAppCore.dividenumbers(-1, -4)));                                                         // On left
            Assert.That("-78", Is.EqualTo(CalculatorAppCore.dividenumbers(78, -1)));                                                          // On right
            Assert.That("1", Is.EqualTo(CalculatorAppCore.dividenumbers(-1, -1)));                                                            // On both
            Assert.That("-1", Is.EqualTo(CalculatorAppCore.dividenumbers(-1, 1)));                                                            // with 1
            Assert.That((-(float)1 / (float)Int32.MaxValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(-1, Int32.MaxValue)));     // with maxvalue
            Assert.That((-(float)1 / (float)Int32.MinValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(-1, Int32.MinValue)));     // with minvalue

            // 0
            Assert.That("0", Is.EqualTo(CalculatorAppCore.dividenumbers(0, 4)));                                         // On left
            Assert.That("0", Is.EqualTo(CalculatorAppCore.dividenumbers(0, 1)));                                         // with 1
            Assert.That("0", Is.EqualTo(CalculatorAppCore.dividenumbers(0, -1)));                                        // with -1
            Assert.That("0", Is.EqualTo(CalculatorAppCore.dividenumbers(0, Int32.MaxValue)));                            // with maxvalue
            Assert.That("0", Is.EqualTo(CalculatorAppCore.dividenumbers(0, Int32.MinValue)));                            // with minvalue

            // maxvalue
            Assert.That(((float)Int32.MaxValue / (float)3).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, 3)));                                // On left
            Assert.That(((float)45 / (float)Int32.MaxValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(45, Int32.MaxValue)));                              // On right
            Assert.That(((float)Int32.MaxValue / (float)Int32.MaxValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, Int32.MaxValue)));      // On both
            Assert.That(((float)Int32.MaxValue / -(float)1).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, -1)));                              // with -1
            Assert.That(((float)Int32.MaxValue / (float)1).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, 1)));                                // with 1
            Assert.That(((float)Int32.MaxValue / (float)Int32.MinValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, Int32.MinValue)));      // with minvalue

            // minvalue
            Assert.That(((float)Int32.MinValue / -(float)3).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, -3)));                             // On left
            Assert.That((-(float)8 / (float)Int32.MinValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(-8, Int32.MinValue)));                             // On right
            Assert.That(((float)Int32.MinValue / (float)Int32.MinValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, Int32.MinValue)));     // On both
            Assert.That(((float)Int32.MinValue / (float)1).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, 1)));                               // with 1
            Assert.That(((float)Int32.MinValue / -(float)1).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, -1)));                             // with -1
            Assert.That(((float)Int32.MinValue / (float)Int32.MaxValue).ToString(), Is.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, Int32.MaxValue)));     // with maxvalue
        }
コード例 #2
0
        public void testFailingDividenumbers()
        {
            // Normal additions
            Assert.That("75", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(43, 4)));
            Assert.That("25", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(5230, 25)));
            Assert.That("-2", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(33, -35)));

            // Edge cases [-1, 0, 1, maxvalue, minvalue]
            // 1
            Assert.That("326", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(1, 5)));                     // On left
            Assert.That("54", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(543, 1)));                    // On right
            Assert.That("32343", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(1, 1)));                   // On both
            Assert.That("32343", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(1, -1)));                  // with -1
            Assert.That("32343", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(1, Int32.MaxValue)));      // with maxvalue
            Assert.That("32343", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(1, Int32.MinValue)));      // with minvalue


            // -1
            Assert.That("-34", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(-1, -3)));                   // On left
            Assert.That("-3", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(-532, -1)));                  // On right
            Assert.That("52", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(-1, -1)));                    // On both
            Assert.That("52", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(-1, 1)));                     // with 1
            Assert.That("52", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(-1, Int32.MaxValue)));        // with maxvalue
            Assert.That("52", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(-1, Int32.MinValue)));        // with minvalue

            // 0
            Assert.That("432", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, 321)));                   // On left
            Assert.That("32", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, 4242)));                   // On right
            Assert.That("551", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, 12121)));                 // on both
            Assert.That("551", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, 1)));                     // with 1
            Assert.That("551", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, -1)));                    // with -1
            Assert.That("551", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, Int32.MaxValue)));        // with maxvalue
            Assert.That("551", Is.Not.EqualTo(CalculatorAppCore.dividenumbers(0, Int32.MinValue)));        // with minvalue


            // maxvalue
            Assert.That(Int32.MinValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, 3)));                // On left
            Assert.That(Int32.MinValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(3, Int32.MaxValue)));                // On right
            Assert.That(Int32.MinValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, Int32.MaxValue)));   // On both
            Assert.That(Int32.MinValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, 1)));                // with 1
            Assert.That(Int32.MinValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, -1)));               // with -1
            Assert.That(Int32.MinValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MaxValue, Int32.MinValue)));   // with minvalue

            // minvalue
            Assert.That(Int32.MaxValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, -3)));               // On left
            Assert.That(Int32.MaxValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(3, Int32.MinValue)));                // On right
            Assert.That(Int32.MaxValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, Int32.MinValue)));   // On both
            Assert.That(Int32.MaxValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, 1)));                // with 1
            Assert.That(Int32.MaxValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, -1)));               // with -1
            Assert.That(Int32.MaxValue.ToString(), Is.Not.EqualTo(CalculatorAppCore.dividenumbers(Int32.MinValue, Int32.MaxValue)));   // with maxvalue
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine(
                "1. Add two numbers\n" +
                "2. Subtract two numbers\n" +
                "3. Multiply two numbers\n" +
                "4. Divide two numbers\n" +
                "Please select option: ");

            try                                     // Exception catch for parsing from string to int
            {
                string option    = Console.ReadLine();
                int    intOption = Int32.Parse(option);

                Console.WriteLine("Enter first number:");
                string i1    = Console.ReadLine();
                int    inti1 = Int32.Parse(i1);
                Console.WriteLine("Enter second number:");
                string i2    = Console.ReadLine();
                int    inti2 = Int32.Parse(i2);

                switch (intOption)
                {
                case 1:
                    Console.WriteLine(i1 + " + " + i2 + " = " + CalculatorAppCore.addnumbers(inti1, inti2));
                    break;

                case 2:
                    Console.WriteLine(i1 + " - " + i2 + " = " + CalculatorAppCore.subtractnumbers(inti1, inti2));
                    break;

                case 3:
                    Console.WriteLine(i1 + " * " + i2 + " = " + CalculatorAppCore.multiplynumbers(inti1, inti2));
                    break;

                case 4:
                    Console.WriteLine(i1 + " / " + i2 + " = " + CalculatorAppCore.dividenumbers(inti1, inti2));
                    break;

                default:
                    Console.WriteLine("Please select from given options");
                    break;
                }
            }
            catch (ArgumentNullException)        // If input is null
            {
                Console.WriteLine("Please enter a value\n");
            }
            catch (FormatException)              // If input is not in the correct format.
            {
                Console.WriteLine("Please enter a valid value\n");
            }
            catch (OverflowException)            // If input represents a number less than MinValue or greater than MaxValue.
            {
                Console.WriteLine("Please enter a number between " + Int32.MaxValue + " and " + Int32.MinValue + "\n");
            }
            catch (DivideByZeroException)       // If divide is chosen, and i2 is 0
            {
                Console.WriteLine("Please dont divide by 0\n");
            }
            catch (Exception)                    // If something else happens
            {
                Console.WriteLine("There was an error\n");
            }
        }