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

Diff of /lbbs/src/test_section_list.c

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

Revision 1.9 by sysadm, Thu May 22 06:20:47 2025 UTC Revision 1.37 by sysadm, Tue Nov 4 13:49:51 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 by 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 43  int main(int argc, char *argv[]) Line 42  int main(int argc, char *argv[])
42  {  {
43          SECTION_LIST *p_section[BBS_max_section];          SECTION_LIST *p_section[BBS_max_section];
44          ARTICLE *p_article;          ARTICLE *p_article;
45            ARTICLE *p_next;
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;
53          int group_count;          int group_count = 100;
54          int article_count;          int article_count;
55            int step;
56            int32_t page;
57            int32_t offset;
58            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 58  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 - 1;          block_count = BBS_article_limit_per_section * BBS_max_section / ARTICLE_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 74  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(sname[i % section_conf_count],                  sid = i * 3 + 1;
138                    p_section[i] = section_list_create(sid,
139                                                                                       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;                          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;
152                    }
153            }
154    
155            for (i = 0; i < section_conf_count; i++)
156            {
157                    if (section_list_find_by_name(sname[i]) == NULL)
158                    {
159                            printf("section_list_find_by_name(%s) error\n", sname[i]);
160                            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++)
# Line 94  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 115  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                  for (j = 0; j < p_section[i]->article_count; j++)          last_aid = 0;
234    
235            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 134  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 151  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 162  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          group_count = 100;          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 191  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 204  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 226  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 236  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)
397                    {
398                            printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
399                    }
400    
401                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
402                  {                  {
403                          last_aid = p_section[i]->p_article_head->aid + j;                          last_aid = p_section[i]->p_article_head->aid + j;
# Line 288  int main(int argc, char *argv[]) Line 450  int main(int argc, char *argv[])
450                          {                          {
451                                  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",
452                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
453                                  break;                                  // break;
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    
466          printf("Testing #3 ...\n");          printf("Testing #3 ...\n");
467    
468          for (i = 0; i < section_conf_count; i++)          for (i = 0; i < section_count; i++)
469          {          {
470                  if (section_list_find_by_name(sname[i]) == NULL)                  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++)
479                    {
480                            if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
481                            {
482                                    printf("Non-ascending aid found at section %d page %d, %d <= %d\n", i, j, p_section[i]->p_page_first_article[j]->aid, last_aid);
483                            }
484    
485                            if (j > 0)
486                            {
487                                    step = 0;
488    
489                                    for (p_article = p_section[i]->p_page_first_article[j];
490                                             p_article != p_section[i]->p_page_first_article[j - 1];
491                                             p_article = p_article->p_prior)
492                                    {
493                                            step++;
494                                    }
495    
496                                    if (step != BBS_article_limit_per_page)
497                                    {
498                                            printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
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");
511    
512            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;
521                    for (j = group_count; j < BBS_article_limit_per_section; j += step)
522                    {
523                            last_aid = i * BBS_article_limit_per_section + j + 1;
524    
525                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
526    
527                            if (p_article == NULL)
528                            {
529                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
530                                    break;
531                            }
532    
533                            if (p_article->aid != last_aid)
534                            {
535                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
536                                    break;
537                            }
538    
539                            if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
540                            {
541                                    printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
542                                    break;
543                            }
544                    }
545    
546                    last_aid = p_section[i]->p_article_head->aid;
547                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
548                    {
549                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
550                            break;
551                    }
552    
553                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
554                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
555                            p_section[i]->page_count)
556                    {
557                            printf("#1 Inconsistent page count in section %d offset %d, %d != %d, "
558                                       "visible_article_count = %d, last_page_visible_count = %d\n",
559                                       i, j,
560                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
561                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
562                                       p_section[i]->page_count, p_section[i]->visible_article_count,
563                                       p_section[i]->last_page_visible_article_count);
564                            break;
565                    }
566    
567                    affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
568                    if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
569                    {
570                            printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
571                                       p_section[i]->article_count - p_section[i]->visible_article_count,
572                                       affected_count);
573                            break;
574                    }
575    
576                    article_count = p_section[i]->visible_article_count;
577    
578                    for (j = 0; j < group_count; j += 1)
579                    {
580                            last_aid = i * BBS_article_limit_per_section + j + 1;
581    
582                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
583    
584                            if (p_article == NULL)
585                            {
586                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
587                                    break;
588                            }
589    
590                            if (p_article->aid != last_aid)
591                            {
592                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
593                                    break;
594                            }
595    
596                            if (p_article->tid != 0)
597                            {
598                                    printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
599                                    break;
600                            }
601    
602                            if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
603                            {
604                                    printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
605                                    break;
606                            }
607    
608                            article_count -= affected_count;
609                    }
610    
611                    if (article_count != 0)
612                    {
613                            printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
614                            break;
615                    }
616    
617                    if (p_section[i]->visible_article_count > 0)
618                    {
619                            printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
620                            break;
621                    }
622    
623                    if (p_section[i]->visible_topic_count > 0)
624                    {
625                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
626                            break;
627                    }
628    
629                    last_aid = p_section[i]->p_article_head->aid;
630                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
631                    {
632                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
633                            break;
634                    }
635    
636                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
637                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
638                            p_section[i]->page_count)
639                    {
640                            printf("#2 Inconsistent page count in section %d offset %d, %d != %d, "
641                                       "visible_article_count = %d, last_page_visible_count = %d\n",
642                                       i, j,
643                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
644                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
645                                       p_section[i]->page_count, p_section[i]->visible_article_count,
646                                       p_section[i]->last_page_visible_article_count);
647                            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++)
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;
666    
667                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
668                    {
669                            last_aid = i * BBS_article_limit_per_section + j + 1;
670    
671                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
672                            {
673                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
674                                    break;
675                            }
676    
677                            affected_count++;
678                    }
679    
680                    if (affected_count != p_section[i]->article_count)
681                    {
682                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
683                            break;
684                    }
685    
686                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
687                    {
688                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
689                            break;
690                    }
691    
692                    if (p_section[i]->visible_topic_count != group_count)
693                    {
694                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
695                            break;
696                    }
697    
698                    last_aid = p_section[i]->p_article_head->aid;
699                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
700                    {
701                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
702                            break;
703                    }
704    
705                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
706                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
707                            p_section[i]->page_count)
708                    {
709                            printf("#3 Inconsistent page count in section %d offset %d, %d != %d, "
710                                       "visible_article_count = %d, last_page_visible_count = %d\n",
711                                       i, j,
712                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
713                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
714                                       p_section[i]->page_count, p_section[i]->visible_article_count,
715                                       p_section[i]->last_page_visible_article_count);
716                            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");
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)
734            {
735                    log_error("article_block_reset() error\n");
736                    return -4;
737            }
738    
739            for (i = 0; i < section_count; i++)
740            {
741                    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;
750    
751            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;
760    
761                    for (j = 0; j < BBS_article_limit_per_section; j++)
762                    {
763                            last_aid++;
764    
765                            // Set article data
766                            article.aid = last_aid;
767                            // Group articles into group_count topics
768                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
769                            article.sid = i * 3 + 1;
770                            article.cid = article.aid;
771                            article.uid = 1;
772                            article.visible = 1;
773                            article.excerption = 0;
774                            article.ontop = 0;
775                            article.lock = 0;
776                            article.transship = 0;
777    
778                            if (section_list_append_article(p_section[i], &article) < 0)
779                            {
780                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
781                                    break;
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);
792            }
793    
794            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;
803    
804                    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);
807                            if (p_article == NULL)
808                            {
809                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
810                                               section_first_aid + j, i);
811                                    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_data_find_section_by_name(%s) error\n", sname[i]);                          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++)
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;
847    
848                    for (j = 0; j < group_count; j++)
849                    {
850                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
851    
852                            if (affected_count < 0)
853                            {
854                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
855                                    break;
856                            }
857    
858                            if (affected_count != BBS_article_limit_per_section / group_count)
859                            {
860                                    printf("move topic (aid = %d) affected article count %d != %d\n",
861                                               section_first_aid + j, affected_count,
862                                               BBS_article_limit_per_section / group_count);
863                                    // 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++)
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))
894                    {
895                            printf("Topic count error in section %d, %d != %d\n", i,
896                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
897                            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))
908                    {
909                            printf("Article count error in section %d, %d != %d\n", i,
910                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
911                            break;
912                    }
913    
914                    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,
924                                       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;
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