aboutsummaryrefslogtreecommitdiff
path: root/test/i2ctcp/main.cpp
blob: 1f0c3ff52babac4521b27d146a97bbe5e0e79802 (plain)
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
#include <algorithm>
#include <gtest/gtest.h>

#include "i2ctcpv1.h"

using std::min;

const uint8_t data[] = { 0xff, 0x00, 0xde, 0xad, 0xbe, 0xef, };
const size_t data_len = sizeof(data);
const size_t chunk_size = 6;

char * send_data = nullptr;
size_t send_size = 0;

TEST(i2ctcp, send) {
	i2ctcp_msg_t send_msg = {
		.addr = 0x1122,
		.data = (char *) data,
		.length = data_len,
	};

	ASSERT_TRUE(i2ctcp_write(&send_msg, &send_data, &send_size));
	ASSERT_NE(send_data, nullptr);
	ASSERT_GE(send_size, 0);
}

TEST(i2ctcp, recv) {
	i2ctcp_msg_t recv_msg;
	i2ctcp_read_reset(&recv_msg);
	for (size_t i = 0; i < send_size; i += chunk_size) {
		size_t expected_size = min(send_size, i + chunk_size) - i;

		int parsed = i2ctcp_read(&recv_msg, send_data + i, expected_size);
		EXPECT_GE(parsed, 0);

		if (i + expected_size == send_size)
			EXPECT_EQ(parsed, 0);
		else
			EXPECT_GT(parsed, 0);
	}

	ASSERT_NE(recv_msg.data, nullptr);
	ASSERT_EQ(recv_msg.length, data_len);
	ASSERT_EQ(0, memcmp(recv_msg.data, data, data_len));
}