home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stl2vac.zip / STLport-4_5_3.zip / STLport-4.5.3 / test / eh / main.cpp < prev    next >
C/C++ Source or Header  |  2001-01-26  |  13KB  |  388 lines

  1. /***********************************************************************************
  2.     Main.cpp
  3.     
  4.  * Copyright (c) 1997
  5.  * Mark of the Unicorn, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute and sell this software
  8.  * and its documentation for any purpose is hereby granted without fee,
  9.  * provided that the above copyright notice appear in all copies and
  10.  * that both that copyright notice and this permission notice appear
  11.  * in supporting documentation.  Mark of the Unicorn makes no
  12.  * representations about the suitability of this software for any
  13.  * purpose.  It is provided "as is" without express or implied warranty.
  14.  
  15.  * Copyright (c) 1997
  16.  * Moscow Center for SPARC Technology
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Moscow Center for SPARC Technology makes 
  23. no
  24.  * representations about the suitability of this software for any
  25.  * purpose.  It is provided "as is" without express or implied warranty.
  26.  
  27. ***********************************************************************************/
  28. #include "Prefix.h"
  29. #include "Tests.h"
  30.  
  31. #if defined (EH_NEW_IOSTREAMS)
  32. # include <iostream>
  33. # else
  34. # include <iostream.h>
  35. #endif
  36.  
  37. #if defined(macintosh)&&(!defined(__MRC__) && !defined(__SC__)) || defined (_MAC) && defined(__MWERKS__)
  38.  
  39. # include <console.h>
  40. # include <Types.h>
  41. # include <Strings.h>
  42.  
  43. # ifdef EH_NEW_HEADERS
  44. #  include <cstdio>
  45. #  include <cstring>
  46. #  include <cassert>
  47. # else
  48. #  include <stdio.h>
  49. #  include <string.h>
  50. #  include <assert.h>
  51. # endif
  52.  
  53. # if defined ( EH_USE_SGI_STL ) 
  54. // Override assertion behavior
  55. #  include <cstdarg>
  56. //#  include <stldebug.h>
  57. void __stl_debug_message(const char * format_str, ...)
  58. {
  59.     std::va_list args;
  60.     va_start( args, format_str );
  61.     char msg[256];
  62.     std::vsnprintf(msg, sizeof(msg)/sizeof(*msg) - 1, format_str, args );
  63.     DebugStr( c2pstr(msg) );
  64. }
  65. # else
  66. /*===================================================================================
  67.     __assertion_failed  (override standard library function)
  68.  
  69.     EFFECTS: Breaks into the debugger and shows the assertion. This implementation
  70.         is Mac-specific; others could be added for other platforms.
  71. ====================================================================================*/
  72. extern "C"
  73. {
  74.     void __assertion_failed(char *condition, char *testfilename, int lineno);
  75.     void __assertion_failed(char *condition, char *testfilename, int lineno)
  76.     {
  77.         char msg[256];
  78.         std::strncpy( msg, condition, 255 );
  79.         std::strncat( msg, ": ", 255 );
  80.         std::strncat(  msg, testfilename, 255 );
  81.         std::strncat( msg, ", ", 255 );
  82.         char line[20];
  83.         std::sprintf( line, "%d", lineno );
  84.         std::strncat(  msg, line, 255 );
  85.         DebugStr( c2pstr( msg ) );
  86.     }
  87. }
  88. # endif
  89.  
  90. #endif
  91.  
  92. #include "nc_alloc.h"
  93.  
  94. #if defined (EH_NEW_HEADERS)
  95. # include <vector>
  96. # include <cstring>
  97. # else
  98. # include <vector.h>
  99. # include <string.h>
  100. #endif
  101.  
  102. #include "TestClass.h"
  103. #include "LeakCheck.h"
  104. #include "test_construct.h"
  105. #ifdef __BORLANDC__
  106. # include <except.h>
  107. #endif
  108.  
  109. # if defined(EH_USE_NAMESPACES)
  110. namespace  // dwa 1/21/00 - must use unnamed namespace here to avoid conflict under gcc using native streams
  111. {
  112. using EH_STD::cerr;
  113. using EH_STD::endl;
  114. }
  115. # endif
  116.  
  117.  
  118. /*===================================================================================
  119.     usage  (file-static helper)
  120.  
  121.     EFFECTS: Prints a message describing the command-line parameters
  122. ====================================================================================*/
  123. static void usage(const char* name)
  124. {
  125.     cerr<<name<< 
  126.         " Usage : leak_test [-n <iterations>] [-s <size>] [-l] [-e] [-q]/[-v] [-t] [test_name...]\n";
  127.     cerr<<"\t[-n <iterations>] : number of test iterations, default==100;"<<endl;
  128.     cerr<<"\t[-s <size>] : base value for random container sizes, default==1000;"<<endl;
  129.     cerr<<"\t[-e] : don't throw exceptions, test for leak in normal conditions;"<<endl;
  130. // This option was never actually used -- dwa 9/22/97
  131. //    cerr<<"\t[-i] : ignore leak errors;"<<endl;
  132.     cerr<<"\t[-q] : quiet mode;"<<endl;
  133.     cerr<<"\t[-v] : verbose mode;"<<endl;
  134.     cerr<<"\t[-t] : track each allocation;"<<endl;
  135.     cerr<<"\t[test name [test name...]] : run only some of the tests by name (default==all tests):"<<endl;
  136.     cerr<<"\t\tpossible test names are : algo vector bit_vector list slist deque set map hash_set hash_map rope string bitset valarray"<<endl;
  137.     EH_CSTD::exit(1);
  138. }
  139.  
  140. # ifdef EH_NEW_HEADERS
  141. #  include <set>
  142. # else
  143. #  include <set.h>
  144. # endif
  145.  
  146. int _STLP_CALL main(int argc, char** argv)
  147. {
  148. #if defined( __MWERKS__ ) && defined( macintosh )    // Get command line.
  149.     argc = ccommand(&argv);
  150.     // Allow the i/o window to be repositioned.
  151. //    EH_STD::string s;
  152. //    getline(EH_STD::cin, s);
  153. #endif
  154.     unsigned int niters=2;
  155.     bool run_all=true;
  156.     bool run_slist = false;
  157.     bool run_list = false;
  158.     bool run_vector = false;
  159.     bool run_bit_vector = false;
  160.     bool run_deque = false;
  161.     bool run_hash_map = false;
  162.     bool run_hash_set = false;
  163.     bool run_set = false;
  164.     bool run_map = false;
  165.     bool run_algo = false;
  166.     bool run_algobase = false;
  167.     bool run_rope = false;
  168.     bool run_string = false;
  169.     bool run_bitset = false;
  170.     bool run_valarray = false;
  171.  
  172.     int cur_argv;
  173.     char *p, *p1;
  174. #if defined (EH_NEW_IOSTREAMS)
  175.     std::ios_base::sync_with_stdio(false);
  176. #endif
  177.  
  178.     cerr << argv[0]<<" : Exception handling testsuite.\n";
  179.     cerr.flush();
  180.     
  181.     bool track_allocations = false;
  182.     // parse parameters :
  183.     // leak_test [-iterations] [-test] ...
  184.     for (cur_argv=1; cur_argv<argc; cur_argv++) {
  185.         p = argv[cur_argv];
  186.         if (*p == '-') {
  187.             switch (p[1]) {
  188.             case 'q':
  189.                 gTestController.SetVerbose(false);
  190.                 break;
  191.             case 'v':
  192.                 gTestController.SetVerbose(true);
  193.                 break;
  194. #if 0    // This option was never actually used -- dwa 9/22/97
  195.             case 'i':
  196.                 gTestController.IgnoreLeaks(true);
  197.                 break;
  198. #endif
  199.             case 'n':
  200.                 p1 = argv[++cur_argv];
  201.                 if (p1 && EH_CSTD::sscanf(p1, "%i", &niters)==1)
  202.                     cerr <<" Doing "<<niters<<" iterations\n";
  203.                 else
  204.                     usage(argv[0]);
  205.                 break;
  206.             case 't':
  207.                 track_allocations = true;
  208.                 break;
  209.             case 'e':
  210.                 gTestController.TurnOffExceptions();
  211.                 break;
  212.             case 's':
  213.                 p1 = argv[++cur_argv];
  214.                 if (p1 && EH_CSTD::sscanf(p1, "%i", &random_base)==1)
  215.                     cerr <<" Setting  "<<random_base<<" as base for random sizes.\n";
  216.                 else
  217.                     usage(argv[0]);
  218.                 break;
  219.             default:
  220.                 usage(argv[0]);
  221.                 break;
  222.             }
  223.         } else {
  224.             run_all = false;
  225.             // test name 
  226.             if (EH_CSTD::strcmp(p, "algo")==0) {
  227.                 run_algo=true;
  228.             } else if (EH_CSTD::strcmp(p, "vector")==0) {
  229.                 run_vector=true;               
  230.             } else if (EH_CSTD::strcmp(p, "bit_vector")==0) {
  231.                 run_bit_vector=true; 
  232.             } else if (EH_CSTD::strcmp(p, "list")==0) {
  233.                 run_list=true;               
  234.             } else if (EH_CSTD::strcmp(p, "slist")==0) {
  235.                 run_slist=true;               
  236.             } else if (EH_CSTD::strcmp(p, "deque")==0) {
  237.                 run_deque=true;               
  238.             } else if (EH_CSTD::strcmp(p, "set")==0) {
  239.                 run_set=true;               
  240.             } else if (EH_CSTD::strcmp(p, "map")==0) {
  241.                 run_map=true;               
  242.             } else if (EH_CSTD::strcmp(p, "hash_set")==0) {
  243.                 run_hash_set=true;               
  244.             } else if (EH_CSTD::strcmp(p, "hash_map")==0) {
  245.                 run_hash_map=true;               
  246.             } else if (EH_CSTD::strcmp(p, "rope")==0) {
  247.                 run_rope=true;               
  248.             } else if (EH_CSTD::strcmp(p, "string")==0) {
  249.                 run_string=true;               
  250.             } else if (EH_CSTD::strcmp(p, "bitset")==0) {
  251.                 run_bitset=true;               
  252.             } else if (EH_CSTD::strcmp(p, "valarray")==0) {
  253.                 run_valarray=true;               
  254.             } else {
  255.                 usage(argv[0]);
  256.             }
  257.             
  258.         }
  259.     }
  260.  
  261.     gTestController.TrackAllocations( track_allocations );
  262.  
  263.     // Over and over...
  264.     for ( unsigned i = 0; i < niters ; i++ )
  265.     {
  266.        cerr << "iteration #" << i << "\n";
  267.         if (run_all || run_algobase) {
  268.             gTestController.SetCurrentContainer("algobase");
  269.             cerr << "EH test : algobase" << endl;
  270.             test_algobase();
  271.         }
  272.         if (run_all || run_algo) {
  273.             gTestController.SetCurrentContainer("algo");
  274.             cerr << "EH test : algo" << endl;
  275.             test_algo();
  276.         }
  277.  
  278.         if (run_all || run_vector) {
  279.             gTestController.SetCurrentContainer("vector");
  280.             cerr << "EH test : vector" << endl;
  281.             test_vector();
  282.         }
  283.  
  284. #if defined( EH_BIT_VECTOR_IMPLEMENTED )
  285.         if (run_all || run_bit_vector) {
  286.             gTestController.SetCurrentContainer("bit_vector");
  287.            cerr << "EH test : bit_vector" << endl;
  288.             test_bit_vector();
  289.         }
  290. #endif
  291.  
  292.         if (run_all || run_list) {
  293.             gTestController.SetCurrentContainer("list");
  294.             cerr << "EH test : list" << endl;
  295.             test_list();
  296.         }
  297.         
  298. #if defined( EH_SLIST_IMPLEMENTED )
  299.         if (run_all || run_slist) {
  300.             gTestController.SetCurrentContainer("slist");
  301.             cerr << "EH test : slist" << endl;
  302.             test_slist();
  303.         }
  304. #endif // EH_SLIST_IMPLEMENTED
  305.  
  306.         if (run_all || run_deque) {
  307.             gTestController.SetCurrentContainer("deque");
  308.             cerr << "EH test : deque" << endl;
  309.             test_deque();
  310.         }
  311.         if (run_all || run_set) {
  312.             gTestController.SetCurrentContainer("set");
  313.             cerr << "EH test : set" << endl;
  314.             test_set();
  315.             gTestController.SetCurrentContainer("multiset");
  316.             cerr << "EH test : multiset" << endl;
  317.             test_multiset();
  318.         }
  319.  
  320.         if (run_all || run_map) {
  321.             gTestController.SetCurrentContainer("map");
  322.             cerr << "EH test : map" << endl;
  323.             test_map();
  324.             gTestController.SetCurrentContainer("multimap");
  325.             cerr << "EH test : multimap" << endl;
  326.             test_multimap();
  327.         }
  328.  
  329. #if defined( EH_HASHED_CONTAINERS_IMPLEMENTED )
  330.         if (run_all || run_hash_map) {
  331.             gTestController.SetCurrentContainer("hash_map");
  332.             cerr << "EH test : hash_map" << endl;
  333.             test_hash_map();
  334.             gTestController.SetCurrentContainer("hash_multimap");
  335.             cerr << "EH test : hash_multimap" << endl;
  336.             test_hash_multimap();
  337.         }
  338.  
  339.         if (run_all || run_hash_set) {
  340.             gTestController.SetCurrentContainer("hash_set");
  341.             cerr << "EH test : hash_set" << endl;
  342.             test_hash_set();
  343.             gTestController.SetCurrentContainer("hash_multiset");
  344.             cerr << "EH test : hash_multiset" << endl;
  345.             test_hash_multiset();
  346.         }
  347. #endif // EH_HASHED_CONTAINERS_IMPLEMENTED
  348.  
  349. #if defined( EH_ROPE_IMPLEMENTED )
  350.     // CW1.8 can't compile this for some reason!
  351. #if !( defined(__MWERKS__) && __MWERKS__ < 0x1900 )
  352.         if (run_all || run_rope) {
  353.             gTestController.SetCurrentContainer("rope");
  354.             cerr << "EH test : rope" << endl;
  355.             test_rope();
  356.         }
  357. #endif
  358. #endif // EH_ROPE_IMPLEMENTED
  359. #if defined( EH_STRING_IMPLEMENTED )
  360.         if (run_all || run_string) {
  361.             gTestController.SetCurrentContainer("string");
  362.             cerr << "EH test : string" << endl;
  363.             test_string();
  364.         }
  365. #endif
  366. #if defined( EH_BITSET_IMPLEMENTED )
  367.         if (run_all || run_bitset) {
  368.             gTestController.SetCurrentContainer("bitset");
  369.             cerr << "EH test : bitset" << endl;
  370.             test_bitset();
  371.         }
  372. #endif
  373. #if defined( EH_VALARRAY_IMPLEMENTED )
  374.         if (run_all || run_bitset) {
  375.             gTestController.SetCurrentContainer("valarray");
  376.             cerr << "EH test : valarray" << endl;
  377.             test_valarray();
  378.         }
  379. #endif
  380.     }
  381.  
  382.     gTestController.TrackAllocations( false );
  383.     
  384.     cerr << "EH test : Done\n";
  385.     return 0;
  386. }
  387.  
  388.