/[LeafOK_CVS]/lbbs/src/test_section_list.c
ViewVC logotype

Contents of /lbbs/src/test_section_list.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (show annotations)
Tue Nov 4 14:58:56 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.37: +1 -1 lines
Content type: text/x-csrc
Refine file header information comments

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * test_section_list
4 * - tester for data models and basic operations of section and article
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #include "bbs.h"
10 #include "log.h"
11 #include "section_list.h"
12 #include "trie_dict.h"
13 #include "user_list.h"
14 #include <errno.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
19 #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
20 #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
21 #define USER_LIST_SHM_FILE "~user_list_shm.dat"
22
23 const char *sname[] = {
24 "Test",
25 "ABCDEFG",
26 "_1234_"};
27
28 const char *stitle[] = {
29 " Test Section ",
30 "字母组合ABC",
31 "_数字_123"};
32
33 const char *master_name[] = {
34 "sysadm",
35 "SYSOP",
36 ""};
37
38 int section_conf_count = 3;
39 int section_count = BBS_max_section;
40
41 int main(int argc, char *argv[])
42 {
43 SECTION_LIST *p_section[BBS_max_section];
44 ARTICLE *p_article;
45 ARTICLE *p_next;
46 ARTICLE article;
47 int block_count;
48 int i, j;
49 int sid;
50 int last_aid;
51 int current_tid;
52 int section_first_aid;
53 int group_count = 100;
54 int article_count;
55 int step;
56 int32_t page;
57 int32_t offset;
58 int affected_count;
59 FILE *fp;
60
61 if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
62 {
63 printf("Open log error\n");
64 return -1;
65 }
66
67 log_common_redir(STDOUT_FILENO);
68 log_error_redir(STDERR_FILENO);
69
70 // - 1 to make blocks allocated is less than required, to trigger error handling
71 block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK;
72
73 if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL)
74 {
75 log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
76 return -1;
77 }
78 fclose(fp);
79
80 if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL)
81 {
82 log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE);
83 return -1;
84 }
85 fclose(fp);
86
87 if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
88 {
89 log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
90 return -1;
91 }
92 fclose(fp);
93
94 if ((fp = fopen(USER_LIST_SHM_FILE, "w")) == NULL)
95 {
96 log_error("fopen(%s) error\n", USER_LIST_SHM_FILE);
97 return -1;
98 }
99 fclose(fp);
100
101 if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
102 {
103 printf("trie_dict_init failed\n");
104 return -1;
105 }
106
107 if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)
108 {
109 log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
110 return -2;
111 }
112
113 if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
114 {
115 log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
116 return -2;
117 }
118
119 // Load user_list and online_user_list
120 if (user_list_pool_init(USER_LIST_SHM_FILE) < 0)
121 {
122 log_error("user_list_pool_init() error\n");
123 return -2;
124 }
125
126 printf("Testing #1 ...\n");
127
128 last_aid = 0;
129
130 if (section_list_rw_lock(NULL) < 0)
131 {
132 printf("section_list_rw_lock(sid = %d) error\n", 0);
133 }
134
135 for (i = 0; i < section_count; i++)
136 {
137 sid = i * 3 + 1;
138 p_section[i] = section_list_create(sid,
139 sname[i % section_conf_count],
140 stitle[i % section_conf_count],
141 master_name[i % section_conf_count]);
142 if (p_section[i] == NULL)
143 {
144 printf("section_list_create(i = %d) error\n", i);
145 return -3;
146 }
147
148 if (get_section_index(p_section[i]) != i)
149 {
150 printf("get_section_index(i = %d) error\n", i);
151 return -3;
152 }
153 }
154
155 for (i = 0; i < section_conf_count; i++)
156 {
157 if (section_list_find_by_name(sname[i]) == NULL)
158 {
159 printf("section_list_find_by_name(%s) error\n", sname[i]);
160 return -3;
161 }
162 }
163
164 for (i = 0; i < section_count; i++)
165 {
166 sid = i * 3 + 1;
167 if (section_list_find_by_sid(sid) == NULL || section_list_find_by_sid(sid)->sid != sid)
168 {
169 printf("section_list_find_by_sid(%d) error\n", sid);
170 return -3;
171 }
172 }
173
174 if (section_list_rw_unlock(NULL) < 0)
175 {
176 printf("section_list_rw_unlock(sid = %d) error\n", 0);
177 }
178
179 for (j = 0; j < BBS_article_limit_per_section; j++)
180 {
181 for (i = 0; i < section_count; i++)
182 {
183 last_aid++;
184
185 // Set article data
186 article.aid = last_aid;
187 article.tid = 0;
188 article.sid = i * 3 + 1;
189 article.cid = article.aid;
190 article.uid = 1;
191 article.visible = 1;
192 article.excerption = 0;
193 article.ontop = 0;
194 article.lock = 0;
195 article.transship = 0;
196
197 if (section_list_rw_lock(p_section[i]) < 0)
198 {
199 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
200 break;
201 }
202
203 if (section_list_append_article(p_section[i], &article) < 0)
204 {
205 printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
206 break;
207 }
208
209 if (section_list_rw_unlock(p_section[i]) < 0)
210 {
211 printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
212 break;
213 }
214 }
215 }
216
217 for (i = 0; i < section_count; i++)
218 {
219 // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
220 }
221
222 if (last_aid != article_block_last_aid())
223 {
224 printf("last_aid != %d\n", article_block_last_aid());
225 }
226
227 if (article_block_article_count() != section_count * BBS_article_limit_per_section)
228 {
229 printf("article_block_article_count() error %d != %d * %d\n",
230 article_block_article_count(), section_count, BBS_article_limit_per_section);
231 }
232
233 last_aid = 0;
234
235 for (j = 0; j < BBS_article_limit_per_section; j++)
236 {
237 for (i = 0; i < section_count; i++)
238 {
239 last_aid++;
240
241 p_article = article_block_find_by_aid(last_aid);
242 if (p_article == NULL || p_article->aid != last_aid)
243 {
244 printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
245 }
246
247 if (section_list_rw_lock(p_section[i]) < 0)
248 {
249 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
250 break;
251 }
252
253 if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
254 {
255 printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
256 }
257
258 if (section_list_rw_unlock(p_section[i]) < 0)
259 {
260 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
261 break;
262 }
263 }
264
265 // printf("Verified %d articles in section %d\n", p_section[i]->article_count, i);
266 }
267
268 printf("Testing #2 ...\n");
269
270 if (section_list_rw_lock(NULL) < 0)
271 {
272 printf("section_list_rw_lock(sid = %d) error\n", 0);
273 }
274
275 if (article_block_reset() != 0)
276 {
277 log_error("article_block_reset() error\n");
278 return -4;
279 }
280
281 for (i = 0; i < section_count; i++)
282 {
283 section_list_reset_articles(p_section[i]);
284 }
285
286 if (section_list_rw_unlock(NULL) < 0)
287 {
288 printf("section_list_rw_unlock(sid = %d) error\n", 0);
289 }
290
291 last_aid = 0;
292
293 for (i = 0; i < section_count; i++)
294 {
295 section_first_aid = last_aid + 1;
296
297 if (section_list_rw_lock(p_section[i]) < 0)
298 {
299 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
300 break;
301 }
302
303 for (j = 0; j < BBS_article_limit_per_section; j++)
304 {
305 last_aid++;
306
307 // Set article data
308 article.aid = last_aid;
309 // Group articles into group_count topics
310 article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
311 article.sid = i * 3 + 1;
312 article.cid = article.aid;
313 article.uid = 1;
314 article.visible = 1;
315 article.excerption = 0;
316 article.ontop = 0;
317 article.lock = 0;
318 article.transship = 0;
319
320 if (section_list_append_article(p_section[i], &article) < 0)
321 {
322 printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
323 break;
324 }
325 }
326
327 if (section_list_rw_unlock(p_section[i]) < 0)
328 {
329 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
330 break;
331 }
332
333 // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
334 }
335
336 for (i = 0; i < section_count; i++)
337 {
338 if (p_section[i]->article_count == 0)
339 {
340 continue;
341 }
342
343 article_count = 0;
344 last_aid = 0;
345
346 if (section_list_rd_lock(p_section[i]) < 0)
347 {
348 printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
349 break;
350 }
351
352 p_article = p_section[i]->p_article_head;
353
354 do
355 {
356 article_count++;
357
358 if (p_article->aid <= last_aid)
359 {
360 printf("Non-ascending aid found %d <= %d at article\n", p_article->aid, last_aid);
361 }
362 last_aid = p_article->aid;
363
364 p_article = p_article->p_next;
365 } while (p_article != p_section[i]->p_article_head);
366
367 if (article_count != p_section[i]->article_count)
368 {
369 printf("Count of articles in section %d is different from expected %d != %d\n",
370 i, article_count, p_section[i]->article_count);
371 break;
372 }
373
374 if (section_list_rd_unlock(p_section[i]) < 0)
375 {
376 printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
377 break;
378 }
379
380 // printf("Verified %d articles in section %d\n", group_count, i);
381 }
382
383 for (i = 0; i < section_count; i++)
384 {
385 if (p_section[i]->article_count == 0)
386 {
387 continue;
388 }
389
390 if (section_list_rd_lock(p_section[i]) < 0)
391 {
392 printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
393 break;
394 }
395
396 if (p_section[i]->topic_count != group_count)
397 {
398 printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
399 }
400
401 for (j = 0; j < group_count; j++)
402 {
403 last_aid = p_section[i]->p_article_head->aid + j;
404
405 p_article = article_block_find_by_aid(last_aid);
406 if (p_article == NULL)
407 {
408 printf("NULL p_article at section %d index %d\n", i, j);
409 break;
410 }
411 if (p_article->aid != last_aid)
412 {
413 printf("Inconsistent aid at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
414 break;
415 }
416
417 article_count = 1;
418 last_aid = 0;
419 current_tid = p_article->aid;
420
421 do
422 {
423 if (p_article->aid <= last_aid)
424 {
425 printf("Non-ascending aid found %d <= %d\n", p_article->aid, last_aid);
426 }
427 last_aid = p_article->aid;
428
429 p_article = p_article->p_topic_next;
430
431 if (p_article == NULL)
432 {
433 printf("NULL p_article found\n");
434 break;
435 }
436 if (p_article->tid == 0) // loop
437 {
438 break;
439 }
440 if (p_article->tid != current_tid)
441 {
442 printf("Inconsistent tid %d != %d\n", p_article->tid, current_tid);
443 break;
444 }
445
446 article_count++;
447 } while (1);
448
449 if (article_count != p_section[i]->article_count / group_count)
450 {
451 printf("Count of articles in topic %d is different from expected %d != %d\n",
452 j + 1, article_count, p_section[i]->article_count / group_count);
453 // break;
454 }
455 }
456
457 if (section_list_rd_unlock(p_section[i]) < 0)
458 {
459 printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
460 break;
461 }
462
463 // printf("Verified %d topics in section %d\n", group_count, i);
464 }
465
466 printf("Testing #3 ...\n");
467
468 for (i = 0; i < section_count; i++)
469 {
470 last_aid = 0;
471
472 if (section_list_rd_lock(p_section[i]) < 0)
473 {
474 printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
475 break;
476 }
477
478 for (j = 0; j < p_section[i]->page_count; j++)
479 {
480 if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
481 {
482 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);
483 }
484
485 if (j > 0)
486 {
487 step = 0;
488
489 for (p_article = p_section[i]->p_page_first_article[j];
490 p_article != p_section[i]->p_page_first_article[j - 1];
491 p_article = p_article->p_prior)
492 {
493 step++;
494 }
495
496 if (step != BBS_article_limit_per_page)
497 {
498 printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
499 }
500 }
501 }
502
503 if (section_list_rd_unlock(p_section[i]) < 0)
504 {
505 printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
506 break;
507 }
508 }
509
510 printf("Testing #4 ...\n");
511
512 for (i = 0; i < section_count; i++)
513 {
514 if (section_list_rw_lock(p_section[i]) < 0)
515 {
516 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
517 break;
518 }
519
520 step = i % 10 + 1;
521 for (j = group_count; j < BBS_article_limit_per_section; j += step)
522 {
523 last_aid = i * BBS_article_limit_per_section + j + 1;
524
525 p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
526
527 if (p_article == NULL)
528 {
529 printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
530 break;
531 }
532
533 if (p_article->aid != last_aid)
534 {
535 printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
536 break;
537 }
538
539 if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
540 {
541 printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
542 break;
543 }
544 }
545
546 last_aid = p_section[i]->p_article_head->aid;
547 if (section_list_calculate_page(p_section[i], last_aid) < 0)
548 {
549 printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
550 break;
551 }
552
553 if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
554 (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
555 p_section[i]->page_count)
556 {
557 printf("#1 Inconsistent page count in section %d offset %d, %d != %d, "
558 "visible_article_count = %d, last_page_visible_count = %d\n",
559 i, j,
560 p_section[i]->visible_article_count / BBS_article_limit_per_page +
561 (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
562 p_section[i]->page_count, p_section[i]->visible_article_count,
563 p_section[i]->last_page_visible_article_count);
564 break;
565 }
566
567 affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
568 if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
569 {
570 printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
571 p_section[i]->article_count - p_section[i]->visible_article_count,
572 affected_count);
573 break;
574 }
575
576 article_count = p_section[i]->visible_article_count;
577
578 for (j = 0; j < group_count; j += 1)
579 {
580 last_aid = i * BBS_article_limit_per_section + j + 1;
581
582 p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
583
584 if (p_article == NULL)
585 {
586 printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
587 break;
588 }
589
590 if (p_article->aid != last_aid)
591 {
592 printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
593 break;
594 }
595
596 if (p_article->tid != 0)
597 {
598 printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
599 break;
600 }
601
602 if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
603 {
604 printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
605 break;
606 }
607
608 article_count -= affected_count;
609 }
610
611 if (article_count != 0)
612 {
613 printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
614 break;
615 }
616
617 if (p_section[i]->visible_article_count > 0)
618 {
619 printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
620 break;
621 }
622
623 if (p_section[i]->visible_topic_count > 0)
624 {
625 printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
626 break;
627 }
628
629 last_aid = p_section[i]->p_article_head->aid;
630 if (section_list_calculate_page(p_section[i], last_aid) < 0)
631 {
632 printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
633 break;
634 }
635
636 if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
637 (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
638 p_section[i]->page_count)
639 {
640 printf("#2 Inconsistent page count in section %d offset %d, %d != %d, "
641 "visible_article_count = %d, last_page_visible_count = %d\n",
642 i, j,
643 p_section[i]->visible_article_count / BBS_article_limit_per_page +
644 (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
645 p_section[i]->page_count, p_section[i]->visible_article_count,
646 p_section[i]->last_page_visible_article_count);
647 break;
648 }
649
650 if (section_list_rw_unlock(p_section[i]) < 0)
651 {
652 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
653 break;
654 }
655 }
656
657 for (i = 0; i < BBS_max_section; i++)
658 {
659 if (section_list_rw_lock(p_section[i]) < 0)
660 {
661 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
662 break;
663 }
664
665 affected_count = 0;
666
667 for (j = 0; j < BBS_article_limit_per_section; j += 1)
668 {
669 last_aid = i * BBS_article_limit_per_section + j + 1;
670
671 if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
672 {
673 printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
674 break;
675 }
676
677 affected_count++;
678 }
679
680 if (affected_count != p_section[i]->article_count)
681 {
682 printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
683 break;
684 }
685
686 if (p_section[i]->visible_article_count != p_section[i]->article_count)
687 {
688 printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
689 break;
690 }
691
692 if (p_section[i]->visible_topic_count != group_count)
693 {
694 printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
695 break;
696 }
697
698 last_aid = p_section[i]->p_article_head->aid;
699 if (section_list_calculate_page(p_section[i], last_aid) < 0)
700 {
701 printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
702 break;
703 }
704
705 if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
706 (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
707 p_section[i]->page_count)
708 {
709 printf("#3 Inconsistent page count in section %d offset %d, %d != %d, "
710 "visible_article_count = %d, last_page_visible_count = %d\n",
711 i, j,
712 p_section[i]->visible_article_count / BBS_article_limit_per_page +
713 (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
714 p_section[i]->page_count, p_section[i]->visible_article_count,
715 p_section[i]->last_page_visible_article_count);
716 break;
717 }
718
719 if (section_list_rw_unlock(p_section[i]) < 0)
720 {
721 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
722 break;
723 }
724 }
725
726 printf("Testing #5 ...\n");
727
728 if (section_list_rw_lock(NULL) < 0)
729 {
730 printf("section_list_rw_lock(sid = %d) error\n", 0);
731 }
732
733 if (article_block_reset() != 0)
734 {
735 log_error("article_block_reset() error\n");
736 return -4;
737 }
738
739 for (i = 0; i < section_count; i++)
740 {
741 section_list_reset_articles(p_section[i]);
742 }
743
744 if (section_list_rw_unlock(NULL) < 0)
745 {
746 printf("section_list_rw_unlock(sid = %d) error\n", 0);
747 }
748
749 last_aid = 0;
750
751 for (i = 0; i < section_count / 2; i++)
752 {
753 if (section_list_rw_lock(p_section[i]) < 0)
754 {
755 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
756 break;
757 }
758
759 section_first_aid = last_aid + 1;
760
761 for (j = 0; j < BBS_article_limit_per_section; j++)
762 {
763 last_aid++;
764
765 // Set article data
766 article.aid = last_aid;
767 // Group articles into group_count topics
768 article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
769 article.sid = i * 3 + 1;
770 article.cid = article.aid;
771 article.uid = 1;
772 article.visible = 1;
773 article.excerption = 0;
774 article.ontop = 0;
775 article.lock = 0;
776 article.transship = 0;
777
778 if (section_list_append_article(p_section[i], &article) < 0)
779 {
780 printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
781 break;
782 }
783 }
784
785 if (section_list_rw_unlock(p_section[i]) < 0)
786 {
787 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
788 break;
789 }
790
791 // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
792 }
793
794 for (i = 0; i < section_count / 2; i++)
795 {
796 if (section_list_rw_lock(p_section[i]) < 0)
797 {
798 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
799 break;
800 }
801
802 section_first_aid = p_section[i]->p_article_head->aid;
803
804 for (j = 0; j < group_count; j += 2)
805 {
806 p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
807 if (p_article == NULL)
808 {
809 printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
810 section_first_aid + j, i);
811 break;
812 }
813
814 if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
815 {
816 printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
817 }
818 }
819
820 if (section_list_rw_unlock(p_section[i]) < 0)
821 {
822 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
823 break;
824 }
825 }
826
827 for (i = 0; i < section_count / 2; i++)
828 {
829 if (section_list_rw_lock(p_section[i]) < 0)
830 {
831 printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
832 break;
833 }
834
835 if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
836 {
837 printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
838
839 if (section_list_rw_unlock(p_section[i]) < 0)
840 {
841 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
842 }
843 break;
844 }
845
846 section_first_aid = p_section[i]->p_article_head->aid;
847
848 for (j = 0; j < group_count; j++)
849 {
850 affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
851
852 if (affected_count < 0)
853 {
854 printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
855 break;
856 }
857
858 if (affected_count != BBS_article_limit_per_section / group_count)
859 {
860 printf("move topic (aid = %d) affected article count %d != %d\n",
861 section_first_aid + j, affected_count,
862 BBS_article_limit_per_section / group_count);
863 // break;
864 }
865 }
866
867 if (section_list_rw_unlock(p_section[i]) < 0)
868 {
869 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
870 break;
871 }
872
873 if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
874 {
875 printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
876
877 if (section_list_rw_unlock(p_section[i]) < 0)
878 {
879 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
880 }
881 break;
882 }
883 }
884
885 for (i = 0; i < section_count; i++)
886 {
887 if (section_list_rd_lock(p_section[i]) < 0)
888 {
889 printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
890 break;
891 }
892
893 if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
894 {
895 printf("Topic count error in section %d, %d != %d\n", i,
896 p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
897 break;
898 }
899
900 if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
901 {
902 printf("Visible topic count error in section %d, %d != %d\n", i,
903 p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
904 break;
905 }
906
907 if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
908 {
909 printf("Article count error in section %d, %d != %d\n", i,
910 p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
911 break;
912 }
913
914 if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
915 {
916 printf("Visible article count error in section %d, %d != %d\n", i,
917 p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
918 break;
919 }
920
921 if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
922 {
923 printf("Page count error in section %d, %d != %d\n", i,
924 p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
925 break;
926 }
927
928 if (section_list_rd_unlock(p_section[i]) < 0)
929 {
930 printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
931 break;
932 }
933 }
934
935 printf("Testing #6 ...\n");
936
937 for (i = 0; i < section_count; i++)
938 {
939 if (section_list_rd_lock(p_section[i]) < 0)
940 {
941 printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
942 break;
943 }
944 }
945
946 printf("Try rw_lock for 5 sec...\n");
947 if (section_list_try_rw_lock(NULL, 5) == 0)
948 {
949 printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
950 }
951
952 for (i = 0; i < section_count; i++)
953 {
954 if (section_list_rd_unlock(p_section[i]) < 0)
955 {
956 printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
957 break;
958 }
959 }
960
961 if (section_list_try_rw_lock(NULL, 5) < 0)
962 {
963 printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
964 }
965
966 for (i = 0; i < section_count; i++)
967 {
968 if (section_list_try_rd_lock(p_section[i], 0) == 0)
969 {
970 printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
971 break;
972 }
973 }
974
975 if (section_list_rw_unlock(NULL) < 0)
976 {
977 printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
978 }
979
980 printf("Press ENTER to exit...");
981 getchar();
982
983 user_list_pool_cleanup();
984 section_list_cleanup();
985 article_block_cleanup();
986 trie_dict_cleanup();
987
988 if (unlink(USER_LIST_SHM_FILE) < 0)
989 {
990 log_error("unlink(%s) error\n", USER_LIST_SHM_FILE);
991 return -1;
992 }
993
994 if (unlink(TRIE_DICT_SHM_FILE) < 0)
995 {
996 log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
997 return -1;
998 }
999
1000 if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
1001 {
1002 log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
1003 return -1;
1004 }
1005
1006 if (unlink(SECTION_LIST_SHM_FILE) < 0)
1007 {
1008 log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
1009 return -1;
1010 }
1011
1012 log_end();
1013
1014 return 0;
1015 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1