mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 11:08:34 +08:00
improve wbuffer/rbuffer
This commit is contained in:
parent
c252652f4e
commit
23fee87e4d
@ -126,7 +126,25 @@ ret_t wbuffer_write_uint32(wbuffer_t* wbuffer, uint32_t value) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t wbuffer_write_float(wbuffer_t* wbuffer, float_t value) {
|
||||
ret_t wbuffer_write_uint64(wbuffer_t* wbuffer, uint64_t value) {
|
||||
return_value_if_fail(wbuffer_extend_delta(wbuffer, sizeof(value)) == RET_OK, RET_BAD_PARAMS);
|
||||
|
||||
memcpy(wbuffer->data + wbuffer->cursor, &value, sizeof(value));
|
||||
wbuffer->cursor += sizeof(value);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t wbuffer_write_float(wbuffer_t* wbuffer, float value) {
|
||||
return_value_if_fail(wbuffer_extend_delta(wbuffer, sizeof(value)) == RET_OK, RET_BAD_PARAMS);
|
||||
|
||||
memcpy(wbuffer->data + wbuffer->cursor, &value, sizeof(value));
|
||||
wbuffer->cursor += sizeof(value);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t wbuffer_write_double(wbuffer_t* wbuffer, double value) {
|
||||
return_value_if_fail(wbuffer_extend_delta(wbuffer, sizeof(value)) == RET_OK, RET_BAD_PARAMS);
|
||||
|
||||
memcpy(wbuffer->data + wbuffer->cursor, &value, sizeof(value));
|
||||
@ -211,7 +229,27 @@ ret_t rbuffer_read_uint32(rbuffer_t* rbuffer, uint32_t* value) {
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t rbuffer_read_float(rbuffer_t* rbuffer, float_t* value) {
|
||||
ret_t rbuffer_read_uint64(rbuffer_t* rbuffer, uint64_t* value) {
|
||||
return_value_if_fail(rbuffer != NULL && rbuffer->data != NULL && value != NULL, RET_BAD_PARAMS);
|
||||
return_value_if_fail((rbuffer->cursor + sizeof(*value)) <= rbuffer->capacity, RET_BAD_PARAMS);
|
||||
|
||||
memcpy(value, rbuffer->data + rbuffer->cursor, sizeof(*value));
|
||||
rbuffer->cursor += sizeof(*value);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t rbuffer_read_float(rbuffer_t* rbuffer, float* value) {
|
||||
return_value_if_fail(rbuffer != NULL && rbuffer->data != NULL && value != NULL, RET_BAD_PARAMS);
|
||||
return_value_if_fail((rbuffer->cursor + sizeof(*value)) <= rbuffer->capacity, RET_BAD_PARAMS);
|
||||
|
||||
memcpy(value, rbuffer->data + rbuffer->cursor, sizeof(*value));
|
||||
rbuffer->cursor += sizeof(*value);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t rbuffer_read_double(rbuffer_t* rbuffer, double* value) {
|
||||
return_value_if_fail(rbuffer != NULL && rbuffer->data != NULL && value != NULL, RET_BAD_PARAMS);
|
||||
return_value_if_fail((rbuffer->cursor + sizeof(*value)) <= rbuffer->capacity, RET_BAD_PARAMS);
|
||||
|
||||
|
@ -167,14 +167,34 @@ ret_t wbuffer_write_uint16(wbuffer_t* wbuffer, uint16_t value);
|
||||
ret_t wbuffer_write_uint32(wbuffer_t* wbuffer, uint32_t value);
|
||||
|
||||
/**
|
||||
* @method wbuffer_write_float
|
||||
* 写入float数据。
|
||||
* @method wbuffer_write_uint64
|
||||
* 写入uint64数据。
|
||||
* @param {wbuffer_t*} wbuffer wbuffer对象。
|
||||
* @param {float_t} value 要写入的数据。
|
||||
* @param {uint64_t} value 要写入的数据。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t wbuffer_write_float(wbuffer_t* wbuffer, float_t value);
|
||||
ret_t wbuffer_write_uint64(wbuffer_t* wbuffer, uint64_t value);
|
||||
|
||||
/**
|
||||
* @method wbuffer_write_float
|
||||
* 写入float数据。
|
||||
* @param {wbuffer_t*} wbuffer wbuffer对象。
|
||||
* @param {float} value 要写入的数据。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t wbuffer_write_float(wbuffer_t* wbuffer, float value);
|
||||
|
||||
/**
|
||||
* @method wbuffer_write_double
|
||||
* 写入double数据。
|
||||
* @param {wbuffer_t*} wbuffer wbuffer对象。
|
||||
* @param {double} value 要写入的数据。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t wbuffer_write_double(wbuffer_t* wbuffer, double value);
|
||||
|
||||
/**
|
||||
* @method wbuffer_write_binary
|
||||
@ -309,14 +329,34 @@ ret_t rbuffer_read_uint16(rbuffer_t* rbuffer, uint16_t* value);
|
||||
ret_t rbuffer_read_uint32(rbuffer_t* rbuffer, uint32_t* value);
|
||||
|
||||
/**
|
||||
* @method rbuffer_read_float
|
||||
* 读取float数据。
|
||||
* @method rbuffer_read_uint64
|
||||
* 读取uint64数据。
|
||||
* @param {rbuffer_t*} rbuffer rbuffer对象。
|
||||
* @param {float_t*} value 返回读取的数据。
|
||||
* @param {uint64_t*} value 返回读取的数据。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t rbuffer_read_float(rbuffer_t* rbuffer, float_t* value);
|
||||
ret_t rbuffer_read_uint64(rbuffer_t* rbuffer, uint64_t* value);
|
||||
|
||||
/**
|
||||
* @method rbuffer_read_float
|
||||
* 读取float数据。
|
||||
* @param {rbuffer_t*} rbuffer rbuffer对象。
|
||||
* @param {float*} value 返回读取的数据。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t rbuffer_read_float(rbuffer_t* rbuffer, float* value);
|
||||
|
||||
/**
|
||||
* @method rbuffer_read_double
|
||||
* 读取double数据。
|
||||
* @param {rbuffer_t*} rbuffer rbuffer对象。
|
||||
* @param {double*} value 返回读取的数据。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t rbuffer_read_double(rbuffer_t* rbuffer, double* value);
|
||||
|
||||
/**
|
||||
* @method rbuffer_read_binary
|
||||
|
@ -31,9 +31,9 @@ TEST(Buffer, demo2) {
|
||||
}
|
||||
|
||||
static void testWriteRead(wbuffer_t* wb) {
|
||||
uint8_t v8;
|
||||
uint16_t v16;
|
||||
uint32_t v32;
|
||||
uint8_t v8 = 0;
|
||||
uint16_t v16 = 0;
|
||||
uint32_t v32 = 0;
|
||||
rbuffer_t rbuffer;
|
||||
rbuffer_t* rb = &rbuffer;
|
||||
|
||||
@ -72,13 +72,49 @@ static void testWriteRead(wbuffer_t* wb) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Buffer, basic) {
|
||||
TEST(Buffer, basic1) {
|
||||
float f32 = 0;
|
||||
double f64 = 0;
|
||||
uint64_t v64 = 0;
|
||||
uint8_t data[128];
|
||||
wbuffer_t wbuffer;
|
||||
rbuffer_t rbuffer;
|
||||
rbuffer_t* rb = &rbuffer;
|
||||
|
||||
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
|
||||
ASSERT_EQ(wbuffer_write_uint64(&wbuffer, 0x112233445566), RET_OK);
|
||||
ASSERT_EQ(wbuffer.cursor, sizeof(uint64_t));
|
||||
ASSERT_EQ(rbuffer_init(rb, wbuffer.data, wbuffer.cursor)!= NULL, true);
|
||||
ASSERT_EQ(rbuffer_read_uint64(rb, &v64), RET_OK);
|
||||
ASSERT_EQ(v64, 0x112233445566);
|
||||
ASSERT_EQ(rbuffer_has_more(rb), FALSE);
|
||||
|
||||
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
|
||||
ASSERT_EQ(wbuffer_write_float(&wbuffer, 100), RET_OK);
|
||||
ASSERT_EQ(wbuffer.cursor, sizeof(float));
|
||||
ASSERT_EQ(rbuffer_init(rb, wbuffer.data, wbuffer.cursor)!= NULL, true);
|
||||
ASSERT_EQ(rbuffer_read_float(rb, &f32), RET_OK);
|
||||
ASSERT_EQ(f32, 100.0F);
|
||||
ASSERT_EQ(rbuffer_has_more(rb), FALSE);
|
||||
|
||||
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
|
||||
ASSERT_EQ(wbuffer_write_double(&wbuffer, 100), RET_OK);
|
||||
ASSERT_EQ(wbuffer.cursor, sizeof(double));
|
||||
ASSERT_EQ(rbuffer_init(rb, wbuffer.data, wbuffer.cursor)!= NULL, true);
|
||||
ASSERT_EQ(rbuffer_read_double(rb, &f64), RET_OK);
|
||||
ASSERT_EQ(f64, 100.0F);
|
||||
ASSERT_EQ(rbuffer_has_more(rb), FALSE);
|
||||
}
|
||||
|
||||
TEST(Buffer, basic2) {
|
||||
uint8_t data[128];
|
||||
wbuffer_t wbuffer;
|
||||
ASSERT_EQ(wbuffer_init(&wbuffer, data, sizeof(data)), &wbuffer);
|
||||
|
||||
testWriteRead(&wbuffer);
|
||||
}
|
||||
uint64_t v64 = 0;
|
||||
float f32 = 0;
|
||||
double f64 = 0;
|
||||
|
||||
TEST(Buffer, extendable) {
|
||||
wbuffer_t wbuffer;
|
||||
|
Loading…
Reference in New Issue
Block a user