PageRenderTime 50ms CodeModel.GetById 2ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Examples/test-suite/csharp/li_boost_shared_ptr_runme.cs

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