-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSuite.js
More file actions
126 lines (106 loc) · 2.97 KB
/
TestSuite.js
File metadata and controls
126 lines (106 loc) · 2.97 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
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
var ten = 10,
hundred = 100,
thousand = 1000,
ten_thousand = 10000,
hundred_thousand = 100000,
million = 1000000;
// very self-explanatory
// int numberOfTests, int max, int dataLength, boolean showSortedArray = false, runTest = true
doIt(ten, million, ten_thousand);
/**
* NOTE from the developer:
* You only need to manipulate the codes on the top, the codes below are the core
* functionality of the program and requires no refactoring
*/
// generates random data for testing purpose
function generateRandomTestData(len, max) {
var randomTestData = [];
for (var c = 1; c <= len; c++) {
randomTestData.push(Math.floor(Math.random() * max) + 1);
}
return randomTestData;
}
function bubbleSort(randomTestData) {
for (var a = randomTestData.length - 1; a > 0; a--) {
for (var b = 0; b < a; b++) {
if (randomTestData[b] > randomTestData[b + 1]) {
var temp = randomTestData[b];
randomTestData[b] = randomTestData[b + 1];
randomTestData[b + 1] = temp;
}
}
}
return randomTestData;
}
function assert(sorted) {
for (var a = 0; a < sorted.length - 1; a++) {
if (sorted[a] > sorted[a + 1]) {
return false;
}
}
return true;
}
function doIt(numberOfTests, max, dataLength, showSortedArray = false, runTest = true) {
var startTime = 0,
endTime = 0,
average = 0,
longestTime = 0,
shortestTime = 0,
timeConsumed = 0,
randomTestData,
sorted,
failedTests = 0,
passedTests = 0,
message = '';
for (var a = 1; a <= numberOfTests; a++) {
// generate random data
randomTestData = generateRandomTestData(dataLength, max);
// get start time
startTime = new Date().getTime();
// run the sort
sorted = bubbleSort(randomTestData);
// get end time
endTime = new Date().getTime();
// calculate how much time was consumed
timeConsumed = (endTime - startTime) / 1000;
// change the longest time
if (timeConsumed > longestTime) {
longestTime = timeConsumed;
}
// change the shortest time
if (a == 1 || timeConsumed < shortestTime) {
shortestTime = timeConsumed;
}
// add the time consumed to the average for later use
average += timeConsumed;
if (showSortedArray) {
message = `${sorted}`;
} else {
message = `${timeConsumed} second(s)`;
}
if (runTest) {
if (!assert(sorted)) {
message = `test #${a}: FAILED : ${message}`;
++failedTests;
} else {
message = `test #${a}: PASSED : ${message}`;
++passedTests;
}
} else {
message = `test #${$a}: ${message}`;
}
console.log(message);
}
// compute average time consumed
average = average / numberOfTests;
console.log(`-------------`);
console.log(`Conducted ${numberOfTests} test(s).`);
console.log(`language: JavaScript`);
console.log(`Data length: ${dataLength}`);
console.log(`Average time: ${average}`);
console.log(`Shortest time: ${shortestTime}`);
console.log(`Longest time: ${longestTime}`);
console.log(`Failed Tests: ${failedTests}`);
console.log(`Passed Tests: ${passedTests}`);
console.log(`-------------`);
}