2014-11-14 40 views
2

欲徹底地測試一個字符串匹配算法,命名myAlgo(CHAR []一,CHAR [] B)爪哇:測試算法:所有可能的組合

的窮舉測試包括沒有。不同的char字母,alplhabet「l」,在一個「n」長的數組中。然後,測試計算所有組合,並將其與具有相似屬性的另一個數組的所有組合(如真值表)進行比較,

我還沒有能夠計算的東西,會產生大小爲n和字母l的數組的每個組合,否則我能夠使代碼能夠將計算組合到迭代測試用例中(測試所有的兩個數組的組合被比較),儘管代碼能夠生成組合,但是嵌套的for-loop應該執行所需的測試。

我的目標是打破我的算法,使其計算一些它不應該計算的東西。

Test(char[] l, int n) 
    l = [a;b] //a case could be 
    n = 2  //a case could be 
    myAlgo([a;a],[a;a]); //loops over my algorithm in the following way 
    myAlgo([a;b],[a;a]); 
    myAlgo([b;a],[a;a]); 
    myAlgo([b;b],[a;a]); 
    myAlgo([a;a],[a;b]); 
    myAlgo([a;b],[a;b]); 
    myAlgo([b;a],[a;b]); 
    myAlgo([b;b],[a;b]); 
    myAlgo([a;a],[b;a]); 
    myAlgo([a;b],[b;a]); 
    ... 
    myAlgo([b;b],[b;b]); 

自己的解決方案(僅適用於一組有限的「L」),並開始打印在後面的迭代奇怪的輸出。

公共類的測試{

//aux function to format chars 
public static String concatChar(char [] c){ 
    String s = ""; 
    for(char cc : c){ 
     s += cc; 
    } 
    return s; 
} 

public static void main(String[] args) { 

    String ss1 = "AA";       //TestCases, n = 2 
    String ss2 = "AA"; 
    char[] test1 = ss1.toCharArray(); 
    char[] test2 = ss2.toCharArray(); 
    Fordi fordi = new Fordi();     //my algorithm 
    TestGenerator tGen = new TestGenerator(); //my testGenerator 
    for(int i=0; i<Math.pow(4.0, 2.0);i++){  //to test all different cases 

     for(int j=0; j<Math.pow(4.0, 2.0);j++){ 
      int k = fordi.calculate(test1, test2); //my algorithm 
      String mys1 = concatChar(test1);  //to print result 
      String mys2 = concatChar(test2);  //to print result 
      System.out.println(mys1 + " - " + mys2); 
      System.out.println(k); 
      test2 = tGen.countArray(test2);   //"flip" one number 
     } 
     test2 = ss1.toCharArray(); 
     test1 = tGen.countArray(test1);    //"flip" 
    } 
} 

}

我arrayflipper代碼:

public char[] countArray(char[] a){ 
    int i=0; 
    while(i<a.length){ 
     switch (a[i]){ 
     case 'A': 
      a[i]='B'; 
      clearBottom(a,i); 
      return a; 
     case 'B': 
      a[i]='C'; 
      clearBottom(a,i); 
      return a; 
     case 'C': 
      a[i]='D'; 
      clearBottom(a,i); 
      return a; 
     case 'D': 
      i++; 
      break; 
     default: 
      System.out.println("Something went terribly wrong!"); 

     } 
    } 
    return a; 

} 
public char[] clearBottom(char [] a, int i){ 
    while(i >0){ 
     i--; 
     a[i] = 'A'; 
    } 
    return a; 
} 
+0

這是很不清楚你在問什麼 - 我也沒有看到所有的變化可能和位翻轉算法(但我不知道算法相當好)之間的連接。 – JBA 2014-11-14 15:08:17

+2

更新,感謝您的反饋 – user3711518 2014-11-14 15:15:30

+0

不客氣 - 它已經更清晰你試圖存檔 - 因此我upvote讓你回到正軌。現在你堅持在什麼地方?像你有問題產生所有不同的組合?或者是你能夠生成所有這些組合,但不知道如何從中動態生成測試用例?或者它既是組合以及調用已經工作,但你的'myAlgo'方法不能按預期工作? (在最後一種情況下,我們將不得不看到'myAlgo'方法,而在其他情況下,它不是真正的興趣。 – JBA 2014-11-14 15:29:10

回答

0

據我瞭解,你的目標是創建所有n個字符的字符串(單獨保存作爲數組中的元素)由L字母字母組成的字母組成?

完成此操作的一種方法是訂購您的字母(A = 0,B = 1,C = 2等)。然後,您可以從AAA ... AAA(n個字符長)的起始字符串中繼續添加1.基本上,您實現了一個添加算法。加1會將A = 0變成B = 1。例如,n = 3和L = 3:

start:AAA(0,0,0)。

加1變成AAB(0,0,1)

添加1再次成爲AAC(0,0,2)

再次加1(因爲我們是出書,現在我們隨身攜帶位)ABA(0,1,0)。

您可以將流程簡化爲查找未超出最右邊的數字並將其加1(然後該數字右側的所有數字都會回到零)。所以在字符串ABCCC中,B數字是最右邊未被最大化的數字,它增加1並且變成C,然後右邊的所有最大數字返回到0(A),作爲下一個離開ACAAA串。

您的算法只是重複添加1,直到字符串中的所有元素都被刷新爲止。

0

除了使用switch語句之外,我建議將要測試的每個字符(A,B,C,D)放入數組中,然後使用XOR操作從迭代數中計算每個字符的索引在類似如下的方式:

char[] l = new char[]{'A','B','C','D'}; 
int n = 2; 
char[] test1 = new char[n]; 
char[] test2 = new char[n]; 
int max = (int)Math.pow(l.length, n); 
for (int i = 0; i < max; i++) { 
    for (int k = 0; k < n; k++) { 
     test2[k] = l[(i % (int)Math.pow(l.length, k + 1))/(int)Math.pow(l.length, k)]; 
    } 
    for (int j = 0; j < max; j++) { 
     for (int k = 0; k < n; k++) { 
      test1[k] = l[(j % (int)Math.pow(l.length, k + 1))/(int)Math.pow(l.length, k)]; 
     } 
     int k = fordi.calculate(test1, test2); 
     System.out.println(new String(test1) + "-" + new String(test2)); 
     System.out.println(k); 
    } 
} 

您可以添加更多的字符l以及增加n,它應該仍然工作。當然,這可以進一步優化,但你應該明白。希望這個答案有幫助!