1#include <new>
2#include <vector>
3
4#include "cppunit/cppunit_proxy.h"
5
6#if defined (_STLP_USE_NAMESPACES)
7using namespace std;
8#endif
9
10//
11// TestCase class
12//
13class ConfigTest : public CPPUNIT_NS::TestCase
14{
15  CPPUNIT_TEST_SUITE(ConfigTest);
16#if !defined (STLPORT)
17  CPPUNIT_IGNORE;
18#endif
19  CPPUNIT_TEST(placement_new_bug);
20  CPPUNIT_TEST(endianess);
21  CPPUNIT_TEST(template_function_partial_ordering);
22#if !defined (_STLP_USE_EXCEPTIONS)
23  CPPUNIT_IGNORE;
24#endif
25  CPPUNIT_TEST(new_throw_bad_alloc);
26  CPPUNIT_TEST_SUITE_END();
27
28  protected:
29    void placement_new_bug();
30    void endianess();
31    void template_function_partial_ordering();
32    void new_throw_bad_alloc();
33};
34
35CPPUNIT_TEST_SUITE_REGISTRATION(ConfigTest);
36
37void ConfigTest::placement_new_bug()
38{
39#if defined (STLPORT)
40  int int_val = 1;
41  int *pint;
42  pint = new(&int_val) int();
43  CPPUNIT_ASSERT( pint == &int_val );
44#  if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
45  CPPUNIT_ASSERT( int_val != 0 );
46#  else
47  CPPUNIT_ASSERT( int_val == 0 );
48#  endif
49#endif
50}
51
52void ConfigTest::endianess()
53{
54#if defined (STLPORT)
55  int val = 0x01020304;
56  char *ptr = (char*)(&val);
57#  if defined (_STLP_BIG_ENDIAN)
58  //This test only work if sizeof(int) == 4, this is a known limitation
59  //that will be handle the day we find a compiler for which it is false.
60  CPPUNIT_ASSERT( *ptr == 0x01 ||
61                  sizeof(int) > 4 && *ptr == 0x00 );
62#  elif defined (_STLP_LITTLE_ENDIAN)
63  CPPUNIT_ASSERT( *ptr == 0x04 );
64#  endif
65#endif
66}
67
68void ConfigTest::template_function_partial_ordering()
69{
70#if defined (STLPORT)
71  vector<int> vect1(10, 0);
72  int* pvect1Front = &vect1.front();
73  vector<int> vect2(10, 0);
74  int* pvect2Front = &vect2.front();
75
76  swap(vect1, vect2);
77
78#  if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) || defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
79  CPPUNIT_ASSERT( pvect1Front == &vect2.front() );
80  CPPUNIT_ASSERT( pvect2Front == &vect1.front() );
81#  else
82  CPPUNIT_ASSERT( pvect1Front != &vect2.front() );
83  CPPUNIT_ASSERT( pvect2Front != &vect1.front() );
84#  endif
85#endif
86}
87
88void ConfigTest::new_throw_bad_alloc()
89{
90#if defined (STLPORT) && defined (_STLP_USE_EXCEPTIONS)
91  try
92  {
93  /* We try to exhaust heap memory. However, we don't actually use the
94    largest possible size_t value bus slightly less in order to avoid
95    triggering any overflows due to the allocator adding some more for
96    its internal data structures. */
97    size_t const huge_amount = size_t(-1) - 1024;
98    void* pvoid = operator new (huge_amount);
99#if !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
100    // Allocation should have fail
101    CPPUNIT_ASSERT( pvoid != 0 );
102#endif
103    // Just in case it succeeds:
104    operator delete(pvoid);
105  }
106  catch (const bad_alloc&)
107  {
108#if defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
109    // Looks like your compiler new operator finally throw bad_alloc, you can fix
110    // configuration.
111    CPPUNIT_FAIL;
112#endif
113  }
114  catch (...)
115  {
116    //We shouldn't be there:
117    //Not bad_alloc exception thrown.
118    CPPUNIT_FAIL;
119  }
120#endif
121}
122