Buffer integer finished with tests

This commit is contained in:
Imbus 2025-08-08 05:13:20 +02:00
parent 3d45b8b472
commit 713d96d770
3 changed files with 258 additions and 30 deletions

124
buf/buf.c
View file

@ -1,6 +1,18 @@
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
/**
* @brief Write a 8 bit unsigned int to the buffer
*
* @param buf The buffer to write to
* @param value The 8 bit unsigned int to write
* @param index The index read/write pointer
*/
void buf_append_i8(uint8_t *buf, int8_t value, size_t *index) {
buf[(*index)++] = value;
}
/** /**
* @brief Write a 8 bit unsigned int to the buffer * @brief Write a 8 bit unsigned int to the buffer
* *
@ -24,6 +36,18 @@ void buf_append_i16(uint8_t *buf, int16_t value, size_t *index) {
buf[(*index)++] = value & 0xFF; buf[(*index)++] = value & 0xFF;
} }
/**
* @brief Write a 16 bit unsigned int to the buffer
*
* @param buf The buffer to write to
* @param value The 16 bit signed int to write
* @param index The index read/write pointer
*/
void buf_append_u16(uint8_t *buf, uint16_t value, size_t *index) {
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = value & 0xFF;
}
/** /**
* @brief Write a 32 bit signed int to the buffer * @brief Write a 32 bit signed int to the buffer
* *
@ -39,16 +63,54 @@ void buf_append_i32(uint8_t *buf, int32_t value, size_t *index) {
} }
/** /**
* @brief Write a 32 bit float to the buffer * @brief Write a 32 bit unsigned int to the buffer
* *
* @param buf The buffer to write to * @param buf The buffer to write to
* @param value The 32 bit float to write * @param value The 32 bit signed int to write
* @param index The index read/write pointer * @param index The index read/write pointer
*/ */
void buf_append_f32(uint8_t *buf, float value, size_t *index) { void buf_append_u32(uint8_t *buf, uint32_t value, size_t *index) {
uint32_t u; buf[(*index)++] = (value >> 24) & 0xFF;
memcpy(&u, &value, sizeof(float)); // Safe conversion buf[(*index)++] = (value >> 16) & 0xFF;
buf_append_i32(buf, (int32_t)u, index); buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = value & 0xFF;
}
void buf_append_i64(uint8_t *buf, int64_t value, int32_t *index) {
buf[(*index)++] = (value >> 56) & 0xFF;
buf[(*index)++] = (value >> 48) & 0xFF;
buf[(*index)++] = (value >> 40) & 0xFF;
buf[(*index)++] = (value >> 32) & 0xFF;
buf[(*index)++] = (value >> 24) & 0xFF;
buf[(*index)++] = (value >> 16) & 0xFF;
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = (value) & 0xFF;
}
void buf_append_u64(uint8_t *buf, uint64_t value, int32_t *index) {
buf[(*index)++] = (value >> 56) & 0xFF;
buf[(*index)++] = (value >> 48) & 0xFF;
buf[(*index)++] = (value >> 40) & 0xFF;
buf[(*index)++] = (value >> 32) & 0xFF;
buf[(*index)++] = (value >> 24) & 0xFF;
buf[(*index)++] = (value >> 16) & 0xFF;
buf[(*index)++] = (value >> 8) & 0xFF;
buf[(*index)++] = (value) & 0xFF;
}
/*
* READS
*/
/**
* @brief Read a 8 bit signed int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 8 bit signed int
*/
int8_t buf_read_i8(const uint8_t *buf, size_t *index) {
return buf[(*index)++];
} }
/** /**
@ -69,7 +131,21 @@ uint8_t buf_read_u8(const uint8_t *buf, size_t *index) {
* @param index The index read/write pointer * @param index The index read/write pointer
* @return 16 bit signed int * @return 16 bit signed int
*/ */
uint8_t buf_read_i16(const uint8_t *buf, size_t *index) { int16_t buf_read_i16(const uint8_t *buf, size_t *index) {
int16_t val = 0;
val |= ((int16_t)buf[(*index)++]) << 8;
val |= ((int16_t)buf[(*index)++]);
return val;
}
/**
* @brief Read a 16 bit int from the buffer
*
* @param buf The buffer to read from
* @param index The index read/write pointer
* @return 16 bit unsigned int
*/
uint16_t buf_read_u16(const uint8_t *buf, size_t *index) {
int16_t val = 0; int16_t val = 0;
val |= ((int16_t)buf[(*index)++]) << 8; val |= ((int16_t)buf[(*index)++]) << 8;
val |= ((int16_t)buf[(*index)++]); val |= ((int16_t)buf[(*index)++]);
@ -93,15 +169,35 @@ int32_t buf_read_i32(const uint8_t *buf, size_t *index) {
} }
/** /**
* @brief Read a 32 bit float from the buffer * @brief Read a 32 bit uint from the buffer
* *
* @param buf The buffer to read from * @param buf The buffer to read from
* @param index The index read/write pointer * @param index The index read/write pointer
* @return 32 bit float * @return 32 bit signed int
*/ */
float buf_read_f32(const uint8_t *buf, size_t *index) { uint32_t buf_read_u32(const uint8_t *buf, size_t *index) {
uint32_t u = (uint32_t)buf_read_i32(buf, index); uint32_t val = 0;
float f; val |= ((uint32_t)buf[(*index)++]) << 24;
memcpy(&f, &u, sizeof(float)); val |= ((uint32_t)buf[(*index)++]) << 16;
return f; val |= ((uint32_t)buf[(*index)++]) << 8;
val |= ((uint32_t)buf[(*index)++]);
return val;
}
int64_t buf_read_i64(const uint8_t *buf, int32_t *index) {
uint64_t res = ((uint64_t)buf[*index]) << 56 | ((uint64_t)buf[*index + 1]) << 48 |
((uint64_t)buf[*index + 2]) << 40 | ((uint64_t)buf[*index + 3]) << 32 |
((uint64_t)buf[*index + 4]) << 24 | ((uint64_t)buf[*index + 5]) << 16 |
((uint64_t)buf[*index + 6]) << 8 | ((uint64_t)buf[*index + 7]);
*index += 8;
return res;
}
uint64_t buf_read_u64(const uint8_t *buf, size_t *index) {
uint64_t res = ((uint64_t)buf[*index]) << 56 | ((uint64_t)buf[*index + 1]) << 48 |
((uint64_t)buf[*index + 2]) << 40 | ((uint64_t)buf[*index + 3]) << 32 |
((uint64_t)buf[*index + 4]) << 24 | ((uint64_t)buf[*index + 5]) << 16 |
((uint64_t)buf[*index + 6]) << 8 | ((uint64_t)buf[*index + 7]);
*index += 8;
return res;
} }

View file

@ -1,12 +1,48 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
void buf_append_i8(uint8_t *buf, int8_t value, size_t *index);
void buf_append_u8(uint8_t *buf, uint8_t value, size_t *index); void buf_append_u8(uint8_t *buf, uint8_t value, size_t *index);
void buf_append_i16(uint8_t *buf, int16_t value, size_t *index);
void buf_append_i32(uint8_t *buf, int32_t value, size_t *index);
void buf_append_f32(uint8_t *buf, float value, size_t *index);
void buf_append_i16(uint8_t *buf, int16_t value, size_t *index);
void buf_append_u16(uint8_t *buf, uint16_t value, size_t *index);
void buf_append_i32(uint8_t *buf, int32_t value, size_t *index);
void buf_append_u32(uint8_t *buf, uint32_t value, size_t *index);
void buf_append_i64(uint8_t *buf, int64_t value, size_t *index);
void buf_append_u64(uint8_t *buf, uint64_t value, size_t *index);
int8_t buf_read_i8(const uint8_t *buf, size_t *index);
uint8_t buf_read_u8(const uint8_t *buf, size_t *index); uint8_t buf_read_u8(const uint8_t *buf, size_t *index);
int16_t buf_read_i16(const uint8_t *buf, size_t *index); int16_t buf_read_i16(const uint8_t *buf, size_t *index);
uint16_t buf_read_u16(const uint8_t *buf, size_t *index);
int32_t buf_read_i32(const uint8_t *buf, size_t *index); int32_t buf_read_i32(const uint8_t *buf, size_t *index);
float buf_read_f32(const uint8_t *buf, size_t *index); uint32_t buf_read_u32(const uint8_t *buf, size_t *index);
int64_t buf_read_i64(const uint8_t *buf, size_t *index);
uint64_t buf_read_u64(const uint8_t *buf, size_t *index);
/*
* Float stuff
*/
// void buf_append_f16(uint8_t *buf, float value, size_t *index);
// void buf_append_f16(uint8_t *buf, float value, float scale, size_t *index);
// void buf_append_f32(uint8_t *buf, float value, size_t *index);
// void buf_append_f32_auto(uint8_t *buf, float value, size_t *index);
//
// void buf_append_f64(uint8_t *buf, double value, size_t *index);
// void buf_append_f64_auto(uint8_t *buf, double value, size_t *index);
// float buf_read_f16(const uint8_t *buf, size_t *index);
// float buf_read_f16(const uint8_t *buf, float scale, size_t *index);
//
// float buf_read_f32(const uint8_t *buf, size_t *index);
// float buf_read_f32_auto(const uint8_t *buf, size_t *index);
//
// double buf_read_f64(const uint8_t *buf, size_t *index);
// double buf_read_f64_auto(const uint8_t *buf, size_t *index);

View file

@ -2,7 +2,9 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
/* /*
* Inspired by Vedders bldc code. * Inspired by Vedders bldc code.
@ -26,7 +28,11 @@ static inline bool float_eq(float a, float b, float epsilon) {
return fabsf(a - b) < epsilon; return fabsf(a - b) < epsilon;
} }
int tests(void);
int main(void) { int main(void) {
(void)tests();
/* This index acts as a read/write pointer */ /* This index acts as a read/write pointer */
size_t index = 0; size_t index = 0;
@ -34,17 +40,15 @@ int main(void) {
uint8_t uint8bit = 8; uint8_t uint8bit = 8;
int16_t int16bit = 16; int16_t int16bit = 16;
int32_t int32bit = 32; int32_t int32bit = 32;
float float32bit = 1337.42;
/* When appending the buffer, the index is passed along and incremented accordingly */ /* When appending the buffer, the index is passed along and incremented
* accordingly */
buf_append_u8(buf, uint8bit, &index); buf_append_u8(buf, uint8bit, &index);
assert(index == 1); assert(index == 1);
buf_append_i16(buf, int16bit, &index); buf_append_i16(buf, int16bit, &index);
assert(index == 3); assert(index == 3);
buf_append_i32(buf, int32bit, &index); buf_append_i32(buf, int32bit, &index);
assert(index == 7); assert(index == 7);
buf_append_f32(buf, float32bit, &index);
assert(index == 11);
/* /*
* Note that after we're done appending, the index value will hold the * Note that after we're done appending, the index value will hold the
@ -52,17 +56,16 @@ int main(void) {
* other medium, you dont have to send the entire buffer length, just send * other medium, you dont have to send the entire buffer length, just send
* up to the index. * up to the index.
* *
* For bonus points for doing it vesc-style, prepend the message with the message type (a * For bonus points for doing it vesc-style, prepend the message with the
* byte-length enum shared by both sides, for example), as well as a message * message type (a byte-length enum shared by both sides, for example), as
* length. You could also use the last two bytes of every message for a * well as a message length. You could also use the last two bytes of every
* crc16 checksum for error resilience. * message for a crc16 checksum for error resilience.
*/ */
/* Zero the original data */ /* Zero the original data */
uint8bit = 0; uint8bit = 0;
int16bit = 0; int16bit = 0;
int32bit = 0; int32bit = 0;
float32bit = 0.f;
/* Read from beginning */ /* Read from beginning */
index = 0; index = 0;
@ -74,14 +77,107 @@ int main(void) {
assert(index == 3); assert(index == 3);
int32bit = buf_read_i32(buf, &index); int32bit = buf_read_i32(buf, &index);
assert(index == 7); assert(index == 7);
float32bit = buf_read_f32(buf, &index);
assert(index == 11);
/* Assert that it all worked out */ /* Assert that it all worked out */
assert(uint8bit == 8); assert(uint8bit == 8);
assert(int16bit == 16); assert(int16bit == 16);
assert(int32bit == 32); assert(int32bit == 32);
assert(float_eq(float32bit, 1337.42, 0.01));
printf("All asserts passed!\n"); printf("All asserts passed!\n");
return 0;
}
void test_i8_u8() {
uint8_t buf[10];
size_t index = 0;
int8_t i8_val = -123;
uint8_t u8_val = 234;
buf_append_i8(buf, i8_val, &index);
buf_append_u8(buf, u8_val, &index);
assert(index == 2);
size_t read_index = 0;
int8_t i8_read = buf_read_i8(buf, &read_index);
uint8_t u8_read = buf_read_u8(buf, &read_index);
assert(i8_read == i8_val);
assert(u8_read == u8_val);
assert(read_index == 2);
}
void test_i16_u16() {
uint8_t buf[10];
size_t index = 0;
int16_t i16_val = -32000;
uint16_t u16_val = 65000;
buf_append_i16(buf, i16_val, &index);
buf_append_u16(buf, u16_val, &index);
assert(index == 4);
size_t read_index = 0;
int16_t i16_read = buf_read_i16(buf, &read_index);
uint16_t u16_read = buf_read_u16(buf, &read_index);
assert(i16_read == i16_val);
assert(u16_read == u16_val);
assert(read_index == 4);
}
void test_i32_u32() {
uint8_t buf[20];
size_t index = 0;
int32_t i32_val = -2000000000;
uint32_t u32_val = 4000000000U;
buf_append_i32(buf, i32_val, &index);
buf_append_u32(buf, u32_val, &index);
assert(index == 8);
size_t read_index = 0;
int32_t i32_read = buf_read_i32(buf, &read_index);
uint32_t u32_read = buf_read_u32(buf, &read_index);
assert(i32_read == i32_val);
assert(u32_read == u32_val);
assert(read_index == 8);
}
void test_i64_u64() {
uint8_t buf[20];
size_t index = 0;
int64_t i64_val = -9000000000000000000LL;
uint64_t u64_val = 18000000000000000000ULL;
buf_append_i64(buf, i64_val, &index);
buf_append_u64(buf, u64_val, &index);
assert(index == 16);
size_t read_index = 0;
int64_t i64_read = buf_read_i64(buf, &read_index);
uint64_t u64_read = buf_read_u64(buf, &read_index);
assert(i64_read == i64_val);
assert(u64_read == u64_val);
assert(read_index == 16);
}
int tests() {
test_i8_u8();
test_i16_u16();
test_i32_u32();
test_i64_u64();
printf("All tests passed.\n");
return 0;
} }