diff --git a/demos/byte.c b/demos/byte.c new file mode 100644 index 0000000000000000000000000000000000000000..ecb9928042cbcd2d2eba4ed022b6a8752ebe1334 --- /dev/null +++ b/demos/byte.c @@ -0,0 +1,33 @@ +#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); + } + + 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 diff --git a/include/common_interface.h b/include/common_interface.h index 2475999fa2212517ac6a2779472d10e2dab720f7..4f8bfccab2c16b0a758162747cfbc24c68a6e1fe 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,84 @@ 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); + +/** + * @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 5e17ff57221cf1e453dd8f23fd7f3500a3bdd42a..3da2995dc422cea95476a74ca28ba0fadb24d66e 100644 --- a/src/common_interface.c +++ b/src/common_interface.c @@ -102,4 +102,96 @@ 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 +} + +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 >> position) & 1) ; +} + +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; + } + + *in = (uint8_t)internal_byte_common_trans((uint64_t)*in, byte_type, position); + + 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 > 15) { + 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 > 15) { + return COM_BAD_INPUT; + } + + *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 > 31) { + 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 > 31) { + 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 > 63) { + 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 > 63) { + return COM_BAD_INPUT; + } + + *byte_type = internal_byte_to_type((uint64_t)*in, position); + + return COM_OK; +} diff --git a/test/check_byte.c b/test/check_byte.c new file mode 100644 index 0000000000000000000000000000000000000000..2a940f35ec96a699d69dbe7db4c674bd21ba1156 --- /dev/null +++ b/test/check_byte.c @@ -0,0 +1,179 @@ +#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_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; + 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_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; + 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_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; + 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; + +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; + 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_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); + + return s; +} \ No newline at end of file diff --git a/test/check_interface.c b/test/check_interface.c index f3e091c395b56949cf23829da6b98cb028080b0d..c4aa5305d027bd17877edcd8de7e16f544d68e1c 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 668d5fe9a0d153dcd0f9e1c18fd34f52199beccb..45b23988a6a8d94bb709d7d85dba62ae7ad93f22 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