C# 测试计算器的方法

C# 测试计算器的方法,c#,unit-testing,testing,uwp,C#,Unit Testing,Testing,Uwp,我有一个我用UWP做的计算器。它可以加,乘,减,除法,把摄氏度转换成华氏度,反之亦然 我的问题是,我需要一种方法来测试它,这样我就可以在一种测试方法中进行加、减、除、乘。 同时还要进行测试,这样我就可以得到一个摄氏度的值,然后把它转换成华氏度,所有这些都在一个测试方法中 我可以改变它,使我的一些方法成为公共的而不是私有的,但我不知道我应该如何测试其中的一些 我的测试方法位于代码的顶部 using NUnit.Framework; using System; using System.Data;

我有一个我用UWP做的计算器。它可以加,乘,减,除法,把摄氏度转换成华氏度,反之亦然

我的问题是,我需要一种方法来测试它,这样我就可以在一种测试方法中进行加、减、除、乘。 同时还要进行测试,这样我就可以得到一个摄氏度的值,然后把它转换成华氏度,所有这些都在一个测试方法中

我可以改变它,使我的一些方法成为公共的而不是私有的,但我不知道我应该如何测试其中的一些

我的测试方法位于代码的顶部

using NUnit.Framework;
using System;
using System.Data;
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Calc
{
    /// <summary>
    /// A calculator, namespace start. 
    /// </summary>
    /// 

    
    /// Public start, mainpage. 
    public sealed partial class MainPage : Page
    {


       
        /// Input for Celcius/Farh.
        string input;


        /// <summary>
        /// Testing celcius / F.
        /// </summary>
        [TestMethod]
         private void TestDifferentScales()
          {
          
          }


        /// <summary>
        /// Test method - testing all functions regarding mathematical equations!
        /// </summary>
        [TestMethod()]
        public void AdditionTest()
        {
            
           
            
        }




        /// <summary>
        /// Start main program!
        /// </summary>
        public MainPage()
        {
            //Startar allt.
            this.InitializeComponent();
        }


        /// <summary>
        /// Button for number 1.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            textBox.Text += '1';
            input += "1";
          
        }

        /// <summary>
        /// Button for number 2.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            textBox.Text += '2';
            input += "2";

        }


        /// <summary>
        /// Button for number 3.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            textBox.Text += '3';
            input += "3";
        }


        /// <summary>
        /// Button for number 4.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            textBox.Text += '4';
            input += "4";

        }


        /// <summary>
        /// Button for number 5.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            textBox.Text += '5';
            input += "5";
        }



        /// <summary>
        /// Button for number 6.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            textBox.Text += '6';
            input += "6";

        }



        /// <summary>
        /// Button for number 7.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            textBox.Text += '7';
            input += "7";

        }



        /// <summary>
        /// Button for number 8.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            textBox.Text += '8';
            input += "8";
        }



        /// <summary>
        /// Button for number 9.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            textBox.Text += '9';
            input += "9";

        }



        /// <summary>
        /// Button for numbers 42 "Marcus".
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            textBox.Text += "42";
            input += "42";
        }



        /// <summary>
        /// Button for number 0.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_10(object sender, RoutedEventArgs e)
        {
            textBox.Text += '0';
            input += "0";
        }



        /// <summary>
        /// Button for multiplication of equation "F = M*A".
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_11(object sender, RoutedEventArgs e)
        {

            /* F = M * A 
             * Denna ekvation är simpel och man lär sig den redan i början utav Fysik 1.
             * Ekvationen är inte specifik, så som tex "F = M * G" då gravitationen kan bli ett "fixed" värde som 
             * går mellan 9,802 och 9,820.
             * Anledningen till varför denna är så simpel är för att jag ville bevisa att det gick att ha många komplikaitoner 
             * i ett knapp tryck. Så denna skulle lika gärna kunnat vara knappen för multiplikation.
             */
            textBox.Text += '*';

        }



        /// <summary>
        /// Button for addition.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickPlus(object sender, RoutedEventArgs e)
        {

            /* "Exceptions without Execeptions"
             * Här försökte jag att gå runt utan att behöva använda en 
             * try/catch eller Exception inför min "data table" på min "=" knapp.
             * Men, som sagt, det är ingen skillnad här och på min knapp för "F = M * A".
             * Det finns ju klara skillander, jag har försökt att se till så att jag går tillbaka ett
             * steg ifall det finns flera matematiska karaktärer. 
             * 
             * MEN: Det skapar problem då jag fortfarande ville kunna köra "6--1 = 7" utan att krasha allt.
             * 
             */
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '+';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '+';
            }
        }


        /// <summary>
        /// Button for subtraction. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickMinus(object sender, RoutedEventArgs e)
        {
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '-';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '-';
            }
        }



        /// <summary>
        /// Button for multiplication.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickTimes(object sender, RoutedEventArgs e)
        {
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '*';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '*';
            }
        }
        

        /// <summary>
        /// Button for division. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickDEv(object sender, RoutedEventArgs e)
        {
            var last = textBox.Text;
            if (last[last.Length - 1] != '+' || last[last.Length - 1] != '-' || last[last.Length - 1] != '*' || last[last.Length - 1] != '/')
            {
                textBox.Text += '/';
            }
            else
            {
                textBox.Text = last.Remove(last.Length - 1, 1);
                textBox.Text += '/';
            }
        }


        /// <summary>
        /// Button for "Equal", the equal button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickEqual(object sender, RoutedEventArgs e)
        {
            /* Exception.
             * Jag prövade att unit testa här med en thread timer för att kunna 
             * skriva ett meddelande i boxen typ "Error: Please try again" 
             * och sen rensa allt, men det gav error, det funkade men jag ville
             * inte ha något error med så jag tog bort den från min "catch".
             * 
             */
            try
            {
             object q = new DataTable().Compute(textBox.Text, null);
                        textBox.Text = q.ToString();
            }
            catch
            {
             
                /*
                 * Här återställer jag bara allt.  
                 */
           
                textBox.Text = string.Empty;
                input = string.Empty;
            }
           
        }



        /// <summary>
        /// Button for reset of calcuator.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ClickReset(object sender, RoutedEventArgs e)
        {

            /*
             * Åter ställer här på "ClickReset" också.
             */
            textBox.Text = string.Empty;
            input = string.Empty;

        }

        /// <summary>
        /// Button from  F to C.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_fc_(object sender, RoutedEventArgs e)
        {

            /*
             * Här så var jag tvungen att gå fram och tillbaka med att 
             * konvertera saker från och till int och string. 
             * Multiplicerade och addade 32 eller tog bort 32 för att
             * få rätt värden i slutet. 
             */
            int value = int.Parse(input);
            int after = value - 32; 
            int a = (int)(decimal.Divide(after, (decimal)1.8));
            string answer = a.ToString();
            textBox.Text = string.Empty;
            textBox.Text = answer;
        }


        /// <summary>
        /// Button from C to F.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Button_Click_cf_(object sender, RoutedEventArgs e)
        {
            int value = int.Parse(input);
            int a = (int)(decimal.Multiply(value, (decimal)1.8) + 32);
            string answer = a.ToString();
            textBox.Text = string.Empty;
            textBox.Text = answer; 
        }
    }
}
使用NUnit.Framework;
使用制度;
使用系统数据;
运用系统反思;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间计算
{
/// 
///一个计算器,名称空间开始。
/// 
/// 
///公共开始,主页。
公共密封部分类主页面:第页
{
///Celcius/Farh的输入。
字符串输入;
/// 
///测试celcius/F。
/// 
[测试方法]
私有void TestDifferentScales()
{
}
/// 
///测试方法-测试有关数学方程的所有函数!
/// 
[TestMethod()]
public void AdditionTest()
{
}
/// 
///启动主程序!
/// 
公共主页()
{
//Startar allt。
this.InitializeComponent();
}
/// 
///1号按钮。
/// 
/// 
/// 
私有无效按钮\u单击(对象发送者,路由目标e)
{
textBox.Text+=“1”;
输入+=“1”;
}
/// 
///2号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
textBox.Text+=“2”;
输入+=“2”;
}
/// 
///3号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 2(对象发送方,路由目标)
{
textBox.Text+='3';
输入+=“3”;
}
/// 
///4号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 3(对象发送者,路由目标)
{
textBox.Text+='4';
输入+=“4”;
}
/// 
///5号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 4(对象发送者,路由目标)
{
textBox.Text+=“5”;
输入+=“5”;
}
/// 
///6号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 5(对象发送者,路由目标)
{
textBox.Text+='6';
输入+=“6”;
}
/// 
///7号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 6(对象发送者,路由目标)
{
textBox.Text+=“7”;
输入+=“7”;
}
/// 
///8号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 7(对象发送者,路由目标)
{
textBox.Text+='8';
输入+=“8”;
}
/// 
///9号的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 8(对象发送者,路由目标)
{
textBox.Text+='9';
输入+=“9”;
}
/// 
///42号按钮“Marcus”。
/// 
/// 
/// 
私有无效按钮\u单击\u 9(对象发送方,路由目标)
{
textBox.Text+=“42”;
输入+=“42”;
}
/// 
///数字0的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 10(对象发送者,路由目标)
{
textBox.Text+=“0”;
输入+=“0”;
}
/// 
///用于公式“F=M*A”乘法的按钮。
/// 
/// 
/// 
私有无效按钮\u单击\u 11(对象发送者,路由目标)
{
/*F=M*A
*Denna Ekavationär simpel och man lär sig den redan i början utav Fysik 1。
*根据具体情况,såsom tex“F=M*G”då引力可以“固定”vårde som
*går mellan 9802 och 9820。
*直到varför dennaår såsimpelår för att jag ville bevisa att det gick att ha många Komplikaitor
*我认为纳普·特里克·斯丹娜·斯库纳·库纳特·瓦拉·纳彭·弗尔是一个多重形象。
*/
textBox.Text+='*';
}
/// 
///添加按钮。
/// 
/// 
/// 
私有无效按钮\u ClickPlus(对象发送者,路由目标)
{
/*“没有例外的例外”
*赫尔·弗尔斯克特·贾格·格朗特·乌塔·贝赫瓦·安达·恩
*try/catch eller Exception inför min“数据表”påmin=“knapp。
*男性,som sagt,detär ingen skillnad här och påmin knapp för“f=M*A”。
*芬兰人从克拉拉·斯基兰德尔开始,一直到蒂尔巴卡为止
*芬兰人弗莱拉·马特马蒂斯卡·卡拉克特·雷尔。
* 
*男人:德贾格·福特法兰德维尔·库纳·科拉6--1=7“的斯卡帕问题。
* 
*/
var last=textBox.Text;
如果(last[last.Length-1]!='+'| | last[last.Length-1]!='-'| | | last[last.Length-1]!='*'| | last[last.Length-1]!='/'))
{
//real example in world
string infix = "(((13+26)*12)-15+15*(20/6))";
List<string> arrayPostfix;
            
if (ValidBraces(infix))
{
    convertToPostfixArray(ref infix, out arrayPostfix);
    double result = calculate(arrayPostfix);
    Console.WriteLine(result);
}
else
    Console.WriteLine("invalid experssion***********************");
static bool ValidBraces(string infix)
{
   bool valid = true;
   int i = 0;
   Stack<char> symbls = new Stack<char>();

   while (i < infix.Length)
   {
       if (infix[i] == '(')
           symbls.Push(infix[i]);

       if (infix[i] == ')')
           if (symbls.Count == 0)
           {
              valid = false;
              break;
           }
           else
           {
               char ch = symbls.Pop();
               if (ch != '(')
               {
                  valid = false;
                  break;
               }
           }

           i++;
   }

   if (symbls.Count > 0)
       valid = false;

   return valid;
}
static bool convertToPostfixArray(ref string infix, out List<string> postfix)
{
    int prio = 0;
    postfix = new List<string>();
    Stack<Char> s1 = new Stack<char>();
    string value = "";
    for (int i = 0; i < infix.Length; i++)
    {
        char ch = infix[i];
        if (ch == '(')
        {  
           s1.Push(ch);                     
        }
        else if (ch == ')')
        {
           if (!string.IsNullOrEmpty(value))
           {
               postfix.Add(value);
               value = string.Empty;
           }
           while (s1.Peek() != '(')
           {
               postfix.Add(s1.Pop().ToString());
           }
           s1.Pop();
        }
        else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
        {
            if (!string.IsNullOrEmpty(value))
            {
                postfix.Add(value);
                value = string.Empty;
            }
            if (s1.Count <= 0)
            {
                s1.Push(ch);
            }
            else
            {
                if (s1.Peek() == '*' || s1.Peek() == '/')
                    prio = 1;
                else
                    prio = 0;
                if (prio == 1)
                {
                   char c = s1.Peek();
                   if (c != '(')
                       postfix.Add(s1.Pop().ToString());
                   i--;
                }
                else
                {
                    if (ch == '+' || ch == '-')
                    {
                        char c = s1.Peek();
                        if (c != '(')
                          postfix.Add(s1.Pop().ToString());
                        s1.Push(ch);
                    }
                    else
                       s1.Push(ch);
                }
            }
        }
        else
        {
            value += ch.ToString();
        }
    }

    if (!string.IsNullOrEmpty(value))
    {
        postfix.Add(value);
        value = string.Empty;
    }

    int len = s1.Count;
    for (int j = 0; j < len; j++)
    {
       char c = s1.Pop();
       if (c != '(')
          postfix.Add(c.ToString());
    }
    return true;
}
static double calculate(List<string> postfix)
{
   Stack<double> stack = new Stack<double>();
   foreach (string item in postfix)
   {
       double answer = 0;
       if (item == "+" || item == "-" || item == "*" || item == "/")
       {
          char charVal = System.Convert.ToChar(item);
          double operand1 = ((Double)stack.Pop());
          double operand2 = ((Double)stack.Pop());

          answer = Solve(charVal, operand1, operand2);
          stack.Push((answer));
       }
       else
       {
           stack.Push(Convert.ToDouble(item));
       }
   }
   return stack.Pop();
}
public static double Solve(char operation, double operand1, double operand2)
{
    double answer = 0;
    switch (operation)
    {
        case '+':
          answer = operand1 + operand2;
          break;
        case '-':
          answer = operand2 - operand1;
          break;
        case '*':
          answer = operand1 * operand2;
          break;
        case '/':
          answer = operand2 / operand1;
          break;
    }
    return answer;
}