@matfah/

Download Info FR 2013

Java

No description

fork
loading
Files
  • Main.java
  • DownloadInfo.java
  • MusicDownloads.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
import java.util.*;

class Main {
  public static void main(String[] args) {
    testGetDownloadInfo();
    testUpdateDownloads();
  }
  
  public static void testGetDownloadInfo() {
    System.out.println("Testing getDownloadInfo()...");
    try {
      MusicDownloads downs = new MusicDownloads();
      java.lang.reflect.Field f = MusicDownloads.class.getDeclaredField("downloadList");
      f.setAccessible(true);
      
      ArrayList<DownloadInfo> internalList = (ArrayList<DownloadInfo>)f.get(downs);
      
      String[] names = {"Hey Jude", "Soul Sister", "Aqualung", "Smells Like Teen Spirit"};
      ArrayList<Integer> hashCodes = new ArrayList<Integer>();
      
      for(String n: names) {
        DownloadInfo d = new DownloadInfo(new String(n));
        hashCodes.add(System.identityHashCode(d));
        internalList.add(d);
      }
      for(int i = 0; i < names.length; i++) {
        DownloadInfo a = downs.getDownloadInfo(names[i]);
        
        //user modifed the internal list!
        if(!sameMemoryAddresses(internalList, hashCodes)) {
          System.out.println("   FAILED TEST #" + i + " - you modified the list!");
          return;
        }
        else {
          if(a == null) {
            System.out.println("   FAILED TEST #" + i + " - you returned null incorrectly");
            System.out.println("   Correct object was at position " + i);
            return;
          }
          else if(!a.getTitle().equals(names[i])) { //wrong object`
            System.out.println("   FAILED TEST #" + i + " - you returned an object with the wrong name");
            return;
          }
          else {
            System.out.println("   Passed Test " + i);
          }
        }
      }
      
      DownloadInfo a = downs.getDownloadInfo("I Want It That Way");
        
        //user modifed the internal list!
        if(!sameMemoryAddresses(internalList, hashCodes)) {
          System.out.println("   FAILED TEST #" + 4 + " - you modified the list!");
          return;
        }
        else {
          if(a == null) {
            System.out.println("   Passed Test " + 4);
            return;
          }
          else { //wrong object`
            System.out.println("   FAILED TEST #" + 4 + " - you returned an object when you should have returned null");
            return;
          }
        }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    
  }
  
  public static void testUpdateDownloads() {
    System.out.println("Testing updateDownloads()...");
    
    try {
      MusicDownloads downs = new MusicDownloads();
      java.lang.reflect.Field f = MusicDownloads.class.getDeclaredField("downloadList");
      f.setAccessible(true);
      
      ArrayList<DownloadInfo> internalList = (ArrayList<DownloadInfo>)f.get(downs);
      
      String[][] tests = {
        {},
        {"Hey Jude", "Soul Sister", "Aqualung", "Smells Like Teen Spirit"},
        {"Lights", "Aqualung", "Soul Sister", "Go Now", "Lights", "Soul Sister"},
      };
      
      String[][] resultNames = {
        {},
        {"Hey Jude", "Soul Sister", "Aqualung", "Smells Like Teen Spirit"},
        {"Hey Jude", "Soul Sister", "Aqualung", "Smells Like Teen Spirit", "Lights", "Go Now"}
      };
      
      int[][] resultCounts = {
        {},
        {1, 1, 1, 1},
        {1, 3, 2, 1, 2, 1}
      };
      
      for(int test = 0; test < tests.length; test++) {
        downs.updateDownloads(Arrays.asList(tests[test]));
        
        //need to make sure all the same names are in the list, and with the right counts!
        
        boolean sameNames = true;
        if(internalList.size() != resultNames[test].length) {
          sameNames = false;
        }
        else {
          for(int i = 0; i < internalList.size(); i++) {
            if(!internalList.get(i).getTitle().equals(resultNames[test][i])) {
              sameNames = false;
              break;
            }
          }
        }
        
        if(!sameNames) {
          System.out.println("   FAILED TEST #" + test + " - wrong names in final list!");
          return;
        }
        
        boolean sameNums = true;
        java.lang.reflect.Field f2 = DownloadInfo.class.getDeclaredField("count");
        f2.setAccessible(true);
        
        for(int i = 0; i < internalList.size(); i++) {
            if(f2.getInt(internalList.get(i)) != resultCounts[test][i]) {
              sameNums = false;
              break;
            }
        }
        
        if(!sameNames) {
          System.out.println("   FAILED TEST #" + test + " - wrong counts!");
          return;
        }
        else {
          System.out.println("   Passed Test " + test);
        }
      }
      
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    
    System.out.println("\nYour solution to updateDownloads() should be calling the getDownloadInfo() method you wrote in part (a).  If you don't know what that means, you should ask!  You would lose points on this problem on the AP test if you reimplement what you wrote for part (a) in part (b) again;");
  }
  
  public static boolean sameMemoryAddresses(ArrayList<DownloadInfo> list, ArrayList<Integer> hash) {
    
      for(int i = 0; i < list.size(); i++) {
        if(System.identityHashCode(list.get(i)) != hash.get(i))
          return false;
      }
      return true;
        
  }
}