/[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.33 - (show annotations)
Mon Oct 13 02:23:27 2025 UTC (5 months ago) by sysadm
Branch: MAIN
Changes since 1.32: +2 -2 lines
Content type: text/x-csrc
Support locate at specific article while listing section articles
Remember last located article of each section, before logout

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

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