bool VectorTest::performTest() { srand((unsigned)time(NULL)); const Elem cv_data[] = { 1, 2, 3 }; const Elem rv_data[] = { -4, 0, 8 }; ColVector cv(3, cv_data); RowVector rv(3, rv_data); cout << "Vector cv: " << cv << endl << "Vector rv: " << rv << endl; // Shift { const Elem cv_left[] = { 2, 3, 0 }; const Elem cv_right[] = { 0, 1, 2 }; ColVector cvl(cv); cvl.shiftLeft(); cout << "Vector rv shifted left: " << cvl << endl; if (ColVector(3, cv_left) != cvl) return false; ColVector cvr(cv); cvr.shiftRight(); cout << "Vector rv shifted right: " << cvr << endl; if (ColVector(3, cv_right) != cvr) return false; } // rv + rv { RowVector result = rv + rv; cout << "rv + rv = " << result << endl; for (unsigned int i = 0; i < rv.dim(); i++) { if (!epsilonCheck(result(i), rv(i) + rv(i))) return false; } } // rv - rv { RowVector result = rv - rv; cout << "rv - rv = " << result << endl; for (unsigned int i = 0; i < rv.dim(); i++) { if (!epsilonCheck(result(i), rv(i) - rv(i))) return false; } } // cv + cv { ColVector result = cv + cv; cout << "cv + cv = " << result << endl; for (unsigned int i = 0; i < cv.dim(); i++) { if (!epsilonCheck(result(i), cv(i) + cv(i))) return false; } } // cv - cv { ColVector result = cv - cv; cout << "cv - cv = " << result << endl; for (unsigned int i = 0; i < cv.dim(); i++) { if (!epsilonCheck(result(i), cv(i) - cv(i))) return false; } } // Operators += and -= { ColVector cvc(cv); cout << "cvc = " << cvc << endl; cvc += cv; cout << "cvc += cv => " << cvc << endl; if (!epsilonCheck(cvc(0), 2) || !epsilonCheck(cvc(1), 4) || !epsilonCheck(cvc(2), 6)) return false; cvc -= cv; cout << "cvc -= cv => " << cvc << endl; if (!epsilonCheck(cvc(0), 1) || !epsilonCheck(cvc(1), 2) || !epsilonCheck(cvc(2), 3)) return false; RowVector rvc(rv); // -4 0 8 cout << "rvc = " << rvc << endl; rvc += rv; cout << "rvc += rv => " << rvc << endl; if (!epsilonCheck(rvc(0), -8) || !epsilonCheck(rvc(1), 0) || !epsilonCheck(rvc(2), 16)) return false; rvc -= rv; cout << "rvc -= rv => " << rvc << endl; if (!epsilonCheck(rvc(0), -4) || !epsilonCheck(rvc(1), 0) || !epsilonCheck(rvc(2), 8)) return false; } // Operator *= { ColVector cvc(cv); cout << "cvc = " << cvc << endl; cvc *= 5; cout << "cvc *= 5 => " << cvc << endl; if (!epsilonCheck(cvc(0), 5) || !epsilonCheck(cvc(1), 10) || !epsilonCheck(cvc(2), 15)) return false; } // rv * cv { const Elem inner_prod = rv * cv; cout << "rv * cv = " << inner_prod << endl; if (!epsilonCheck(inner_prod, 20)) return false; } // cv * rv { const Matrix cvrv(cv * rv); cout << "cv * rv = " << endl << cvrv; const Elem correct_cvrv_data[] = { -4, 0, 8, -8, 0, 16, -12, 0, 24 }; const Matrix correct_cvrv(3, 3, correct_cvrv_data); if (correct_cvrv != cvrv) return false; } // cv * rv larger test { int n_tests = 100; for (int k = 0; k < n_tests; ++k) { cout << "cv * rv #" << k << endl; const int m = 25; const int n = 500; double* data1 = new double[m]; double* data2 = new double[n]; for (int i = 0; i < m; ++i) data1[i] = generators::random(i); for (int i = 0; i < n; ++i) data2[i] = generators::random(i); ColVector randomCv(m, data1); RowVector randomRv(n, data2); Matrix cvAsMatrix(m, 1, data1); Matrix rvAsMatrix(1, n, data2); Matrix ref(m, n); cvAsMatrix.multWithMatrix(rvAsMatrix, &ref); Matrix res(randomCv * randomRv); if (ref != res) return false; delete[] data2; delete[] data1; } } // cv * 5 { ColVector tmp = cv * 5; cout << "cv * 5 = " << tmp << endl; if (!epsilonCheck(tmp(0), 5) || !epsilonCheck(tmp(1), 10) || !epsilonCheck(tmp(2), 15)) return false; } // 4 * cv { ColVector tmp = 4 * cv; cout << "4 * cv = " << tmp << endl; if (!epsilonCheck(tmp(0), 4) || !epsilonCheck(tmp(1), 8) || !epsilonCheck(tmp(2), 12)) return false; } // rv * 5 { RowVector tmp = rv * 5; cout << "rv * 5 = " << tmp << endl; if (!epsilonCheck(tmp(0), -20) || !epsilonCheck(tmp(1), 0) || !epsilonCheck(tmp(2), 40)) return false; } // 4 * rv { RowVector tmp = 4 * rv; cout << "4 * rv = " << tmp << endl; if (!epsilonCheck(tmp(0), -16) || !epsilonCheck(tmp(1), 0) || !epsilonCheck(tmp(2), 32)) return false; } // Maximum, minimum const Elem mv_data[] = { -16, -1, 4, 8 }; RowVector mv(4, mv_data); cout << "mv = " << mv << endl; Elem m = mv.minimum(); cout << "Minimum of mv: " << m << endl; if (m != -16) return false; m = mv.minimum(true); cout << "Minimum of mv (absolute): " << m << endl; if (m != -1) return false; m = mv.maximum(); cout << "Maximum of mv: " << m << endl; if (m != 8) return false; m = mv.maximum(true); cout << "Maximum of mv (absolute): " << m << endl; if (m != -16) return false; // Length cout << "Length of rv: " << rv.length() << endl; if (!epsilonCheck(rv.length(), 8.94427191)) return false; // Angle between cv and rv cout << "Angle between cv and rv: " << Vector::angle(cv, rv) << endl; if (!epsilonCheck(Vector::angle(cv, rv), 0.930274014115)) return false; // Random vector of unit length ColVector randcv(3, generators::random); cout << "Random vector: " << randcv << endl; // Silent operator() check // In case of errors this would fail during compile time { ColVector a(5); const ColVector b(3); a(1) = b(2); } // Dump and read { const Elem v_data[] = { 3, 2.5, 1.5, -1, -2.5, -1.5 }; RowVector rv(6, v_data); ColVector cv(6, v_data); Poco::TemporaryFile rvTmpFile, cvTmpFile; rv.dump(rvTmpFile.path()); cv.dump(cvTmpFile.path()); RowVector rv2(rvTmpFile.path()); ColVector cv2(cvTmpFile.path()); cout << "---" << endl << "rv = " << rv << endl << "cv = " << cv << endl << "rv from file: " << rv2 << endl << "cv from file: " << cv2 << endl; for (unsigned int i = 0; i < 6; ++i) if (rv(i) != rv2(i) || cv(i) != cv2(i)) return false; } return true; }
// Hotrod 2.8 with mediatype allows to store and retrieve entries in different data format // this can be done specifying the mediatype for the communication int main(int argc, char** argv) { ConfigurationBuilder builder; builder.protocolVersion(Configuration::PROTOCOL_VERSION_28); builder.addServer().host("127.0.0.1").port(11222); builder.balancingStrategyProducer(nullptr); RemoteCacheManager cacheManager(builder.build(), false); cacheManager.start(); std::cout << "Tests for CacheManager" << std::endl; Marshaller<std::string> *km = new JBasicMarshaller<std::string>(); Marshaller<std::string> *vm = new JBasicMarshaller<std::string>(); RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km, &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("transcodingCache")); // Define a data format for json DataFormat<std::string, std::string> df; df.keyMediaType = MediaType(APPLICATION_UNKNOWN_TYPE); df.valueMediaType = MediaType(APPLICATION_JSON_TYPE); df.valueMarshaller.reset(new BasicMarshaller<std::string>()); RemoteCache<std::string, std::string> cacheJson = cache.withDataFormat(&df); // Define a data forma for jboss marshaller DataFormat<std::string, std::string> df1; df1.keyMediaType = MediaType(APPLICATION_JBOSS_MARSHALLING_TYPE); df1.valueMediaType = MediaType(APPLICATION_JBOSS_MARSHALLING_TYPE); RemoteCache<std::string, std::string> cacheJBoss = cache.withDataFormat(&df1); std::string k1("key13"); std::string k2("key14"); std::string v1("boron"); std::string v2("chlorine"); cache.clear(); // put cache.put(k1, v1); std::unique_ptr<std::string> rv(cache.get(k1)); assert_not_null("get returned null!", __LINE__, rv); if (rv->compare(v1)) { std::cerr << "get/put fail for " << k1 << " got " << *rv << " expected " << v1 << std::endl; return 1; } cache.put(k2, v2); std::unique_ptr<std::string> rv2(cache.get(k2)); assert_not_null("get returned null!", __LINE__, rv2); if (rv2->compare(v2)) { std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected " << v2 << std::endl; return 1; } std::unique_ptr<std::string> rv2Json(cacheJson.get(k2)); assert_not_null("get returned null!", __LINE__, rv2Json); if (rv2Json->compare("\"chlorine\"")) { std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected \"boron\"" << std::endl; return 1; } std::unique_ptr<std::string> rv2JBoss(cacheJBoss.get(k2)); assert_not_null("get returned null!", __LINE__, rv2JBoss); if (rv2JBoss->compare(v2)) { std::cerr << "get/put fail for " << k2 << " got " << *rv2JBoss << " expected " << v2 << std::endl; return 1; } }