Skip to content

Commit

Permalink
p_arr
Browse files Browse the repository at this point in the history
  • Loading branch information
Chong Liu committed Jan 27, 2024
1 parent eaa69b5 commit 62363fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions F01_Common_Functions/inc/common_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author : Chong Liu
* @CreateDate : 2024-01-09 17:28:45
* @LastEditors : Chong Liu
* @LastEditTime : 2024-01-27 14:26:50
* @LastEditTime : 2024-01-27 14:27:13
* =================================================================================
* Copyright (c) 2024 by Chong Liu, All Rights Reserved.
* =================================================================================
Expand Down Expand Up @@ -49,8 +49,8 @@ int Array_Reverse(int *arr, int left_index, int right_index); /* Reverse array i
/**********************************************************************************/
int Array_Print(int *arr, int arr_size); /* Print array */
int Array_Print2D(int **arr, int arr_size, int *arr_col_sizes); /* Print 2D array */
int Array_Free(int **arrPtr); /* Free array */
int Array_Free2D(int ***arrPtr, int arr_size); /* Free 2D array */
int Array_Free(int **p_arr); /* Free array */
int Array_Free2D(int ***p_arr, int arr_size); /* Free 2D array */

/**********************************************************************************/
/* */
Expand Down
26 changes: 13 additions & 13 deletions F01_Common_Functions/src/common_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,34 @@ int Array_Print2D(int **arr, int arr_size, int *arr_col_sizes) {
/**
* @description: 释放数组
* =================================================================================
* @param {int} **arrPtr 返回数组
* @param {int} **p_arr 返回数组
* @return {int} 打印结果
*/
int Array_Free(int **arrPtr) {
if (*arrPtr != NULL) {
free(*arrPtr);
*arrPtr = NULL;
int Array_Free(int **p_arr) {
if (*p_arr != NULL) {
free(*p_arr);
*p_arr = NULL;
}
return COMMON_OK;
}

/**
* @description: 释放二维数组
* =================================================================================
* @param {int} ***arrPtr 返回数组
* @param {int} ***p_arr 返回数组
* @param {int} arr_size 返回数组大小
* @return {int} 打印结果
*/
int Array_Free2D(int ***arrPtr, int arr_size) {
if (*arrPtr != NULL) {
int Array_Free2D(int ***p_arr, int arr_size) {
if (*p_arr != NULL) {
for (int i = 0; i < arr_size; i++) {
if ((*arrPtr)[i] != NULL) {
free((*arrPtr)[i]);
(*arrPtr)[i] = NULL;
if ((*p_arr)[i] != NULL) {
free((*p_arr)[i]);
(*p_arr)[i] = NULL;
}
}
free(*arrPtr);
*arrPtr = NULL;
free(*p_arr);
*p_arr = NULL;
}
return COMMON_OK;
}
Expand Down
56 changes: 28 additions & 28 deletions F01_Common_Functions/test/common_array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,28 @@ TEST_F(TestCommonArray, TestSortArray) {
/* ============================================================================== */
TEST_F(TestCommonArray, TestSort2DArray) {
/* 准备数据 */
int **arrPtr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
int **p_arr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
arrPtr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
p_arr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
arrPtr[0][0] = 5;
arrPtr[0][1] = 4;
arrPtr[0][2] = 3;
arrPtr[1][0] = 2;
arrPtr[1][1] = 1;
arrPtr[1][2] = 0;
p_arr[0][0] = 5;
p_arr[0][1] = 4;
p_arr[0][2] = 3;
p_arr[1][0] = 2;
p_arr[1][1] = 1;
p_arr[1][2] = 0;
int arrColSize[2] = {3, 3};

/* 运行结果 */
int result = Array_Sort2D(arrPtr, 2, arrColSize);
int result = Array_Sort2D(p_arr, 2, arrColSize);

/* Validate result (验证函数调用结果) */
EXPECT_EQ(0, arrPtr[0][0]) << "Error occurred in Array_Sort2D arrPtr[0][0]";
EXPECT_EQ(1, arrPtr[0][1]) << "Error occurred in Array_Sort2D arrPtr[0][1]";
EXPECT_EQ(2, arrPtr[0][2]) << "Error occurred in Array_Sort2D arrPtr[0][2]";
EXPECT_EQ(3, arrPtr[1][0]) << "Error occurred in Array_Sort2D arrPtr[1][0]";
EXPECT_EQ(4, arrPtr[1][1]) << "Error occurred in Array_Sort2D arrPtr[1][1]";
EXPECT_EQ(5, arrPtr[1][2]) << "Error occurred in Array_Sort2D arrPtr[1][2]";
EXPECT_EQ(0, p_arr[0][0]) << "Error occurred in Array_Sort2D p_arr[0][0]";
EXPECT_EQ(1, p_arr[0][1]) << "Error occurred in Array_Sort2D p_arr[0][1]";
EXPECT_EQ(2, p_arr[0][2]) << "Error occurred in Array_Sort2D p_arr[0][2]";
EXPECT_EQ(3, p_arr[1][0]) << "Error occurred in Array_Sort2D p_arr[1][0]";
EXPECT_EQ(4, p_arr[1][1]) << "Error occurred in Array_Sort2D p_arr[1][1]";
EXPECT_EQ(5, p_arr[1][2]) << "Error occurred in Array_Sort2D p_arr[1][2]";
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Sort2D result";
}

Expand Down Expand Up @@ -159,21 +159,21 @@ TEST_F(TestCommonArray, TestPrintArray) {
/* ============================================================================== */
TEST_F(TestCommonArray, TestPrint2DArray) {
/* 准备数据 */
int **arrPtr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
int **p_arr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
arrPtr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
p_arr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
arrPtr[0][0] = 1;
arrPtr[0][1] = 2;
arrPtr[0][2] = 3;
arrPtr[1][0] = 4;
arrPtr[1][1] = 5;
arrPtr[1][2] = 6;
p_arr[0][0] = 1;
p_arr[0][1] = 2;
p_arr[0][2] = 3;
p_arr[1][0] = 4;
p_arr[1][1] = 5;
p_arr[1][2] = 6;
int arrColSize[2] = {3, 3};

/* 运行结果 */
printf("============================================================ Print Start.\n");
int result = Array_Print2D(arrPtr, 2, arrColSize);
int result = Array_Print2D(p_arr, 2, arrColSize);
printf("============================================================ Print End.\n");

/* 检查结果 */
Expand All @@ -200,16 +200,16 @@ TEST_F(TestCommonArray, TestFreeArray) {
/* ============================================================================== */
TEST_F(TestCommonArray, TestFree2DArray) {
/* 准备数据 */
int **arrPtr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
int **p_arr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
arrPtr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
p_arr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}

/* 运行结果 */
int result = Array_Free2D(&arrPtr, 2);
int result = Array_Free2D(&p_arr, 2);

/* Validate result (验证函数调用结果) */
EXPECT_EQ(nullptr, arrPtr) << "Error occurred in Array_Free2D";
EXPECT_EQ(nullptr, p_arr) << "Error occurred in Array_Free2D";
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Free2D result";
}

Expand Down

0 comments on commit 62363fb

Please sign in to comment.