/[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.11 by sysadm, Thu May 22 14:12:33 2025 UTC Revision 1.27 by sysadm, Tue May 27 07:21:43 2025 UTC
# Line 15  Line 15 
15   ***************************************************************************/   ***************************************************************************/
16    
17  #include "section_list.h"  #include "section_list.h"
18    #include "trie_dict.h"
19  #include "bbs.h"  #include "bbs.h"
20  #include "log.h"  #include "log.h"
21  #include <stdio.h>  #include <stdio.h>
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;
# Line 55  int main(int argc, char *argv[]) Line 62  int main(int argc, char *argv[])
62          int32_t page;          int32_t page;
63          int32_t offset;          int32_t offset;
64          int affected_count;          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 66  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 78  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          }          }
# Line 94  int main(int argc, char *argv[]) Line 148  int main(int argc, char *argv[])
148          {          {
149                  if (section_list_find_by_name(sname[i]) == NULL)                  if (section_list_find_by_name(sname[i]) == NULL)
150                  {                  {
151                          printf("section_data_find_section_by_name(%s) error\n", sname[i]);                          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;                          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 107  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                            article.transship = 0;
188    
189                            if (section_list_rw_lock(p_section[i]) < 0)
190                            {
191                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
192                                    break;
193                            }
194    
195                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
196                          {                          {
197                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
198                                  break;                                  break;
199                          }                          }
200    
201                            if (section_list_rw_unlock(p_section[i]) < 0)
202                            {
203                                    printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
204                                    break;
205                            }
206                  }                  }
207          }          }
208    
# Line 128  int main(int argc, char *argv[]) Line 211  int main(int argc, char *argv[])
211                  // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);                  // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
212          }          }
213    
214          last_aid = 0;          if (last_aid != article_block_last_aid())
215            {
216                    printf("last_aid != %d\n", article_block_last_aid());
217            }
218    
219          for (i = 0; i < section_count; i++)          if (article_block_article_count() != section_count * BBS_article_limit_per_section)
220          {          {
221                  if (p_section[i]->article_count == 0)                  printf("article_block_article_count() error %d != %d * %d\n",
222                  {                             article_block_article_count(), section_count, BBS_article_limit_per_section);
223                          continue;          }
                 }  
224    
225                  for (j = 0; j < p_section[i]->article_count; j++)          last_aid = 0;
226    
227            for (j = 0; j < BBS_article_limit_per_section; j++)
228            {
229                    for (i = 0; i < section_count; i++)
230                  {                  {
231                          last_aid++;                          last_aid++;
232    
# Line 147  int main(int argc, char *argv[]) Line 236  int main(int argc, char *argv[])
236                                  printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);                                  printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
237                          }                          }
238    
239                          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)  
240                          {                          {
241                                  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);
242                                    break;
243                          }                          }
244    
245                          if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)                          if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
246                          {                          {
247                                  printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);                                  printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
248                          }                          }
249    
250                            if (section_list_rw_unlock(p_section[i]) < 0)
251                            {
252                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
253                                    break;
254                            }
255                  }                  }
256    
257                  // printf("Verified %d articles in section %d\n", p_section[i]->article_count, i);                  // printf("Verified %d articles in section %d\n", p_section[i]->article_count, i);
# Line 164  int main(int argc, char *argv[]) Line 259  int main(int argc, char *argv[])
259    
260          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
261    
262            if (section_list_rw_lock(NULL) < 0)
263            {
264                    printf("section_list_rw_lock(sid = %d) error\n", 0);
265            }
266    
267          if (article_block_reset() != 0)          if (article_block_reset() != 0)
268          {          {
269                  log_error("section_data_free_block(i=%d) error\n", i);                  log_error("article_block_reset() error\n");
270                  return -4;                  return -4;
271          }          }
272    
# Line 175  int main(int argc, char *argv[]) Line 275  int main(int argc, char *argv[])
275                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
276          }          }
277    
278            if (section_list_rw_unlock(NULL) < 0)
279            {
280                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
281            }
282    
283          last_aid = 0;          last_aid = 0;
284    
285          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
286          {          {
287                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
288    
289                    if (section_list_rw_lock(p_section[i]) < 0)
290                    {
291                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
292                            break;
293                    }
294    
295                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
296                  {                  {
297                          last_aid++;                          last_aid++;
298    
299                          // Set article data                          // Set article data
300                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
301                          // Group articles into group_count topics                          // Group articles into group_count topics
302                          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));
303                            article.sid = i * 3 + 1;
304                            article.cid = article.aid;
305                          article.uid = 1; // TODO: randomize                          article.uid = 1; // TODO: randomize
306                          article.visible = 1;                          article.visible = 1;
307                          article.excerption = 0;                          article.excerption = 0;
308                          article.ontop = 0;                          article.ontop = 0;
309                          article.lock = 0;                          article.lock = 0;
310                            article.transship = 0;
311    
312                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
313                          {                          {
# Line 203  int main(int argc, char *argv[]) Line 316  int main(int argc, char *argv[])
316                          }                          }
317                  }                  }
318    
319                    if (section_list_rw_unlock(p_section[i]) < 0)
320                    {
321                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
322                            break;
323                    }
324    
325                  // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);                  // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
326          }          }
327    
# Line 216  int main(int argc, char *argv[]) Line 335  int main(int argc, char *argv[])
335                  article_count = 0;                  article_count = 0;
336                  last_aid = 0;                  last_aid = 0;
337    
338                    if (section_list_rd_lock(p_section[i]) < 0)
339                    {
340                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
341                            break;
342                    }
343    
344                  p_article = p_section[i]->p_article_head;                  p_article = p_section[i]->p_article_head;
345    
346                  do                  do
# Line 238  int main(int argc, char *argv[]) Line 363  int main(int argc, char *argv[])
363                          break;                          break;
364                  }                  }
365    
366                    if (section_list_rd_unlock(p_section[i]) < 0)
367                    {
368                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
369                            break;
370                    }
371    
372                  // printf("Verified %d articles in section %d\n", group_count, i);                  // printf("Verified %d articles in section %d\n", group_count, i);
373          }          }
374    
# Line 248  int main(int argc, char *argv[]) Line 379  int main(int argc, char *argv[])
379                          continue;                          continue;
380                  }                  }
381    
382                    if (section_list_rd_lock(p_section[i]) < 0)
383                    {
384                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
385                            break;
386                    }
387    
388                  if (p_section[i]->topic_count != group_count)                  if (p_section[i]->topic_count != group_count)
389                  {                  {
390                          printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);                          printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
# Line 305  int main(int argc, char *argv[]) Line 442  int main(int argc, char *argv[])
442                          {                          {
443                                  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",
444                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
445                                  break;                                  // break;
446                          }                          }
447                  }                  }
448    
449                    if (section_list_rd_unlock(p_section[i]) < 0)
450                    {
451                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
452                            break;
453                    }
454    
455                  // printf("Verified %d topics in section %d\n", group_count, i);                  // printf("Verified %d topics in section %d\n", group_count, i);
456          }          }
457    
# Line 318  int main(int argc, char *argv[]) Line 461  int main(int argc, char *argv[])
461          {          {
462                  last_aid = 0;                  last_aid = 0;
463    
464                    if (section_list_rd_lock(p_section[i]) < 0)
465                    {
466                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
467                            break;
468                    }
469    
470                  for (j = 0; j < p_section[i]->page_count; j++)                  for (j = 0; j < p_section[i]->page_count; j++)
471                  {                  {
472                          if (p_section[i]->p_page_first_article[j]->aid <= last_aid)                          if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
# Line 342  int main(int argc, char *argv[]) Line 491  int main(int argc, char *argv[])
491                                  }                                  }
492                          }                          }
493                  }                  }
494    
495                    if (section_list_rd_unlock(p_section[i]) < 0)
496                    {
497                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
498                            break;
499                    }
500          }          }
501    
502          printf("Testing #4 ...\n");          printf("Testing #4 ...\n");
503    
504          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
505          {          {
506                    if (section_list_rw_lock(p_section[i]) < 0)
507                    {
508                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
509                            break;
510                    }
511    
512                  step = i % 10 + 1;                  step = i % 10 + 1;
513                  for (j = group_count; j < BBS_article_limit_per_section; j += step)                  for (j = group_count; j < BBS_article_limit_per_section; j += step)
514                  {                  {
515                          last_aid = i * BBS_article_limit_per_section + j + 1;                          last_aid = i * BBS_article_limit_per_section + j + 1;
516    
517                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset);                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
518    
519                          if (p_article == NULL)                          if (p_article == NULL)
520                          {                          {
# Line 410  int main(int argc, char *argv[]) Line 571  int main(int argc, char *argv[])
571                  {                  {
572                          last_aid = i * BBS_article_limit_per_section + j + 1;                          last_aid = i * BBS_article_limit_per_section + j + 1;
573    
574                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset);                          p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
575    
576                          if (p_article == NULL)                          if (p_article == NULL)
577                          {                          {
# Line 447  int main(int argc, char *argv[]) Line 608  int main(int argc, char *argv[])
608    
609                  if (p_section[i]->visible_article_count > 0)                  if (p_section[i]->visible_article_count > 0)
610                  {                  {
611                          printf("Inconsistent invisible count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);                          printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
612                            break;
613                    }
614    
615                    if (p_section[i]->visible_topic_count > 0)
616                    {
617                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
618                            break;
619                    }
620    
621                    last_aid = p_section[i]->p_article_head->aid;
622                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
623                    {
624                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
625                            break;
626                    }
627    
628                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
629                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
630                            p_section[i]->page_count)
631                    {
632                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
633                                       "visible_article_count = %d, last_page_visible_count = %d\n",
634                                       i, j,
635                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
636                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
637                                       p_section[i]->page_count, p_section[i]->visible_article_count,
638                                       p_section[i]->last_page_visible_article_count);
639                            break;
640                    }
641    
642                    if (section_list_rw_unlock(p_section[i]) < 0)
643                    {
644                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
645                            break;
646                    }
647            }
648    
649            for (i = 0; i < BBS_max_section; i++)
650            {
651                    if (section_list_rw_lock(p_section[i]) < 0)
652                    {
653                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
654                            break;
655                    }
656    
657                    affected_count = 0;
658    
659                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
660                    {
661                            last_aid = i * BBS_article_limit_per_section + j + 1;
662    
663                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
664                            {
665                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
666                                    break;
667                            }
668    
669                            affected_count++;
670                    }
671    
672                    if (affected_count != p_section[i]->article_count)
673                    {
674                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
675                            break;
676                    }
677    
678                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
679                    {
680                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
681                            break;
682                    }
683    
684                    if (p_section[i]->visible_topic_count != group_count)
685                    {
686                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
687                            break;
688                    }
689    
690                    last_aid = p_section[i]->p_article_head->aid;
691                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
692                    {
693                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
694                            break;
695                    }
696    
697                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
698                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
699                            p_section[i]->page_count)
700                    {
701                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
702                                       "visible_article_count = %d, last_page_visible_count = %d\n",
703                                       i, j,
704                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
705                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
706                                       p_section[i]->page_count, p_section[i]->visible_article_count,
707                                       p_section[i]->last_page_visible_article_count);
708                            break;
709                    }
710    
711                    if (section_list_rw_unlock(p_section[i]) < 0)
712                    {
713                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
714                            break;
715                    }
716            }
717    
718            printf("Testing #5 ...\n");
719    
720            if (section_list_rw_lock(NULL) < 0)
721            {
722                    printf("section_list_rw_lock(sid = %d) error\n", 0);
723            }
724    
725            if (article_block_reset() != 0)
726            {
727                    log_error("article_block_reset() error\n");
728                    return -4;
729            }
730    
731            for (i = 0; i < section_count; i++)
732            {
733                    section_list_reset_articles(p_section[i]);
734            }
735    
736            if (section_list_rw_unlock(NULL) < 0)
737            {
738                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
739            }
740    
741            last_aid = 0;
742    
743            for (i = 0; i < section_count / 2; i++)
744            {
745                    if (section_list_rw_lock(p_section[i]) < 0)
746                    {
747                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
748                            break;
749                    }
750    
751                    section_first_aid = last_aid + 1;
752    
753                    for (j = 0; j < BBS_article_limit_per_section; j++)
754                    {
755                            last_aid++;
756    
757                            // Set article data
758                            article.aid = last_aid;
759                            // Group articles into group_count topics
760                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
761                            article.sid = i * 3 + 1;
762                            article.cid = article.aid;
763                            article.uid = 1; // TODO: randomize
764                            article.visible = 1;
765                            article.excerption = 0;
766                            article.ontop = 0;
767                            article.lock = 0;
768                            article.transship = 0;
769    
770                            if (section_list_append_article(p_section[i], &article) < 0)
771                            {
772                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
773                                    break;
774                            }
775                    }
776    
777                    if (section_list_rw_unlock(p_section[i]) < 0)
778                    {
779                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
780                            break;
781                    }
782    
783                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
784            }
785    
786            for (i = 0; i < section_count / 2; i++)
787            {
788                    if (section_list_rw_lock(p_section[i]) < 0)
789                    {
790                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
791                            break;
792                    }
793    
794                    section_first_aid = p_section[i]->p_article_head->aid;
795    
796                    for (j = 0; j < group_count; j += 2)
797                    {
798                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
799                            if (p_article == NULL)
800                            {
801                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
802                                               section_first_aid + j, i);
803                                    break;
804                            }
805    
806                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
807                            {
808                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
809                            }
810                    }
811    
812                    if (section_list_rw_unlock(p_section[i]) < 0)
813                    {
814                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
815                          break;                          break;
816                  }                  }
817          }          }
818    
819            for (i = 0; i < section_count / 2; i++)
820            {
821                    if (section_list_rw_lock(p_section[i]) < 0)
822                    {
823                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
824                            break;
825                    }
826    
827                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
828                    {
829                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
830    
831                            if (section_list_rw_unlock(p_section[i]) < 0)
832                            {
833                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
834                            }
835                            break;
836                    }
837    
838                    section_first_aid = p_section[i]->p_article_head->aid;
839    
840                    for (j = 0; j < group_count; j++)
841                    {
842                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
843    
844                            if (affected_count < 0)
845                            {
846                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
847                                    break;
848                            }
849    
850                            if (affected_count != BBS_article_limit_per_section / group_count)
851                            {
852                                    printf("move topic (aid = %d) affected article count %d != %d\n",
853                                               section_first_aid + j, affected_count,
854                                               BBS_article_limit_per_section / group_count);
855                                    // break;
856                            }
857                    }
858    
859                    if (section_list_rw_unlock(p_section[i]) < 0)
860                    {
861                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
862                            break;
863                    }
864    
865                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
866                    {
867                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
868    
869                            if (section_list_rw_unlock(p_section[i]) < 0)
870                            {
871                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
872                            }
873                            break;
874                    }
875            }
876    
877            for (i = 0; i < section_count; i++)
878            {
879                    if (section_list_rd_lock(p_section[i]) < 0)
880                    {
881                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
882                            break;
883                    }
884    
885                    if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
886                    {
887                            printf("Topic count error in section %d, %d != %d\n", i,
888                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
889                            break;
890                    }
891    
892                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
893                    {
894                            printf("Visible topic count error in section %d, %d != %d\n", i,
895                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
896                            break;
897                    }
898    
899                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
900                    {
901                            printf("Article count error in section %d, %d != %d\n", i,
902                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
903                            break;
904                    }
905    
906                    if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
907                    {
908                            printf("Visible article count error in section %d, %d != %d\n", i,
909                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
910                            break;
911                    }
912    
913                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
914                    {
915                            printf("Page count error in section %d, %d != %d\n", i,
916                                       p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
917                            break;
918                    }
919    
920                    if (section_list_rd_unlock(p_section[i]) < 0)
921                    {
922                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
923                            break;
924                    }
925            }
926    
927            printf("Testing #6 ...\n");
928    
929            for (i = 0; i < section_count; i++)
930            {
931                    if (section_list_rd_lock(p_section[i]) < 0)
932                    {
933                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
934                            break;
935                    }
936            }
937    
938            printf("Try rw_lock for 5 sec...\n");
939            if (section_list_try_rw_lock(NULL, 5) == 0)
940            {
941                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
942            }
943    
944            for (i = 0; i < section_count; i++)
945            {
946                    if (section_list_rd_unlock(p_section[i]) < 0)
947                    {
948                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
949                            break;
950                    }
951            }
952    
953            if (section_list_try_rw_lock(NULL, 5) < 0)
954            {
955                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
956            }
957    
958            for (i = 0; i < section_count; i++)
959            {
960                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
961                    {
962                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
963                            break;
964                    }
965            }
966    
967            if (section_list_rw_unlock(NULL) < 0)
968            {
969                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
970            }
971    
972          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
973          getchar();          getchar();
974    
975            section_list_cleanup();
976          article_block_cleanup();          article_block_cleanup();
977            trie_dict_cleanup();
978    
979            if (unlink(TRIE_DICT_SHM_FILE) < 0)
980            {
981                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
982                    return -1;
983            }
984    
985            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
986            {
987                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
988                    return -1;
989            }
990    
991            if (unlink(SECTION_LIST_SHM_FILE) < 0)
992            {
993                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
994                    return -1;
995            }
996    
997          log_end();          log_end();
998    


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

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