PageRenderTime 66ms CodeModel.GetById 15ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Examples/test-suite/java/li_boost_intrusive_ptr_runme.java

#
Java | 701 lines | 579 code | 71 blank | 51 comment | 71 complexity | 352a5cfe6611dfc08fed391d32a93e90 MD5 | raw file
  1import li_boost_intrusive_ptr.*;
  2
  3public class li_boost_intrusive_ptr_runme {
  4  static {
  5    try {
  6        System.loadLibrary("li_boost_intrusive_ptr");
  7    } catch (UnsatisfiedLinkError e) {
  8      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
  9      System.exit(1);
 10    }
 11  }
 12
 13  // Debugging flag
 14  public final static boolean debug = false;
 15
 16  public static void main(String argv[])
 17  {
 18    if (debug)
 19      System.out.println("Started");
 20
 21    li_boost_intrusive_ptr.setDebug_shared(debug);
 22
 23    // Change loop count to run for a long time to monitor memory
 24    final int loopCount = 5000; //5000;
 25    for (int i=0; i<loopCount; i++) {
 26      new li_boost_intrusive_ptr_runme().runtest();
 27      System.gc();
 28      System.runFinalization();
 29      try {
 30        if (i%100 == 0) {
 31          java.lang.Thread.sleep(1); // give some time to the lower priority finalizer thread
 32        }
 33      } catch (java.lang.InterruptedException e) {
 34      }
 35    }
 36
 37    if (debug)
 38      System.out.println("Nearly finished");
 39
 40    int countdown = 50;
 41    while (true) {
 42      System.gc();
 43      System.runFinalization();
 44      try {
 45        java.lang.Thread.sleep(100);
 46      } catch (java.lang.InterruptedException e) {
 47      }
 48      if (--countdown == 0)
 49        break;
 50      if (Klass.getTotal_count() == 1 && KlassWithoutRefCount.getTotal_count() == 0 &&
 51          li_boost_intrusive_ptr.getTotal_IgnoredRefCountingBase_count() == 0 &&
 52          KlassDerived.getTotal_count() == 0 && KlassDerivedDerived.getTotal_count() == 1)
 53        // Expect 1 Klass instance - the one global variable (GlobalValue)
 54        break;
 55    };
 56    if (Klass.getTotal_count() != 1)
 57      throw new RuntimeException("Klass.total_count=" + Klass.getTotal_count());
 58    if (KlassWithoutRefCount.getTotal_count() != 0)
 59      throw new RuntimeException("KlassWithoutRefCount.total_count=" + KlassWithoutRefCount.getTotal_count());
 60    if (li_boost_intrusive_ptr.getTotal_IgnoredRefCountingBase_count() != 0)
 61      throw new RuntimeException("IgnoredRefCountingBase.total_count=" + li_boost_intrusive_ptr.getTotal_IgnoredRefCountingBase_count());
 62    if (KlassDerived.getTotal_count() != 0)
 63      throw new RuntimeException("KlassDerived.total_count=" + KlassDerived.getTotal_count());
 64    if (KlassDerivedDerived.getTotal_count() != 0)
 65      throw new RuntimeException("KlassDerivedDerived.total_count=" + KlassDerivedDerived.getTotal_count());
 66
 67    int wrapper_count = li_boost_intrusive_ptr.intrusive_ptr_wrapper_count();
 68    if (wrapper_count != li_boost_intrusive_ptr.getNOT_COUNTING())
 69      if (wrapper_count != 1) // Expect 1 instance - the one global variable (GlobalSmartValue)
 70        throw new RuntimeException("shared_ptr wrapper count=" + wrapper_count);
 71
 72    if (debug)
 73      System.out.println("Finished");
 74  }
 75
 76  private void runtest() {
 77    // simple shared_ptr usage - created in C++
 78    {
 79      Klass k = new Klass("me oh my");
 80      String val = k.getValue();
 81      verifyValue("me oh my", val);
 82      verifyCount(1, k);
 83    }
 84
 85    // simple shared_ptr usage - not created in C++
 86    {
 87      Klass k = li_boost_intrusive_ptr.factorycreate();
 88      String val = k.getValue();
 89      verifyValue("factorycreate", val);
 90      verifyCount(1, k);
 91    }
 92
 93    // pass by shared_ptr
 94    {
 95      Klass k = new Klass("me oh my");
 96      Klass kret = li_boost_intrusive_ptr.smartpointertest(k);
 97      String val = kret.getValue();
 98      verifyValue("me oh my smartpointertest", val);
 99      verifyCount(1, k);
100      verifyIntrusiveCount(2, k);
101      verifyCount(1, kret);
102      verifyIntrusiveCount(2, kret);
103    }
104
105    // pass by shared_ptr pointer
106    {
107      Klass k = new Klass("me oh my");
108      Klass kret = li_boost_intrusive_ptr.smartpointerpointertest(k);
109      String val = kret.getValue();
110      verifyValue("me oh my smartpointerpointertest", val);
111      verifyCount(1, k);
112      verifyIntrusiveCount(2, k);
113      verifyCount(1, kret);
114      verifyIntrusiveCount(2, kret);
115    }
116
117    // pass by shared_ptr reference
118    {
119      Klass k = new Klass("me oh my");
120      Klass kret = li_boost_intrusive_ptr.smartpointerreftest(k);
121      String val = kret.getValue();
122      verifyValue("me oh my smartpointerreftest", val);
123      verifyCount(1, k);
124      verifyIntrusiveCount(2, k);
125      verifyCount(1, kret);
126      verifyIntrusiveCount(2, kret);
127    }
128
129    // pass by shared_ptr pointer reference
130    {
131      Klass k = new Klass("me oh my");
132      Klass kret = li_boost_intrusive_ptr.smartpointerpointerreftest(k);
133      String val = kret.getValue();
134      verifyValue("me oh my smartpointerpointerreftest", val);
135      verifyCount(1, k);
136      verifyIntrusiveCount(2, k);
137      verifyCount(1, kret);
138      verifyIntrusiveCount(2, kret);
139    }
140
141    // const pass by shared_ptr
142    {
143      Klass k = new Klass("me oh my");
144      Klass kret = li_boost_intrusive_ptr.constsmartpointertest(k);
145      String val = kret.getValue();
146      verifyValue("me oh my", val);
147      verifyCount(1, k);
148      verifyIntrusiveCount(2, k);
149      verifyCount(1, kret);
150      verifyIntrusiveCount(2, kret);
151    }
152
153    // const pass by shared_ptr pointer
154    {
155      Klass k = new Klass("me oh my");
156      Klass kret = li_boost_intrusive_ptr.constsmartpointerpointertest(k);
157      String val = kret.getValue();
158      verifyValue("me oh my", val);
159      verifyCount(1, k);
160      verifyIntrusiveCount(2, k);
161      verifyCount(1, kret);
162      verifyIntrusiveCount(2, kret);
163    }
164
165    // const pass by shared_ptr reference
166    {
167      Klass k = new Klass("me oh my");
168      Klass kret = li_boost_intrusive_ptr.constsmartpointerreftest(k);
169      String val = kret.getValue();
170      verifyValue("me oh my", val);
171      verifyCount(1, k);
172      verifyIntrusiveCount(2, k);
173      verifyCount(1, kret);
174      verifyIntrusiveCount(2, kret);
175    }
176
177    // pass by value
178    {
179      Klass k = new Klass("me oh my");
180      Klass kret = li_boost_intrusive_ptr.valuetest(k);
181      String val = kret.getValue();
182      verifyValue("me oh my valuetest", val);
183      verifyCount(1, k);
184      verifyCount(1, kret);
185    }
186
187    // pass by pointer
188    {
189      Klass k = new Klass("me oh my");
190      Klass kret = li_boost_intrusive_ptr.pointertest(k);
191      String val = kret.getValue();
192      verifyValue("me oh my pointertest", val);
193      verifyCount(1, k);
194      verifyCount(1, kret);
195    }
196
197    // pass by reference
198    {
199      Klass k = new Klass("me oh my");
200      Klass kret = li_boost_intrusive_ptr.reftest(k);
201      String val = kret.getValue();
202      verifyValue("me oh my reftest", val);
203      verifyCount(1, k);
204      verifyCount(1, kret);
205    }
206
207    // pass by pointer reference
208    {
209      Klass k = new Klass("me oh my");
210      Klass kret = li_boost_intrusive_ptr.pointerreftest(k);
211      String val = kret.getValue();
212      verifyValue("me oh my pointerreftest", val);
213      verifyCount(1, k);
214      verifyCount(1, kret);
215    }
216
217    // null tests
218    {
219      Klass k = null;
220
221      if (li_boost_intrusive_ptr.smartpointertest(k) != null)
222        throw new RuntimeException("return was not null");
223
224      if (li_boost_intrusive_ptr.smartpointerpointertest(k) != null)
225        throw new RuntimeException("return was not null");
226
227      if (li_boost_intrusive_ptr.smartpointerreftest(k) != null)
228        throw new RuntimeException("return was not null");
229
230      if (li_boost_intrusive_ptr.smartpointerpointerreftest(k) != null)
231        throw new RuntimeException("return was not null");
232
233      if (!li_boost_intrusive_ptr.nullsmartpointerpointertest(null).equals("null pointer"))
234        throw new RuntimeException("not null smartpointer pointer");
235
236      try { li_boost_intrusive_ptr.valuetest(k); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
237
238      if (li_boost_intrusive_ptr.pointertest(k) != null)
239        throw new RuntimeException("return was not null");
240
241      try { li_boost_intrusive_ptr.reftest(k); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
242    }
243
244    // $owner
245    {
246      Klass k = li_boost_intrusive_ptr.pointerownertest();
247      String val = k.getValue();
248      verifyValue("pointerownertest", val);
249      verifyCount(1, k);
250    }
251    {
252      Klass k = li_boost_intrusive_ptr.smartpointerpointerownertest();
253      String val = k.getValue();
254      verifyValue("smartpointerpointerownertest", val);
255      verifyCount(1, k);
256    }
257
258    ////////////////////////////////// Derived classes ////////////////////////////////////////
259    // derived access to base class which cannot be wrapped in an intrusive_ptr
260	{
261	  KlassWithoutRefCount k = new KlassDerived("me oh my");
262	  verifyValue("this class cannot be wrapped by intrusive_ptrs but we can still use it", k.getSpecialValueFromUnwrappableClass());
263	}
264    // derived pass by shared_ptr
265    {
266      KlassDerived k = new KlassDerived("me oh my");
267      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrtest(k);
268      String val = kret.getValue();
269      verifyValue("me oh my derivedsmartptrtest-Derived", val);
270      verifyIntrusiveCount(2, k);
271      verifyCount(2, k); // includes extra reference for upcast
272      verifyIntrusiveCount(2, kret);
273      verifyCount(2, kret);
274	}
275
276    // derived pass by shared_ptr pointer
277    {
278      KlassDerived k = new KlassDerived("me oh my");
279      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrpointertest(k);
280      String val = kret.getValue();
281      verifyValue("me oh my derivedsmartptrpointertest-Derived", val);
282      verifyIntrusiveCount(2, k);
283      verifyCount(2, k); // includes extra reference for upcast
284      verifyIntrusiveCount(2, kret);
285      verifyCount(2, kret);
286    }
287    // derived pass by shared_ptr ref
288    {
289      KlassDerived k = new KlassDerived("me oh my");
290      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrreftest(k);
291      String val = kret.getValue();
292      verifyValue("me oh my derivedsmartptrreftest-Derived", val);
293      verifyIntrusiveCount(2, k);
294      verifyCount(2, k); // includes extra reference for upcast
295      verifyIntrusiveCount(2, kret);
296      verifyCount(2, kret);
297    }
298    // derived pass by shared_ptr pointer ref
299    {
300      KlassDerived k = new KlassDerived("me oh my");
301      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrpointerreftest(k);
302      String val = kret.getValue();
303      verifyValue("me oh my derivedsmartptrpointerreftest-Derived", val);
304      verifyIntrusiveCount(2, k);
305      verifyCount(2, k); // includes extra reference for upcast
306      verifyIntrusiveCount(2, kret);
307      verifyCount(2, kret);
308    }
309    // derived pass by pointer
310    {
311      KlassDerived k = new KlassDerived("me oh my");
312      verifyCount(2, k); // includes an extra reference for the upcast in the proxy class
313      KlassDerived kret = li_boost_intrusive_ptr.derivedpointertest(k);
314      verifyCount(2, kret);
315      String val = kret.getValue();
316      verifyValue("me oh my derivedpointertest-Derived", val);
317      verifyIntrusiveCount(1, k); //one shared_ptr has a null deleter
318      verifyCount(2, k); // includes extra reference for upcast
319      verifyIntrusiveCount(1, kret); //one shared_ptr has a null deleter
320      verifyCount(2, kret);
321    }
322    // derived pass by ref
323    {
324      KlassDerived k = new KlassDerived("me oh my");
325      verifyCount(2, k); // includes an extra reference for the upcast in the proxy class
326      KlassDerived kret = li_boost_intrusive_ptr.derivedreftest(k);
327      verifyCount(2, kret);
328      String val = kret.getValue();
329      verifyValue("me oh my derivedreftest-Derived", val);
330      verifyIntrusiveCount(1, k); //one shared_ptr has a null deleter
331      verifyCount(2, k); // includes extra reference for upcast
332      verifyIntrusiveCount(1, kret); //one shared_ptr has a null deleter
333      verifyCount(2, kret);
334    }
335
336    ////////////////////////////////// Derived and base class mixed ////////////////////////////////////////
337    // pass by shared_ptr (mixed)
338    {
339      KlassDerived k = new KlassDerivedDerived("me oh my");
340      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrtest(k);
341      String val = kret.getValue();
342      verifyValue("me oh my derivedsmartptrtest-DerivedDerived", val);
343      verifyIntrusiveCount(2, k);
344      verifyCount(3, k); // an extra reference for the upcast in the proxy class
345      verifyIntrusiveCount(2, kret);
346      verifyCount(2, kret);
347    }
348
349    // pass by shared_ptr pointer (mixed)
350    {
351      KlassDerived k = new KlassDerivedDerived("me oh my");
352      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrpointertest(k);
353      String val = kret.getValue();
354      verifyValue("me oh my derivedsmartptrpointertest-DerivedDerived", val);
355      verifyIntrusiveCount(2, k);
356      verifyCount(3, k); // an extra reference for the upcast in the proxy class
357      verifyIntrusiveCount(2, kret);
358      verifyCount(2, kret);
359    }
360
361    // pass by shared_ptr reference (mixed)
362    {
363      KlassDerived k = new KlassDerivedDerived("me oh my");
364      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrreftest(k);
365      String val = kret.getValue();
366      verifyValue("me oh my derivedsmartptrreftest-DerivedDerived", val);
367      verifyIntrusiveCount(2, k);
368      verifyCount(3, k); // an extra reference for the upcast in the proxy class
369      verifyIntrusiveCount(2, kret);
370      verifyCount(2, kret);
371    }
372
373    // pass by shared_ptr pointer reference (mixed)
374    {
375      KlassDerived k = new KlassDerivedDerived("me oh my");
376      KlassDerived kret = li_boost_intrusive_ptr.derivedsmartptrpointerreftest(k);
377      String val = kret.getValue();
378      verifyValue("me oh my derivedsmartptrpointerreftest-DerivedDerived", val);
379      verifyIntrusiveCount(2, k);
380      verifyCount(3, k); // an extra reference for the upcast in the proxy class
381      verifyIntrusiveCount(2, kret);
382      verifyCount(2, kret);
383    }
384
385    // pass by value (mixed)
386    {
387      KlassDerived k = new KlassDerivedDerived("me oh my");
388      KlassDerived kret = li_boost_intrusive_ptr.derivedvaluetest(k);
389      String val = kret.getValue();
390      verifyValue("me oh my derivedvaluetest-Derived", val); // note slicing
391      verifyIntrusiveCount(1, k);
392      verifyCount(3, k); // an extra reference for the upcast in the proxy class
393      verifyIntrusiveCount(1, kret);
394      verifyCount(2, kret);
395    }
396
397    // pass by pointer (mixed)
398    {
399      KlassDerived k = new KlassDerivedDerived("me oh my");
400      KlassDerived kret = li_boost_intrusive_ptr.derivedpointertest(k);
401      String val = kret.getValue();
402      verifyValue("me oh my derivedpointertest-DerivedDerived", val);
403      verifyIntrusiveCount(1, k); //one shared_ptr has a null deleter
404      verifyCount(3, k); // an extra reference for the upcast in the proxy class
405      verifyIntrusiveCount(1, kret); //one shared_ptr has a null deleter
406      verifyCount(2, kret);
407    }
408
409    // pass by ref (mixed)
410    {
411      KlassDerived k = new KlassDerivedDerived("me oh my");
412      KlassDerived kret = li_boost_intrusive_ptr.derivedreftest(k);
413      String val = kret.getValue();
414      verifyValue("me oh my derivedreftest-DerivedDerived", val);
415      verifyIntrusiveCount(1, k); //one shared_ptr has a null deleter
416      verifyCount(3, k); // an extra reference for the upcast in the proxy class
417      verifyIntrusiveCount(1, kret); //one shared_ptr has a null deleter
418      verifyCount(2, kret);
419    }
420
421    ////////////////////////////////// Member variables ////////////////////////////////////////
422    // smart pointer by value
423    {
424      MemberVariables m = new MemberVariables();
425      Klass k = new Klass("smart member value");
426      m.setSmartMemberValue(k);
427      String val = k.getValue();
428      verifyValue("smart member value", val);
429      verifyIntrusiveCount(2, k);
430      verifyCount(1, k);
431
432      Klass kmember = m.getSmartMemberValue();
433      val = kmember.getValue();
434      verifyValue("smart member value", val);
435      verifyIntrusiveCount(3, kmember);
436      verifyIntrusiveCount(3, k);
437      verifyCount(1, k);
438      verifyCount(1, kmember);
439
440      m.delete();
441      verifyIntrusiveCount(2, kmember);
442      verifyIntrusiveCount(2, k);
443    }
444
445    // smart pointer by pointer
446    {
447      MemberVariables m = new MemberVariables();
448      Klass k = new Klass("smart member pointer");
449      m.setSmartMemberPointer(k);
450      String val = k.getValue();
451      verifyValue("smart member pointer", val);
452      verifyCount(1, k);
453      verifyIntrusiveCount(2, k);
454
455      Klass kmember = m.getSmartMemberPointer();
456      val = kmember.getValue();
457      verifyValue("smart member pointer", val);
458      verifyIntrusiveCount(3, kmember);
459      verifyCount(1, kmember);
460      verifyIntrusiveCount(3, k);
461      verifyCount(1, k);
462
463      m.delete();
464      verifyIntrusiveCount(2, kmember);
465      verifyCount(1, kmember);
466      verifyIntrusiveCount(2, k);
467      verifyCount(1, k);
468    }
469    // smart pointer by reference
470    {
471      MemberVariables m = new MemberVariables();
472      Klass k = new Klass("smart member reference");
473      m.setSmartMemberReference(k);
474      String val = k.getValue();
475      verifyValue("smart member reference", val);
476      verifyIntrusiveCount(2, k);
477      verifyCount(1, k);
478
479      Klass kmember = m.getSmartMemberReference();
480      val = kmember.getValue();
481      verifyValue("smart member reference", val);
482      verifyIntrusiveCount(3, kmember);
483      verifyCount(1, kmember);
484      verifyIntrusiveCount(3, k);
485      verifyCount(1, k);
486
487      // The C++ reference refers to SmartMemberValue...
488      m.setSmartMemberValue(k);
489      Klass kmemberVal = m.getSmartMemberValue();
490      val = kmember.getValue();
491      verifyValue("smart member reference", val);
492      verifyIntrusiveCount(5, kmemberVal);
493      verifyCount(1, kmemberVal);
494      verifyIntrusiveCount(5, kmember);
495      verifyCount(1, kmember);
496      verifyIntrusiveCount(5, k);
497      verifyCount(1, k);
498
499      m.delete();
500      verifyIntrusiveCount(3, kmemberVal);
501      verifyCount(1, kmemberVal);
502      verifyIntrusiveCount(3, kmember);
503      verifyCount(1, kmember);
504      verifyIntrusiveCount(3, k);
505      verifyCount(1, k);
506    }
507
508    //plain by value
509    {
510      MemberVariables m = new MemberVariables();
511      Klass k = new Klass("plain member value");
512      m.setMemberValue(k);
513      String val = k.getValue();
514      verifyValue("plain member value", val);
515      verifyCount(1, k);
516
517      Klass kmember = m.getMemberValue();
518      val = kmember.getValue();
519      verifyValue("plain member value", val);
520      verifyCount(1, kmember);
521      verifyCount(1, k);
522
523      m.delete();
524      verifyCount(1, kmember);
525      verifyCount(1, k);
526    }
527    //plain by pointer
528    {
529      MemberVariables m = new MemberVariables();
530      Klass k = new Klass("plain member pointer");
531      m.setMemberPointer(k);
532      String val = k.getValue();
533      verifyValue("plain member pointer", val);
534      verifyCount(1, k);
535
536      Klass kmember = m.getMemberPointer();
537      val = kmember.getValue();
538      verifyValue("plain member pointer", val);
539      verifyCount(1, kmember);
540      verifyCount(1, k);
541
542      m.delete();
543      verifyCount(1, kmember);
544      verifyCount(1, k);
545    }
546    //plain by reference
547    {
548      MemberVariables m = new MemberVariables();
549      Klass k = new Klass("plain member reference");
550      m.setMemberReference(k);
551      String val = k.getValue();
552      verifyValue("plain member reference", val);
553      verifyCount(1, k);
554
555      Klass kmember = m.getMemberReference();
556      val = kmember.getValue();
557      verifyValue("plain member reference", val);
558      verifyCount(1, kmember);
559      verifyCount(1, k);
560
561      m.delete();
562      verifyCount(1, kmember);
563      verifyCount(1, k);
564    }
565    //null member variables
566    {
567      MemberVariables m = new MemberVariables();
568
569      // shared_ptr by value
570      Klass k = m.getSmartMemberValue();
571      if (k != null)
572        throw new RuntimeException("expected null");
573      m.setSmartMemberValue(null);
574      k = m.getSmartMemberValue();
575      if (k != null)
576        throw new RuntimeException("expected null");
577      verifyCount(0, k);
578
579      // plain by value
580      try { m.setMemberValue(null); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
581    }
582}
583private void toIgnore() {
584    ////////////////////////////////// Global variables ////////////////////////////////////////
585    // smart pointer
586    {
587      Klass kglobal = li_boost_intrusive_ptr.getGlobalSmartValue();
588      if (kglobal != null)
589        throw new RuntimeException("expected null");
590
591      Klass k = new Klass("smart global value");
592      li_boost_intrusive_ptr.setGlobalSmartValue(k);
593      verifyIntrusiveCount(2, k);
594      verifyCount(1, k);
595
596      kglobal = li_boost_intrusive_ptr.getGlobalSmartValue();
597      String val = kglobal.getValue();
598      verifyValue("smart global value", val);
599      verifyIntrusiveCount(3, kglobal);
600      verifyCount(1, kglobal);
601      verifyIntrusiveCount(3, k);
602      verifyCount(1, k);
603      verifyValue("smart global value", li_boost_intrusive_ptr.getGlobalSmartValue().getValue());
604      li_boost_intrusive_ptr.setGlobalSmartValue(null);
605    }
606    // plain value
607    {
608      Klass kglobal;
609
610      Klass k = new Klass("global value");
611      li_boost_intrusive_ptr.setGlobalValue(k);
612      verifyCount(1, k);
613
614      kglobal = li_boost_intrusive_ptr.getGlobalValue();
615      String val = kglobal.getValue();
616      verifyValue("global value", val);
617      verifyCount(1, kglobal);
618      verifyCount(1, k);
619      verifyValue("global value", li_boost_intrusive_ptr.getGlobalValue().getValue());
620
621      try { li_boost_intrusive_ptr.setGlobalValue(null); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
622    }
623    //plain pointer
624    {
625      Klass kglobal = li_boost_intrusive_ptr.getGlobalPointer();
626      if (kglobal != null)
627        throw new RuntimeException("expected null");
628
629      Klass k = new Klass("global pointer");
630      li_boost_intrusive_ptr.setGlobalPointer(k);
631      verifyCount(1, k);
632
633      kglobal = li_boost_intrusive_ptr.getGlobalPointer();
634      String val = kglobal.getValue();
635      verifyValue("global pointer", val);
636      verifyCount(1, kglobal);
637      verifyCount(1, k);
638      li_boost_intrusive_ptr.setGlobalPointer(null);
639    }
640
641    // plain reference
642    {
643      Klass kglobal;
644
645      Klass k = new Klass("global reference");
646      li_boost_intrusive_ptr.setGlobalReference(k);
647      verifyCount(1, k);
648
649      kglobal = li_boost_intrusive_ptr.getGlobalReference();
650      String val = kglobal.getValue();
651      verifyValue("global reference", val);
652      verifyCount(1, kglobal);
653      verifyCount(1, k);
654
655      try { li_boost_intrusive_ptr.setGlobalReference(null); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
656    }
657
658    ////////////////////////////////// Templates ////////////////////////////////////////
659    {
660      PairIntDouble pid = new PairIntDouble(10, 20.2);
661      if (pid.getBaseVal1() != 20 || pid.getBaseVal2() != 40.4)
662        throw new RuntimeException("Base values wrong");
663      if (pid.getVal1() != 10 || pid.getVal2() != 20.2)
664        throw new RuntimeException("Derived Values wrong");
665    }
666  }
667  private void verifyValue(String expected, String got) {
668    if (!expected.equals(got))
669      throw new RuntimeException("verify value failed. Expected: " + expected + " Got: " + got);
670  }
671  private void verifyCount(int expected, Klass k) {
672    int got = li_boost_intrusive_ptr.use_count(k);
673    if (expected != got)
674      throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
675  }
676  private void verifyCount(int expected, KlassDerived kd) {
677      int got = li_boost_intrusive_ptr.use_count(kd);
678      if (expected != got)
679        throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
680  }
681  private void verifyCount(int expected, KlassDerivedDerived kdd) {
682      int got = li_boost_intrusive_ptr.use_count(kdd);
683      if (expected != got)
684        throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
685  }
686  private void verifyIntrusiveCount(int expected, Klass k) {
687    int got = k.use_count();
688    if (expected != got)
689      throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
690  }
691  private void verifyIntrusiveCount(int expected, KlassDerived kd) {
692      int got = kd.use_count();
693      if (expected != got)
694        throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
695  }
696  private void verifyIntrusiveCount(int expected, KlassDerivedDerived kdd) {
697      int got = kdd.use_count();
698      if (expected != got)
699        throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
700  }
701}