From 54a882bf2ff2a9afad7c98a0d118a6321d1867c4 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Wed, 6 Mar 2024 14:11:30 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=B8=80=E5=AD=97?= =?UTF-8?q?=E8=8A=82set=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/common_interface.h | 15 +++++++++++++++ src/common_interface.c | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/common_interface.h b/include/common_interface.h index 2475999..fe9d0f5 100644 --- a/include/common_interface.h +++ b/include/common_interface.h @@ -6,6 +6,7 @@ #include +#include #ifndef COMMON_INTERFACE_H #define COMMON_INTERFACE_H @@ -15,6 +16,11 @@ typedef enum { COMMON_SMALL_ENDIAN //小端 }common_endian; +typedef enum { + COMMON_BYTE_ZERO = 0, //位0 + COMMON_BYTE_ONE = 1 //位1 +}common_byte_type; + /** * \brief 将prinf恢复为默认模式 */ @@ -95,4 +101,13 @@ int base64_encode(char out[], int *olen, int osize, char in[], int inlen); */ int base64_decode(char out[], int *olen, int osize, char in[], int inlen); +/** + * @brief 1字节位操作set接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[in] 要将该位设置为1或0 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_1_set(uint8_t *in, common_byte_type byte_type, int position); #endif \ No newline at end of file diff --git a/src/common_interface.c b/src/common_interface.c index 5e17ff5..697bc1d 100644 --- a/src/common_interface.c +++ b/src/common_interface.c @@ -102,4 +102,22 @@ int quick_sort(int *arr, int left, int right){ } quick_sort(arr, left, l-1); quick_sort(arr, l+1, right); -} \ No newline at end of file +} + +int common_byte_1_set(uint8_t *in, common_byte_type byte_type, int position) { + uint8_t number = 0; + uint8_t mask = 1 << position; + + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 7) { + return COM_BAD_INPUT; + } + + number = *in; + + if(((number & mask) == 0 && byte_type == COMMON_BYTE_ONE) || ((number & mask) != 0 && byte_type == COMMON_BYTE_ZERO)) { + number ^= mask; + } +ok: + *in = number; + return COM_OK; +} -- Gitee From f5cdefa1c562e9943d21966b7a69b42437067a5d Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Wed, 6 Mar 2024 14:11:51 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=AE=9E=E7=8E=B0demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demos/byte.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 demos/byte.c diff --git a/demos/byte.c b/demos/byte.c new file mode 100644 index 0000000..8e4e804 --- /dev/null +++ b/demos/byte.c @@ -0,0 +1,21 @@ +#include +#include + +#include "common_errno.h" +#include "common_interface.h" + +int main() { + uint8_t test = 0x0; + int ret = 0; + int i; + + for(i = -2; i <= 10; i++) { + test = 0; + ret = common_byte_1_set(&test, COMMON_BYTE_ONE, i); + + printf("ret = 0x%04x\n", ret); + printf("0x%0x\n\n", test); + } + + return 0; +} \ No newline at end of file -- Gitee From d0841953c27d2d0e7aa4a20477a9726cd6e1c9a6 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Wed, 6 Mar 2024 16:04:49 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=AE=8C=E6=88=901=202=204=208=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E4=BD=8Dget/set=E6=8E=A5=E5=8F=A3=E7=9A=84=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/common_interface.h | 73 ++++++++++++++++++++++++++++++- src/common_interface.c | 90 ++++++++++++++++++++++++++++++++++---- 2 files changed, 154 insertions(+), 9 deletions(-) diff --git a/include/common_interface.h b/include/common_interface.h index fe9d0f5..4f8bfcc 100644 --- a/include/common_interface.h +++ b/include/common_interface.h @@ -107,7 +107,78 @@ int base64_decode(char out[], int *olen, int osize, char in[], int inlen); * @param in[in] 要操作的指针 * @param byte_type[in] 要将该位设置为1或0 * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 - * @return #COM_OK 操作成功 + * @return #COM_OK 操作成功 */ int common_byte_1_set(uint8_t *in, common_byte_type byte_type, int position); + +/** + * @brief 1字节位操作get接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[out] 返回这一位是0还是1 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_1_get(uint8_t *in, common_byte_type *byte_type, int position); + +/** + * @brief 2字节位操作set接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[in] 要将该位设置为1或0 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_2_set(uint16_t *in, common_byte_type byte_type, int position); + +/** + * @brief 2字节位操作get接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[out] 返回这一位是0还是1 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_2_get(uint16_t *in, common_byte_type *byte_type, int position); + +/** + * @brief 4字节位操作set接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[in] 要将该位设置为1或0 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_4_set(uint32_t *in, common_byte_type byte_type, int position); + +/** + * @brief 4字节位操作get接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[out] 返回这一位是0还是1 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_4_get(uint32_t *in, common_byte_type *byte_type, int position); + +/** + * @brief 8字节位操作set接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[in] 要将该位设置为1或0 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_8_set(uint64_t *in, common_byte_type byte_type, int position); + +/** + * @brief 8字节位操作get接口 + * + * @param in[in] 要操作的指针 + * @param byte_type[out] 返回这一位是0还是1 + * @param position[in] 操作的位,从低位开始算,例如: 1 0 1 1 0 0 0 1,修改第三位为1后:1 0 1 1 0 1 0 1 + * @return #COM_OK 操作成功 + */ +int common_byte_8_get(uint64_t *in, common_byte_type *byte_type, int position); + #endif \ No newline at end of file diff --git a/src/common_interface.c b/src/common_interface.c index 697bc1d..7825476 100644 --- a/src/common_interface.c +++ b/src/common_interface.c @@ -104,20 +104,94 @@ int quick_sort(int *arr, int left, int right){ quick_sort(arr, l+1, right); } -int common_byte_1_set(uint8_t *in, common_byte_type byte_type, int position) { - uint8_t number = 0; - uint8_t mask = 1 << position; +static uint64_t internal_byte_common_trans(uint64_t number, common_byte_type byte_type, int position) { + uint64_t mask = 1 << position; + if(((number & mask) == 0 && byte_type == COMMON_BYTE_ONE) || ((number & mask) != 0 && byte_type == COMMON_BYTE_ZERO)) { + number ^= mask; + } + return number; +} + +static common_byte_type internal_byte_to_type(uint64_t number, int position) { + return (common_byte_type)(number & (1 << position)) >> position; +} +int common_byte_1_set(uint8_t *in, common_byte_type byte_type, int position) { if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 7) { return COM_BAD_INPUT; } - number = *in; + *in = (uint8_t)internal_byte_common_trans((uint64_t)*in, byte_type, position); - if(((number & mask) == 0 && byte_type == COMMON_BYTE_ONE) || ((number & mask) != 0 && byte_type == COMMON_BYTE_ZERO)) { - number ^= mask; + return COM_OK; +} + +int common_byte_1_get(uint8_t *in, common_byte_type *byte_type, int position) { + if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + return COM_BAD_INPUT; + } + + *byte_type = internal_byte_to_type((uint64_t)*in, position); + + return COM_OK; +} + +int common_byte_2_set(uint16_t *in, common_byte_type byte_type, int position) { + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 16) { + return COM_BAD_INPUT; + } + + *in = (uint16_t)internal_byte_common_trans((uint64_t)*in, byte_type, position); + + return COM_OK; +} + +int common_byte_2_get(uint16_t *in, common_byte_type *byte_type, int position) { + if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + return COM_BAD_INPUT; } -ok: - *in = number; + + *byte_type = internal_byte_to_type((uint64_t)*in, position); + + return COM_OK; +} + +int common_byte_4_set(uint32_t *in, common_byte_type byte_type, int position) { + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 32) { + return COM_BAD_INPUT; + } + + *in = (uint32_t)internal_byte_common_trans((uint64_t)*in, byte_type, position); + + return COM_OK; +} + +int common_byte_4_get(uint32_t *in, common_byte_type *byte_type, int position) { + if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + return COM_BAD_INPUT; + } + + *byte_type = internal_byte_to_type((uint64_t)*in, position); + + return COM_OK; +} + +int common_byte_8_set(uint64_t *in, common_byte_type byte_type, int position) { + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 64) { + return COM_BAD_INPUT; + } + + *in = (uint64_t)internal_byte_common_trans((uint64_t)*in, byte_type, position); + + return COM_OK; +} + +int common_byte_8_get(uint64_t *in, common_byte_type *byte_type, int position) { + if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + return COM_BAD_INPUT; + } + + *byte_type = internal_byte_to_type((uint64_t)*in, position); + return COM_OK; } -- Gitee From 860e567df068b2d171ec6c0c2f7a34cd461569b3 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Wed, 6 Mar 2024 16:58:18 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demos/byte.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/demos/byte.c b/demos/byte.c index 8e4e804..ecb9928 100644 --- a/demos/byte.c +++ b/demos/byte.c @@ -17,5 +17,17 @@ int main() { printf("0x%0x\n\n", test); } + test = 0x11; + common_byte_type type = 0; + for(i = 7; i >= 0; i--) { + ret = common_byte_1_get(&test, &type, i); + if(ret != COM_OK) { + printf("error at line: %d, i = %d\n", __LINE__, i); + return 1; + } + printf("%d ", type); + } + printf("\n"); + return 0; } \ No newline at end of file -- Gitee From 4a80334ee266e8bc13559d2ed7a0021f629d3606 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Wed, 6 Mar 2024 16:58:30 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common_interface.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common_interface.c b/src/common_interface.c index 7825476..a818f87 100644 --- a/src/common_interface.c +++ b/src/common_interface.c @@ -113,7 +113,7 @@ static uint64_t internal_byte_common_trans(uint64_t number, common_byte_type byt } static common_byte_type internal_byte_to_type(uint64_t number, int position) { - return (common_byte_type)(number & (1 << position)) >> position; + return (common_byte_type)((number >> position) & 1) ; } int common_byte_1_set(uint8_t *in, common_byte_type byte_type, int position) { @@ -147,7 +147,7 @@ int common_byte_2_set(uint16_t *in, common_byte_type byte_type, int position) { } int common_byte_2_get(uint16_t *in, common_byte_type *byte_type, int position) { - if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + if(in == NULL || byte_type == NULL || position < 0 || position > 16) { return COM_BAD_INPUT; } @@ -167,7 +167,7 @@ int common_byte_4_set(uint32_t *in, common_byte_type byte_type, int position) { } int common_byte_4_get(uint32_t *in, common_byte_type *byte_type, int position) { - if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + if(in == NULL || byte_type == NULL || position < 0 || position > 32) { return COM_BAD_INPUT; } @@ -187,7 +187,7 @@ int common_byte_8_set(uint64_t *in, common_byte_type byte_type, int position) { } int common_byte_8_get(uint64_t *in, common_byte_type *byte_type, int position) { - if(in == NULL || byte_type == NULL || position < 0 || position > 7) { + if(in == NULL || byte_type == NULL || position < 0 || position > 64) { return COM_BAD_INPUT; } -- Gitee From f6481c28f635c83926ad0715b610ed2733271694 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Wed, 6 Mar 2024 16:59:19 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E7=9A=84=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/check_byte.c | 107 +++++++++++++++++++++++++++++++++++++++++ test/check_interface.c | 1 + test/check_interface.h | 1 + 3 files changed, 109 insertions(+) create mode 100644 test/check_byte.c diff --git a/test/check_byte.c b/test/check_byte.c new file mode 100644 index 0000000..321f8f9 --- /dev/null +++ b/test/check_byte.c @@ -0,0 +1,107 @@ +#include +#include + +#include "check.h" +#include "common_interface.h" +#include "check_interface.h" +#include "common_errno.h" + +START_TEST(test_1_ok) { + uint8_t number = 0x0f - 1; + common_byte_type type = COMMON_BYTE_ONE; + int i = 0; + + ck_assert_int_eq(common_byte_1_set(&number, COMMON_BYTE_ONE, 0), COM_OK); + ck_assert_int_eq(number, 0x0f); + + // 低四位判断 + for(i = 0; i < 4; i++) { + ck_assert_int_eq(common_byte_1_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ONE); + } + + // 高四位判断 + for(i = 4; i < 8; i++) { + ck_assert_int_eq(common_byte_1_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ZERO); + } +} +END_TEST; + +START_TEST(test_2_ok) { + uint16_t number = 0x00ff - 1; + common_byte_type type = COMMON_BYTE_ONE; + int i = 0; + + ck_assert_int_eq(common_byte_2_set(&number, COMMON_BYTE_ONE, 0), COM_OK); + ck_assert_int_eq(number, 0x00ff); + + for(i = 0; i < 8; i++) { + ck_assert_int_eq(common_byte_2_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ONE); + } + + for(i = 8; i < 16; i++) { + ck_assert_int_eq(common_byte_2_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ZERO); + } +} +END_TEST; + +START_TEST(test_4_ok) { + uint32_t number = 0x0000ffff - 1; + common_byte_type type = COMMON_BYTE_ONE; + int i = 0; + + ck_assert_int_eq(common_byte_4_set(&number, COMMON_BYTE_ONE, 0), COM_OK); + ck_assert_int_eq(number, 0x0000ffff); + + for(i = 0; i < 16; i++) { + ck_assert_int_eq(common_byte_4_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ONE); + } + + for(i = 16; i < 32; i++) { + ck_assert_int_eq(common_byte_4_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ZERO); + } +} +END_TEST; + +START_TEST(test_8_ok) { + uint64_t number = 0x00000000ffffffff - 1; + common_byte_type type = COMMON_BYTE_ONE; + int i = 0; + + ck_assert_int_eq(common_byte_8_set(&number, COMMON_BYTE_ONE, 0), COM_OK); + ck_assert_int_eq(number, 0x00000000ffffffff); + + for(i = 0; i < 32; i++) { + ck_assert_int_eq(common_byte_8_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ONE); + } + + for(i = 50; i < 64; i++) { + ck_assert_int_eq(common_byte_8_get(&number, &type, i), COM_OK); + ck_assert_int_eq(type, COMMON_BYTE_ZERO); + } +} +END_TEST; + +Suite *check_byte(void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create("byte get & set test"); + + tc_core = tcase_create("core"); + tcase_add_test(tc_core, test_1_ok); + tcase_add_test(tc_core, test_2_ok); + tcase_add_test(tc_core, test_4_ok); + tcase_add_test(tc_core, test_8_ok); + + suite_add_tcase(s, tc_core); + + return s; +} \ No newline at end of file diff --git a/test/check_interface.c b/test/check_interface.c index f3e091c..c4aa530 100644 --- a/test/check_interface.c +++ b/test/check_interface.c @@ -7,6 +7,7 @@ int main() int number_failed = 0; sr = srunner_create(check_base64()); + srunner_add_suite(sr, check_byte()); /* can debug with gdb */ srunner_set_fork_status(sr, CK_NOFORK); diff --git a/test/check_interface.h b/test/check_interface.h index 668d5fe..45b2398 100644 --- a/test/check_interface.h +++ b/test/check_interface.h @@ -6,5 +6,6 @@ #include "common_interface.h" Suite *check_base64(void); +Suite *check_byte(void); #endif \ No newline at end of file -- Gitee From adfae4fb15dfeb25f85a9fa13c1c9226ecf73d09 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Thu, 7 Mar 2024 10:39:25 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=AE=8C=E6=88=90byte=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/check_byte.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/test/check_byte.c b/test/check_byte.c index 321f8f9..2a940f3 100644 --- a/test/check_byte.c +++ b/test/check_byte.c @@ -28,6 +28,23 @@ START_TEST(test_1_ok) { } END_TEST; +START_TEST(test_1_input) { + uint8_t number = 0x0f - 1; + common_byte_type type = COMMON_BYTE_ONE; + + ck_assert_int_eq(common_byte_1_set(NULL, COMMON_BYTE_ONE, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_set(&number, 2, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_set(&number, -1, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_set(&number, COMMON_BYTE_ONE, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_set(&number, COMMON_BYTE_ONE, 8), COM_BAD_INPUT); + + ck_assert_int_eq(common_byte_1_get(NULL, &type, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_get(&number, NULL, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_get(&number, &type, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_1_get(&number, &type, 8), COM_BAD_INPUT); +} +END_TEST; + START_TEST(test_2_ok) { uint16_t number = 0x00ff - 1; common_byte_type type = COMMON_BYTE_ONE; @@ -48,6 +65,23 @@ START_TEST(test_2_ok) { } END_TEST; +START_TEST(test_2_input) { + uint16_t number = 0x00ff - 1; + common_byte_type type = COMMON_BYTE_ONE; + + ck_assert_int_eq(common_byte_2_set(NULL, COMMON_BYTE_ONE, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_set(&number, 2, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_set(&number, -1, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_set(&number, COMMON_BYTE_ONE, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_set(&number, COMMON_BYTE_ONE, 16), COM_BAD_INPUT); + + ck_assert_int_eq(common_byte_2_get(NULL, &type, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_get(&number, NULL, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_get(&number, &type, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_2_get(&number, &type, 16), COM_BAD_INPUT); +} +END_TEST; + START_TEST(test_4_ok) { uint32_t number = 0x0000ffff - 1; common_byte_type type = COMMON_BYTE_ONE; @@ -68,6 +102,23 @@ START_TEST(test_4_ok) { } END_TEST; +START_TEST(test_4_input) { + uint32_t number = 0x0000ffff - 1; + common_byte_type type = COMMON_BYTE_ONE; + + ck_assert_int_eq(common_byte_4_set(NULL, COMMON_BYTE_ONE, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_set(&number, 2, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_set(&number, -1, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_set(&number, COMMON_BYTE_ONE, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_set(&number, COMMON_BYTE_ONE, 32), COM_BAD_INPUT); + + ck_assert_int_eq(common_byte_4_get(NULL, &type, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_get(&number, NULL, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_get(&number, &type, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_4_get(&number, &type, 32), COM_BAD_INPUT); +} +END_TEST; + START_TEST(test_8_ok) { uint64_t number = 0x00000000ffffffff - 1; common_byte_type type = COMMON_BYTE_ONE; @@ -88,6 +139,23 @@ START_TEST(test_8_ok) { } END_TEST; +START_TEST(test_8_input) { + uint64_t number = 0x00000000ffffffff - 1; + common_byte_type type = COMMON_BYTE_ONE; + + ck_assert_int_eq(common_byte_8_set(NULL, COMMON_BYTE_ONE, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_set(&number, 2, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_set(&number, -1, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_set(&number, COMMON_BYTE_ONE, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_set(&number, COMMON_BYTE_ONE, 64), COM_BAD_INPUT); + + ck_assert_int_eq(common_byte_8_get(NULL, &type, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_get(&number, NULL, 0), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_get(&number, &type, -1), COM_BAD_INPUT); + ck_assert_int_eq(common_byte_8_get(&number, &type, 64), COM_BAD_INPUT); +} +END_TEST; + Suite *check_byte(void) { Suite *s; @@ -97,9 +165,13 @@ Suite *check_byte(void) tc_core = tcase_create("core"); tcase_add_test(tc_core, test_1_ok); + tcase_add_test(tc_core, test_1_input); tcase_add_test(tc_core, test_2_ok); + tcase_add_test(tc_core, test_2_input); tcase_add_test(tc_core, test_4_ok); + tcase_add_test(tc_core, test_4_input); tcase_add_test(tc_core, test_8_ok); + tcase_add_test(tc_core, test_8_input); suite_add_tcase(s, tc_core); -- Gitee From f74fac8230f3036f9875c542baa28e6fad383c43 Mon Sep 17 00:00:00 2001 From: bear <1248825327@qq.com> Date: Thu, 7 Mar 2024 10:39:47 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D2=204=208=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E7=9A=84position=E5=8F=82=E6=95=B0=E9=99=90=E5=AE=9A?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E4=B8=8D=E5=AF=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common_interface.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/common_interface.c b/src/common_interface.c index a818f87..3da2995 100644 --- a/src/common_interface.c +++ b/src/common_interface.c @@ -137,7 +137,7 @@ int common_byte_1_get(uint8_t *in, common_byte_type *byte_type, int position) { } int common_byte_2_set(uint16_t *in, common_byte_type byte_type, int position) { - if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 16) { + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 15) { return COM_BAD_INPUT; } @@ -147,7 +147,7 @@ int common_byte_2_set(uint16_t *in, common_byte_type byte_type, int position) { } int common_byte_2_get(uint16_t *in, common_byte_type *byte_type, int position) { - if(in == NULL || byte_type == NULL || position < 0 || position > 16) { + if(in == NULL || byte_type == NULL || position < 0 || position > 15) { return COM_BAD_INPUT; } @@ -157,7 +157,7 @@ int common_byte_2_get(uint16_t *in, common_byte_type *byte_type, int position) { } int common_byte_4_set(uint32_t *in, common_byte_type byte_type, int position) { - if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 32) { + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 31) { return COM_BAD_INPUT; } @@ -167,7 +167,7 @@ int common_byte_4_set(uint32_t *in, common_byte_type byte_type, int position) { } int common_byte_4_get(uint32_t *in, common_byte_type *byte_type, int position) { - if(in == NULL || byte_type == NULL || position < 0 || position > 32) { + if(in == NULL || byte_type == NULL || position < 0 || position > 31) { return COM_BAD_INPUT; } @@ -177,7 +177,7 @@ int common_byte_4_get(uint32_t *in, common_byte_type *byte_type, int position) { } int common_byte_8_set(uint64_t *in, common_byte_type byte_type, int position) { - if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 64) { + if(in == NULL || (byte_type != COMMON_BYTE_ZERO && byte_type != COMMON_BYTE_ONE) || position < 0 || position > 63) { return COM_BAD_INPUT; } @@ -187,7 +187,7 @@ int common_byte_8_set(uint64_t *in, common_byte_type byte_type, int position) { } int common_byte_8_get(uint64_t *in, common_byte_type *byte_type, int position) { - if(in == NULL || byte_type == NULL || position < 0 || position > 64) { + if(in == NULL || byte_type == NULL || position < 0 || position > 63) { return COM_BAD_INPUT; } -- Gitee