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


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

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