Skip to content

Instantly share code, notes, and snippets.

/testCase.js Secret
Created Apr 13, 2017

Embed
What would you like to do?
Test Cases for mergeArray
function an(a, b) {
for(var r=[],i=0,j=0;i<a.length||j<b.length;){
if(b[j]&&a[i]){
if(a[i]<b[j]) {
r.push(a[i])
while(a[++i]==r[r.length-1]);
}
else if(a[i]>b[j]){
r.push(b[j])
while(b[++j]==r[r.length-1]);
}
else{
var n=a[i],x=0,y=0
while(a[++i]==n) x++
while(b[++j]==n) y++
if(x==y) r.push(n)
}
}
else if(a[i]) while(a[i]) {
if(a[i]!=a[i+1]) r.push(a[i])
i++
}
else if(b[j]) while(b[j]){
if(b[j]!=b[j+1]) r.push(b[j])
j++
}
}
return r
}
function rndc(){
var allc="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return allc[~~(allc.length*Math.random())];
}
function rndc1(){
var allc="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ !@#$%^&*_(),.?|{}[]-=+\\/"
return allc[~~(allc.length*Math.random())];
}
function rndclo(){
var allc="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return allc[~~(allc.length*Math.random())];
}
function rndcn(){
var allc="1234567890"
return allc[~~(allc.length*Math.random())];
}
function rndcl(){
var allc="abcdefghijklmnopqrstuvwxyz"
return allc[~~(allc.length*Math.random())];
}
function rndcno(){
var allc="1234567890_ !@#$%^&*_(),.?|{}[]-=+"
return allc[~~(allc.length*Math.random())];
}
function rndch(){
var allc="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
return allc[~~(allc.length*Math.random())];
}
function rnd9(){
var allc="123456789";
return allc[~~(allc.length*Math.random())];
}
function rnd10(){
var allc="0123456789";
return allc[~~(allc.length*Math.random())];
}
function rnds(n){
var len= n||rand(4,10)
for (var i=0,rs=[];i<len;i++) rs[i]=rndcl();
return rs.join("");
}
function rndsx(n){
var len= n||rand(4,10)
for (var i=0,rs=[];i<len;i++) rs[i]=rndc();
return rs.join("");
}
function rndss(n){
var len= n||rand(2,5)
for (var i=0,rs=[];i<len;i++) rs[i]=rnds();
return rs.join(" ")
}
function rndsss(n){
var len= n||rand(5,15)
for (var i=0,rs=[];i<len;i++) rs[i]=rnds();
return rndch()+rs.join(" ")
}
function shuff(arr){
for(var i=0;i<100;i++){
var idx1=rand(0,arr.length-1),idx2=rand(0,arr.length-1)
var t=arr[idx1]
arr[idx1]=arr[idx2]
arr[idx2]=t
}
}
function rnds2(n){
var len= n||~~(15*Math.random())+4;
for (var i=0,rs=[];i<len;i++) rs[i]=rndcl();
return rs.join("");
}
function rand(from,to){
return Math.floor((to-from+1)*Math.random()+from)
}
//var arr100=Array.from({length:100},(_,i)=>i+1)
function rndtest1(tp){
var len=tp?1000000:rand(10,100),start=rand(1,100),n1=start,n2=start,r1=[],r2=[],i=0
while(len--){
r1.push(n1)
r2.push(n2)
n1+=rand(0,3)?0:rand(0,3)
n2+=rand(0,3)?0:rand(0,3)
}
r2.push(Math.max(n1,n2)+rand(1,3))
return [r1,r2]
}
var [hugeArray1,hugeArray2]=rndtest1(1)
//console.log(hugeArray1.slice(0,20))
//console.log(hugeArray2.slice(0,20))
function rndtest(tp){
var r1,r2,len=tp?500000:100
var idx=rand(0,200000)
r1=hugeArray1.slice(idx,idx+len)
r2=hugeArray2.slice(idx,idx+len)
return [r1,r2]
}
function rndname(){
var len=3
for(var r=rndch(),i=0;i<len;i++) r+=rndcl()
return r
}
function rndarr(){
var len=rand(10,50),r1=[]
for (var i=0;i<len;i++) {
r1[i]=rand(1,99)
}
return r1
}
function rndarr2(){
var len=10000,len1=len+1,add=110000,r1=[],r2=[]
for (var i=0;i<len;i++) {
var t=rand(0,1000)%4?rand(1,len):rand(len1,add)
r1[i]=t
r2[i]=t
}
return [r1,r2]
}
function showResult(s,color="00cc00",who="Your"){
console.log("<font face='sans-serif' color='#"+color+"' size=3><b>"+who+" result is:</b></font><font face='sans-serif' color='#cccc00' size=3>"+
"\n"+s+"</font>","")
}
describe("Basic Tests", function(){
it("It should works for basic tests.", function(){
Test.assertDeepEquals(mergeArrays([10, 10, 10, 15, 20, 20, 25, 25, 30, 7000],[10, 15, 20, 20, 27, 7200]),[15, 20, 25, 27, 30, 7000, 7200])
Test.assertDeepEquals(mergeArrays([500, 550, 1000, 1000, 1400, 3500],[2, 2, 2, 2, 3, 1500]),[2, 3, 500, 550, 1000, 1400, 1500, 3500])
Test.assertDeepEquals(mergeArrays([5],[5, 5, 7]),[7])
Test.assertDeepEquals(mergeArrays([2,2,2,2,2,2,2,2,2,3,3,3,3,3,5,7],[2,2,2,2,2,2,2,2,2,3,3,3,3,3,6,6,8] ),[2, 3, 5, 6, 7, 8])
Test.assertDeepEquals(mergeArrays([33,33,34,34,34,37,37,37,37,38,38,38,38,41],[33,33,36,36,38,38,38,38,41,44,44,44,44,44,47] ),[33, 34, 36, 37, 38, 41, 44, 47])
})})
var failed=0,recfailed=[]
describe("100 Random Tests --- Testing for correctness of solution", function(){
it("It should works for random tests.", function(){
function check(a,b){
for(var i=0;i<a.length;i++) if(a[i]!==b[i]) return "Array a and b are different at index "+i+": a["+i+"] = "+a[i]+", b["+i+"] = "+b[i]
return "Passed!"
}
for(var iii=0;iii<100;iii++){
var [ddd,eee]=iii<20?rndtest1(0):rndtest(1)
//eee=rand(1,50)*2,
//fff=rand(0,5),
//ggg=rand(2,30),hhh=rand(1,50),
if(iii<20) console.log("<font face='sans-serif' color='#00cc00' size=3><b>Testing for:</b></font><font face='sans-serif' color='#eeee00' size=3>"+
"\na = "+JSON.stringify(ddd)
+"\nb = "+JSON.stringify(eee)
//+"\nc = "+JSON.stringify(fff)
//+"\ncandy = "+JSON.stringify(ggg)
//+", maxW = "+JSON.stringify(hhh)
+"</font>","")
else console.log("<font face='sans-serif' color='#00cc00' size=3><b>Testing for:</b></font><font face='sans-serif' color='#eeee00' size=3>"+
"\nHuge array(omitted)"+"</font>","")
var ans=an(ddd,eee)
var useran=mergeArrays(ddd,eee)
/*
if(JSON.stringify(useran)!=JSON.stringify(ans)) {
failed++
}
*/
if(iii<20) {
Test.assertDeepEquals(useran,ans)
if(JSON.stringify(useran)!=JSON.stringify(ans)) failed++
}
else {
var testresult=check(useran,ans)
if(testresult!="Passed!") failed++
Test.assertEquals(check(useran,ans),"Passed!")
}
}
});
});
//console.log(recfailed)
if(!failed){
describe("Congratulations! You have passed all the tests!", function(){
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.