コード例 #1
0
  ComplexFloatArrayTestPatch(){
    success=true;
    debugMessage("start");
    passed=0;
    failed=0;
    int size=100;
    ComplexFloat data[size];
    ComplexFloat backupData[size];
    for(int n=0; n<size; n++){ //initialize array
      float value= n<size/2 ? n : size-n; 
      data[n].re=backupData[n].re=value;
      data[n].im=backupData[n].im=-value;
    }
    ComplexFloatArray cfa(data,size);
    ComplexFloatArray backupcf(backupData,size);
    ComplexFloat tempcData[size]; 
    ComplexFloatArray tempc(tempcData,size);
    ComplexFloat tempc2Data[size]; 
    ComplexFloatArray tempc2(tempc2Data,size);
    float tempfData[size]; 
    FloatArray tempf(tempfData,size);
        
    assertr(cfa.getSize()==size,"ComplexFloatArray.getSize()");
    for(int n=0; n<size; n++){ 
      //checking cast and indexing operators
      assertr(cfa[n].re==data[n].re,"ComplexFloat& operator []");
      assertr(cfa[n].im==data[n].im,"ComplexFloat& operator []");
      assertr((ComplexFloat*)cfa==data,"(ComplexFloat*)cfa==data");
      assertr((float*)cfa==(float *)data,"(ComplexFloat*)cfa==data");
      assertr(&cfa[n]==&data[n],"cfa[n]==data[n]");
      
      //check that indexing works properly overlaying struct and float
      float *f=(float*)data;
      assertr(data[n].re==f[2*n],"data[n].re==f[2*n]");
      assertr(data[n].im==f[2*n+1],"data[n].im==f[2*n+1]");
      //checking scalar complex math functions
      assertr(cfa.mag(n)==sqrtf(f[2*n]*f[2*n]+f[2*n+1]*f[2*n+1]),"mag");
      assertr(cfa.mag2(n)==f[2*n]*f[2*n]+f[2*n+1]*f[2*n+1],"mag2");
    }
    
    //test equals(ComplexFloatArray) method
    {
      tempc.copyFrom(cfa);
      assertr(cfa.equals(tempc), ".equals()");
      
    }
    cfa.getMagnitudeValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==sqrtf(cfa[n].re*cfa[n].re+cfa[n].im*cfa[n].im),"getMagnitudeValues", n);
    }
    cfa.getMagnitudeSquaredValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==cfa[n].re*cfa[n].re+cfa[n].im*cfa[n].im,"getMagnitudeSquaredValues");
    }
    cfa.getComplexConjugateValues(tempc);
    for(int n=0; n<size; n++){
      assertr(tempc[n].re==cfa[n].re,"getComplexConjugateValues-real");
      assertr(tempc[n].im==-cfa[n].im,"getComplexConjugateValues-imag");
    }
    cfa.complexByComplexMultiplication(tempc, tempc2);
    for(int n=0; n<size; n++){
      assertr(tempc2[n].re==cfa[n].re*tempc[n].re - cfa[n].im*tempc[n].im,"complexByComplexMultiplication-real");
      assertr(tempc2[n].im==cfa[n].re*tempc[n].im + cfa[n].im*tempc[n].re,"complexByComplexMultiplication-imag");
    }
    cfa.complexByRealMultiplication(tempf, tempc2);
    for(int n=0; n<size; n++){
      assertr(tempc2[n].re==cfa[n].re*tempf[n],"complexByRealMultiplication-real");
      assertr(tempc2[n].im==cfa[n].im*tempf[n],"complexByRealMultiplication-imag");
    }
    
  //ComplexDotProduct
    ComplexFloat tempcf;
    for(int n=0; n<size; n++){
      tempcData[n].re=backupData[n].re=n*2;
      tempcData[n].im=backupData[n].im=-(n*2+1);
    }
    cfa.complexDotProduct(tempc, tempcf);
    float realResult=0;
    float imagResult=0;
    for(int n=0; n<size; n++) {    
      realResult += cfa[n].re*tempc[n].re - cfa[n].im*tempc[n].im;
      imagResult += cfa[n].re*tempc[n].im + cfa[n].im*tempc[n].re;
    }
    assertr(realResult==tempcf.re,"duct-re");
    assertr(imagResult==tempcf.im,"duct-im");

    float maxMagVal=-1;
    int maxMagInd=-1;
    for(int n=0; n<size; n++){
      if(cfa.mag(n)>maxMagVal){
        maxMagVal=cfa.mag(n);
        maxMagInd=n;
      }
    }
    assertr(maxMagVal==cfa.getMaxMagnitudeValue(),"getMaxMagnitudeValue()");
    assertr(maxMagInd==cfa.getMaxMagnitudeIndex(),"getMaxMagnitudeIndex()");
    
    cfa.getRealValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==cfa[n].re,"getRealValues");
    }
    cfa.getImaginaryValues(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==cfa[n].im,"getImaginaryValues");
    }
    
    //test setAll
    // test setAll(float)
    {
      for(int n=0; n<size; n++){
        tempc[n].re=1;
        tempc[n].im=1;
      }
      float allValue=0.1;
      tempc.setAll(allValue);
      for(int n=0; n<size; n++){
        assertr(tempc[n].re==allValue, "setAll(float) real");
        assertr(tempc[n].im==allValue, "setAll(float) imag");
      }
    }
      // test setAll(float, float)
    {
      float allValueRe=0.3;
      float allValueIm=-0.3;
      tempc.setAll(allValueRe, allValueIm);
      for(int n=0; n<size; n++){
        assertr(tempc[n].re==allValueRe, "setAll(float, float) real");
        assertr(tempc[n].im==allValueIm, "setAll(float, float) imag");
      }
    }
      // test setAll(ComplexFloat)
    {
      ComplexFloat allValue;
      allValue.re=0.4;
      allValue.im=-0.1;
      tempc.setAll(allValue);
      for(int n=0; n<size; n++){
        assertr(tempc[n].re==allValue.re, "setAll(ComplexFloat) real");
        assertr(tempc[n].im==allValue.im, "setAll(ComplexFloat) imag");
      }
    }
    
    //test copyFrom
    //test copyFrom(FloatArray)
    tempc.setAll(0);
    tempc.copyFrom(tempf);
    for(int n=0; n<size; n++){
      assertr(tempf[n]==tempc[n].re, "copyFrom(FloatArray)");
    }
    //test copyFrom(ComplexFloatArray)
    tempc.setAll(0);
    tempc.copyFrom(cfa);
    for(int n=0; n<size; n++){
      assertr(cfa[n].re==tempc[n].re, "copyFrom(ComplexFloatArray), real");
      assertr(cfa[n].im==tempc[n].im, "copyFrom(ComplexFloatArray), imag");
    }
    //test copyFrom(ComplexFloat *, int)
    tempc.setAll(0);
    tempc.copyFrom(cfa.getData(), cfa.getSize());
    for(int n=0; n<size; n++){
      assertr(cfa[n].re==tempc[n].re, "copyFrom(ComplexFloat*, int), real");
      assertr(cfa[n].im==tempc[n].im, "copyFrom(ComplexFloat*, int), imag");
    }
    
      //test ComplexFloat methods

    {
      ASSERT(tempc.getSize()==size,"tempc");
      //test ComplexFloat.setPolar()
      ComplexFloat i;
      i.re=0;
      i.im=0;
      float magnitude=rand()/(float)RAND_MAX*10;
      float phase=rand()/(float)RAND_MAX*2*M_PI - M_PI;
      i.setPolar(magnitude, phase);
      assertr(i.re=magnitude*cosf(phase),"ComplexFloat setPolar() real");
      assertr(i.im=magnitude*sinf(phase),"ComplexFloat setPolar() imag");
      //test ComplexFloat.getPhase()
      assertt(i.getPhase(),phase, "ComplexFloat getPhase()");
      //test ComplexFloat.getMagnitude()
      // debugMessage("magnitude",i.getMagnitude(),magnitude, phase);
      assertt(i.getMagnitude(),magnitude, "ComplexFloat getMagnitude()", 0.0001);
    }
    {
      //test ComplexFloat.setMagnitude()
      ComplexFloat i;
      float magnitude=rand()/(float)RAND_MAX*10;
      float phase=rand()/(float)RAND_MAX*2*M_PI - M_PI;
      i.setPolar(magnitude,phase);
      float newMagnitude=magnitude+1;
      i.setMagnitude(newMagnitude);
      assertt(i.getMagnitude(), newMagnitude, "ComplexFloat setMagnitude() magnitude", 0.0001);
      assertt(i.getPhase(), phase, "ComplexFloat setMagnitude() phase", 0.0001); //check that the phase has not changed
    }
    {
      //test ComplexFloat.setPhase()
      ComplexFloat i;
      float magnitude=rand()/(float)RAND_MAX*10;
      float phase=rand()/(float)RAND_MAX*2*M_PI - M_PI;
      i.setPolar(magnitude,phase);
      float newPhase=phase+1;
      i.setPhase(newPhase);
      assertt(i.getPhase(), newPhase, "ComplexFloat setPhase() phase", 0.0001);
      assertt(i.getMagnitude(), magnitude, "ComplexFloat setPhase() magnitude", 0.0001); //check that the magnitude has not changed
    }
    {
      // test ComplexFloatArray.setPhase()
      tempc.copyFrom(cfa);
      FloatArray phase=FloatArray::create(size);
      phase.noise(-M_PI, M_PI);
      // setPhase(FloatArray)
      tempc.setPhase(phase);
      for(int n=0; n<size; n++){
        if(cfa[n].getMagnitude()<0.001)
          continue; //skip small values of magnitude which would make the phase noisy
        assertt(tempc[n].getPhase(), phase[n], "setPhase() phase", 0.01);
        assertt(tempc[n].getMagnitude(), cfa[n].getMagnitude(), "setPhase() magnitude", 0.01);
      }
      // setPhase(FloatArray, int, int)
      int offset=size/3;
      int count= size/2;
      tempc.copyFrom(cfa);
      tempc.setPhase(phase, offset, count);
      for(int n=0; n<size; n++){
        if(n>=offset && n<offset+count){
          if(cfa[n].getMagnitude()<0.001)
            continue; //skip small values of magnitude which would make the phase noisy
          assertt(tempc[n].getPhase(), phase[n], "setPhase() phase wrong", 0.01);
          assertt(tempc[n].getMagnitude(), cfa[n].getMagnitude(), "setPhase() magnitude wrong", 0.01);
        } else { //check that values outside the range have not changed
          assertr(tempc[n].getPhase()==cfa[n].getPhase(), "setPhase() phase changed", 0);
          assertr(tempc[n].getMagnitude()==cfa[n].getMagnitude(), "setPhase() magnitude changed", 0);
        }
      }
      
      // test ComplexFloatArray.setMagnitude()
      tempc.copyFrom(cfa);
      FloatArray magnitude=FloatArray::create(size);
      magnitude.noise(0, 10);
      // setMagnitude(FloatArray)
      tempc.setMagnitude(magnitude);
      for(int n=0; n<size; n++){
        if(cfa[n].getMagnitude()<0.001 || tempc[n].getMagnitude()<0.001)
          continue; //skip small values of magnitude which would make the phase noisy
        assertt(tempc[n].getPhase(), cfa[n].getPhase(), "setMagnitude() phase", 0.01);
        assertt(tempc[n].getMagnitude(), magnitude[n], "setMagnitude() magnitude", 0.01);
      }
      // setMagnitude(FloatArray, int, int)
      offset=size/3.0f;
      count=size/2.0f;
      tempc.copyFrom(cfa);
      tempc.setMagnitude(magnitude, offset, count);
      for(int n=0; n<size; n++){
        if(n>=offset && n<offset+count){
          if(cfa[n].getMagnitude()<0.001 || tempc[n].getMagnitude()<0.001)
            continue; //skip small values of magnitude which would make the phase noisy
          assertt(tempc[n].getPhase(), cfa[n].getPhase(), "setMagnitude() phase", 0.01);
          assertt(tempc[n].getMagnitude(), magnitude[n], "setMagnitude() magnitude", 0.01);
        } else { //check that values outside the range have not changed
          if(abs(tempc[n].getPhase()-cfa[n].getPhase())>0){
            debugMessage("error", (float)n, tempc[n].getPhase(), cfa[n].getPhase());
            return;
          }
          assertr(tempc[n].getPhase()==cfa[n].getPhase(), "setMagnitude()t phase");
          assertr(tempc[n].getMagnitude()==cfa[n].getMagnitude(), "setMagnitude() magnitude");
        }
      }
      FloatArray::destroy(phase);
      FloatArray::destroy(magnitude);
    }
