/[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.9 by sysadm, Thu May 22 06:20:47 2025 UTC Revision 1.24 by sysadm, Mon May 26 03:32:55 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>
22  #include <unistd.h>  #include <unistd.h>
23  #include <errno.h>  #include <errno.h>
24    
25    #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
26    #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",
31          "ABCDEFG",          "ABCDEFG",
# Line 43  int main(int argc, char *argv[]) Line 48  int main(int argc, char *argv[])
48  {  {
49          SECTION_LIST *p_section[BBS_max_section];          SECTION_LIST *p_section[BBS_max_section];
50          ARTICLE *p_article;          ARTICLE *p_article;
51            ARTICLE *p_next;
52          ARTICLE article;          ARTICLE article;
53          int block_count;          int block_count;
54          int i, j;          int i, j;
55            int sid;
56          int last_aid;          int last_aid;
57          int current_tid;          int current_tid;
58          int section_first_aid;          int section_first_aid;
59          int group_count;          int group_count = 100;
60          int article_count;          int article_count;
61            int step;
62            int32_t page;
63            int32_t offset;
64            int affected_count;
65            FILE *fp;
66    
67          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
68          {          {
# Line 62  int main(int argc, char *argv[]) Line 74  int main(int argc, char *argv[])
74          log_err_redirect(STDERR_FILENO);          log_err_redirect(STDERR_FILENO);
75    
76          // - 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
77          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;
78    
79            if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL)
80            {
81                    log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
82                    return -1;
83            }
84            fclose(fp);
85    
86            if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL)
87            {
88                    log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE);
89                    return -1;
90            }
91            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)
107            {
108                    log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
109                    return -2;
110            }
111    
112          if (article_block_init("../conf/menu.conf", block_count) < 0)          if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
113          {          {
114                  log_error("section_data_pool_init() error\n");                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
115                  return -2;                  return -2;
116          }          }
117    
# Line 74  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                  p_section[i] = section_list_create(sname[i % section_conf_count],                  sid = i * 3 + 1;
130                    p_section[i] = section_list_create(sid,
131                                                                                       sname[i % section_conf_count],
132                                                                                     stitle[i % section_conf_count],                                                                                     stitle[i % section_conf_count],
133                                                                                     master_name[i % section_conf_count]);                                                                                     master_name[i % section_conf_count]);
134                  if (p_section[i] == NULL)                  if (p_section[i] == NULL)
135                  {                  {
136                          log_error("section_data_create(i=%d) error\n", i);                          printf("section_list_create(i = %d) error\n", i);
137                            return -3;
138                    }
139    
140                    if (get_section_index(p_section[i]) != i)
141                    {
142                            printf("get_section_index(i = %d) error\n", i);
143                          return -3;                          return -3;
144                  }                  }
145          }          }
146    
147            for (i = 0; i < section_conf_count; i++)
148            {
149                    if (section_list_find_by_name(sname[i]) == NULL)
150                    {
151                            printf("section_list_find_by_name(%s) error\n", sname[i]);
152                            return -3;
153                    }
154            }
155    
156            for (i = 0; i < section_count; i++)
157            {
158                    sid = i * 3 + 1;
159                    if (section_list_find_by_sid(sid) == NULL || section_list_find_by_sid(sid)->sid != sid)
160                    {
161                            printf("section_list_find_by_sid(%d) error\n", sid);
162                            return -3;
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 94  int main(int argc, char *argv[]) Line 176  int main(int argc, char *argv[])
176    
177                          // Set article data                          // Set article data
178                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
179                          article.tid = 0;                          article.tid = 0;
180                            article.sid = i * 3 + 1;
181                            article.cid = article.aid;
182                          article.uid = 1; // TODO: randomize                          article.uid = 1; // TODO: randomize
183                          article.visible = 1;                          article.visible = 1;
184                          article.excerption = 0;                          article.excerption = 0;
185                          article.ontop = 0;                          article.ontop = 0;
186                          article.lock = 0;                          article.lock = 0;
187    
188                            if (section_list_rw_lock(p_section[i]) < 0)
189                            {
190                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
191                                    break;
192                            }
193    
194                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
195                          {                          {
196                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
197                                  break;                                  break;
198                          }                          }
199    
200                            if (section_list_rw_unlock(p_section[i]) < 0)
201                            {
202                                    printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
203                                    break;
204                            }
205                  }                  }
206          }          }
207    
# Line 117  int main(int argc, char *argv[]) Line 212  int main(int argc, char *argv[])
212    
213          last_aid = 0;          last_aid = 0;
214    
215          for (i = 0; i < section_count; i++)          for (j = 0; j < BBS_article_limit_per_section; j++)
216          {          {
217                  if (p_section[i]->article_count == 0)                  for (i = 0; i < section_count; i++)
                 {  
                         continue;  
                 }  
   
                 for (j = 0; j < p_section[i]->article_count; j++)  
218                  {                  {
219                          last_aid++;                          last_aid++;
220    
# Line 134  int main(int argc, char *argv[]) Line 224  int main(int argc, char *argv[])
224                                  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);
225                          }                          }
226    
227                          p_article = article_block_find_by_index(last_aid - 1);                          if (section_list_rw_lock(p_section[i]) < 0)
                         if (p_article == NULL || p_article->aid != last_aid)  
228                          {                          {
229                                  printf("article_block_find_by_index() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);                                  printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
230                                    break;
231                          }                          }
232    
233                          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)
234                          {                          {
235                                  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);
236                          }                          }
237    
238                            if (section_list_rw_unlock(p_section[i]) < 0)
239                            {
240                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
241                                    break;
242                            }
243                  }                  }
244    
245                  // 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 151  int main(int argc, char *argv[]) Line 247  int main(int argc, char *argv[])
247    
248          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
249    
250            if (section_list_rw_lock(NULL) < 0)
251            {
252                    printf("section_list_rw_lock(sid = %d) error\n", 0);
253            }
254    
255          if (article_block_reset() != 0)          if (article_block_reset() != 0)
256          {          {
257                  log_error("section_data_free_block(i=%d) error\n", i);                  log_error("article_block_reset() error\n");
258                  return -4;                  return -4;
259          }          }
260    
# Line 162  int main(int argc, char *argv[]) Line 263  int main(int argc, char *argv[])
263                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
264          }          }
265    
266          group_count = 100;          if (section_list_rw_unlock(NULL) < 0)
267            {
268                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
269            }
270    
271          last_aid = 0;          last_aid = 0;
272    
273          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
274          {          {
275                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
276    
277                    if (section_list_rw_lock(p_section[i]) < 0)
278                    {
279                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
280                            break;
281                    }
282    
283                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
284                  {                  {
285                          last_aid++;                          last_aid++;
286    
287                          // Set article data                          // Set article data
288                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
289                          // Group articles into group_count topics                          // Group articles into group_count topics
290                          article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));                          article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
291                            article.sid = i * 3 + 1;
292                            article.cid = article.aid;
293                          article.uid = 1; // TODO: randomize                          article.uid = 1; // TODO: randomize
294                          article.visible = 1;                          article.visible = 1;
295                          article.excerption = 0;                          article.excerption = 0;
# Line 191  int main(int argc, char *argv[]) Line 303  int main(int argc, char *argv[])
303                          }                          }
304                  }                  }
305    
306                    if (section_list_rw_unlock(p_section[i]) < 0)
307                    {
308                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
309                            break;
310                    }
311    
312                  // 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);
313          }          }
314    
# Line 204  int main(int argc, char *argv[]) Line 322  int main(int argc, char *argv[])
322                  article_count = 0;                  article_count = 0;
323                  last_aid = 0;                  last_aid = 0;
324    
325                    if (section_list_rd_lock(p_section[i]) < 0)
326                    {
327                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
328                            break;
329                    }
330    
331                  p_article = p_section[i]->p_article_head;                  p_article = p_section[i]->p_article_head;
332    
333                  do                  do
# Line 226  int main(int argc, char *argv[]) Line 350  int main(int argc, char *argv[])
350                          break;                          break;
351                  }                  }
352    
353                    if (section_list_rd_unlock(p_section[i]) < 0)
354                    {
355                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
356                            break;
357                    }
358    
359                  // printf("Verified %d articles in section %d\n", group_count, i);                  // printf("Verified %d articles in section %d\n", group_count, i);
360          }          }
361    
# Line 236  int main(int argc, char *argv[]) Line 366  int main(int argc, char *argv[])
366                          continue;                          continue;
367                  }                  }
368    
369                    if (section_list_rd_lock(p_section[i]) < 0)
370                    {
371                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
372                            break;
373                    }
374    
375                    if (p_section[i]->topic_count != group_count)
376                    {
377                            printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
378                    }
379    
380                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
381                  {                  {
382                          last_aid = p_section[i]->p_article_head->aid + j;                          last_aid = p_section[i]->p_article_head->aid + j;
# Line 288  int main(int argc, char *argv[]) Line 429  int main(int argc, char *argv[])
429                          {                          {
430                                  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",
431                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
432                                  break;                                  // break;
433                          }                          }
434                  }                  }
435    
436                    if (section_list_rd_unlock(p_section[i]) < 0)
437                    {
438                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
439                            break;
440                    }
441    
442                  // printf("Verified %d topics in section %d\n", group_count, i);                  // printf("Verified %d topics in section %d\n", group_count, i);
443          }          }
444    
445          printf("Testing #3 ...\n");          printf("Testing #3 ...\n");
446    
447          for (i = 0; i < section_conf_count; i++)          for (i = 0; i < section_count; i++)
448          {          {
449                  if (section_list_find_by_name(sname[i]) == NULL)                  last_aid = 0;
450    
451                    if (section_list_rd_lock(p_section[i]) < 0)
452                    {
453                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
454                            break;
455                    }
456    
457                    for (j = 0; j < p_section[i]->page_count; j++)
458                    {
459                            if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
460                            {
461                                    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);
462                            }
463    
464                            if (j > 0)
465                            {
466                                    step = 0;
467    
468                                    for (p_article = p_section[i]->p_page_first_article[j];
469                                             p_article != p_section[i]->p_page_first_article[j - 1];
470                                             p_article = p_article->p_prior)
471                                    {
472                                            step++;
473                                    }
474    
475                                    if (step != BBS_article_limit_per_page)
476                                    {
477                                            printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
478                                    }
479                            }
480                    }
481    
482                    if (section_list_rd_unlock(p_section[i]) < 0)
483                    {
484                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
485                            break;
486                    }
487            }
488    
489            printf("Testing #4 ...\n");
490    
491            for (i = 0; i < section_count; i++)
492            {
493                    if (section_list_rw_lock(p_section[i]) < 0)
494                    {
495                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
496                            break;
497                    }
498    
499                    step = i % 10 + 1;
500                    for (j = group_count; j < BBS_article_limit_per_section; j += step)
501                    {
502                            last_aid = i * BBS_article_limit_per_section + j + 1;
503    
504                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
505    
506                            if (p_article == NULL)
507                            {
508                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
509                                    break;
510                            }
511    
512                            if (p_article->aid != last_aid)
513                            {
514                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
515                                    break;
516                            }
517    
518                            if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
519                            {
520                                    printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
521                                    break;
522                            }
523                    }
524    
525                    last_aid = p_section[i]->p_article_head->aid;
526                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
527                    {
528                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
529                            break;
530                    }
531    
532                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
533                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
534                            p_section[i]->page_count)
535                    {
536                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
537                                       "visible_article_count = %d, last_page_visible_count = %d\n",
538                                       i, j,
539                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
540                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
541                                       p_section[i]->page_count, p_section[i]->visible_article_count,
542                                       p_section[i]->last_page_visible_article_count);
543                            break;
544                    }
545    
546                    affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
547                    if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
548                  {                  {
549                          printf("section_data_find_section_by_name(%s) error\n", sname[i]);                          printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
550                                       p_section[i]->article_count - p_section[i]->visible_article_count,
551                                       affected_count);
552                            break;
553                    }
554    
555                    article_count = p_section[i]->visible_article_count;
556    
557                    for (j = 0; j < group_count; j += 1)
558                    {
559                            last_aid = i * BBS_article_limit_per_section + j + 1;
560    
561                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
562    
563                            if (p_article == NULL)
564                            {
565                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
566                                    break;
567                            }
568    
569                            if (p_article->aid != last_aid)
570                            {
571                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
572                                    break;
573                            }
574    
575                            if (p_article->tid != 0)
576                            {
577                                    printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
578                                    break;
579                            }
580    
581                            if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
582                            {
583                                    printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
584                                    break;
585                            }
586    
587                            article_count -= affected_count;
588                    }
589    
590                    if (article_count != 0)
591                    {
592                            printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
593                            break;
594                    }
595    
596                    if (p_section[i]->visible_article_count > 0)
597                    {
598                            printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
599                            break;
600                    }
601    
602                    if (p_section[i]->visible_topic_count > 0)
603                    {
604                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
605                            break;
606                    }
607    
608                    last_aid = p_section[i]->p_article_head->aid;
609                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
610                    {
611                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
612                            break;
613                    }
614    
615                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
616                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
617                            p_section[i]->page_count)
618                    {
619                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
620                                       "visible_article_count = %d, last_page_visible_count = %d\n",
621                                       i, j,
622                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
623                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
624                                       p_section[i]->page_count, p_section[i]->visible_article_count,
625                                       p_section[i]->last_page_visible_article_count);
626                            break;
627                    }
628    
629                    if (section_list_rw_unlock(p_section[i]) < 0)
630                    {
631                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
632                            break;
633                  }                  }
634          }          }
635    
636            for (i = 0; i < BBS_max_section; i++)
637            {
638                    if (section_list_rw_lock(p_section[i]) < 0)
639                    {
640                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
641                            break;
642                    }
643    
644                    affected_count = 0;
645    
646                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
647                    {
648                            last_aid = i * BBS_article_limit_per_section + j + 1;
649    
650                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
651                            {
652                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
653                                    break;
654                            }
655    
656                            affected_count++;
657                    }
658    
659                    if (affected_count != p_section[i]->article_count)
660                    {
661                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
662                            break;
663                    }
664    
665                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
666                    {
667                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
668                            break;
669                    }
670    
671                    if (p_section[i]->visible_topic_count != group_count)
672                    {
673                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
674                            break;
675                    }
676    
677                    last_aid = p_section[i]->p_article_head->aid;
678                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
679                    {
680                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
681                            break;
682                    }
683    
684                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
685                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
686                            p_section[i]->page_count)
687                    {
688                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
689                                       "visible_article_count = %d, last_page_visible_count = %d\n",
690                                       i, j,
691                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
692                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
693                                       p_section[i]->page_count, p_section[i]->visible_article_count,
694                                       p_section[i]->last_page_visible_article_count);
695                            break;
696                    }
697    
698                    if (section_list_rw_unlock(p_section[i]) < 0)
699                    {
700                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
701                            break;
702                    }
703            }
704    
705            printf("Testing #5 ...\n");
706    
707            if (section_list_rw_lock(NULL) < 0)
708            {
709                    printf("section_list_rw_lock(sid = %d) error\n", 0);
710            }
711    
712            if (article_block_reset() != 0)
713            {
714                    log_error("article_block_reset() error\n");
715                    return -4;
716            }
717    
718            for (i = 0; i < section_count; i++)
719            {
720                    section_list_reset_articles(p_section[i]);
721            }
722    
723            if (section_list_rw_unlock(NULL) < 0)
724            {
725                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
726            }
727    
728            last_aid = 0;
729    
730            for (i = 0; i < section_count / 2; i++)
731            {
732                    if (section_list_rw_lock(p_section[i]) < 0)
733                    {
734                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
735                            break;
736                    }
737    
738                    section_first_aid = last_aid + 1;
739    
740                    for (j = 0; j < BBS_article_limit_per_section; j++)
741                    {
742                            last_aid++;
743    
744                            // Set article data
745                            article.aid = last_aid;
746                            // Group articles into group_count topics
747                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
748                            article.sid = i * 3 + 1;
749                            article.cid = article.aid;
750                            article.uid = 1; // TODO: randomize
751                            article.visible = 1;
752                            article.excerption = 0;
753                            article.ontop = 0;
754                            article.lock = 0;
755    
756                            if (section_list_append_article(p_section[i], &article) < 0)
757                            {
758                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
759                                    break;
760                            }
761                    }
762    
763                    if (section_list_rw_unlock(p_section[i]) < 0)
764                    {
765                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
766                            break;
767                    }
768    
769                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
770            }
771    
772            for (i = 0; i < section_count / 2; i++)
773            {
774                    if (section_list_rw_lock(p_section[i]) < 0)
775                    {
776                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
777                            break;
778                    }
779    
780                    section_first_aid = p_section[i]->p_article_head->aid;
781    
782                    for (j = 0; j < group_count; j += 2)
783                    {
784                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
785                            if (p_article == NULL)
786                            {
787                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
788                                               section_first_aid + j, i);
789                                    break;
790                            }
791    
792                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
793                            {
794                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
795                            }
796                    }
797    
798                    if (section_list_rw_unlock(p_section[i]) < 0)
799                    {
800                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
801                            break;
802                    }
803            }
804    
805            for (i = 0; i < section_count / 2; i++)
806            {
807                    if (section_list_rw_lock(p_section[i]) < 0)
808                    {
809                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
810                            break;
811                    }
812    
813                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
814                    {
815                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
816    
817                            if (section_list_rw_unlock(p_section[i]) < 0)
818                            {
819                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
820                            }
821                            break;
822                    }
823    
824                    section_first_aid = p_section[i]->p_article_head->aid;
825    
826                    for (j = 0; j < group_count; j++)
827                    {
828                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
829    
830                            if (affected_count < 0)
831                            {
832                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
833                                    break;
834                            }
835    
836                            if (affected_count != BBS_article_limit_per_section / group_count)
837                            {
838                                    printf("move topic (aid = %d) affected article count %d != %d\n",
839                                               section_first_aid + j, affected_count,
840                                               BBS_article_limit_per_section / group_count);
841                                    // break;
842                            }
843                    }
844    
845                    if (section_list_rw_unlock(p_section[i]) < 0)
846                    {
847                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
848                            break;
849                    }
850    
851                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
852                    {
853                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
854    
855                            if (section_list_rw_unlock(p_section[i]) < 0)
856                            {
857                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
858                            }
859                            break;
860                    }
861            }
862    
863            for (i = 0; i < section_count; i++)
864            {
865                    if (section_list_rd_lock(p_section[i]) < 0)
866                    {
867                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
868                            break;
869                    }
870    
871                    if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
872                    {
873                            printf("Topic count error in section %d, %d != %d\n", i,
874                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
875                            break;
876                    }
877    
878                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
879                    {
880                            printf("Visible topic count error in section %d, %d != %d\n", i,
881                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
882                            break;
883                    }
884    
885                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
886                    {
887                            printf("Article count error in section %d, %d != %d\n", i,
888                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
889                            break;
890                    }
891    
892                    if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
893                    {
894                            printf("Visible article count error in section %d, %d != %d\n", i,
895                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
896                            break;
897                    }
898    
899                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
900                    {
901                            printf("Page count error in section %d, %d != %d\n", i,
902                                       p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
903                            break;
904                    }
905    
906                    if (section_list_rd_unlock(p_section[i]) < 0)
907                    {
908                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
909                            break;
910                    }
911            }
912    
913            printf("Testing #6 ...\n");
914    
915            for (i = 0; i < section_count; i++)
916            {
917                    if (section_list_rd_lock(p_section[i]) < 0)
918                    {
919                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
920                            break;
921                    }
922            }
923    
924            printf("Try rw_lock for 5 sec...\n");
925            if (section_list_try_rw_lock(NULL, 5) == 0)
926            {
927                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
928            }
929    
930            for (i = 0; i < section_count; i++)
931            {
932                    if (section_list_rd_unlock(p_section[i]) < 0)
933                    {
934                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
935                            break;
936                    }
937            }
938    
939            if (section_list_try_rw_lock(NULL, 5) < 0)
940            {
941                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
942            }
943    
944            for (i = 0; i < section_count; i++)
945            {
946                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
947                    {
948                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
949                            break;
950                    }
951            }
952    
953            if (section_list_rw_unlock(NULL) < 0)
954            {
955                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
956            }
957    
958          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
959          getchar();          getchar();
960    
961            section_list_cleanup();
962          article_block_cleanup();          article_block_cleanup();
963            trie_dict_cleanup();
964    
965            if (unlink(TRIE_DICT_SHM_FILE) < 0)
966            {
967                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
968                    return -1;
969            }
970    
971            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
972            {
973                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
974                    return -1;
975            }
976    
977            if (unlink(SECTION_LIST_SHM_FILE) < 0)
978            {
979                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
980                    return -1;
981            }
982    
983          log_end();          log_end();
984    


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

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