| [0b990d] | 1 | //
 | 
|---|
 | 2 | // reftest.cc
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
 | 5 | //
 | 
|---|
 | 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
 | 7 | // Maintainer: LPS
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // This file is part of the SC Toolkit.
 | 
|---|
 | 10 | //
 | 
|---|
 | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
 | 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
 | 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
 | 14 | // any later version.
 | 
|---|
 | 15 | //
 | 
|---|
 | 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
 | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 19 | // GNU Library General Public License for more details.
 | 
|---|
 | 20 | //
 | 
|---|
 | 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
 | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
 | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
 | 24 | //
 | 
|---|
 | 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
 | 26 | //
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include <util/ref/ref.h>
 | 
|---|
 | 29 | #include <util/ref/reftestx.h>
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #if HAVE_PTHREAD==1 && REF_USE_LOCKS==1
 | 
|---|
 | 32 | #include <pthread.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | using namespace std;
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | using sc::Y;
 | 
|---|
 | 38 | using sc::X;
 | 
|---|
 | 39 | using sc::Ref;
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | void
 | 
|---|
 | 42 | test1()
 | 
|---|
 | 43 | {
 | 
|---|
 | 44 |   Y *y1p = new Y;
 | 
|---|
 | 45 |   Y *y2p = new Y;
 | 
|---|
 | 46 |   X *x1p = y1p;
 | 
|---|
 | 47 |   X *x2p = y2p;
 | 
|---|
 | 48 |   void *vy1p = (void*) y1p;
 | 
|---|
 | 49 |   void *vy2p = (void*) y2p;
 | 
|---|
 | 50 |   void *vx1p = (void*) x1p;
 | 
|---|
 | 51 |   void *vx2p = (void*) x2p;
 | 
|---|
 | 52 |   Ref<Y> y1 = y1p;
 | 
|---|
 | 53 |   Ref<Y> y2 = y2p;
 | 
|---|
 | 54 |   cout << "X::nx after Ref<Y> assignment: " << X::nx << "(2)" << endl;
 | 
|---|
 | 55 |   cout << "y1->nreference() after Ref<Y> assignment: "
 | 
|---|
 | 56 |        << y1->nreference() << "(1)" << endl;
 | 
|---|
 | 57 |   Ref<X> x1(y1);
 | 
|---|
 | 58 |   Ref<X> x2(y2);
 | 
|---|
 | 59 |   cout << "X::nx after Ref<X> assignment: " << X::nx << "(2)" << endl;
 | 
|---|
 | 60 |   cout << "y1->nreference() after Ref<X> assignment: "
 | 
|---|
 | 61 |        << y1->nreference() << "(2)" << endl;
 | 
|---|
 | 62 |   x1 = y1;
 | 
|---|
 | 63 |   x2 = y2;
 | 
|---|
 | 64 |   Ref<Y> yb1, yb2;
 | 
|---|
 | 65 |   yb1 << x1;
 | 
|---|
 | 66 |   yb2 << x2;
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |   cout << "x1 = " << (void*)x1.pointer() << "(" << vx1p << ")" << endl;
 | 
|---|
 | 69 |   cout << "x2 = " << (void*)x2.pointer() << "(" << vx2p << ")" << endl;
 | 
|---|
 | 70 |   cout << "y1 = " << (void*)y1.pointer() << "(" << vy1p << ")" << endl;
 | 
|---|
 | 71 |   cout << "y2 = " << (void*)y2.pointer() << "(" << vy2p << ")" << endl;
 | 
|---|
 | 72 |   cout << "yb1 = " << (void*)yb1.pointer()
 | 
|---|
 | 73 |        << "(" << (void*)y1.pointer() << ")" << endl;
 | 
|---|
 | 74 |   cout << "yb2 = " << (void*)yb2.pointer()
 | 
|---|
 | 75 |        << "(" << (void*)y2.pointer() << ")" << endl;
 | 
|---|
 | 76 | }
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | void
 | 
|---|
 | 79 | test2()
 | 
