/[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.15 by sysadm, Fri May 23 10:45:54 2025 UTC Revision 1.39 by sysadm, Wed Nov 5 03:01:14 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                          file_section_list.c  -  description  /*
3                                                           -------------------   * test_section_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - tester for data models and basic operations of section and article
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #include "section_list.h"  
9  #include "bbs.h"  #include "bbs.h"
10  #include "log.h"  #include "log.h"
11    #include "section_list.h"
12    #include "trie_dict.h"
13    #include "user_list.h"
14    #include <errno.h>
15  #include <stdio.h>  #include <stdio.h>
16  #include <unistd.h>  #include <unistd.h>
17  #include <errno.h>  
18    #define ARTICLE_BLOCK_SHM_FILE "~article_block_shm.dat"
19    #define SECTION_LIST_SHM_FILE "~section_list_shm.dat"
20    #define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat"
21    #define USER_LIST_SHM_FILE "~user_list_shm.dat"
22    
23  const char *sname[] = {  const char *sname[] = {
24          "Test",          "Test",
# Line 28  const char *sname[] = { Line 27  const char *sname[] = {
27    
28  const char *stitle[] = {  const char *stitle[] = {
29          " Test Section ",          " Test Section ",
30          "ĸABC",          "字母组合ABC",
31          "__123"};          "_数字_123"};
32    
33  const char *master_name[] = {  const char *master_name[] = {
34          "sysadm",          "sysadm",
# Line 47  int main(int argc, char *argv[]) Line 46  int main(int argc, char *argv[])
46          ARTICLE article;          ARTICLE article;
47          int block_count;          int block_count;
48          int i, j;          int i, j;
49            int sid;
50          int last_aid;          int last_aid;
51          int current_tid;          int current_tid;
52          int section_first_aid;          int section_first_aid;
# Line 56  int main(int argc, char *argv[]) Line 56  int main(int argc, char *argv[])
56          int32_t page;          int32_t page;
57          int32_t offset;          int32_t offset;
58          int affected_count;          int affected_count;
59            FILE *fp;
60    
61          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
62          {          {
# Line 63  int main(int argc, char *argv[]) Line 64  int main(int argc, char *argv[])
64                  return -1;                  return -1;
65          }          }
66    
67          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
68          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
69    
70          // - 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
71          block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_PER_BLOCK;          block_count = BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block;
72    
73            if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL)
74            {
75                    log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
76                    return -1;
77            }
78            fclose(fp);
79    
80            if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL)
81            {
82                    log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE);
83                    return -1;
84            }
85            fclose(fp);
86    
87            if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
88            {
89                    log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
90                    return -1;
91            }
92            fclose(fp);
93    
94            if ((fp = fopen(USER_LIST_SHM_FILE, "w")) == NULL)
95            {
96                    log_error("fopen(%s) error\n", USER_LIST_SHM_FILE);
97                    return -1;
98            }
99            fclose(fp);
100    
101            if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
102            {
103                    printf("trie_dict_init failed\n");
104                    return -1;
105            }
106    
107            if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)
108            {
109                    log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
110                    return -2;
111            }
112    
113          if (article_block_init("../conf/menu.conf", block_count) < 0)          if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
114          {          {
115                  log_error("section_data_pool_init() error\n");                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
116                    return -2;
117            }
118    
119            // Load user_list and online_user_list
120            if (user_list_pool_init(USER_LIST_SHM_FILE) < 0)
121            {
122                    log_error("user_list_pool_init() error\n");
123                  return -2;                  return -2;
124          }          }
125    
# Line 79  int main(int argc, char *argv[]) Line 127  int main(int argc, char *argv[])
127    
128          last_aid = 0;          last_aid = 0;
129    
130            if (section_list_rw_lock(NULL) < 0)
131            {
132                    printf("section_list_rw_lock(sid = %d) error\n", 0);
133            }
134    
135          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
136          {          {
137                  p_section[i] = section_list_create(i + 1,                  sid = i * 3 + 1;
138                    p_section[i] = section_list_create(sid,
139                                                                                     sname[i % section_conf_count],                                                                                     sname[i % section_conf_count],
140                                                                                     stitle[i % section_conf_count],                                                                                     stitle[i % section_conf_count],
141                                                                                     master_name[i % section_conf_count]);                                                                                     master_name[i % section_conf_count]);
142                  if (p_section[i] == NULL)                  if (p_section[i] == NULL)
143                  {                  {
144                          log_error("section_data_create(i=%d) error\n", i);                          printf("section_list_create(i = %d) error\n", i);
145                            return -3;
146                    }
147    
148                    if (get_section_index(p_section[i]) != i)
149                    {
150                            printf("get_section_index(i = %d) error\n", i);
151                          return -3;                          return -3;
152                  }                  }
153          }          }
# Line 96  int main(int argc, char *argv[]) Line 156  int main(int argc, char *argv[])
156          {          {
157                  if (section_list_find_by_name(sname[i]) == NULL)                  if (section_list_find_by_name(sname[i]) == NULL)
158                  {                  {
159                          printf("section_data_find_section_by_name(%s) error\n", sname[i]);                          printf("section_list_find_by_name(%s) error\n", sname[i]);
160                          return -3;                          return -3;
161                  }                  }
162          }          }
163    
164            for (i = 0; i < section_count; i++)
165            {
166                    sid = i * 3 + 1;
167                    if (section_list_find_by_sid(sid) == NULL || section_list_find_by_sid(sid)->sid != sid)
168                    {
169                            printf("section_list_find_by_sid(%d) error\n", sid);
170                            return -3;
171                    }
172            }
173    
174            if (section_list_rw_unlock(NULL) < 0)
175            {
176                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
177            }
178    
179          for (j = 0; j < BBS_article_limit_per_section; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
180          {          {
181                  for (i = 0; i < section_count; i++)                  for (i = 0; i < section_count; i++)
# Line 109  int main(int argc, char *argv[]) Line 184  int main(int argc, char *argv[])
184    
185                          // Set article data                          // Set article data
186                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
187                          article.tid = 0;                          article.tid = 0;
188                          article.uid = 1; // TODO: randomize                          article.sid = i * 3 + 1;
189                            article.cid = article.aid;
190                            article.uid = 1;
191                          article.visible = 1;                          article.visible = 1;
192                          article.excerption = 0;                          article.excerption = 0;
193                          article.ontop = 0;                          article.ontop = 0;
194                          article.lock = 0;                          article.lock = 0;
195                            article.transship = 0;
196    
197                            if (section_list_rw_lock(p_section[i]) < 0)
198                            {
199                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
200                                    break;
201                            }
202    
203                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
204                          {                          {
205                                  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);
206                                  break;                                  break;
207                          }                          }
208    
209                            if (section_list_rw_unlock(p_section[i]) < 0)
210                            {
211                                    printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
212                                    break;
213                            }
214                  }                  }
215          }          }
216    
# Line 130  int main(int argc, char *argv[]) Line 219  int main(int argc, char *argv[])
219                  // 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);
220          }          }
221    
222          last_aid = 0;          if (last_aid != article_block_last_aid())
223            {
224                    printf("last_aid != %d\n", article_block_last_aid());
225            }
226    
227          for (i = 0; i < section_count; i++)          if (article_block_article_count() != section_count * BBS_article_limit_per_section)
228          {          {
229                  if (p_section[i]->article_count == 0)                  printf("article_block_article_count() error %d != %d * %d\n",
230                  {                             article_block_article_count(), section_count, BBS_article_limit_per_section);
231                          continue;          }
232                  }  
233            last_aid = 0;
234    
235                  for (j = 0; j < p_section[i]->article_count; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
236            {
237                    for (i = 0; i < section_count; i++)
238                  {                  {
239                          last_aid++;                          last_aid++;
240    
# Line 149  int main(int argc, char *argv[]) Line 244  int main(int argc, char *argv[])
244                                  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);
245                          }                          }
246    
247                          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)  
248                          {                          {
249                                  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);
250                                    break;
251                          }                          }
252    
253                          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)
254                          {                          {
255                                  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);
256                          }                          }
257    
258                            if (section_list_rw_unlock(p_section[i]) < 0)
259                            {
260                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
261                                    break;
262                            }
263                  }                  }
264    
265                  // 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 166  int main(int argc, char *argv[]) Line 267  int main(int argc, char *argv[])
267    
268          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
269    
270            if (section_list_rw_lock(NULL) < 0)
271            {
272                    printf("section_list_rw_lock(sid = %d) error\n", 0);
273            }
274    
275          if (article_block_reset() != 0)          if (article_block_reset() != 0)
276          {          {
277                  log_error("section_data_free_block(i=%d) error\n", i);                  log_error("article_block_reset() error\n");
278                  return -4;                  return -4;
279          }          }
280    
# Line 177  int main(int argc, char *argv[]) Line 283  int main(int argc, char *argv[])
283                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
284          }          }
285    
286            if (section_list_rw_unlock(NULL) < 0)
287            {
288                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
289            }
290    
291          last_aid = 0;          last_aid = 0;
292    
293          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
294          {          {
295                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
296    
297                    if (section_list_rw_lock(p_section[i]) < 0)
298                    {
299                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
300                            break;
301                    }
302    
303                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
304                  {                  {
305                          last_aid++;                          last_aid++;
306    
307                          // Set article data                          // Set article data
308                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
309                          // Group articles into group_count topics                          // Group articles into group_count topics
310                          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));
311                          article.uid = 1; // TODO: randomize                          article.sid = i * 3 + 1;
312                            article.cid = article.aid;
313                            article.uid = 1;
314                          article.visible = 1;                          article.visible = 1;
315                          article.excerption = 0;                          article.excerption = 0;
316                          article.ontop = 0;                          article.ontop = 0;
317                          article.lock = 0;                          article.lock = 0;
318                            article.transship = 0;
319    
320                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
321                          {                          {
# Line 205  int main(int argc, char *argv[]) Line 324  int main(int argc, char *argv[])
324                          }                          }
325                  }                  }
326    
327                    if (section_list_rw_unlock(p_section[i]) < 0)
328                    {
329                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
330                            break;
331                    }
332    
333                  // 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);
334          }          }
335    
# Line 218  int main(int argc, char *argv[]) Line 343  int main(int argc, char *argv[])
343                  article_count = 0;                  article_count = 0;
344                  last_aid = 0;                  last_aid = 0;
345    
346                    if (section_list_rd_lock(p_section[i]) < 0)
347                    {
348                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
349                            break;
350                    }
351    
352                  p_article = p_section[i]->p_article_head;                  p_article = p_section[i]->p_article_head;
353    
354                  do                  do
# Line 240  int main(int argc, char *argv[]) Line 371  int main(int argc, char *argv[])
371                          break;                          break;
372                  }                  }
373    
374                    if (section_list_rd_unlock(p_section[i]) < 0)
375                    {
376                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
377                            break;
378                    }
379    
380                  // printf("Verified %d articles in section %d\n", group_count, i);                  // printf("Verified %d articles in section %d\n", group_count, i);
381          }          }
382    
# Line 250  int main(int argc, char *argv[]) Line 387  int main(int argc, char *argv[])
387                          continue;                          continue;
388                  }                  }
389    
390                    if (section_list_rd_lock(p_section[i]) < 0)
391                    {
392                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
393                            break;
394                    }
395    
396                  if (p_section[i]->topic_count != group_count)                  if (p_section[i]->topic_count != group_count)
397                  {                  {
398                          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 311  int main(int argc, char *argv[]) Line 454  int main(int argc, char *argv[])
454                          }                          }
455                  }                  }
456    
457                    if (section_list_rd_unlock(p_section[i]) < 0)
458                    {
459                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
460                            break;
461                    }
462    
463                  // printf("Verified %d topics in section %d\n", group_count, i);                  // printf("Verified %d topics in section %d\n", group_count, i);
464          }          }
465    
# Line 320  int main(int argc, char *argv[]) Line 469  int main(int argc, char *argv[])
469          {          {
470                  last_aid = 0;                  last_aid = 0;
471    
472                    if (section_list_rd_lock(p_section[i]) < 0)
473                    {
474                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
475                            break;
476                    }
477    
478                  for (j = 0; j < p_section[i]->page_count; j++)                  for (j = 0; j < p_section[i]->page_count; j++)
479                  {                  {
480                          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 344  int main(int argc, char *argv[]) Line 499  int main(int argc, char *argv[])
499                                  }                                  }
500                          }                          }
501                  }                  }
502    
503                    if (section_list_rd_unlock(p_section[i]) < 0)
504                    {
505                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
506                            break;
507                    }
508          }          }
509    
510          printf("Testing #4 ...\n");          printf("Testing #4 ...\n");
511    
512          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
513          {          {
514                    if (section_list_rw_lock(p_section[i]) < 0)
515                    {
516                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
517                            break;
518                    }
519    
520                  step = i % 10 + 1;                  step = i % 10 + 1;
521                  for (j = group_count; j < BBS_article_limit_per_section; j += step)                  for (j = group_count; j < BBS_article_limit_per_section; j += step)
522                  {                  {
# Line 387  int main(int argc, char *argv[]) Line 554  int main(int argc, char *argv[])
554                                  (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=                                  (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
555                          p_section[i]->page_count)                          p_section[i]->page_count)
556                  {                  {
557                          printf("Inconsistent page count in section %d offset %d, %d != %d, "                          printf("#1 Inconsistent page count in section %d offset %d, %d != %d, "
558                                     "visible_article_count = %d, last_page_visible_count = %d\n",                                     "visible_article_count = %d, last_page_visible_count = %d\n",
559                                     i, j,                                     i, j,
560                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +
# Line 470  int main(int argc, char *argv[]) Line 637  int main(int argc, char *argv[])
637                                  (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=                                  (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
638                          p_section[i]->page_count)                          p_section[i]->page_count)
639                  {                  {
640                          printf("Inconsistent page count in section %d offset %d, %d != %d, "                          printf("#2 Inconsistent page count in section %d offset %d, %d != %d, "
641                                     "visible_article_count = %d, last_page_visible_count = %d\n",                                     "visible_article_count = %d, last_page_visible_count = %d\n",
642                                     i, j,                                     i, j,
643                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +
# Line 479  int main(int argc, char *argv[]) Line 646  int main(int argc, char *argv[])
646                                     p_section[i]->last_page_visible_article_count);                                     p_section[i]->last_page_visible_article_count);
647                          break;                          break;
648                  }                  }
649    
650                    if (section_list_rw_unlock(p_section[i]) < 0)
651                    {
652                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
653                            break;
654                    }
655          }          }
656    
657          for (i = 0; i < BBS_max_section; i++)          for (i = 0; i < BBS_max_section; i++)
658          {          {
659                    if (section_list_rw_lock(p_section[i]) < 0)
660                    {
661                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
662                            break;
663                    }
664    
665                  affected_count = 0;                  affected_count = 0;
666    
667                  for (j = 0; j < BBS_article_limit_per_section; j += 1)                  for (j = 0; j < BBS_article_limit_per_section; j += 1)
# Line 527  int main(int argc, char *argv[]) Line 706  int main(int argc, char *argv[])
706                                  (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=                                  (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
707                          p_section[i]->page_count)                          p_section[i]->page_count)
708                  {                  {
709                          printf("Inconsistent page count in section %d offset %d, %d != %d, "                          printf("#3 Inconsistent page count in section %d offset %d, %d != %d, "
710                                     "visible_article_count = %d, last_page_visible_count = %d\n",                                     "visible_article_count = %d, last_page_visible_count = %d\n",
711                                     i, j,                                     i, j,
712                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +                                     p_section[i]->visible_article_count / BBS_article_limit_per_page +
# Line 536  int main(int argc, char *argv[]) Line 715  int main(int argc, char *argv[])
715                                     p_section[i]->last_page_visible_article_count);                                     p_section[i]->last_page_visible_article_count);
716                          break;                          break;
717                  }                  }
718    
719                    if (section_list_rw_unlock(p_section[i]) < 0)
720                    {
721                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
722                            break;
723                    }
724          }          }
725    
726          printf("Testing #5 ...\n");          printf("Testing #5 ...\n");
727    
728            if (section_list_rw_lock(NULL) < 0)
729            {
730                    printf("section_list_rw_lock(sid = %d) error\n", 0);
731            }
732    
733          if (article_block_reset() != 0)          if (article_block_reset() != 0)
734          {          {
735                  log_error("section_data_free_block(i=%d) error\n", i);                  log_error("article_block_reset() error\n");
736                  return -4;                  return -4;
737          }          }
738    
# Line 551  int main(int argc, char *argv[]) Line 741  int main(int argc, char *argv[])
741                  section_list_reset_articles(p_section[i]);                  section_list_reset_articles(p_section[i]);
742          }          }
743    
744            if (section_list_rw_unlock(NULL) < 0)
745            {
746                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
747            }
748    
749          last_aid = 0;          last_aid = 0;
750    
751          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
752          {          {
753                    if (section_list_rw_lock(p_section[i]) < 0)
754                    {
755                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
756                            break;
757                    }
758    
759                  section_first_aid = last_aid + 1;                  section_first_aid = last_aid + 1;
760    
761                  for (j = 0; j < BBS_article_limit_per_section; j++)                  for (j = 0; j < BBS_article_limit_per_section; j++)
# Line 563  int main(int argc, char *argv[]) Line 764  int main(int argc, char *argv[])
764    
765                          // Set article data                          // Set article data
766                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
767                          // Group articles into group_count topics                          // Group articles into group_count topics
768                          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));
769                          article.uid = 1; // TODO: randomize                          article.sid = i * 3 + 1;
770                            article.cid = article.aid;
771                            article.uid = 1;
772                          article.visible = 1;                          article.visible = 1;
773                          article.excerption = 0;                          article.excerption = 0;
774                          article.ontop = 0;                          article.ontop = 0;
775                          article.lock = 0;                          article.lock = 0;
776                            article.transship = 0;
777    
778                          if (section_list_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
779                          {                          {
# Line 579  int main(int argc, char *argv[]) Line 782  int main(int argc, char *argv[])
782                          }                          }
783                  }                  }
784    
785                    if (section_list_rw_unlock(p_section[i]) < 0)
786                    {
787                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
788                            break;
789                    }
790    
791                  // 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);
792          }          }
793    
794          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
795          {          {
796                    if (section_list_rw_lock(p_section[i]) < 0)
797                    {
798                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
799                            break;
800                    }
801    
802                  section_first_aid = p_section[i]->p_article_head->aid;                  section_first_aid = p_section[i]->p_article_head->aid;
803    
804                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j += 2)
805                  {                  {
806                          p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);                          p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
807                          if (p_article == NULL)                          if (p_article == NULL)
# Line 595  int main(int argc, char *argv[]) Line 810  int main(int argc, char *argv[])
810                                             section_first_aid + j, i);                                             section_first_aid + j, i);
811                                  break;                                  break;
812                          }                          }
813    
814                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
815                            {
816                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
817                            }
818                    }
819    
820                    if (section_list_rw_unlock(p_section[i]) < 0)
821                    {
822                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
823                            break;
824                  }                  }
825          }          }
826    
827          for (i = 0; i < section_count / 2; i++)          for (i = 0; i < section_count / 2; i++)
828          {          {
829                    if (section_list_rw_lock(p_section[i]) < 0)
830                    {
831                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
832                            break;
833                    }
834    
835                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
836                    {
837                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
838    
839                            if (section_list_rw_unlock(p_section[i]) < 0)
840                            {
841                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
842                            }
843                            break;
844                    }
845    
846                  section_first_aid = p_section[i]->p_article_head->aid;                  section_first_aid = p_section[i]->p_article_head->aid;
847    
848                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
# Line 620  int main(int argc, char *argv[]) Line 863  int main(int argc, char *argv[])
863                                  // break;                                  // break;
864                          }                          }
865                  }                  }
866    
867                    if (section_list_rw_unlock(p_section[i]) < 0)
868                    {
869                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
870                            break;
871                    }
872    
873                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
874                    {
875                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
876    
877                            if (section_list_rw_unlock(p_section[i]) < 0)
878                            {
879                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
880                            }
881                            break;
882                    }
883          }          }
884    
885          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
886          {          {
887                    if (section_list_rd_lock(p_section[i]) < 0)
888                    {
889                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
890                            break;
891                    }
892    
893                  if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))                  if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
894                  {                  {
895                          printf("Topic count error in section %d, %d != %d\n", i,                          printf("Topic count error in section %d, %d != %d\n", i,
# Line 631  int main(int argc, char *argv[]) Line 897  int main(int argc, char *argv[])
897                          break;                          break;
898                  }                  }
899    
900                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
901                    {
902                            printf("Visible topic count error in section %d, %d != %d\n", i,
903                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
904                            break;
905                    }
906    
907                  if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))                  if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
908                  {                  {
909                          printf("Article count error in section %d, %d != %d\n", i,                          printf("Article count error in section %d, %d != %d\n", i,
# Line 638  int main(int argc, char *argv[]) Line 911  int main(int argc, char *argv[])
911                          break;                          break;
912                  }                  }
913    
914                  if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / BBS_article_limit_per_page))                  if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
915                    {
916                            printf("Visible article count error in section %d, %d != %d\n", i,
917                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
918                            break;
919                    }
920    
921                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
922                  {                  {
923                          printf("Page count error in section %d, %d != %d\n", i,                          printf("Page count error in section %d, %d != %d\n", i,
924                                     p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / BBS_article_limit_per_page));                                     p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
925                            break;
926                    }
927    
928                    if (section_list_rd_unlock(p_section[i]) < 0)
929                    {
930                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
931                            break;
932                    }
933            }
934    
935            printf("Testing #6 ...\n");
936    
937            for (i = 0; i < section_count; i++)
938            {
939                    if (section_list_rd_lock(p_section[i]) < 0)
940                    {
941                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
942                            break;
943                    }
944            }
945    
946            printf("Try rw_lock for 5 sec...\n");
947            if (section_list_try_rw_lock(NULL, 5) == 0)
948            {
949                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
950            }
951    
952            for (i = 0; i < section_count; i++)
953            {
954                    if (section_list_rd_unlock(p_section[i]) < 0)
955                    {
956                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
957                            break;
958                    }
959            }
960    
961            if (section_list_try_rw_lock(NULL, 5) < 0)
962            {
963                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
964            }
965    
966            for (i = 0; i < section_count; i++)
967            {
968                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
969                    {
970                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
971                          break;                          break;
972                  }                  }
973          }          }
974    
975            if (section_list_rw_unlock(NULL) < 0)
976            {
977                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
978            }
979    
980          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
981          getchar();          getchar();
982    
983            user_list_pool_cleanup();
984            section_list_cleanup();
985          article_block_cleanup();          article_block_cleanup();
986            trie_dict_cleanup();
987    
988            if (unlink(USER_LIST_SHM_FILE) < 0)
989            {
990                    log_error("unlink(%s) error\n", USER_LIST_SHM_FILE);
991                    return -1;
992            }
993    
994            if (unlink(TRIE_DICT_SHM_FILE) < 0)
995            {
996                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
997                    return -1;
998            }
999    
1000            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
1001            {
1002                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
1003                    return -1;
1004            }
1005    
1006            if (unlink(SECTION_LIST_SHM_FILE) < 0)
1007            {
1008                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
1009                    return -1;
1010            }
1011    
1012          log_end();          log_end();
1013    


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

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