Skip to content

Commit 6b76bae

Browse files
committed
Unit tests: Clean up DataType tests
1 parent 6df5a15 commit 6b76bae

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

testing/DataType.cpp

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,39 @@
11
#include "catch.hpp"
22
#include "helpers.h"
3+
#include "helper_type.hpp"
34
#include <cstdint>
45
#include <lsl_cpp.h>
56

6-
7-
namespace {
8-
template <typename T> void test_DataType(const char *name, lsl::channel_format_t cf) {
9-
const int numBounces = sizeof(T) * 8;
7+
TEMPLATE_TEST_CASE("datatransfer", "[datatransfer][basic]", char, int16_t, int32_t, int64_t, float, double) {
8+
const int numBounces = sizeof(TestType) * 8;
109
double timestamps[numBounces][2];
10+
const char* name = SampleType<TestType>::fmt_string();
11+
lsl::channel_format_t cf = (lsl::channel_format_t) SampleType<TestType>::chan_fmt;
1112

1213
Streampair sp(create_streampair(
13-
lsl::stream_info(name, "Bounce", 1, lsl::IRREGULAR_RATE, cf, "streamid")));
14+
lsl::stream_info(name, "Bounce", 2, lsl::IRREGULAR_RATE, cf, "streamid")));
1415
lsl::stream_inlet &inlet = sp.in_;
1516
lsl::stream_outlet &outlet = sp.out_;
1617

1718
inlet.open_stream(2);
1819
outlet.wait_for_consumers(2);
1920

20-
T sent_data = 0x1;
21+
TestType sent_data[2] = {0x1, 0x1};
2122
for (int32_t counter = 0; counter < numBounces; counter++) {
22-
T received_data;
23+
TestType received_data[2];
2324
timestamps[counter][0] = lsl::local_clock();
24-
outlet.push_sample(&sent_data, timestamps[counter][0], true);
25+
sent_data[1] = -sent_data[0] + 1;
26+
outlet.push_sample(sent_data, timestamps[counter][0], true);
2527

26-
CHECK(inlet.pull_sample(&received_data, 1, .5) != 0.0);
28+
CHECK(inlet.pull_sample(received_data, 2, .5) != 0.0);
2729

2830
timestamps[counter][1] = lsl::local_clock();
29-
CHECK(received_data == sent_data);
30-
sent_data = static_cast<T>(sent_data << 1);
31-
}
32-
}
33-
34-
TEST_CASE("data type int8", "[datatransfer][basic]") { test_DataType<char>("cf_int8", lsl::cf_int8); }
35-
TEST_CASE("data type int16", "[datatransfer][basic]") { test_DataType<int16_t>("cf_int16", lsl::cf_int16); }
36-
TEST_CASE("data type int32", "[datatransfer][basic]") { test_DataType<int32_t>("cf_int32", lsl::cf_int32); }
37-
TEST_CASE("data type int64", "[datatransfer][basic]") { test_DataType<int64_t>("cf_int64", lsl::cf_int64); }
38-
39-
template <typename T> void test_DataTypeMulti(const char *name, lsl::channel_format_t cf) {
40-
const int numChannels = sizeof(T) * 8;
41-
42-
Streampair sp(create_streampair(
43-
lsl::stream_info(name, "Bounce", numChannels, lsl::IRREGULAR_RATE, cf, "streamid")));
44-
lsl::stream_inlet &inlet = sp.in_;
45-
lsl::stream_outlet &outlet = sp.out_;
46-
47-
std::vector<T> sent_data(numChannels);
48-
T data = 0x1LL;
49-
for (uint32_t counter = 0; counter < numChannels; counter++) {
50-
sent_data[counter] = data;
51-
data = static_cast<T>(data << 1LL);
31+
CHECK(received_data[0] == sent_data[0]);
32+
CHECK(received_data[1] == sent_data[1]);
33+
sent_data[0] = static_cast<TestType>(static_cast<int64_t>(sent_data[0]) << 1);
5234
}
53-
54-
std::vector<T> received_data(numChannels);
55-
outlet.push_sample(sent_data, lsl::local_clock(), true);
56-
CHECK(inlet.pull_sample(received_data, 0.5) != 0.0);
57-
CHECK(received_data == sent_data);
5835
}
5936

60-
TEST_CASE("data type int8 multi", "[datatransfer][multi]") { test_DataTypeMulti<char>("cf_int8", lsl::cf_int8); }
61-
TEST_CASE("data type int16 multi", "[datatransfer][multi]") { test_DataTypeMulti<int16_t>("cf_int16", lsl::cf_int16); }
62-
TEST_CASE("data type int32 multi", "[datatransfer][multi]") { test_DataTypeMulti<int32_t>("cf_int32", lsl::cf_int32); }
63-
TEST_CASE("data type int64 multi", "[datatransfer][multi]") { test_DataTypeMulti<int64_t>("cf_int64", lsl::cf_int64); }
64-
6537
TEST_CASE("data datatransfer", "[datatransfer][multi][string]") {
6638
const std::size_t numChannels = 2;
6739

@@ -104,4 +76,3 @@ TEST_CASE("data typeconversion", "[datatransfer][types][basic]") {
10476
}
10577
}
10678

107-
} // namespace

testing/helper_type.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include "../include/lsl/common.h"
3+
#include <string>
4+
5+
template <typename T> struct SampleType {
6+
static const lsl_channel_format_t chan_fmt = cft_undefined;
7+
static const char *fmt_string();
8+
};
9+
10+
template <> struct SampleType<char> {
11+
static const lsl_channel_format_t chan_fmt = cft_int8;
12+
static const char *fmt_string() { return "cf_int8"; }
13+
};
14+
15+
template <> struct SampleType<int16_t> {
16+
static const lsl_channel_format_t chan_fmt = cft_int16;
17+
static const char *fmt_string() { return "cf_int16"; }
18+
};
19+
20+
template <> struct SampleType<int32_t> {
21+
static const lsl_channel_format_t chan_fmt = cft_int32;
22+
static const char *fmt_string() { return "cf_int32"; }
23+
};
24+
25+
template <> struct SampleType<int64_t> {
26+
static const lsl_channel_format_t chan_fmt = cft_int64;
27+
static const char *fmt_string() { return "cf_int64"; }
28+
};
29+
30+
template <> struct SampleType<float> {
31+
static const lsl_channel_format_t chan_fmt = cft_float32;
32+
static const char *fmt_string() { return "cf_float32"; }
33+
};
34+
35+
template <> struct SampleType<double> {
36+
static const lsl_channel_format_t chan_fmt = cft_double64;
37+
static const char *fmt_string() { return "cf_double64"; }
38+
};
39+
40+
template <> struct SampleType<std::string> {
41+
static const lsl_channel_format_t chan_fmt = cft_string;
42+
static const char *fmt_string() { return "cf_string"; }
43+
};

0 commit comments

Comments
 (0)