TestCxxFeatures.cxx 15.1 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestCxxFeatures.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
13
14
15
16
17
18
19
20
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// .NAME TestCxxFeatures
// .SECTION Description
// Provides a reference for the set of C++ features that can be used
// by VTK.

Brad King's avatar
Brad King committed
21
#include "vtkConfigure.h"
22
23
24

//----------------------------------------------------------------------------

25
26
27
28
29
30
/* Check for known compilers.  */

#if defined(_MSC_VER)
# define VTK_CXX_MSVC
#endif

31
32
33
34
35
#if defined(__sgi) && !defined(__GNUC__)
# define VTK_CXX_SGI
# if !defined(_COMPILER_VERSION)
#  define VTK_CXX_SGI_6
# endif
36
37
#endif

38
39
40
41
42
43
44
45
46
#if defined(__HP_aCC)
# define VTK_CXX_ACC
#endif

#if defined(__SUNPRO_CC)
# define VTK_CXX_SUNPRO
#endif

#if defined(__GNUC__) && (__GNUC__ < 3)
47
48
49
50
51
# if (__GNUC__ < 3)
#  define VTK_CXX_GCC_2
# elif (__GNUC__ == 3)
#  define VTK_CXX_GCC_3
# endif
52
53
#endif

54
55
56
57
58
59
60
61
62
63
64
//----------------------------------------------------------------------------

/* Check for known compiler limitations.  */

// Check for IRIX64-6.5-CC-o32 (old SGI compiler).
#if defined(VTK_CXX_SGI_6)
# define VTK_TYPENAME /* empty */
# define VTK_CLASS_TEMPLATE_SPECIALIZATION /* empty */
#endif

// Check for MSVC.
65
#if defined(VTK_CXX_MSVC) && (_MSC_VER < 1310)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# define VTK_TYPENAME /* empty */
#endif

// Assume standard behavior if symbol is not already defined.
#if !defined(VTK_TYPENAME)
# define VTK_TYPENAME typename
#endif

// Assume standard behavior if symbol is not already defined.
#if !defined(VTK_CLASS_TEMPLATE_SPECIALIZATION)
# define VTK_CLASS_TEMPLATE_SPECIALIZATION template <>
#endif

//----------------------------------------------------------------------------

Brad King's avatar
Brad King committed
81
82
83
84
#include "vtkSystemIncludes.h"

//----------------------------------------------------------------------------

85
/* Test inclusion of sstream header.  */
86
87
88
//#if !(defined(VTK_CXX_GCC_2) || defined(VTK_CXX_ACC) || defined(VTK_CXX_SGI_6))
#if defined(VTK_CXX_GCC_3)
# include <sstream>
89
90
91
92
#endif

//----------------------------------------------------------------------------

93
94
95
96
97
98
/* Test inclusion of typeinfo header.  */

#include <typeinfo>

//----------------------------------------------------------------------------

Brad King's avatar
Brad King committed
99
/* Test use of namespaces.  */
100
101
102

#if !defined(VTK_CXX_SGI_6)
// Fails on kulu.crd IRIX64-6.5-CC-o32 (old SGI compiler).
Brad King's avatar
Brad King committed
103
104
105
106
107
108
109
110
namespace NamespaceTest {}
namespace {}
void NamespaceTestFunc() {}
namespace NamespaceTest
{
  using ::NamespaceTestFunc;
}
using namespace NamespaceTest;
111
#endif
Brad King's avatar
Brad King committed
112
113
114

//----------------------------------------------------------------------------

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Test nested classes defined outside.  */

class NestedTestOuter
{
public:
  NestedTestOuter();
  ~NestedTestOuter();
private:
  class NestedTestInner;
  NestedTestInner* Inner;
};

class NestedTestOuter::NestedTestInner
{
public:
  NestedTestInner() {}
  ~NestedTestInner() {}
};

NestedTestOuter::NestedTestOuter()
{
  this->Inner = new NestedTestInner;
}

NestedTestOuter::~NestedTestOuter()
{
  delete this->Inner;
}

//----------------------------------------------------------------------------

