-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon_array_test.cc
276 lines (244 loc) · 11.6 KB
/
common_array_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* @FilePath : \Practice_LeetCode_C\F01_Common_Functions\test\common_array_test.cc
* @Author : Chong Liu
* @CreateDate : 2023-12-01 02:17:51
* @LastEditors : Chong Liu
* @LastEditTime : 2024-01-24 00:37:11
* =================================================================================
* Copyright (c) 2023 by Chong Liu, All Rights Reserved.
* =================================================================================
* @Description : Common Array Unit Test (通用数组单元测试)
*/
extern "C" {
/* Target header file (目标头文件) */
#include "F01_Common_Functions/inc/common_array.h"
}
/* Google test header file (谷歌测试头文件) */
#include "gtest/gtest.h"
/**********************************************************************************/
/* */
/* TEST SUITE */
/* */
/**********************************************************************************/
class TestCommonArray : public ::testing::Test {
public:
static void SetUpTestCase() {
/* Code here will be called once before all tests */
}
void SetUp() override {
/* Code here will be called before each test */
}
void TearDown() override {
/* Code here will be called after each test */
}
static void TearDownTestCase() {
/* Code here will be called once after all tests */
}
};
/**********************************************************************************/
/* */
/* MACRO FUNCTIONS */
/* */
/**********************************************************************************/
/* ============================================================================== */
/* ARR_SIZE */
/* ============================================================================== */
TEST_F(TestCommonArray, TestArrSize) {
/* 准备数据 */
int arr[5] = {1, 2, 3, 4, 5};
/* 运行结果 */
int arr_size = ARR_SIZE(arr);
/* Validate result (验证函数调用结果) */
EXPECT_EQ(arr_size, 5);
}
/**********************************************************************************/
/* */
/* UTILITY FUNCTIONS */
/* */
/**********************************************************************************/
/* ============================================================================== */
/* Array_CmpElement */
/* ============================================================================== */
TEST_F(TestCommonArray, TestCompareElements) {
/* 准备数据 */
int a = 5;
int b = 3;
/* 运行结果 & 比较结果 */
EXPECT_EQ(2, Array_CmpElement(&a, &b)) << "Error occurred in Array_CmpElement a > b";
/* 准备数据 */
a = 3;
b = 5;
/* 运行结果 & 比较结果 */
EXPECT_EQ(-2, Array_CmpElement(&a, &b)) << "Error occurred in Array_CmpElement a < b";
/* 准备数据 */
a = 5;
b = 5;
/* 运行结果 & 比较结果 */
EXPECT_EQ(0, Array_CmpElement(&a, &b)) << "Error occurred in Array_CmpElement a = b";
}
/* ============================================================================== */
/* Array_Sort */
/* ============================================================================== */
TEST_F(TestCommonArray, TestSortArray) {
/* 准备数据 */
int arr[5] = {5, 4, 3, 2, 1};
/* 运行结果 */
int result = Array_Sort(arr, 5);
/* Validate result (验证函数调用结果) */
EXPECT_EQ(1, arr[0]) << "Error occurred in Array_Sort arr[0]";
EXPECT_EQ(2, arr[1]) << "Error occurred in Array_Sort arr[1]";
EXPECT_EQ(3, arr[2]) << "Error occurred in Array_Sort arr[2]";
EXPECT_EQ(4, arr[3]) << "Error occurred in Array_Sort arr[3]";
EXPECT_EQ(5, arr[4]) << "Error occurred in Array_Sort arr[4]";
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Sort result";
}
/* ============================================================================== */
/* Array_Sort2D */
/* ============================================================================== */
TEST_F(TestCommonArray, TestSort2DArray) {
/* 准备数据 */
int **p_arr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
p_arr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
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(p_arr, 2, arrColSize);
/* Validate result (验证函数调用结果) */
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";
}
/**********************************************************************************/
/* */
/* HELPER FUNCTIONS */
/* */
/**********************************************************************************/
/* ============================================================================== */
/* Array_Print */
/* ============================================================================== */
TEST_F(TestCommonArray, TestPrintArray) {
/* 准备数据 */
int arr[5] = {1, 2, 3, 4, 5};
/* 运行结果 */
printf("============================================================ Print Start.\n");
int result = Array_Print(arr, 5);
printf("============================================================ Print End.\n");
/* 检查结果 */
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Print";
}
/* ============================================================================== */
/* Array_Print2D */
/* ============================================================================== */
TEST_F(TestCommonArray, TestPrint2DArray) {
/* 准备数据 */
int **p_arr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
p_arr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
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(p_arr, 2, arrColSize);
printf("============================================================ Print End.\n");
/* 检查结果 */
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Print2D";
}
/* ============================================================================== */
/* Array_Free */
/* ============================================================================== */
TEST_F(TestCommonArray, TestFreeArray) {
/* 准备数据 */
int *arr = (int *)malloc(sizeof(int) * 5); /* NOLINT(readability/casting) */
/* 运行结果 */
int result = Array_Free(&arr);
/* Validate result (验证函数调用结果) */
EXPECT_EQ(nullptr, arr) << "Error occurred in Array_Free";
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Free result";
}
/* ============================================================================== */
/* Array_Free2D */
/* ============================================================================== */
TEST_F(TestCommonArray, TestFree2DArray) {
/* 准备数据 */
int **p_arr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
p_arr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
/* 运行结果 */
int result = Array_Free2D(&p_arr, 2);
/* Validate result (验证函数调用结果) */
EXPECT_EQ(nullptr, p_arr) << "Error occurred in Array_Free2D";
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Array_Free2D result";
}
/**********************************************************************************/
/* */
/* TEST_FUNCTION */
/* */
/**********************************************************************************/
/* ============================================================================== */
/* Validate_Array */
/* ============================================================================== */
TEST_F(TestCommonArray, TestValidateArray) {
/* 准备数据 */
int expectArr[5] = {1, 2, 3, 4, 5};
int actualArr[5] = {5, 4, 3, 2, 1};
/* 运行结果 */
Array_Sort(actualArr, 5);
printf("============================================================ Print Start.\n");
int result = Validate_Array('1', expectArr, 5, actualArr, 5, 1);
printf("============================================================ Print End.\n");
/* Validate result (验证函数调用结果) */
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Validate_Array";
}
/* ============================================================================== */
/* Validate_Array2D */
/* ============================================================================== */
TEST_F(TestCommonArray, TestValidate2DArray) {
/* 准备数据 */
int **expect2DArr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
expect2DArr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
expect2DArr[0][0] = 1;
expect2DArr[0][1] = 2;
expect2DArr[0][2] = 3;
expect2DArr[1][0] = 4;
expect2DArr[1][1] = 5;
expect2DArr[1][2] = 6;
int expectRtnColSize[2] = {3, 3};
int **actual2DArr = (int **)malloc(sizeof(int *) * 2); /* NOLINT(readability/casting) */
for (int i = 0; i < 2; i++) {
actual2DArr[i] = (int *)malloc(sizeof(int) * 3); /* NOLINT(readability/casting) */
}
actual2DArr[0][0] = 6;
actual2DArr[0][1] = 5;
actual2DArr[0][2] = 4;
actual2DArr[1][0] = 3;
actual2DArr[1][1] = 2;
actual2DArr[1][2] = 1;
int actualRtnColSize[2] = {3, 3};
/* 运行结果 */
Array_Sort2D(actual2DArr, 2, actualRtnColSize);
printf("============================================================ Print Start.\n");
int result = Validate_Array2D('1', expect2DArr, 2, expectRtnColSize, actual2DArr, 2, actualRtnColSize, 1);
printf("============================================================ Print End.\n");
/* Validate result (验证函数调用结果) */
EXPECT_EQ(COMMON_OK, result) << "Error occurred in Validate_Array2D";
}