home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / aros / source / exec / lists / tests / test.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-16  |  4.6 KB  |  261 lines

  1.  
  2. #define AROS_ALMOST_COMPATIBLE
  3.  
  4. #include <stdio.h>
  5. #include <exec/lists.h>
  6. #include <clib/exec_protos.h>
  7.  
  8. void PrintList (struct List * list)
  9. {
  10.     struct Node * node;
  11.  
  12.     assert (list);
  13.  
  14.     printf ("(struct List *)%p = { lh_Head=%p, lh_Tail=%p,\n"
  15.     "lh_TailPred=%p }\n"
  16.     , list
  17.     , list->lh_Head
  18.     , list->lh_Tail
  19.     , list->lh_TailPred
  20.     );
  21.  
  22.     for (node=GetHead(list); node; node=GetSucc(node))
  23.     {
  24.     assert (node);
  25.  
  26.     printf ("(struct Node *)%p = { ln_Succ=%p, ln_Pred=%p }\n"
  27.         , node
  28.         , node->ln_Succ
  29.         , node->ln_Pred
  30.         );
  31.     }
  32. }
  33.  
  34. int main (int argc, char ** argv)
  35. {
  36.     struct List list;
  37.     struct Node nodes[5], * node;
  38.     char names[5][10];
  39.     int t;
  40.     int error = 0;
  41.  
  42.     printf ("Test for exec-lists\n"
  43.     "The correct addresses are:\n"
  44.     "list=%p, nodes[0]=%p, nodes[1]=%p, nodes[2]=%p,\n"
  45.     "nodes[3]=%p, nodes[4]=%p\n"
  46.     , &list
  47.     , &nodes[0]
  48.     , &nodes[1]
  49.     , &nodes[2]
  50.     , &nodes[3]
  51.     , &nodes[4]
  52.     );
  53.  
  54.     /* Init list */
  55.     NewList (&list);
  56.  
  57.     /* Print empty list */
  58.     PrintList (&list);
  59.  
  60.     /* Insert some nodes */
  61.     /* Test AddHead */
  62.     Insert (&list, &nodes[2], NULL);
  63.  
  64.     /* Test AddHead with some other node already in the list */
  65.     Insert (&list, &nodes[0], NULL);
  66.  
  67.     /* Test AddTail */
  68.     Insert (&list, &nodes[4], &nodes[2]);
  69.  
  70.     /* Test Insert 2nd Node */
  71.     Insert (&list, &nodes[1], &nodes[0]);
  72.  
  73.     /* Test Insert 2nd last Node */
  74.     Insert (&list, &nodes[3], &nodes[2]);
  75.  
  76.     /* Print list with all 5 nodes */
  77.     PrintList (&list);
  78.  
  79.     /* Check list */
  80.     for (node=GetHead(&list),t=0; node; (node=GetSucc(node)),t++)
  81.     {
  82.     if (node != &nodes[t])
  83.     {
  84.         error ++;
  85.         printf ("Insert: Node %d is wrong (Is: %p, Should: %p)\n"
  86.         , t
  87.         , node
  88.         , &nodes[t]
  89.         );
  90.     }
  91.     }
  92.  
  93.     /* Init list again */
  94.     NewList (&list);
  95.  
  96.     /* Give nodes the pri 20, 10, 0, -10, -20 */
  97.     for (t=0; t<5; t++)
  98.     {
  99.     nodes[t].ln_Pri = -20 + 10*t;
  100.     }
  101.  
  102.     /* Put nodes in the list */
  103.     Enqueue (&list, &nodes[2]); /* 0 */
  104.     Enqueue (&list, &nodes[1]); /* -10 */
  105.     Enqueue (&list, &nodes[4]); /* 20 */
  106.     Enqueue (&list, &nodes[0]); /* -20 */
  107.     Enqueue (&list, &nodes[3]); /* 10 */
  108.  
  109.     /* Check list */
  110.     for (node=GetHead(&list),t=0; node; (node=GetSucc(node)),t++)
  111.     {
  112.     if (node != &nodes[t])
  113.     {
  114.         error ++;
  115.         printf ("Enqueue: Node %d is wrong (Is: %p, Should: %p, Pri=%d)\n"
  116.         , t
  117.         , node
  118.         , &nodes[t]
  119.         , node->ln_Pri
  120.         );
  121.     }
  122.     }
  123.  
  124.     /* Give nodes names */
  125.     for (t=4; t>=0; t--)
  126.     {
  127.     sprintf (names[t], "node %d", t);
  128.     nodes[t].ln_Name = names[t];
  129.     }
  130.  
  131.     node = FindName (&list, "node 0");
  132.  
  133.     if (node != &nodes[0])
  134.     {
  135.     error ++;
  136.     printf ("FindName: Node 0 is wrong (Is: %p, Should: %p)\n"
  137.         , node
  138.         , &nodes[0]
  139.         );
  140.     }
  141.  
  142.     node = FindName (&list, "node 3");
  143.  
  144.     if (node != &nodes[3])
  145.     {
  146.     error ++;
  147.     printf ("FindName: Node 3 is wrong (Is: %p, Should: %p)\n"
  148.         , node
  149.         , &nodes[3]
  150.         );
  151.     }
  152.  
  153.     node = FindName (&list, "node 4");
  154.  
  155.     if (node != &nodes[4])
  156.     {
  157.     error ++;
  158.     printf ("FindName: Node 4 is wrong (Is: %p, Should: %p)\n"
  159.         , node
  160.         , &nodes[4]
  161.         );
  162.     }
  163.  
  164.     node = RemHead (&list);
  165.  
  166.     if (node != &nodes[0])
  167.     {
  168.     error ++;
  169.     printf ("RemHead: Node 0 is wrong (Is: %p, Should: %p)\n"
  170.         , node
  171.         , &nodes[0]
  172.         );
  173.     }
  174.  
  175.     AddHead (&list, node);
  176.  
  177.     if (node != GetHead(&list))
  178.     {
  179.     error ++;
  180.     printf ("AddHead: Node 0 is wrong (Is: %p, Should: %p)\n"
  181.         , node
  182.         , &nodes[0]
  183.         );
  184.     }
  185.  
  186.     node = RemTail (&list);
  187.  
  188.     if (node != &nodes[4])
  189.     {
  190.     error ++;
  191.     printf ("RemTail: Node 4 is wrong (Is: %p, Should: %p)\n"
  192.         , node
  193.         , &nodes[4]
  194.         );
  195.     }
  196.  
  197.     AddTail (&list, node);
  198.  
  199.     if (node != GetTail(&list))
  200.     {
  201.     error ++;
  202.     printf ("AddTail: Node 4 is wrong (Is: %p, Should: %p)\n"
  203.         , node
  204.         , &nodes[4]
  205.         );
  206.     }
  207.  
  208.     /* Remove */
  209.     Remove (&nodes[0]);
  210.     node = GetHead (&list);
  211.  
  212.     if (node != &nodes[1])
  213.     {
  214.     error ++;
  215.     printf ("Remove &[0] is wrong (Is: %p, Should: %p)\n"
  216.         , node
  217.         , &nodes[1]
  218.         );
  219.     }
  220.  
  221.     Remove (&nodes[4]);
  222.     node = GetTail (&list);
  223.  
  224.     if (node != &nodes[3])
  225.     {
  226.     error ++;
  227.     printf ("Remove &[4] is wrong (Is: %p, Should: %p)\n"
  228.         , node
  229.         , &nodes[3]
  230.         );
  231.     }
  232.  
  233.     Remove (&nodes[2]);
  234.     node = GetHead (&list);
  235.  
  236.     if (node != &nodes[1])
  237.     {
  238.     error ++;
  239.     printf ("Remove &[2] (1) is wrong (Is: %p, Should: %p)\n"
  240.         , node
  241.         , &nodes[1]
  242.         );
  243.     }
  244.  
  245.     node = GetTail (&list);
  246.  
  247.     if (node != &nodes[3])
  248.     {
  249.     error ++;
  250.     printf ("Remove &[2] (3) is wrong (Is: %p, Should: %p)\n"
  251.         , node
  252.         , &nodes[3]
  253.         );
  254.     }
  255.  
  256.     if (error)
  257.     printf ("Tests yielded %d errors\n", error);
  258.     else
  259.     printf ("No errors\n");
  260. }
  261.