146
/* Test inclusion of some stl headers.  */
Ken Martin's avatar
Ken Martin committed
147
#ifdef _MSC_VER
Andy Cedilnik's avatar
Andy Cedilnik committed
148
#pragma warning (push, 2)
Ken Martin's avatar
Ken Martin committed
149
#endif
Andy Cedilnik's avatar
Andy Cedilnik committed
150

151
#include <vector>
Andy Cedilnik's avatar
Andy Cedilnik committed
152

Ken Martin's avatar
Ken Martin committed
153
154
155
#ifdef _MSC_VER
#pragma warning(pop)
#endif
Andy Cedilnik's avatar
Andy Cedilnik committed
156

Brad King's avatar
Brad King committed
157
158
#if !defined(VTK_CXX_SGI_6)
// Fails on kulu.crd IRIX64-6.5-CC-o32 (old SGI compiler).
159
160
161
162
163
void UsingStdVector()
{
  using vtkstd::vector;
  vector<int>();
}
Brad King's avatar
Brad King committed
164
#endif
165
166
167

//----------------------------------------------------------------------------

168
/* Test full template specialization of functions.  */
169
170
171
172
173
174
template <class T>
int FullySpecializedFunction(T*)
{
  return 0;
}

175
#if !defined(VTK_CXX_SGI)
176
// Fails on kulu.crd IRIX64-6.5-CC-o32 (old SGI compiler).
177
// Fails on manifold.crd IRIX64-6.5-CC-n32 (new SGI compiler).
178
179
180
181
182
template <>
int FullySpecializedFunction<int>(int*)
{
  return 1;
}
183
184
185
186
187
188
189
#else
// Let overload resolution pick this one instead.
int FullySpecializedFunction(int*)
{
  return 1;
}
#endif
190
191
192
193
194
195
196
197
198
199
200
201
202
203

int TestFullySpecializedFunction()
{
  int result = 1;
  int should_be_0 = FullySpecializedFunction(static_cast<float*>(0));
  if(should_be_0 != 0)
    {
    cerr << "FullySpecializedFunction<float*>() returned "
         << should_be_0 << ", not 0.\n";
    result = 0;
    }
  int should_be_1 = FullySpecializedFunction(static_cast<int*>(0));
  if(should_be_1 != 1)
    {    
204
    cerr << "FullySpecializedFunction(int*) returned "
205
206
207
208
209
210
211
212
         << should_be_1 << ", not 1.\n";
    result = 0;
    }
  return result;
}

//----------------------------------------------------------------------------

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Test member template of non-template.  */

class NonTemplate
{
  void* Pointer;
public:
  template <class T> void Set(T* t) { this->Pointer = t; }
  template <class T> void Get(T*& t) { t = static_cast<T*>(this->Pointer); }
};

int TestNonTemplateMemberTemplate()
{
  int x = 123;
  int* px = 0;
  NonTemplate nt;
  nt.Set(&x);
  nt.Get(px);
  return (*px == 123);
}

//----------------------------------------------------------------------------

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Test member template of template.  */

template <class T>
class OuterTemplate
{
  T* Pointer;
public:
  template <class U> void Set(U* u) { this->Pointer = u; }
  template <class U> void Get(U*& u) { u = static_cast<U*>(this->Pointer); }
};

int TestTemplateMemberTemplate()
{
  int x = 123;
  int* px = 0;
  OuterTemplate<void> nt;
  nt.Set(&x);
  nt.Get(px);
  return (*px == 123);
}

//----------------------------------------------------------------------------

258
259
/* Test use of standard "bool" type and values.  */

260
#if !defined(VTK_CXX_SGI_6)
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
bool GetFalse()
{
  return false;
}

bool GetTrue()
{
  return true;
}

int TestBool()
{
  int result = 1;
  bool should_be_false = GetFalse();
  bool should_be_true = GetTrue();
  if(should_be_false)
    {
    cerr << "GetFalse() returned " << should_be_false << ", not false.\n";
    result = 0;
    }
  if(!should_be_true)
    {
    cerr << "GetTrue() returned " << should_be_true << ", not true.\n";
    result = 0;
    }
  return result;
}
288
#endif
289
290
//----------------------------------------------------------------------------

