Advertisement
drorgrebel

Ex14- Q4 Tester

Jan 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. /**
  2. * Write a description of class Ex14_Tester here.
  3. *
  4. * @author (dror)
  5. * @version (a version number or a date)
  6. */
  7. /*import static org.junit.Assert.*;
  8. import static org.hamcrest.CoreMatchers.*;
  9. import java.lang.reflect.Field;
  10. import java.time.LocalDate;*/
  11. import java.util.Random;
  12. /*import java.util.function.Function;
  13. import org.junit.Test;
  14. import org.junit.Assert;*/
  15. public class Ex14_Tester_Q4
  16. {
  17. private static boolean matchWithLoop (int[] a, int[] p)
  18. {
  19. int a_len = a.length;
  20. int p_len = p.length;
  21. boolean found_match = false;
  22.  
  23. //no p --> return true
  24. if(p == null){
  25. return true;
  26. }
  27. //a < p --> return false
  28. else if(a.length < p.length){
  29. return false;
  30. }
  31.  
  32. //Iterate a[] with pattern
  33. for(int i=0; i< a_len - p_len + 1; i++){
  34. if(found_match == true){
  35. return true;
  36. }
  37. else{//set to true for the inner loop
  38. found_match = true;
  39. }
  40. for (int j=0; (j< p_len)&&found_match; j++){
  41. int reminder = a[i+j]/10;
  42. reminder = (reminder < 0) ? -1*reminder : reminder;
  43. if(reminder >= 10){//three digits or more
  44. found_match=false;
  45. }
  46. else if(reminder >= 1){ // two digits
  47. if(p[j]==2 || p[j]==0){
  48. found_match = true;
  49. }
  50. else{ //p[j] == 1
  51. found_match = false;
  52. }
  53. }
  54. else if(reminder < 1){ //single digit
  55. if(p[j]==1 || p[j]==0){
  56. found_match = true;
  57. }
  58. else{//p[j] == 2
  59. found_match = false;
  60. }
  61. }
  62. }
  63. }
  64. return found_match; //for the case that a.len == p.len
  65. }
  66.  
  67. public static void main( String args[] ){
  68. final int LOOP_Q1 = 100;
  69. boolean printouts = true;
  70. int i;
  71. for (i = 0;i< LOOP_Q1;i++)
  72. {
  73. Random rand = new Random();
  74. int[] a = new int[rand.nextInt(50)];//define a[] length up to 200 elements
  75. int[] p = new int[rand.nextInt(10)];//define p[] length up to 20 elements
  76. for (int t = 0; t < a.length; t++)
  77. {
  78. a[t] = rand.nextInt(220) - 100; //set random numbers for a[]: -100 to 120
  79. }
  80. for(int s = 0; s < p.length; s++)
  81. {
  82. p[s] = rand.nextInt(3); //set random values: 0,1,2
  83. }
  84. boolean result = Ex14.match(a,p);
  85. boolean expected = matchWithLoop(a,p);
  86. if(printouts){
  87. System.out.printf("%d) [", i);
  88. for (int t = 0; t < a.length; t++)
  89. {
  90. System.out.printf("%d ",a[t]);
  91. }
  92. System.out.printf("]\n");
  93. System.out.printf(" %d) [", i);
  94. for (int s = 0; s < p.length; s++)
  95. {
  96. System.out.printf("%d ",p[s]);
  97. }
  98. System.out.printf("]\n");
  99. System.out.printf(" Passed: expected: %b, actual: %b\n\n ",expected,result);
  100.  
  101.  
  102.  
  103. }
  104. if(expected != result){
  105. System.out.println("failed: expected: "+ expected + " Actual: "+ result );
  106. System.out.println();
  107. System.exit(1);
  108. }
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement