如何自动测试C程序使用文件?

时间:2012-04-25 11:45:03

标签: testing

我有一个C程序,我需要使用包含一些测试数据的文件对其进行测试并自动输出结果。我怎样才能做到这一点?我应该修改程序还是应该使用C或其他类似Python的程序编写另一个程序?哪个更容易?

这是我的C程序:

int main()
{
    double wh,sh,salary;
    int num = 0;
    int valid = 1;
    printf("Please input two non-negative numbers as a employee's working hours in one week and salary per hour.\n");
    printf("Notice that the salary per hour can't be greater than 1000\nAlso you can't input more than 10 sets of data\n");
    printf("Input them like this:\n20,34\nthen enter the Enter key\n");
    while(scanf("%lf,%lf",&wh,&sh) == 2 && num < 10)
    {
        if(sh <0 || wh <0)
        {
            printf("Salary per hour or salary per hour can't be negative!\n");
        }
        else if(wh > 168)
        {
            printf("Working hours in one week can't be more than 168!\n");
        }
        else if(sh > 1000)
        {
            printf("Salary can't be greater than 1000!\n");
        }
        else
        {
            num ++;
            if(wh <= 40)
            {
                if(sh <= 1000)
                {
                    salary = wh * sh;
                }
                else if(sh > 1000 )
                {
                    salary = wh * 1000;
                }
            }
            else if(wh > 40 && wh <= 50)
            {
                if(sh*1.5 < 1000)
                {
                    salary = 40*sh + (wh-40)*1.5*sh;
                }
                else if(sh*1.5 > 1000 && sh < 1000)
                {
                    salary = 40*sh + (wh-40)*1000;
                }
                else if(sh > 1000)
                {
                    salary = wh * 1000;
                }
            }
            else if(wh > 50)
            {
                if(sh*3 < 1000)
                {
                    salary = 40*sh + 10*1.5*sh + (wh-50)*3*sh;
                }
                else if(sh*1.5 < 1000 && sh*3 >=1000)
                {
                    salary = 40*sh + 10*1.5*sh + (wh-50)*1000;
                }
                else if(sh*1.5 >= 1000 && sh <= 1000)
                {
                    salary = 40*sh + (wh-40)*1000;
                }
                else if(sh > 1000)
                {
                    salary = wh*1000;
                }
            }
            printf("The total working hours is %lf, and salary per hour is %lf, salary is %lf\n",wh,sh,salary);
        }
    }
    return 0;
}

暂时请忽略这些文字。

我想包含测试数据的文件是这样的:

1,2
34,34
67,43
...

我真的不知道这个测试自动化是如何工作的,请帮忙!

1 个答案:

答案 0 :(得分:0)

在cmd提示符

输入

c:\&gt; myprog.exe&lt; input.txt&gt; output.txt的

将所有测试用例放入input.txt

你的输出将在output.txt文件中打印