/[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.27 by sysadm, Tue May 27 07:21:43 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 162  int main(int argc, char *argv[]) Line 184  int main(int argc, char *argv[])
184                          article.excerption = 0;                          article.excerption = 0;
185                          article.ontop = 0;                          article.ontop = 0;
186                          article.lock = 0;                          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)                          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);                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
198                                  break;                                  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    
# Line 176  int main(int argc, char *argv[]) Line 211  int main(int argc, char *argv[])
211                  // 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);
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;          last_aid = 0;
226    
227          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 236  int main(int argc, char *argv[])
236                                  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);
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)                          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);                                  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);                  // printf("Verified %d articles in section %d\n", p_section[i]->article_count, i);
# Line 201  int main(int argc, char *argv[]) Line 259  int main(int argc, char *argv[])
259    
260          printf("Testing #2 ...\n");          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)          if (article_block_reset() != 0)
268          {          {
269                  log_error("article_block_reset() error\n");                  log_error("article_block_reset() error\n");
# Line 212  int main(int argc, char *argv[]) Line 275  int main(int argc, char *argv[])
275                  section_list_reset_articles(p_section[i]);                  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;          last_aid = 0;
284    
285          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
286          {          {
287                  section_first_aid = last_aid + 1;                  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++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
296                  {                  {
297                          last_aid++;                          last_aid++;
# Line 233  int main(int argc, char *argv[]) Line 307  int main(int argc, char *argv[])
307                          article.excerption = 0;                          article.excerption = 0;
308                          article.ontop = 0;                          article.ontop = 0;
309                          article.lock = 0;                          article.lock = 0;
310                            article.transship = 0;
311    
312                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
313                          {                          {
# Line 241  int main(int argc, char *argv[]) Line 316  int main(int argc, char *argv[])
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);                  // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
326          }          }
327    
# Line 254  int main(int argc, char *argv[]) Line 335  int main(int argc, char *argv[])
335                  article_count = 0;                  article_count = 0;
336                  last_aid = 0;                  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;                  p_article = p_section[i]->p_article_head;
345    
346                  do                  do
# Line 276  int main(int argc, char *argv[]) Line 363  int main(int argc, char *argv[])
363                          break;                          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);                  // printf("Verified %d articles in section %d\n", group_count, i);
373          }          }
374    
# Line 286  int main(int argc, char *argv[]) Line 379  int main(int argc, char *argv[])
379                          continue;                          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)                  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);                          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 446  int main(int argc, char *argv[])
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);                  // printf("Verified %d topics in section %d\n", group_count, i);
456          }          }
457    
# Line 356  int main(int argc, char *argv[]) Line 461  int main(int argc, char *argv[])
461          {          {
462                  last_aid = 0;                  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++)                  for (j = 0; j < p_section[i]->page_count; j++)
471                  {                  {
472                          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 491  int main(int argc, char *argv[])
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");          printf("Testing #4 ...\n");
503    
504          for (i = 0; i < section_count; i++)          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;                  step = i % 10 + 1;
513                  for (j = group_count; j < BBS_article_limit_per_section; j += step)                  for (j = group_count; j < BBS_article_limit_per_section; j += step)
514                  {                  {
# Line 515  int main(int argc, char *argv[]) Line 638  int main(int argc, char *argv[])
638                                     p_section[i]->last_page_visible_article_count);                                     p_section[i]->last_page_visible_article_count);
639                          break;                          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++)          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;                  affected_count = 0;
658    
659                  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 707  int main(int argc, char *argv[])
707                                     p_section[i]->last_page_visible_article_count);                                     p_section[i]->last_page_visible_article_count);
708                          break;                          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");          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)          if (article_block_reset() != 0)
726          {          {
727                  log_error("article_block_reset() error\n");                  log_error("article_block_reset() error\n");
# Line 587  int main(int argc, char *argv[]) Line 733  int main(int argc, char *argv[])
733                  section_list_reset_articles(p_section[i]);                  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;          last_aid = 0;
742    
743          for (i = 0; i < section_count / 2; i++)          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;                  section_first_aid = last_aid + 1;
752    
753                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
# Line 608  int main(int argc, char *argv[]) Line 765  int main(int argc, char *argv[])
765                          article.excerption = 0;                          article.excerption = 0;
766                          article.ontop = 0;                          article.ontop = 0;
767                          article.lock = 0;                          article.lock = 0;
768                            article.transship = 0;
769    
770                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
771                          {                          {
# Line 616  int main(int argc, char *argv[]) Line 774  int main(int argc, char *argv[])
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);                  // 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++)          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;                  section_first_aid = p_section[i]->p_article_head->aid;
795    
796                  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 808  int main(int argc, char *argv[])
808                                  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);
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++)          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;                  section_first_aid = p_section[i]->p_article_head->aid;
839    
840                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
# Line 662  int main(int argc, char *argv[]) Line 855  int main(int argc, char *argv[])
855                                  // break;                                  // 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++)          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))                  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,                          printf("Topic count error in section %d, %d != %d\n", i,
# Line 700  int main(int argc, char *argv[]) Line 916  int main(int argc, char *argv[])
916                                     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));
917                          break;                          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...");          printf("Press ENTER to exit...");
973          getchar();          getchar();
974    
975            section_list_cleanup();
976          article_block_cleanup();          article_block_cleanup();
977          section_list_pool_cleanup();          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)          if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
986          {          {


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

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