-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheTest.java
More file actions
94 lines (76 loc) · 3.17 KB
/
CacheTest.java
File metadata and controls
94 lines (76 loc) · 3.17 KB
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
import java.util.*;
import java.io.*;
public class CacheTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
//Parse command line to set up 1 cache or 2 cache of certain size
if(args.length == 3){
//first cache code
int cacheSize = Integer.parseInt(args[1]);
Cache<String> cache = new Cache<String>(cacheSize);
File file = new File(args[2]);
try{
Scanner input = new Scanner(file);
while(input.hasNext()){
String word = input.next();
cache.incrementFirstCacheReferences();
if(cache.hasObjectFirst(word)){
cache.incrementFirstCacheHits();
cache.removeObjectFirst(word);
cache.addObjectFirst(word);
}
else{
cache.removeObjectFirst(word);
cache.addObjectFirst(word);
}
}
input.close();
}
catch(IOException e){
System.out.println("File not found");
e.printStackTrace();
}
cache.toStringFirst(cache, args[1], startTime);
}
if(args.length == 4){
//second cache code
int cacheSize1 = Integer.parseInt(args[1]);
int cacheSize2 = Integer.parseInt(args[2]);
Cache<String> cache = new Cache<String>(cacheSize1, cacheSize2);
File file = new File(args[3]);
try{
Scanner input = new Scanner(file);
while(input.hasNext()){
String word = input.next();
cache.incrementFirstCacheReferences();
if(cache.hasObjectFirst(word)){
cache.incrementFirstCacheHits();
cache.removeObjectFirst(word);
cache.addObjectFirst(word);
cache.removeObjectSecond(word);
cache.addObjectSecond(word);
}
else{
cache.incrementSecondCacheReferences();
if(cache.hasObjectSecond(word)){
cache.incrementSecondCacheHits();
cache.removeObjectSecond(word);
cache.addObjectSecond(word);
cache.addObjectFirst(word);
}
else{
cache.addObjectFirst(word);
cache.addObjectSecond(word);
}
}
}
input.close();
}
catch(IOException e){
System.out.println("File not found");
e.printStackTrace();
}
cache.toStringBoth(cache, args[1], args[2], startTime);
}
}
}