mirror of
https://github.com/westonrobot/ugv_sdk
synced 2023-04-08 06:32:14 +08:00
updated to use updated asyncio implementation
This commit is contained in:
@@ -38,7 +38,7 @@ void parse_buffer(uint8_t *buf, const size_t bufsize, size_t bytes_received)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::shared_ptr<ASyncCAN> canbus = std::make_shared<ASyncCAN>("can1");
|
||||
std::shared_ptr<AsyncCAN> canbus = std::make_shared<AsyncCAN>("can1");
|
||||
|
||||
// canbus->set_receive_callback(parse_buffer);
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ void parse_buffer(uint8_t *buf, const size_t bufsize, size_t bytes_received)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// ASyncSerial::Ptr serial = ASyncSerial::open_url("/dev/ttyUSB0:115200");
|
||||
std::shared_ptr<ASyncSerial> serial = std::make_shared<ASyncSerial>("/dev/ttyO5", 115200);
|
||||
// AsyncSerial::Ptr serial = AsyncSerial::open_url("/dev/ttyUSB0:115200");
|
||||
std::shared_ptr<AsyncSerial> serial = std::make_shared<AsyncSerial>("/dev/ttyO5", 115200);
|
||||
|
||||
serial->set_receive_callback(parse_buffer);
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ int main(int argc, char *argv[])
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::shared_ptr<ASyncSerial> serial = std::make_shared<ASyncSerial>(device_name, baud);
|
||||
std::shared_ptr<AsyncSerial> serial = std::make_shared<AsyncSerial>(device_name, baud);
|
||||
|
||||
serial->set_receive_callback(parse_buffer);
|
||||
|
||||
|
||||
137
tests/unit_tests/ringbuf_test.cpp
Normal file
137
tests/unit_tests/ringbuf_test.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* ringbuf_test.cpp
|
||||
*
|
||||
* Created on: Dec 09, 2019 11:03
|
||||
* Description:
|
||||
*
|
||||
* Copyright (c) 2019 Ruixiang Du (rdu)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
// #define ALLOW_OVERWRITE
|
||||
#include "asyncio/ring_buffer.hpp"
|
||||
|
||||
using namespace westonrobot;
|
||||
|
||||
#define RINGBUF_SIZE 8
|
||||
|
||||
struct RingBufferTest : testing::Test {
|
||||
RingBufferTest() {}
|
||||
|
||||
RingBuffer<uint8_t, RINGBUF_SIZE> buff;
|
||||
uint8_t test_data[10] = {0x01, 0x02, 0x03, 0x04, 0x05,
|
||||
0x06, 0x07, 0x08, 0x09, 0x0a};
|
||||
uint8_t read_data[10];
|
||||
};
|
||||
|
||||
TEST_F(RingBufferTest, WriteTest) {
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// write less elements than buffer size
|
||||
ASSERT_EQ(buff.Write(test_data, 4), 4);
|
||||
ASSERT_EQ(buff.GetOccupiedSize(), 4);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// write more elements than buffer size
|
||||
ASSERT_EQ(buff.Write(test_data, 8), 4);
|
||||
auto data = buff.GetBuffer();
|
||||
ASSERT_TRUE(data[0] == test_data[0] && data[1] == test_data[1] &&
|
||||
data[2] == test_data[2] && data[3] == test_data[3] &&
|
||||
data[4] == test_data[0] && data[5] == test_data[1] &&
|
||||
data[6] == test_data[2] && data[7] == test_data[3]);
|
||||
// buff.PrintBuffer();
|
||||
}
|
||||
|
||||
TEST_F(RingBufferTest, ReadTest) {
|
||||
buff.Write(test_data, 4);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// read first two elements
|
||||
ASSERT_EQ(buff.Read(read_data, 2), 2);
|
||||
ASSERT_TRUE(read_data[0] == test_data[0] && read_data[1] == test_data[1]);
|
||||
|
||||
// read the next two elements
|
||||
ASSERT_EQ(buff.Read(&read_data[2], 2), 2);
|
||||
ASSERT_TRUE(read_data[2] == test_data[2] && read_data[3] == test_data[3]);
|
||||
|
||||
// std::cout << "read content: " << std::endl;
|
||||
// for (int i = 0; i < 4; ++i)
|
||||
// std::cout << "[" << i << "]"
|
||||
// << " " << static_cast<int>(read_data[i]) << std::endl;
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// no element to be read
|
||||
ASSERT_EQ(buff.Read(read_data, 4), 0);
|
||||
}
|
||||
|
||||
TEST_F(RingBufferTest, PeekTest) {
|
||||
buff.Write(test_data, 4);
|
||||
|
||||
// peek less elements than what's available
|
||||
ASSERT_EQ(buff.Peek(read_data, 2), 2);
|
||||
ASSERT_TRUE(read_data[0] == test_data[0] && read_data[1] == test_data[1]);
|
||||
|
||||
// peek more elements than what's available
|
||||
ASSERT_EQ(buff.Peek(read_data, 5), 4);
|
||||
ASSERT_TRUE(read_data[0] == test_data[0] && read_data[1] == test_data[1] &&
|
||||
read_data[2] == test_data[2] && read_data[3] == test_data[3]);
|
||||
|
||||
// read all elements out
|
||||
ASSERT_EQ(buff.Read(read_data, 4), 4);
|
||||
ASSERT_TRUE(read_data[0] == test_data[0] && read_data[1] == test_data[1] &&
|
||||
read_data[2] == test_data[2] && read_data[3] == test_data[3]);
|
||||
|
||||
// no element to be peeked
|
||||
ASSERT_EQ(buff.Peek(read_data, 2), 0);
|
||||
}
|
||||
|
||||
TEST_F(RingBufferTest, OverwriteTest) {
|
||||
// expecting overflow after writting 8 elements
|
||||
ASSERT_EQ(buff.Write(test_data, 8), 8);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// expecting overflow after writting 2 more elements
|
||||
ASSERT_EQ(buff.Write(&test_data[8], 2), 0);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// not expecting overflow after reading 2 elements
|
||||
ASSERT_EQ(buff.Read(read_data, 2), 2);
|
||||
ASSERT_TRUE(read_data[0] == test_data[0] && read_data[1] == test_data[1]);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
// expecting overflow after writting 2 elements again
|
||||
ASSERT_EQ(buff.Write(test_data, 2), 2);
|
||||
}
|
||||
|
||||
TEST_F(RingBufferTest, StateTest) {
|
||||
ASSERT_TRUE(buff.IsEmpty());
|
||||
|
||||
buff.Write(test_data, 4);
|
||||
buff.Read(read_data, 2);
|
||||
|
||||
ASSERT_EQ(buff.GetFreeSize(), 6);
|
||||
ASSERT_EQ(buff.GetOccupiedSize(), 2);
|
||||
|
||||
buff.Read(&read_data[2], 2);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
ASSERT_TRUE(buff.IsEmpty());
|
||||
|
||||
buff.Write(test_data, 8);
|
||||
|
||||
// buff.PrintBuffer();
|
||||
|
||||
ASSERT_EQ(buff.GetOccupiedSize(), 8);
|
||||
ASSERT_TRUE(buff.IsFull());
|
||||
}
|
||||
Reference in New Issue
Block a user