/[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.6 by sysadm, Wed May 21 09:18:17 2025 UTC Revision 1.43 by sysadm, Tue Nov 18 15:15:18 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     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
 #include "section_list.h"  
13  #include "bbs.h"  #include "bbs.h"
14  #include "log.h"  #include "log.h"
15    #include "section_list.h"
16    #include "trie_dict.h"
17    #include "user_list.h"
18    #include <errno.h>
19  #include <stdio.h>  #include <stdio.h>
20  #include <unistd.h>  #include <unistd.h>
21  #include <errno.h>  
22    static const char ARTICLE_BLOCK_SHM_FILE[] = "~article_block_shm.dat";
23    static const char SECTION_LIST_SHM_FILE[] = "~section_list_shm.dat";
24    static const char TRIE_DICT_SHM_FILE[] = "~trie_dict_shm.dat";
25    static const char USER_LIST_SHM_FILE[] = "~user_list_shm.dat";
26    
27  const char *sname[] = {  const char *sname[] = {
28          "Test",          "Test",
# Line 28  const char *sname[] = { Line 31  const char *sname[] = {
31    
32  const char *stitle[] = {  const char *stitle[] = {
33          " Test Section ",          " Test Section ",
34          "ĸABC",          "字母组合ABC",
35          "__123"};          "_数字_123"};
36    
37  const char *master_name[] = {  const char *master_name[] = {
38          "sysadm",          "sysadm",
# Line 41  int section_count = BBS_max_section; Line 44  int section_count = BBS_max_section;
44    
45  int main(int argc, char *argv[])  int main(int argc, char *argv[])
46  {  {
47          SECTION_DATA *p_section[BBS_max_section];          SECTION_LIST *p_section[BBS_max_section];
48          ARTICLE *p_article;          ARTICLE *p_article;
49            ARTICLE *p_next;
50          ARTICLE article;          ARTICLE article;
51          int block_count;          int block_count;
52          int i, j;          int i, j;
53            int sid;
54          int last_aid;          int last_aid;
55          int group_count;          int current_tid;
56            int section_first_aid;
57            int group_count = 100;
58          int article_count;          int article_count;
59            int step;
60            int32_t page;
61            int32_t offset;
62            int affected_count;
63            FILE *fp;
64    
65          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
66          {          {
# Line 56  int main(int argc, char *argv[]) Line 68  int main(int argc, char *argv[])
68                  return -1;                  return -1;
69          }          }
70    
71          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
72          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
73    
74            // - 1 to make blocks allocated is less than required, to trigger error handling
75            block_count = BBS_article_limit_per_section * BBS_max_section / BBS_article_count_per_block;
76    
77            if ((fp = fopen(ARTICLE_BLOCK_SHM_FILE, "w")) == NULL)
78            {
79                    log_error("fopen(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
80                    return -1;
81            }
82            fclose(fp);
83    
84            if ((fp = fopen(SECTION_LIST_SHM_FILE, "w")) == NULL)
85            {
86                    log_error("fopen(%s) error\n", SECTION_LIST_SHM_FILE);
87                    return -1;
88            }
89            fclose(fp);
90    
91            if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
92            {
93                    log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
94                    return -1;
95            }
96            fclose(fp);
97    
98            if ((fp = fopen(USER_LIST_SHM_FILE, "w")) == NULL)
99            {
100                    log_error("fopen(%s) error\n", USER_LIST_SHM_FILE);
101                    return -1;
102            }
103            fclose(fp);
104    
105            if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
106            {
107                    printf("trie_dict_init failed\n");
108                    return -1;
109            }
110    
111            if (article_block_init(ARTICLE_BLOCK_SHM_FILE, block_count) < 0)
112            {
113                    log_error("article_block_init(%s, %d) error\n", ARTICLE_BLOCK_SHM_FILE, block_count);
114                    return -2;
115            }
116    
117          // block_count = BBS_article_block_limit_per_section * BBS_max_section; // This statement is correct          if (section_list_init(SECTION_LIST_SHM_FILE) < 0)
118          // - 1 to make blocks allocated is less than required          {
119          // Some error will be triggered while invoking section_data_append_article()                  log_error("section_list_pool_init(%s) error\n", SECTION_LIST_SHM_FILE);
120          block_count = BBS_article_block_limit_per_section * BBS_max_section - 1;                  return -2;
121            }
122    
123          if (section_data_pool_init("../conf/menu.conf", block_count) < 0)          // Load user_list and online_user_list
124            if (user_list_pool_init(USER_LIST_SHM_FILE) < 0)
125          {          {
126                  log_error("section_data_pool_init() error\n");                  log_error("user_list_pool_init() error\n");
127                  return -2;                  return -2;
128          }          }
129    
# Line 74  int main(int argc, char *argv[]) Line 131  int main(int argc, char *argv[])
131    
132          last_aid = 0;          last_aid = 0;
133    
134            if (section_list_rw_lock(NULL) < 0)
135            {
136                    printf("section_list_rw_lock(sid = %d) error\n", 0);
137            }
138    
139          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
140          {          {
141                  p_section[i] = section_data_create(sname[i % section_conf_count],                  sid = i * 3 + 1;
142                    p_section[i] = section_list_create(sid,
143                                                                                       sname[i % section_conf_count],
144                                                                                     stitle[i % section_conf_count],                                                                                     stitle[i % section_conf_count],
145                                                                                     master_name[i % section_conf_count]);                                                                                     master_name[i % section_conf_count]);
146                  if (p_section[i] == NULL)                  if (p_section[i] == NULL)
147                  {                  {
148                          log_error("section_data_create(i=%d) error\n", i);                          printf("section_list_create(i = %d) error\n", i);
149                            return -3;
150                    }
151    
152                    if (get_section_index(p_section[i]) != i)
153                    {
154                            printf("get_section_index(i = %d) error\n", i);
155                            return -3;
156                    }
157            }
158    
159            for (i = 0; i < section_conf_count; i++)
160            {
161                    if (section_list_find_by_name(sname[i]) == NULL)
162                    {
163                            printf("section_list_find_by_name(%s) error\n", sname[i]);
164                          return -3;                          return -3;
165                  }                  }
166            }
167    
168                  for (j = 0; j < BBS_article_limit_per_block * BBS_article_block_limit_per_section; j++)          for (i = 0; i < section_count; i++)
169            {
170                    sid = i * 3 + 1;
171                    if (section_list_find_by_sid(sid) == NULL || section_list_find_by_sid(sid)->sid != sid)
172                    {
173                            printf("section_list_find_by_sid(%d) error\n", sid);
174                            return -3;
175                    }
176            }
177    
178            if (section_list_rw_unlock(NULL) < 0)
179            {
180                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
181            }
182    
183            for (j = 0; j < BBS_article_limit_per_section; j++)
184            {
185                    for (i = 0; i < section_count; i++)
186                  {                  {
187                          last_aid++;                          last_aid++;
188    
189                          // Set article data                          // Set article data
190                          article.aid = last_aid;                          article.aid = last_aid;
                         article.cid = article.aid;  
191                          article.tid = 0;                          article.tid = 0;
192                          article.uid = 1; // TODO: randomize                          article.sid = i * 3 + 1;
193                            article.cid = article.aid;
194                            article.uid = 1;
195                          article.visible = 1;                          article.visible = 1;
196                          article.excerption = 0;                          article.excerption = 0;
197                          article.ontop = 0;                          article.ontop = 0;
198                          article.lock = 0;                          article.lock = 0;
199                            article.transship = 0;
200    
201                            if (section_list_rw_lock(p_section[i]) < 0)
202                            {
203                                    printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
204                                    break;
205                            }
206    
207                          if (section_data_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
208                          {                          {
209                                  printf("append article (aid = %d) error\n", article.aid);                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
210                                    break;
211                            }
212    
213                            if (section_list_rw_unlock(p_section[i]) < 0)
214                            {
215                                    printf("section_list_rw_unlock(sid = %d) error %d\n", p_section[i]->sid, errno);
216                                  break;                                  break;
217                          }                          }
218                  }                  }
219            }
220    
221                  printf("Load %d articles into section %d\n", p_section[i]->article_count, i);          for (i = 0; i < section_count; i++)
222            {
223                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
224          }          }
225    
226          last_aid = 0;          if (last_aid != article_block_last_aid())
227            {
228                    printf("last_aid != %d\n", article_block_last_aid());
229            }
230    
231          for (i = 0; i < section_count; i++)          if (article_block_article_count() != section_count * BBS_article_limit_per_section)
232          {          {
233                  if (p_section[i]->article_count == 0)                  printf("article_block_article_count() error %d != %d * %d\n",
234                  {                             article_block_article_count(), section_count, BBS_article_limit_per_section);
235                          continue;          }
                 }  
236    
237                  for (j = 0; j < p_section[i]->article_count; j++)          last_aid = 0;
238    
239            for (j = 0; j < BBS_article_limit_per_section; j++)
240            {
241                    for (i = 0; i < section_count; i++)
242                  {                  {
243                          last_aid++;                          last_aid++;
244    
245                          p_article = section_data_find_article_by_index(p_section[i], j);                          p_article = article_block_find_by_aid(last_aid);
246                          if (p_article == NULL || p_article->aid != last_aid)                          if (p_article == NULL || p_article->aid != last_aid)
247                          {                          {
248                                  printf("Inconsistent aid at index %d != %d\n", j, last_aid);                                  printf("article_block_find_by_aid() at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
249                          }                          }
250    
251                          if (section_data_mark_del_article(p_section[i], p_article->aid) != 1)                          if (section_list_rw_lock(p_section[i]) < 0)
252                          {                          {
253                                  printf("section_data_mark_del_article(aid = %d) error\n", p_article->aid);                                  printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
254                                    break;
255                            }
256    
257                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
258                            {
259                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
260                            }
261    
262                            if (section_list_rw_unlock(p_section[i]) < 0)
263                            {
264                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
265                                    break;
266                          }                          }
267                  }                  }
268    
269                  printf("Verify %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);
                 printf("Delete %d articles in section %d\n", p_section[i]->delete_count, i);  
270          }          }
271    
272          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
273    
274          group_count = 100;          if (section_list_rw_lock(NULL) < 0)
275            {
276                    printf("section_list_rw_lock(sid = %d) error\n", 0);
277            }
278    
279            if (article_block_reset() != 0)
280            {
281                    log_error("article_block_reset() error\n");
282                    return -4;
283            }
284    
285          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
286          {          {
287                  if (section_data_free_block(p_section[i]) != 0)                  section_list_reset_articles(p_section[i]);
288            }
289    
290            if (section_list_rw_unlock(NULL) < 0)
291            {
292                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
293            }
294    
295            last_aid = 0;
296    
297            for (i = 0; i < section_count; i++)
298            {
299                    section_first_aid = last_aid + 1;
300    
301                    if (section_list_rw_lock(p_section[i]) < 0)
302                  {                  {
303                          log_error("section_data_free_block(i=%d) error\n", i);                          printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
304                          return -4;                          break;
305                  }                  }
306    
307                  last_aid = 0;                  for (j = 0; j < BBS_article_limit_per_section; j++)
   
                 for (j = 0; j < BBS_article_limit_per_block * BBS_article_block_limit_per_section; j++)  
308                  {                  {
309                          last_aid++;                          last_aid++;
310    
311                          // Set article data                          // Set article data
312                          article.aid = last_aid;                          article.aid = last_aid;
313                            // Group articles into group_count topics
314                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
315                            article.sid = i * 3 + 1;
316                          article.cid = article.aid;                          article.cid = article.aid;
317                          article.tid = (article.aid <= group_count ? 0 : (article.aid - 1) % group_count + 1); // Group articles into group_count topics                          article.uid = 1;
                         article.uid = 1;                                                                                                                                          // TODO: randomize  
318                          article.visible = 1;                          article.visible = 1;
319                          article.excerption = 0;                          article.excerption = 0;
320                          article.ontop = 0;                          article.ontop = 0;
321                          article.lock = 0;                          article.lock = 0;
322                            article.transship = 0;
323    
324                          if (section_data_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
325                          {                          {
326                                  printf("append article (aid = %d) error\n", article.aid);                                  printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
327                                  break;                                  break;
328                          }                          }
329                  }                  }
330    
331                  printf("Load %d articles into section %d\n", p_section[i]->article_count, i);                  if (section_list_rw_unlock(p_section[i]) < 0)
332                    {
333                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
334                            break;
335                    }
336    
337                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
338            }
339    
340            for (i = 0; i < section_count; i++)
341            {
342                    if (p_section[i]->article_count == 0)
343                    {
344                            continue;
345                    }
346    
347                    article_count = 0;
348                    last_aid = 0;
349    
350                    if (section_list_rd_lock(p_section[i]) < 0)
351                    {
352                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
353                            break;
354                    }
355    
356                    p_article = p_section[i]->p_article_head;
357    
358                    do
359                    {
360                            article_count++;
361    
362                            if (p_article->aid <= last_aid)
363                            {
364                                    printf("Non-ascending aid found %d <= %d at article\n", p_article->aid, last_aid);
365                            }
366                            last_aid = p_article->aid;
367    
368                            p_article = p_article->p_next;
369                    } while (p_article != p_section[i]->p_article_head);
370    
371                    if (article_count != p_section[i]->article_count)
372                    {
373                            printf("Count of articles in section %d is different from expected %d != %d\n",
374                                       i, article_count, p_section[i]->article_count);
375                            break;
376                    }
377    
378                    if (section_list_rd_unlock(p_section[i]) < 0)
379                    {
380                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
381                            break;
382                    }
383    
384                    // printf("Verified %d articles in section %d\n", group_count, i);
385          }          }
386    
387          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
# Line 183  int main(int argc, char *argv[]) Line 391  int main(int argc, char *argv[])
391                          continue;                          continue;
392                  }                  }
393    
394                    if (section_list_rd_lock(p_section[i]) < 0)
395                    {
396                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
397                            break;
398                    }
399    
400                    if (p_section[i]->topic_count != group_count)
401                    {
402                            printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
403                    }
404    
405                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
406                  {                  {
407                          p_article = section_data_find_article_by_index(p_section[i], j);                          last_aid = p_section[i]->p_article_head->aid + j;
408    
409                            p_article = article_block_find_by_aid(last_aid);
410                          if (p_article == NULL)                          if (p_article == NULL)
411                          {                          {
412                                  printf("NULL p_article at index %d\n", j);                                  printf("NULL p_article at section %d index %d\n", i, j);
413                                  break;                                  break;
414                          }                          }
415                          if (p_article->aid != j + 1)                          if (p_article->aid != last_aid)
416                          {                          {
417                                  printf("Inconsistent aid at index %d != %d\n", j, j + 1);                                  printf("Inconsistent aid at section %d index %d, %d != %d\n", i, j, p_article->aid, last_aid);
418                                  break;                                  break;
419                          }                          }
420    
421                          article_count = 1;                          article_count = 1;
422                            last_aid = 0;
423                            current_tid = p_article->aid;
424    
425                          do                          do
426                          {                          {
427                                  if (p_article->next_aid <= p_article->aid && p_article->next_aid != p_article->tid)                                  if (p_article->aid <= last_aid)
428                                  {                                  {
429                                          printf("Non-ascending aid found %d >= %d\n", p_article->aid, p_article->next_aid);                                          printf("Non-ascending aid found %d <= %d\n", p_article->aid, last_aid);
                                         break;  
430                                  }                                  }
431                                    last_aid = p_article->aid;
432    
433                                    p_article = p_article->p_topic_next;
434    
                                 last_aid = p_article->next_aid;  
                                 p_article = section_data_find_article_by_aid(p_section[i], last_aid);  
435                                  if (p_article == NULL)                                  if (p_article == NULL)
436                                  {                                  {
437                                          printf("NULL p_article at aid %d\n", last_aid);                                          printf("NULL p_article found\n");
438                                          break;                                          break;
439                                  }                                  }
440                                  if (p_article->tid == 0) // loop                                  if (p_article->tid == 0) // loop
441                                  {                                  {
442                                          break;                                          break;
443                                  }                                  }
444                                  if (p_article->tid != j + 1)                                  if (p_article->tid != current_tid)
445                                  {                                  {
446                                          printf("Inconsistent tid at aid %d != %d\n", last_aid, j + 1);                                          printf("Inconsistent tid %d != %d\n", p_article->tid, current_tid);
447                                          break;                                          break;
448                                  }                                  }
449    
450                                  article_count++;                                  article_count++;
451                          } while (1);                          } while (1);
452    
453                          if (article_count != p_section[i]->article_count / group_count)                          if (article_count != p_section[i]->article_count / group_count)
454                          {                          {
455                                  printf("Count of articles in topic %d is less than expected %d < %d\n",                                  printf("Count of articles in topic %d is different from expected %d != %d\n",
456                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
457                                    // break;
458                          }                          }
459                  }                  }
460    
461                  printf("Verify %d topics in section %d\n", group_count, i);                  if (section_list_rd_unlock(p_section[i]) < 0)
462                    {
463                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
464                            break;
465                    }
466    
467                    // printf("Verified %d topics in section %d\n", group_count, i);
468          }          }
469    
470          printf("Testing #3 ...\n");          printf("Testing #3 ...\n");
471    
472          for (i = 0; i < section_conf_count; i++)          for (i = 0; i < section_count; i++)
473          {          {
474                  if (section_data_find_section_by_name(sname[i]) == NULL)                  last_aid = 0;
475    
476                    if (section_list_rd_lock(p_section[i]) < 0)
477                    {
478                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
479                            break;
480                    }
481    
482                    for (j = 0; j < p_section[i]->page_count; j++)
483                    {
484                            if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
485                            {
486                                    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);
487                            }
488    
489                            if (j > 0)
490                            {
491                                    step = 0;
492    
493                                    for (p_article = p_section[i]->p_page_first_article[j];
494                                             p_article != p_section[i]->p_page_first_article[j - 1];
495                                             p_article = p_article->p_prior)
496                                    {
497                                            step++;
498                                    }
499    
500                                    if (step != BBS_article_limit_per_page)
501                                    {
502                                            printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
503                                    }
504                            }
505                    }
506    
507                    if (section_list_rd_unlock(p_section[i]) < 0)
508                  {                  {
509                          printf("section_data_find_section_by_name(%s) error\n", sname[i]);                          printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
510                            break;
511                  }                  }
512          }          }
513    
514            printf("Testing #4 ...\n");
515    
516            for (i = 0; i < section_count; i++)
517            {
518                    if (section_list_rw_lock(p_section[i]) < 0)
519                    {
520                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
521                            break;
522                    }
523    
524                    step = i % 10 + 1;
525                    for (j = group_count; j < BBS_article_limit_per_section; j += step)
526                    {
527                            last_aid = i * BBS_article_limit_per_section + j + 1;
528    
529                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
530    
531                            if (p_article == NULL)
532                            {
533                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
534                                    break;
535                            }
536    
537                            if (p_article->aid != last_aid)
538                            {
539                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
540                                    break;
541                            }
542    
543                            if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
544                            {
545                                    printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
546                                    break;
547                            }
548                    }
549    
550                    last_aid = p_section[i]->p_article_head->aid;
551                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
552                    {
553                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
554                            break;
555                    }
556    
557                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
558                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
559                            p_section[i]->page_count)
560                    {
561                            printf("#1 Inconsistent page count in section %d offset %d, %d != %d, "
562                                       "visible_article_count = %d, last_page_visible_count = %d\n",
563                                       i, j,
564                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
565                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
566                                       p_section[i]->page_count, p_section[i]->visible_article_count,
567                                       p_section[i]->last_page_visible_article_count);
568                            break;
569                    }
570    
571                    affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
572                    if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
573                    {
574                            printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
575                                       p_section[i]->article_count - p_section[i]->visible_article_count,
576                                       affected_count);
577                            break;
578                    }
579    
580                    article_count = p_section[i]->visible_article_count;
581    
582                    for (j = 0; j < group_count; j += 1)
583                    {
584                            last_aid = i * BBS_article_limit_per_section + j + 1;
585    
586                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
587    
588                            if (p_article == NULL)
589                            {
590                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
591                                    break;
592                            }
593    
594                            if (p_article->aid != last_aid)
595                            {
596                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
597                                    break;
598                            }
599    
600                            if (p_article->tid != 0)
601                            {
602                                    printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
603                                    break;
604                            }
605    
606                            if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
607                            {
608                                    printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
609                                    break;
610                            }
611    
612                            article_count -= affected_count;
613                    }
614    
615                    if (article_count != 0)
616                    {
617                            printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
618                            break;
619                    }
620    
621                    if (p_section[i]->visible_article_count > 0)
622                    {
623                            printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
624                            break;
625                    }
626    
627                    if (p_section[i]->visible_topic_count > 0)
628                    {
629                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
630                            break;
631                    }
632    
633                    last_aid = p_section[i]->p_article_head->aid;
634                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
635                    {
636                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
637                            break;
638                    }
639    
640                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
641                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
642                            p_section[i]->page_count)
643                    {
644                            printf("#2 Inconsistent page count in section %d offset %d, %d != %d, "
645                                       "visible_article_count = %d, last_page_visible_count = %d\n",
646                                       i, j,
647                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
648                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
649                                       p_section[i]->page_count, p_section[i]->visible_article_count,
650                                       p_section[i]->last_page_visible_article_count);
651                            break;
652                    }
653    
654                    if (section_list_rw_unlock(p_section[i]) < 0)
655                    {
656                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
657                            break;
658                    }
659            }
660    
661            for (i = 0; i < BBS_max_section; i++)
662            {
663                    if (section_list_rw_lock(p_section[i]) < 0)
664                    {
665                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
666                            break;
667                    }
668    
669                    affected_count = 0;
670    
671                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
672                    {
673                            last_aid = i * BBS_article_limit_per_section + j + 1;
674    
675                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
676                            {
677                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
678                                    break;
679                            }
680    
681                            affected_count++;
682                    }
683    
684                    if (affected_count != p_section[i]->article_count)
685                    {
686                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
687                            break;
688                    }
689    
690                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
691                    {
692                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
693                            break;
694                    }
695    
696                    if (p_section[i]->visible_topic_count != group_count)
697                    {
698                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
699                            break;
700                    }
701    
702                    last_aid = p_section[i]->p_article_head->aid;
703                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
704                    {
705                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
706                            break;
707                    }
708    
709                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
710                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
711                            p_section[i]->page_count)
712                    {
713                            printf("#3 Inconsistent page count in section %d offset %d, %d != %d, "
714                                       "visible_article_count = %d, last_page_visible_count = %d\n",
715                                       i, j,
716                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
717                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
718                                       p_section[i]->page_count, p_section[i]->visible_article_count,
719                                       p_section[i]->last_page_visible_article_count);
720                            break;
721                    }
722    
723                    if (section_list_rw_unlock(p_section[i]) < 0)
724                    {
725                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
726                            break;
727                    }
728            }
729    
730            printf("Testing #5 ...\n");
731    
732            if (section_list_rw_lock(NULL) < 0)
733            {
734                    printf("section_list_rw_lock(sid = %d) error\n", 0);
735            }
736    
737            if (article_block_reset() != 0)
738            {
739                    log_error("article_block_reset() error\n");
740                    return -4;
741            }
742    
743            for (i = 0; i < section_count; i++)
744            {
745                    section_list_reset_articles(p_section[i]);
746            }
747    
748            if (section_list_rw_unlock(NULL) < 0)
749            {
750                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
751            }
752    
753            last_aid = 0;
754    
755            for (i = 0; i < section_count / 2; i++)
756            {
757                    if (section_list_rw_lock(p_section[i]) < 0)
758                    {
759                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
760                            break;
761                    }
762    
763                    section_first_aid = last_aid + 1;
764    
765                    for (j = 0; j < BBS_article_limit_per_section; j++)
766                    {
767                            last_aid++;
768    
769                            // Set article data
770                            article.aid = last_aid;
771                            // Group articles into group_count topics
772                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
773                            article.sid = i * 3 + 1;
774                            article.cid = article.aid;
775                            article.uid = 1;
776                            article.visible = 1;
777                            article.excerption = 0;
778                            article.ontop = 0;
779                            article.lock = 0;
780                            article.transship = 0;
781    
782                            if (section_list_append_article(p_section[i], &article) < 0)
783                            {
784                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
785                                    break;
786                            }
787                    }
788    
789                    if (section_list_rw_unlock(p_section[i]) < 0)
790                    {
791                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
792                            break;
793                    }
794    
795                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
796            }
797    
798            for (i = 0; i < section_count / 2; i++)
799            {
800                    if (section_list_rw_lock(p_section[i]) < 0)
801                    {
802                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
803                            break;
804                    }
805    
806                    section_first_aid = p_section[i]->p_article_head->aid;
807    
808                    for (j = 0; j < group_count; j += 2)
809                    {
810                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
811                            if (p_article == NULL)
812                            {
813                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
814                                               section_first_aid + j, i);
815                                    break;
816                            }
817    
818                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
819                            {
820                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
821                            }
822                    }
823    
824                    if (section_list_rw_unlock(p_section[i]) < 0)
825                    {
826                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
827                            break;
828                    }
829            }
830    
831            for (i = 0; i < section_count / 2; i++)
832            {
833                    if (section_list_rw_lock(p_section[i]) < 0)
834                    {
835                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
836                            break;
837                    }
838    
839                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
840                    {
841                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
842    
843                            if (section_list_rw_unlock(p_section[i]) < 0)
844                            {
845                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
846                            }
847                            break;
848                    }
849    
850                    section_first_aid = p_section[i]->p_article_head->aid;
851    
852                    for (j = 0; j < group_count; j++)
853                    {
854                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
855    
856                            if (affected_count < 0)
857                            {
858                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
859                                    break;
860                            }
861    
862                            if (affected_count != BBS_article_limit_per_section / group_count)
863                            {
864                                    printf("move topic (aid = %d) affected article count %d != %d\n",
865                                               section_first_aid + j, affected_count,
866                                               BBS_article_limit_per_section / group_count);
867                                    // break;
868                            }
869                    }
870    
871                    if (section_list_rw_unlock(p_section[i]) < 0)
872                    {
873                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
874                            break;
875                    }
876    
877                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
878                    {
879                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
880    
881                            if (section_list_rw_unlock(p_section[i]) < 0)
882                            {
883                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
884                            }
885                            break;
886                    }
887            }
888    
889            for (i = 0; i < section_count; i++)
890            {
891                    if (section_list_rd_lock(p_section[i]) < 0)
892                    {
893                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
894                            break;
895                    }
896    
897                    if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
898                    {
899                            printf("Topic count error in section %d, %d != %d\n", i,
900                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
901                            break;
902                    }
903    
904                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
905                    {
906                            printf("Visible topic count error in section %d, %d != %d\n", i,
907                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
908                            break;
909                    }
910    
911                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
912                    {
913                            printf("Article count error in section %d, %d != %d\n", i,
914                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
915                            break;
916                    }
917    
918                    if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
919                    {
920                            printf("Visible article count error in section %d, %d != %d\n", i,
921                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
922                            break;
923                    }
924    
925                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
926                    {
927                            printf("Page count error in section %d, %d != %d\n", i,
928                                       p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
929                            break;
930                    }
931    
932                    if (section_list_rd_unlock(p_section[i]) < 0)
933                    {
934                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
935                            break;
936                    }
937            }
938    
939            printf("Testing #6 ...\n");
940    
941            for (i = 0; i < section_count; i++)
942            {
943                    if (section_list_rd_lock(p_section[i]) < 0)
944                    {
945                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
946                            break;
947                    }
948            }
949    
950    #ifndef __CYGWIN__
951            printf("Try rw_lock for 5 sec...\n");
952            if (section_list_try_rw_lock(NULL, 5) == 0)
953            {
954                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
955            }
956    #endif
957    
958            for (i = 0; i < section_count; i++)
959            {
960                    if (section_list_rd_unlock(p_section[i]) < 0)
961                    {
962                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
963                            break;
964                    }
965            }
966    
967            if (section_list_try_rw_lock(NULL, 5) < 0)
968            {
969                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
970            }
971    
972    #ifndef __CYGWIN__
973            for (i = 0; i < section_count; i++)
974            {
975                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
976                    {
977                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
978                            break;
979                    }
980            }
981    #endif
982    
983            if (section_list_rw_unlock(NULL) < 0)
984            {
985                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
986            }
987    
988          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
989          getchar();          getchar();
990            
991          section_data_pool_cleanup();          user_list_pool_cleanup();
992            section_list_cleanup();
993            article_block_cleanup();
994            trie_dict_cleanup();
995    
996            if (unlink(USER_LIST_SHM_FILE) < 0)
997            {
998                    log_error("unlink(%s) error\n", USER_LIST_SHM_FILE);
999                    return -1;
1000            }
1001    
1002            if (unlink(TRIE_DICT_SHM_FILE) < 0)
1003            {
1004                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
1005                    return -1;
1006            }
1007    
1008            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
1009            {
1010                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
1011                    return -1;
1012            }
1013    
1014            if (unlink(SECTION_LIST_SHM_FILE) < 0)
1015            {
1016                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
1017                    return -1;
1018            }
1019    
1020          log_end();          log_end();
1021    


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

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