/[LeafOK_CVS]/lbbs/src/test_section_list.c
ViewVC logotype

Diff of /lbbs/src/test_section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.10 by sysadm, Thu May 22 11:10:19 2025 UTC Revision 1.36 by sysadm, Mon Nov 3 15:05:02 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                          file_section_list.c  -  description                                          test_section_list.c  -  description
3                                                           -------------------                                                           -------------------
4          Copyright            : (C) 2004-2025 by Leaflet          Copyright            : (C) 2004-2025 by Leaflet
5          Email                : leaflet@leafok.com          Email                : leaflet@leafok.com
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "section_list.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "log.h"  #include "log.h"
19    #include "section_list.h"
20    #include "trie_dict.h"
21    #include "user_list.h"
22    #include <errno.h>
23  #include <stdio.h>  #include <stdio.h>
24  #include <unistd.h>  #include <unistd.h>
25  #include <errno.h>  
26    #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
27    #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
28    #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
29    #define USER_LIST_SHM_FILE "~user_list_shm.dat"
30    
31  const char *sname[] = {  const char *sname[] = {
32          "Test",          "Test",
# Line 28  const char *sname[] = { Line 35  const char *sname[] = {
35    
36  const char *stitle[] = {  const char *stitle[] = {
37          " Test Section ",          " Test Section ",
38          "ĸABC",          "字母组合ABC",
39          "__123"};          "_数字_123"};
40    
41  const char *master_name[] = {  const char *master_name[] = {
42          "sysadm",          "sysadm",
# Line 43  int main(int argc, char *argv[]) Line 50  int main(int argc, char *argv[])
50  {  {
51          SECTION_LIST *p_section[BBS_max_section];          SECTION_LIST *p_section[BBS_max_section];
52          ARTICLE *p_article;          ARTICLE *p_article;
53            ARTICLE *p_next;
54          ARTICLE article;          ARTICLE article;
55          int block_count;          int block_count;
56          int i, j;          int i, j;
57            int sid;
58          int last_aid;          int last_aid;
59          int current_tid;          int current_tid;
60          int section_first_aid;          int section_first_aid;
# Line 55  int main(int argc, char *argv[]) Line 64  int main(int argc, char *argv[])
64          int32_t page;          int32_t page;
65          int32_t offset;          int32_t offset;
66          int affected_count;          int affected_count;
67            FILE *fp;
68    
69          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
70          {          {
# Line 62  int main(int argc, char *argv[]) Line 72  int main(int argc, char *argv[])
72                  return -1;                  return -1;
73          }          }
74    
75          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
76          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
77    
78          // - 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
79          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;
80    
81            if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL)
82            {
83                    log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
84                    return -1;
85            }
86            fclose(fp);
87    
88            if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL)
89            {
90                    log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE);
91                    return -1;
92            }
93            fclose(fp);
94    
95            if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
96            {
97                    log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
98                    return -1;
99            }
100            fclose(fp);
101    
102            if ((fp = fopen(USER_LIST_SHM_FILE, "w")) == NULL)
103            {
104                    log_error("fopen(%s) error\n", USER_LIST_SHM_FILE);
105                    return -1;
106            }
107            fclose(fp);
108    
109            if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
110            {
111                    printf("trie_dict_init failed\n");
112                    return -1;
113            }
114    
115            if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)
116            {
117                    log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
118                    return -2;
119            }
120    
121            if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
122            {
123                    log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
124                    return -2;
125            }
126    
127          if (article_block_init("../conf/menu.conf", block_count) < 0)          // Load user_list and online_user_list
128            if (user_list_pool_init(USER_LIST_SHM_FILE) < 0)
129          {          {
130                  log_error("section_data_pool_init() error\n");                  log_error("user_list_pool_init() error\n");
131                  return -2;                  return -2;
132          }          }
133    
# Line 78  int main(int argc, char *argv[]) Line 135  int main(int argc, char *argv[])
135    
136          last_aid = 0;          last_aid = 0;
137    
138            if (section_list_rw_lock(NULL) < 0)
139            {
140                    printf("section_list_rw_lock(sid = %d) error\n", 0);
141            }
142    
143          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
144          {          {
145                  p_section[i] = section_list_create(sname[i % section_conf_count],                  sid = i * 3 + 1;
146                    p_section[i] = section_list_create(sid,
147                                                                                       sname[i % section_conf_count],
148                                                                                     stitle[i % section_conf_count],                                                                                     stitle[i % section_conf_count],
149                                                                                     master_name[i % section_conf_count]);                                                                                     master_name[i % section_conf_count]);
150                  if (p_section[i] == NULL)                  if (p_section[i] == NULL)
151                  {                  {
152                          log_error("section_data_create(i=%d) error\n", i);                          printf("section_list_create(i = %d) error\n", i);
153                            return -3;
154                    }
155    
156                    if (get_section_index(p_section[i]) != i)
157                    {
158                            printf("get_section_index(i = %d) error\n", i);
159                          return -3;                          return -3;
160                  }                  }
161          }          }
# Line 94  int main(int argc, char *argv[]) Line 164  int main(int argc, char *argv[])
164          {          {
165                  if (section_list_find_by_name(sname[i]) == NULL)                  if (section_list_find_by_name(sname[i]) == NULL)
166                  {                  {
167                          printf("section_data_find_section_by_name(%s) error\n", sname[i]);                          printf("section_list_find_by_name(%s) error\n", sname[i]);
168                            return -3;
169                    }
170            }
171    
172            for (i = 0; i < section_count; i++)
173            {
174                    sid = i * 3 + 1;
175                    if (section_list_find_by_sid(sid) == NULL || section_list_find_by_sid(sid)->sid != sid)
176                    {
177                            printf("section_list_find_by_sid(%d) error\n", sid);
178                          return -3;                          return -3;
179                  }                  }
180          }          }
181    
182            if (section_list_rw_unlock(NULL) < 0)
183            {
184                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
185            }
186    
187          for (j = 0; j < BBS_article_limit_per_section; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
188          {          {
189                  for (i = 0; i < section_count; i++)                  for (i = 0; i < section_count; i++)
# Line 107  int main(int argc, char *argv[]) Line 192  int main(int argc, char *argv[])
192    
193                          // Set article data                          // Set article data
194                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
195                          article.tid = 0;                          article.tid = 0;
196                          article.uid = 1; // TODO: randomize                          article.sid = i * 3 + 1;
197                            article.cid = article.aid;
198                            article.uid = 1;
199                          article.visible = 1;                          article.visible = 1;
200                          article.excerption = 0;                          article.excerption = 0;
201                          article.ontop = 0;                          article.ontop = 0;
202                          article.lock = 0;                          article.lock = 0;
203                            article.transship = 0;
204    
205                            if (section_list_rw_lock(p_section[i]) < 0)
206                            {
207                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
208                                    break;
209                            }
210    
211                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
212                          {                          {
213                                  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);
214                                  break;                                  break;
215                          }                          }
216    
217                            if (section_list_rw_unlock(p_section[i]) < 0)
218                            {
219                                    printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
220                                    break;
221                            }
222                  }                  }
223          }          }
224    
# Line 128  int main(int argc, char *argv[]) Line 227  int main(int argc, char *argv[])
227                  // 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);
228          }          }
229    
230          last_aid = 0;          if (last_aid != article_block_last_aid())
231            {
232                    printf("last_aid != %d\n", article_block_last_aid());
233            }
234    
235          for (i = 0; i < section_count; i++)          if (article_block_article_count() != section_count * BBS_article_limit_per_section)
236          {          {
237                  if (p_section[i]->article_count == 0)                  printf("article_block_article_count() error %d != %d * %d\n",
238                  {                             article_block_article_count(), section_count, BBS_article_limit_per_section);
239                          continue;          }
                 }  
240    
241                  for (j = 0; j < p_section[i]->article_count; j++)          last_aid = 0;
242    
243            for (j = 0; j < BBS_article_limit_per_section; j++)
244            {
245                    for (i = 0; i < section_count; i++)
246                  {                  {
247                          last_aid++;                          last_aid++;
248    
# Line 147  int main(int argc, char *argv[]) Line 252  int main(int argc, char *argv[])
252                                  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);
253                          }                          }
254    
255                          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)  
256                          {                          {
257                                  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);
258                                    break;
259                          }                          }
260    
261                          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)
262                          {                          {
263                                  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);
264                          }                          }
265    
266                            if (section_list_rw_unlock(p_section[i]) < 0)
267                            {
268                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
269                                    break;
270                            }
271                  }                  }
272    
273                  // 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 275  int main(int argc, char *argv[])
275    
276          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
277    
278            if (section_list_rw_lock(NULL) < 0)
279            {
280                    printf("section_list_rw_lock(sid = %d) error\n", 0);
281            }
282    
283          if (article_block_reset() != 0)          if (article_block_reset() != 0)
284          {          {
285                  log_error("section_data_free_block(i=%d) error\n", i);                  log_error("article_block_reset() error\n");
286                  return -4;                  return -4;
287          }          }
288    
# Line 175  int main(int argc, char *argv[]) Line 291  int main(int argc, char *argv[])
291                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
292          }          }
293    
294            if (section_list_rw_unlock(NULL) < 0)
295            {
296                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
297            }
298    
299          last_aid = 0;          last_aid = 0;
300    
301          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
302          {          {
303                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
304    
305                    if (section_list_rw_lock(p_section[i]) < 0)
306                    {
307                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
308                            break;
309                    }
310    
311                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
312                  {                  {
313                          last_aid++;                          last_aid++;
314    
315                          // Set article data                          // Set article data
316                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
317                          // Group articles into group_count topics                          // Group articles into group_count topics
318                          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));
319                          article.uid = 1; // TODO: randomize                          article.sid = i * 3 + 1;
320                            article.cid = article.aid;
321                            article.uid = 1;
322                          article.visible = 1;                          article.visible = 1;
323                          article.excerption = 0;                          article.excerption = 0;
324                          article.ontop = 0;                          article.ontop = 0;
325                          article.lock = 0;                          article.lock = 0;
326                            article.transship = 0;
327    
328                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
329                          {                          {
# Line 203  int main(int argc, char *argv[]) Line 332  int main(int argc, char *argv[])
332                          }                          }
333                  }                  }
334    
335                    if (section_list_rw_unlock(p_section[i]) < 0)
336                    {
337                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
338                            break;
339                    }
340    
341                  // 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);
342          }          }
343    
# Line 216  int main(int argc, char *argv[]) Line 351  int main(int argc, char *argv[])
351                  article_count = 0;                  article_count = 0;
352                  last_aid = 0;                  last_aid = 0;
353    
354                    if (section_list_rd_lock(p_section[i]) < 0)
355                    {
356                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
357                            break;
358                    }
359    
360                  p_article = p_section[i]->p_article_head;                  p_article = p_section[i]->p_article_head;
361    
362                  do                  do
# Line 238  int main(int argc, char *argv[]) Line 379  int main(int argc, char *argv[])
379                          break;                          break;
380                  }                  }
381    
382                    if (section_list_rd_unlock(p_section[i]) < 0)
383                    {
384                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
385                            break;
386                    }
387    
388                  // printf("Verified %d articles in section %d\n", group_count, i);                  // printf("Verified %d articles in section %d\n", group_count, i);
389          }          }
390    
# Line 248  int main(int argc, char *argv[]) Line 395  int main(int argc, char *argv[])
395                          continue;                          continue;
396                  }                  }
397    
398                    if (section_list_rd_lock(p_section[i]) < 0)
399                    {
400                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
401                            break;
402                    }
403    
404                  if (p_section[i]->topic_count != group_count)                  if (p_section[i]->topic_count != group_count)
405                  {                  {
406                          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 458  int main(int argc, char *argv[])
458                          {                          {
459                                  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",
460                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
461                                  break;                                  // break;
462                          }                          }
463                  }                  }
464    
465                    if (section_list_rd_unlock(p_section[i]) < 0)
466                    {
467                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
468                            break;
469                    }
470    
471                  // printf("Verified %d topics in section %d\n", group_count, i);                  // printf("Verified %d topics in section %d\n", group_count, i);
472          }          }
473    
# Line 318  int main(int argc, char *argv[]) Line 477  int main(int argc, char *argv[])
477          {          {
478                  last_aid = 0;                  last_aid = 0;
479    
480                    if (section_list_rd_lock(p_section[i]) < 0)
481                    {
482                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
483                            break;
484                    }
485    
486                  for (j = 0; j < p_section[i]->page_count; j++)                  for (j = 0; j < p_section[i]->page_count; j++)
487                  {                  {
488                          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 507  int main(int argc, char *argv[])
507                                  }                                  }
508                          }                          }
509                  }                  }
510    
511                    if (section_list_rd_unlock(p_section[i]) < 0)
512                    {
513                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
514                            break;
515                    }
516          }          }
517    
518          printf("Testing #4 ...\n");          printf("Testing #4 ...\n");
519    
520          for (i = 0; i < BBS_max_section; i++)          for (i = 0; i < section_count; i++)
521          {          {
522                    if (section_list_rw_lock(p_section[i]) < 0)
523                    {
524                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
525                            break;
526                    }
527    
528                  step = i % 10 + 1;                  step = i % 10 + 1;
529                  for (j = group_count; j < BBS_article_limit_per_section; j += step)                  for (j = group_count; j < BBS_article_limit_per_section; j += step)
530                  {                  {
531                          last_aid = i * BBS_article_limit_per_section + j + 1;                          last_aid = i * BBS_article_limit_per_section + j + 1;
532    
533                          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);
534    
535                          if (p_article == NULL)                          if (p_article == NULL)
536                          {                          {
# Line 367  int main(int argc, char *argv[]) Line 544  int main(int argc, char *argv[])
544                                  break;                                  break;
545                          }                          }
546    
                         if (page * BBS_article_limit_per_page + offset != j)  
                         {  
                                 printf("Inconsistent article offset %d in section %d page %d offset %d\n", j, i, page, offset);  
                                 break;  
                         }  
   
547                          if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)                          if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
548                          {                          {
549                                  printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);                                  printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
# Line 380  int main(int argc, char *argv[]) Line 551  int main(int argc, char *argv[])
551                          }                          }
552                  }                  }
553    
554                    last_aid = p_section[i]->p_article_head->aid;
555                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
556                    {
557                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
558                            break;
559                    }
560    
561                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
562                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
563                            p_section[i]->page_count)
564                    {
565                            printf("#1 Inconsistent page count in section %d offset %d, %d != %d, "
566                                       "visible_article_count = %d, last_page_visible_count = %d\n",
567                                       i, j,
568                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
569                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
570                                       p_section[i]->page_count, p_section[i]->visible_article_count,
571                                       p_section[i]->last_page_visible_article_count);
572                            break;
573                    }
574    
575                  affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);                  affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
576                  if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)                  if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
577                  {                  {
# Line 395  int main(int argc, char *argv[]) Line 587  int main(int argc, char *argv[])
587                  {                  {
588                          last_aid = i * BBS_article_limit_per_section + j + 1;                          last_aid = i * BBS_article_limit_per_section + j + 1;
589    
590                          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);
591    
592                          if (p_article == NULL)                          if (p_article == NULL)
593                          {                          {
# Line 432  int main(int argc, char *argv[]) Line 624  int main(int argc, char *argv[])
624    
625                  if (p_section[i]->visible_article_count > 0)                  if (p_section[i]->visible_article_count > 0)
626                  {                  {
627                          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);
628                            break;
629                    }
630    
631                    if (p_section[i]->visible_topic_count > 0)
632                    {
633                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
634                            break;
635                    }
636    
637                    last_aid = p_section[i]->p_article_head->aid;
638                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
639                    {
640                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
641                            break;
642                    }
643    
644                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
645                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
646                            p_section[i]->page_count)
647                    {
648                            printf("#2 Inconsistent page count in section %d offset %d, %d != %d, "
649                                       "visible_article_count = %d, last_page_visible_count = %d\n",
650                                       i, j,
651                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
652                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
653                                       p_section[i]->page_count, p_section[i]->visible_article_count,
654                                       p_section[i]->last_page_visible_article_count);
655                            break;
656                    }
657    
658                    if (section_list_rw_unlock(p_section[i]) < 0)
659                    {
660                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
661                          break;                          break;
662                  }                  }
663          }          }
664    
665            for (i = 0; i < BBS_max_section; i++)
666            {
667                    if (section_list_rw_lock(p_section[i]) < 0)
668                    {
669                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
670                            break;
671                    }
672    
673                    affected_count = 0;
674    
675                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
676                    {
677                            last_aid = i * BBS_article_limit_per_section + j + 1;
678    
679                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
680                            {
681                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
682                                    break;
683                            }
684    
685                            affected_count++;
686                    }
687    
688                    if (affected_count != p_section[i]->article_count)
689                    {
690                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
691                            break;
692                    }
693    
694                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
695                    {
696                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
697                            break;
698                    }
699    
700                    if (p_section[i]->visible_topic_count != group_count)
701                    {
702                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
703                            break;
704                    }
705    
706                    last_aid = p_section[i]->p_article_head->aid;
707                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
708                    {
709                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
710                            break;
711                    }
712    
713                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
714                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
715                            p_section[i]->page_count)
716                    {
717                            printf("#3 Inconsistent page count in section %d offset %d, %d != %d, "
718                                       "visible_article_count = %d, last_page_visible_count = %d\n",
719                                       i, j,
720                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
721                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
722                                       p_section[i]->page_count, p_section[i]->visible_article_count,
723                                       p_section[i]->last_page_visible_article_count);
724                            break;
725                    }
726    
727                    if (section_list_rw_unlock(p_section[i]) < 0)
728                    {
729                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
730                            break;
731                    }
732            }
733    
734            printf("Testing #5 ...\n");
735    
736            if (section_list_rw_lock(NULL) < 0)
737            {
738                    printf("section_list_rw_lock(sid = %d) error\n", 0);
739            }
740    
741            if (article_block_reset() != 0)
742            {
743                    log_error("article_block_reset() error\n");
744                    return -4;
745            }
746    
747            for (i = 0; i < section_count; i++)
748            {
749                    section_list_reset_articles(p_section[i]);
750            }
751    
752            if (section_list_rw_unlock(NULL) < 0)
753            {
754                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
755            }
756    
757            last_aid = 0;
758    
759            for (i = 0; i < section_count / 2; i++)
760            {
761                    if (section_list_rw_lock(p_section[i]) < 0)
762                    {
763                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
764                            break;
765                    }
766    
767                    section_first_aid = last_aid + 1;
768    
769                    for (j = 0; j < BBS_article_limit_per_section; j++)
770                    {
771                            last_aid++;
772    
773                            // Set article data
774                            article.aid = last_aid;
775                            // Group articles into group_count topics
776                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
777                            article.sid = i * 3 + 1;
778                            article.cid = article.aid;
779                            article.uid = 1;
780                            article.visible = 1;
781                            article.excerption = 0;
782                            article.ontop = 0;
783                            article.lock = 0;
784                            article.transship = 0;
785    
786                            if (section_list_append_article(p_section[i], &article) < 0)
787                            {
788                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
789                                    break;
790                            }
791                    }
792    
793                    if (section_list_rw_unlock(p_section[i]) < 0)
794                    {
795                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
796                            break;
797                    }
798    
799                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
800            }
801    
802            for (i = 0; i < section_count / 2; i++)
803            {
804                    if (section_list_rw_lock(p_section[i]) < 0)
805                    {
806                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
807                            break;
808                    }
809    
810                    section_first_aid = p_section[i]->p_article_head->aid;
811    
812                    for (j = 0; j < group_count; j += 2)
813                    {
814                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
815                            if (p_article == NULL)
816                            {
817                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
818                                               section_first_aid + j, i);
819                                    break;
820                            }
821    
822                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
823                            {
824                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
825                            }
826                    }
827    
828                    if (section_list_rw_unlock(p_section[i]) < 0)
829                    {
830                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
831                            break;
832                    }
833            }
834    
835            for (i = 0; i < section_count / 2; i++)
836            {
837                    if (section_list_rw_lock(p_section[i]) < 0)
838                    {
839                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
840                            break;
841                    }
842    
843                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
844                    {
845                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
846    
847                            if (section_list_rw_unlock(p_section[i]) < 0)
848                            {
849                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
850                            }
851                            break;
852                    }
853    
854                    section_first_aid = p_section[i]->p_article_head->aid;
855    
856                    for (j = 0; j < group_count; j++)
857                    {
858                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
859    
860                            if (affected_count < 0)
861                            {
862                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
863                                    break;
864                            }
865    
866                            if (affected_count != BBS_article_limit_per_section / group_count)
867                            {
868                                    printf("move topic (aid = %d) affected article count %d != %d\n",
869                                               section_first_aid + j, affected_count,
870                                               BBS_article_limit_per_section / group_count);
871                                    // break;
872                            }
873                    }
874    
875                    if (section_list_rw_unlock(p_section[i]) < 0)
876                    {
877                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
878                            break;
879                    }
880    
881                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
882                    {
883                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
884    
885                            if (section_list_rw_unlock(p_section[i]) < 0)
886                            {
887                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
888                            }
889                            break;
890                    }
891            }
892    
893            for (i = 0; i < section_count; i++)
894            {
895                    if (section_list_rd_lock(p_section[i]) < 0)
896                    {
897                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
898                            break;
899                    }
900    
901                    if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
902                    {
903                            printf("Topic count error in section %d, %d != %d\n", i,
904                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
905                            break;
906                    }
907    
908                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
909                    {
910                            printf("Visible topic count error in section %d, %d != %d\n", i,
911                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
912                            break;
913                    }
914    
915                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
916                    {
917                            printf("Article count error in section %d, %d != %d\n", i,
918                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
919                            break;
920                    }
921    
922                    if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
923                    {
924                            printf("Visible article count error in section %d, %d != %d\n", i,
925                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
926                            break;
927                    }
928    
929                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
930                    {
931                            printf("Page count error in section %d, %d != %d\n", i,
932                                       p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
933                            break;
934                    }
935    
936                    if (section_list_rd_unlock(p_section[i]) < 0)
937                    {
938                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
939                            break;
940                    }
941            }
942    
943            printf("Testing #6 ...\n");
944    
945            for (i = 0; i < section_count; i++)
946            {
947                    if (section_list_rd_lock(p_section[i]) < 0)
948                    {
949                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
950                            break;
951                    }
952            }
953    
954            printf("Try rw_lock for 5 sec...\n");
955            if (section_list_try_rw_lock(NULL, 5) == 0)
956            {
957                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
958            }
959    
960            for (i = 0; i < section_count; i++)
961            {
962                    if (section_list_rd_unlock(p_section[i]) < 0)
963                    {
964                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
965                            break;
966                    }
967            }
968    
969            if (section_list_try_rw_lock(NULL, 5) < 0)
970            {
971                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
972            }
973    
974            for (i = 0; i < section_count; i++)
975            {
976                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
977                    {
978                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
979                            break;
980                    }
981            }
982    
983            if (section_list_rw_unlock(NULL) < 0)
984            {
985                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
986            }
987    
988          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
989          getchar();          getchar();
990    
991            user_list_pool_cleanup();
992            section_list_cleanup();
993          article_block_cleanup();          article_block_cleanup();
994            trie_dict_cleanup();
995    
996            if (unlink(USER_LIST_SHM_FILE) < 0)
997            {
998                    log_error("unlink(%s) error\n", USER_LIST_SHM_FILE);
999                    return -1;
1000            }
1001    
1002            if (unlink(TRIE_DICT_SHM_FILE) < 0)
1003            {
1004                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
1005                    return -1;
1006            }
1007    
1008            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
1009            {
1010                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
1011                    return -1;
1012            }
1013    
1014            if (unlink(SECTION_LIST_SHM_FILE) < 0)
1015            {
1016                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
1017                    return -1;
1018            }
1019    
1020          log_end();          log_end();
1021    


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

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