@matfah/

Trail FR 2010

Java

No description

fork
loading
Files
  • Main.java
  • Trail.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    
    for(int i = 0; i < 10000; i++)
      testTrailMethods();
    System.out.println("All tests passed!");  
  }
  
  public static void testTrailMethods() {
    
    Field markersField = null;
    
    try {
      markersField = Trail.class.getDeclaredField("markers");
    }
    catch(Exception e) {
      System.out.println("Error accessing markers instance variable");
      System.exit(0);
    }
    
    markersField.setAccessible(true);
    
    int[] markers = new int[(int)(Math.random()*20)+1];
    int[] copy = new int[markers.length];
    
    int start = (int)(Math.random()*markers.length);
    int end = (int)(Math.random()*markers.length);
    if(start > end) {
      int temp = start;
      start = end;
      end = temp;
    }
    
    int[] diffs = new int[markers.length - 1];
    boolean level = true;
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    int difficult = 0;
    for(int i = 0; i < markers.length; i++) {
      markers[i] = (int)(Math.random()*100);
      copy[i] = markers[i];
      if(i != 0) {
        diffs[i-1] = Math.abs(markers[i] - markers[i-1]);
        if(diffs[i-1] >= 30) {
          difficult++;
        }
      }
      
      if(i >= start && i <= end) {
        min = Math.min(min, markers[i]);
        max = Math.max(max, markers[i]);
        level = Math.abs(max - min) <= 10;
      }
    }
    
    Trail trail = new Trail();
    
    try {
      markersField.set(trail, markers);
    }
    catch(Exception e) {
      System.out.println("Error setting markers instance variable");
      System.exit(0);
    }
    
    boolean result = false;
    try {
      result = trail.isLevelTrailSegment(start, end);
    }
    catch(Exception e) {
      System.out.println("Error calling isLevelTrailSegment()");
      System.exit(0);
    }
    
    if(result != level) {
      System.out.println("Result of isLevelTrailSegment() is wrong");
      System.exit(0);
    }
    
    if(!java.util.Arrays.equals(markers, copy)) {
      System.out.println("isLevelTrailSegment() should not change the markers array");
      System.exit(0);
    }
    
    result = false;
    try {
      result = trail.isDifficult();
    }
    catch(Exception e) {
      System.out.println("Error calling isDifficult()");
      System.exit(0);
    }
    
    if(result != (difficult >= 3)) {
      System.out.println("Result of isDifficult() is wrong");
      System.exit(0);
    }
    
    if(!java.util.Arrays.equals(markers, copy)) {
      System.out.println("isDifficult() should not change the markers array");
      System.exit(0);
    }
  }
}