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


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

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