/[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.10 by sysadm, Thu May 22 11:10:19 2025 UTC Revision 1.17 by sysadm, Fri May 23 14:04:05 2025 UTC
# Line 21  Line 21 
21  #include <unistd.h>  #include <unistd.h>
22  #include <errno.h>  #include <errno.h>
23    
24    #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
25    #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
26    
27  const char *sname[] = {  const char *sname[] = {
28          "Test",          "Test",
29          "ABCDEFG",          "ABCDEFG",
# Line 43  int main(int argc, char *argv[]) Line 46  int main(int argc, char *argv[])
46  {  {
47          SECTION_LIST *p_section[BBS_max_section];          SECTION_LIST *p_section[BBS_max_section];
48          ARTICLE *p_article;          ARTICLE *p_article;
49            ARTICLE *p_next;
50          ARTICLE article;          ARTICLE article;
51          int block_count;          int block_count;
52          int i, j;          int i, j;
# Line 55  int main(int argc, char *argv[]) Line 59  int main(int argc, char *argv[])
59          int32_t page;          int32_t page;
60          int32_t offset;          int32_t offset;
61          int affected_count;          int affected_count;
62            FILE *fp;
63    
64          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
65          {          {
# Line 66  int main(int argc, char *argv[]) Line 71  int main(int argc, char *argv[])
71          log_err_redirect(STDERR_FILENO);          log_err_redirect(STDERR_FILENO);
72    
73          // - 1 to make blocks allocated is less than required, to trigger error handling          // - 1 to make blocks allocated is less than required, to trigger error handling
74          block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK - 1;          block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK;
75    
76            if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL)
77            {
78                    log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
79                    return -1;
80            }
81            fclose(fp);
82    
83            if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL)
84            {
85                    log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE);
86                    return -1;
87            }
88            fclose(fp);
89    
90            if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)
91            {
92                    log_error("section_data_pool_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
93                    return -2;
94            }
95    
96          if (article_block_init("../conf/menu.conf", block_count) < 0)          if (section_list_pool_init(SECTION_LIST_SHM_FILE) < 0)
97          {          {
98                  log_error("section_data_pool_init() error\n");                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
99                  return -2;                  return -2;
100          }          }
101    
# Line 80  int main(int argc, char *argv[]) Line 105  int main(int argc, char *argv[])
105    
106          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
107          {          {
108                  p_section[i] = section_list_create(sname[i % section_conf_count],                  p_section[i] = section_list_create(i + 1,
109                                                                                       sname[i % section_conf_count],
110                                                                                     stitle[i % section_conf_count],                                                                                     stitle[i % section_conf_count],
111                                                                                     master_name[i % section_conf_count]);                                                                                     master_name[i % section_conf_count]);
112                  if (p_section[i] == NULL)                  if (p_section[i] == NULL)
# Line 305  int main(int argc, char *argv[]) Line 331  int main(int argc, char *argv[])
331                          {                          {
332                                  printf("Count of articles in topic %d is different from expected %d != %d\n",                                  printf("Count of articles in topic %d is different from expected %d != %d\n",
333                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
334                                  break;                                  // break;
335                          }                          }
336                  }                  }
337    
# Line 346  int main(int argc, char *argv[]) Line 372  int main(int argc, char *argv[])
372    
373          printf("Testing #4 ...\n");          printf("Testing #4 ...\n");
374    
375          for (i = 0; i < BBS_max_section; i++)          for (i = 0; i < section_count; i++)
376          {          {
377                  step = i % 10 + 1;                  step = i % 10 + 1;
378                  for (j = group_count; j < BBS_article_limit_per_section; j += step)                  for (j = group_count; j < BBS_article_limit_per_section; j += step)
379                  {                  {
380                          last_aid = i * BBS_article_limit_per_section + j + 1;                          last_aid = i * BBS_article_limit_per_section + j + 1;
381    
382                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset);                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
383    
384                          if (p_article == NULL)                          if (p_article == NULL)
385                          {                          {
# Line 367  int main(int argc, char *argv[]) Line 393  int main(int argc, char *argv[])
393                                  break;                                  break;
394                          }                          }
395    
                         if (page * BBS_article_limit_per_page + offset != j)  
                         {  
                                 printf("Inconsistent article offset %d in section %d page %d offset %d\n", j, i, page, offset);  
                                 break;  
                         }  
   
396                          if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)                          if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
397                          {                          {
398                                  printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);                                  printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
# Line 380  int main(int argc, char *argv[]) Line 400  int main(int argc, char *argv[])
400                          }                          }
401                  }                  }
402    
403                    last_aid = p_section[i]->p_article_head->aid;
404                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
405                    {
406                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
407                            break;
408                    }
409    
410                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
411                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
412                            p_section[i]->page_count)
413                    {
414                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
415                                       "visible_article_count = %d, last_page_visible_count = %d\n",
416                                       i, j,
417                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
418                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
419                                       p_section[i]->page_count, p_section[i]->visible_article_count,
420                                       p_section[i]->last_page_visible_article_count);
421                            break;
422                    }
423    
424                  affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);                  affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
425                  if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)                  if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
426                  {                  {
# Line 395  int main(int argc, char *argv[]) Line 436  int main(int argc, char *argv[])
436                  {                  {
437                          last_aid = i * BBS_article_limit_per_section + j + 1;                          last_aid = i * BBS_article_limit_per_section + j + 1;
438    
439                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset);                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
440    
441                          if (p_article == NULL)                          if (p_article == NULL)
442                          {                          {
# Line 432  int main(int argc, char *argv[]) Line 473  int main(int argc, char *argv[])
473    
474                  if (p_section[i]->visible_article_count > 0)                  if (p_section[i]->visible_article_count > 0)
475                  {                  {
476                          printf("Inconsistent invisible count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);                          printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
477                            break;
478                    }
479    
480                    if (p_section[i]->visible_topic_count > 0)
481                    {
482                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
483                            break;
484                    }
485    
486                    last_aid = p_section[i]->p_article_head->aid;
487                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
488                    {
489                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
490                            break;
491                    }
492    
493                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
494                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
495                            p_section[i]->page_count)
496                    {
497                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
498                                       "visible_article_count = %d, last_page_visible_count = %d\n",
499                                       i, j,
500                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
501                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
502                                       p_section[i]->page_count, p_section[i]->visible_article_count,
503                                       p_section[i]->last_page_visible_article_count);
504                            break;
505                    }
506            }
507    
508            for (i = 0; i < BBS_max_section; i++)
509            {
510                    affected_count = 0;
511    
512                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
513                    {
514                            last_aid = i * BBS_article_limit_per_section + j + 1;
515    
516                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
517                            {
518                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
519                                    break;
520                            }
521    
522                            affected_count++;
523                    }
524    
525                    if (affected_count != p_section[i]->article_count)
526                    {
527                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
528                            break;
529                    }
530    
531                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
532                    {
533                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
534                            break;
535                    }
536    
537                    if (p_section[i]->visible_topic_count != group_count)
538                    {
539                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
540                            break;
541                    }
542    
543                    last_aid = p_section[i]->p_article_head->aid;
544                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
545                    {
546                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
547                            break;
548                    }
549    
550                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
551                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
552                            p_section[i]->page_count)
553                    {
554                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
555                                       "visible_article_count = %d, last_page_visible_count = %d\n",
556                                       i, j,
557                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
558                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
559                                       p_section[i]->page_count, p_section[i]->visible_article_count,
560                                       p_section[i]->last_page_visible_article_count);
561                            break;
562                    }
563            }
564    
565            printf("Testing #5 ...\n");
566    
567            if (article_block_reset() != 0)
568            {
569                    log_error("section_data_free_block(i=%d) error\n", i);
570                    return -4;
571            }
572    
573            for (i = 0; i < section_count; i++)
574            {
575                    section_list_reset_articles(p_section[i]);
576            }
577    
578            last_aid = 0;
579    
580            for (i = 0; i < section_count / 2; i++)
581            {
582                    section_first_aid = last_aid + 1;
583    
584                    for (j = 0; j < BBS_article_limit_per_section; j++)
585                    {
586                            last_aid++;
587    
588                            // Set article data
589                            article.aid = last_aid;
590                            article.cid = article.aid;
591                            // Group articles into group_count topics
592                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
593                            article.uid = 1; // TODO: randomize
594                            article.visible = 1;
595                            article.excerption = 0;
596                            article.ontop = 0;
597                            article.lock = 0;
598    
599                            if (section_list_append_article(p_section[i], &article) < 0)
600                            {
601                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
602                                    break;
603                            }
604                    }
605    
606                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
607            }
608    
609            for (i = 0; i < section_count / 2; i++)
610            {
611                    section_first_aid = p_section[i]->p_article_head->aid;
612    
613                    for (j = 0; j < group_count; j += 2)
614                    {
615                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
616                            if (p_article == NULL)
617                            {
618                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
619                                               section_first_aid + j, i);
620                                    break;
621                            }
622    
623                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
624                            {
625                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
626                            }
627                    }
628            }
629    
630            for (i = 0; i < section_count / 2; i++)
631            {
632                    section_first_aid = p_section[i]->p_article_head->aid;
633    
634                    for (j = 0; j < group_count; j++)
635                    {
636                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
637    
638                            if (affected_count < 0)
639                            {
640                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
641                                    break;
642                            }
643    
644                            if (affected_count != BBS_article_limit_per_section / group_count)
645                            {
646                                    printf("move topic (aid = %d) affected article count %d != %d\n",
647                                               section_first_aid + j, affected_count,
648                                               BBS_article_limit_per_section / group_count);
649                                    // break;
650                            }
651                    }
652            }
653    
654            for (i = 0; i < section_count; i++)
655            {
656                    if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
657                    {
658                            printf("Topic count error in section %d, %d != %d\n", i,
659                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
660                            break;
661                    }
662    
663                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
664                    {
665                            printf("Visible topic count error in section %d, %d != %d\n", i,
666                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
667                            break;
668                    }
669    
670                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
671                    {
672                            printf("Article count error in section %d, %d != %d\n", i,
673                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
674                            break;
675                    }
676    
677                    if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
678                    {
679                            printf("Visible article count error in section %d, %d != %d\n", i,
680                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
681                            break;
682                    }
683    
684                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
685                    {
686                            printf("Page count error in section %d, %d != %d\n", i,
687                                       p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
688                          break;                          break;
689                  }                  }
690          }          }
# Line 441  int main(int argc, char *argv[]) Line 693  int main(int argc, char *argv[])
693          getchar();          getchar();
694    
695          article_block_cleanup();          article_block_cleanup();
696            section_list_pool_cleanup();
697    
698            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
699            {
700                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
701                    return -1;
702            }
703    
704            if (unlink(SECTION_LIST_SHM_FILE) < 0)
705            {
706                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
707                    return -1;
708            }
709    
710          log_end();          log_end();
711    


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

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