291
292
293
294
295
296
/* Test full template specialization of classes.  */

template <class T>
struct FullySpecializedClass
{
  static int Method() { return 0; }
297
  typedef T Type;
298
299
};

300
301
VTK_CLASS_TEMPLATE_SPECIALIZATION
struct FullySpecializedClass<float>
302
303
{
  static int Method() { return 1; }
304
  typedef int Type;
305
306
};

307
308
309
310
311
312
313
314
315
316
317
template <class T>
int TestFullySpecializedClassTrait(T*)
{
  typedef VTK_TYPENAME FullySpecializedClass<T>::Type Type;
  if(static_cast<Type>(3.1) == 3.1)
    {
    return 0;
    }
  return 1;
}

318
319
320
int TestFullySpecializedClass()
{
  int result = 1;
321
  int should_be_0 = FullySpecializedClass<int>::Method();
322
323
  if(should_be_0 != 0)
    {
324
    cerr << "FullySpecializedClass<int>::Method() returned "
325
326
327
         << should_be_0 << ", not 0.\n";
    result = 0;
    }
328
  int should_be_1 = FullySpecializedClass<float>::Method();
329
330
  if(should_be_1 != 1)
    {    
331
    cerr << "FullySpecializedClass<float>::Method() returned "
332
333
334
         << should_be_1 << ", not 1.\n";
    result = 0;
    }
335
336
337
338
339
  if(!TestFullySpecializedClassTrait(static_cast<float*>(0)))
    {
    cerr << "Trait lookup of float didn't produce int.";
    result = 0;
    }
340
341
  return result;
}
342

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//----------------------------------------------------------------------------

/* Test if(int x = f()) style scoping.  */

int TestIfScopeHelper(int i)
{
  int result = 1;
  if(int x = i)
    {
    if(x != i)
      {
      cerr << "TestIfScope: x != " << i << "\n";
      result = 0;
      }
    }
  else
    {
    if(x != i)
      {
      cerr << "TestIfScope: x != " << i << "\n";
      result = 0;
      }
    }
366
367
  int x = result;
  return x;
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
}

int TestIfScope()
{
  int result = 1;
  if(!TestIfScopeHelper(1))
    {
    result = 0;
    }
  if(!TestIfScopeHelper(0))
    {
    result = 0;
    }
  return result;
}
383
384
385

//----------------------------------------------------------------------------

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Test non-type template parameter.  */

template <int I>
struct NonTypeTemplate
{
  static int GetValue() { return I; }
};

int TestNonTypeTemplate()
{
  int result = 1;
  if(NonTypeTemplate<0>::GetValue() != 0)
    {
    cerr << "NonTypeTemplate<0>::GetValue() != 0\n";
    result = 0;
    }
  if(NonTypeTemplate<1>::GetValue() != 1)
    {
    cerr << "NonTypeTemplate<1>::GetValue() != 1\n";
    result = 0;
    }
  if(NonTypeTemplate<2>::GetValue() != 2)
    {
    cerr << "NonTypeTemplate<2>::GetValue() != 2\n";
    result = 0;
    }
  return result;
}

//----------------------------------------------------------------------------

417
418
/* Test mixed type and non-type template arguments in a non-trival way.  */

419
420
#if !(defined(VTK_CXX_MSVC) && (_MSC_VER < 1300)) && !defined(__BORLANDC__)
// Visual Studio 6 and Borland do not support this fancy array template.
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
template <class T, int N>
int TestMixedTypeTemplateFunction(T (*)[N])
{
  return N;
}
int TestMixedTypeTemplate()
{
  int x2[2];
  float x3[3];
  int result = 1;
  if(TestMixedTypeTemplateFunction(&x2) != 2)
    {
    cerr << "TestMixedTypeTemplateFunction(&x2) != 2\n";
    result = 0;
    }
  if(TestMixedTypeTemplateFunction(&x3) != 3)
    {
    cerr << "TestMixedTypeTemplateFunction(&x3) != 3\n";
    result = 0;
    }
Bill Lorensen's avatar
Bill Lorensen committed
441
  return result;
442
}
443
#endif
444
445
446

