Java 最佳拼图测试用例

Java 最佳拼图测试用例,java,testing,Java,Testing,我已经为此编写了以下代码。我已经列出了一个测试输入列表,我认为它涵盖了所有的情况,代码通过了所有这些情况。但是机器人不接受我的回答。看在FSM的份上,我需要结束这件事,这样我今晚就可以睡觉了。有人能推荐一个我的代码会失败的测试用例吗 主要类别: package bestBefore; import java.text.SimpleDateFormat; import java.text.DateFormat; import java.util.List; import java.util.Ar

我已经为此编写了以下代码。我已经列出了一个测试输入列表,我认为它涵盖了所有的情况,代码通过了所有这些情况。但是机器人不接受我的回答。看在FSM的份上,我需要结束这件事,这样我今晚就可以睡觉了。有人能推荐一个我的代码会失败的测试用例吗

主要类别:

package bestBefore;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class BestBefore {

    private String d_inputDate;
    private List<Date> d_validDateList ;
    private static String s_dateFormat = "yyyy-MM-dd";
    private static DateFormat s_df ;
    private static Calendar s_upperBoundary ;
    private static Calendar s_lowerBoundary ; 

    static {
        s_df = new SimpleDateFormat(s_dateFormat);
        s_df.setLenient(false);
        s_upperBoundary = new GregorianCalendar();

            s_upperBoundary.clear();
        s_upperBoundary.set(3000,Calendar.JANUARY,1);
        s_lowerBoundary = new GregorianCalendar();
            s_lowerBoundary.clear();
        s_lowerBoundary.set(1999,Calendar.DECEMBER,31);
    }


    public static void main ( String[] args ) {

        String inputDate = "";;
        if ( args.length == 0 ){
            //sop("Usage: java BestBefore <input_date>");
            try {
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                inputDate = stdin.readLine();
            }catch (IOException ioe) {
                sop(ioe.getMessage());
            }
        }else{
            inputDate = args[0];
        }

        BestBefore bb = new BestBefore(inputDate);
        sop(bb.getEarliestDate());
    }

    public BestBefore ( String inputDate ) {
        d_inputDate = inputDate;
        d_validDateList = new ArrayList<Date> ();
        processDate();
    }


    private void processDate() {

        if ( d_inputDate == null)
            return;

        String[] dates = d_inputDate.split("/");
        int[] datesInt = new int[3];  // to store the parseInts

        if( dates.length !=3 ) {
            return;
        }

        int yearInd = -1;
        try {
        // check if any 4 digits , then that is the year and also integer parse
        for ( int i = 0 ; i < dates.length ; i ++ ) {

            if(dates[i].length() == 3)
                return;

            //Covnert to int 
            datesInt[i] = Integer.parseInt(dates[i].trim());

            if (datesInt[i] > 31){ // if number greater than 31 then year  : TODO check month too and avoid looops
                if ( yearInd == -1) {
                    yearInd = i;
                }
                else{
                    return;
                }
            }


            // Check for negative numbers
            if ( datesInt[i] < 0 ) {
                return;
            }
        }


        //ProcesDates
        if ( yearInd != -1 )  // we have a year / cuts down number of date parses by factor of 3
            haveYear(datesInt,yearInd);
        else
            noYear(datesInt);

    }catch (Exception e ) { // not a number
        return;
    }

    }


    private void haveYear(int[] dates, int yearInd ) {
        String dateToParse = padYear(dates[yearInd]+"") + "-";
        int[] mD = new int[2];

        for ( int i = 0,j = 0   ; i < dates.length ; i ++ ) {
            if ( i == yearInd ) 
                continue;
            mD[j] = dates[i] ;
            j++;
        }

        // parse date and rotate  and repeat
        for ( int i = 0 ; i < mD.length ; i ++ ) {
            try { 
                Date date = s_df.parse( dateToParse+mD[0]+"-"+mD[1] );
                if ( (s_lowerBoundary.getTime()).before(date) && (s_upperBoundary.getTime().after(date)))
                    d_validDateList.add(date );
            }catch ( Exception e ) {
                // Not a valid date
                continue;
            }finally {
                mD = rotate(mD);
            }
        }
    }

    private void noYear(int[] dates){
        // parse  and repeat
        for ( int i = 0 ; i < dates.length ; i ++ ) {
            haveYear( dates, i );
        }
    }

    public String getEarliestDate( ) {

        try{
            Date earliestDate = null;

            if (d_validDateList.size() == 0) {
                return d_inputDate+" is illegal";
            }

            // get the earliest date
            for ( Date dt : d_validDateList ) {
                if ( earliestDate == null ) {
                    earliestDate  = dt;
                    continue;
                }
                if ( dt.before(earliestDate) )
                    earliestDate = dt ;
            }

            return (s_df.format(earliestDate)) ;
        }
        catch(Exception e ){
            return d_inputDate+" is illegal";
        }


    }



    //
    // Static utility methods follow
    //

    private static void sop ( String message ) {
        System.out.println(message);
    }

    private static int[] rotate(int[] a){
        if (a.length ==0 )
            return a;

        int firstElement = a[0];

        for ( int i = 0 ; i < a.length-1 ; i ++ ) {
            a[i]= a[i+1];
        }
        a[a.length-1] = firstElement;
        return a;
    }

    private static String padYear(String s ){
        if ( s.length() > 3) 
            return s;

        String frt =  (String.format("2%1$#3s", s)).replace(" ","0");
        return ( frt) ;
    }

    // Invalid Date Exception Class


}
谢谢大家。我相信这将是编写测试用例的一次很好的学习体验

