File: SmallObjectTest.h

package info (click to toggle)
libloki 0.1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,608 kB
  • sloc: cpp: 30,475; ansic: 1,974; makefile: 365; php: 316; perl: 108
file content (176 lines) | stat: -rw-r--r-- 3,265 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
///////////////////////////////////////////////////////////////////////////////
// Unit Test for Loki
//
// Copyright Terje Sletteb and Pavel Vozenilek 2002.
//
// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef SMALLOBJECTTEST_H
#define SMALLOBJECTTEST_H

// $Id: SmallObjectTest.h 760 2006-10-17 20:36:13Z syntheticpp $


#include <cstdlib>
#include <loki/SmallObj.h>
#include "UnitTest.h"

///////////////////////////////////////////////////////////////////////////////
// SmallObjectTest
///////////////////////////////////////////////////////////////////////////////

class SmallObjectTest : public Test
{
public:
  SmallObjectTest() : Test("SmallObject.h") {}

  virtual void execute(TestResult &result)
    {
    printName(result);

    using namespace Loki;

    bool r;

    SmallClass* a = new SmallClass;
    delete a;

    bool smallTest1=a!=NULL;

    a = new SmallClass2;
    delete a;

    bool smallTest2=a!=NULL;

    BigClass* b = new BigClass;
    delete b;

    bool bigTest1=b!=NULL;

    b = new BigClass2;
    delete b;

    bool bigTest2=b!=NULL;

    char* buff = static_cast<char*>(Loki::SmallObject<>::operator new(10));
    Loki::SmallObject<>::operator delete(buff, 10);

    bool test=buff!=NULL;

//    stress_test();

    r=smallTest1 && smallTest2 && bigTest1 && bigTest2 && test;

    testAssert("SmallObject",r,result);

    std::cout << '\n';
    }

private:
  class SmallClass : public Loki::SmallObject<>
  {
    int a;
  };

  class SmallClass2 : public SmallClass
  {
    int b;
  };

  class BigClass : public Loki::SmallObject<>
  {
    char a[200];
  };

  class BigClass2 : public BigClass
  {
    int b;
  };

  class Base
  {
  public:
    virtual ~Base() {}
  };

  class A : public Base, public Loki::SmallObject<>
  {
    int a[1];
  };

  class B : public Base, public Loki::SmallObject<>
  {
    int a[2];
  };

  class C : public Base, public Loki::SmallObject<>
  {
    int a[3];
  };

  class D : public Base, public Loki::SmallObject<>
  {
    int a[4];
  };

  static void stress_test()
  {
    std::vector<Base*> vec;

    vec.reserve(20 * 1024);

    std::srand(1231);

    for (int i = 0; i < 10; ++i)
    {
      for (int j = 0; j < 2 * 1024; ++j)
      {
        Base* p;

        switch (std::rand() % 4)
        {
          case 0: p = new A; break;
          case 1: p = new B; break;
          case 2: p = new C; break;
          case 3: p = new D; break;
        }

        vec.push_back(p);
      }

      for (int j = 0; j < 1024; ++j)
      {
        size_t pos = std::rand() % vec.size();

        delete vec[pos];

        vec[pos] = 0;
      }
    }

    while (!vec.empty())
    {
      delete vec.back();

      vec.pop_back();
    }
  }
} smallObjectTest;

#ifndef SMALLOBJ_CPP
#    define SMALLOBJ_CPP
#    ifdef LOKI_NONCC
#         include "../../include/noncc/loki/SmallObj.cpp"
#    else
#         include "../../src/SmallObj.cpp"
#    endif
#endif



#endif