/[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.25 by sysadm, Mon May 26 23:38:11 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("append article (aid = %d) error\n", article.aid);                                  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 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;          last_aid = 0;
219    
220                  for (j = 0; j < p_section[i]->article_count; j++)          for (j = 0; j < BBS_article_limit_per_section; j++)
221            {
222                    for (i = 0; i < section_count; i++)
223                  {                  {
224                          p_article = section_data_find_article_by_index(p_section[i], j);                          last_aid++;
225    
226                            p_article = article_block_find_by_aid(last_aid);
227                          if (p_article == NULL || p_article->aid != last_aid)                          if (p_article == NULL || p_article->aid != last_aid)
228                          {                          {
229                                  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);
230                          }                          }
231    
232                          if (section_data_mark_del_article(p_section[i], p_article->aid) != 1)                          if (section_list_rw_lock(p_section[i]) < 0)
233                          {                          {
234                                  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);
235                                    break;
236                          }                          }
237    
238                          last_aid += section_count;                          if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != 1)
239                  }                          {
240                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
241                            }
242    
243                  printf("Verify %d articles in section %d\n", p_section[i]->article_count, i);                          if (section_list_rw_unlock(p_section[i]) < 0)
244                            {
245                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
246                                    break;
247                            }
248                    }
249    
250                  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);
251          }          }
252    
253          printf("Testing #2 ...\n");          printf("Testing #2 ...\n");
254    
255          group_count = 100;          if (section_list_rw_lock(NULL) < 0)
256            {
257                    printf("section_list_rw_lock(sid = %d) error\n", 0);
258            }
259    
260            if (article_block_reset() != 0)
261            {
262                    log_error("article_block_reset() error\n");
263                    return -4;
264            }
265    
266            for (i = 0; i < section_count; i++)
267            {
268                    section_list_reset_articles(p_section[i]);
269            }
270    
271            if (section_list_rw_unlock(NULL) < 0)
272            {
273                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
274            }
275    
276            last_aid = 0;
277    
278          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
279          {          {
280                  if (section_data_free_block(p_section[i]) != 0)                  section_first_aid = last_aid + 1;
281    
282                    if (section_list_rw_lock(p_section[i]) < 0)
283                  {                  {
284                          log_error("section_data_free_block(i=%d) error\n", i);                          printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
285                          return -4;                          break;
286                  }                  }
287    
288                  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++)  
289                  {                  {
290                          last_aid++;                          last_aid++;
291    
292                          // Set article data                          // Set article data
293                          article.aid = last_aid;                          article.aid = last_aid;
294                            // Group articles into group_count topics
295                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
296                            article.sid = i * 3 + 1;
297                          article.cid = article.aid;                          article.cid = article.aid;
298                          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  
299                          article.visible = 1;                          article.visible = 1;
300                          article.excerption = 0;                          article.excerption = 0;
301                          article.ontop = 0;                          article.ontop = 0;
302                          article.lock = 0;                          article.lock = 0;
303    
304                          if (section_data_append_article(p_section[i], &article) < 0)                          if (section_list_append_article(p_section[i], &article) < 0)
305                          {                          {
306                                  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);
307                                  break;                                  break;
308                          }                          }
309                  }                  }
310    
311                  printf("Load %d articles into section %d\n", p_section[i]->article_count, i);                  if (section_list_rw_unlock(p_section[i]) < 0)
312                    {
313                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
314                            break;
315                    }
316    
317                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
318          }          }
319    
320          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
# Line 193  int main(int argc, char *argv[]) Line 327  int main(int argc, char *argv[])
327                  article_count = 0;                  article_count = 0;
328                  last_aid = 0;                  last_aid = 0;
329    
330                    if (section_list_rd_lock(p_section[i]) < 0)
331                    {
332                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
333                            break;
334                    }
335    
336                  p_article = p_section[i]->p_article_head;                  p_article = p_section[i]->p_article_head;
337    
338                  do                  do
# Line 201  int main(int argc, char *argv[]) Line 341  int main(int argc, char *argv[])
341    
342                          if (p_article->aid <= last_aid)                          if (p_article->aid <= last_aid)
343                          {                          {
344                                  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);
345                          }                          }
346                          last_aid = p_article->aid;                          last_aid = p_article->aid;
347    
# Line 212  int main(int argc, char *argv[]) Line 352  int main(int argc, char *argv[])
352                  {                  {
353                          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",
354                                     i, article_count, p_section[i]->article_count);                                     i, article_count, p_section[i]->article_count);
355                            break;
356                    }
357    
358                    if (section_list_rd_unlock(p_section[i]) < 0)
359                    {
360                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
361                            break;
362                  }                  }
363    
364                  printf("Verify %d articles in section %d\n", group_count, i);                  // printf("Verified %d articles in section %d\n", group_count, i);
365          }          }
366    
367          for (i = 0; i < section_count; i++)          for (i = 0; i < section_count; i++)
# Line 224  int main(int argc, char *argv[]) Line 371  int main(int argc, char *argv[])
371                          continue;                          continue;
372                  }                  }
373    
374                    if (section_list_rd_lock(p_section[i]) < 0)
375                    {
376                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
377                            break;
378                    }
379    
380                    if (p_section[i]->topic_count != group_count)
381                    {
382                            printf("Inconsistent topic count in section %d, %d != %d\n", i, p_section[i]->topic_count, group_count);
383                    }
384    
385                  for (j = 0; j < group_count; j++)                  for (j = 0; j < group_count; j++)
386                  {                  {
387                          p_article = section_data_find_article_by_index(p_section[i], j);                          last_aid = p_section[i]->p_article_head->aid + j;
388    
389                            p_article = article_block_find_by_aid(last_aid);
390                          if (p_article == NULL)                          if (p_article == NULL)
391                          {                          {
392                                  printf("NULL p_article at index %d\n", j);                                  printf("NULL p_article at section %d index %d\n", i, j);
393                                  break;                                  break;
394                          }                          }
395                          if (p_article->aid != j + 1)                          if (p_article->aid != last_aid)
396                          {                          {
397                                  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);
398                                  break;                                  break;
399                          }                          }
400    
401                          article_count = 1;                          article_count = 1;
402                          last_aid = 0;                          last_aid = 0;
403                            current_tid = p_article->aid;
404    
405                          do                          do
406                          {                          {
# Line 260  int main(int argc, char *argv[]) Line 421  int main(int argc, char *argv[])
421                                  {                                  {
422                                          break;                                          break;
423                                  }                                  }
424                                  if (p_article->tid != j + 1)                                  if (p_article->tid != current_tid)
425                                  {                                  {
426                                          printf("Inconsistent tid  %d != %d\n", last_aid, j + 1);                                          printf("Inconsistent tid %d != %d\n", p_article->tid, current_tid);
427                                          break;                                          break;
428                                  }                                  }
429    
# Line 271  int main(int argc, char *argv[]) Line 432  int main(int argc, char *argv[])
432    
433                          if (article_count != p_section[i]->article_count / group_count)                          if (article_count != p_section[i]->article_count / group_count)
434                          {                          {
435                                  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",
436                                             j + 1, article_count, p_section[i]->article_count / group_count);                                             j + 1, article_count, p_section[i]->article_count / group_count);
437                                    // break;
438                          }                          }
439                  }                  }
440    
441                  printf("Verify %d topics in section %d\n", group_count, i);                  if (section_list_rd_unlock(p_section[i]) < 0)
442                    {
443                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
444                            break;
445                    }
446    
447                    // printf("Verified %d topics in section %d\n", group_count, i);
448          }          }
449    
450          printf("Testing #3 ...\n");          printf("Testing #3 ...\n");
451    
452          for (i = 0; i < section_conf_count; i++)          for (i = 0; i < section_count; i++)
453            {
454                    last_aid = 0;
455    
456                    if (section_list_rd_lock(p_section[i]) < 0)
457                    {
458                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
459                            break;
460                    }
461    
462                    for (j = 0; j < p_section[i]->page_count; j++)
463                    {
464                            if (p_section[i]->p_page_first_article[j]->aid <= last_aid)
465                            {
466                                    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);
467                            }
468    
469                            if (j > 0)
470                            {
471                                    step = 0;
472    
473                                    for (p_article = p_section[i]->p_page_first_article[j];
474                                             p_article != p_section[i]->p_page_first_article[j - 1];
475                                             p_article = p_article->p_prior)
476                                    {
477                                            step++;
478                                    }
479    
480                                    if (step != BBS_article_limit_per_page)
481                                    {
482                                            printf("Incorrect page size %d at section %d page %d\n", step, i, j - 1);
483                                    }
484                            }
485                    }
486    
487                    if (section_list_rd_unlock(p_section[i]) < 0)
488                    {
489                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
490                            break;
491                    }
492            }
493    
494            printf("Testing #4 ...\n");
495    
496            for (i = 0; i < section_count; i++)
497            {
498                    if (section_list_rw_lock(p_section[i]) < 0)
499                    {
500                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
501                            break;
502                    }
503    
504                    step = i % 10 + 1;
505                    for (j = group_count; j < BBS_article_limit_per_section; j += step)
506                    {
507                            last_aid = i * BBS_article_limit_per_section + j + 1;
508    
509                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
510    
511                            if (p_article == NULL)
512                            {
513                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
514                                    break;
515                            }
516    
517                            if (p_article->aid != last_aid)
518                            {
519                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
520                                    break;
521                            }
522    
523                            if (section_list_set_article_visible(p_section[i], last_aid, 0) != 1)
524                            {
525                                    printf("Error set article %d invisible in section %d offset %d\n", last_aid, i, j);
526                                    break;
527                            }
528                    }
529    
530                    last_aid = p_section[i]->p_article_head->aid;
531                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
532                    {
533                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
534                            break;
535                    }
536    
537                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
538                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
539                            p_section[i]->page_count)
540                    {
541                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
542                                       "visible_article_count = %d, last_page_visible_count = %d\n",
543                                       i, j,
544                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
545                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
546                                       p_section[i]->page_count, p_section[i]->visible_article_count,
547                                       p_section[i]->last_page_visible_article_count);
548                            break;
549                    }
550    
551                    affected_count = (BBS_article_limit_per_section - group_count) / step + ((BBS_article_limit_per_section - group_count) % step ? 1 : 0);
552                    if (p_section[i]->article_count - p_section[i]->visible_article_count != affected_count)
553                    {
554                            printf("Inconsistent set invisible count in section %d, %d != %d\n", i,
555                                       p_section[i]->article_count - p_section[i]->visible_article_count,
556                                       affected_count);
557                            break;
558                    }
559    
560                    article_count = p_section[i]->visible_article_count;
561    
562                    for (j = 0; j < group_count; j += 1)
563                    {
564                            last_aid = i * BBS_article_limit_per_section + j + 1;
565    
566                            p_article = section_list_find_article_with_offset(p_section[i], last_aid, &page, &offset, &p_next);
567    
568                            if (p_article == NULL)
569                            {
570                                    printf("Error find article %d in section %d offset %d\n", last_aid, i, j);
571                                    break;
572                            }
573    
574                            if (p_article->aid != last_aid)
575                            {
576                                    printf("Inconsistent article aid in section %d page %d offset %d %d != %d\n", i, page, offset, p_article->aid, last_aid);
577                                    break;
578                            }
579    
580                            if (p_article->tid != 0)
581                            {
582                                    printf("Non-topic article aid in section %d page %d offset %d %d != 0\n", i, page, offset, p_article->tid);
583                                    break;
584                            }
585    
586                            if ((affected_count = section_list_set_article_visible(p_section[i], last_aid, 0)) <= 0)
587                            {
588                                    printf("Error set topic %d invisible in section %d offset %d\n", last_aid, i, j);
589                                    break;
590                            }
591    
592                            article_count -= affected_count;
593                    }
594    
595                    if (article_count != 0)
596                    {
597                            printf("Inconsistent total set invisible topic count in section %d, %d > 0\n", i, article_count);
598                            break;
599                    }
600    
601                    if (p_section[i]->visible_article_count > 0)
602                    {
603                            printf("Inconsistent invisible article count in section %d, %d > 0\n", i, p_section[i]->visible_article_count);
604                            break;
605                    }
606    
607                    if (p_section[i]->visible_topic_count > 0)
608                    {
609                            printf("Inconsistent invisible topic count in section %d, %d > 0\n", i, p_section[i]->visible_topic_count);
610                            break;
611                    }
612    
613                    last_aid = p_section[i]->p_article_head->aid;
614                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
615                    {
616                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
617                            break;
618                    }
619    
620                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
621                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
622                            p_section[i]->page_count)
623                    {
624                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
625                                       "visible_article_count = %d, last_page_visible_count = %d\n",
626                                       i, j,
627                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
628                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
629                                       p_section[i]->page_count, p_section[i]->visible_article_count,
630                                       p_section[i]->last_page_visible_article_count);
631                            break;
632                    }
633    
634                    if (section_list_rw_unlock(p_section[i]) < 0)
635                    {
636                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
637                            break;
638                    }
639            }
640    
641            for (i = 0; i < BBS_max_section; i++)
642            {
643                    if (section_list_rw_lock(p_section[i]) < 0)
644                    {
645                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
646                            break;
647                    }
648    
649                    affected_count = 0;
650    
651                    for (j = 0; j < BBS_article_limit_per_section; j += 1)
652                    {
653                            last_aid = i * BBS_article_limit_per_section + j + 1;
654    
655                            if (section_list_set_article_visible(p_section[i], last_aid, 1) <= 0)
656                            {
657                                    printf("Error set article %d visible in section %d offset %d\n", last_aid, i, j);
658                                    break;
659                            }
660    
661                            affected_count++;
662                    }
663    
664                    if (affected_count != p_section[i]->article_count)
665                    {
666                            printf("Inconsistent total set visible article count in section %d, %d != %d\n", i, affected_count, p_section[i]->article_count);
667                            break;
668                    }
669    
670                    if (p_section[i]->visible_article_count != p_section[i]->article_count)
671                    {
672                            printf("Inconsistent visible article count in section %d, %d != %d\n", i, p_section[i]->visible_article_count, p_section[i]->article_count);
673                            break;
674                    }
675    
676                    if (p_section[i]->visible_topic_count != group_count)
677                    {
678                            printf("Inconsistent visible topic count in section %d, %d != %d\n", i, p_section[i]->visible_topic_count, group_count);
679                            break;
680                    }
681    
682                    last_aid = p_section[i]->p_article_head->aid;
683                    if (section_list_calculate_page(p_section[i], last_aid) < 0)
684                    {
685                            printf("section_list_calculate_page(aid = %d) error in section %d offset %d\n", last_aid, i, j);
686                            break;
687                    }
688    
689                    if (p_section[i]->visible_article_count / BBS_article_limit_per_page +
690                                    (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0) !=
691                            p_section[i]->page_count)
692                    {
693                            printf("Inconsistent page count in section %d offset %d, %d != %d, "
694                                       "visible_article_count = %d, last_page_visible_count = %d\n",
695                                       i, j,
696                                       p_section[i]->visible_article_count / BBS_article_limit_per_page +
697                                               (p_section[i]->visible_article_count % BBS_article_limit_per_page ? 1 : 0),
698                                       p_section[i]->page_count, p_section[i]->visible_article_count,
699                                       p_section[i]->last_page_visible_article_count);
700                            break;
701                    }
702    
703                    if (section_list_rw_unlock(p_section[i]) < 0)
704                    {
705                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
706                            break;
707                    }
708            }
709    
710            printf("Testing #5 ...\n");
711    
712            if (section_list_rw_lock(NULL) < 0)
713            {
714                    printf("section_list_rw_lock(sid = %d) error\n", 0);
715            }
716    
717            if (article_block_reset() != 0)
718            {
719                    log_error("article_block_reset() error\n");
720                    return -4;
721            }
722    
723            for (i = 0; i < section_count; i++)
724            {
725                    section_list_reset_articles(p_section[i]);
726            }
727    
728            if (section_list_rw_unlock(NULL) < 0)
729            {
730                    printf("section_list_rw_unlock(sid = %d) error\n", 0);
731            }
732    
733            last_aid = 0;
734    
735            for (i = 0; i < section_count / 2; i++)
736            {
737                    if (section_list_rw_lock(p_section[i]) < 0)
738                    {
739                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
740                            break;
741                    }
742    
743                    section_first_aid = last_aid + 1;
744    
745                    for (j = 0; j < BBS_article_limit_per_section; j++)
746                    {
747                            last_aid++;
748    
749                            // Set article data
750                            article.aid = last_aid;
751                            // Group articles into group_count topics
752                            article.tid = ((article.aid < section_first_aid + group_count) ? 0 : (section_first_aid + j % group_count));
753                            article.sid = i * 3 + 1;
754                            article.cid = article.aid;
755                            article.uid = 1; // TODO: randomize
756                            article.visible = 1;
757                            article.excerption = 0;
758                            article.ontop = 0;
759                            article.lock = 0;
760    
761                            if (section_list_append_article(p_section[i], &article) < 0)
762                            {
763                                    printf("append article (aid = %d) error at section %d index %d\n", article.aid, i, j);
764                                    break;
765                            }
766                    }
767    
768                    if (section_list_rw_unlock(p_section[i]) < 0)
769                    {
770                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
771                            break;
772                    }
773    
774                    // printf("Loaded %d articles into section %d\n", p_section[i]->article_count, i);
775            }
776    
777            for (i = 0; i < section_count / 2; i++)
778            {
779                    if (section_list_rw_lock(p_section[i]) < 0)
780                    {
781                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
782                            break;
783                    }
784    
785                    section_first_aid = p_section[i]->p_article_head->aid;
786    
787                    for (j = 0; j < group_count; j += 2)
788                    {
789                            p_article = section_list_find_article_with_offset(p_section[i], section_first_aid + j, &page, &offset, &p_next);
790                            if (p_article == NULL)
791                            {
792                                    printf("section_list_find_article_with_offset(aid = %d) not found in section %d\n",
793                                               section_first_aid + j, i);
794                                    break;
795                            }
796    
797                            if (section_list_set_article_visible(p_section[i], p_article->aid, 0) != BBS_article_limit_per_section / group_count)
798                            {
799                                    printf("section_list_set_article_visible(aid = %d) error\n", p_article->aid);
800                            }
801                    }
802    
803                    if (section_list_rw_unlock(p_section[i]) < 0)
804                    {
805                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
806                            break;
807                    }
808            }
809    
810            for (i = 0; i < section_count / 2; i++)
811          {          {
812                  if (section_data_find_section_by_name(sname[i]) == NULL)                  if (section_list_rw_lock(p_section[i]) < 0)
813                    {
814                            printf("section_list_rw_lock(sid = %d) error\n", p_section[i]->sid);
815                            break;
816                    }
817    
818                    if (section_list_rw_lock(p_section[section_count / 2 + i]) < 0)
819                    {
820                            printf("section_list_rw_lock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
821    
822                            if (section_list_rw_unlock(p_section[i]) < 0)
823                            {
824                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
825                            }
826                            break;
827                    }
828    
829                    section_first_aid = p_section[i]->p_article_head->aid;
830    
831                    for (j = 0; j < group_count; j++)
832                    {
833                            affected_count = section_list_move_topic(p_section[i], p_section[section_count / 2 + i], section_first_aid + j);
834    
835                            if (affected_count < 0)
836                            {
837                                    printf("move topic (aid = %d) error from section %d to section %d\n", section_first_aid + j, i, section_count / 2 + i);
838                                    break;
839                            }
840    
841                            if (affected_count != BBS_article_limit_per_section / group_count)
842                            {
843                                    printf("move topic (aid = %d) affected article count %d != %d\n",
844                                               section_first_aid + j, affected_count,
845                                               BBS_article_limit_per_section / group_count);
846                                    // break;
847                            }
848                    }
849    
850                    if (section_list_rw_unlock(p_section[i]) < 0)
851                  {                  {
852                          printf("section_data_find_section_by_name(%s) error\n", sname[i]);                          printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
853                            break;
854                    }
855    
856                    if (section_list_rw_unlock(p_section[section_count / 2 + i]) < 0)
857                    {
858                            printf("section_list_rw_unlock(sid = %d) error\n", p_section[section_count / 2 + i]->sid);
859    
860                            if (section_list_rw_unlock(p_section[i]) < 0)
861                            {
862                                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
863                            }
864                            break;
865                  }                  }
866          }          }
867    
868            for (i = 0; i < section_count; i++)
869            {
870                    if (section_list_rd_lock(p_section[i]) < 0)
871                    {
872                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
873                            break;
874                    }
875    
876                    if (p_section[i]->topic_count != (i < section_count / 2 ? 0 : group_count))
877                    {
878                            printf("Topic count error in section %d, %d != %d\n", i,
879                                       p_section[i]->topic_count, (i < section_count / 2 ? 0 : group_count));
880                            break;
881                    }
882    
883                    if (p_section[i]->visible_topic_count != (i < section_count / 2 ? 0 : group_count / 2))
884                    {
885                            printf("Visible topic count error in section %d, %d != %d\n", i,
886                                       p_section[i]->visible_topic_count, (i < section_count / 2 ? 0 : group_count / 2));
887                            break;
888                    }
889    
890                    if (p_section[i]->article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section))
891                    {
892                            printf("Article count error in section %d, %d != %d\n", i,
893                                       p_section[i]->article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section));
894                            break;
895                    }
896    
897                    if (p_section[i]->visible_article_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2))
898                    {
899                            printf("Visible article count error in section %d, %d != %d\n", i,
900                                       p_section[i]->visible_article_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2));
901                            break;
902                    }
903    
904                    if (p_section[i]->page_count != (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page))
905                    {
906                            printf("Page count error in section %d, %d != %d\n", i,
907                                       p_section[i]->page_count, (i < section_count / 2 ? 0 : BBS_article_limit_per_section / 2 / BBS_article_limit_per_page));
908                            break;
909                    }
910    
911                    if (section_list_rd_unlock(p_section[i]) < 0)
912                    {
913                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
914                            break;
915                    }
916            }
917    
918            printf("Testing #6 ...\n");
919    
920            for (i = 0; i < section_count; i++)
921            {
922                    if (section_list_rd_lock(p_section[i]) < 0)
923                    {
924                            printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
925                            break;
926                    }
927            }
928    
929            printf("Try rw_lock for 5 sec...\n");
930            if (section_list_try_rw_lock(NULL, 5) == 0)
931            {
932                    printf("section_list_try_rw_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
933            }
934    
935            for (i = 0; i < section_count; i++)
936            {
937                    if (section_list_rd_unlock(p_section[i]) < 0)
938                    {
939                            printf("section_list_rd_unlock(sid = %d) error\n", p_section[i]->sid);
940                            break;
941                    }
942            }
943    
944            if (section_list_try_rw_lock(NULL, 5) < 0)
945            {
946                    printf("section_list_rd_lock(sid = %d) error\n", p_section[i]->sid);
947            }
948    
949            for (i = 0; i < section_count; i++)
950            {
951                    if (section_list_try_rd_lock(p_section[i], 0) == 0)
952                    {
953                            printf("section_list_try_rd_lock(sid = %d) error, expectation is timeout\n", p_section[i]->sid);
954                            break;
955                    }
956            }
957    
958            if (section_list_rw_unlock(NULL) < 0)
959            {
960                    printf("section_list_rw_unlock(sid = %d) error\n", p_section[i]->sid);
961            }
962    
963          printf("Press ENTER to exit...");          printf("Press ENTER to exit...");
964          getchar();          getchar();
965    
966          section_data_pool_cleanup();          section_list_cleanup();
967            article_block_cleanup();
968            trie_dict_cleanup();
969    
970            if (unlink(TRIE_DICT_SHM_FILE) < 0)
971            {
972                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
973                    return -1;
974            }
975    
976            if (unlink(ARTICLE_BLOCK_SHM_FILE) < 0)
977            {
978                    log_error("unlink(%s) error\n", ARTICLE_BLOCK_SHM_FILE);
979                    return -1;
980            }
981    
982            if (unlink(SECTION_LIST_SHM_FILE) < 0)
983            {
984                    log_error("unlink(%s) error\n", SECTION_LIST_SHM_FILE);
985                    return -1;
986            }
987    
988          log_end();          log_end();
989    


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

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