TEST(Connection, Reconnect) { ConnectionManager::UseTimer = false; Timer::GetInstance().UseVirtualTime(); const BufferAddress addr0(1000); EdgeListener *be0 = EdgeListenerFactory::GetInstance().CreateEdgeListener(addr0); QSharedPointer<RpcHandler> rpc0(new RpcHandler()); Id id0; ConnectionManager cm0(id0, rpc0); cm0.AddEdgeListener(QSharedPointer<EdgeListener>(be0)); be0->Start(); const BufferAddress addr1(10001); EdgeListener *be1 = EdgeListenerFactory::GetInstance().CreateEdgeListener(addr1); QSharedPointer<RpcHandler> rpc1(new RpcHandler()); Id id1; ConnectionManager cm1(id1, rpc1); cm1.AddEdgeListener(QSharedPointer<EdgeListener>(be1)); be1->Start(); ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_FALSE(cm1.GetConnectionTable().GetConnection(id0)); cm1.ConnectTo(addr0); cm0.ConnectTo(addr1); qint64 next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } ASSERT_TRUE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_TRUE(cm1.GetConnectionTable().GetConnection(id0)); cm1.GetConnectionTable().GetConnection(id0)->Disconnect(); next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_FALSE(cm1.GetConnectionTable().GetConnection(id0)); cm1.ConnectTo(addr0); cm0.ConnectTo(addr1); next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } ASSERT_TRUE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_TRUE(cm1.GetConnectionTable().GetConnection(id0)); ConnectionManager::UseTimer = true; }
TEST(Connection, Timeout) { Timer::GetInstance().UseVirtualTime(); SignalCounter sc_new; SignalCounter sc_close; const BufferAddress addr0(1000); QSharedPointer<EdgeListener> be0(EdgeListenerFactory::GetInstance().CreateEdgeListener(addr0)); QSharedPointer<RpcHandler> rpc0(new RpcHandler()); Id id0; ConnectionManager cm0(id0, rpc0); QObject::connect(&cm0, SIGNAL(NewConnection(const QSharedPointer<Connection> &)), &sc_new, SLOT(Counter())); cm0.AddEdgeListener(be0); be0->Start(); cm0.Start(); const BufferAddress addr1(10001); QSharedPointer<EdgeListener> be1(EdgeListenerFactory::GetInstance().CreateEdgeListener(addr1)); QSharedPointer<RpcHandler> rpc1(new RpcHandler()); Id id1; ConnectionManager cm1(id1, rpc1); QObject::connect(&cm1, SIGNAL(NewConnection(const QSharedPointer<Connection> &)), &sc_new, SLOT(Counter())); cm1.AddEdgeListener(QSharedPointer<EdgeListener>(be1)); be1->Start(); cm1.Start(); ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_FALSE(cm1.GetConnectionTable().GetConnection(id0)); cm1.ConnectTo(addr0); RunUntil(sc_new, 2); ASSERT_TRUE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_TRUE(cm1.GetConnectionTable().GetConnection(id0)); QObject::connect(cm0.GetConnectionTable().GetConnection(id1)->GetEdge().data(), SIGNAL(StoppedSignal()), &sc_close, SLOT(Counter())); QObject::connect(cm1.GetConnectionTable().GetConnection(id0)->GetEdge().data(), SIGNAL(StoppedSignal()), &sc_close, SLOT(Counter())); cm0.GetConnectionTable().GetConnection(id1)->GetEdge()->Stop("For fun"); RunUntil(sc_close, 1); ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_TRUE(cm1.GetConnectionTable().GetConnection(id0)); RunUntil(sc_close, 2); ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_FALSE(cm1.GetConnectionTable().GetConnection(id0)); cm1.Stop(); cm0.Stop(); qint64 next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } }
TEST(Connection, SimultaneousDisconnect) { ConnectionManager::UseTimer = false; Timer::GetInstance().UseVirtualTime(); const BufferAddress addr0(1000); EdgeListener *be0 = EdgeListenerFactory::GetInstance().CreateEdgeListener(addr0); QSharedPointer<RpcHandler> rpc0(new RpcHandler()); Id id0; ConnectionManager cm0(id0, rpc0); cm0.AddEdgeListener(QSharedPointer<EdgeListener>(be0)); be0->Start(); const BufferAddress addr1(10001); EdgeListener *be1 = EdgeListenerFactory::GetInstance().CreateEdgeListener(addr1); QSharedPointer<RpcHandler> rpc1(new RpcHandler()); Id id1; ConnectionManager cm1(id1, rpc1); cm1.AddEdgeListener(QSharedPointer<EdgeListener>(be1)); be1->Start(); ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_FALSE(cm1.GetConnectionTable().GetConnection(id0)); cm1.ConnectTo(addr0); cm0.ConnectTo(addr1); qint64 next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } ASSERT_TRUE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_TRUE(cm1.GetConnectionTable().GetConnection(id0)); TestRpc test0; rpc0->Register("Add", &test0, "Add"); TestResponse test1; QSharedPointer<ResponseHandler> res_h( new ResponseHandler(&test1, "HandleResponse")); QVariantList data; data.append(3); data.append(6); ASSERT_EQ(0, test1.GetValue()); rpc1->SendRequest(cm1.GetConnectionTable().GetConnection(id0), "Add", data, res_h); next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } ASSERT_EQ(9, test1.GetValue()); cm1.GetConnectionTable().GetConnection(id0)->Disconnect(); cm0.GetConnectionTable().GetConnection(id1)->Disconnect(); next = Timer::GetInstance().VirtualRun(); while(next != -1) { Time::GetInstance().IncrementVirtualClock(next); next = Timer::GetInstance().VirtualRun(); } ASSERT_FALSE(cm0.GetConnectionTable().GetConnection(id1)); ASSERT_FALSE(cm1.GetConnectionTable().GetConnection(id0)); ConnectionManager::UseTimer = true; }
int main(void) { cout << "main by kk. Last updated 20.09.2011\n"; /////////////////////////////////////////////////////// int it1[] = {84, 23, 84, 21, 120, 93, -131, 238}; int it2[] = {249, 24, 82, 3}; int it3[] = {439, 828, 39, 230, 95, 1, -242, 34}; aghMatrix<int>* imptr1 = new aghMatrix<int>(2, 4); aghMatrix<int>* imptr2 = new aghMatrix<int>(4, 1); aghMatrix<int>* imptr3 = new aghMatrix<int>(4, 1); aghMatrix<int>* imptr4 = new aghMatrix<int>(2, 4); aghMatrix<int> imtest1, imtest2, imtest3; imtest1.setItems(4, 1, 249, 24, 82, 3); imtest2.setItems(2, 4, 500+23, 851, 123, 251, 215, 94, -373, 272); imtest3.setItems(2, 1, 28419, 22084); aghMatrix<int>& imref1 = *imptr1; aghMatrix<int>& imref2 = *imptr2; aghMatrix<int>& imref3 = *imptr3; aghMatrix<int>& imref4 = *imptr4; imptr1->setItems(it1); imptr2->setItems(it2); imref3 = imref2; imptr4->setItems(it3); // 1st test - operator przypisania showTestResult(1, imref3 == imtest1); // 2nd test - operator przypisania try { imref3 = imref3; showTestResult(2, imref3 == imref2); } catch(...) { showTestResult(2, false); } // 3rd test - jawne wywolanie destruktora imref2.~aghMatrix(); try { showTestResult(3, imref3 != imref2); } catch(...) { showTestResult(3, false); } // 4th test - destruktor try { delete imptr2; showTestResult(4, true); } catch(...) { showTestResult(4, false); } // 5th test - kopiowanie wartosci showTestResult(5, imref3 == imtest1); // 6th test - operator dodawania try { showTestResult(6, (imref1+imref4) == imtest2); } catch(...) { showTestResult(6, false); } // 7th test - operator mnożenia aghMatrix<int> immulres; try { aghMatrix<int> immultmp = imref1*imref3; immulres = immultmp; } catch(...) { showTestResult(7, false); } showTestResult(7, immulres == imtest3); // 8th test - range test try { immulres(10, 11); immulres(10, -11); } catch(aghException& e) { showTestResult(8, true); } catch(...) { showTestResult(8, false); } // 9th test - dimesion test try { imref1+imref3; } catch(aghException& e) { showTestResult(9, true); } catch(...) { showTestResult(9, false); } // 10th test - dimesion test try { imref1*imref4; } catch(aghException& e) { showTestResult(10, true); } catch(...) { showTestResult(10, false); } ///////////////////////////////////////////////////// char ct[] = {'b', 'k', 'l', 'd', 'h', 'z', 'd', 'j', 'z', 'x', 'c', 'n', 'b', 'k', 'z', 'd', 'w'}; aghMatrix<char> cm1(3, 2); aghMatrix<char> cm2(3, 2); aghMatrix<char> cm3(2, 4); cm1.setItems(ct); cm2.setItems(ct+5); cm3.setItems(ct+8); aghMatrix<char> cmtest1, cmtest2; cmtest1.setItems(3, 2, 'a', 'n', 'u', 'c', 'e', 'b'); cmtest2.setItems(3, 4, 'j', 't', 's', 'r', 's', 'x', 't', 'w', 's', 'v', 'p', 'k'); // 11th test - operator dodawania try { showTestResult(11, (cm1+cm2) == cmtest1); } catch(...) { showTestResult(11, false); } // 12th test - operator mnożenia try { showTestResult(12, (cm1*cm3) == cmtest2); } catch(...) { showTestResult(12, false); } ///////////////////////////////////////////////////// char* cpt[] = {"to", "jest", "tablica", "wyrazow", "o", "rozmiarze", "jedenascie", "ktore", "beda", "elementami", "macierzy"}; aghMatrix<char*> cpm1(2, 3); aghMatrix<char*> cpm2(2, 3); aghMatrix<char*> cpm3(3, 1); aghMatrix<char*> cpm4; aghMatrix<char*> cpm5; aghMatrix<char*>* cpmptr1 = new aghMatrix<char*>(1, 1); aghMatrix<char*> cpmtest1, cpmtest2; cpm1.setItems(cpt); cpm2.setItems(cpt+5); cpm3.setItems(cpt+3); cpmptr1->setItem(0, 0, cpt[5]); cpmtest1.setItems(2, 3, "torzmiae", "jestdnaci", "tablickore", "wyrazobed", "oelmntai", "rozmiaecy"); cpmtest2.setItems(2, 1, "oai", "wyrazomie"); // 13th test - destruktor char* itemptr = cpmptr1->operator()(0,0); itemptr[0] = 'a'; delete cpmptr1; showTestResult(13, strcmp(itemptr, cpt[5]) != 0); // 14th test - operator przypisania p1 cpm5 = cpm4 = cpm3; showTestResult(14, (cpm3 == cpm4) && (cpm3 == cpm5)); // 15th test - operator przypisania p2 cpm5.setItem(0, 0, "nothing"); showTestResult(15, (cpm3 == cpm4) && (cpm3 != cpm5) && (strcmp(cpm3(0,0), "wyrazow") == 0)); // 16th test - operator przypisania cpm5 = cpm3; try { cpm5 = cpm5; showTestResult(16, cpm3 == cpm5); } catch(...) { showTestResult(16, false); } // 17th test - jawne wywolanie destruktora cpm5 = cpm3; cpm5.~aghMatrix(); try { showTestResult(17, (cpm3 != cpm5)); } catch(...) { showTestResult(17, false); } // 18th test - operator dodawania try { showTestResult(18, (cpm1+cpm2) == cpmtest1); } catch(...) { showTestResult(18, false); } // 19th test - operator mnożenia try { showTestResult(19, (cpm1*cpm3) == cpmtest2); } catch(...) { showTestResult(19, false); } ///////////////////////////////////////////////////// aghComplex cmplxt1[] = {aghComplex(1,1), aghComplex(1,2), aghComplex(2,1), aghComplex(2,2), aghComplex(3,3), aghComplex(3,1), aghComplex(1,0), aghComplex(0,1)}; aghComplex cmplxt2[] = {aghComplex(-1,2), aghComplex(0,3), aghComplex(2,6), aghComplex(3,2), aghComplex(3,4)}; aghMatrix<aghComplex> cmplxm1(3, 2); aghMatrix<aghComplex> cmplxm2(2, 1); aghMatrix<aghComplex> cmplxm3(2, 1); aghMatrix<aghComplex> cmplxm4(2, 1); aghMatrix<aghComplex> cmplxmtest1(3, 1); aghMatrix<aghComplex> cmplxmtest2(2, 1); cmplxm1.setItems(cmplxt1); cmplxm2.setItems(cmplxt1+6); cmplxm3.setItems(cmplxt1+3); cmplxmtest1.setItems(cmplxt2); cmplxmtest2.setItems(cmplxt2+3); cmplxm4 = cmplxm2 + cmplxm3; // 20th test - operator dodawania try { showTestResult(20, cmplxm4 == cmplxmtest2); } catch(...) { showTestResult(20, false); } // 21th test - operator mnozenia try { showTestResult(21, (cmplxm1*cmplxm2) == cmplxmtest1); } catch(...) { showTestResult(21, false); } ///////////////////////////////////////////////////// cout << "Finally, this is the end...\n"; return 0; }
/** * Description not yet available. * \param */ dvar_matrix operator*(const dvar_matrix& m1, const dmatrix& cm2) { if (m1.colmin() != cm2.rowmin() || m1.colmax() != cm2.rowmax()) { cerr << " Incompatible array bounds in " "dmatrix operator*(const dvar_matrix& x, const dmatrix& m)\n"; ad_exit(21); } dmatrix cm1=value(m1); //dmatrix cm2=value(m2); dmatrix tmp(m1.rowmin(),m1.rowmax(), cm2.colmin(), cm2.colmax()); #ifdef OPT_LIB const size_t rowsize = (size_t)cm2.rowsize(); #else const int _rowsize = cm2.rowsize(); assert(_rowsize > 0); const size_t rowsize = (size_t)_rowsize; #endif try { double* temp_col = new double[rowsize]; temp_col-=cm2.rowmin(); for (int j=cm2.colmin(); j<=cm2.colmax(); j++) { for (int k=cm2.rowmin(); k<=cm2.rowmax(); k++) { temp_col[k] = cm2.elem(k,j); } for (int i=cm1.rowmin(); i<=cm1.rowmax(); i++) { double sum=0.0; dvector& temp_row = cm1(i); for (int k=cm1.colmin(); k<=cm1.colmax(); k++) { sum+=temp_row(k) * (temp_col[k]); // sum+=temp_row(k) * cm2(k,j); } tmp(i,j)=sum; } } temp_col+=cm2.rowmin(); delete [] temp_col; temp_col = 0; } catch (std::bad_alloc& e) { cerr << "Error[" << __FILE__ << ':' << __LINE__ << "]: Unable to allocate array.\n"; //ad_exit(21); throw e; } dvar_matrix vtmp=nograd_assign(tmp); save_identifier_string("TEST1"); //m1.save_dvar_matrix_value(); m1.save_dvar_matrix_position(); cm2.save_dmatrix_value(); cm2.save_dmatrix_position(); vtmp.save_dvar_matrix_position(); save_identifier_string("TEST6"); gradient_structure::GRAD_STACK1-> set_gradient_stack(dmcm_prod); return vtmp; }
void resultCombiner(const char * name_dv1, const char * name_cm1, const int length1, const char * name_dv2, const char * name_cm2, const int length2, int ftr = 1, const char * outputNameStub = "output") { TVectorD dv1(length1), dv2(length2); TMatrixT<double> cm1(length1, length1), cm2(length2, length2); double binLimits1[length1][2], binLimits2[length2][2], ccCov1[length1], ccCov2[length1]; readDataVector(name_dv1, dv1, binLimits1, ftr, ccCov1); printf("Read data vector 1 (%d)\n",length1); readDataVector(name_dv2, dv2, binLimits2, ftr, ccCov2); printf("Read data vector 2 (%d)\n",length2); readCovMatrix(name_cm1, cm1); printf("Read covariance matrix 1\n"); readCovMatrix(name_cm2, cm2); printf("Read covariance matrix 2\n"); std::vector<double*> binLimits; std::vector<std::vector<int > > preU; int i1 = 0, i2 = 0; while(i1 < length1 || i2 < length2) { if(i1 < length1 && i2 < length2) { if((binLimits1[i1][1] + binLimits1[i1][0])/2 > binLimits2[i2][0] && (binLimits1[i1][1] + binLimits1[i1][0])/2 < binLimits2[i2][1]) { binLimits.push_back(binLimits1[i1]); std::vector<int> tmp; tmp.push_back(i1); tmp.push_back(i2); preU.push_back(tmp); i1++; i2++; } else if((binLimits1[i1][1] + binLimits1[i1][0])/2 <= binLimits2[i2][0]) { binLimits.push_back(binLimits1[i1]); std::vector<int> tmp; tmp.push_back(i1); tmp.push_back(-1); preU.push_back(tmp); i1++; } else { binLimits.push_back(binLimits2[i2]); std::vector<int> tmp; tmp.push_back(-1); tmp.push_back(i2); preU.push_back(tmp); i2++; } } else if(i1 < length1 && i2 >= length2) { binLimits.push_back(binLimits1[i1]); std::vector<int> tmp; tmp.push_back(i1); tmp.push_back(-1); preU.push_back(tmp); i1++; } else { binLimits.push_back(binLimits2[i2]); std::vector<int> tmp; tmp.push_back(-1); tmp.push_back(i2); preU.push_back(tmp); i2++; } } TVectorD dv(length1 + length2); for(int i = 0; i < length1 + length2; i++) { dv[i] = (i < length1) ? dv1[i] : dv2[i - length1]; } TMatrixT<double> cm(length1 + length2, length1 + length2), U(length1 + length2, preU.size()); for(int i = 0; i < length1; i++) { for(int j = 0; j < length1; j++) { cm[i][j] = cm1[i][j]; } } for(int i = length1; i < length1 + length2; i++) { for(int j = length1; j < length1 + length2; j++) { cm[i][j] = cm2[i - length1][j - length1]; } } for(unsigned int i = 0; i < preU.size(); i++) { if(preU[i][0] >= 0) U[preU[i][0]][i] = 1; if(preU[i][1] >= 0) U[preU[i][1] + length1][i] = 1; if(ftr > 1 && preU[i][0] >= 0 && preU[i][1] >= 0) cm[preU[i][0]][preU[i][1] + length1] = cm[preU[i][1] + length1][preU[i][0]] = ccCov1[preU[i][0]]*ccCov2[preU[i][1]]; } // cm.Print(); TMatrixT<double> Ut(U); Ut.T(); TMatrixT<double> cmInv(cm); cmInv.Invert(); TMatrixT<double> step1 = Ut * cmInv * U; TMatrixT<double> step2 = Ut * cmInv; TMatrixT<double> lambda = step1.Invert() * step2; TVectorD bV = lambda*dv; TMatrixT<double> bcm = (Ut * cmInv * U).Invert(); printf("Done with combination.\n"); //write output FILE *file; char bVoutName[128], CMoutName[128]; sprintf(bVoutName, "%s_data.txt", outputNameStub); file = fopen(bVoutName, "w"); if(file) { fprintf(file, "#\n#%9s %9s %9s %15s %15s\n", "Bin", "Y_min", "Y_max", "Value", "Uncertainty"); for(int i = 0; i < bV.GetNoElements(); i++) { fprintf(file, " %9i %9.2f %9.2f %15e %15e\n", i + 1, binLimits[i][0], binLimits[i][1], bV[i], sqrt(bcm[i][i])); } fclose(file); } sprintf(CMoutName, "%s_covMat.txt", outputNameStub); file = fopen(CMoutName, "w"); if(file) { fprintf(file, "#\n#%9s %9s %15s\n", "Bin i", "Bin j", "Value"); for(int i = 0; i < bcm.GetNrows(); i++) { for(int j = 0; j < bcm.GetNcols(); j++) { fprintf(file, " %9i %9i %15e\n", i + 1, j + 1, bcm[i][j]); } } fclose(file); } printf("Output complete.\n"); }