File: samples.cpp

package info (click to toggle)
liblsl 1.16.2b1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,724 kB
  • sloc: cpp: 12,515; ansic: 666; python: 28; sh: 25; makefile: 18
file content (74 lines) | stat: -rw-r--r-- 2,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "../src/consumer_queue.h"
#include "../src/sample.h"
#include <atomic>
#include <catch2/catch.hpp>
#include <thread>

// clazy:excludeall=non-pod-global-static

TEST_CASE("consumer_queue", "[queue][basic]") {
	const int size = 10;
	lsl::factory fac(lsl_channel_format_t::cft_int8, 4, size / 2);
	lsl::consumer_queue queue(size);
	for (int i = 0; i <= size; ++i) queue.push_sample(fac.new_sample(i, true));

	// Does the queue respect the capacity?
	CHECK(queue.read_available() == size);

	// Are the right samples dropped when going over capacity?
	CHECK(static_cast<int>(queue.pop_sample()->timestamp()) == 1);

	// Does flush() return the correct count?
	CHECK(queue.flush() == size - 1);

	// Is the queue empty after flush()ing?
	CHECK(queue.empty());
}

TEST_CASE("consumer_queue_threaded", "[queue][threads]") {
	const unsigned int size = 100000;
	lsl::factory fac(lsl_channel_format_t::cft_int8, 4, 1);
	auto sample = fac.new_sample(0.0, true);
	lsl::consumer_queue queue(size);
	std::atomic<bool> done{false};

	std::thread pusher([&]() {
		for (unsigned int i = 0; i < size; ++i) queue.push_sample(sample);
		done = true;
	});

	unsigned flushes = 0, pulled = 0;
	// Pull samples until the pusher is done and the queue is empty
	while (true) {
		unsigned int n = queue.flush();
		if (n) {
			flushes++;
			pulled += n;
		} else {
			if(done && queue.read_available() == 0) break;
			std::this_thread::yield();
		}
	}
	INFO(flushes)
	CHECK(pulled == size);
	pusher.join();
}

TEST_CASE("sample conversion", "[basic]") {
	lsl::factory fac(lsl_channel_format_t::cft_int64, 2, 1);
	double values[2] = {1, -1};
	int64_t buf[2];
	std::string strbuf[2];
	for (int i = 0; i < 30; ++i) {
		auto sample = fac.new_sample(0.0, true);
		sample->assign_typed(values);
		sample->retrieve_untyped(buf);
		sample->retrieve_typed(strbuf);
		for (int j = 0; j < 1; ++j) {
			CHECK(values[j] == static_cast<int64_t>(buf[j]));
			CHECK(strbuf[j] == std::to_string(buf[j]));
		}
		values[0] = (double)(buf[0] << 1);
		values[1] = (double)(-buf[0]);
	}
}