//----------------------------------------------------------------------------

447
448
449
int TestBinaryWriting()
{
  int result = 1;
450
451
452
453
454
455
456
  // ios::binary does not exist on SGI and OSF cxx (DEC)
  // it failed to compile on these machines:
  // ct02_oc.crd IRIX64-6.5-CC-64 
  // manifold IRIX64-6.5-CC-n32  
  // kulu.crd IRIX64-6.5-CC-o32 
  // a62.iue.tuwien.ac.at OSF1-V5.1-cxx 
#if defined(VTK_CXX_SGI) || defined( __DECCXX_VER)
457
  ofstream fout_with_warning_C4701("TestCxxFeatures_TestBinaryWriting", ios::out );
458
#else  
459
  ofstream fout_with_warning_C4701("TestCxxFeatures_TestBinaryWriting", ios::out | ios::binary);
460
#endif
461
  if(!fout_with_warning_C4701)
462
463
464
465
466
467
468
469
470
    {
    cerr << "Error opening TestCxxFeatures_TestBinaryWriting for binary writing.\n";
    result = 0;
    }
  return result;
}

//----------------------------------------------------------------------------

Brad King's avatar
Brad King committed
471
472
473
474
475
476
477
478
479
480
481
class SafeBoolIdiomClass
{
private:
  struct SafeBoolDummy { void Dummy() {} };
  typedef void (SafeBoolDummy::* SafeBool)();
public:
  SafeBoolIdiomClass(int x): Value(x) {}
  operator SafeBool()
    {
    return this->Value? &SafeBoolDummy::Dummy : 0;
    }
482
483
484
485
  SafeBool operator !()
    {
    return this->Value? 0 : &SafeBoolDummy::Dummy;
    }
Brad King's avatar
Brad King committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
protected:
  int Value;
};

int TestSafeBoolIdiom()
{
  int result = 1;
  SafeBoolIdiomClass cTrue(1);
  SafeBoolIdiomClass cFalse(0);
  if(cTrue) {}
  else
    {
    cerr << "if(cTrue) evaluates to false.\n";
    result = 0;
    }
  if(!cTrue)
    {
    cerr << "if(!cTrue) evaluates to true.\n";
    result = 0;
    }
  if(cFalse)
    {
    cerr << "if(cFalse) evaluates to true.\n";
    result = 0;
    }
  if(!cFalse) {}
  else
    {
    cerr << "if(!cFalse) evaluates to false.\n";
    result = 0;
    }
  return result;
}

520
521
522
523
//----------------------------------------------------------------------------

/* Test use of exceptions.  */

524
525
526
527
528
#if defined(_MSC_VER)
# pragma warning (push)
# pragma warning (disable: 4702) /* Unreachable code. */
#endif

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
class TestExceptionUnwind
{
  int* pvalue;
public:
  TestExceptionUnwind(int* p): pvalue(p) {}
  ~TestExceptionUnwind() { *pvalue = 1; }
  void Use() {}
};

class ExceptionClass {};

void TestThrowException(int* p)
{
  TestExceptionUnwind unwind(p);
  unwind.Use();
  throw ExceptionClass();
}

int TestException()
{
  int value = 0;
  try
    {
    TestThrowException(&value);
    }
  catch(ExceptionClass&)
    {
    if(value)
      {
      return 1;
      }
    else
      {
      cerr << "TestExceptionUnwind object not destroyed!" << endl;
      return 0;
      }
    }
  catch(...)
    {
    cerr << "ExceptionClass not caught!" << endl;
    return 0;
    }
  cerr << "No exception caught!" << endl;
  return 0;
}

575
576
577
578
#if defined(_MSC_VER)
# pragma warning (pop)
#endif

579
580
581
582
583
584
585
//----------------------------------------------------------------------------

/* Test void return type syntax.  */

