-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
73 lines (59 loc) · 1.95 KB
/
Copy pathtest.js
File metadata and controls
73 lines (59 loc) · 1.95 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
/* Correct output test. To run, redirect output of ManageConsumers.js to 'test.txt' and
then call test.js <# number consumers> */
var fs = require('fs');
var numConsumers = parseInt(process.argv[2]);
if (isNaN(numConsumers)){
console.log("Usage: test.js <# consumers>");
}
else{
fs.readFile('test.txt', function (err, fileContents){
if (err) {
console.log("error occured");
}
else {
var splitFileString = fileContents.toString().split('\n');
var counterObj = {};
var kList = [];
//Get k values and port numbers for consumers
for (var i = 0; i < numConsumers; i++){
var k = splitFileString[i].split(" ")[0]; // k values
var kVal = parseInt(k);
kList.push(kVal);
var temp = splitFileString[numConsumers + i]; // port values
counterObj[temp.slice(2,7)] = 0;
}
// Count messages for each port number
for (var i = numConsumers; i < splitFileString.length; i++){
var temp = splitFileString[i].slice(2,7);
counterObj[temp] = counterObj[temp] + 1;
}
// Organize and sort number of messages received
var counterArray = [];
for (var port in counterObj) {
counterArray.push(parseInt(counterObj[port]));
}
kList.sort(compareNumbers);
counterArray.sort();
//console.log(kList);
//console.log(counterArray);
// Compare expected and actual numbers of messages per K value
var outString = "All values correct.";
for (var i = 0; i < kList.length; i++){
var kVal = kList[i];
var secs = counterArray[i];
if (secs == (10 + 5*kVal)) {
console.log("kVal " + kVal + " --> " + secs + " secs");
}
else{
console.log(kVal + " " + secs);
outString = "Error! Time values incorrect";
}
}
console.log(outString);
}
})
}
// Compare numbers for sort()
function compareNumbers(a, b){
return a - b;
}