| 1 |
load("testsrc/assert.js"); |
| 2 |
|
| 3 |
function makeArray(length) { |
| 4 |
var a = []; |
| 5 |
for (var i = 0; i < length; i++) { |
| 6 |
a.push(Math.trunc(Math.random() * 100000)); |
| 7 |
} |
| 8 |
return a; |
| 9 |
} |
| 10 |
|
| 11 |
function ensureSorted(a) { |
| 12 |
var last = 0; |
| 13 |
for (var i in a) { |
| 14 |
assertTrue(a[i] >= last); |
| 15 |
last = a[i]; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
function compareIntGood(a, b) { |
| 20 |
if (a < b) { |
| 21 |
return -1; |
| 22 |
} |
| 23 |
if (b < a) { |
| 24 |
return 1; |
| 25 |
} |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
function compareIntBad(a, b) { |
| 30 |
return (Math.random() * 10) - 5; |
| 31 |
} |
| 32 |
|
| 33 |
var a = makeArray(100); |
| 34 |
a.sort(compareIntGood); |
| 35 |
ensureSorted(a); |
| 36 |
|
| 37 |
a = makeArray(10000); |
| 38 |
a.sort(compareIntBad); |
| 39 |
|
| 40 |
|