/*! */ void TestHsDomainModel::testWidgetComponent() { QScopedPointer<HsWidgetComponent> compo(new HsWidgetComponent("mockwidgetplugin")); QVERIFY(!compo.isNull()); QString path = compo->rootPath(); QVERIFY(!compo->isAvailable()); // is not in ca store QVERIFY(compo->uri() == "mockwidgetplugin"); compo->uninstallTranslator(); }
void TestHsDomainModel::testWidgetComponentEmits() { QScopedPointer<HsWidgetComponent> compo(new HsWidgetComponent("mockwidgetplugin")); QSignalSpy aboutToUninstallSpy(compo.data(),SIGNAL(aboutToUninstall())); QSignalSpy uninstalledSpy(compo.data(),SIGNAL(uninstalled())); QSignalSpy unavailableSpy(compo.data(),SIGNAL(unavailable())); QSignalSpy availableSpy(compo.data(),SIGNAL(available())); QSignalSpy updatedSpy(compo.data(),SIGNAL(updated())); compo->emitAboutToUninstall(); QVERIFY(aboutToUninstallSpy.count() == 1); // should not emit again compo->emitAboutToUninstall(); QVERIFY(aboutToUninstallSpy.count() == 1); compo->emitUninstalled(); QVERIFY(uninstalledSpy.count() == 1); // should not emit again compo->emitUninstalled(); QVERIFY(uninstalledSpy.count() == 1); compo->emitUnavailable(); QVERIFY(unavailableSpy.count() == 1); // should not emit again compo->emitUnavailable(); QVERIFY(unavailableSpy.count() == 1); compo->emitAvailable(); QVERIFY(availableSpy.count() == 1); // should not emit again compo->emitAvailable(); QVERIFY(availableSpy.count() == 1); compo->emitAboutToUninstall(); compo->emitUpdated(); QVERIFY(updatedSpy.count() == 1); // should not emit again compo->emitUpdated(); QVERIFY(updatedSpy.count() == 1); }
void ga() { static char taken[MAXCHIPS]; double prob; int gen=1,i,j,k,ix,iy,a,b,t,*p,*q,*r,rev; int best=-1; popdata=malloc(sizeof(int)*chips*(POP+CAND)); if(!popdata) error("out of memory"); initpop(); while(1) { if(gen%100==0) printf("start of generation %d\n",gen); gen++; qsort(pop,POP,sizeof(ind_t),compo); if(best!=pop[0].cost) { best=pop[0].cost; printind(0); } normalizecumul(); nextp=POP; for(i=0;i<CAND;i++) { again: prob=rand01(); if(prob<UNI1) { /* mutation operator 1: swap 2 nodes */ ix=pickparent(); copyind(POP+i,ix); /* swap 2 random elements */ a=rand()%chips; do b=rand()%chips; while(a==b); p=popdata+pop[POP+i].dataptr*chips; t=p[a]; p[a]=p[b]; p[b]=t; fixind(p); if(!compo(pop+POP+i,pop+ix)) goto again; /* if no change, discard */ pop[POP+i].cost=findcost(p); pop[POP+i].fitness=calcfitness(pop[POP+i].cost); nextp++; } else if(prob<UNI1+UNI2) { /* mutation operator 2: move 1 node */ ix=pickparent(); copyind(POP+i,ix); /* pick random element, and new position for it */ a=rand()%chips; do b=rand()%chips; while(a==b); p=popdata+pop[ix].dataptr*chips; q=popdata+pop[POP+i].dataptr*chips; for(j=0;j<chips;j++) q[j]=-1; q[b]=p[a]; for(j=k=0;j<chips;j++) if(j!=a) { while(k<chips && q[k]>-1) k++; if(k==chips) printf("sanity error"); q[k++]=p[j]; } fixind(q); pop[POP+i].cost=findcost(p); pop[POP+i].fitness=calcfitness(pop[POP+i].cost); nextp++; } else { /* crossover operator: copy substring from parent 1, take remaining nodes from parent 2 in order of occurrence */ ix=pickparent(); do iy=pickparent(); while(ix==iy); copyind(POP+i,iy); a=rand()%chips; b=rand()%chips; if(a>b) t=a,a=b,b=t; b++; do t=rand()%chips; while(t+b-a>chips); rev=rand()&1; p=popdata+pop[ix].dataptr*chips; q=popdata+pop[iy].dataptr*chips; r=popdata+pop[POP+i].dataptr*chips; for(k=0;k<chips;k++) r[k]=-1; for(k=0;k<chips;k++) taken[k]=0; if(rev) for(j=0;j<b-a;j++) taken[p[j+a]]++,r[t+b-a-j-1]=p[j+a]; else for(k=a;k<b;k++) taken[p[k]]++,r[k+t-a]=p[k]; for(k=j=0;k<chips;k++) if(!taken[q[k]]) { while(j<chips && r[j]>-1) j++; if(j==chips) printf("sanity error"); r[j++]=q[k]; } fixind(r); if(!compo(pop+POP+i,pop+ix)) goto again; /* if no change, discard */ if(!compo(pop+POP+i,pop+iy)) goto again; /* if no change, discard */ pop[POP+i].cost=findcost(r); pop[POP+i].fitness=calcfitness(pop[POP+i].cost); nextp++; } } /* sort all children */ qsort(pop+POP,CAND,sizeof(ind_t),compo); /* ensure we only have unique elitists */ /* here shallow copy is ok! */ if(ELITISM) for(j=i=1;i<ELITISM;i++) { if(compo(pop+i,pop+i-1)) pop[j++]=pop[i]; } else j=0; pop[j++]=pop[POP]; /* ensure we only have unique children */ for(i=POP+1;j<POP && i<POP+CAND;i++) if(compo(pop+i,pop+i-1)) pop[j++]=pop[i]; if(j<POP) error("ga failed, need more unique children"); compress(); } }