void Sz4RPNParam::test2() { rpn_unit_test::IPKContainerMock2 mock; std::wstringstream base_dir_name; base_dir_name << L"/tmp/sz4_rpn_param" << getpid() << L"." << time(NULL) << L".tmp"; boost::filesystem::wpath base_path(base_dir_name.str()); boost::filesystem::wpath param_dir(base_path / L"BASE/szbase/A/B/C"); boost::filesystem::create_directories(param_dir); #if BOOST_FILESYSTEM_VERSION == 3 sz4::base_templ<rpn_unit_test::test_types> base(base_path.wstring(), &mock); #else sz4::base_templ<rpn_unit_test::test_types> base(base_path.file_string(), &mock); #endif auto buff = base.buffer_for_param(mock.GetParam(L"BASE:A:B:D")); TestObserver o; #if BOOST_FILESYSTEM_VERSION == 3 base.param_monitor().add_observer(&o, mock.GetParam(L"BASE:A:B:C"), param_dir.native(), 10); #else base.param_monitor().add_observer(&o, mock.GetParam(L"BASE:A:B:C"), param_dir.external_file_string(), 10); #endif { sz4::weighted_sum<double, sz4::second_time_t> sum; buff->get_weighted_sum(mock.GetParam(L"BASE:A:B:D"), 100u, 200u, PT_SEC10, sum); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(0), sum.weight()); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(100), sum.no_data_weight()); CPPUNIT_ASSERT_EQUAL(false, sum.fixed()); std::wstringstream file_name; file_name << std::setfill(L'0') << std::setw(10) << 100 << L".sz4"; std::string file_path_str; #if BOOST_FILESYSTEM_VERSION == 3 file_path_str = (param_dir / file_name.str()).native(); #else file_path_str = (param_dir / file_name.str()).external_file_string(); #endif int fd; CPPUNIT_ASSERT_NO_THROW(fd = sz4::open_writelock(file_path_str.c_str(), O_WRONLY | O_CREAT)); char buf[3]; short v = 10; unsigned char t = 0x32; memcpy(buf, &v, 2); memcpy(buf + 2, &t, 1); write(fd, buf, sizeof(buf)); sz4::close_unlock(fd); } CPPUNIT_ASSERT(o.wait_for(1, 2)); { sz4::weighted_sum<double, sz4::second_time_t> sum; sz4::weighted_sum<double, sz4::second_time_t>::time_diff_type weight; buff->get_weighted_sum(mock.GetParam(L"BASE:A:B:D"), 100u, 200u, PT_SEC10, sum); CPPUNIT_ASSERT_EQUAL(550., double(sum.sum(weight))); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(50), weight); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(50), sum.no_data_weight()); CPPUNIT_ASSERT_EQUAL(false, sum.fixed()); } }
void Sz4BufferTestCase::test2() { std::wstringstream base_dir_name; base_dir_name << L"/tmp/szb_bufer_unit_test_2" << getpid() << L"." << time(NULL) << L".tmp"; std::wstringstream param_dir_name; param_dir_name << base_dir_name.str() << L"/A/A/A"; boost::filesystem::create_directories(param_dir_name.str()); for (int i = 1000; i < 2000; i += 100) { std::wstringstream ss; ss << param_dir_name.str() << L"/" << std::setfill(L'0') << std::setw(10) << i << L".sz4"; std::ofstream ofs(SC::S2A(ss.str()).c_str(), std::ios_base::binary); unsigned data = 10; ofs.write((const char*) &data, sizeof(data)); unsigned char delta = 50; ofs.write((const char*) &delta, sizeof(delta)); } TParam param(NULL); param.SetName(L"A:A:A"); param.SetTimeType(TParam::SECOND); param.SetDataType(TParam::INT); SzbParamMonitor monitor(L""); mocks::IPKContainerMock container_mock; container_mock.add_param(¶m); sz4::buffer_templ<sz4::base_templ<mocks::mock_types>> buffer(&m_base, &monitor, &container_mock, L"TEST", base_dir_name.str()); sz4::weighted_sum<int, sz4::second_time_t> sum; sz4::weighted_sum<int, sz4::second_time_t>::time_diff_type weight; buffer.get_weighted_sum(¶m, sz4::second_time_t(1900), sz4::second_time_t(2000), PT_SEC10, sum); CPPUNIT_ASSERT_EQUAL(sz4::value_sum<int>::type(500), sum.sum(weight)); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(50), weight); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(50), sum.no_data_weight()); CPPUNIT_ASSERT_EQUAL(false, sum.fixed()); TestObserver o; monitor.add_observer(&o, ¶m, SC::S2A(param_dir_name.str()), 1); { std::wstringstream ss; ss << param_dir_name.str() << L"/" << std::setfill(L'0') << std::setw(10) << 1900 << L".sz4"; std::ofstream ofs(SC::S2A(ss.str()).c_str(), std::ios_base::binary | std::ios_base::app); unsigned data = 20; ofs.write((const char*) &data, sizeof(data)); unsigned char delta = 50; ofs.write((const char*) &delta, sizeof(delta)); } o.wait_for(1); sum = sz4::weighted_sum<int, sz4::second_time_t>(); buffer.get_weighted_sum(¶m, sz4::second_time_t(1900), sz4::second_time_t(2000), PT_SEC10, sum); CPPUNIT_ASSERT_EQUAL(sz4::value_sum<int>::type(1500), sum.sum(weight)); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(100), weight); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(0), sum.no_data_weight()); CPPUNIT_ASSERT_EQUAL(true, sum.fixed()); { std::wstringstream ss; ss << param_dir_name.str() << L"/" << std::setfill(L'0') << std::setw(10) << 1900 << L".sz4"; std::ofstream ofs(SC::S2A(ss.str()).c_str(), std::ios_base::binary | std::ios_base::app); unsigned data = 30; ofs.write((const char*) &data, sizeof(data)); //3000; unsigned char delta[] = { 0x83u, 0xe8u }; ofs.write((const char*) delta, sizeof(delta)); } o.wait_for(2); sum = sz4::weighted_sum<int, sz4::second_time_t>(); buffer.get_weighted_sum(¶m, sz4::second_time_t(1900), sz4::second_time_t(3000), PT_SEC10, sum); CPPUNIT_ASSERT_EQUAL(sz4::value_sum<int>::type(31500), sum.sum(weight)); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(1100), weight); CPPUNIT_ASSERT_EQUAL(sz4::time_difference<sz4::second_time_t>::type(0), sum.no_data_weight()); CPPUNIT_ASSERT_EQUAL(true, sum.fixed()); sum = sz4::weighted_sum<int, sz4::second_time_t>(); buffer.get_weighted_sum(¶m, sz4::second_time_t(1900), sz4::second_time_t(4000), PT_SEC10, sum); CPPUNIT_ASSERT_EQUAL(false, sum.fixed()); boost::filesystem::remove_all(boost::filesystem::wpath(base_dir_name.str())); }