| 41 |
|
|
| 42 |
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
| 43 |
{ |
{ |
| 44 |
SECTION_DATA *p_section[BBS_max_section]; |
SECTION_LIST *p_section[BBS_max_section]; |
| 45 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 46 |
ARTICLE article; |
ARTICLE article; |
| 47 |
|
int block_count; |
| 48 |
int i, j; |
int i, j; |
| 49 |
int last_aid; |
int last_aid; |
| 50 |
int group_count; |
int current_tid; |
| 51 |
|
int section_first_aid; |
| 52 |
|
int group_count = 100; |
| 53 |
|
int article_count; |
| 54 |
|
int step; |
| 55 |
|
int32_t page; |
| 56 |
|
int32_t offset; |
| 57 |
|
int affected_count; |
| 58 |
|
|
| 59 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
| 60 |
{ |
{ |
| 65 |
log_std_redirect(STDOUT_FILENO); |
log_std_redirect(STDOUT_FILENO); |
| 66 |
log_err_redirect(STDERR_FILENO); |
log_err_redirect(STDERR_FILENO); |
| 67 |
|
|
| 68 |
if (section_data_pool_init("../conf/menu.conf", BBS_article_block_limit_per_section * BBS_max_section) < 0) |
// - 1 to make blocks allocated is less than required, to trigger error handling |
| 69 |
|
block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK - 1; |
| 70 |
|
|
| 71 |
|
if (article_block_init("../conf/menu.conf", block_count) < 0) |
| 72 |
{ |
{ |
| 73 |
log_error("section_data_pool_init() error\n"); |
log_error("section_data_pool_init() error\n"); |
| 74 |
return -2; |
return -2; |
| 80 |
|
|
| 81 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 82 |
{ |
{ |
| 83 |
p_section[i] = section_data_create(sname[i % section_conf_count], |
p_section[i] = section_list_create(sname[i % section_conf_count], |
| 84 |
stitle[i % section_conf_count], |
stitle[i % section_conf_count], |
| 85 |
master_name[i % section_conf_count]); |
master_name[i % section_conf_count]); |
| 86 |
if (p_section[i] == NULL) |
if (p_section[i] == NULL) |
| 88 |
log_error("section_data_create(i=%d) error\n", i); |
log_error("section_data_create(i=%d) error\n", i); |
| 89 |
return -3; |
return -3; |
| 90 |
} |
} |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
for (i = 0; i < section_conf_count; i++) |
| 94 |
|
{ |
| 95 |
|
if (section_list_find_by_name(sname[i]) == NULL) |
| 96 |
|
{ |
| 97 |
|
printf("section_data_find_section_by_name(%s) error\n", sname[i]); |
| 98 |
|
return -3; |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
|
| 102 |
for (j = 0; j < BBS_article_limit_per_block * BBS_article_block_limit_per_section; j++) |
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 103 |
|
{ |
| 104 |
|
for (i = 0; i < section_count; i++) |
| 105 |
{ |
{ |
| 106 |
last_aid++; |
last_aid++; |
| 107 |
|
|
| 115 |
article.ontop = 0; |
article.ontop = 0; |
| 116 |
article.lock = 0; |
article.lock = 0; |
| 117 |
|
|
| 118 |
section_data_append_article(p_section[i], &article); |
if (section_list_append_article(p_section[i], &article) < 0) |
| 119 |
|
{ |
| 120 |
|
printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j); |
| 121 |
|
break; |
| 122 |
|
} |
| 123 |
} |
} |
| 124 |
|
} |
| 125 |
|
|
| 126 |
printf("Load %d articles into section %d\n", p_section[i]->article_count, i); |
for (i = 0; i < section_count; i++) |
| 127 |
|
{ |
| 128 |
|
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
| 129 |
} |
} |
| 130 |
|
|
| 131 |
last_aid = 0; |
last_aid = 0; |
| 132 |
|
|
| 133 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 134 |
{ |
{ |
| 135 |
|
if (p_section[i]->article_count == 0) |
| 136 |
|
{ |
| 137 |
|
continue; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
for (j = 0; j < p_section[i]->article_count; j++) |
for (j = 0; j < p_section[i]->article_count; j++) |
| 141 |
{ |
{ |
| 142 |
last_aid++; |
last_aid++; |
| 143 |
|
|
| 144 |
p_article = section_data_find_article_by_index(p_section[i], j); |
p_article = article_block_find_by_aid(last_aid); |
| 145 |
|
if (p_article == NULL || p_article->aid != last_aid) |
| 146 |
|
{ |
| 147 |
|
printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
p_article = article_block_find_by_index(last_aid - 1); |
| 151 |
if (p_article == NULL || p_article->aid != last_aid) |
if (p_article == NULL || p_article->aid != last_aid) |
| 152 |
{ |
{ |
| 153 |
printf("Inconsistent aid at index %d != %d\n", j, last_aid); |
printf("article_block_find_by_index() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1) |
| 157 |
|
{ |
| 158 |
|
printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid); |
| 159 |
} |
} |
| 160 |
} |
} |
| 161 |
|
|
| 162 |
printf("Verify %d articles in section %d\n", p_section[i]->article_count, i); |
// printf("Verified %d articles in section %d\n", p_section[i]->article_count, i); |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
printf("Testing #2 ...\n"); |
printf("Testing #2 ...\n"); |
| 166 |
|
|
| 167 |
group_count = 100; |
if (article_block_reset() != 0) |
| 168 |
|
{ |
| 169 |
|
log_error("section_data_free_block(i=%d) error\n", i); |
| 170 |
|
return -4; |
| 171 |
|
} |
| 172 |
|
|
| 173 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 174 |
{ |
{ |
| 175 |
if (section_data_free_block(p_section[i]) != 0) |
section_list_reset_articles(p_section[i]); |
| 176 |
{ |
} |
|
log_error("section_data_free_block(i=%d) error\n", i); |
|
|
return -4; |
|
|
} |
|
| 177 |
|
|
| 178 |
last_aid = 0; |
last_aid = 0; |
| 179 |
|
|
| 180 |
for (j = 0; j < BBS_article_limit_per_block * BBS_article_block_limit_per_section; j++) |
for (i = 0; i < section_count; i++) |
| 181 |
|
{ |
| 182 |
|
section_first_aid = last_aid + 1; |
| 183 |
|
|
| 184 |
|
for (j = 0; j < BBS_article_limit_per_section; j++) |
| 185 |
{ |
{ |
| 186 |
last_aid++; |
last_aid++; |
| 187 |
|
|
| 188 |
// Set article data |
// Set article data |
| 189 |
article.aid = last_aid; |
article.aid = last_aid; |
| 190 |
article.cid = article.aid; |
article.cid = article.aid; |
| 191 |
article.tid = (article.aid <= group_count ? 0 : (article.aid - 1) % group_count + 1); // Group articles into group_count topics |
// Group articles into group_count topics |
| 192 |
article.uid = 1; // TODO: randomize |
article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count)); |
| 193 |
|
article.uid = 1; // TODO: randomize |
| 194 |
article.visible = 1; |
article.visible = 1; |
| 195 |
article.excerption = 0; |
article.excerption = 0; |
| 196 |
article.ontop = 0; |
article.ontop = 0; |
| 197 |
article.lock = 0; |
article.lock = 0; |
| 198 |
|
|
| 199 |
section_data_append_article(p_section[i], &article); |
if (section_list_append_article(p_section[i], &article) < 0) |
| 200 |
|
{ |
| 201 |
|
printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j); |
| 202 |
|
break; |
| 203 |
|
} |
| 204 |
} |
} |
| 205 |
|
|
| 206 |
printf("Load %d articles into section %d\n", p_section[i]->article_count, i); |
// printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i); |
| 207 |
} |
} |
| 208 |
|
|
| 209 |
for (i = 0; i < section_count; i++) |
for (i = 0; i < section_count; i++) |
| 210 |
{ |
{ |
| 211 |
|
if (p_section[i]->article_count == 0) |
| 212 |
|
{ |
| 213 |
|
continue; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
article_count = 0; |
| 217 |
|
last_aid = 0; |
| 218 |
|
|
| 219 |
|
p_article = p_section[i]->p_article_head; |
| 220 |
|
|
| 221 |
|
do |
| 222 |
|
{ |
| 223 |
|
article_count++; |
| 224 |
|
|
| 225 |
|
if (p_article->aid <= last_aid) |
| 226 |
|
{ |
| 227 |
|
printf("Non-ascending aid found %d <= %d at article\n", p_article->aid, last_aid); |
| 228 |
|
} |
| 229 |
|
last_aid = p_article->aid; |
| 230 |
|
|
| 231 |
|
p_article = p_article->p_next; |
| 232 |
|
} while (p_article != p_section[i]->p_article_head); |
| 233 |
|
|
| 234 |
|
if (article_count != p_section[i]->article_count) |
| 235 |
|
{ |
| 236 |
|
printf("Count of articles in section %d is different from expected %d != %d\n", |
| 237 |
|
i, article_count, p_section[i]->article_count); |
| 238 |
|
break; |
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
// printf("Verified %d articles in section %d\n", group_count, i); |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
for (i = 0; i < section_count; i++) |
| 245 |
|
{ |
| 246 |
|
if (p_section[i]->article_count == 0) |
| 247 |
|
{ |
| 248 |
|
continue; |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
if (p_section[i]->topic_count != group_count) |
| 252 |
|
{ |
| 253 |
|
printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count); |
| 254 |
|
} |
| 255 |
|
|
| 256 |
for (j = 0; j < group_count; j++) |
for (j = 0; j < group_count; j++) |
| 257 |
{ |
{ |
| 258 |
p_article = section_data_find_article_by_index(p_section[i], j); |
last_aid = p_section[i]->p_article_head->aid + j; |
| 259 |
|
|
| 260 |
|
p_article = article_block_find_by_aid(last_aid); |
| 261 |
if (p_article == NULL) |
if (p_article == NULL) |
| 262 |
{ |
{ |
| 263 |
printf("NULL p_article at index %d\n", j); |
printf("NULL p_article at section %d index %d\n", i, j); |
| 264 |
break; |
break; |
| 265 |
} |
} |
| 266 |
if (p_article->aid != j + 1) |
if (p_article->aid != last_aid) |
| 267 |
{ |
{ |
| 268 |
printf("Inconsistent aid at index %d != %d\n", j, j + 1); |
printf("Inconsistent aid at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid); |
| 269 |
break; |
break; |
| 270 |
} |
} |
| 271 |
|
|
| 272 |
|
article_count = 1; |
| 273 |
|
last_aid = 0; |
| 274 |
|
current_tid = p_article->aid; |
| 275 |
|
|
| 276 |
do |
do |
| 277 |
{ |
{ |
| 278 |
if (p_article->next_aid <= p_article->aid && p_article->next_aid != p_article->tid) |
if (p_article->aid <= last_aid) |
| 279 |
{ |
{ |
| 280 |
printf("Non-ascending aid found %d >= %d\n", p_article->aid, p_article->next_aid); |
printf("Non-ascending aid found %d <= %d\n", p_article->aid, last_aid); |
|
break; |
|
| 281 |
} |
} |
| 282 |
|
last_aid = p_article->aid; |
| 283 |
|
|
| 284 |
|
p_article = p_article->p_topic_next; |
| 285 |
|
|
|
last_aid = p_article->next_aid; |
|
|
p_article = section_data_find_article_by_aid(p_section[i], last_aid); |
|
| 286 |
if (p_article == NULL) |
if (p_article == NULL) |
| 287 |
{ |
{ |
| 288 |
printf("NULL p_article at aid %d\n", last_aid); |
printf("NULL p_article found\n"); |
| 289 |
break; |
break; |
| 290 |
} |
} |
| 291 |
if (p_article->tid == 0) // loop |
if (p_article->tid == 0) // loop |
| 292 |
{ |
{ |
| 293 |
break; |
break; |
| 294 |
} |
} |
| 295 |
if (p_article->tid != j + 1) |
if (p_article->tid != current_tid) |
| 296 |
{ |
{ |
| 297 |
printf("Inconsistent tid at aid %d != %d\n", last_aid, j + 1); |
printf("Inconsistent tid %d != %d\n", p_article->tid, current_tid); |
| 298 |
break; |
break; |
| 299 |
} |
} |
| 300 |
|
|
| 301 |
|
article_count++; |
| 302 |
} while (1); |
} while (1); |
| 303 |
|
|
| 304 |
|
if (article_count != p_section[i]->article_count / group_count) |
| 305 |
|
{ |
| 306 |
|
printf("Count of articles in topic %d is different from expected %d != %d\n", |
| 307 |
|
j + 1, article_count, p_section[i]->article_count / group_count); |
| 308 |
|
break; |
| 309 |
|
} |
| 310 |
|
} |
| 311 |
|
|
| 312 |
|
// printf("Verified %d topics in section %d\n", group_count, i); |
| 313 |
|
} |
| 314 |
|
|
| 315 |
|
printf("Testing #3 ...\n"); |
| 316 |
|
|
| 317 |
|
for (i = 0; i < section_count; i++) |
| 318 |
|
{ |
| 319 |
|
last_aid = 0; |
| 320 |
|
|
| 321 |
|
for (j = 0; j < p_section[i]->page_count; j++) |
| 322 |
|
{ |
| 323 |
|
if (p_section[i]->p_page_first_article[j]->aid <= last_aid) |
| 324 |
|
{ |
| 325 |
|
printf("Non-ascending aid found at section %d page %d, %d <= %d\n", i, j, p_section[i]->p_page_first_article[j]->aid, last_aid); |
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
if (j > 0) |
| 329 |
|
{ |
| 330 |
|
step = 0; |
| 331 |
|
|
| 332 |
|
for (p_article = p_section[i]->p_page_first_article[j]; |
| 333 |
|
p_article != p_section[i]->p_page_first_article[j - 1]; |
| 334 |
|
p_article = p_article->p_prior) |
| 335 |
|
{ |
| 336 |
|
step++; |
| 337 |
|
} |
| 338 |
|
|
| 339 |
|
if (step != BBS_article_limit_per_page) |
| 340 |
|
{ |
| 341 |
|
printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1); |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
} |
} |
| 345 |
|
} |
| 346 |
|
|
| 347 |
|
printf("Testing #4 ...\n"); |
| 348 |
|
|
| 349 |
|
for (i = 0; i < BBS_max_section; i++) |
| 350 |
|
{ |
| 351 |
|
step = i % 10 + 1; |
| 352 |
|
for (j = group_count; j < BBS_article_limit_per_section; j += step) |
| 353 |
|
{ |
| 354 |
|
last_aid = i * BBS_article_limit_per_section + j + 1; |
| 355 |
|
|
| 356 |
|
p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset); |
| 357 |
|
|
| 358 |
|
if (p_article == NULL) |
| 359 |
|
{ |
| 360 |
|
printf("Error find article %d in section %d offset %d\n", last_aid, i, j); |
| 361 |
|
break; |
| 362 |
|
} |
| 363 |
|
|
| 364 |
|
if (p_article->aid != last_aid) |
| 365 |
|
{ |
| 366 |
|
printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid); |
| 367 |
|
break; |
| 368 |
|
} |
| 369 |
|
|
| 370 |
|
if (page * BBS_article_limit_per_page + offset != j) |
| 371 |
|
{ |
| 372 |
|
printf("Inconsistent article offset %d in section %d page %d offset %d\n", j, i, page, offset); |
| 373 |
|
break; |
| 374 |
|
} |
| 375 |
|
|
| 376 |
printf("Verify %d topics in section %d\n", group_count, i); |
if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1) |
| 377 |
|
{ |
| 378 |
|
printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j); |
| 379 |
|
break; |
| 380 |
|
} |
| 381 |
|
} |
| 382 |
|
|
| 383 |
|
affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0); |
| 384 |
|
if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count) |
| 385 |
|
{ |
| 386 |
|
printf("Inconsistent set invisible count in section %d, %d != %d\n", i, |
| 387 |
|
p_section[i]->article_count - p_section[i]->visible_article_count, |
| 388 |
|
affected_count); |
| 389 |
|
break; |
| 390 |
|
} |
| 391 |
|
|
| 392 |
|
article_count = p_section[i]->visible_article_count; |
| 393 |
|
|
| 394 |
|
for (j = 0; j < group_count; j += 1) |
| 395 |
|
{ |
| 396 |
|
last_aid = i * BBS_article_limit_per_section + j + 1; |
| 397 |
|
|
| 398 |
|
p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset); |
| 399 |
|
|
| 400 |
|
if (p_article == NULL) |
| 401 |
|
{ |
| 402 |
|
printf("Error find article %d in section %d offset %d\n", last_aid, i, j); |
| 403 |
|
break; |
| 404 |
|
} |
| 405 |
|
|
| 406 |
|
if (p_article->aid != last_aid) |
| 407 |
|
{ |
| 408 |
|
printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid); |
| 409 |
|
break; |
| 410 |
|
} |
| 411 |
|
|
| 412 |
|
if (p_article->tid != 0) |
| 413 |
|
{ |
| 414 |
|
printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid); |
| 415 |
|
break; |
| 416 |
|
} |
| 417 |
|
|
| 418 |
|
if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0) |
| 419 |
|
{ |
| 420 |
|
printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j); |
| 421 |
|
break; |
| 422 |
|
} |
| 423 |
|
|
| 424 |
|
article_count -= affected_count; |
| 425 |
|
} |
| 426 |
|
|
| 427 |
|
if (article_count != 0) |
| 428 |
|
{ |
| 429 |
|
printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count); |
| 430 |
|
break; |
| 431 |
|
} |
| 432 |
|
|
| 433 |
|
if (p_section[i]->visible_article_count > 0) |
| 434 |
|
{ |
| 435 |
|
printf("Inconsistent invisible count in section %d, %d > 0\n", i, p_section[i]->visible_article_count); |
| 436 |
|
break; |
| 437 |
|
} |
| 438 |
} |
} |
| 439 |
|
|
| 440 |
section_data_pool_cleanup(); |
printf("Press ENTER to exit..."); |
| 441 |
|
getchar(); |
| 442 |
|
|
| 443 |
|
article_block_cleanup(); |
| 444 |
|
|
| 445 |
log_end(); |
log_end(); |
| 446 |
|
|