下面的代码如何:

public static void main ( String[] args){

  for ( int i = 0; i < dta.length ; i ++ )    {
    BestBefore bb = new BestBefore(***null***);
    System.out.println(bb.getEarliestDate());
  }

}

该规范意味着应该拒绝3位数的年份,而不是用2填充。@DavidWallace这也是我看到的唯一的差异-你应该将其作为answer@DavidWallace,但愿如此;我本应该只上一堂规范阅读课,但我想还有更多的课要学。在您的帮助下,Bot仍然拒绝。我怀疑您的代码会拒绝
31/12/2999
,尽管它应该接受它。然而,我不知道
after
方法是否奇怪,它说今天是在今天之后。@Daniel Fischer。我已经考虑并尝试了这个测试用例,我的程序为它提供了正确的输出。我编辑了我的问题以反映这一点。我相信这是因为当创建新的GreogrianCalendar()对象时,它将具有该机器时间实例的HH:mm:ss.SSS,并且我只覆盖了其中的yyyy-mm-dd部分。我意识到,如果这个程序在机器时间为HH:mm:ss.SSS的时候运行,那么这个例子就会失败,我应该处理这个问题。但是它仍然会通过剩余的时间。null是一个很好的建议。我已经编辑了我的测试用例和结果,并将它们添加到测试用例中,我的代码似乎正确地处理了它们。谢谢。
2001-01-01
999/12/31 is illegal
2012-12-12
0/0/0 is illegal
2001-04-05
999/12/12 is illegal
2010-11-12
qwe/1/1 is illegal
-1/10/10 is illegal
1000/1/1 is illegal
2100/2/29 is illegal
31/11/76 is illegal
2400-02-29
2404-02-29
2401/2/29 is illegal
 is illegal
1/1/ is illegal
000000000000/ / is illegal
1/1/1.0 is illegal
2001-01-01
2001-02-03
2005-09-30
2012-12-26
2036-12-12
0 is illegal
1.1/3.0/3 is illegal
999/31/12 is illegal
1/1/000 is illegal
2/10 is illegal
2067-02-04
 is illegal
unknown is illegal
2012-10-10 is illegal
20121010 is illegal
2012-10-10
2012-10-10
2012-01-10
2012-01-10
2010-10-12
2002-10-10
2/10 is illegal
2 is illegal
2999-12-31
1/1/3000 is illegal
null is illegal
public static void main ( String[] args){

  for ( int i = 0; i < dta.length ; i ++ )    {
    BestBefore bb = new BestBefore(***null***);
    System.out.println(bb.getEarliestDate());
  }

}
null
""
"unknown"
"2012-10-10"
"20121010"
"2012/10/10/"

"2012/10/10"
"2012/1/10"
"2012/10/1"
"12/10/10"
"2/10/10"
"2/10"
"2"