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