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