/[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.1 by sysadm, Wed May 21 04:07:42 2025 UTC Revision 1.14 by sysadm, Fri May 23 07:06:57 2025 UTC
# Line 24  Line 24 
24  const char *sname[] = {  const char *sname[] = {
25          "Test",          "Test",
26          "ABCDEFG",          "ABCDEFG",
27          "_1234_"          "_1234_"};
 };  
28    
29  const char *stitle[] = {  const char *stitle[] = {
30          " Test Section ",          " Test Section ",
31          "字母组合ABC",          "字母组合ABC",
32          "_数字_123"          "_数字_123"};
 };  
33    
34  const char *master_name[] = {  const char *master_name[] = {
35          "sysadm",          "sysadm",
36          "SYSOP",          "SYSOP",
37          ""          ""};
 };  
38    
39  int section_count = 3;  int section_conf_count = 3;
40    int section_count = BBS_max_section;
41    
42  int main(int argc, char *argv[])  int main(int argc, char *argv[])
43  {  {
44          SECTION_DATA *p_section[BBS_max_section];          SECTION_LIST *p_section[BBS_max_section];
45            ARTICLE *p_article;
46            ARTICLE *p_next;
47          ARTICLE article;          ARTICLE article;
48            int block_count;
49          int i, j;          int i, j;
50          int last_aid;          int last_aid;
51            int current_tid;
52            int section_first_aid;
53            int group_count = 100;
54            int article_count;
55            int step;
56            int32_t page;
57            int32_t offset;
58            int affected_count;
59    
60          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
61          {          {
# Line 57  int main(int argc, char *argv[]) Line 66  int main(int argc, char *argv[])
66          log_std_redirect(STDOUT_FILENO);          log_std_redirect(STDOUT_FILENO);
67          log_err_redirect(STDERR_FILENO);          log_err_redirect(STDERR_FILENO);
68    
69          if (section_data_pool_init("../conf/menu.conf", BBS_article_block_limit_per_section * BBS_max_section) < 0)          // - 1 to make blocks allocated is less than required, to trigger error handling
70            block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK;
71    
72            if (article_block_init("../conf/menu.conf", block_count) < 0)
73          {          {
74                  log_error("section_data_pool_init() error\n");                  log_error("section_data_pool_init() error\n");
75                  return -2;                  return -2;
76          }          }
77    
78            printf("Testing #1 ...\n");
79    
80          last_aid = 0;          last_aid = 0;
81    
82          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
83          {          {
84                  if ((p_section[i] = section_data_create(sname[i], stitle[i], master_name[i])) == NULL)                  p_section[i] = section_list_create(i + 1,
85                                                                                       sname[i % section_conf_count],
86                                                                                       stitle[i % section_conf_count],
87                                                                                       master_name[i % section_conf_count]);
88                    if (p_section[i] == NULL)
89                  {                  {
90                          log_error("section_data_create(i=%d) error\n", i);                          log_error("section_data_create(i=%d) error\n", i);
91                          return -3;                          return -3;
92                  }                  }
93            }
94    
95                  for (j = 0; j < BBS_article_limit_per_block * BBS_article_block_limit_per_section; j++)          for (i = 0; i < section_conf_count; i++)
96            {
97                    if (section_list_find_by_name(sname[i]) == NULL)
98                    {
99                            printf("section_data_find_section_by_name(%s) error\n", sname[i]);
100                            return -3;
101                    }
102            }
103    
104            for (j = 0; j < BBS_article_limit_per_section; j++)
105            {
106                    for (i = 0; i < section_count; i++)
107                  {                  {
108                          last_aid++;                          last_aid++;
109    
# Line 87  int main(int argc, char *argv[]) Line 117  int main(int argc, char *argv[])
117                          article.ontop = 0;                          article.ontop = 0;
118                          article.lock = 0;                          article.lock = 0;
119    
120                          section_data_append_article(p_section[i], &article);                          if (section_list_append_article(p_section[i], &article) < 0)
121                            {
122                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
123                                    break;
124                            }
125                    }
126            }
127    
128            for (i = 0; i < section_count; i++)
129            {
130                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
131            }
132    
133            last_aid = 0;
134    
135            for (i = 0; i < section_count; i++)
136            {
137                    if (p_section[i]->article_count == 0)
138                    {
139                            continue;
140                    }
141    
142                    for (j = 0; j < p_section[i]->article_count; j++)
143                    {
144                            last_aid++;
145    
146                            p_article = article_block_find_by_aid(last_aid);
147                            if (p_article == NULL || p_article->aid != last_aid)
148                            {
149                                    printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
150                            }
151    
152                            p_article = article_block_find_by_index(last_aid - 1);
153                            if (p_article == NULL || p_article->aid != last_aid)
154                            {
155                                    printf("article_block_find_by_index() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
156                            }
157    
158                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
159                            {
160                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
161                            }
162                    }
163    
164                    // printf("Verified %d articles in section %d\n", p_section[i]->article_count, i);
165            }
166    
167            printf("Testing #2 ...\n");
168    
169            if (article_block_reset() != 0)
170            {
171                    log_error("section_data_free_block(i=%d) error\n", i);
172                    return -4;
173            }
174    
175            for (i = 0; i < section_count; i++)
176            {
177                    section_list_reset_articles(p_section[i]);
178            }
179    
180            last_aid = 0;
181    
182            for (i = 0; i < section_count; i++)
183            {
184                    section_first_aid = last_aid + 1;
185    
186                    for (j = 0; j < BBS_article_limit_per_section; j++)
187                    {
188                            last_aid++;
189    
190                            // Set article data
191                            article.aid = last_aid;
192                            article.cid = article.aid;
193                            // Group articles into group_count topics
194                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
195                            article.uid = 1; // TODO: randomize
196                            article.visible = 1;
197                            article.excerption = 0;
198                            article.ontop = 0;
199                            article.lock = 0;
200    
201                            if (section_list_append_article(p_section[i], &article) < 0)
202                            {
203                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
204                                    break;
205                            }
206                    }
207    
208                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
209            }
210    
211            for (i = 0; i < section_count; i++)
212            {
213                    if (p_section[i]->article_count == 0)
214                    {
215                            continue;
216                    }
217    
218                    article_count = 0;
219                    last_aid = 0;
220    
221                    p_article = p_section[i]->p_article_head;
222    
223                    do
224                    {
225                            article_count++;
226    
227                            if (p_article->aid <= last_aid)
228                            {
229                                    printf("Non-ascending aid found %d <= %d at article\n", p_article->aid, last_aid);
230                            }
231                            last_aid = p_article->aid;
232    
233                            p_article = p_article->p_next;
234                    } while (p_article != p_section[i]->p_article_head);
235    
236                    if (article_count != p_section[i]->article_count)
237                    {
238                            printf("Count of articles in section %d is different from expected %d != %d\n",
239                                       i, article_count, p_section[i]->article_count);
240                            break;
241                    }
242    
243                    // printf("Verified %d articles in section %d\n", group_count, i);
244            }
245    
246            for (i = 0; i < section_count; i++)
247            {
248                    if (p_section[i]->article_count == 0)
249                    {
250                            continue;
251                    }
252    
253                    if (p_section[i]->topic_count != group_count)
254                    {
255                            printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
256                    }
257    
258                    for (j = 0; j < group_count; j++)
259                    {
260                            last_aid = p_section[i]->p_article_head->aid + j;
261    
262                            p_article = article_block_find_by_aid(last_aid);
263                            if (p_article == NULL)
264                            {
265                                    printf("NULL p_article at section %d index %d\n", i, j);
266                                    break;
267                            }
268                            if (p_article->aid != last_aid)
269                            {
270                                    printf("Inconsistent aid at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
271                                    break;
272                            }
273    
274                            article_count = 1;
275                            last_aid = 0;
276                            current_tid = p_article->aid;
277    
278                            do
279                            {
280                                    if (p_article->aid <= last_aid)
281                                    {
282                                            printf("Non-ascending aid found %d <= %d\n", p_article->aid, last_aid);
283                                    }
284                                    last_aid = p_article->aid;
285    
286                                    p_article = p_article->p_topic_next;
287    
288                                    if (p_article == NULL)
289                                    {
290                                            printf("NULL p_article found\n");
291                                            break;
292                                    }
293                                    if (p_article->tid == 0) // loop
294                                    {
295                                            break;
296                                    }
297                                    if (p_article->tid != current_tid)
298                                    {
299                                            printf("Inconsistent tid %d != %d\n", p_article->tid, current_tid);
300                                            break;
301                                    }
302    
303                                    article_count++;
304                            } while (1);
305    
306                            if (article_count != p_section[i]->article_count / group_count)
307                            {
308                                    printf("Count of articles in topic %d is different from expected %d != %d\n",
309                                               j + 1, article_count, p_section[i]->article_count / group_count);
310                                    break;
311                            }
312                    }
313    
314                    // printf("Verified %d topics in section %d\n", group_count, i);
315            }
316    
317            printf("Testing #3 ...\n");
318    
319            for (i = 0; i < section_count; i++)
320            {
321                    last_aid = 0;
322    
323                    for (j = 0; j < p_section[i]->page_count; j++)
324                    {
325                            if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
326                            {
327                                    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);
328                            }
329    
330                            if (j > 0)
331                            {
332                                    step = 0;
333    
334                                    for (p_article = p_section[i]->p_page_first_article[j];
335                                             p_article != p_section[i]->p_page_first_article[j - 1];
336                                             p_article = p_article->p_prior)
337                                    {
338                                            step++;
339                                    }
340    
341                                    if (step != BBS_article_limit_per_page)
342                                    {
343                                            printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
344                                    }
345                            }
346                    }
347            }
348    
349            printf("Testing #4 ...\n");
350    
351            for (i = 0; i < section_count; i++)
352            {
353                    step = i % 10 + 1;
354                    for (j = group_count; j < BBS_article_limit_per_section; j += step)
355                    {
356                            last_aid = i * BBS_article_limit_per_section + j + 1;
357    
358                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
359    
360                            if (p_article == NULL)
361                            {
362                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
363                                    break;
364                            }
365    
366                            if (p_article->aid != last_aid)
367                            {
368                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
369                                    break;
370                            }
371    
372                            if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
373                            {
374                                    printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
375                                    break;
376                            }
377                    }
378    
379                    last_aid = p_section[i]->p_article_head->aid;
380                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
381                    {
382                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
383                            break;
384                    }
385    
386                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
387                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
388                            p_section[i]->page_count)
389                    {
390                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
391                                       "visible_article_count = %d, last_page_visible_count = %d\n",
392                                       i, j,
393                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
394                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
395                                       p_section[i]->page_count, p_section[i]->visible_article_count,
396                                       p_section[i]->last_page_visible_article_count);
397                            break;
398                    }
399    
400                    affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
401                    if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
402                    {
403                            printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
404                                       p_section[i]->article_count - p_section[i]->visible_article_count,
405                                       affected_count);
406                            break;
407                    }
408    
409                    article_count = p_section[i]->visible_article_count;
410    
411                    for (j = 0; j < group_count; j += 1)
412                    {
413                            last_aid = i * BBS_article_limit_per_section + j + 1;
414    
415                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
416    
417                            if (p_article == NULL)
418                            {
419                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
420                                    break;
421                            }
422    
423                            if (p_article->aid != last_aid)
424                            {
425                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
426                                    break;
427                            }
428    
429                            if (p_article->tid != 0)
430                            {
431                                    printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
432                                    break;
433                            }
434    
435                            if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
436                            {
437                                    printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
438                                    break;
439                            }
440    
441                            article_count -= affected_count;
442                    }
443    
444                    if (article_count != 0)
445                    {
446                            printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
447                            break;
448                    }
449    
450                    if (p_section[i]->visible_article_count > 0)
451                    {
452                            printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
453                            break;
454                    }
455    
456                    if (p_section[i]->visible_topic_count > 0)
457                    {
458                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
459                            break;
460                    }
461    
462                    last_aid = p_section[i]->p_article_head->aid;
463                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
464                    {
465                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
466                            break;
467                    }
468    
469                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
470                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
471                            p_section[i]->page_count)
472                    {
473                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
474                                       "visible_article_count = %d, last_page_visible_count = %d\n",
475                                       i, j,
476                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
477                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
478                                       p_section[i]->page_count, p_section[i]->visible_article_count,
479                                       p_section[i]->last_page_visible_article_count);
480                            break;
481                    }
482            }
483    
484            for (i = 0; i < BBS_max_section; i++)
485            {
486                    affected_count = 0;
487    
488                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
489                    {
490                            last_aid = i * BBS_article_limit_per_section + j + 1;
491    
492                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
493                            {
494                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
495                                    break;
496                            }
497    
498                            affected_count++;
499                    }
500    
501                    if (affected_count != p_section[i]->article_count)
502                    {
503                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
504                            break;
505                    }
506    
507                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
508                    {
509                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
510                            break;
511                    }
512    
513                    if (p_section[i]->visible_topic_count != group_count)
514                    {
515                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
516                            break;
517                    }
518    
519                    last_aid = p_section[i]->p_article_head->aid;
520                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
521                    {
522                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
523                            break;
524                    }
525    
526                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
527                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
528                            p_section[i]->page_count)
529                    {
530                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
531                                       "visible_article_count = %d, last_page_visible_count = %d\n",
532                                       i, j,
533                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
534                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
535                                       p_section[i]->page_count, p_section[i]->visible_article_count,
536                                       p_section[i]->last_page_visible_article_count);
537                            break;
538                    }
539            }
540    
541            printf("Testing #5 ...\n");
542    
543            if (article_block_reset() != 0)
544            {
545                    log_error("section_data_free_block(i=%d) error\n", i);
546                    return -4;
547            }
548    
549            for (i = 0; i < section_count; i++)
550            {
551                    section_list_reset_articles(p_section[i]);
552            }
553    
554            last_aid = 0;
555    
556            for (i = 0; i < section_count / 2; i++)
557            {
558                    section_first_aid = last_aid + 1;
559    
560                    for (j = 0; j < BBS_article_limit_per_section; j++)
561                    {
562                            last_aid++;
563    
564                            // Set article data
565                            article.aid = last_aid;
566                            article.cid = article.aid;
567                            // Group articles into group_count topics
568                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
569                            article.uid = 1; // TODO: randomize
570                            article.visible = 1;
571                            article.excerption = 0;
572                            article.ontop = 0;
573                            article.lock = 0;
574    
575                            if (section_list_append_article(p_section[i], &article) < 0)
576                            {
577                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
578                                    break;
579                            }
580                    }
581    
582                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
583            }
584    
585            for (i = 0; i < section_count / 2; i++)
586            {
587                    section_first_aid = p_section[i]->p_article_head->aid;
588    
589                    for (j = 0; j < group_count; j++)
590                    {
591                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
592                            if (p_article == NULL)
593                            {
594                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
595                                               section_first_aid + j, i);
596                                    break;
597                            }
598                    }
599            }
600    
601            for (i = 0; i < section_count / 2; i++)
602            {
603                    section_first_aid = p_section[i]->p_article_head->aid;
604    
605                    for (j = 0; j < group_count; j++)
606                    {
607                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
608    
609                            if (affected_count < 0)
610                            {
611                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
612                                    break;
613                            }
614    
615                            if (affected_count != BBS_article_limit_per_section / group_count)
616                            {
617                                    printf("move topic (aid = %d) affected article count %d != %d\n",
618                                               section_first_aid + j, affected_count,
619                                               BBS_article_limit_per_section / group_count);
620                                    break;
621                            }
622                  }                  }
623          }          }
624    
625          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
626          {          {
627                  if (section_data_free_block(p_section[i]) != 0)                  if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
628                    {
629                            printf("Topic count error in section %d, %d != %d\n", i,
630                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
631                            break;
632                    }
633    
634                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
635                    {
636                            printf("Article count error in section %d, %d != %d\n", i,
637                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
638                            break;
639                    }
640    
641                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / BBS_article_limit_per_page))
642                  {                  {
643                          log_error("section_data_free_block(i=%d) error\n", i);                          printf("Page count error in section %d, %d != %d\n", i,
644                          return -4;                                     p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / BBS_article_limit_per_page));
645                            break;
646                  }                  }
647          }          }
648    
649          section_data_pool_cleanup();          printf("Press ENTER to exit...");
650            getchar();
651    
652            article_block_cleanup();
653    
654          log_end();          log_end();
655    


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

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