/[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.13 by sysadm, Fri May 23 00:12:59 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 article;          ARTICLE article;
47            int block_count;
48          int i, j;          int i, j;
49          int last_aid;          int last_aid;
50            int current_tid;
51            int section_first_aid;
52            int group_count = 100;
53            int article_count;
54            int step;
55            int32_t page;
56            int32_t offset;
57            int affected_count;
58    
59          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
60          {          {
# Line 57  int main(int argc, char *argv[]) Line 65  int main(int argc, char *argv[])
65          log_std_redirect(STDOUT_FILENO);          log_std_redirect(STDOUT_FILENO);
66          log_err_redirect(STDERR_FILENO);          log_err_redirect(STDERR_FILENO);
67    
68          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
69            block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK;
70    
71            if (article_block_init("../conf/menu.conf", block_count) < 0)
72          {          {
73                  log_error("section_data_pool_init() error\n");                  log_error("section_data_pool_init() error\n");
74                  return -2;                  return -2;
75          }          }
76    
77            printf("Testing #1 ...\n");
78    
79          last_aid = 0;          last_aid = 0;
80    
81          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
82          {          {
83                  if ((p_section[i] = section_data_create(sname[i], stitle[i], master_name[i])) == NULL)                  p_section[i] = section_list_create(sname[i % section_conf_count],
84                                                                                       stitle[i % section_conf_count],
85                                                                                       master_name[i % section_conf_count]);
86                    if (p_section[i] == NULL)
87                  {                  {
88                          log_error("section_data_create(i=%d) error\n", i);                          log_error("section_data_create(i=%d) error\n", i);
89                          return -3;                          return -3;
90                  }                  }
91            }
92    
93            for (i = 0; i < section_conf_count; i++)
94            {
95                    if (section_list_find_by_name(sname[i]) == NULL)
96                    {
97                            printf("section_data_find_section_by_name(%s) error\n", sname[i]);
98                            return -3;
99                    }
100            }
101    
102                  for (j = 0; j < BBS_article_limit_per_block * BBS_article_block_limit_per_section; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
103            {
104                    for (i = 0; i < section_count; i++)
105                  {                  {
106                          last_aid++;                          last_aid++;
107    
# Line 87  int main(int argc, char *argv[]) Line 115  int main(int argc, char *argv[])
115                          article.ontop = 0;                          article.ontop = 0;
116                          article.lock = 0;                          article.lock = 0;
117    
118                          section_data_append_article(p_section[i], &article);                          if (section_list_append_article(p_section[i], &article) < 0)
119                            {
120                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
121                                    break;
122                            }
123                  }                  }
124          }          }
125    
126          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
127          {          {
128                  if (section_data_free_block(p_section[i]) != 0)                  // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
129            }
130    
131            last_aid = 0;
132    
133            for (i = 0; i < section_count; i++)
134            {
135                    if (p_section[i]->article_count == 0)
136                  {                  {
137                          log_error("section_data_free_block(i=%d) error\n", i);                          continue;
                         return -4;  
138                  }                  }
139    
140                    for (j = 0; j < p_section[i]->article_count; j++)
141                    {
142                            last_aid++;
143    
144                            p_article = article_block_find_by_aid(last_aid);
145                            if (p_article == NULL || p_article->aid != last_aid)
146                            {
147                                    printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
148                            }
149    
150                            p_article = article_block_find_by_index(last_aid - 1);
151                            if (p_article == NULL || p_article->aid != last_aid)
152                            {
153                                    printf("article_block_find_by_index() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
154                            }
155    
156                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
157                            {
158                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
159                            }
160                    }
161    
162                    // printf("Verified %d articles in section %d\n", p_section[i]->article_count, i);
163            }
164    
165            printf("Testing #2 ...\n");
166    
167            if (article_block_reset() != 0)
168            {
169                    log_error("section_data_free_block(i=%d) error\n", i);
170                    return -4;
171            }
172    
173            for (i = 0; i < section_count; i++)
174            {
175                    section_list_reset_articles(p_section[i]);
176          }          }
177    
178          section_data_pool_cleanup();          last_aid = 0;
179    
180            for (i = 0; i < section_count; i++)
181            {
182                    section_first_aid = last_aid + 1;
183    
184                    for (j = 0; j < BBS_article_limit_per_section; j++)
185                    {
186                            last_aid++;
187    
188                            // Set article data
189                            article.aid = last_aid;
190                            article.cid = article.aid;
191                            // Group articles into group_count topics
192                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
193                            article.uid = 1; // TODO: randomize
194                            article.visible = 1;
195                            article.excerption = 0;
196                            article.ontop = 0;
197                            article.lock = 0;
198    
199                            if (section_list_append_article(p_section[i], &article) < 0)
200                            {
201                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
202                                    break;
203                            }
204                    }
205    
206                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
207            }
208    
209            for (i = 0; i < section_count; i++)
210            {
211                    if (p_section[i]->article_count == 0)
212                    {
213                            continue;
214                    }
215    
216                    article_count = 0;
217                    last_aid = 0;
218    
219                    p_article = p_section[i]->p_article_head;
220    
221                    do
222                    {
223                            article_count++;
224    
225                            if (p_article->aid <= last_aid)
226                            {
227                                    printf("Non-ascending aid found %d <= %d at article\n", p_article->aid, last_aid);
228                            }
229                            last_aid = p_article->aid;
230    
231                            p_article = p_article->p_next;
232                    } while (p_article != p_section[i]->p_article_head);
233    
234                    if (article_count != p_section[i]->article_count)
235                    {
236                            printf("Count of articles in section %d is different from expected %d != %d\n",
237                                       i, article_count, p_section[i]->article_count);
238                            break;
239                    }
240    
241                    // printf("Verified %d articles in section %d\n", group_count, i);
242            }
243    
244            for (i = 0; i < section_count; i++)
245            {
246                    if (p_section[i]->article_count == 0)
247                    {
248                            continue;
249                    }
250    
251                    if (p_section[i]->topic_count != group_count)
252                    {
253                            printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
254                    }
255    
256                    for (j = 0; j < group_count; j++)
257                    {
258                            last_aid = p_section[i]->p_article_head->aid + j;
259    
260                            p_article = article_block_find_by_aid(last_aid);
261                            if (p_article == NULL)
262                            {
263                                    printf("NULL p_article at section %d index %d\n", i, j);
264                                    break;
265                            }
266                            if (p_article->aid != last_aid)
267                            {
268                                    printf("Inconsistent aid at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
269                                    break;
270                            }
271    
272                            article_count = 1;
273                            last_aid = 0;
274                            current_tid = p_article->aid;
275    
276                            do
277                            {
278                                    if (p_article->aid <= last_aid)
279                                    {
280                                            printf("Non-ascending aid found %d <= %d\n", p_article->aid, last_aid);
281                                    }
282                                    last_aid = p_article->aid;
283    
284                                    p_article = p_article->p_topic_next;
285    
286                                    if (p_article == NULL)
287                                    {
288                                            printf("NULL p_article found\n");
289                                            break;
290                                    }
291                                    if (p_article->tid == 0) // loop
292                                    {
293                                            break;
294                                    }
295                                    if (p_article->tid != current_tid)
296                                    {
297                                            printf("Inconsistent tid %d != %d\n", p_article->tid, current_tid);
298                                            break;
299                                    }
300    
301                                    article_count++;
302                            } while (1);
303    
304                            if (article_count != p_section[i]->article_count / group_count)
305                            {
306                                    printf("Count of articles in topic %d is different from expected %d != %d\n",
307                                               j + 1, article_count, p_section[i]->article_count / group_count);
308                                    break;
309                            }
310                    }
311    
312                    // printf("Verified %d topics in section %d\n", group_count, i);
313            }
314    
315            printf("Testing #3 ...\n");
316    
317            for (i = 0; i < section_count; i++)
318            {
319                    last_aid = 0;
320    
321                    for (j = 0; j < p_section[i]->page_count; j++)
322                    {
323                            if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
324                            {
325                                    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);
326                            }
327    
328                            if (j > 0)
329                            {
330                                    step = 0;
331    
332                                    for (p_article = p_section[i]->p_page_first_article[j];
333                                             p_article != p_section[i]->p_page_first_article[j - 1];
334                                             p_article = p_article->p_prior)
335                                    {
336                                            step++;
337                                    }
338    
339                                    if (step != BBS_article_limit_per_page)
340                                    {
341                                            printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
342                                    }
343                            }
344                    }
345            }
346    
347            printf("Testing #4 ...\n");
348    
349            for (i = 0; i < section_count; i++)
350            {
351                    step = i % 10 + 1;
352                    for (j = group_count; j < BBS_article_limit_per_section; j += step)
353                    {
354                            last_aid = i * BBS_article_limit_per_section + j + 1;
355    
356                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset);
357    
358                            if (p_article == NULL)
359                            {
360                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
361                                    break;
362                            }
363    
364                            if (p_article->aid != last_aid)
365                            {
366                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
367                                    break;
368                            }
369    
370                            if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
371                            {
372                                    printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
373                                    break;
374                            }
375                    }
376    
377                    last_aid = p_section[i]->p_article_head->aid;
378                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
379                    {
380                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
381                            break;
382                    }
383    
384                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
385                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
386                            p_section[i]->page_count)
387                    {
388                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
389                                       "visible_article_count = %d, last_page_visible_count = %d\n",
390                                       i, j,
391                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
392                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
393                                       p_section[i]->page_count, p_section[i]->visible_article_count,
394                                       p_section[i]->last_page_visible_article_count);
395                            break;
396                    }
397    
398                    affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
399                    if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
400                    {
401                            printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
402                                       p_section[i]->article_count - p_section[i]->visible_article_count,
403                                       affected_count);
404                            break;
405                    }
406    
407                    article_count = p_section[i]->visible_article_count;
408    
409                    for (j = 0; j < group_count; j += 1)
410                    {
411                            last_aid = i * BBS_article_limit_per_section + j + 1;
412    
413                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset);
414    
415                            if (p_article == NULL)
416                            {
417                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
418                                    break;
419                            }
420    
421                            if (p_article->aid != last_aid)
422                            {
423                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
424                                    break;
425                            }
426    
427                            if (p_article->tid != 0)
428                            {
429                                    printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
430                                    break;
431                            }
432    
433                            if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
434                            {
435                                    printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
436                                    break;
437                            }
438    
439                            article_count -= affected_count;
440                    }
441    
442                    if (article_count != 0)
443                    {
444                            printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
445                            break;
446                    }
447    
448                    if (p_section[i]->visible_article_count > 0)
449                    {
450                            printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
451                            break;
452                    }
453    
454                    if (p_section[i]->visible_topic_count > 0)
455                    {
456                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
457                            break;
458                    }
459    
460                    last_aid = p_section[i]->p_article_head->aid;
461                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
462                    {
463                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
464                            break;
465                    }
466    
467                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
468                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
469                            p_section[i]->page_count)
470                    {
471                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
472                                       "visible_article_count = %d, last_page_visible_count = %d\n",
473                                       i, j,
474                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
475                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
476                                       p_section[i]->page_count, p_section[i]->visible_article_count,
477                                       p_section[i]->last_page_visible_article_count);
478                            break;
479                    }
480            }
481    
482            for (i = 0; i < BBS_max_section; i++)
483            {
484                    affected_count = 0;
485    
486                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
487                    {
488                            last_aid = i * BBS_article_limit_per_section + j + 1;
489    
490                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
491                            {
492                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
493                                    break;
494                            }
495    
496                            affected_count++;
497                    }
498    
499                    if (affected_count != p_section[i]->article_count)
500                    {
501                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
502                            break;
503                    }
504    
505                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
506                    {
507                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
508                            break;
509                    }
510    
511                    if (p_section[i]->visible_topic_count != group_count)
512                    {
513                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
514                            break;
515                    }
516    
517                    last_aid = p_section[i]->p_article_head->aid;
518                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
519                    {
520                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
521                            break;
522                    }
523    
524                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
525                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
526                            p_section[i]->page_count)
527                    {
528                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
529                                       "visible_article_count = %d, last_page_visible_count = %d\n",
530                                       i, j,
531                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
532                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
533                                       p_section[i]->page_count, p_section[i]->visible_article_count,
534                                       p_section[i]->last_page_visible_article_count);
535                            break;
536                    }
537            }
538    
539            printf("Press ENTER to exit...");
540            getchar();
541    
542            article_block_cleanup();
543    
544          log_end();          log_end();
545    


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

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