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

Diff of /lbbs/src/test_section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.19 by sysadm, Sat May 24 07:32:46 2025 UTC Revision 1.25 by sysadm, Mon May 26 23:38:11 2025 UTC
# Line 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "section_list.h"  #include "section_list.h"
18    #include "trie_dict.h"
19  #include "bbs.h"  #include "bbs.h"
20  #include "log.h"  #include "log.h"
21  #include <stdio.h>  #include <stdio.h>
# Line 23  Line 24 
24    
25  #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"  #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
26  #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"  #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
27    #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
28    
29  const char *sname[] = {  const char *sname[] = {
30          "Test",          "Test",
# Line 88  int main(int argc, char *argv[]) Line 90  int main(int argc, char *argv[])
90          }          }
91          fclose(fp);          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)          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);                  log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
109                  return -2;                  return -2;
110          }          }
111    
112          if (section_list_pool_init(SECTION_LIST_SHM_FILE) < 0)          if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
113          {          {
114                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
115                  return -2;                  return -2;
# Line 104  int main(int argc, char *argv[]) Line 119  int main(int argc, char *argv[])
119    
120          last_aid = 0;          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++)          for (i = 0; i < section_count; i++)
128          {          {
129                  sid = i * 3 + 1;                  sid = i * 3 + 1;
# Line 116  int main(int argc, char *argv[]) Line 136  int main(int argc, char *argv[])
136                          printf("section_list_create(i = %d) error\n", i);                          printf("section_list_create(i = %d) error\n", i);
137                          return -3;                          return -3;
138                  }                  }
         }  
