You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
9.5 KiB
C++

/***************************************************************************
* Copyright (C) 2018 by gempa GmbH *
* *
* All Rights Reserved. *
* *
* NOTICE: All information contained herein is, and remains *
* the property of gempa GmbH and its suppliers, if any. The intellectual *
* and technical concepts contained herein are proprietary to gempa GmbH *
* and its suppliers. *
* Dissemination of this information or reproduction of this material *
* is strictly forbidden unless prior written permission is obtained *
* from gempa GmbH. *
* *
* Author: Tracey Werner, Enrico Ellguth *
* Email: tracey.werner@gempa.de, enrico.ellguth@gempa.de *
***************************************************************************/
#ifndef M_PI
#define M_PI 3.14159265359
#endif
#define SEISCOMP_TEST_MODULE gempa
#include "test_utils.h"
#include <seiscomp3/unittest/unittests.h>
#include <gempa/caps/rawpacket.cpp>
#include <gempa/caps/mseedpacket.cpp>
#include <gempa/caps/datetime.h>
#include <math.h>
#include <string>
#include <streambuf>
namespace gc = Gempa::CAPS;
namespace bu = boost::unit_test;
using namespace std;
namespace {
struct Record {
Record() {
gc::DataRecord::Header header;
header.setSamplingTime(fromString("2018-01-01 00:00:01"));
header.dataType = gc::DT_INT32;
header.samplingFrequencyNumerator = 1;
header.samplingFrequencyDenominator = 1;
rdr.setHeader(header);
}
gc::RawDataRecord rdr;
};
}
template<typename T> void fillRecord(T *data, size_t len , gc::Time stime, int sample_microsecs,
double amplitude, double period) {
double periodScale = (2 * M_PI) / period;
double x = (double)stime * periodScale;
double xOffset = sample_microsecs * 1E-6 * periodScale;
for ( size_t i = 0; i < len; ++i ) {
*data = amplitude * sin(x);
++data;
x += xOffset;
}
}
BOOST_AUTO_TEST_SUITE(gempa_common_caps_rawpacket)
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(getAndSet, Record) {
bu::unit_test_log.set_threshold_level(bu::log_warnings);
bu::unit_test_log.set_threshold_level(bu::log_messages);
vector<int> data;
data.resize(256);
rdr.setBuffer(data.data(), 256);
// Set data type and get size of it
rdr.setDataType(gc::DT_FLOAT);
BOOST_CHECK_EQUAL(sizeof(float), gc::sizeOf(rdr.header()->dataType));
BOOST_CHECK_EQUAL("RAW/FLOAT", rdr.formatName());
rdr.setDataType(gc::DT_INT64);
BOOST_CHECK_EQUAL(sizeof(int64_t), gc::sizeOf(rdr.header()->dataType));
BOOST_CHECK_EQUAL("RAW/INT64", rdr.formatName());
// get data size with and without header
size_t sizeWithout = rdr.dataSize(false);
BOOST_CHECK_EQUAL(sizeWithout, 256);
size_t sizeWith = rdr.dataSize(true);
BOOST_CHECK_EQUAL(sizeWithout + rdr.header()->dataSize(), sizeWith);
data.resize(65536);
rdr.setBuffer(data.data(), 65536);
sizeWithout = rdr.dataSize(false);
BOOST_CHECK_EQUAL(sizeWithout, 65536);
sizeWith = rdr.dataSize(true);
BOOST_CHECK_EQUAL(sizeWithout + rdr.header()->dataSize(), sizeWith);
// Set new FrequencyNumerator and FrequencyDenominator
rdr.setSamplingFrequency(100,1);
BOOST_CHECK_EQUAL(rdr.header()->samplingFrequencyNumerator, 100);
BOOST_CHECK_EQUAL(rdr.header()->samplingFrequencyDenominator, 1);
rdr.setSamplingFrequency(20,1);
BOOST_CHECK_EQUAL(rdr.header()->samplingFrequencyNumerator, 20);
BOOST_CHECK_EQUAL(rdr.header()->samplingFrequencyDenominator, 1);
gc::Time t = gc::getEndTime(rdr.startTime(),20, *rdr.header());
BOOST_CHECK_EQUAL(t.iso(), "2018-01-01T00:00:02.0000Z");
gc::DataRecord::Header otherHeader;
BOOST_CHECK_NO_THROW(rdr.setHeader(otherHeader));
BOOST_CHECK_EQUAL("RAW/UNKWN", rdr.formatName());
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(checkTime, Record) {
gc::Time t = gc::getEndTime(rdr.startTime(), 3, *rdr.header());
BOOST_CHECK_EQUAL(t.iso(), "2018-01-01T00:00:04.0000Z");
rdr.setStartTime(fromString("2018-02-01 00:00:03.0000"));
vector<int> data;
data.resize(1024);
rdr.setBuffer(data.data(), 1024);
t = gc::getEndTime(rdr.startTime(), 3, *rdr.header());
BOOST_CHECK_EQUAL(rdr.startTime().iso(), "2018-02-01T00:00:03.0000Z");
BOOST_CHECK_EQUAL(t.iso(), "2018-02-01T00:00:06.0000Z");
rdr.setStartTime(fromString("1988-03-14 14:22:57.322"));
t = gc::getEndTime(rdr.startTime(), 2, *rdr.header());
BOOST_CHECK_EQUAL(t.iso(), "1988-03-14T14:22:59.0000Z");
rdr.setStartTime(fromString("1970-01-01 00:00:00.0000"));
rdr.setSamplingFrequency(20,1);
BOOST_CHECK_EQUAL(rdr.endTime().iso(), "1970-01-01T00:00:12.8Z");
rdr.setDataType(gc::DT_FLOAT);
BOOST_CHECK_EQUAL(rdr.endTime().iso(), "1970-01-01T00:00:12.8Z");
rdr.setDataType(gc::DT_DOUBLE);
data.resize(2048);
rdr.setBuffer(data.data(), 2048);
BOOST_CHECK_EQUAL(rdr.endTime().iso(), "1970-01-01T00:00:12.8Z");
rdr.setDataType(gc::DT_INT64);
BOOST_CHECK_EQUAL(rdr.endTime().iso(), "1970-01-01T00:00:12.8Z");
rdr.setDataType(gc::DT_INT8);
data.resize(256);
rdr.setBuffer(data.data(), 256);
BOOST_CHECK_EQUAL(rdr.endTime().iso(), "1970-01-01T00:00:12.8Z");
rdr.setDataType(gc::DT_INT16);
data.resize(512);
rdr.setBuffer(data.data(),512);
BOOST_CHECK_EQUAL(rdr.endTime().iso(), "1970-01-01T00:00:12.8Z");
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(trimTest, Record) {
// Raw records are trimable -> always true
BOOST_CHECK(rdr.canTrim());
const gc::Time endT = gc::getEndTime(rdr.startTime(), 1, *rdr.header());
BOOST_CHECK_EQUAL(rdr.canTrim(), true);
bool trimTest = rdr.trim(rdr.startTime(), endT);
BOOST_CHECK_EQUAL(trimTest, true);
BOOST_CHECK_EQUAL(rdr.startTime().iso(), "2018-01-01T00:00:01.0000Z");
BOOST_CHECK_EQUAL(endT.iso(), "2018-01-01T00:00:02.0000Z");
trimTest = rdr.trim(endT, rdr.startTime());
BOOST_CHECK_EQUAL(trimTest, false);
rdr.setSamplingFrequency(0,0);
trimTest = rdr.trim(rdr.startTime(), endT);
BOOST_CHECK_EQUAL(trimTest, false);
BOOST_CHECK_EQUAL(rdr.canMerge(), true);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(getAndPut, Record) {
char data[500000];
uint64_t sample_microsecs = uint64_t(rdr.header()->samplingFrequencyDenominator) * 1000000 / rdr.header()->samplingFrequencyNumerator;
fillRecord<float>((float*)&data, 1024, rdr.startTime(), (int)sample_microsecs, 4.2, 2.1);
gc::arraybuf abuf(data, 1024);
vector<int> arr;
arr.resize(1024);
rdr.setBuffer(arr.data(), 1024);
bool check = rdr.put(abuf, true);
BOOST_CHECK_EQUAL(check, false);
// RS_PARTIAL = 2
gc::RawDataRecord rdrOther;
gc::DataRecord::ReadStatusCode ret = rdrOther.get(abuf, 1024, rdr.startTime(),rdr.endTime(), 10);
BOOST_CHECK_EQUAL(ret, gc::DataRecord::RS_Partial);
BOOST_CHECK_EQUAL(rdrOther.startTime() == rdr.startTime(), true);
// RS_COMPLETE = 1
gc::arraybuf abuf2(data, 1024);
check = rdrOther.put(abuf2, true);
BOOST_CHECK_EQUAL(check, true);
gc::RawDataRecord rdrOther2;
ret = rdrOther2.get(abuf2, 1024, rdrOther2.startTime(),rdrOther2.endTime(), 10);
BOOST_CHECK_EQUAL(ret, gc::DataRecord::RS_Complete);
gc::DataRecord::Header header = *rdrOther2.header();
gc::Time start = rdrOther2.startTime();
gc::Time end = gc::getEndTime(rdrOther2.startTime(), 2, header);
rdrOther2.readMetaData(abuf, 1024, header, start, end);
BOOST_CHECK_EQUAL(rdrOther2.startTime().iso(), "2018-01-01T00:00:01.0000Z");
BOOST_CHECK_EQUAL(rdrOther2.endTime().iso(), "2018-01-01T00:04:13.0000Z");
// RS_BEFORE_TIME_WINDOW = 3
gc::arraybuf abuf3(data, 1024);
gc::RawDataRecord rdrBefore;
header.setSamplingTime(fromString("2018-01-01 00:00:00"));
header.dataType = gc::DT_INT32;
header.samplingFrequencyNumerator = 1;
header.samplingFrequencyDenominator = 1;
rdrBefore.setHeader(header);
check = rdrBefore.put(abuf3, true);
BOOST_CHECK_EQUAL(check, true);
ret = rdrOther.get(abuf3, 1024, rdr.endTime(),rdr.startTime(), 10);
BOOST_CHECK_EQUAL(ret, gc::DataRecord::RS_BeforeTimeWindow);
// RS_AFTER_TIME_WINDOW = 4
gc::arraybuf abuf4(data, 1024);
gc::RawDataRecord rdrAfter;
header.setSamplingTime(fromString("2020-01-01 00:00:00"));
header.dataType = gc::DT_INT32;
header.samplingFrequencyNumerator = 1;
header.samplingFrequencyDenominator = 1;
rdrAfter.setHeader(header);
check = rdrAfter.put(abuf4, true);
BOOST_CHECK_EQUAL(check, true);
ret = rdrOther.get(abuf4, 1024, rdrBefore.startTime(),rdrBefore.endTime(), 10);
BOOST_CHECK_EQUAL(ret, gc::DataRecord::RS_AfterTimeWindow);
// RS_ERROR = 0
gc::RawDataRecord rdrError;
ret = rdrError.get(abuf, 1248, rdr.startTime(),rdr.endTime(), 10);
BOOST_CHECK_EQUAL(ret, gc::DataRecord::RS_Error);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_SUITE_END()