UNPKG

area51

Version:

Experimental prototypes of alien things built in JavaScript. The bits may end up living in a different package.

150 lines (117 loc) 4.39 kB
var dualPivotQuickSort = require("../../lib/collections/dual-pivot-quick-sort"); var mergeSort = require("../../lib/collections/merge-sort"); var insertionSort = require("../../lib/collections/insertion-sort"); var quickSort = require("../../lib/collections/quick-sort"); var assert = require("assert"); var wordLimit = 10; var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 *()!@#+_"; var generateWord = function() { var word = ""; for( var i=0; i < wordLimit; i++ ) { var position = nextInt(0, characters.length); word += characters[position]; if(word === undefined) { console.log("position " + position + " causes undefined"); } } return word; }; function nextInt(min, max) { min = min || 0; max = max || 60000; return Math.floor(Math.random() * (max - min)) + min; } var generateArray = function(length) { var array = [], i = 0; length = length || nextInt(); array.length = length; for(; i < length; i++) { array[i] = generateWord(); } return array; }; var unsorted = generateArray(); module.exports = { "sorting": { "native sort": function() { assert.ok(unsorted != null); var copy = [].concat(unsorted); copy.sort(); assert.equal(unsorted.length, copy.length); var detect = copy.indexOf(undefined); assert.ok(detect === -1, "sort mutated array values: undefined found in copy after at index " + detect); if(copy.length > 1) { var last = copy[0]; for(var i = 1; i < copy.length; i++) { var current = copy[i]; assert.ok(last <= current, "the previous item "+last+" must be less or equal the current item " + current); } } }, "dualPivotQuickSort": function() { assert.ok(unsorted != null); var copy = [].concat(unsorted); dualPivotQuickSort(copy); assert.equal(unsorted.length, copy.length); var detect = copy.indexOf(undefined); assert.ok(detect === -1, "sort mutated array values: undefined found in copy after at index " + detect); if(copy.length > 1) { var last = copy[0]; for(var i = 1; i < copy.length; i++) { var current = copy[i]; assert.ok(last <= current, "the previous item "+last+" must be less or equal the current item " + current); } } }, "mergeSort": function() { this.timeout(6000); var unsorted = generateArray(); assert.ok(unsorted != null); var copy = [].concat(unsorted); assert.equal(unsorted.length, copy.length); mergeSort(copy); assert.equal(unsorted.length, copy.length, "mutated array. extra item. last item is " + copy[copy.length - 1]); var detect = copy.indexOf(undefined); assert.ok(detect === -1, "sort mutated array values: undefined found in copy after sort at index " + detect); if(copy.length > 1) { var last = copy[0]; for(var i = 1; i < copy.length; i++) { var current = copy[i]; assert.ok(last <= current, "the previous item "+last+" must be less or equal the current item " + current); } } }, "insertionSort": function() { var unsorted = generateArray(100); assert.ok(unsorted != null); var copy = [].concat(unsorted); insertionSort(copy); assert.equal(unsorted.length, copy.length); var detect = copy.indexOf(undefined); assert.ok(detect === -1, "sort mutated array values: undefined found in copy after sort at index " + detect); if(copy.length > 1) { var last = copy[0]; for(var i = 1; i < copy.length; i++) { var current = copy[i]; assert.ok(last <= current, "the previous item "+last+" must be less or equal the current item " + current); } } }, "quickSort": function() { assert.ok(unsorted != null); var copy = [].concat(unsorted); quickSort(copy); assert.equal(unsorted.length, copy.length); var detect = copy.indexOf(undefined); assert.ok(detect === -1, "sort mutated array values: undefined found in copy after sort at index " + detect); if(copy.length > 1) { var last = copy[0]; for(var i = 1; i < copy.length; i++) { var current = copy[i]; assert.ok(last <= current, "the previous item "+last+" must be less or equal the current item " + current); } } } } };