PageRenderTime 59ms CodeModel.GetById 24ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 603 lines | 482 code | 71 blank | 50 comment | 47 complexity | 732e10a5b8d8175608d6396ec3565b6b MD5 | raw file
  1import li_boost_shared_ptr.*;
  2
  3public class li_boost_shared_ptr_runme {
  4  static {
  5    try {
  6        System.loadLibrary("li_boost_shared_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_shared_ptr.setDebug_shared(debug);
 22
 23    // Change loop count to run for a long time to monitor memory
 24    final int loopCount = 1; //5000;
 25    for (int i=0; i<loopCount; i++) {
 26      new li_boost_shared_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 = 100;
 41    while (true) {
 42      System.gc();
 43      System.runFinalization();
 44      try {
 45        java.lang.Thread.sleep(10);
 46      } catch (java.lang.InterruptedException e) {
 47      }
 48      if (--countdown == 0)
 49        break;
 50      if (Klass.getTotal_count() == 1) // Expect 1 instance - the one global variable (GlobalValue)
 51        break;
 52    };
 53    if (Klass.getTotal_count() != 1)
 54      throw new RuntimeException("Klass.total_count=" + Klass.getTotal_count());
 55
 56    int wrapper_count = li_boost_shared_ptr.shared_ptr_wrapper_count(); 
 57    if (wrapper_count != li_boost_shared_ptr.getNOT_COUNTING())
 58      if (wrapper_count != 1) // Expect 1 instance - the one global variable (GlobalSmartValue)
 59        throw new RuntimeException("shared_ptr wrapper count=" + wrapper_count);
 60
 61    if (debug)
 62      System.out.println("Finished");
 63  }
 64
 65  private void runtest() {
 66    // simple shared_ptr usage - created in C++
 67    {
 68      Klass k = new Klass("me oh my");
 69      String val = k.getValue();
 70      verifyValue("me oh my", val);
 71      verifyCount(1, k);
 72    }
 73
 74    // simple shared_ptr usage - not created in C++
 75    {
 76      Klass k = li_boost_shared_ptr.factorycreate();
 77      String val = k.getValue();
 78      verifyValue("factorycreate", val);
 79      verifyCount(1, k);
 80    }
 81
 82    // pass by shared_ptr
 83    {
 84      Klass k = new Klass("me oh my");
 85      Klass kret = li_boost_shared_ptr.smartpointertest(k);
 86      String val = kret.getValue();
 87      verifyValue("me oh my smartpointertest", val);
 88      verifyCount(2, k);
 89      verifyCount(2, kret);
 90    }
 91
 92    // pass by shared_ptr pointer
 93    {
 94      Klass k = new Klass("me oh my");
 95      Klass kret = li_boost_shared_ptr.smartpointerpointertest(k);
 96      String val = kret.getValue();
 97      verifyValue("me oh my smartpointerpointertest", val);
 98      verifyCount(2, k);
 99      verifyCount(2, kret);
100    }
101
102    // pass by shared_ptr reference
103    {
104      Klass k = new Klass("me oh my");
105      Klass kret = li_boost_shared_ptr.smartpointerreftest(k);
106      String val = kret.getValue();
107      verifyValue("me oh my smartpointerreftest", val);
108      verifyCount(2, k);
109      verifyCount(2, kret);
110    }
111
112    // pass by shared_ptr pointer reference
113    {
114      Klass k = new Klass("me oh my");
115      Klass kret = li_boost_shared_ptr.smartpointerpointerreftest(k);
116      String val = kret.getValue();
117      verifyValue("me oh my smartpointerpointerreftest", val);
118      verifyCount(2, k);
119      verifyCount(2, kret);
120    }
121
122    // const pass by shared_ptr
123    {
124      Klass k = new Klass("me oh my");
125      Klass kret = li_boost_shared_ptr.constsmartpointertest(k);
126      String val = kret.getValue();
127      verifyValue("me oh my", val);
128      verifyCount(2, k);
129      verifyCount(2, kret);
130    }
131
132    // const pass by shared_ptr pointer
133    {
134      Klass k = new Klass("me oh my");
135      Klass kret = li_boost_shared_ptr.constsmartpointerpointertest(k);
136      String val = kret.getValue();
137      verifyValue("me oh my", val);
138      verifyCount(2, k);
139      verifyCount(2, kret);
140    }
141
142    // const pass by shared_ptr reference
143    {
144      Klass k = new Klass("me oh my");
145      Klass kret = li_boost_shared_ptr.constsmartpointerreftest(k);
146      String val = kret.getValue();
147      verifyValue("me oh my", val);
148      verifyCount(2, k);
149      verifyCount(2, kret);
150    }
151
152    // pass by value
153    {
154      Klass k = new Klass("me oh my");
155      Klass kret = li_boost_shared_ptr.valuetest(k);
156      String val = kret.getValue();
157      verifyValue("me oh my valuetest", val);
158      verifyCount(1, k);
159      verifyCount(1, kret);
160    }
161
162    // pass by pointer
163    {
164      Klass k = new Klass("me oh my");
165      Klass kret = li_boost_shared_ptr.pointertest(k);
166      String val = kret.getValue();
167      verifyValue("me oh my pointertest", val);
168      verifyCount(1, k);
169      verifyCount(1, kret);
170    }
171
172    // pass by reference
173    {
174      Klass k = new Klass("me oh my");
175      Klass kret = li_boost_shared_ptr.reftest(k);
176      String val = kret.getValue();
177      verifyValue("me oh my reftest", val);
178      verifyCount(1, k);
179      verifyCount(1, kret);
180    }
181
182    // pass by pointer reference
183    {
184      Klass k = new Klass("me oh my");
185      Klass kret = li_boost_shared_ptr.pointerreftest(k);
186      String val = kret.getValue();
187      verifyValue("me oh my pointerreftest", val);
188      verifyCount(1, k);
189      verifyCount(1, kret);
190    }
191
192    // null tests
193    {
194      Klass k = null;
195
196      if (li_boost_shared_ptr.smartpointertest(k) != null)
197        throw new RuntimeException("return was not null");
198
199      if (li_boost_shared_ptr.smartpointerpointertest(k) != null)
200        throw new RuntimeException("return was not null");
201
202      if (li_boost_shared_ptr.smartpointerreftest(k) != null)
203        throw new RuntimeException("return was not null");
204
205      if (li_boost_shared_ptr.smartpointerpointerreftest(k) != null)
206        throw new RuntimeException("return was not null");
207
208      if (!li_boost_shared_ptr.nullsmartpointerpointertest(null).equals("null pointer"))
209        throw new RuntimeException("not null smartpointer pointer");
210
211      try { li_boost_shared_ptr.valuetest(k); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
212
213      if (li_boost_shared_ptr.pointertest(k) != null)
214        throw new RuntimeException("return was not null");
215
216      try { li_boost_shared_ptr.reftest(k); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
217    }
218
219    // $owner
220    {
221      Klass k = li_boost_shared_ptr.pointerownertest();
222      String val = k.getValue();
223      verifyValue("pointerownertest", val);
224      verifyCount(1, k);
225    }
226    {
227      Klass k = li_boost_shared_ptr.smartpointerpointerownertest();
228      String val = k.getValue();
229      verifyValue("smartpointerpointerownertest", val);
230      verifyCount(1, k);
231    }
232
233    ////////////////////////////////// Derived classes ////////////////////////////////////////
234    // derived pass by shared_ptr
235    {
236      KlassDerived k = new KlassDerived("me oh my");
237      KlassDerived kret = li_boost_shared_ptr.derivedsmartptrtest(k);
238      String val = kret.getValue();
239      verifyValue("me oh my derivedsmartptrtest-Derived", val);
240      verifyCount(4, k); // includes two extra references for upcasts
241      verifyCount(4, kret);
242    }
243    // derived pass by shared_ptr pointer
244    {
245      KlassDerived k = new KlassDerived("me oh my");
246      KlassDerived kret = li_boost_shared_ptr.derivedsmartptrpointertest(k);
247      String val = kret.getValue();
248      verifyValue("me oh my derivedsmartptrpointertest-Derived", val);
249      verifyCount(4, k); // includes two extra references for upcasts in the proxy classes
250      verifyCount(4, kret);
251    }
252    // derived pass by shared_ptr ref
253    {
254      KlassDerived k = new KlassDerived("me oh my");
255      KlassDerived kret = li_boost_shared_ptr.derivedsmartptrreftest(k);
256      String val = kret.getValue();
257      verifyValue("me oh my derivedsmartptrreftest-Derived", val);
258      verifyCount(4, k); // includes two extra references for upcasts in the proxy classes
259      verifyCount(4, kret);
260    }
261    // derived pass by shared_ptr pointer ref
262    {
263      KlassDerived k = new KlassDerived("me oh my");
264      KlassDerived kret = li_boost_shared_ptr.derivedsmartptrpointerreftest(k);
265      String val = kret.getValue();
266      verifyValue("me oh my derivedsmartptrpointerreftest-Derived", val);
267      verifyCount(4, k); // includes two extra references for upcasts in the proxy classes
268      verifyCount(4, kret);
269    }
270    // derived pass by pointer
271    {
272      KlassDerived k = new KlassDerived("me oh my");
273      KlassDerived kret = li_boost_shared_ptr.derivedpointertest(k);
274      String val = kret.getValue();
275      verifyValue("me oh my derivedpointertest-Derived", val);
276      verifyCount(2, k); // includes an extra reference for the upcast in the proxy class
277      verifyCount(2, kret);
278    }
279    // derived pass by ref
280    {
281      KlassDerived k = new KlassDerived("me oh my");
282      KlassDerived kret = li_boost_shared_ptr.derivedreftest(k);
283      String val = kret.getValue();
284      verifyValue("me oh my derivedreftest-Derived", val);
285      verifyCount(2, k); // includes an extra reference for the upcast in the proxy class
286      verifyCount(2, kret);
287    }
288
289    ////////////////////////////////// Derived and base class mixed ////////////////////////////////////////
290    // pass by shared_ptr (mixed)
291    {
292      Klass k = new KlassDerived("me oh my");
293      Klass kret = li_boost_shared_ptr.smartpointertest(k);
294      String val = kret.getValue();
295      verifyValue("me oh my smartpointertest-Derived", val);
296      verifyCount(3, k); // an extra reference for the upcast in the proxy class
297      verifyCount(3, kret);
298    }
299
300    // pass by shared_ptr pointer (mixed)
301    {
302      Klass k = new KlassDerived("me oh my");
303      Klass kret = li_boost_shared_ptr.smartpointerpointertest(k);
304      String val = kret.getValue();
305      verifyValue("me oh my smartpointerpointertest-Derived", val);
306      verifyCount(3, k); // an extra reference for the upcast in the proxy class
307      verifyCount(3, kret);
308    }
309
310    // pass by shared_ptr reference (mixed)
311    {
312      Klass k = new KlassDerived("me oh my");
313      Klass kret = li_boost_shared_ptr.smartpointerreftest(k);
314      String val = kret.getValue();
315      verifyValue("me oh my smartpointerreftest-Derived", val);
316      verifyCount(3, k); // an extra reference for the upcast in the proxy class
317      verifyCount(3, kret);
318    }
319
320    // pass by shared_ptr pointer reference (mixed)
321    {
322      Klass k = new KlassDerived("me oh my");
323      Klass kret = li_boost_shared_ptr.smartpointerpointerreftest(k);
324      String val = kret.getValue();
325      verifyValue("me oh my smartpointerpointerreftest-Derived", val);
326      verifyCount(3, k); // an extra reference for the upcast in the proxy class
327      verifyCount(3, kret);
328    }
329
330    // pass by value (mixed)
331    {
332      Klass k = new KlassDerived("me oh my");
333      Klass kret = li_boost_shared_ptr.valuetest(k);
334      String val = kret.getValue();
335      verifyValue("me oh my valuetest", val); // note slicing
336      verifyCount(2, k); // an extra reference for the upcast in the proxy class
337      verifyCount(1, kret);
338    }
339
340    // pass by pointer (mixed)
341    {
342      Klass k = new KlassDerived("me oh my");
343      Klass kret = li_boost_shared_ptr.pointertest(k);
344      String val = kret.getValue();
345      verifyValue("me oh my pointertest-Derived", val);
346      verifyCount(2, k); // an extra reference for the upcast in the proxy class
347      verifyCount(1, kret);
348    }
349
350    // pass by ref (mixed)
351    {
352      Klass k = new KlassDerived("me oh my");
353      Klass kret = li_boost_shared_ptr.reftest(k);
354      String val = kret.getValue();
355      verifyValue("me oh my reftest-Derived", val);
356      verifyCount(2, k); // an extra reference for the upcast in the proxy class
357      verifyCount(1, kret);
358    }
359
360    // 3rd derived class
361    {
362      Klass k = new Klass3rdDerived("me oh my");
363      String val = k.getValue();
364      verifyValue("me oh my-3rdDerived", val);
365      verifyCount(3, k); // 3 classes in inheritance chain == 3 swigCPtr values
366      val = li_boost_shared_ptr.test3rdupcast(k);
367      verifyValue("me oh my-3rdDerived", val);
368      verifyCount(3, k);
369    }
370
371    ////////////////////////////////// Member variables ////////////////////////////////////////
372    // smart pointer by value
373    {
374      MemberVariables m = new MemberVariables();
375      Klass k = new Klass("smart member value");
376      m.setSmartMemberValue(k);
377      String val = k.getValue();
378      verifyValue("smart member value", val);
379      verifyCount(2, k);
380
381      Klass kmember = m.getSmartMemberValue();
382      val = kmember.getValue();
383      verifyValue("smart member value", val);
384      verifyCount(3, kmember);
385      verifyCount(3, k);
386
387      m.delete();
388      verifyCount(2, kmember);
389      verifyCount(2, k);
390    }
391    // smart pointer by pointer
392    {
393      MemberVariables m = new MemberVariables();
394      Klass k = new Klass("smart member pointer");
395      m.setSmartMemberPointer(k);
396      String val = k.getValue();
397      verifyValue("smart member pointer", val);
398      verifyCount(1, k);
399
400      Klass kmember = m.getSmartMemberPointer();
401      val = kmember.getValue();
402      verifyValue("smart member pointer", val);
403      verifyCount(2, kmember);
404      verifyCount(2, k);
405
406      m.delete();
407      verifyCount(2, kmember);
408      verifyCount(2, k);
409    }
410    // smart pointer by reference
411    {
412      MemberVariables m = new MemberVariables();
413      Klass k = new Klass("smart member reference");
414      m.setSmartMemberReference(k);
415      String val = k.getValue();
416      verifyValue("smart member reference", val);
417      verifyCount(2, k);
418
419      Klass kmember = m.getSmartMemberReference();
420      val = kmember.getValue();
421      verifyValue("smart member reference", val);
422      verifyCount(3, kmember);
423      verifyCount(3, k);
424
425      // The C++ reference refers to SmartMemberValue...
426      Klass kmemberVal = m.getSmartMemberValue();
427      val = kmember.getValue();
428      verifyValue("smart member reference", val);
429      verifyCount(4, kmemberVal);
430      verifyCount(4, kmember);
431      verifyCount(4, k);
432
433      m.delete();
434      verifyCount(3, kmemberVal);
435      verifyCount(3, kmember);
436      verifyCount(3, k);
437    }
438    // plain by value
439    {
440      MemberVariables m = new MemberVariables();
441      Klass k = new Klass("plain member value");
442      m.setMemberValue(k);
443      String val = k.getValue();
444      verifyValue("plain member value", val);
445      verifyCount(1, k);
446
447      Klass kmember = m.getMemberValue();
448      val = kmember.getValue();
449      verifyValue("plain member value", val);
450      verifyCount(1, kmember);
451      verifyCount(1, k);
452
453      m.delete();
454      verifyCount(1, kmember);
455      verifyCount(1, k);
456    }
457    // plain by pointer
458    {
459      MemberVariables m = new MemberVariables();
460      Klass k = new Klass("plain member pointer");
461      m.setMemberPointer(k);
462      String val = k.getValue();
463      verifyValue("plain member pointer", val);
464      verifyCount(1, k);
465
466      Klass kmember = m.getMemberPointer();
467      val = kmember.getValue();
468      verifyValue("plain member pointer", val);
469      verifyCount(1, kmember);
470      verifyCount(1, k);
471
472      m.delete();
473      verifyCount(1, kmember);
474      verifyCount(1, k);
475    }
476    // plain by reference
477    {
478      MemberVariables m = new MemberVariables();
479      Klass k = new Klass("plain member reference");
480      m.setMemberReference(k);
481      String val = k.getValue();
482      verifyValue("plain member reference", val);
483      verifyCount(1, k);
484
485      Klass kmember = m.getMemberReference();
486      val = kmember.getValue();
487      verifyValue("plain member reference", val);
488      verifyCount(1, kmember);
489      verifyCount(1, k);
490
491      m.delete();
492      verifyCount(1, kmember);
493      verifyCount(1, k);
494    }
495
496    // null member variables
497    {
498      MemberVariables m = new MemberVariables();
499
500      // shared_ptr by value
501      Klass k = m.getSmartMemberValue();
502      if (k != null)
503        throw new RuntimeException("expected null");
504      m.setSmartMemberValue(null);
505      k = m.getSmartMemberValue();
506      if (k != null)
507        throw new RuntimeException("expected null");
508      verifyCount(0, k);
509
510      // plain by value
511      try { m.setMemberValue(null); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
512    }
513
514    ////////////////////////////////// Global variables ////////////////////////////////////////
515    // smart pointer
516    {
517      Klass kglobal = li_boost_shared_ptr.getGlobalSmartValue();
518      if (kglobal != null)
519        throw new RuntimeException("expected null");
520
521      Klass k = new Klass("smart global value");
522      li_boost_shared_ptr.setGlobalSmartValue(k);
523      verifyCount(2, k);
524
525      kglobal = li_boost_shared_ptr.getGlobalSmartValue();
526      String val = kglobal.getValue();
527      verifyValue("smart global value", val);
528      verifyCount(3, kglobal);
529      verifyCount(3, k);
530      verifyValue("smart global value", li_boost_shared_ptr.getGlobalSmartValue().getValue());
531      li_boost_shared_ptr.setGlobalSmartValue(null);
532    }
533    // plain value
534    {
535      Klass kglobal;
536
537      Klass k = new Klass("global value");
538      li_boost_shared_ptr.setGlobalValue(k);
539      verifyCount(1, k);
540
541      kglobal = li_boost_shared_ptr.getGlobalValue();
542      String val = kglobal.getValue();
543      verifyValue("global value", val);
544      verifyCount(1, kglobal);
545      verifyCount(1, k);
546      verifyValue("global value", li_boost_shared_ptr.getGlobalValue().getValue());
547
548      try { li_boost_shared_ptr.setGlobalValue(null); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
549    }
550    // plain pointer
551    {
552      Klass kglobal = li_boost_shared_ptr.getGlobalPointer();
553      if (kglobal != null)
554        throw new RuntimeException("expected null");
555
556      Klass k = new Klass("global pointer");
557      li_boost_shared_ptr.setGlobalPointer(k);
558      verifyCount(1, k);
559
560      kglobal = li_boost_shared_ptr.getGlobalPointer();
561      String val = kglobal.getValue();
562      verifyValue("global pointer", val);
563      verifyCount(1, kglobal);
564      verifyCount(1, k);
565      li_boost_shared_ptr.setGlobalPointer(null);
566    }
567
568    // plain reference
569    {
570      Klass kglobal;
571
572      Klass k = new Klass("global reference");
573      li_boost_shared_ptr.setGlobalReference(k);
574      verifyCount(1, k);
575
576      kglobal = li_boost_shared_ptr.getGlobalReference();
577      String val = kglobal.getValue();
578      verifyValue("global reference", val);
579      verifyCount(1, kglobal);
580      verifyCount(1, k);
581
582      try { li_boost_shared_ptr.setGlobalReference(null); throw new RuntimeException("Failed to catch null pointer"); } catch (NullPointerException e) {}
583    }
584
585    ////////////////////////////////// Templates ////////////////////////////////////////
586    {
587      PairIntDouble pid = new PairIntDouble(10, 20.2);
588      if (pid.getBaseVal1() != 20 || pid.getBaseVal2() != 40.4)
589        throw new RuntimeException("Base values wrong");
590      if (pid.getVal1() != 10 || pid.getVal2() != 20.2)
591        throw new RuntimeException("Derived Values wrong");
592    }
593  }
594  private void verifyValue(String expected, String got) {
595    if (!expected.equals(got))
596      throw new RuntimeException("verify value failed. Expected: " + expected + " Got: " + got);
597  }
598  private void verifyCount(int expected, Klass k) {
599    int got = li_boost_shared_ptr.use_count(k); 
600    if (expected != got)
601      throw new RuntimeException("verify use_count failed. Expected: " + expected + " Got: " + got);
602  }
603}