/*
    ComplexFloatArray subarray(int offset, int length);
*/
    if(success==true){
      debugMessage("Tests passed",passed);
    }
  };
コード例 #2
0
ファイル: teinifile.cpp プロジェクト: app/tradeequip
/*! This function updates .ini file in two stages. It copies original .ini file
  into temporary one line by line, replacing/deleting/adding lines as necessary.
  Then it copies temporary file into original one and deletes temporary file.
*/
bool TEIniFile::update()
{
  close();
	if (!f.open(IO_ReadWrite)) return false;
  QString tempfn=f.name()+".tmp";
  QFile tempf(tempfn);
  if (!tempf.open(IO_ReadWrite | IO_Truncate)) return false;
  QTextStream tsorg;
  bool skipsec=false;

  tsorg.setEncoding(QTextStream::UnicodeUTF8);
  tsorg.setDevice(&f);
	ts.setDevice(&tempf); // writeXXX function hardwired to write into ts. So we set it appropriatedly.
	// prepare
  QMap<QString,bool> secProcd, valueProcd; // processedp flag's maps
  QMapIterator<QString,type_ValueList> it1;
  for(it1=SectionList.begin();it1!=SectionList.end();++it1)
  {
    secProcd[it1.key()]=false;
  }
	// scan form sections
	QString line, sec;
	type_Processing pr = P_NONE;	// nothing done yet
	type_ValueList ValueList;

	while (!((line = tsorg.readLine()).isNull())) {
		if ((pr == P_NONE) || (pr == P_SECTION)) {
			// trim line
			line = line.stripWhiteSpace();
			// skip if empty
			if (line.isEmpty())
      {
        writeBreak();
        continue;
      }
			// skip if comment
			if (line.startsWith("#"))
      {
        writeComment(line.mid(1));
        continue;
      }
			// check if section
			if (line.startsWith("[") && line.endsWith("]")) {
				if (pr == P_SECTION) {
					// update previous section
          // write new values
          type_ValueList::iterator it;
          type_ValueList & curSec=SectionList[sec];
          type_ValueList & curSecDef=SectionListDef[sec];
          for(it=curSec.begin();it!=curSec.end();++it)
          {
            if (!valueProcd[it.key()])
            { // this value was not processed yet, hence it's new or just default.
              if (curSecDef.find(it.key())==curSecDef.end() || curSecDef[it.key()]!=it.data()) // if it equals default value, skip it.
                ts << it.key() << "\t" << it.data() << "\n";
              valueProcd[it.key()]=true; // it is for completeness' sake, so it don't really necessary (yet?).
            }
          }
					secProcd[sec]=true;
				}
				// section found!
        valueProcd.clear(); // get ready for next section
				pr = P_SECTION;
				sec = line.mid(1, line.length()-2).stripWhiteSpace();
        writeSection(sec);
        skipsec=SectionList.find(sec)==SectionList.end() || secProcd[sec]==true; // Skip deleted or already processed section
        type_ValueList & curSec=SectionList[sec];
        type_ValueList::iterator it;
        for(it=curSec.begin();it!=curSec.end();++it)
        {
          valueProcd[it.key()]=false;
        }
				ValueList.clear();
				continue;
			}
			if (pr == P_SECTION) {
				// read name
        if (skipsec)
          continue;
				QString nam;
				int j = 0, l = (int)line.length();
				QChar c;
				while ( (j < l) && !((c = line.ref((uint)j)).isSpace()) ) {
					nam += c;
					j++;
				}
				// read value
        bool rawData=false;
        bool doubleQuotes=false;
				QString val;
				val = line.mid((uint)j).stripWhiteSpace();
				if (val == "data{") {
          rawData=true;
					// read raw data...
					val = "";
					while (!((line = ts.readLine()).isNull())) {
						if (line == "}data") {
							ValueList[nam] = val;
							break;
						}
						val += line + "\n";
					}
				} else {
					// test for ""
					if (val.startsWith("\"") && val.endsWith("\""))
          {
            doubleQuotes=true;
						val = val.mid(1, val.length()-2).stripWhiteSpace();
          }
					ValueList[nam] = val;
				}
        if (SectionList[sec].find(nam)!=SectionList[sec].end() && valueProcd[nam]==false)
        { // write modified value
          if (rawData)
            writeData(nam,SectionList[sec][nam]);
          else if (doubleQuotes)
            writeString(nam,SectionList[sec][nam]);
          else
            ts << nam << "\t" << SectionList[sec][nam] << "\n";
          valueProcd[nam]=true;
        }
			}
      else
        writeComment(line);
		}
	}

	if (pr == P_SECTION) {
		// update last section
    // write new values
    type_ValueList::iterator it;
    type_ValueList & curSec=SectionList[sec];
    type_ValueList & curSecDef=SectionListDef[sec];
    for(it=curSec.begin();it!=curSec.end();++it)
    {
      if (!valueProcd[it.key()])
      { // this value was not processed yet, hence it's new.
        if (curSecDef.find(it.key())==curSecDef.end() || curSecDef[it.key()]!=it.data())
          ts << it.key() << "\t" << it.data() << "\n";
        valueProcd[it.key()]=true; // it is for completeness' sake, so it don't really necessary (yet?).
      }
    }
		secProcd[sec]=true;
	}
  QMapIterator<QString,bool> it;
  for(it=secProcd.begin();it!=secProcd.end();++it)
  {
    QString sec=it.key();
    if (!it.data())
    {
      writeSection(sec);
      type_ValueList & curSec=SectionList[sec];
      type_ValueList & curSecDef=SectionListDef[sec];
      type_ValueList::iterator it1;
      for(it1=curSec.begin();it1!=curSec.end();++it1)
        if (curSecDef.find(it1.key())==curSecDef.end() || curSecDef[it1.key()]!=it1.data())
          ts << it1.key() << "\t" << it1.data() << "\n";
    }
  }
  tempf.flush();
  if (!tempf.reset())
    return false;
  f.close();
  if (!f.open(IO_WriteOnly | IO_Truncate))
    return false;
  const int N=64*1024;
  char * buf=new char[N];
  Q_LONG len;
  while(true)
  {
    len=tempf.readBlock(buf,N);
    if (len<=0)
      break;
    f.writeBlock(buf,(Q_ULONG)len);
  }
  delete [] buf;
  f.close();
  tempf.close();
  tempf.remove();
  ts.setDevice(&f);
	return len>=0;
}