void TestVoidReturnInner() {}
void TestVoidReturnOuter()
{
586
587
  // Visual Studio 6 and MIPSpro 7.3 do not support void returns.
#if !(defined(_MSC_VER) && (_MSC_VER < 1300) || defined(_COMPILER_VERSION) && (_COMPILER_VERSION < 740))
588
589
590
591
  return TestVoidReturnInner();
#endif
}

592
593
594
595
// MIPSpro warns about type qualifiers on return types.
#if defined(_COMPILER_VERSION)
# pragma set woff 3303 // type qualifier on return is meaningless
#endif
Brad King's avatar
Brad King committed
596
597
598
599
600
// Intel C++ warns about type qualifiers on return types.
#if defined(__INTEL_COMPILER)
# pragma warning (push)
# pragma warning (disable:858) // type qualifier on return is meaningless
#endif
601

602
603
604
605
606

// aCC warns "type qualifier on return type is meaningless" - just omit the
// function on aCC builds since there is no way to suppress the warning via
// pragmas...
#if !defined(__HP_aCC)
607
void const TestVoidConstReturn() {}
608
609
#endif

610

Brad King's avatar
Brad King committed
611
612
613
614
#if defined(__INTEL_COMPILER)
# pragma warning (pop)
#endif

615
616
617
618
619
#if defined(_COMPILER_VERSION)
# pragma reset woff 3303 // type qualifier on return is meaningless
#endif


620
621
622
623
624
625
626
627
628
629
630
631
632
//-------------------------------------------------------------------
// See if the following code works on all platforms
#if defined(_MSC_VER) && defined(_DEBUG)
/* MSVC debug hook to prevent dialogs when running from DART.  */
# include <crtdbg.h>
static int TestDriverDebugReport(int type, char* message, int* retVal)
{
  (void)type; (void)retVal;
  fprintf(stderr, message);
  exit(1);
}
#endif

633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

//----------------------------------------------------------------------------

/* Test setlocale  */
#include <locale.h>
int TestSetLocale()
{
  char *oldLocale = strdup(setlocale(LC_NUMERIC,NULL));
  setlocale(LC_NUMERIC,"English");
  
  // restore the local
  if (oldLocale)
    {
    setlocale(LC_NUMERIC,oldLocale);
    delete [] oldLocale;
    return 1;
    }
  return 0;
}

Brad King's avatar
Brad King committed
653
654
//----------------------------------------------------------------------------

655
656
657
658
#define DO_TEST(x) \
  if(x()) { cout << "Passed: " #x "\n"; } \
  else { cout << "Failed: " #x "\n"; result = 1; }

659
660
int main()
{
661
  int result = 0;
662
  DO_TEST(TestFullySpecializedFunction);
663
  DO_TEST(TestNonTemplateMemberTemplate);
664
  DO_TEST(TestTemplateMemberTemplate);
665
666
#if !defined(VTK_CXX_SGI_6)
  DO_TEST(TestBool);
667
#endif
668
669
  DO_TEST(TestFullySpecializedClass);
  DO_TEST(TestIfScope);
670
  DO_TEST(TestNonTypeTemplate);
671
#if !(defined(VTK_CXX_MSVC) && (_MSC_VER < 1300)) && !defined(__BORLANDC__)
672
  DO_TEST(TestMixedTypeTemplate);
673
#endif
674
  DO_TEST(TestBinaryWriting);
Brad King's avatar
Brad King committed
675
  DO_TEST(TestSafeBoolIdiom);
676
#ifndef VTK_CXX_GCC_2 // avoid strange exception problem on debian gcc 2.95
677
  DO_TEST(TestException);
678
#endif
679
680
  DO_TEST(TestSetLocale);
  
681
682
683
684
685
686
687
688
689
690
691
#if defined(_MSC_VER) && defined(_DEBUG)
  // just call the code to shut up a linker warning
  int retVal = 0;
  if (result)
    {
    // really shouldn't be calle dunless somehting else failed
    // just want to make the compiler think it might get called
    // all this will be yanked once I see the results of this test
    TestDriverDebugReport(0, "a temp test", &retVal);
    }
#endif
692
  return result;
693
}