139    
         for (i = 0; i < section_count; i++)  
         {  
140                  if (get_section_index(p_section[i]) != i)                  if (get_section_index(p_section[i]) != i)
141                  {                  {
142                          printf("get_section_index(i = %d) error\n", i);                          printf("get_section_index(i = %d) error\n", i);
# Line 146  int main(int argc, char *argv[]) Line 163  int main(int argc, char *argv[])
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++)          for (j = 0; j < BBS_article_limit_per_section; j++)
172          {          {
173                  for (i = 0; i < section_count; i++)                  for (i = 0; i < section_count; i++)
# Line 163  int main(int argc, char *argv[]) Line 185  int main(int argc, char *argv[])
185                          article.ontop = 0;                          article.ontop = 0;
186                          article.lock = 0;                          article.lock = 0;
187    
188                            if (section_list_rw_lock(p_section[i]) < 0)
189                            {
190                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
191                                    break;
192                            }
193    
194                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
195                          {                          {
196                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
197                                  break;                                  break;
198                          }                          }
199    
200                            if (section_list_rw_unlock(p_section[i]) < 0)
201                            {
202                                    printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
203                                    break;
204                            }
205                  }                  }
206          }          }
207    
# Line 176  int main(int argc, char *argv[]) Line 210  int main(int argc, char *argv[])
210                  // printf("Loaded %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);
211          }          }
212    
213            if (last_aid != article_block_last_aid())
214            {
215                    printf("last_aid != %d\n", article_block_last_aid());
216            }
217    
218          last_aid = 0;          last_aid = 0;
219    
220          for (j = 0; j < BBS_article_limit_per_section; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
# Line 190  int main(int argc, char *argv[]) Line 229  int main(int argc, char *argv[])
229                                  printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);                                  printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
230                          }                          }
231    
232                            if (section_list_rw_lock(p_section[i]) < 0)
233                            {
234                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
235                                    break;
236                            }
237    
238                          if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)                          if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
239                          {                          {
240                                  printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);                                  printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
241                          }                          }
242    
243                            if (section_list_rw_unlock(p_section[i]) < 0)
244                            {
245                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
246                                    break;
247                            }
248                  }                  }
249    
250                  // printf("Verified %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);
# Line 201  int main(int argc, char *argv[]) Line 252  int main(int argc, char *argv[])
252    
253          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
254    
255            if (section_list_rw_lock(NULL) < 0)
256            {
257                    printf("section_list_rw_lock(sid = %d) error\n", 0);
258            }
259    
260          if (article_block_reset() != 0)          if (article_block_reset() != 0)
261          {          {
262                  log_error("article_block_reset() error\n");                  log_error("article_block_reset() error\n");
# Line 212  int main(int argc, char *argv[]) Line 268  int main(int argc, char *argv[])
268                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
269          }          }
270    
271            if (section_list_rw_unlock(NULL) < 0)
272            {
273                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
274            }
275    
276          last_aid = 0;          last_aid = 0;
277    
278          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
279          {          {
280                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
281    
282                    if (section_list_rw_lock(p_section[i]) < 0)
283                    {
284                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
285                            break;
286                    }
287    
288                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
289                  {                  {
290                          last_aid++;                          last_aid++;
# Line 241  int main(int argc, char *argv[]) Line 308  int main(int argc, char *argv[])
308                          }                          }
309                  }                  }
310    
311                    if (section_list_rw_unlock(p_section[i]) < 0)
312                    {
313                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
314                            break;
315                    }
316    
317                  // printf("Loaded %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);
318          }          }
319    
# Line 254  int main(int argc, char *argv[]) Line 327  int main(int argc, char *argv[])
327                  article_count = 0;                  article_count = 0;
328                  last_aid = 0;                  last_aid = 0;
329    
330                    if (section_list_rd_lock(p_section[i]) < 0)
331                    {
332                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
333                            break;
334                    }
335    
336                  p_article = p_section[i]->p_article_head;                  p_article = p_section[i]->p_article_head;
337    
338                  do                  do
# Line 276  int main(int argc, char *argv[]) Line 355  int main(int argc, char *argv[])
355                          break;                          break;
356                  }                  }
357    
358                    if (section_list_rd_unlock(p_section[i]) < 0)
359                    {
360                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
361                            break;
362                    }
363    
364                  // printf("Verified %d articles in section %d\n", group_count, i);                  // printf("Verified %d articles in section %d\n", group_count, i);
365          }          }
366    
# Line 286  int main(int argc, char *argv[]) Line 371  int main(int argc, char *argv[])
371                          continue;                          continue;
372                  }                  }
373    
374                    if (section_list_rd_lock(p_section[i]) < 0)
375                    {
376                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
377                            break;
378                    }
379    
380                  if (p_section[i]->topic_count != group_count)                  if (p_section[i]->topic_count != group_count)
381                  {                  {
382                          printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);                          printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
# Line 347  int main(int argc, char *argv[]) Line 438  int main(int argc, char *argv[])
438                          }                          }
439                  }                  }
440    
441                    if (section_list_rd_unlock(p_section[i]) < 0)
442                    {
443                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
444                            break;
445                    }
446    
447                  // printf("Verified %d topics in section %d\n", group_count, i);                  // printf("Verified %d topics in section %d\n", group_count, i);
448          }          }
449    
# Line 356  int main(int argc, char *argv[]) Line 453  int main(int argc, char *argv[])
453          {          {
454                  last_aid = 0;                  last_aid = 0;
455    
456                    if (section_list_rd_lock(p_section[i]) < 0)
457                    {
458                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
459                            break;
460                    }
461    
462                  for (j = 0; j < p_section[i]->page_count; j++)                  for (j = 0; j < p_section[i]->page_count; j++)
463                  {                  {
464                          if (p_section[i]->p_page_first_article[j]->aid <= last_aid)                          if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
# Line 380  int main(int argc, char *argv[]) Line 483  int main(int argc, char *argv[])
483                                  }                                  }
484                          }                          }
485                  }                  }
486    
487                    if (section_list_rd_unlock(p_section[i]) < 0)
488                    {
489                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
490                            break;
491                    }
492          }          }
493    
494          printf("Testing #4 ...\n");          printf("Testing #4 ...\n");
495    
496          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
497          {          {
498                    if (section_list_rw_lock(p_section[i]) < 0)
499                    {
500                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
501                            break;
502                    }
503    
504                  step = i % 10 + 1;                  step = i % 10 + 1;
505                  for (j = group_count; j < BBS_article_limit_per_section; j += step)                  for (j = group_count; j < BBS_article_limit_per_section; j += step)
506                  {                  {
# Line 515  int main(int argc, char *argv[]) Line 630  int main(int argc, char *argv[])
630                                     p_section[i]->last_page_visible_article_count);                                     p_section[i]->last_page_visible_article_count);
631                          break;                          break;
632                  }                  }
633    
634                    if (section_list_rw_unlock(p_section[i]) < 0)
635                    {
636                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
637                            break;
638                    }
639          }          }
640    
641          for (i = 0; i < BBS_max_section; i++)          for (i = 0; i < BBS_max_section; i++)
642          {          {
643                    if (section_list_rw_lock(p_section[i]) < 0)
644                    {
645                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
646                            break;
647                    }
648    
649                  affected_count = 0;                  affected_count = 0;
650    
651                  for (j = 0; j < BBS_article_limit_per_section; j += 1)                  for (j = 0; j < BBS_article_limit_per_section; j += 1)
# Line 572  int main(int argc, char *argv[]) Line 699  int main(int argc, char *argv[])
699                                     p_section[i]->last_page_visible_article_count);                                     p_section[i]->last_page_visible_article_count);
700                          break;                          break;
701                  }                  }
702    
703                    if (section_list_rw_unlock(p_section[i]) < 0)
704                    {
705                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
706                            break;
707                    }
708          }          }
709    
710          printf("Testing #5 ...\n");          printf("Testing #5 ...\n");
711    
712            if (section_list_rw_lock(NULL) < 0)
713            {
714                    printf("section_list_rw_lock(sid = %d) error\n", 0);
715            }
716    
717          if (article_block_reset() != 0)          if (article_block_reset() != 0)
718          {          {
719                  log_error("article_block_reset() error\n");                  log_error("article_block_reset() error\n");
# Line 587  int main(int argc, char *argv[]) Line 725  int main(int argc, char *argv[])
725                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
726          }          }
727    
728            if (section_list_rw_unlock(NULL) < 0)
729            {
730                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
731            }
732    
733          last_aid = 0;          last_aid = 0;
734    
735          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
736          {          {
737                    if (section_list_rw_lock(p_section[i]) < 0)
738                    {
739                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
740                            break;
741                    }
742    
743                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
744    
745                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
# Line 616  int main(int argc, char *argv[]) Line 765  int main(int argc, char *argv[])
765                          }                          }
766                  }                  }
767    
768                    if (section_list_rw_unlock(p_section[i]) < 0)
769                    {
770                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
771                            break;
772                    }
773    
774                  // printf("Loaded %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);
775          }          }
776    
777          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
778          {          {
779                    if (section_list_rw_lock(p_section[i]) < 0)
780                    {
781                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
782                            break;
783                    }
784    
785                  section_first_aid = p_section[i]->p_article_head->aid;                  section_first_aid = p_section[i]->p_article_head->aid;
786    
787                  for (j = 0; j < group_count; j += 2)                  for (j = 0; j < group_count; j += 2)
# Line 638  int main(int argc, char *argv[]) Line 799  int main(int argc, char *argv[])
799                                  printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);                                  printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
800                          }                          }
801                  }                  }
802    
803                    if (section_list_rw_unlock(p_section[i]) < 0)
804                    {
805                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
806                            break;
807                    }
808          }          }
809    
810          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
811          {          {
812                    if (section_list_rw_lock(p_section[i]) < 0)
813                    {
814                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
815                            break;
816                    }
817    
818                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
819                    {
820                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
821    
822                            if (section_list_rw_unlock(p_section[i]) < 0)
823                            {
824                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
825                            }
826                            break;
827                    }
828    
829                  section_first_aid = p_section[i]->p_article_head->aid;                  section_first_aid = p_section[i]->p_article_head->aid;
830    
831                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
# Line 662  int main(int argc, char *argv[]) Line 846  int main(int argc, char *argv[])
846                                  // break;                                  // break;
847                          }                          }
848                  }                  }
849    
850                    if (section_list_rw_unlock(p_section[i]) < 0)
851                    {
852                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
853                            break;
854                    }
855    
856                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
857                    {
858                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
859    
860                            if (section_list_rw_unlock(p_section[i]) < 0)
861                            {
862                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
863                            }
864                            break;
865                    }
866          }          }
867    
868          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
869          {          {
870                    if (section_list_rd_lock(p_section[i]) < 0)
871                    {
872                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
873                            break;
874                    }
875    
876                  if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))                  if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
877                  {                  {
878                          printf("Topic count error in section %d, %d != %d\n", i,                          printf("Topic count error in section %d, %d != %d\n", i,
# Line 700  int main(int argc, char *argv[]) Line 907  int main(int argc, char *argv[])
907                                     p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));                                     p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
908                          break;                          break;
909                  }                  }
910    
911                    if (section_list_rd_unlock(p_section[i]) < 0)
912                    {
913                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
914                            break;
915                    }
916            }
917    
918            printf("Testing #6 ...\n");
919    
920            for (i = 0; i < section_count; i++)
921            {
922                    if (section_list_rd_lock(p_section[i]) < 0)
923                    {
924                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
925                            break;
926                    }
927            }
928    
929            printf("Try rw_lock for 5 sec...\n");
930            if (section_list_try_rw_lock(NULL, 5) == 0)
931            {
932                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
933            }
934    
935            for (i = 0; i < section_count; i++)
936            {
937                    if (section_list_rd_unlock(p_section[i]) < 0)
938                    {
939                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
940                            break;
941                    }
942            }
943    
944            if (section_list_try_rw_lock(NULL, 5) < 0)
945            {
946                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
947            }
948    
949            for (i = 0; i < section_count; i++)
950            {
951                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
952                    {
953                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
954                            break;
955                    }
956            }
957    
958            if (section_list_rw_unlock(NULL) < 0)
959            {
960                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
961          }          }
962    
963          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
964          getchar();          getchar();
965    
966            section_list_cleanup();
967          article_block_cleanup();          article_block_cleanup();
968          section_list_pool_cleanup();          trie_dict_cleanup();
969    
970            if (unlink(TRIE_DICT_SHM_FILE) < 0)
971            {
972                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
973                    return -1;
974            }
975    
976          if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)          if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
977          {          {


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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