生成或预测不同的可能测试用例

时间:2013-08-13 16:55:39

标签: java

我想知道我怎么知道我的程序出错了。下面的问题是一项任务,必须在线提交。但是在10个测试用例中,我的解决方案适用于前6个。其他四个人给出了一个错误答案作为输出。测试用例没有给出或显示给参与者。所以人们可以告诉我如何为下面的问题生成我自己的一组测试用例知道我的代码失败了哪些场景?

说明

Given M busy-time slots of N people, You need to print all the available time slots when all the N people can schedule a meeting for a duration of K minutes.
Event time will be of form HH MM ( where 0 <= HH <= 23 and 0 <= MM <= 59 ), K will be in the form minutes.

    Input Format:

    M K [ M number of busy time slots , K is the duration in minutes ]
    Followed by M lines with 4 numbers on each line.

Each line will be of form StartHH StartMM EndHH EndMM  [ Example 9Am-11Am time slot will be given as 9 00 11 00 ]
An event time slot is of form [Start Time, End Time ) . Which means it inclusive at start time but doesn’t include the end time. 
So an event of form 10 00  11 00 => implies that the meeting start at 10:00 and ends at 11:00, so another meeting can start at 11:00.

    Sample Input:
    5 120
    16 00 17 00
    10 30 14 30
    20 45 22 15
    10 00 13 15
    09 00 11 00

    Sample Output:
    00 00 09 00
    17 00 20 45

    Sample Input:
    8 60
    08 00 10 15
    22 00 23 15
    17 00 19 00
    07 00 09 45
    09 00 13 00
    16 00 17 45
    12 00 13 30
    11 30 12 30

    Sample Output:
    00 00 07 00
    13 30 16 00
    19 00 22 00

Constraints :
1 <= M <= 100

Note: 24 00 has to be presented as 00 00.

我不想要一个解决方案,因为我已经拥有一个(虽然不完全完美)但只是如何生成我自己的一组测试用例?我正在使用Java。我的问题与如何测试我的代码有关?

我要求的解决方案: -

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;


public class Solution {
    private static final Scanner scan = new Scanner(System.in);
    static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    static ArrayList<Integer> stime = new ArrayList<Integer>();
    static StringTokenizer str = null;
    static int limit;
    static int[] timeArr = new int[1440];
    ArrayList<Integer> time = new ArrayList<Integer>();
    public static void main(String args[])
    {
        initialize(timeArr);
        String line = scan.nextLine().toString();
        str = new StringTokenizer(line);
        int trials;
        trials = Integer.parseInt(str.nextToken().toString());
        limit = Integer.parseInt(str.nextToken().toString());
        int startHr,startMin,endHr,endMin,startTime,endTime;
        for(int i=0;i<trials;i++)
            {
                line = scan.nextLine().toString();
                str = new StringTokenizer(line);
                startHr = Integer.parseInt(str.nextToken());
                startMin = Integer.parseInt(str.nextToken());
                startTime = startHr*60 + startMin;
                endHr = Integer.parseInt(str.nextToken());
                endMin = Integer.parseInt(str.nextToken());
                if(endHr==00 && endMin == 00)
                    {
                        endHr = 23;
                        endMin = 60;
                    }
                endTime = (endHr*60 + endMin);
                //System.out.println(startHr + ":" + startMin + " to " + endHr + ":" + endMin + " is " + startTime + " to " + endTime);
                fillSlots(startTime,endTime);

            }
        //display();
        fillMap();
        //display();
        limitMap();
        toProperTime();
    }
    private static void toProperTime() {
        // TODO Auto-generated method stub
        int starthr,startmin,endhr,endmin;
        for(int i=0;i<stime.size();i++)
            {
                starthr = (stime.get(i))/60;
                startmin = (stime.get(i))%60;
                if(map.get(stime.get(i))==1439)
                    {
                        endhr = (map.get(stime.get(i)) + 1)%24;
                        endmin = (map.get(stime.get(i)) + 1)%24;
                    }
                else
                    {
                        endhr = (map.get(stime.get(i)) + 1)/60;
                        endmin = (map.get(stime.get(i)) + 1)%60;
                    }


                System.out.println(getTime(starthr) + " " + getTime(startmin) + " " + getTime(endhr) + " " + getTime(endmin));
            }
    }
    private static void fillMap() {
        // TODO Auto-generated method stub
        int counter = 0;
        int endTime ,startTime,currentTime ;
        boolean loop1 = false;
        boolean loop2 =false;
        while(counter<1440)
            {
                //System.out.println(counter);
                currentTime = counter;
                while((counter<1440)&&timeArr[counter]!=1)
                    {
                        loop1 = true;
                        if((counter==1439) || timeArr[counter] == 1)
                            {
                                endTime = counter;
                                counter++;
                                break;
                            }
                        counter++;
                    }
                if((loop1==true)&&(loop2==false))
                    {
                        stime.add(currentTime);
                        map.put(currentTime,counter-1);
                    }

                while((counter<1440)&&timeArr[counter]!=0)
                    {
                        counter++;
                    }
                loop1 = false;



            }

    }
    private static void displayMap() {
        // TODO Auto-generated method stub
        for(int i=0;i<stime.size();i++)
            System.out.println(stime.get(i) +" till " + map.get(stime.get(i)));
    }
    private static void limitMap() {
        // TODO Auto-generated method stub
        for(int i=0;i<stime.size();i++)
            {
                if((map.get(stime.get(i)) - stime.get(i)) + 1 < limit)
                    {
                        map.remove(stime.get(i));
                        stime.remove(i);
                    }
            }
    }
    private static void display() {
        // TODO Auto-generated method stub
        for(int i=0;i<timeArr.length;i++)
            {
                if((i)%60==0)
                    System.out.println();
                System.out.print(timeArr[i]);
            }
    }
    private static void initialize(int[] timeArr) {
        // TODO Auto-generated method stub
        for(int i=0;i<timeArr.length;i++)
            {
                timeArr[i] = 0;
            }
    }
    private static void fillSlots(int startTime,int endTime) {
        // TODO Auto-generated method stub
        for(int i =startTime;i<endTime;i++)
            {
                timeArr[i] = 1;
            }
    }

    private static String  getTime(int x) {
        // TODO Auto-generated method stub 
        NumberFormat format=NumberFormat.getInstance();  
        format.setMaximumIntegerDigits(2);  
        format.setMinimumIntegerDigits(2);  
        return format.format(x).replace(",","")  ;
    }

}

1 个答案:

答案 0 :(得分:1)

以下是生成测试用例的一种方法:

获取一些方格纸或翻转衬纸侧面。标记垂直线00到24.然后绘制重叠“会议”的模式。您应该能够识别应该测试的会议模式。

可能看起来像这样:

 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
             ------------- (4 to 8)
                      ---- (7 to 8)
                                              --- (3 pm to 4 pm)

哪个成为:

03 60       <- Also try different lengths of desired meeting
04 00 08 00
07 00 08 00
15 00 16 00

例如,

  • 不重叠的行。
  • 仅部分重叠的行。
  • 与另一个完全重叠的行。
  • 从另一个停止的地方开始的行。
  • 相同的行。
  • 从午夜(或午夜结束)开始的行
  • 仅限1行
  • 仅限2行
  • 多行
  • 一些行正好在小时,有些不在小时(做开始和结束)

将这些行转换为测试用例,但处理与输入顺序相关的其他情况:

  • 会议全部按照开始时间顺序排列的情况。 (或结束时间)
  • 会议顺序相反的情况。
  • 会议混淆的情况。一些提升。有些下降。