Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src ="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" > </script >
<script src ="https://ajax.googleapis.com/ajax/libs/dojo/1/dojo/dojo.xd.js" > </script >
<script src ="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js" > </script >
<script >
var Suite = {
array1 : ["Vijendra" , "Singh" , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ],
array2 : ["Singh" , "Shakya" , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 , 20 ],
setup : {
},
tests : {},
};
Suite .setup .extendLoop = function (loopLength ) {
if (!loopLength) {
loopLength = 100 ;
}
for (var loop = 0 ; loop < loopLength; loop++) {
var val1 = Math .floor (Math .random () * 100 );
var val2 = Math .floor (Math .random () * 100 );
Suite .array1 .push (val1);
Suite .array2 .push (val2);
};
};
Suite .tests .arrayUnique = function (array1, array2 ) {
var array = array1.concat (array2);
var a = array.concat ();
for (var i = 0 ; i < a.length ; ++i) {
for (var j = i + 1 ; j < a.length ; ++j) {
if (a[i] === a[j]) {
a.splice (j--, 1 );
}
}
}
return a;
};
Suite .tests .filterArray = function (array1, array2 ) {
return array1.concat (array2.filter (function (i ) {
return array1.indexOf (i) == -1 ;
}));
};
Suite .tests .whileLoop = function (array1, array2 ) {
var array3 = [];
var arr = array1.concat (array2);
var len = arr.length ;
var assoc = {};
while (len--) {
var itm = arr[len];
if (array3.indexOf (itm) === -1 ) {
array3.unshift (itm);
}
}
return array3;
};
Suite .tests .lodashUnion = _.union ;
Suite .tests .whileLoopAlt = function (array1, array2 ) {
var array3 = [];
var arr = array1.concat (array2);
var len = arr.length ;
var assoc = {};
while (len--) {
var itm = arr[len];
if (!assoc[itm]) {
array3.unshift (itm);
assoc[itm] = true ;
}
}
return array3;
};
Suite .tests .multipleArrayMerge = function ( ) {
var result = [];
for (var i = 0 ; i < arguments .length ; i++){
var array = arguments [i];
for (var j = 0 ; j < array.length ; j++){
if (result.indexOf (array[j]) === -1 ) {
result.push (array[j]);
}
}
}
return result;
};
Suite .tests .alternateLoopSolution = function (array1, array2 ) {
var merged = array1.concat (array2);
var t;
for (var i = 0 ; i < merged.length ; i++) {
if ((t = merged.indexOf (i + 1 , merged[i])) != -1 ) {
merged.splice (t, 1 );
i--;
}
}
return merged;
};
Suite .tests .mergeStringArrays = function (array1, array2 ) {
var hash = {};
var ret = [];
for (var i = 0 ; i < array1.length ; i++) {
var e = array1[i];
if (!hash[e]) {
hash[e] = true ;
ret.push (e);
}
}
for (var i = 0 ; i < array2.length ; i++) {
var e = array2[i];
if (!hash[e]) {
hash[e] = true ;
ret.push (e);
}
}
return ret;
};
Suite .tests .addPrototype = function (array1, array2 ) {
var a = array1.concat ();
if (array2.push || !array2.length ) return a;
if (!a.length ) return b.concat ();
for (var i = 0 ; i < b.length ; i++) {
if (a.indexOf (b[i]) == -1 ) {
a.push (b[i]);
}
}
return a;
};
Suite .tests .jQueryTest = function (array1, array2 ) {
$.unique (array1.concat (array2));
};
Suite .tests .flatMerge = function (array1, array2 ) {
return Array .prototype .reduce .call (arguments , function (result, current ) {
if (!(current instanceof Array )) {
if (result.indexOf (current) === -1 ) {
result.push (current);
}
} else {
current.forEach (function (value ) {
if (result.indexOf (value) === -1 ) {
result.push (value);
}
});
}
return result;
}, []);
};
Suite .tests .dojosan = function (array1, array2 ) {
var unique = [];
var array3 = array1.concat (array2);
dojo.forEach (array3, function (item ) {
if (dojo.indexOf (unique, item) > -1 ) return ;
unique.push (item);
});
return unique;
};
Suite .tests .greatMerge = function (array1, array2 ) {
var hash = {}, i;
for (i=0 ; i<array1.length ; i++) {
hash[array1[i]]=true ;
}
for (i=0 ; i<array2.length ; i++) {
hash[array2[i]]=true ;
}
return Object .keys (hash);
};
Suite .tests .es3Merge = function (a, b ) {
var hash = {},
i = (a = a.slice (0 )).length ,
e;
while (i--) {
hash[a[i]] = 1 ;
}
for (i = 0 ; i < b.length ; i++) {
hash[e = b[i]] || a.push (e);
}
return a;
};
Suite .tests .es3MergeWithoutOrder = function (a, b ) {
var hash = {},
i = (a = a.slice (0 )).length ,
e;
while (i--) {
hash[a[i]] = 1 ;
}
i = b.length ;
while (i--) {
hash[e = b[i]] || a.push (e);
}
return a;
};
Suite .tests .order = [
Suite .tests .arrayUnique ,
Suite .tests .filterArray ,
Suite .tests .whileLoop ,
Suite .tests .lodashUnion ,
Suite .tests .multipleArrayMerge ,
Suite .tests .alternateLoopSolution ,
Suite .tests .mergeStringArrays ,
Suite .tests .addPrototype ,
Suite .tests .jQueryTest ,
Suite .tests .flatMerge ,
Suite .tests .dojosan ,
Suite .tests .greatMerge ,
Suite .tests .es3Merge ,
Suite .tests .es3MergeWithoutOrder
];
Suite .setup .extendLoop ();
</script >
Setup JS
Teardown JS
Test cases
Test #1 Title *
Async
Code * Suite .tests .arrayUnique (Suite .array1 , Suite .array2 );
Test #2 Title *
Async
Code * Suite .tests .filterArray (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .whileLoop (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .lodashUnion (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .whileLoopAlt (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .multipleArrayMerge (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .alternateLoopSolution (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .mergeStringArrays (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .dojosan (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .jQueryTest (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .flatMerge (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .greatMerge (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .es3Merge (Suite .array1 , Suite .array2 );
Title *
Async
Code * Suite .tests .es3MergeWithoutOrder (Suite .array1 , Suite .array2 );