|---|
 | 80 | {
 | 
|---|
 | 81 |   Ref<Y> y1 = new Y;
 | 
|---|
 | 82 |   Ref<Y> y2 = new Y;
 | 
|---|
 | 83 |   cout << "X::nx after Ref<Y> assignment: " << X::nx << "(2)" << endl;
 | 
|---|
 | 84 |   cout << "y1->nreference() after Ref<Y> assignment: "
 | 
|---|
 | 85 |        << y1->nreference() << "(1)" << endl;
 | 
|---|
 | 86 |   Ref<X> x1(y1);
 | 
|---|
 | 87 |   Ref<X> x2(y2);
 | 
|---|
 | 88 |   cout << "X::nx after Ref<X> assignment: " << X::nx << "(2)" << endl;
 | 
|---|
 | 89 |   cout << "y1->nreference() after Ref<X> assignment: "
 | 
|---|
 | 90 |        << y1->nreference() << "(2)" << endl;
 | 
|---|
 | 91 |   x1 = y1;
 | 
|---|
 | 92 |   x2 = y2;
 | 
|---|
 | 93 |   if (x1 > x2) {
 | 
|---|
 | 94 |       Ref<X> tmp = x1;
 | 
|---|
 | 95 |       x1 = x2;
 | 
|---|
 | 96 |       x2 = tmp;
 | 
|---|
 | 97 |       y1 << x1;
 | 
|---|
 | 98 |       y2 << x2;
 | 
|---|
 | 99 |     }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   cout << "x1 == x1: " << (x1 == x1) << "(1)";
 | 
|---|
 | 102 |   cout << " x1 == x2: " << (x1 == x2) << "(0)";
 | 
|---|
 | 103 |   cout << " x1 == y1: " << (x1 == y1) << "(1)";
 | 
|---|
 | 104 |   cout << " x1 == y2: " << (x1 == y2) << "(0)" << endl;
 | 
|---|
 | 105 |   cout << "x1 != x1: " << (x1 != x1) << "(0)";
 | 
|---|
 | 106 |   cout << " x1 != x2: " << (x1 != x2) << "(1)";
 | 
|---|
 | 107 |   cout << " x1 != y1: " << (x1 != y1) << "(0)";
 | 
|---|
 | 108 |   cout << " x1 != y2: " << (x1 != y2) << "(1)" << endl;
 | 
|---|
 | 109 |   cout << "x1 <  x1: " << (x1 <  x1) << "(0)";
 | 
|---|
 | 110 |   cout << " x1 <  x2: " << (x1 <  x2) << "(1)";
 | 
|---|
 | 111 |   cout << " x1 <  y1: " << (x1 <  y1) << "(0)";
 | 
|---|
 | 112 |   cout << " x1 <  y2: " << (x1 <  y2) << "(1)" << endl;
 | 
|---|
 | 113 |   cout << "x1 <= x1: " << (x1 <= x1) << "(1)";
 | 
|---|
 | 114 |   cout << " x1 <= x2: " << (x1 <= x2) << "(1)";
 | 
|---|
 | 115 |   cout << " x1 <= y1: " << (x1 <= y1) << "(1)";
 | 
|---|
 | 116 |   cout << " x1 <= y2: " << (x1 <= y2) << "(1)" << endl;
 | 
|---|
 | 117 |   cout << "x1 >  x1: " << (x1 >  x1) << "(0)";
 | 
|---|
 | 118 |   cout << " x1 >  x2: " << (x1 >  x2) << "(0)";
 | 
|---|
 | 119 |   cout << " x1 >  y1: " << (x1 >  y1) << "(0)";
 | 
|---|
 | 120 |   cout << " x1 >  y2: " << (x1 >  y2) << "(0)" << endl;
 | 
|---|
 | 121 |   cout << "x1 >= x1: " << (x1 >= x1) << "(1)";
 | 
|---|
 | 122 |   cout << " x1 >= x2: " << (x1 >= x2) << "(0)";
 | 
|---|
 | 123 |   cout << " x1 >= y1: " << (x1 >= y1) << "(1)";
 | 
|---|
 | 124 |   cout << " x1 >= y2: " << (x1 >= y2) << "(0)" << endl;
 | 
|---|
 | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | void
 | 
|---|
 | 128 | test3()
 | 
|---|
 | 129 | {
 | 
|---|
 | 130 |    cout << "nx = " << X::nx << "(0) (outer scope on entry)" << endl;
 | 
|---|
 | 131 |   {
 | 
|---|
 | 132 |   Ref<X> x1 = new X;
 | 
|---|
 | 133 |   Ref<X> x2 = new X;
 | 
|---|
 | 134 |   X *x2p = x2.pointer();
 | 
|---|
 | 135 |   cout << "nx = " << X::nx << "(2) (inner scope after alloc)" << endl;
 | 
|---|
 | 136 | #if REF_MANAGE
 | 
|---|
 | 137 |   x2->unmanage();
 | 
|---|
 | 138 | #endif
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |   x2 = x1.pointer();
 | 
|---|
 | 141 |   if (x1 != x1) abort();
 | 
|---|
 | 142 |   if (x2 != x1) abort();
 | 
|---|
 | 143 | #if REF_MANAGE
 | 
|---|
 | 144 |   // x2 was unmanaged, so delete it manually
 | 
|---|
 | 145 |   delete x2p;
 | 
|---|
 | 146 | #endif
 | 
|---|
 | 147 |   cout << "nx = " << X::nx << "(1) (inner scope after assign)" << endl;
 | 
|---|
 | 148 |   
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 |   int i;
 | 
|---|
 | 151 |   for (i=1000000; i; i--) {
 | 
|---|
 | 152 |       x1->reference();
 | 
|---|
 | 153 |     }
 | 
|---|
 | 154 |   for (i=1000000; i; i--) {
 | 
|---|
 | 155 |       x1->dereference();
 | 
|---|
 | 156 |     }
 | 
|---|
 | 157 |   for (i=1000000; i; i--) {
 | 
|---|
 | 158 |       Ref<X> y = x1;
 | 
|---|
 | 159 |     }
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 |   cout << "nx = " << X::nx << "(1) (inner scope)" << endl;
 | 
|---|
 | 162 |   }
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 |    cout << "nx = " << X::nx << "(0) (outer scope on exit)" << endl;
 | 
|---|
 | 165 | }
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 | #if HAVE_PTHREAD==1 && REF_USE_LOCKS==1
 | 
|---|
 | 168 | static Ref<X> sx1, sx2;
 | 
|---|
 | 169 | void *
 | 
|---|
 | 170 | test4_run(void *)
 | 
|---|
 | 171 | {
 | 
|---|
 | 172 |   for (int i=0; i<20000; i++) {
 | 
|---|
 | 173 |       Ref<X> x1 = sx1;
 | 
|---|
 | 174 |       Ref<X> x2 = sx2;
 | 
|---|
 | 175 |       x1 = x2;
 | 
|---|
 | 176 |       x1 = x2;
 | 
|---|
 | 177 |       x1 = x1;
 | 
|---|
 | 178 |       x1 = 0;
 | 
|---|
 | 179 |     }
 | 
|---|
 | 180 |   return 0;
 | 
|---|
 | 181 | }
 | 
|---|
 | 182 | void
 | 
|---|
 | 183 | test4()
 | 
|---|
 | 184 | {
 | 
|---|
 | 185 |   cout << "test4: running pthread tests" << endl;
 | 
|---|
 | 186 |   sx1 = new X;
 | 
|---|
 | 187 |   sx2 = new X;
 | 
|---|
 | 188 |   sx1->use_locks(true);
 | 
|---|
 | 189 |   sx2->use_locks(true);
 | 
|---|
 | 190 |   for (int iter=0; iter<10; iter++) {
 | 
|---|
 | 191 |       const int nthread = 2;
 | 
|---|
 | 192 |       pthread_t id[4];
 | 
|---|
 | 193 |       for (int i=0; i<nthread; i++) {
 | 
|---|
 | 194 |           pthread_create(&id[i],0,test4_run,0);
 | 
|---|
 | 195 |         }
 | 
|---|
 | 196 |       for (int i=0; i<nthread; i++) {
 | 
|---|
 | 197 |           void *v;
 | 
|---|
 | 198 |           pthread_join(id[i],&v);
 | 
|---|
 | 199 |         }
 | 
|---|
 | 200 |     }
 | 
|---|
 | 201 |   sx1 = 0;
 | 
|---|
 | 202 |   sx2 = 0;
 | 
|---|
 | 203 | }
 | 
|---|
 | 204 | #else
 | 
|---|
 | 205 | void test4() {}
 | 
|---|
 | 206 | #endif
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 | int
 | 
|---|
 | 209 | main()
 | 
|---|
 | 210 | {
 | 
|---|
 | 211 |   cout << "X::nx before test1: " << X::nx << "(0)" << endl;
 | 
|---|
 | 212 |   test1();
 | 
|---|
 | 213 |   cout << "X::nx before test2: " << X::nx << "(0)" << endl;
 | 
|---|
 | 214 |   test2();
 | 
|---|
 | 215 |   cout << "X::nx before test3: " << X::nx << "(0)" << endl;
 | 
|---|
 | 216 |   test3();
 | 
|---|
 | 217 |   cout << "X::nx before test4: " << X::nx << "(0)" << endl;
 | 
|---|
 | 218 |   test4();
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 |   cout << "X::nx before exit: " << X::nx << "(0)" << endl;
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 |   return 0;
 | 
|---|
 | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 226 | 
 | 
|---|
 | 227 | // Local Variables:
 | 
|---|
 | 228 | // mode: c++
 | 
|---|
 | 229 | // c-file-style: "CLJ"
 | 
|---|
 | 230 | // End:
 | 
|---|