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.

228 lines
6.9 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 *
***************************************************************************/
#define SEISCOMP_TEST_MODULE gempa
#include "test_utils.h"
#include <seiscomp3/unittest/unittests.h>
#include <gempa/caps/mseedpacket.h>
#include <gempa/caps/rawpacket.cpp>
#include <gempa/caps/utils.h>
#include <gempa/caps/datetime.h>
#include <fstream>
#include <iostream>
#include <math.h>
#include <string>
#include <streambuf>
namespace bu = boost::unit_test;
using namespace std;
using namespace Gempa::CAPS;
namespace {
struct Record {
Record() {
vector<char> mseed;
mseed.resize(512);
ifstream ifs("data/AM.RFE4F.00.SHZ.20180912.mseed");
if( ifs.is_open() ) {
ifs.read(mseed.data(), 512);
testRec.setData(mseed.data(), 512);
}
else
BOOST_TEST_MESSAGE("unable to open test data file.");
}
MSEEDDataRecord testRec;
};
}
BOOST_AUTO_TEST_SUITE(gempa_common_caps_mseedpacket)
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(equal_functions) {
bu::unit_test_log.set_threshold_level(bu::log_warnings);
bu::unit_test_log.set_threshold_level(bu::log_messages);
MSEEDDataRecord::Header header;
MSEEDDataRecord::Header otherHeader;
header.setSamplingTime(fromString("2018-01-01 00:00:01"));
header.samplingFrequencyNumerator = 30;
header.samplingFrequencyDenominator = 1;
BOOST_CHECK_EQUAL(header != otherHeader, true);
bool valid = header.compatible(otherHeader);
BOOST_CHECK_EQUAL(valid, false);
otherHeader = header;
valid = header.compatible(otherHeader);
BOOST_CHECK_EQUAL(valid, true);
BOOST_CHECK_EQUAL(header != otherHeader, false);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(header_functions) {
MSEEDDataRecord::Header header;
header.dataType = DT_INT64;
header.samplingFrequencyNumerator = 1;
header.samplingFrequencyDenominator = 1;
char buf[1024];
arraybuf abuf(buf, 1024);
BOOST_CHECK_EQUAL(header.put(abuf), true);
MSEEDDataRecord::Header otherHeader;
BOOST_CHECK_EQUAL(otherHeader.get(abuf), true);
BOOST_CHECK_EQUAL(otherHeader.samplingFrequencyNumerator, 1);
BOOST_CHECK_EQUAL(otherHeader.samplingFrequencyDenominator, 1);
BOOST_CHECK_EQUAL(otherHeader.dataType, DT_INT64);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(trim_function) {
// MSEEDs records can not be trimmed -> always false
MSEEDDataRecord testRec;
BOOST_CHECK(!testRec.canTrim());
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(merge_function) {
// MSEEDs records can not be merged -> always false
MSEEDDataRecord testRec;
BOOST_CHECK(!testRec.canMerge());
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_SUITE_END()
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(getter_and_setter, Record) {
BOOST_CHECK_EQUAL("MSEED", testRec.formatName());
// get data size with and without header
size_t sizeWithout = testRec.dataSize(false);
BOOST_CHECK_EQUAL(sizeWithout, 512);
size_t sizeWith = testRec.dataSize(true);
BOOST_CHECK_EQUAL(512, sizeWith);
// check start end end time
BOOST_CHECK_EQUAL(testRec.startTime().iso(), "2018-09-12T10:55:56.751Z");
BOOST_CHECK_EQUAL(testRec.endTime().iso(), "2018-09-12T10:56:03.511Z");
// check sampling frequency
BOOST_CHECK_EQUAL(testRec.header()->samplingFrequencyNumerator, 50);
BOOST_CHECK_EQUAL(testRec.header()->samplingFrequencyDenominator, 1);
// check packet type
BOOST_CHECK_EQUAL(testRec.packetType(), MSEEDPacket);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(read_header_from_buffer, Record) {
char data[512];
// write test record data into a buffer
arraybuf abuf(data, 512);
BOOST_CHECK(testRec.put(abuf, true));
Time startTime, endTime;
MSEEDDataRecord::Header header;
// read start, end time and further information from buffer
MSEEDDataRecord rec;
rec.readMetaData(abuf, 512, header, startTime, endTime);
BOOST_CHECK_EQUAL(startTime.iso(), "2018-09-12T10:55:56.751Z");
BOOST_CHECK_EQUAL(endTime.iso(), "2018-09-12T10:56:03.511Z");
BOOST_CHECK_EQUAL(header.dataType, DT_INT32);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_FIXTURE_TEST_CASE(read_data_from_file, Record) {
ifstream ifs("data/AM.RFE4F.00.SHZ.20180912.mseed");
BOOST_CHECK(ifs.is_open());
vector<char> buf;
buf.resize(512);
ifs.read(buf.data(), 512);
MSEEDDataRecord rec;
rec.setData(buf.data(),512);
BOOST_CHECK_EQUAL(rec.header()->samplingFrequencyDenominator, 1);
BOOST_CHECK_EQUAL(rec.header()->samplingFrequencyNumerator, 50);
TimeSpan timeSpan = samplesToTimeSpan(*rec.header(), 338);
// check that the record start time + all samples is equal to
// the record end time
Time endTime = rec.startTime() + timeSpan;
BOOST_CHECK(endTime == rec.endTime());
// check both header are equal
BOOST_CHECK_EQUAL(rec.header()->compatible(*testRec.header()), true);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<