Skip to content

Commit

Permalink
Added serialOrdered tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexindigo committed May 21, 2016
1 parent 4de6a27 commit 9b19760
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/test-parallel-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('parallel: iterates over array', function(t)
{
var diff = +new Date() - start;

t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than 1 second');
t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than sum of delays');
t.error(err, 'expect no errors');
t.deepEqual(result, expected, 'expect result to be an ordered letters array');
});
Expand Down
2 changes: 1 addition & 1 deletion test/test-parallel-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('parallel: iterates over object', function(t)
{
var diff = +new Date() - start;

t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than 1 second');
t.ok(diff < 160, 'expect response time (' + diff + 'ms) to be less than sum of delays');
t.error(err, 'expect no errors');
t.deepEqual(result, expected, 'expect result to be an ordered letters object');
});
Expand Down
41 changes: 41 additions & 0 deletions test/test-serialOrdered-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ test('serialOrdered: iterates over array sorted descending', function(t)
});
});

test('serialOrdered: iterates over array custom sorted', function(t)
{
var source = [ 1, 2, 3, 4, 3, 2, 1 ]
, expected = [ 'A', 'B', 'C', 'D', 'C', 'B', 'A' ]
// get smallest even number
, prev = Math.min.apply(Math, source.filter(function(n){ return !(n % 2); }))
// puts even numbers first
, customSort = function(a, b)
{
var order = a < b ? -1 : a > b ? 1 : 0
, aOdd = a % 2
, bOdd = b % 2
;
return aOdd === bOdd ? order : aOdd ? 1 : -1;
}
;

t.plan(expected.length * 2 + 2);

serialOrdered(source, function(item, key, cb)
{
var incr = prev <= item
, shift = (prev % 2) !== (item % 2)
;

t.ok(incr || shift, 'expect item (' + item + ') to increase on each iteration, unless it is switch from even to odd');
t.equal(source[key], item, 'expect iteration indices to match original array positions');

setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item);
prev = item;
},

customSort, // custom sorting

function(err, result)
{
t.error(err, 'expect no errors');
t.deepEqual(result, expected, 'expect result to keep order of the original array');
});
});

//
//
// test('serial: handles sync array iterator asynchronously', function(t)
Expand Down
99 changes: 99 additions & 0 deletions test/test-serialOrdered-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,105 @@ test('serialOrdered: iterates over object sorted descending', function(t)
});
});


test('serialOrdered: iterates over object custom sorted', function(t)
{
var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 }
, keys = Object.keys(source)
, expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' }
// separate vowels from consonants
, splitVowels = function(word)
{
var vowels = ['a', 'e', 'i', 'o', 'u']
, wordVow = word.split('').map(function(x){ return vowels.indexOf(x) == -1 ? '~' : x; }).join('')
, wordCon = word.split('').map(function(x){ return vowels.indexOf(x) == -1 ? x : '.'; }).join('')
;
return [wordVow, wordCon];
}
// sort words based on vowels and their position
, customSort = function(wordA, wordB)
{
wordA = splitVowels(wordA);
wordB = splitVowels(wordB);
return wordA[0] < wordB[0] ? -1 : (
wordA[0] > wordB[0] ? 1 : (
wordA[1] < wordB[1] ? -1 : wordA[1] > wordB[1] ? 1 : 0
)
);
}
// pre-sort list keys
, customSortedKeys = keys.sort(customSort)
, prev
;

t.plan(keys.length * 3 + 2);

serialOrdered(source, function(item, key, cb)
{
t.notEqual(customSortedKeys.indexOf(key), -1, 'expect key to be in the list');
t.ok(customSortedKeys.indexOf(prev) < customSortedKeys.indexOf(key), 'expect key (' + prev + ' -> ' + key + ') index (' + customSortedKeys.indexOf(prev) + ' -> ' + customSortedKeys.indexOf(key) + ') to increase on each iteration');
t.equal(source[key], item, 'expect iteration indices to match original array positions');

setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item);
prev = key;
},

customSort, // custom sorting

function(err, result)
{
t.error(err, 'expect no errors');
t.deepEqual(result, expected, 'expect result to keep order of the original array');
});
});

test('serialOrdered: iterates over object custom sorted over values', function(t)
{
var source = { first: 1, second: 2, third: 3, fourth: 4, three: 3, two: 2, one: 1 }
, keys = Object.keys(source)
, values = keys.map(function(k){ return source[k]; })
, expected = { first: 'A', second: 'B', third: 'C', fourth: 'D', three: 'C', two: 'B', one: 'A' }
// get smallest even number
, prev = Math.min.apply(Math, values.filter(function(n){ return !(n % 2); }))
// puts even numbers first
, evenOddSort = function(a, b)
{
var order = a < b ? -1 : a > b ? 1 : 0
, aOdd = a % 2
, bOdd = b % 2
;
return aOdd === bOdd ? order : aOdd ? 1 : -1;
}
, customSort = function(keyA, keyB)
{
return evenOddSort(source[keyA], source[keyB]);
}
;

t.plan(keys.length * 2 + 2);

serialOrdered(source, function(item, key, cb)
{
var incr = prev <= item
, shift = (prev % 2) !== (item % 2)
;

t.ok(incr || shift, 'expect item (' + prev + ' -> ' + item + ') to increase on each iteration, unless it is switch from even to odd');
t.equal(source[key], item, 'expect iteration indices to match original array positions');

setTimeout(cb.bind(null, null, String.fromCharCode(64 + item)), 10 * item);
prev = item;
},

customSort, // custom sorting

function(err, result)
{
t.error(err, 'expect no errors');
t.deepEqual(result, expected, 'expect result to keep order of the original array');
});
});

//
//
// test('serial: handles sync array iterator asynchronously', function(t)
Expand Down

0 comments on commit 9b19760

Please sign in to comment.