home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / c++advio 2.3 / Advanced i⁄o / vvoc.cc < prev   
Encoding:
Text File  |  1997-05-05  |  8.8 KB  |  275 lines  |  [TEXT/ttxt]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. //        Verify the vocabulary operations
  3. //
  4. // $Id: vvoc.cc,v 1.1 1995/02/06 20:54:33 oleg Exp oleg $
  5.  
  6.  
  7. #include "myenv.h"
  8. #include <std.h>
  9. #include <fstream.h>
  10.  
  11. #include "voc.h"
  12.  
  13.                 // Testing unloading/loading of a vocabulary
  14. static void test_loading(void)
  15. {
  16.   cout << "\nTest of unloading/loading of a vocabulary" << endl;
  17.  
  18.   const char * temp_name = tmpnam(0);
  19.   
  20.   cout << "\tCreating a sample envelop vocabulary" << endl;
  21.   Voc& voc_env = new_Voc("Load1Env","Sample Envelop Voc 1");
  22.   {
  23.     VocPathMark mark;
  24.     cout << "\tCreating subvocs in the envelop vocabulary" << endl;
  25.     Voc& voc_sub = new_subVoc("Load2Voc","Voc to unload");
  26.     VocPath::push("Load2Voc");
  27.     assert( voc_sub.q_empty() );
  28.     new_DoubleVocItem("load2item1",3.5);
  29.     new_IntVocItem("load2item2",91);
  30.     new_StrVocItem("load2item3","string\"\"");
  31.     cout << "\tunloading the subvoc to " << temp_name << endl;
  32.     ofstream outs(temp_name);
  33.     outs << voc_sub;
  34.   }
  35.   
  36.   cout << "\tCheck to make sure the subvoc is out of path" << endl;
  37.   assert( VocPath::find_with_default("load2item2",11) == 11 );
  38.   assert( &(VocPath::find_with_default("Load2Env",Dummy_Voc)) == &Dummy_Voc );
  39.   
  40.   cout << "\tCreating a proxy" << endl;
  41.   new_VocProxy("Load2Voc",temp_name);
  42.  
  43.   cout << "\tReferencing a proxy" << endl;
  44.   VocPath::push("Load2Voc");
  45.   assert( VocPath::find_double("load2item1") == 3.5 );
  46.   assert( VocPath::find_with_default("load2item2",11) == 91 );
  47.   assure( strcmp(VocPath::find_str("load2item3"),"string\"\"") == 0, "find_str failed" );
  48.   
  49.   cout << "\nDone\n";    
  50. }
  51.  
  52.                 // Testing lookups in the several vocabularies
  53.                 // (within the path) and operations on VocPath
  54. static void test_vocpath(void)
  55. {
  56.   cout << "\nTest searching/updating the Voc path" << endl;
  57.  
  58.   {
  59.     cout << "\tCreating a sample vocabulary for reference" << endl;
  60.     Voc& voc_aux = new_Voc("Path1Voc","Sample Voc 1");
  61.     assert( voc_aux.q_empty() );
  62.     new_DoubleVocItem("path1item1",4.0);
  63.     new_IntVocItem("path1item2",7);
  64.     assert( voc_aux.count() == 2 );
  65.   }
  66.  
  67.   {
  68.     cout << "\tCreating another vocabulary for reference" << endl;
  69.     Voc& voc_aux = new_Voc("Path2Voc","Sample Voc 2");
  70.     assert( voc_aux.q_empty() );
  71.     new_DoubleVocItem("path1item1",5.0);
  72.     new_IntVocItem("path1item2",8);
  73.     assert( voc_aux.count() == 2 );
  74.   }
  75.  
  76.   {
  77.     cout << "\tCheck retrieving items" << endl;
  78.     assert( VocPath::find_double("path1item1") == 5.0);
  79.     assert( VocPath::find_int("path1item2") == 8 );
  80.     assert( VocPath::find_with_default("path1item1",6.0) == 5.0);
  81.     assert( VocPath::find_with_default("path1item2",7.1) == 7.1);
  82.     VocPath::pop();
  83.     assert( VocPath::find_double("path1item1") == 4.0);
  84.     assert( VocPath::find_int("path1item2") == 7 );
  85.     assert( VocPath::find_with_default("path1item1",6.0) == 4.0);
  86.   }
  87.  
  88.   VocPathMark mark;
  89.   {
  90.     cout << "\tCreating subvocs in the 1st vocabulary" << endl;
  91.     Voc& voc_sub = new_subVoc("Path3Voc","Sample Voc 3");
  92.     VocPath::push("Path3Voc");
  93.     assert( voc_sub.q_empty() );
  94.     new_IntVocItem("path1item1",15);
  95.     new_IntVocItem("path1item2",9);
  96.     assert( voc_sub.count() == 2 );
  97.     Voc& voc_sub2 = new_subVoc("Path4Voc","Sample Voc 4");
  98.     VocPath::push("Path4Voc");
  99.     VocPath::print();
  100.   }
  101.  
  102.   {
  103.     cout << "\tCheck retrieving items" << endl;
  104.     assert( VocPath::find_double("path1item1") == 4.0);
  105.     assert( VocPath::find_int("path1item2") == 9 );
  106.     assert( VocPath::find_with_default("path1item1",6.0) == 4.0);
  107.     assert( VocPath::find_with_default("path1item1",7) == 15);
  108.     mark.back_off();
  109.     assert( VocPath::find_int("path1item2") == 7 );
  110.   }
  111.  
  112.   cout << "\nDone\n";
  113. }
  114.  
  115.                 // Test writing/reading of a vocabulary
  116. static void test_io(void)
  117. {
  118.   cout << "\nTest reading/writing of a vocabulary" << endl;
  119.  
  120.   {
  121.     cout << "\tCreating a sample vocabulary for reference" << endl;
  122.     Voc& voc_aux = new_Voc("SampleRefVoc","Sample IO Voc");
  123.     assert( voc_aux.q_empty() );
  124.     new_DoubleVocItem("aux voc item 1",4.0);
  125.     new_IntVocItem("aux voc item 2",7);
  126.     assert( voc_aux.count() == 2 );
  127.   }
  128.  
  129.   cout << "\tCreating a sample vocabulary for io" << endl;
  130.   Voc& voc1 = new_Voc("SampleVoc","Sample IO Voc");
  131.   assert( voc1.q_empty() );
  132.   assert( voc1.count() == 0 );
  133.  
  134.   cout << "\tAdd some items in the voc1" << endl;
  135.   new_IntVocItem("1st item",4);
  136.   new_DoubleVocItem("2nd item",5.0);
  137.   new_StrVocItem("3d  item","sample string for io!");
  138.   new_StrVocItem("4th  item","sample string\\n with a \" sign");
  139.   Voc& subvoc = new_subVoc("5th item","a subvoc");
  140.   VocPath::push(subvoc.q_name());
  141.   new_IntVocItem("subvoc 1st item",14);
  142.   VocPath::pop();
  143.   assert( subvoc.count() == 1 );
  144.   new_subVoc("7th item","empty subvoc");
  145.   new_VocRefItem("SampleRefVoc");
  146.   assert( voc1.count() == 7 );
  147.   voc1.print("vocabulary before dumping");
  148.   cout << voc1;
  149.  
  150.   const char * temp_name = tmpnam(0);
  151.   {
  152.     cout << "dumping the vocabulary into the file '" << temp_name << "'" 
  153.          << endl;
  154.     ofstream sout(temp_name);
  155.     sout << voc1;
  156.     assert( sout.good() );
  157.     sout.close();
  158.   }
  159.   {
  160.     cout << "reading the vocabulary from the file '" << temp_name << "'" 
  161.          << endl;
  162.     ifstream stin(temp_name);
  163.     Voc& voc_read = new_Voc(stin);
  164.     voc_read.print("read vocabulary");
  165.     assert( voc_read.find_int("1st item") == 4 );
  166.     assert( voc_read.find_with_default("2nd item",11.0) == 5.0 );
  167.     assert( strcmp(voc_read.find_str("3d  item"),
  168.            "sample string for io!") == 0 );
  169.     assert( strcmp(voc_read.find_str("4th  item"),
  170.            "sample string\\n with a \" sign") == 0 );
  171.     assert( voc_read.find_voc("5th item").count() == 1 );
  172.     assert( voc_read.find_voc("5th item").find_int("subvoc 1st item") == 14 );
  173.     assert( voc_read.find_with_default("7th item",voc1).q_empty() );
  174.     assert( voc_read.find_with_default("SampleRefVoc",voc1).count() == 2 );
  175.   }
  176. }
  177.                 // Test putting items of various types in a
  178.                 // voc and finding them
  179. static void test_search_one_voc(void)
  180. {
  181.   cout << "\nTest putting items of various types in a voc and finding them\n";
  182.  
  183.   cout << "\tCreating a sample vocabulary\n";
  184.   Voc& voc1 = new_Voc("SampleVoc","Sample Voc");
  185.   assert( voc1.q_empty() );
  186.   assert( voc1.count() == 0 );
  187.  
  188.   {    
  189.     cout << "\tAdd some items in the voc1\n";
  190.                     // Add an int item
  191.     const IntVocItem& item1 = new_IntVocItem("1st item",1);
  192.     assert( !voc1.q_empty() );
  193.  
  194.                     // Add a string item
  195.     const StrVocItem& item2 = new_StrVocItem("1st str item","First str item");
  196.   }
  197.   assert( !voc1.q_empty() );
  198.   assert( voc1.count() == 2 );
  199.   assert( strcmp(voc1.top().q_name(),"1st str item") == 0 );
  200.   assert( strcmp(voc1.bottom().q_name(),"1st item") == 0 );
  201.  
  202.   cout << "\tCreating a bigger vocabulary, voc2\n";
  203.   Voc& voc2 = new_Voc("BiggerVoc","Bigger Voc");
  204.   assert( voc2.q_empty() );
  205.   assert( voc2.count() == 0 );
  206.  
  207.   cout << "\tAdding a few items\n";
  208.   new_IntVocItem("voc2_1",10);
  209.   new_DoubleVocItem("voc2_2",1.1);
  210.   new_StrVocItem("voc2_3","str item voc2");
  211.   new_VocRefItem(voc1);
  212.  
  213.   cout << "\tChecking the top and the bottom of voc2\n";
  214.   assert( strcmp(voc2.top().q_name(),"SampleVoc") == 0 );
  215.   assert( strcmp(voc2.bottom().q_name(),"voc2_1") == 0 );
  216.   assert( voc2.count() == 4 );
  217.  
  218.   print_all_vocabularies();
  219.  
  220.   cout << "\tTrying to find all items put into voc2\n";
  221.   assert( voc2.find_double("voc2_2") == 1.1 );
  222.   assert( voc2.find_int("voc2_1") == 10 );
  223.   assert( strcmp(voc2.find_str("voc2_3"),"str item voc2") == 0 );
  224.   assert( voc2.find_voc("SampleVoc").count() == 2 );
  225.   assert( voc2.find_voc("SampleVoc").find_int("1st item") == 1 );
  226.  
  227.   cout << "\tTrying to find non-existing items (this would fail)\n";
  228. //  voc2.find_int("voc2_3");    // this should fail...
  229.  
  230.   cout << "\tChecking finding with defaults\n";
  231.   assert( voc2.find_with_default("voc2_2",-1.0) == 1.1 );
  232.   assert( voc2.find_with_default("voc2_3",-1) == -1 );
  233.   assert( voc2.find_with_default("voc2_4",-1) == -1 );
  234.   assert( strcmp(voc2.find_with_default("voc2_4","aa"),"aa") == 0 );
  235.   assert( strcmp(voc2.find_with_default("voc2_3","aa"),"str item voc2") == 0 );
  236.   assert( voc2.find_with_default("SampleVoc",voc2).count() == 2 );
  237.   assert( voc2.find_with_default("Sample Voc",voc2).count() == 4 );
  238.   assert( voc2.find_with_default("voc2_2",voc2).count() == 4 );
  239.  
  240.   cout << "\nDone\n";
  241. }
  242.  
  243.                 // Test very primitive operations
  244. static void test_very_primitive(void)
  245. {
  246.                     // Make a sample vocabulary
  247.   Voc& new_voc = new_Voc("SampleVoc","Sample Voc");
  248.   assert( new_voc.q_empty() );
  249.   assert( new_voc.count() == 0 );
  250.  
  251.                     // Add one item
  252.   const IntVocItem& item1 = new_IntVocItem("1st item",1);
  253.   assert( !new_voc.q_empty() );
  254.   assert( new_voc.count() == 1 );
  255.   item1.print("item1");
  256.  
  257.                     // Add another item
  258.   new_IntVocItem("2nd item",-1);
  259.   assert( !new_voc.q_empty() );
  260.   assert( new_voc.count() == 2 );
  261.  
  262.   print_all_vocabularies();        // and print everything
  263.   VocPath::print();
  264. }
  265.  
  266.  
  267. main(void)
  268. {
  269. //  test_very_primitive();
  270.   test_search_one_voc();
  271.   test_io();
  272.   test_vocpath();
  273.   test_loading();
  274. }
  275.