Advertisement
avp210159

clist_test.c first tests of cyclist?

Apr 21st, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 38.05 KB | None | 0 0
  1. // avp 2016 http://pastebin.com/d9Vymps2
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stddef.h>
  6. #include <assert.h>
  7.  
  8. #include "cyclist.h" // http://pastebin.com/TGdUA9Se
  9. #include "debug.h"   // http://pastebin.com/pd0EcWCZ
  10.  
  11. #define IF_DEBUG(e,t) \
  12.   if (DEBUG(1000,e,t))
  13.  
  14. struct cltest {
  15.   int v;
  16.   struct cyclist list;
  17. };
  18.  
  19. struct cltest *clget(int v) {
  20.   struct cltest *p = (__typeof__(p))malloc(sizeof(*p));
  21.   p->v = v;
  22.   return p;
  23. }
  24.  
  25. static int blabla = 0;
  26.  
  27. #define PRINT_LIST(p) print_list(__STRING(p) ": ", p, __LINE__)
  28. #define IF1_PRILIST(p) if (blabla > 1) PRINT_LIST(p)
  29.  
  30. static void print_list (const char *msg, struct cyclist *l, int caller_lineno)
  31. {
  32.   struct cltest *p;
  33.   struct cyclist *t;
  34.   char emsg[100];
  35.   sprintf(emsg, "links corrupted (caller line %d)\n", caller_lineno);
  36.  
  37.   puts(msg);
  38.   cyclist_foreach(t, l) {
  39.     p = list_entry(t, __typeof__(*p), list);
  40.     printf("%d %p {%p %p} in %p\n", p->v, t, t->next, t->prev, l);
  41.     DEBUG(0, (t->next->prev == t && t->prev->next == t), emsg);
  42.   }
  43.   puts("---------------");
  44. }
  45.  
  46. static void vstore (struct cyclist *t, char *str, int size)
  47. {
  48.   int v = 0, l = strlen(str);
  49.   struct cltest *p = CONTAINER_OF(t, struct cltest, list);
  50.   if (p)
  51.     v = p->v;
  52.   DEBUG(0, (sprintf(str + l, "%d", v) + l + 1 < size), str);
  53. }
  54.  
  55. #include <stdarg.h>
  56. int
  57. check_vlist (struct cyclist *l, ...)
  58. {
  59.   va_list args;
  60.   va_start (args, l);
  61.   int rc = 1, v = -1;
  62.   struct cltest *t;
  63.  
  64.   cyclist_foreach_entry(t, l, list) {
  65.     DEBUG(0, (t->list.next->prev == &t->list && t->list.prev->next == &t->list),
  66.       "cyclist corrupted");
  67.     v = va_arg(args, int);
  68.     if (v < 1 || t->v != v)
  69.       break;
  70.   }
  71.  
  72.   if (t || va_arg(args, int))
  73.     rc = 0;
  74.  
  75.   va_end(args);
  76.   return rc;
  77. }
  78.  
  79. int
  80. check_list (struct cyclist *l, const char *str)
  81. {
  82.   struct cltest *t;
  83.  
  84.   cyclist_foreach_entry(t, l, list) {
  85.     DEBUG(0, (t->list.next->prev == &t->list && t->list.prev->next == &t->list),
  86.       "cyclist corrupted");
  87.     if (!*str || t->v + '0' != *str++)
  88.       return 0;
  89.   }
  90.   return *str == 0;
  91. }
  92.  
  93. static void
  94. loop_foreach_test (const char *msg, struct cyclist *l, const char *spat)
  95. {
  96.   int sz, i, j;
  97.   char sbuf[sz = (strlen(spat) + 2)], rspat[sz];
  98.   for (i = sz - 3, j = 0; i >= 0; i--, j++)
  99.     rspat[j] = spat[i];
  100.   rspat[j] = 0;
  101.  
  102.   if (msg && blabla)
  103.     printf("%s  [%s]\n", msg, spat);
  104.   struct cyclist *t = 0;
  105.   struct cltest *p;
  106.   struct cyclist_save sv;
  107.  
  108.   sbuf[0] = 0;
  109.   cyclist_foreach(t, l) {
  110.     vstore(t, sbuf, sz);
  111.     DEBUG(0, (t->next->prev == t && t->prev->next == t),
  112.       "cyclist_foreach links corrupted");
  113.   }
  114.   DEBUG(0, strcmp(spat, sbuf) == 0, "cyclist_foreach");
  115.  
  116.   sbuf[0] = 0;
  117.   cyclist_foreach_rev(t, l)
  118.     vstore(t, sbuf, sz);
  119.   DEBUG(0, strcmp(rspat, sbuf) == 0, "cyclist_foreach_rev");
  120.  
  121.   sbuf[0] = 0;
  122.   cyclist_foreach_safe(t, sv, l)
  123.     vstore(t, sbuf, sz);
  124.   DEBUG(0, strcmp(spat, sbuf) == 0, "cyclist_foreach_safe");
  125.  
  126.   sbuf[0] = 0;
  127.   cyclist_foreach_rev_safe(t, sv, l)
  128.     vstore(t, sbuf, sz);
  129.   DEBUG(0, strcmp(rspat, sbuf) == 0, "cyclist_foreach_rev_safe");
  130.  
  131.   i = 0;
  132.   cyclist_foreach_entry(p, l, list)
  133.     sbuf[i++] = p->v + '0';
  134.   sbuf[i] = 0;
  135.   DEBUG(0, strcmp(spat, sbuf) == 0, "cyclist_foreach_entry");
  136.  
  137.   i = 0;
  138.   cyclist_foreach_rev_entry(p, l, list)
  139.     sbuf[i++] = p->v + '0';
  140.   sbuf[i] = 0;
  141.   DEBUG(0, strcmp(rspat, sbuf) == 0, "cyclist_foreach_rev_entry");
  142.  
  143.   sbuf[0] = 0;    
  144.   cyclist_foreach(t, l)
  145.     break;
  146.   DEBUG(0, (l) ? t->next == l->next : 1, "cyclist_foreach start");
  147.   cyclist_foreach_from(t, l) {
  148.     vstore(t, sbuf, sz);
  149.     break;
  150.   }
  151.   DEBUG(0, sbuf[0] == spat[0], "cyclist_foreach_from");
  152.   cyclist_foreach_cont(t, l)
  153.     vstore(t, sbuf, sz);
  154.   DEBUG(0, strcmp(spat, sbuf) == 0, "cyclist_foreach_cont");
  155.  
  156.   sbuf[0] = 0;    
  157.   cyclist_foreach_rev(t, l)
  158.     break;
  159.   DEBUG(0, l ? t->next == l : 1, "cyclist_foreach_rev start");
  160.   cyclist_foreach_rev_from(t, l) {
  161.     vstore(t, sbuf, sz);
  162.     break;
  163.   }
  164.   DEBUG(0, sbuf[0] == rspat[0], "cyclist_foreach_rev_from");
  165.   cyclist_foreach_rev_cont(t, l)
  166.     vstore(t, sbuf, sz);
  167.   DEBUG(0, t == 0 && strcmp(rspat, sbuf) == 0, "cyclist_foreach_rev_cont");
  168.  
  169.   sbuf[0] = 0;    
  170.   cyclist_foreach_safe(t, sv, l)
  171.     break;
  172.   DEBUG(0, l ? t->next == l->next : 1, "cyclist_foreach_safe start");
  173.   cyclist_foreach_from_safe(t, sv, l) {
  174.     vstore(t, sbuf, sz);
  175.     break;
  176.   }
  177.   DEBUG(0, sbuf[0] == spat[0], "cyclist_foreach_from_safe");
  178.   cyclist_foreach_cont_safe(t, sv, l)
  179.     vstore(t, sbuf, sz);
  180.   DEBUG(0, strcmp(spat, sbuf) == 0, "cyclist_foreach_cont_safe");
  181.  
  182.   sbuf[0] = 0;    
  183.   cyclist_foreach_rev_safe(t, sv, l)
  184.     break;
  185.   DEBUG(0, l ? t->next == l : 1, "cyclist_foreach_rev_safe start");
  186.   cyclist_foreach_rev_from_safe(t, sv, l) {
  187.     vstore(t, sbuf, sz);
  188.     break;
  189.   }
  190.   DEBUG(0, sbuf[0] == rspat[0], "cyclist_foreach_rev_from_safe");
  191.   cyclist_foreach_rev_cont_safe(t, sv, l)
  192.     vstore(t, sbuf, sz);
  193.   DEBUG(0, t == 0 && strcmp(rspat, sbuf) == 0, "cyclist_foreach_rev_cont_safe");
  194.  
  195.  
  196.   // check t = 0 not raise SIGSEGV for _from/_cont MACRO
  197.   cyclist_foreach_from(t, l);
  198.   cyclist_foreach_cont(t, l);
  199.   cyclist_foreach_rev_from(t, l);
  200.   cyclist_foreach_rev_cont(t, l);
  201.  
  202.   cyclist_foreach_from_safe(t, sv, l);
  203.   cyclist_foreach_cont_safe(t, sv, l);
  204.   cyclist_foreach_rev_from_safe(t, sv, l);
  205.   cyclist_foreach_rev_cont_safe(t, sv, l);
  206.  
  207. }
  208.  
  209. void test_swapsort (struct cyclist **pl)
  210. {
  211.   struct cyclist *t, *s, *l = *pl ;
  212.   struct cyclist_save tsv, ssv;
  213.  
  214.   cyclist_foreach_safe(t, tsv, l) {
  215.     int v, v1 = container_of(t, struct cltest, list)->v;
  216.     //    printf(":: %d\n", v1);
  217.     if ((s = t->next) != l)
  218.       cyclist_foreach_from_safe(s, ssv, l) {
  219.  
  220.     if ((v = container_of(s, struct cltest, list)->v) < v1) {
  221.       v1 = v;
  222.       cyclist_swap_safe(&t, &l, &tsv,  &s, 0, &ssv);
  223.     }
  224.     // printf(" -> %d %d\n", v1, container_of(s, struct cltest, list)->v);
  225.       }
  226.   }
  227.  
  228.   *pl = l;
  229. }
  230.  
  231. void check_cutroll (struct cyclist *l1, struct cyclist *list2head, int how,
  232.             const char *cmps, const char *msg)
  233. {
  234.   cyclist_cut(l1, list2head, how);
  235.   char str[10];
  236.   struct cyclist *t;
  237.   int i;
  238.    
  239.   for(i = 0, t = list2head->next; how ? t != list2head : t != 0; t = t->next)
  240.     str[i++] = container_of(t, struct cltest, list)->v + '0';
  241.   str[i] = 0;
  242.   l1 = cyclist_roll(list2head);
  243.   DEBUG(0, (strcmp(str, cmps) == 0 &&
  244.         check_list(l1, cmps)), msg);
  245. }
  246.  
  247.  
  248. int
  249. main (int ac, char *av[])
  250. {
  251. #ifdef PRINT
  252.   blabla = 1;
  253. #endif
  254.   if (av[1]) {
  255.     if (strncmp(av[1], "-v", 2))
  256.       exit(puts("Usage: ./a.out [-v]\n  -v -- verbose"));
  257.     if ((blabla = atoi(av[1] + 2) + 1) < 1)
  258.       blabla = 1;
  259.   }  
  260.  
  261.   printf("Go test it, blabla=%d\n", blabla);
  262.  
  263.   struct cltest *pcl, *p;
  264.   struct cyclist  x, *l1 = 0, *l2 = 0, *ldel = 0, *s, *t = cyclist_singular(&x),
  265.     *dl = 0, *cl = 0, *it;
  266.   struct cyclist_save sv, sv2;
  267.   int n[3], tv, y, m = 0, i, count = 0, err = 0;
  268.  
  269.   loop_foreach_test("foreach... null list", l1, "");
  270.  
  271.   IF_DEBUG(t == &x, "CYCLIST_SINGULAR")
  272.     printf("CYCLIST_SINGULAR(&x %p {%p %p})\n", &x, x.next, x.prev);
  273.  
  274.   cyclist_add(&(pcl = clget(++count))->list, &l1);
  275.   if (DEBUG(0, (l1 == &pcl->list && l1->next == l1->prev && l1->next == l1),
  276.         "ERR: cyclist_add first") || blabla)
  277.     printf("cyclist_add {%d %p %p} list: %p pcl: %p\n",
  278.        (container_of(l1, struct cltest, list))->v, l1->next, l1->prev,
  279.        l1, pcl);
  280.  
  281.   loop_foreach_test("foreach... singular list", l1, "1");
  282.  
  283.   if (blabla)
  284.     puts("foreach_safe 1 item list: del l1 -> add to l2");
  285.   cyclist_foreach_safe(t, sv, l1) {
  286.     cyclist_del(t, &l1);
  287.     cyclist_add(t, &l2);
  288.   }
  289.   loop_foreach_test("  foreach... null list l1", l1, "");
  290.   loop_foreach_test("  foreach... singular list l2", l2, "1");
  291.   if (blabla)
  292.     puts("foreach_rev_safe 1 item list: del l2 -> add to l1");
  293.   cyclist_foreach_rev_safe(t, sv, l2) {
  294.     cyclist_del(t, &l2);
  295.     cyclist_add(t, &l1);
  296.   }
  297.   loop_foreach_test("  foreach... null list l2", l2, "");
  298.   loop_foreach_test("  foreach... singular list l1", l1, "1");
  299.  
  300.   cyclist_add(&(pcl = clget(++count))->list, &l1);
  301.   loop_foreach_test("foreach... 2 items list", l1, "21");
  302.  
  303.   if (blabla)
  304.     puts("foreach_(rev_)safe 2 items del l1 -> add to l2 and l2 -> l1");
  305.   cyclist_foreach_safe(t, sv, l1) {
  306.     cyclist_del(t, &l1);
  307.     cyclist_add(t, &l2);
  308.   }
  309.   cyclist_foreach_rev_safe(t, sv, l2) { // ERR ???
  310.     cyclist_del(t, &l2);
  311.     cyclist_add(t, &l1);
  312.   }
  313.   loop_foreach_test("  foreach... null list l2", l2, "");
  314.   loop_foreach_test("  foreach... 2 items list l1", l1, "12");
  315.   cyclist_add(&(pcl = clget(++count))->list, &l1);
  316.   loop_foreach_test("foreach... 3 items list l1", l1, "312");
  317.   IF1_PRILIST(l1);
  318.  
  319.   if (blabla) puts("while/for technique & cyclist_del()");
  320.   i = 0;
  321.   if ((t = l1))
  322.     do {
  323.       tv = n[i++] = (container_of(t, struct cltest, list))->v;
  324.       IF_DEBUG(i < 4, "do ... while")
  325.     printf("loop4 %d %p: {%d %p %p} \n", i, t, tv, t->next, t->prev),
  326.     err++;
  327.       t = t->next;
  328.     } while (t != l1);
  329.   DEBUG(0, (!err && i == 3 && n[0] == 3 && n[2] == 2),
  330.     "do { ... } while technique");
  331.  
  332.  
  333.   if (blabla)
  334.     printf("delete all %d items from l1, add them to l2 tail\n", i);
  335.   for (i = 0; (t = l1); i++) {
  336.     cyclist_del(t, &l1);
  337.     cyclist_add_tail(t, &l2);
  338.     IF_DEBUG(i < 4, "cyclist_del, cyclist_add_tail")
  339.       printf("move to l2 %p: {%d %p %p} \n", t,
  340.          (container_of(t, struct cltest, list))->v, t->next, t->prev),
  341.       err++;
  342.   }
  343.   DEBUG(0, (!err && l1 == 0 && i == 3 &&
  344.         check_list(l2, "312")),
  345.     "cyclist_del, cyclist_add_tail");
  346.  
  347.   // count = 3
  348.   IF1_PRILIST(l1);
  349.   IF1_PRILIST(l2);
  350.  
  351.   if (blabla) puts("move all odd values from l2 to head l1");
  352.   i = 0;
  353.   cyclist_foreach_safe(t, sv, l2) {
  354.     ++i;
  355.     if (blabla > 2)
  356.       printf("l2 item %p: {%d %p %p}\n", t,
  357.          (container_of(t, struct cltest, list))->v, t->next, t->prev);
  358.     if ((container_of(t, struct cltest, list))->v & 1) {
  359.       cyclist_del(t, &l2);
  360.       cyclist_add(t, &l1);
  361.       if (blabla > 2)
  362.     printf("move to head %d %p: {%d %p %p}\n", i, t,
  363.            (container_of(t, struct cltest, list))->v, t->next, t->prev);
  364.     }
  365.   }
  366.   DEBUG(0, (check_list(l1, "13") && check_list(l2, "2") && i == 3),
  367.     "cyclist_foreach_safe, cyclist_del(t), cyclist_add");
  368.   if (blabla)
  369.     puts("insert singular l2 after head l1");
  370.   l2 = cyclist_insert(l2, l1);
  371.   IF1_PRILIST(l1);
  372.   IF1_PRILIST(l2);
  373.   DEBUG(0, (check_list(l1, "123") && check_list(l2, "231")), "insert 1");
  374.  
  375.   if (blabla)
  376.     puts("swap, cswap, split l2 to again l1 = '13' & l2 = '2'");
  377.   s = l2->next; // 3
  378.   t = l2->prev; // 1
  379.   cyclist_swap(&s, &t);
  380.   DEBUG(0, (check_list(l1, "132") && check_list(l2, "213")
  381.         && check_list(s, "132") && check_list(t, "321")), "swap 1");
  382.   cyclist_cswap(l2->next, l2->prev);
  383.   DEBUG(0, (check_list(l1, "123") && check_list(l2, "231")
  384.         && check_list(s, "123") && check_list(t, "312")), "cswap 1");
  385.   cyclist_cswap(l2->next, l2->prev);
  386.   DEBUG(0, (check_list(l1, "132") && check_list(l2, "213")), "cswap 2");
  387.   cyclist_split(l1, l2);
  388.   DEBUG(0, (check_list(l1, "13") && check_list(l2, "2")),
  389.     "cyclist_split to again l1, l2");
  390.  
  391.  
  392.   if (blabla)
  393.     puts("cyclist_foreach_rev_from, cyclist_foreach_rev_cont for 3 l1 items");
  394.   cyclist_add_tail(&(clget(++count))->list, &l1); i = 0;
  395.   IF1_PRILIST(l2);
  396.   IF1_PRILIST(l1);
  397.  
  398.   cyclist_foreach_rev(t, l1) {
  399.     ++i;
  400.     if (blabla > 2)
  401.       printf("  foreach::%d %p: {%d %p %p}\n", i, t,
  402.          (container_of(t, struct cltest, list))->v, t->next, t->prev);
  403.     break;
  404.   }
  405.   if (blabla > 1) puts("  cont...");
  406.   s = t;
  407.   cyclist_foreach_rev_cont(t, l1) {
  408.     ++i;
  409.     if (blabla > 2)
  410.       printf("  foreach::%d %p: {%d %p %p}\n", i, t,
  411.          (container_of(t, struct cltest, list))->v, t->next, t->prev);
  412.   }
  413.   if (blabla > 1) puts("  again from begin");
  414.   t = s;
  415.   cyclist_foreach_rev_from(t, l1) {
  416.     ++i; s = t;
  417.     if (blabla > 2)
  418.       printf("  foreach::%d %p: {%d %p %p}\n", i, s,
  419.          (container_of(t, struct cltest, list))->v, t->next, t->prev);
  420.   }
  421.   if (blabla > 1) puts("  again cyclist_foreach_rev_from() from last");
  422.   t = s;
  423.   cyclist_foreach_rev_from(t, l1) {
  424.     ++i;
  425.     if (blabla > 2)
  426.       printf("  foreach::%d %p: {%d %p %p}\n", i, t,
  427.          (container_of(t, struct cltest, list))->v, t->next, t->prev);
  428.   }
  429.   DEBUG(0, (i == 7 && check_list(l1, "134")),
  430.     "cyclist_foreach_rev_...(t, l1)");
  431.  
  432.   if (blabla)
  433.     puts("swaps (sort), move, join...");
  434.   test_swapsort(&l2);
  435.   DEBUG(0, check_list(l2, "2"), "swapsort '2'");
  436.   IF1_PRILIST(l2);
  437.   l1 = l1->next;
  438.   IF1_PRILIST(l1);
  439.   test_swapsort(&l1);
  440.   DEBUG(0, check_list(l1, "134"), "swapsort '341'");
  441.   cyclist_join(l1, l2);
  442.   cyclist_cswap(l1, l1->next); l1 = l1->prev;
  443.   IF1_PRILIST(l1);
  444.   test_swapsort(&l1);
  445.   DEBUG(0, check_list(l1, "1234"), "swapsort '3142'");
  446.   cyclist_cswap(l1->next, l1->prev);
  447.   cyclist_split(l1, l2 = l1->next->next);
  448.   IF1_PRILIST(l2);
  449.   IF1_PRILIST(l1);
  450.   test_swapsort(&l1);
  451.   test_swapsort(&l2);
  452.   DEBUG(0, (check_list(l1, "14") && check_list(l2, "23")),
  453.       "swapsort '14', '32'");
  454.   t = l1->prev;
  455.   cyclist_swap(&l1, &t);
  456.   DEBUG(0, check_list(l1, "41"), "swap '14'");
  457.     cyclist_join(l1, l2);
  458.   DEBUG(0, check_list(l1, "4123"), "join '41', '23'");
  459.   test_swapsort(&l1);
  460.   DEBUG(0, check_list(l1, "1234"), "swapsort '4123'");
  461.   // reverse list
  462.   l2 = 0;
  463.   cyclist_foreach_rev_safe(t, sv, l1)
  464.     cyclist_move_safe(t, &l1, &sv, &l2, 0);
  465.   DEBUG(0, (check_list(l1, "") && check_list(l2, "4321")),
  466.     "l2 = reverse '1234'");
  467.   test_swapsort(&l2);
  468.   DEBUG(0, check_list(l2, "1234"), "swapsort '4321'");
  469.   IF1_PRILIST(l2);
  470.   IF1_PRILIST(l1);
  471.   l2 = cyclist_move(l2, &l1);
  472.   t = 0;
  473.   cyclist_move(l2->next, &t);
  474.   DEBUG(0, (check_list(l1, "1")
  475.         && check_list(l2, "24") && check_list(t, "3")),
  476.     "move '1234' to '1','24',t='3'");
  477.   cyclist_swap(&l1, &t);
  478.   DEBUG(0, (check_list(l1, "3")
  479.         && check_list(l2, "24") && check_list(t, "1")),
  480.     "swap l1 = '1', t='3'");
  481.   IF1_PRILIST(l2);
  482.   IF1_PRILIST(l1);
  483.   IF1_PRILIST(t);
  484.   cyclist_cswap(l1, t);
  485.   DEBUG(0, (check_list(l1, "3")
  486.         && check_list(l2, "24") && check_list(t, "1")),
  487.     "cswap both singular l1 = '1', t='3'");
  488.   cyclist_cswap(s = l2->next, t);
  489.   DEBUG(0, (check_list(l1, "3") && check_list(s, "4")
  490.         && check_list(l2, "21") && check_list(t, "12")),
  491.     "cyclist_cswap(s = l2->next, t)");
  492.   cyclist_swap(&t, &s);
  493.   DEBUG(0, (check_list(l1, "3") && check_list(s, "1")
  494.         && check_list(l2, "24") && check_list(t, "42")),
  495.     "cyclist_cswap(&t, &s)");
  496.   IF1_PRILIST(l2);
  497.   IF1_PRILIST(l1);
  498.   IF1_PRILIST(t);
  499.   IF1_PRILIST(s);
  500.   cyclist_join(l1, cyclist_join(l2, s));
  501.   IF1_PRILIST(l1);
  502.   test_swapsort(&l1);
  503.   DEBUG(0, check_list(l1, "1234"),
  504.     "cyclist_join(l1, cyclist_join(l2, s)); swapsort");
  505.   cyclist_del(l2 = l1->next, &l1);
  506.   DEBUG(0, (check_list(l1, "134") && check_list(l2, "2")),
  507.         "cyclist_del(l2 = l1->next, &l1)");
  508.   IF1_PRILIST(l2);
  509.   IF1_PRILIST(l1);
  510.  
  511.   if (blabla) puts("check delete in foreach 1, 2, 3 of 4 ...");
  512.  
  513.   cyclist_add((s = &clget(9)->list), &dl);
  514.   cyclist_foreach_safe(t, sv, dl)
  515.     cyclist_del(t, &dl);
  516.   DEBUG(0, dl == 0 && cyclist_singular(s), "cyclist_foreach_save, cyclist_del 1");
  517.   cyclist_add(s, &dl);
  518.   cyclist_foreach_rev_safe(t, sv, dl) {
  519.     DEBUG(0, (container_of(t, struct cltest, list))->v == 9,
  520.       "cyclist_foreach_rev_save t->v == 9");
  521.     cyclist_del(t, &dl);
  522.   }
  523.   DEBUG(0, dl == 0 && cyclist_singular(s), "cyclist_foreach_rev_save, cyclist_del 1");
  524.   // todo check del ...
  525.  
  526.   t = l1->next; // print l1 = 1 9 3 4
  527.   cyclist_add(s, &t); i = 0;
  528.   cyclist_foreach_safe(t, sv, l1) {
  529.     ++i;
  530.     y = (container_of(t, struct cltest, list))->v;
  531.     if (blabla > 1) printf("l1-> %d %p: {%d %p %p}\n", i, t,
  532.                y, t->next, t->prev);
  533.     if (i == 2 && y == 9)
  534.       m++;
  535.   }
  536.   DEBUG(0, m == 1 && i == 4, "cyclist_foreach_save, l1 4 items");
  537.  
  538.   // here dl == 0
  539.   y = 4; i = 0; t = l1->next->next; // t -> 3
  540.   cyclist_foreach_from_safe(t, sv, l1) {
  541.     IF_DEBUG( ++i < 3 && t->next == sv.next, "cyclist_foreach_from_safe l1")
  542.       printf("from l1->next->next %d %p: {%d %p %p}\n", i, t,
  543.          y = (container_of(t, struct cltest, list))->v, t->next, t->prev);
  544.   }
  545.   DEBUG(0, i == 2 && y == 4, "cyclist_foreach_from_safe l1->next->next");
  546.  
  547.   i = 0; t = l1->next->next; // t -> 3
  548.   cyclist_foreach_cont_safe(t, sv, l1)
  549.     IF_DEBUG( ++i < 2 && t->next == sv.next, "cyclist_foreach_cont_safe l1")
  550.       printf("cont l1->next->next %d %p: {%d %p %p}\n", i, t,
  551.        y = (container_of(t, struct cltest, list))->v, t->next, t->prev);
  552.   DEBUG(0, i == 1 && y == 4, "cyclist_foreach_cont_safe l1->next->next");
  553.  
  554.   // l2 is singular, test it
  555.   i = 0; t = l2;
  556.   cyclist_foreach_from_safe(t, sv, l2)
  557.     IF_DEBUG( ++i < 2 && t->next == sv.next, "cyclist_foreach_from_safe sing")
  558.       printf("l2: %d %p: {%d %p %p}\n", i, t,
  559.          y = (container_of(t, struct cltest, list))->v, t->next, t->prev);
  560.   DEBUG(0, i == 1 && y == 4, "cyclist_foreach_from_safe singular l2");
  561.  
  562.   i = 0; t = l2;
  563.   cyclist_foreach_cont_safe(t, sv, l2)
  564.     IF_DEBUG( ++i, "cyclist_foreach_cont_safe sing")
  565.       printf("l2: %d %p: {%d %p %p}\n", i, t,
  566.        y = (container_of(t, struct cltest, list))->v, t->next, t->prev);
  567.   DEBUG(0, i == 0 && y == 4, "cyclist_foreach_cont_safe singular l2");
  568.  
  569.  
  570.   if (blabla)
  571.     puts("cyclist_foreach_rev_safe ... some tests 4 items l1='1234', dl=''");
  572.  
  573.   IF_DEBUG(dl == 0 &&
  574.        (container_of(l1, struct cltest, list))->v == 1, "dl empty") {
  575.     print_list("dl empty, test 1, 2, 3, 4 cyclist_foreach_rev_safe with delete",
  576.            dl, __LINE__);
  577.     PRINT_LIST(l1);
  578.     abort();
  579.   }
  580.   cyclist_foreach_rev_safe(t, sv, l1) {
  581.     cyclist_del(t, &l1);
  582.     cyclist_add(t, &dl);
  583.   }
  584.   IF_DEBUG(l1 == 0 &&
  585.        (container_of(dl, struct cltest, list))->v == 1 &&
  586.        (container_of(dl->next, struct cltest, list))->v == 9 &&
  587.        (container_of(dl->next->next, struct cltest, list))->v == 3 &&
  588.        (container_of(dl->prev, struct cltest, list))->v == 4 , "l1 empty") {
  589.     print_list("after move  (all 4)\nl1: ", l1, __LINE__);
  590.     PRINT_LIST(dl);
  591.     abort();
  592.   }
  593.   //  puts("move 1 item from dl to cl");
  594.   cyclist_foreach_rev_safe(t, sv, dl) {
  595.     cyclist_del(t, &dl);
  596.     cyclist_add(t, &cl);
  597.     break;
  598.   }
  599.   IF_DEBUG(cl->next == cl->prev, "move 1 item from dl to cl") {
  600.     PRINT_LIST(dl);
  601.     PRINT_LIST(cl);
  602.     abort();
  603.   }
  604.   //  puts("continue move (all 3) from dl to l1");
  605.   cyclist_foreach_rev_safe(t, sv, dl) {
  606.     cyclist_del(t, &dl);
  607.     cyclist_add(t, &l1);
  608.   }
  609.   //    print_list("after delete all\nl1: ", l1);
  610.   //    PRINT_LIST(dl);
  611.   //  puts("again move all (realy 1) from cl to dl");
  612.   cyclist_foreach_rev_safe(t, sv, cl) {
  613.     cyclist_del(t, &cl);
  614.     cyclist_add(t, &dl);
  615.   }
  616.   IF_DEBUG(cl == 0 && l1->next->next == l1->prev && dl == dl->next,
  617.        "OK, (tests 4, 3, and 1 items COMPETED) dl: ") {
  618.     print_list("OK, (tests 4, 3, and 1 items COMPETED) dl: ", dl, __LINE__);
  619.     PRINT_LIST(cl);
  620.     print_list("now move 1 item from l1 to dl (for 2 items in dl) l1: ",
  621.            l1, __LINE__);
  622.     abort();
  623.   }
  624.   t = l1;
  625.   cyclist_del(t, &l1);
  626.   cyclist_add(t, &dl);
  627.   //  print_list("test move 2 items (all) to emty cl from dl:", dl);
  628.   cyclist_foreach_rev_safe(t, sv, dl) {
  629.     cyclist_del(t, &dl);
  630.     cyclist_add(t, &cl);
  631.   }
  632.   IF_DEBUG(dl == 0 && l1->next == l1->prev && cl->next == cl->prev,
  633.        "test move 2 items (all) to emty cl from dl:") {
  634.     print_list("end foreach_rev_safe l1:", l1, __LINE__);
  635.     PRINT_LIST(cl);
  636.     print_list("and restore l1, moving all from cl dl: ", dl, __LINE__);
  637.     abort();
  638.   }
  639.   i = 0;
  640.   cyclist_foreach_rev_safe(t, sv, cl) {
  641.     cyclist_del(t, &cl);
  642.     i++ ?  cyclist_add(t, &l1) :  cyclist_add_tail(t, &l1);
  643.   }
  644.  
  645.   //  puts("foreach_entry in l1 (4 items)");
  646.   IF_DEBUG(cl == 0, "foreach_entry in l1 (4 items)") {
  647.     cyclist_foreach_entry(p, l1, list)
  648.       printf("%d %p %p {%p %p}\n",
  649.          p->v, p, &p->list, p->list.next, p->list.prev);
  650.     puts("---\nforeach_rev_entry in l1 (4 items)");
  651.     cyclist_foreach_rev_entry(p, l1, list)
  652.       printf("%d %p %p {%p %p}\n",
  653.          p->v, p, &p->list, p->list.next, p->list.prev);
  654.  
  655.     print_list("repeate tests for cyclist_foreach_rev_..._safe and l1: ",
  656.            l1, __LINE__);
  657.     abort();
  658.   }
  659.   i =0;
  660.   cyclist_foreach_rev_safe(t, sv, l1) {
  661.     if (i++)
  662.       break;
  663.     cyclist_del(t, &l1);
  664.     cyclist_add(t, &dl);
  665.   }
  666.   cyclist_foreach_rev_cont_safe(t, sv, l1) {
  667.     cyclist_del(t, &l1);
  668.     cyclist_add(t, &cl);
  669.   }
  670.   t = l1;
  671.   cyclist_foreach_rev_from_safe(t, sv, l1) {
  672.     cyclist_del(t, &l1);
  673.     cyclist_add(t, &dl);
  674.   }
  675.   IF_DEBUG((check_list(l1, "") &&
  676.         check_list(cl, "19") && check_list(dl, "34")),
  677.        "check l1 cl dl") {
  678.     PRINT_LIST(l1);
  679.     PRINT_LIST(cl);
  680.     PRINT_LIST(dl);
  681.   }
  682.  
  683.   t = dl->next;
  684.   IF_DEBUG((cyclist_remove(t) &&
  685.         cyclist_remove(t) == 0 &&
  686.         cyclist_remove(t) == 0 &&
  687.         check_list(dl, "3") &&
  688.         check_list(t, "4")), "check remove") {
  689.     PRINT_LIST(dl);
  690.     PRINT_LIST(t);
  691.     abort();
  692.   }
  693.  
  694.   if (blabla) puts("foreach_entry ...");
  695.   m = 0;
  696.   cyclist_foreach_entry(p, dl, list) {
  697.     if (blabla > 1) printf("%d %p %p {%p %p}\n",
  698.                p->v, p, &p->list, p->list.next, p->list.prev);
  699.     m = m * 10 + p->v;
  700.   }
  701.   DEBUG(0, m == 3, "foreach_entry in dl (one entry)");
  702.  
  703.   cyclist_foreach_rev_entry(p, dl, list) {
  704.     if (blabla > 1) printf("%d %p %p {%p %p}\n",
  705.                p->v, p, &p->list, p->list.next, p->list.prev);
  706.     m = m * 10 + p->v;
  707.   }
  708.   DEBUG(0, m == 33, "foreach_rev_entry in dl (one entry)");
  709.  
  710.   IF1_PRILIST(t);
  711.   cyclist_add_tail(t, &dl);
  712.   IF1_PRILIST(dl);
  713.  
  714.   m = 0;
  715.   cyclist_foreach_entry(p, l1, list) {
  716.     if (blabla > 1)
  717.       printf("%d %p %p {%p %p}\n",
  718.        p->v, p, &p->list, p->list.next, p->list.prev);
  719.     m = m * 10 + p->v;
  720.   }
  721.   DEBUG(0, m == 0, "foreach_entry in l1 (empty)");
  722.  
  723.   cyclist_foreach_rev_entry(p, l1, list) {
  724.     if (blabla > 1)
  725.       printf("%d %p %p {%p %p}\n",
  726.          p->v, p, &p->list, p->list.next, p->list.prev);
  727.     m = m * 10 + p->v;
  728.   }
  729.   DEBUG(0, m == 0, "foreach_rev_entry in l1 (empty)");
  730.  
  731.   cyclist_foreach_entry(p, cl, list) {
  732.     if (blabla > 1) printf("%d %p %p {%p %p}\n",
  733.                p->v, p, &p->list, p->list.next, p->list.prev);
  734.     m = m * 10 + p->v;
  735.   }
  736.   DEBUG(0, m == 19, "foreach_entry in cl (two entries)");
  737.  
  738.   m = 0;
  739.   cyclist_foreach_rev_entry(p, cl, list) {
  740.     if (blabla > 1) printf("%d %p %p {%p %p}\n",
  741.                p->v, p, &p->list, p->list.next, p->list.prev);
  742.     m = m * 10 + p->v;
  743.   }
  744.   DEBUG(0, m == 91, "foreach_rev_entry in cl (two entries)");
  745.  
  746.   if (blabla) puts("split/join");
  747.   IF_DEBUG(check_list(cyclist_join(cl, dl), "1934"),
  748.        "cyclist_join(cl, dl)")
  749.     print_list("join cl dl: ", cl, __LINE__);
  750.  
  751.   IF_DEBUG(check_list(t = cyclist_split(cl, dl), "34"),
  752.        "cyclist_split(cl, dl)") {
  753.     print_list("result  split cl dl: ", t, __LINE__);
  754.     print_list("cl: split cl dl: ", cl, __LINE__);
  755.     print_list("dl: split cl dl: ", dl, __LINE__);
  756.   }
  757.  
  758.   IF_DEBUG(check_list(l1 = cyclist_split(cyclist_join(cl, dl), dl->next), "4"),
  759.        "split(join cl,dl) dl->next") {
  760.     print_list("l1 =  split(join cl,dl) dl->next: ", l1, __LINE__);
  761.     print_list("cl: split cl dl: ", cl, __LINE__);
  762.     print_list("dl: split cl dl: ", dl, __LINE__);
  763.   }
  764.  
  765.   dl = cyclist_split(cl, cl->next);
  766.   DEBUG(0, (check_list(cl, "1") &&
  767.         check_list(dl, "93")), "cl: split cl cl->next: / dl ");
  768.  
  769.   cyclist_insert(cl, l1);
  770.   DEBUG(0, (check_list(l1, "41")), "l1 =  insert(cl,l1): ");
  771.  
  772.   cyclist_split(cl, l1);
  773.   DEBUG(0, (check_list(l1, "4") &&
  774.         check_list(cl, "1")), "l1 =  split(cl,l1):  cl =  split(cl,l1): ");
  775.  
  776.   cyclist_insert(cl, l1->prev);
  777.   DEBUG(0, (check_list(l1, "41")), "l1 =  insert(cl,l1->prev): ");
  778.  
  779.   cyclist_insert(l2, cl);
  780.   cyclist_join(cl, cyclist_split(t, t->next));
  781.   cyclist_insert(t, cl->next);
  782.  
  783.   IF_DEBUG((check_list(cl, "12349") &&
  784.         check_list(t, "34912")), "insert/join/insert 5 lists") {
  785.     PRINT_LIST(l1);
  786.     PRINT_LIST(l2);
  787.     PRINT_LIST(dl);
  788.     PRINT_LIST(cl);
  789.     PRINT_LIST(t);
  790.   }
  791.  
  792.   IF_DEBUG((check_list(cyclist_split(cl, t), "349") &&
  793.         check_list(cl, "12")), "split all again") {
  794.     PRINT_LIST(l1);
  795.     PRINT_LIST(l2);
  796.     PRINT_LIST(dl);
  797.     PRINT_LIST(cl);
  798.     PRINT_LIST(t);
  799.   }
  800.  
  801.   cyclist_join(cl, t);
  802.   // =====
  803.   IF_DEBUG((check_list(dl, "91234") &&
  804.         check_vlist(cyclist_replace(s = &clget(++count)->list, dl),
  805.                1, 2, 3, 4, 5, 0) &&
  806.         check_list(dl, "9")), "replace dl->9 to new 5") {
  807.     PRINT_LIST(cl);
  808.     PRINT_LIST(dl);
  809.     PRINT_LIST(s);
  810.   }
  811.  
  812.   IF_DEBUG((check_list(dl, "9") &&
  813.         check_list(cyclist_replace(s = &clget(++count)->list, dl), "6") &&
  814.         check_list(s, "6") &&
  815.         check_list(cl, "12345")), "replace singular dl") {
  816.     PRINT_LIST(cl);
  817.     PRINT_LIST(dl);
  818.     PRINT_LIST(s);
  819.   }
  820.  
  821.   cyclist_join(dl, s);
  822.  
  823.   if (blabla) {
  824.     puts("foreach add_safe/add_tail_safe");
  825.     IF1_PRILIST(l1);
  826.     IF1_PRILIST(l2);
  827.     IF1_PRILIST(dl);
  828.     IF1_PRILIST(cl);
  829.     IF1_PRILIST(t);
  830.   }
  831.  
  832.   s = &clget(++count /*7*/)->list;
  833.   i = 0;
  834.   cyclist_foreach_safe(t, sv, dl) {
  835.     if (i == 0)
  836.       cyclist_add_safe(s, &dl, &sv);
  837.     ++i;
  838.   }
  839.   DEBUG(0, (i == 2 && check_list(dl, "796")),
  840.     "cyclist_foreach_safe, cyclist_add_safe(0)");
  841.   cyclist_del(s, &dl);
  842.  
  843.   i = 0;
  844.   cyclist_foreach_safe(t, sv, dl) {
  845.     if (i == 1)
  846.       cyclist_add_safe(s, &dl, &sv);
  847.     ++i;
  848.   }
  849.   DEBUG(0, (i == 2 && check_list(dl, "796")),
  850.     "cyclist_foreach_safe, cyclist_add_safe(1)");
  851.   cyclist_del(s, &dl);
  852.  
  853.   i = 0;
  854.   cyclist_foreach_safe(t, sv, dl) {
  855.     if (i == 0)
  856.       cyclist_add_tail_safe(s, &dl, &sv);
  857.     ++i;
  858.   }
  859.   DEBUG(0, (i == 3 && check_list(dl, "967")),
  860.     "cyclist_foreach_safe, cyclist_add_tail_safe(0)");
  861.   cyclist_del(s, &dl);
  862.  
  863.   i = 0;
  864.   cyclist_foreach_safe(t, sv, dl) {
  865.     if (i == 1)
  866.       cyclist_add_tail_safe(s, &dl, &sv);
  867.     ++i;
  868.   }
  869.   DEBUG(0, (i == 3 && check_list(dl, "967")),
  870.     "cyclist_foreach_safe, cyclist_add_tail_safe(1)");
  871.   cyclist_del(s, &dl);
  872.  
  873.   i = 0;
  874.   cyclist_foreach_rev_safe(t, sv, dl) {
  875.     if (i == 0)
  876.       cyclist_add_safe(s, &dl, &sv);
  877.     ++i;
  878.   }
  879.   DEBUG(0, (i == 3 && check_list(dl, "796")),
  880.     "cyclist_foreach_rev_safe, cyclist_add_safe(0)");
  881.   cyclist_del(s, &dl);
  882.  
  883.   i = 0;
  884.   cyclist_foreach_rev_safe(t, sv, dl) {
  885.     if (i == 1)
  886.       cyclist_add_safe(s, &dl, &sv);
  887.     ++i;
  888.   }
  889.   DEBUG(0, (i == 3 && check_list(dl, "796")),
  890.     "cyclist_foreach_rev_safe, cyclist_add_safe(1)");
  891.   cyclist_del(s, &dl);
  892.  
  893.   i = 0;
  894.   cyclist_foreach_rev_safe(t, sv, dl) {
  895.     if (i == 0)
  896.       cyclist_add_tail_safe(s, &dl, &sv);
  897.     ++i;
  898.   }
  899.   DEBUG(0, (i == 2 && check_list(dl, "967")),
  900.     "cyclist_foreach_rev_safe, cyclist_add_tail_safe(0)");
  901.   cyclist_del(s, &dl);
  902.  
  903.   i = 0;
  904.   cyclist_foreach_rev_safe(t, sv, dl) {
  905.     if (i == 1)
  906.       cyclist_add_tail_safe(s, &dl, &sv);
  907.     ++i;
  908.   }
  909.   DEBUG(0, (i == 2 && check_list(dl, "967")),
  910.     "cyclist_foreach_rev_safe, cyclist_add_tail_safe(1)");
  911.   cyclist_del(s, &dl);
  912.  
  913.   if (blabla) {
  914.     puts("foreach _del_safe() works (for current pos)");
  915.     IF1_PRILIST(dl);
  916.     IF1_PRILIST(cl);
  917.     IF1_PRILIST(s);
  918.     IF1_PRILIST(ldel);
  919.   }
  920.  
  921.   cyclist_foreach_safe(it, sv, cl) {
  922.     cyclist_del_safe(it, &cl, &sv);
  923.     cyclist_add_tail(it, &ldel);
  924.   }
  925.   DEBUG(0, (check_list(cl, "") && check_list(ldel, "12345")),
  926.     "cyclist_foreach_safe(it, sv, cl) cyclist_del_safe  cyclist_add_tail(it, &ldel)");
  927.   cyclist_foreach_safe(it, sv, ldel) {
  928.     t = it->next;
  929.     cyclist_del_safe(t, &ldel, &sv);
  930.     cyclist_add_tail(t, &cl);
  931.   }
  932.   DEBUG(0, (check_list(cl, "241") && check_list(ldel, "35")),
  933.     "cyclist_foreach_safe(it, sv, cl) cyclist_del_safe  cyclist_add_tail(it, &ldel)");
  934.   cyclist_foreach_rev_safe(it, sv, cl) {
  935.     cyclist_del_safe(it, &cl, &sv);
  936.     cyclist_add_tail(it, &ldel);
  937.   }
  938.   DEBUG(0, (check_list(cl, "") && check_list(ldel, "35142")),
  939.     "cyclist_foreach_safe(it, sv, cl) cyclist_del_safe  cyclist_add_tail(it, &ldel)");  
  940.   cyclist_foreach_rev_safe(it, sv, ldel) {
  941.     t = it->next; // delete back of direction !!!
  942.     cyclist_del_safe(t, &ldel, &sv);
  943.     cyclist_add_tail(t, &cl);
  944.   }
  945.   DEBUG(0, (check_list(cl, "3241") && check_list(ldel, "5")),
  946.     "cyclist_foreach_safe(it, sv, cl) cyclist_del_safe  cyclist_add_tail(it, &ldel)");
  947.   cyclist_foreach_rev_safe(it, sv, cl) {
  948.     t = it->prev; // delete forward
  949.     cyclist_del_safe(t, &cl, &sv);
  950.     cyclist_add_tail(t, &ldel);
  951.   }
  952.   DEBUG(0, (check_list(cl, "21") && check_list(ldel, "543")),
  953.     "cyclist_foreach_safe(it, sv, cl) cyclist_del_safe  cyclist_add_tail(it, &ldel)");
  954.  
  955.   cyclist_join(ldel, cl);
  956.   cl = 0;
  957.   IF_DEBUG((check_list(s, "7") && check_list(cl, "") &&
  958.         check_list(ldel, "54321") && check_list(t, "32154") &&
  959.         check_list(l1, "43215") && check_list(l2, "21543") &&
  960.         check_list(dl, "96") && check_list(it, "")),
  961.        "mid test lists")
  962.     err = 1;
  963.   if (err || blabla > 1) {
  964.     if (!err)
  965.       puts("mid test lists");
  966.     PRINT_LIST(s);
  967.     PRINT_LIST(cl);
  968.     PRINT_LIST(dl);
  969.     PRINT_LIST(ldel);
  970.     PRINT_LIST(t);
  971.     PRINT_LIST(l1);
  972.     PRINT_LIST(l2);
  973.     PRINT_LIST(it);
  974.     if (err)
  975.       abort();
  976.   }
  977.  
  978.   // _del_safe() works, but ... not obviously in loops // IN WORK NOW
  979.   if (blabla)
  980.     puts("foreach _del_safe() works, but ... not obviously in jump over loops");
  981.  
  982.   i = 0;
  983.   cyclist_foreach_safe(it, sv, ldel) {
  984.     t = it->next;
  985.     p = container_of(t, struct cltest, list);
  986.     if (blabla > 1)
  987.       printf("%d: %d   ", i, p->v);
  988.     i++;    
  989.     cyclist_del_safe(t, &ldel, &sv);
  990.     cyclist_add_tail(t, &cl);
  991.   }
  992.   if (blabla > 1) {
  993.     puts("");
  994.     PRINT_LIST(cl);
  995.     PRINT_LIST(ldel);
  996.   }
  997.   cyclist_join(ldel, cl);
  998.   cl = 0;
  999.   IF1_PRILIST(cl);
  1000.   IF1_PRILIST(ldel);
  1001.   DEBUG(0, (check_list(ldel, "31425") && i == 3),
  1002.     "cyclist_foreach_safe(ldel)/del_safe(t->next, cl)/join");
  1003.  
  1004.   i = 0;
  1005.   cyclist_foreach_rev_safe(it, sv, ldel) {
  1006.     t = it->prev;
  1007.     p = container_of(t, struct cltest, list);
  1008.     if (blabla > 1)
  1009.       printf("%d: %d   ", i, p->v);
  1010.     i++;    
  1011.     cyclist_del_safe(t, &ldel, &sv);
  1012.     cyclist_add_tail(t, &cl);
  1013.   }
  1014.   if (blabla > 1) {
  1015.     puts("");
  1016.     PRINT_LIST(cl);
  1017.     PRINT_LIST(ldel);
  1018.     PRINT_LIST(it);
  1019.   }
  1020.   DEBUG(0, (check_list(ldel, "34") && check_list(cl, "215") && i == 3),
  1021.     "cyclist_foreach_rev_safe(ldel)/del_safe(t->next, cl)");
  1022.  
  1023.   l1 = l2 = 0;
  1024.   i = 0;
  1025.   cyclist_foreach_safe(it, sv, ldel) {
  1026.     //    t = it->prev;
  1027.     t = it->next;
  1028.     p = container_of(t, struct cltest, list);
  1029.     if (blabla > 1)
  1030.       printf("%d: %d   ", i, p->v);
  1031.     i++;    
  1032.     cyclist_del_safe(t, &ldel, &sv);
  1033.     cyclist_add_tail(t, &cl);
  1034.   }
  1035.   if (blabla > 1) {
  1036.     puts("");
  1037.     PRINT_LIST(cl);
  1038.     PRINT_LIST(ldel);
  1039.   }
  1040.   cyclist_move(cl->prev, &ldel);
  1041.   if (blabla > 1) {
  1042.     puts("cyclist_move(cl->prev, &ldel);");
  1043.     PRINT_LIST(cl);
  1044.     PRINT_LIST(ldel);
  1045.   }
  1046.   DEBUG(0, (check_list(ldel, "34") && check_list(cl, "215") && i == 1),
  1047.     "test2: cyclist_foreach_safe(ldel)/del_safe(t->next, cl)");
  1048.  
  1049.   cyclist_foreach_rev_safe(it, sv, ldel) {
  1050.     t = it->prev;
  1051.     p = container_of(t, struct cltest, list);
  1052.     if (blabla > 1)
  1053.       printf("%d: %d   ", i, p->v);
  1054.     i++;    
  1055.     cyclist_del_safe(t, &ldel, &sv);
  1056.     cyclist_add_tail(t, &cl);
  1057.   }
  1058.   if (blabla > 1) {
  1059.     puts("");
  1060.     PRINT_LIST(cl);
  1061.     PRINT_LIST(ldel);
  1062.     PRINT_LIST(it);
  1063.   }
  1064.   DEBUG(0, (check_list(ldel, "4") && check_list(cl, "2153") && i == 2),
  1065.     "test2: cyclist_foreach_safe(ldel)/del_safe(t->next, cl)");
  1066.  
  1067.   if (blabla)
  1068.     puts("foreach _insert_safe()");
  1069.   i = 0;
  1070.   cyclist_foreach_safe(t, sv, cl) {
  1071.     p = container_of(t, struct cltest, list);
  1072.     if (blabla > 1) printf("%d: %d   ", i, p->v);
  1073.     i++;    
  1074.     if (i == 2) {
  1075.       if (blabla > 1) puts("");
  1076.       cyclist_foreach_safe(it, sv2, dl) {
  1077.     cyclist_del(it, &dl);
  1078.     p = container_of(it, struct cltest, list);
  1079.     if (blabla > 1) printf(">> %d: %d\n", i, p->v);
  1080.     cyclist_insert_safe(it, t->next, &sv);
  1081.       }
  1082.     }
  1083.   }
  1084.   cyclist_move(ldel, &cl);
  1085.   if (blabla > 1) {
  1086.     puts("");
  1087.     PRINT_LIST(cl);
  1088.     PRINT_LIST(dl);
  1089.     PRINT_LIST(ldel);
  1090.     PRINT_LIST(t);
  1091.     PRINT_LIST(it);
  1092.   }
  1093.   DEBUG(0, (i == 6 && check_list(dl, "") && check_list(cl, "2156934")),
  1094.     "cyclist_foreach_safe(t, cl) { if _insert_safe(t) }");
  1095.   dl = cyclist_split(cl, cl->prev->prev);
  1096.   i = 0;
  1097.   cyclist_foreach_rev_safe(t, sv, cl) {
  1098.     p = container_of(t, struct cltest, list);
  1099.     if (blabla > 1) printf("%d: %d   ", i, p->v);
  1100.     i++;    
  1101.     if (i == 2) {
  1102.       if (blabla > 1) puts("");
  1103.       cyclist_foreach_safe(it, sv2, dl) {
  1104.     cyclist_del(it, &dl);
  1105.     p = container_of(it, struct cltest, list);
  1106.     if (blabla > 1) printf(">> %d: %d\n", i, p->v);
  1107.     cyclist_insert_safe(it, t->prev, &sv);
  1108.       }
  1109.     }
  1110.   }
  1111.   if (blabla > 1) {
  1112.     puts("");
  1113.     PRINT_LIST(cl);
  1114.     PRINT_LIST(dl);
  1115.   }
  1116.   DEBUG(0, (i == 7 && check_list(dl, "") && check_list(cl, "2153469")),
  1117.     "cyclist_foreach_rev_safe(t, cl) { if _insert_safe(t) }");
  1118.  
  1119.   if (blabla)
  1120.     puts("foreach _move_safe()");
  1121.   dl = cyclist_split(cl, cl->prev->prev);
  1122.   i = 0;
  1123.   cyclist_foreach_rev_safe(t, sv, cl) {
  1124.     i++;    
  1125.     p = container_of(t, struct cltest, list);
  1126.     if (blabla > 1) printf("%d: %d   ", i, p->v);
  1127.     if (i & 1) {
  1128.       struct cyclist *x = t->prev;
  1129.       if (blabla > 1) puts("");
  1130.       cyclist_foreach_safe(it, sv2, dl) {
  1131.     p = container_of(it, struct cltest, list);
  1132.     if (blabla > 1) printf(">> %d: %d\n", i, p->v);
  1133.     cyclist_move_safe(it, &dl, &sv2, &x, &sv);
  1134.     break;
  1135.       }
  1136.     }
  1137.   }
  1138.   if (blabla > 1) {
  1139.     puts("");
  1140.     PRINT_LIST(cl);
  1141.     PRINT_LIST(s);
  1142.     PRINT_LIST(dl);
  1143.     PRINT_LIST(l1);
  1144.     PRINT_LIST(l2);
  1145.     PRINT_LIST(ldel);
  1146.   }
  1147.   DEBUG(0, (i == 7 && check_list(dl, "") && check_list(cl, "2195634")),
  1148.     "cyclist_foreach_rev_safe(t, cl) { if _move_safe(t) }");
  1149.  
  1150.   if (blabla)
  1151.     puts("foreach _replace_safe()");
  1152.   l2 = cl;
  1153.   l1 = cyclist_replace_safe(&clget(++count /*8*/)->list, l2, &cl, 0);
  1154.   if (blabla > 1) {
  1155.     PRINT_LIST(cl);
  1156.     PRINT_LIST(s);
  1157.     PRINT_LIST(dl);
  1158.     PRINT_LIST(l1);
  1159.     PRINT_LIST(l2);
  1160.     PRINT_LIST(ldel);
  1161.   }
  1162.   DEBUG(0, (check_list(l2, "2") && check_list(cl, "8195634")
  1163.         && check_list(l1, "1956348")),
  1164.     "cyclist_replace_safe");
  1165.  
  1166.   i = 0;
  1167.   cyclist_foreach_safe(t, sv, cl) {
  1168.     i++;    
  1169.     p = container_of(t, struct cltest, list);
  1170.     if (blabla > 1) printf("%d: %d   ", i, p->v);
  1171.     if (i == 3) {
  1172.       ldel = t;
  1173.       l1 = cyclist_replace_safe(l2, t, &cl, &sv);
  1174.     }
  1175.   }
  1176.   if (blabla > 1) {
  1177.     puts("");
  1178.     PRINT_LIST(cl);
  1179.     PRINT_LIST(s);
  1180.     PRINT_LIST(dl);
  1181.     PRINT_LIST(l1);
  1182.     PRINT_LIST(l2);
  1183.     PRINT_LIST(ldel);
  1184.   }
  1185.   DEBUG(0, (check_list(ldel, "9") && check_list(cl, "8125634")),
  1186.     "test1: foreach_safe / if i == 3 cyclist_replace_safe");
  1187.   i = 0;
  1188.   cyclist_foreach_safe(t, sv, cl) {
  1189.     i++;    
  1190.     p = container_of(t, struct cltest, list);
  1191.     if (blabla > 1) printf("%d: %d   ", i, p->v);
  1192.     if (i == 3) {
  1193.       l2 = t->next;
  1194.       l1 = cyclist_replace_safe(s, l2, &cl, &sv);
  1195.     }
  1196.   }
  1197.   if (blabla > 1) {
  1198.     puts("");
  1199.     PRINT_LIST(cl);
  1200.     PRINT_LIST(s);
  1201.     PRINT_LIST(dl);
  1202.     PRINT_LIST(l1);
  1203.     PRINT_LIST(l2);
  1204.     PRINT_LIST(ldel);
  1205.   }
  1206.   DEBUG(0, (i == 7 && check_list(l2, "5") && check_list(cl, "8127634")),
  1207.     "test2: foreach_safe / if i == 3 cyclist_replace_safe");
  1208.  
  1209.   i = 0;
  1210.   cyclist_join(ldel, l2);
  1211.   l2 = 0;
  1212.   cyclist_foreach_safe(it, sv2, ldel)
  1213.     break;
  1214.  
  1215.  
  1216.   if (blabla)
  1217.     puts("again _move_safe()");
  1218.   cyclist_foreach_safe(t, sv, cl) {
  1219.     i++;    
  1220.     p = container_of(t, struct cltest, list);
  1221.     if (blabla > 1) printf("%d: %d   ", i, p->v);
  1222.     if (i & 1) { // each odd step move 1 item from ldel to cl
  1223.       int j = 0;
  1224.       cyclist_foreach_from_safe(it, sv2, ldel) {
  1225.       if (!j++)
  1226.         cyclist_move_safe(it, &ldel, &sv2, &cl, &sv);
  1227.       else
  1228.         break;
  1229.     }
  1230.     }
  1231.     if (!(p->v & 1)) // move even values to l2
  1232.       cyclist_move_safe(t, &cl, &sv, &l2, 0);
  1233.   }
  1234.   if (blabla > 1) {
  1235.     puts("");
  1236.     PRINT_LIST(cl);
  1237.     PRINT_LIST(s);
  1238.     PRINT_LIST(dl);
  1239.     PRINT_LIST(l1);
  1240.     PRINT_LIST(l2);
  1241.     PRINT_LIST(ldel);
  1242.   }
  1243.   DEBUG(0, (i == 9 && check_list(l2, "8264")
  1244.         && check_list(ldel, "")
  1245.         && check_list(cl, "17395")),
  1246.     "complex: foreach_safe / if ... cyclist_move_safe");
  1247.  
  1248.   i = 0;
  1249.   cyclist_foreach_rev_safe(t, sv, cl) {
  1250.     cyclist_move_safe(l2, &l2, 0, &t, &sv);
  1251.     i++;
  1252.   }
  1253.   if (blabla > 1) {
  1254.     PRINT_LIST(cl);
  1255.     PRINT_LIST(s);
  1256.     PRINT_LIST(dl);
  1257.     PRINT_LIST(l1);
  1258.     PRINT_LIST(l2);
  1259.     PRINT_LIST(ldel);
  1260.   }
  1261.   DEBUG(0, (i == 5 && check_list(l2, "")
  1262.         && check_list(cl, "147632985")),
  1263.     "foreach_rev_safe(cl) / cyclist_move_safe(l2)");
  1264.  
  1265.   if (blabla)
  1266.     puts("cut/roll");
  1267.   struct cyclist list2 = {0};
  1268.   DEBUG(0, check_list(l1 = cyclist_roll(&list2), ""), "roll empty usual");
  1269.   IF1_PRILIST(l1);
  1270.   list2 = (struct cyclist){&list2, &list2};
  1271.   DEBUG(0, check_list(l2 = cyclist_roll(&list2), ""), "roll empty linus");
  1272.   IF1_PRILIST(cyclist_roll(l2));
  1273.   cyclist_cut(l1, &list2, 1);
  1274.   DEBUG(0, list2.next == &list2 && list2.next == list2.prev, "cut empty linus");
  1275.   cyclist_cut(l1, &list2, 0);
  1276.   DEBUG(0, list2.next == 0 && list2.next == list2.prev, "cut empty usual");
  1277.  
  1278.   l1 = cl;
  1279.   l2 = cyclist_split(l1, l1->next);
  1280.   cl = cyclist_split(l2, l2->next->next);
  1281.  
  1282.   IF1_PRILIST(l1);
  1283.   IF1_PRILIST(l2);
  1284.   IF1_PRILIST(cl);
  1285.  
  1286.  
  1287.   check_cutroll(l1, &list2, 0, "1", "cyclist_cut/roll 1 item usual");
  1288.   check_cutroll(l1, &list2, 1, "1", "cyclist_cut/roll 1 item linus");
  1289.   check_cutroll(l2, &list2, 0, "47", "cyclist_cut/roll 2 items usual");
  1290.   check_cutroll(l2, &list2, 1, "47", "cyclist_cut/roll 2 items linus");
  1291.   check_cutroll(cl, &list2, 0, "632985", "cyclist_cut/roll 6 items usual");
  1292.   check_cutroll(cl, &list2, 1, "632985", "cyclist_cut/roll 6 items linus");
  1293.  
  1294.   if (blabla > 1) {
  1295.     PRINT_LIST(cl);
  1296.     PRINT_LIST(l1);
  1297.     PRINT_LIST(s);
  1298.     PRINT_LIST(dl);
  1299.     PRINT_LIST(l2);
  1300.   }
  1301.  
  1302.   dl = cl->next;
  1303.   cyclist_swap(&dl, &s);
  1304.   DEBUG(0, (check_list(dl, "729856") && check_list(s, "34")),
  1305.     "again cyclist_swap in two lists");
  1306.   if (blabla > 1) {
  1307.     PRINT_LIST(cl);
  1308.     PRINT_LIST(l1);
  1309.     PRINT_LIST(s);
  1310.     PRINT_LIST(dl);
  1311.     PRINT_LIST(l2);
  1312.   }
  1313.  
  1314.   return puts("End") == EOF;
  1315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement