os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/allocator_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
#include <memory>
sl@0
     2
#include <vector>
sl@0
     3
sl@0
     4
#include <cstdio>
sl@0
     5
sl@0
     6
#include "cppunit/cppunit_proxy.h"
sl@0
     7
sl@0
     8
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
sl@0
     9
using namespace std;
sl@0
    10
#endif
sl@0
    11
sl@0
    12
//
sl@0
    13
// TestCase class
sl@0
    14
//
sl@0
    15
class AllocatorTest : public CPPUNIT_NS::TestCase
sl@0
    16
{
sl@0
    17
  CPPUNIT_TEST_SUITE(AllocatorTest);
sl@0
    18
  CPPUNIT_TEST(zero_allocation);
sl@0
    19
#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
sl@0
    20
  CPPUNIT_TEST(bad_alloc_test);
sl@0
    21
#endif
sl@0
    22
#if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC)
sl@0
    23
  CPPUNIT_TEST(per_thread_alloc);
sl@0
    24
#endif
sl@0
    25
  CPPUNIT_TEST_SUITE_END();
sl@0
    26
sl@0
    27
protected:
sl@0
    28
  void zero_allocation();
sl@0
    29
  void bad_alloc_test();
sl@0
    30
  void per_thread_alloc();
sl@0
    31
};
sl@0
    32
sl@0
    33
CPPUNIT_TEST_SUITE_REGISTRATION(AllocatorTest);
sl@0
    34
sl@0
    35
//
sl@0
    36
// tests implementation
sl@0
    37
//
sl@0
    38
void AllocatorTest::zero_allocation()
sl@0
    39
{
sl@0
    40
  typedef allocator<char> CharAllocator;
sl@0
    41
  CharAllocator charAllocator;
sl@0
    42
sl@0
    43
  char* buf = charAllocator.allocate(0);
sl@0
    44
  charAllocator.deallocate(buf, 0);
sl@0
    45
sl@0
    46
  charAllocator.deallocate(0, 0);
sl@0
    47
}
sl@0
    48
sl@0
    49
#if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
sl@0
    50
sl@0
    51
struct BigStruct
sl@0
    52
{
sl@0
    53
  char _data[4096];
sl@0
    54
};
sl@0
    55
sl@0
    56
void AllocatorTest::bad_alloc_test()
sl@0
    57
{
sl@0
    58
  typedef allocator<BigStruct> BigStructAllocType;
sl@0
    59
  BigStructAllocType bigStructAlloc;
sl@0
    60
sl@0
    61
  try {
sl@0
    62
    //Lets try to allocate almost 4096 Go (on most of the platforms) of memory:
sl@0
    63
    BigStructAllocType::pointer pbigStruct = bigStructAlloc.allocate(1024 * 1024 * 1024);
sl@0
    64
sl@0
    65
    //Allocation failed but no exception thrown
sl@0
    66
    CPPUNIT_ASSERT( pbigStruct != 0 );
sl@0
    67
  }
sl@0
    68
  catch (bad_alloc const&) {
sl@0
    69
  }
sl@0
    70
  catch (...) {
sl@0
    71
    //We shouldn't be there:
sl@0
    72
    //Not bad_alloc exception thrown.
sl@0
    73
    CPPUNIT_ASSERT( false );
sl@0
    74
  }
sl@0
    75
}
sl@0
    76
#endif
sl@0
    77
sl@0
    78
#if defined (STLPORT) && defined (_STLP_THREADS) && defined (_STLP_USE_PERTHREAD_ALLOC)
sl@0
    79
#  include <pthread.h>
sl@0
    80
sl@0
    81
class SharedDatas
sl@0
    82
{
sl@0
    83
public:
sl@0
    84
  typedef vector<int, per_thread_allocator<int> > thread_vector;
sl@0
    85
sl@0
    86
  SharedDatas(size_t nbElems) : threadVectors(nbElems, (thread_vector*)0) {
sl@0
    87
    pthread_mutex_init(&mutex, 0);
sl@0
    88
    pthread_cond_init(&condition, 0);
sl@0
    89
  }
sl@0
    90
sl@0
    91
  ~SharedDatas() {
sl@0
    92
    for (size_t i = 0; i < threadVectors.size(); ++i) {
sl@0
    93
      delete threadVectors[i];
sl@0
    94
    }
sl@0
    95
  }
sl@0
    96
sl@0
    97
  size_t initThreadVector() {
sl@0
    98
    size_t ret;
sl@0
    99
sl@0
   100
    pthread_mutex_lock(&mutex);
sl@0
   101
sl@0
   102
    for (size_t i = 0; i < threadVectors.size(); ++i) {
sl@0
   103
      if (threadVectors[i] == 0) {
sl@0
   104
        threadVectors[i] = new thread_vector();
sl@0
   105
        ret = i;
sl@0
   106
        break;
sl@0
   107
      }
sl@0
   108
    }
sl@0
   109
sl@0
   110
    if (ret != threadVectors.size() - 1) {
sl@0
   111
      //We wait for other thread(s) to call this method too:
sl@0
   112
      printf("Thread %d wait\n", ret);
sl@0
   113
      pthread_cond_wait(&condition, &mutex);
sl@0
   114
    }
sl@0
   115
    else {
sl@0
   116
      //We are the last thread calling this method, we signal this
sl@0
   117
      //to the other thread(s) that might be waiting:
sl@0
   118
      printf("Thread %d signal\n", ret);
sl@0
   119
      pthread_cond_signal(&condition);
sl@0
   120
    }
sl@0
   121
sl@0
   122
    pthread_mutex_unlock(&mutex);
sl@0
   123
sl@0
   124
    return ret;
sl@0
   125
  }
sl@0
   126
sl@0
   127
  thread_vector& getThreadVector(size_t index) {
sl@0
   128
    //We return other thread thread_vector instance:
sl@0
   129
    return *threadVectors[(index + 1 == threadVectors.size()) ? 0 : index + 1];
sl@0
   130
  }
sl@0
   131
sl@0
   132
private:
sl@0
   133
  pthread_mutex_t mutex;
sl@0
   134
  pthread_cond_t condition;
sl@0
   135
  vector<thread_vector*> threadVectors;
sl@0
   136
};
sl@0
   137
sl@0
   138
void* f(void* pdatas) {
sl@0
   139
  SharedDatas *psharedDatas = (SharedDatas*)pdatas;
sl@0
   140
sl@0
   141
  int threadIndex = psharedDatas->initThreadVector();
sl@0
   142
sl@0
   143
  for (int i = 0; i < 100; ++i) {
sl@0
   144
    psharedDatas->getThreadVector(threadIndex).push_back(i);
sl@0
   145
  }
sl@0
   146
sl@0
   147
  return 0;
sl@0
   148
}
sl@0
   149
sl@0
   150
void AllocatorTest::per_thread_alloc()
sl@0
   151
{
sl@0
   152
  const size_t nth = 2;
sl@0
   153
  SharedDatas datas(nth);
sl@0
   154
  pthread_t t[nth];
sl@0
   155
sl@0
   156
  size_t i;
sl@0
   157
  for (i = 0; i < nth; ++i) {
sl@0
   158
    pthread_create(&t[i], 0, f, &datas);
sl@0
   159
  }
sl@0
   160
sl@0
   161
  for (i = 0; i < nth; ++i ) {
sl@0
   162
    pthread_join(t[i], 0);
sl@0
   163
  }
sl@0
   164
}
sl@0
   165
#endif