@matfah/

SkyView FR 2013

Java

No description

fork
loading
Files
  • Main.java
  • SkyView.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.lang.reflect.*;

class Main {
  public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    
    System.out.println("Testing constructor...");
    
    for(int i = 0; i < 5; i++)
      testConstructor(i);
    
    System.out.println("Done!\n");
    
    System.out.println("Testing getAverage...");
    
    for(int i = 0; i < 5; i++)
      testGetAverage(i);
    
    System.out.println("Done!");
  }
  
  public static void testConstructor(int test) throws NoSuchFieldException, IllegalAccessException {
    double[] scanned;
    double[][] answer;
    int rows;
    int cols;
    String description;
    
    if(test == 0) {
      scanned = new double[]{4.0, 6.0};
      rows = 1;
      cols = 2;
      answer = new double[][]{
        {4.0, 6.0}
      };
      description = "even length, more columns than rows";
    }
    if(test == 1) {
      scanned = new double[]{4.0, 6.0};
      rows = 2;
      cols = 1;
      answer = new double[][]{
        {4.0},
        {6.0}
      };
      description = "even length, more rows than columns";
    }
    else if(test == 2) {
      scanned = new double[]{1.0, 3.0, 7.0, 8.0, 5.0, 6.0, 2.0, 9.0, 4.0, 1.0, 8.0, 2.0, 9.0, 7.0, 5.0};
      rows = 5;
      cols = 3;
      answer = new double[][]{
        {1.0, 3.0, 7.0},
        {6.0, 5.0, 8.0},
        {2.0, 9.0, 4.0},
        {2.0, 8.0, 1.0},
        {9.0, 7.0, 5.0}
      };
      description = "odd length, more rows than columns";
    }
    else if(test == 3) { //all 1's
      scanned = new double[]{1.0, 3.0, 7.0, 8.0, 5.0, 6.0, 2.0, 9.0, 4.0, 1.0, 8.0, 2.0, 9.0, 7.0, 5.0};
      rows = 3;
      cols = 5;
      description = "odd length, more columns than rows";
      answer = new double[][]{
        {1.0, 3.0, 7.0, 8.0, 5.0},
        {1.0, 4.0, 9.0,2.0, 6.0},
        {8.0, 2.0, 9.0, 7.0, 5.0}
      };
    }
    else {
      scanned = new double[]{0.3, 0.7, 0.8, 0.4, 1.4, 1.1, 0.2, 0.5, 0.1, 1.6, 0.6, 0.9};
      rows = 4;
      cols = 3;
      answer = new double[][]{
        {0.3, 0.7, 0.8},
        {1.1, 1.4, 0.4},
        {0.2, 0.5, 0.1},
        {0.9, 0.6, 1.6}
      };
      description = "example in directions";
    }
    
    boolean success = false;
    String message = "";
    try {
      
      SkyView view = new SkyView(rows, cols, scanned);
    
      Field f = SkyView.class.getDeclaredField("view");
      f.setAccessible(true);
      double[][] result = (double[][])f.get(view);
      
      if(!equals(result, answer)) {
        message = "view is not populated correctly";
      }
      else {
        success = true;
      }
    }
    catch(Exception e) {
      message = e.getClass().getSimpleName();
    }
    
    if(success) System.out.println("  Passed Test " + test);
    else {
      System.out.println("  FAILED Test" + test + ": " + message);
      System.out.println("    " + description);
      System.exit(0);
    }
    
  }
  
  public static void testGetAverage(int test) throws NoSuchFieldException, IllegalAccessException {
    double[] scanned;
    double answer;
    int rows;
    int cols;
    int sr, er, sc, ec;
    String description;
    
    scanned = new double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0};
    rows = 3;
    cols = 4;
    
    if(test == 0) {
      sr = 0;
      er = 2;
      sc = 0;
      ec = 3;
      description = "average of entire matrix";
      answer = 6.5;
    }
    else if(test == 1) {
      sr = 1;
      er = 2;
      sc = 0;
      ec = 3;
      description = "average skipping first row";
      answer = 8.5;
    }
    else if(test == 2) {
      sr = 0;
      er = 2;
      sc = 1;
      ec = 3;
      description = "average skipping first column";
      answer = 6.666666666;
    }
    else if(test == 3) {
      sr = 1;
      er = 1;
      sc = 1;
      ec = 2;
      description = "average without the edges";
      answer = 6.5;
    }
    else {
      scanned = new double[]{0.3, 0.7, 0.8, 0.4, 1.4, 1.1, 0.2, 0.5, 0.1, 1.6, 0.6, 0.9};
      rows = 4;
      cols = 3;
      sr = 1;
      er = 2;
      sc = 0;
      ec = 1;
      description = "example in directions";
      answer = 0.8;
    }
    
    boolean success = false;
    String message = "";
    try {
      
      SkyView view = new SkyView(rows, cols, scanned);
      double result = view.getAverage(sr, er, sc, ec);
      
      if(Math.abs(result - answer) > 0.00001) {
        message = "calculated average is " + result + ", should be " + answer;
      }
      else {
        success = true;
      }
    }
    catch(Exception e) {
      message = e.getClass().getSimpleName();
    }
    
    if(success) System.out.println("  Passed Test " + test);
    else {
      System.out.println("  FAILED Test" + test + ": " + message);
      System.out.println("    " + description);
      System.exit(0);
    }
    
  }
  
  public static boolean equals(double[][] array1, double[][] array2) {
    if(array1.length == 0 && array2.length == 0)
      return true;
    
    if(array1.length != array2.length || array1[0].length != array2[0].length)
      return false;
      
    for(int i = 0; i < array1.length; i++)
      for(int j = 0; j < array1[0].length; j++)
        if(array1[i][j] != array2[i][j])
          return false;
    return true;
  }
}