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.

290 lines
8.1 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 <seiscomp3/unittest/unittests.h>
#include <gempa/caps/utils.h>
namespace gc = Gempa::CAPS;
namespace bu = boost::unit_test;
using namespace gc;
using namespace std;
BOOST_AUTO_TEST_SUITE(gempa_common_caps_utils)
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(split_address_function) {
bu::unit_test_log.set_threshold_level(bu::log_warnings);
bu::unit_test_log.set_threshold_level(bu::log_messages);
string name;
unsigned short port;
int defaultPort = 18002;
string hostname = "localhost";
bool valid = splitAddress(name, port, hostname,defaultPort);
BOOST_CHECK_EQUAL(valid, true);
BOOST_CHECK_EQUAL(hostname, "localhost");
BOOST_CHECK_EQUAL(port, 18002);
hostname = "data.gempa.de:18000";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, true);
BOOST_CHECK_EQUAL(name, "data.gempa.de");
BOOST_CHECK_EQUAL(port, 18000);
hostname = "";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, false);
hostname = "localhost:abcde";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, false);
// port range check. By definition allowed ports are 1 - 65535
hostname = "localhost:0";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, false);
hostname = "localhost:1";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, true);
hostname = "localhost:65536";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, false);
hostname = "localhost:-1";
valid = splitAddress(name, port, hostname, defaultPort);
BOOST_CHECK_EQUAL(valid, false);
hostname = "localhost:";
valid = splitAddress(name, port, hostname,defaultPort);
BOOST_CHECK_EQUAL(valid, false);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BOOST_AUTO_TEST_CASE(trim_function) {
// delete whitespaces before and after string
string text = " This is a test text 1";
BOOST_CHECK_EQUAL(trim(text), "This is a test text 1");
const char *str = text.c_str();
int len = text.length();
const char *res = trim(str,len);
BOOST_CHECK_EQUAL(string(res, len), "This is a test text 1");
text = " This is a test text 2";
BOOST_CHECK_EQUAL(trim(text), "This is a test text 2");
str = text.c_str();
len = text.length();
res = trim(str,len);
BOOST_CHECK_EQUAL(string(res,len), "This is a test text 2");
text = "This is a test text ";
BOOST_CHECK_EQUAL(trim(text), "This is a test text");
str = text.c_str();
len = text.length();
res = trim(str,len);
BOOST_CHECK_EQUAL(string(res,len), "This is a test text");
text = " Hello World ";
BOOST_CHECK_EQUAL(trim(text), "Hello World");
str = text.c_str();
len = text.length();
res = trim(str,len);
BOOST_CHECK_EQUAL(string(res,len), "Hello World");
text = " Hello : world";
BOOST_CHECK_EQUAL(trim(text), "Hello : world");
str = text.c_str();
len = text.length();
res = trim(str, len);
BOOST_CHECK_EQUAL(string(res,len), "Hello : world");
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BOOST_AUTO_TEST_CASE(converts_time) {
// Converts time object to timestamp
const Time t(2014,4,14,6,34,39,7389);
TimeStamp ts;
timeToTimestamp(ts, t);
BOOST_CHECK_EQUAL(ts.year, 2014);
BOOST_CHECK_EQUAL(ts.second, 39);
const Time t2(0,0);
timeToTimestamp(ts,t2);
BOOST_CHECK_EQUAL(ts.year, 1970);
BOOST_CHECK_EQUAL(ts.hour, 0);
BOOST_CHECK_EQUAL(ts.yday, 0);
// Converts timestamp to time object
Time t3 = timestampToTime(ts);
int year, yday, hour, min, sec, usec;
t3.get2(&year, &yday, &hour, &min, &sec, &usec);
BOOST_CHECK_EQUAL(year, 1970);
BOOST_CHECK_EQUAL(min, 0);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BOOST_AUTO_TEST_CASE(samples) {
DataRecord::Header head;
head.samplingFrequencyNumerator = 1;
head.samplingFrequencyDenominator = 1;
// Convert samples to timespan
TimeSpan ts = samplesToTimeSpan(head, 2);
BOOST_CHECK_EQUAL(ts, TimeSpan(2,0));
// Convert time span to samples
BOOST_CHECK_EQUAL(timeSpanToSamples(head, ts), 2);
// Convert time span to samples and round up/down
BOOST_CHECK_EQUAL(timeSpanToSamplesCeil(head, TimeSpan(0, 500000)), 1);
BOOST_CHECK_EQUAL(timeSpanToSamplesFloor(head, TimeSpan(0, 600000)), 0);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BOOST_AUTO_TEST_CASE(str_to_int) {
// int tests
const char *str = "1352";
int i;
bool ret = str2int(i, str);
BOOST_CHECK_EQUAL(ret, true);
BOOST_CHECK_EQUAL(i, 1352);
str = "";
ret = str2int(i, str);
BOOST_CHECK_EQUAL(ret, false);
str = "478a2";
ret = str2int(i, str);
BOOST_CHECK_EQUAL(ret, false);
str = " 57721";
ret = str2int(i, str);
BOOST_CHECK_EQUAL(ret, true);
BOOST_CHECK_EQUAL(i, 57721);
str = "4876 9742";
ret = str2int(i, str);
BOOST_CHECK_EQUAL(ret, true);
BOOST_CHECK_EQUAL(i, 4876);
str = "-98256";
ret = str2int(i,str);
BOOST_CHECK_EQUAL(ret, true);
BOOST_CHECK_EQUAL(i, -98256);
str = "-2147483649";
ret = str2int(i,str);
BOOST_CHECK_EQUAL(ret, false);
str = "2147483648";
ret = str2int(i,str);
BOOST_CHECK_EQUAL(ret, false);
// uint16_t tests
str = "1";
uint16_t value;
ret = str2int(value, str);
BOOST_CHECK_EQUAL(ret, true);
str = "-1";
ret = str2int(value, str);
BOOST_CHECK_EQUAL(ret, false);
str = "65535";
ret = str2int(value, str);
BOOST_CHECK_EQUAL(ret, true);
str = "65536";
ret = str2int(value, str);
BOOST_CHECK_EQUAL(ret, false);
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BOOST_AUTO_TEST_CASE(tokenize_function) {
const char *text = "Hello, World!";
int len = strlen(text), tok_len;
const char *tok = tokenize(text, ",", len, tok_len);
BOOST_CHECK_EQUAL(string(tok, tok_len), "Hello");
tok = tokenize(text, ",", len, tok_len);
BOOST_CHECK_EQUAL(string(tok,tok_len)," World!");
text = "This is a book";
len = strlen(text);
int tok_count = 0;
while ( (tok = tokenize(text, ",", len, tok_len)) != NULL ) {
if ( tok_count == 3 ) {
BOOST_CHECK_EQUAL(string(tok, tok_len), "book");
}
++tok_count;
}
text = "";
len = strlen(text);
tok = tokenize(text, ";", len,tok_len);
BOOST_CHECK_EQUAL(string(tok,tok_len), "");
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_SUITE_END()