home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / a_arrays.zip / TEST.CPP < prev    next >
Text File  |  1994-10-31  |  5KB  |  152 lines

  1. // test associative arrays
  2.  
  3. #include <iostream.h>
  4.  
  5. #include "assoc.hpp"
  6.  
  7. static int f2(int x) { return 2 * x; }
  8. static int f3(int x) { return 3 * x; }
  9. static int f4(int x) { return 4 * x; }
  10.  
  11. void main(void)
  12.     {
  13.     const char *p;
  14.  
  15.     // test ASSOCIATION
  16.     ASSOCIATION<int> test(2);
  17.  
  18.     cout << "Testing direct ASSOCIATION with <int>\n";
  19.     if (!test)
  20.         cout << "Could not create array\n";
  21.     else
  22.         {
  23.         test.insert("voodoo",22);
  24.         test.insert("wanda",23);
  25.         test.insert("xray",24);
  26.         test.insert("yodel",25);
  27.         test.insert("zorro",26);
  28.         test.remove("voodoo");
  29.         cout << "Size of array is " << test.size() << "\n";
  30.         if (test.find("zonk") != 0)
  31.             cout << "ERROR: test[zonk] shouldn't exist " << "\n";
  32.         cout << "test[zorro] is " << *test.find("zorro") << "\n";
  33.         cout << "Keys\t\tData\n";
  34.         if ((p = test.first()) != 0)
  35.             {
  36.             do
  37.                 {
  38.                 cout << p << "\t\t" << *test.find(p) << "\n";
  39.                 } while ((p = test.next()) != 0);
  40.             }
  41.         }
  42.  
  43.  
  44.     // test ASSOCIATION
  45.     ASSOCIATION<float *> test2(4);
  46.  
  47.     cout << "\nTesting indirect ASSOCIATION with <float *>\n";
  48.     if (!test2)
  49.         cout << "Could not create array\n";
  50.     else
  51.         {
  52.         float a,b,c,d,e;
  53.  
  54.         a = 1.0;
  55.         b = 2.0;
  56.         c = 3.0;
  57.         d = 4.0;
  58.         e = 5.0;
  59.         test2.insert("alpha",&a);
  60.         test2.insert("epsilon",&e);
  61.         test2.insert("beta",&b);
  62.         test2.remove("epsilon");
  63.         test2.insert("gamma",&c);
  64.         test2.insert("delta",&d);
  65.         test2.insert("epsilon",&e);
  66.         test2.remove("epsilon");
  67.         cout << "Size of array is " << test2.size() << "\n";
  68.         if (test2.find("zonk") != 0)
  69.             cout << "ERROR: test2[zonk] shouldn't exist " << "\n";
  70.         cout << "test2[gamma] is " << **test2.find("gamma") << "\n";
  71.         cout << "Keys\t\tData\n";
  72.         if ((p = test2.first()) != 0)
  73.             {
  74.             do
  75.                 {
  76.                 cout << p << "\t\t" << **test2.find(p) << "\n";
  77.                 } while ((p = test2.next()) != 0);
  78.             }
  79.         }
  80.  
  81.     // test ASSOCIATION with function pointers
  82.     ASSOCIATION<int (*)(int)> multiplier(4);
  83.  
  84.     cout << "\nTesting ASSOCIATION with function pointers\n";
  85.     if (!multiplier)
  86.         cout << "Could not create array\n";
  87.     else
  88.         {
  89.         multiplier.insert("two times",f2);
  90.         multiplier.insert("three times",f3);
  91.         multiplier.insert("four times",f4);
  92.         cout << "Three times Five is " << (**multiplier.find("three times"))(5) << "\n\n";
  93.         }
  94.  
  95.     // test copy constructor
  96.     ASSOCIATION<int> copy(test);
  97.     if (copy.find("zorro") == 0)
  98.         cout << "Copy constructor fails\n";
  99.  
  100.     // test assigment
  101.     ASSOCIATION<int> assign;
  102.     assign = test;
  103.     if (assign.find("zorro") == 0)
  104.         cout << "Object assignment fails\n";
  105.  
  106.     // test static initialization
  107.     static const char *keys[] = { "A","B","C" };
  108.     static char *data[] = { "one","two","three" };
  109.     ASSOCIATION<char *> static_list(keys,data,sizeof(keys)/sizeof(keys[0]));
  110.     if (*static_list.find("A") != data[0])
  111.         cout << "Static initialization fails\n";
  112.  
  113.     // test ASSOC_STORED
  114.     ASSOC_STORED<int> stored(26);
  115.     char letter[] = "A";
  116.     for (int i = 1 ; i <= 26 ; i++, letter[0]++)
  117.         stored.insert(letter,i);
  118.     stored.remove("C");
  119.     if (stored.size() != 25 || *stored.find("W") != 23)
  120.         cout << "Stored association fails (insert/remove)\n";
  121.     if ((p = stored.first()) != 0)
  122.         {
  123.         cout << "Stored keys\n";
  124.         do
  125.             {
  126.             cout << p << " ";
  127.             } while ((p = stored.next()) != 0);
  128.         cout << '\n';
  129.         }
  130.     else
  131.         cout << "Stored association fails (iterating)\n";
  132.     ASSOC_STORED<int> stored_copy(stored);
  133.     ASSOC_STORED<int> stored_assigned;
  134.     stored_assigned = stored;
  135.     if (stored.first()==stored_copy.first() ||
  136.         stored.first()==stored_assigned.first() ||
  137.         stored_copy.first()==stored_assigned.first())
  138.         cout << "Stored association fails (copy constructor)\n";
  139.     if (*stored.find("G")!=7 || *stored_copy.find("G")!=7 || *stored_assigned.find("G")!=7)
  140.         cout << "Stored association fails (assignment\n";
  141.  
  142.     // test [] and () operator semantics
  143.     cout << "\nTesting operator semantics\n";
  144.     cout << "The existence of static_list(\"A\") is " << static_list("A") << "\n";
  145.     cout << "The existence of static_list(\"Z\") is " << static_list("Z") << "\n";
  146.     cout << "The rvalue of static_list[\"A\"] is \"" << static_list["A"] << "\"\n";
  147.     cout << "The rvalue of static_list[\"Z\"] is \"" << static_list["Z"] << "\"\n";
  148.     cout << "NOW the existence of static_list(\"Z\") is " << static_list("Z") << "\n";
  149.     static_list["G"] = "seven";
  150.     cout << "static_list[\"G\"] was used as an lvalue and is now \"" << static_list["G"] << "\"\n";
  151.     }
  152.