Last active
January 4, 2025 09:10
-
-
Save G36maid/6a0021ae838354abf3455d442ebb976c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<stdlib.h> | |
| #include<stdint.h> | |
| #include <string.h> | |
| #include <time.h> | |
| #include "endian.h" | |
| int32_t endian_convert_ta( uint8_t *pSrc, uint32_t size, uint8_t **ppDst ); //remember to free *ppDst | |
| int32_t endian_equality_test_ta( uint8_t *pNum1 , uint32_t size1 , uint8_t type1 , | |
| uint8_t *pNum2 , uint32_t size2 , uint8_t type2 ); | |
| int test_func(); | |
| int test_func_equal(); | |
| int err_test_null(); | |
| int err_test_size(); | |
| int err_test_type(); | |
| int main() { | |
| int point = 0; | |
| for (int i = 0; i < 10; i++) | |
| point += test_func(); | |
| printf("Point: %d\n", point); | |
| for (int i = 0; i < 4; i++) | |
| point += test_func_equal(); | |
| printf("Point: %d\n", point); | |
| if (err_test_null()) | |
| point+=2; | |
| printf("Point: %d\n", point); | |
| if (err_test_size()) | |
| point+=2; | |
| printf("Point: %d\n", point); | |
| if (err_test_type()) | |
| point+=2; | |
| printf("Point: %d\n", point); | |
| return 0; | |
| } | |
| int test_func() { | |
| srand(time(NULL)); | |
| uint32_t size1 = rand() % (1024 * 1024 * 10); | |
| uint32_t size2 = rand() % (1024 * 1024 * 10); | |
| uint8_t *num1 = (uint8_t *)malloc(size1); | |
| uint8_t *num2 = (uint8_t *)malloc(size2); | |
| for (uint32_t i = 0; i < size1; i++) { | |
| num1[i] = rand() % 256; | |
| } | |
| for (uint32_t i = 0; i < size2; i++) { | |
| num2[i] = rand() % 256; | |
| } | |
| uint8_t type1 = rand() % 2; | |
| uint8_t type2 = rand() % 2; | |
| int32_t result_ta = endian_equality_test_ta(num1, size1, type1, num2, size2, type2); | |
| int32_t result_student = endian_equality_test(num1, size1, type1, num2, size2, type2); | |
| free(num1); | |
| free(num2); | |
| return result_ta == result_student; | |
| } | |
| int test_func_equal(){ | |
| srand(time(NULL)); | |
| uint32_t size1 = rand() % (1024 * 1024 * 10); | |
| uint32_t size2 = size1; | |
| uint8_t *num1 = (uint8_t *)malloc(size1); | |
| uint8_t *num2 = (uint8_t *)malloc(size2); | |
| for (uint32_t i = 0; i < size1; i++) { | |
| num1[i] = rand() % 256; | |
| num2[i] = num1[i]; | |
| } | |
| uint8_t type1 = rand() % 2; | |
| uint8_t type2 = type1; | |
| int32_t result_ta = endian_equality_test_ta(num1, size1, type1, num2, size2, type2); | |
| int32_t result_student = endian_equality_test(num1, size1, type1, num2, size2, type2); | |
| free(num1); | |
| free(num2); | |
| return result_ta == result_student; | |
| } | |
| int err_test_null(){ | |
| uint8_t *pNum1 = NULL; | |
| uint8_t *pNum2 = NULL; | |
| uint32_t size1 = 1; | |
| uint32_t size2 = 1; | |
| uint8_t type1 = 0; | |
| uint8_t type2 = 0; | |
| int32_t result_ta = endian_equality_test_ta(pNum1, size1, type1, pNum2, size2, type2); | |
| int32_t result_student = endian_equality_test(pNum2, size1, type1, pNum2, size2, type2); | |
| return result_ta == result_student; | |
| } | |
| int err_test_size(){ | |
| uint8_t *pNum1 = (uint8_t *)malloc(1); | |
| uint8_t *pNum2 = (uint8_t *)malloc(1); | |
| uint32_t size1 = 0; | |
| uint32_t size2 = 1; | |
| uint8_t type1 = 0; | |
| uint8_t type2 = 0; | |
| int32_t result_ta = endian_equality_test_ta(pNum1, size1, type1, pNum2, size2, type2); | |
| int32_t result_student = endian_equality_test(pNum2, size1, type1, pNum2, size2, type2); | |
| return result_ta == result_student; | |
| } | |
| int err_test_type(){ | |
| uint8_t *pNum1 = (uint8_t *)malloc(1); | |
| uint8_t *pNum2 = (uint8_t *)malloc(1); | |
| uint32_t size1 = 1; | |
| uint32_t size2 = 1; | |
| uint8_t type1 = 2; | |
| uint8_t type2 = 0; | |
| int32_t result_ta = endian_equality_test_ta(pNum1, size1, type1, pNum2, size2, type2); | |
| int32_t result_student = endian_equality_test(pNum2, size1, type1, pNum2, size2, type2); | |
| return result_ta == result_student; | |
| } | |
| int32_t endian_convert_ta( uint8_t *pSrc, uint32_t size, uint8_t **ppDst ) { | |
| if (pSrc == NULL || ppDst == NULL) { | |
| return -1; | |
| } | |
| if (size == 0) { | |
| return -1; | |
| } | |
| *ppDst = (uint8_t *)malloc(size); | |
| if (*ppDst == NULL) { | |
| return -1; | |
| } | |
| for (uint32_t i = 0; i < size; i++) { | |
| (*ppDst)[i] = pSrc[size - i - 1]; | |
| } | |
| return 0; | |
| } | |
| int32_t endian_equality_test_ta( uint8_t *pNum1 , uint32_t size1 , uint8_t type1 , | |
| uint8_t *pNum2 , uint32_t size2 , uint8_t type2 ) { | |
| if (pNum1 == NULL || pNum2 == NULL) | |
| return -2; | |
| if (type1 != 0 && type1 != 1) | |
| return -2; | |
| if (type2 != 0 && type2 != 1) | |
| return -2; | |
| if (size1 == 0 || size2 == 0) | |
| return -2; | |
| uint8_t *pNum1Converted = NULL; | |
| uint8_t *pNum2Converted = NULL; | |
| if (type1 == 1) {//to big endian | |
| if (endian_convert_ta(pNum1, size1, &pNum1Converted) == -1) { | |
| return -2; | |
| } | |
| } else { | |
| pNum1Converted = pNum1; | |
| } | |
| if (type2 == 1) {//to big endian | |
| if (endian_convert_ta(pNum2, size2, &pNum2Converted) == -1) { | |
| return -2; | |
| } | |
| } else { | |
| pNum2Converted = pNum2; | |
| } | |
| uint32_t maxSize = size1 > size2 ? size1 : size2; | |
| uint8_t *pNum1Padded = (uint8_t *)calloc(maxSize, sizeof(uint8_t)); | |
| uint8_t *pNum2Padded = (uint8_t *)calloc(maxSize, sizeof(uint8_t)); | |
| memcpy(pNum1Padded + (maxSize - size1), pNum1Converted, size1); | |
| memcpy(pNum2Padded + (maxSize - size2), pNum2Converted, size2); | |
| for (int32_t i = 0; i < maxSize; i++) { | |
| if (pNum1Padded[i] < pNum2Padded[i]) { | |
| free(pNum1Padded); | |
| free(pNum2Padded); | |
| if (type1 == 1) free(pNum1Converted); | |
| if (type2 == 1) free(pNum2Converted); | |
| return -1; | |
| } else if (pNum1Padded[i] > pNum2Padded[i]) { | |
| free(pNum1Padded); | |
| free(pNum2Padded); | |
| if (type1 == 1) free(pNum1Converted); | |
| if (type2 == 1) free(pNum2Converted); | |
| return 1; | |
| } | |
| } | |
| free(pNum1Padded); | |
| free(pNum2Padded); | |
| if (type1 == 1) free(pNum1Converted); | |
| if (type2 == 1) free(pNum2Converted); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment