001: /*
002: * TestRecycled.java --
003: *
004: * Copyright (c) 2006 Mo DeJong
005: *
006: * See the file "license.terms" for information on usage and
007: * redistribution of this file, and for a DISCLAIMER OF ALL
008: * WARRANTIES.
009: *
010: * RCS: @(#) $Id: TestRecycled.java,v 1.1 2006/06/23 20:53:40 mdejong Exp $
011: *
012: */
013:
014: package tcl.lang;
015:
016: class TestRecycled {
017: // Test interp result set operation when the value
018: // to be set is not a common value. This case
019: // can be handled with a recycled TclObject so
020: // that a new is avoided inside setResult(int).
021:
022: public static String testRecycledInt0(Interp interp)
023: throws TclException {
024: StringBuffer sb = new StringBuffer();
025:
026: // Interp result set to empty shared constant
027: interp.resetResult();
028:
029: // Set interp result to int result that is not
030: // a common value.
031:
032: interp.setResult(100);
033: TclObject recycled = interp.getResult();
034:
035: sb.append("recycled " + recycled.toString());
036: sb.append(" ");
037: sb.append("refCount " + recycled.getRefCount());
038: sb.append(" ");
039:
040: // Now set interp result to another value
041: // that is not a common value, this should
042: // reuse the recycled TclObject.
043:
044: interp.setResult(200);
045:
046: // Test that the recycled object was reused
047: // by getting the int value out of the
048: // original ref. Also check that the
049: // interp result is set to the same
050: // TclObject.
051:
052: sb.append("recycled " + recycled.toString());
053: sb.append(" ");
054: sb.append("refCount " + recycled.getRefCount());
055: sb.append(" ");
056: sb.append("was_recycled " + (recycled == interp.getResult()));
057:
058: return sb.toString();
059: }
060:
061: public static String testRecycledInt1(Interp interp)
062: throws TclException {
063: StringBuffer sb = new StringBuffer();
064:
065: // Interp result set to empty shared constant
066: interp.resetResult();
067:
068: // Set interp result to int result that is not
069: // a common value.
070:
071: interp.setResult(100);
072: TclObject recycled = interp.getResult();
073:
074: sb.append("recycled " + recycled.toString());
075: sb.append(" ");
076: sb.append("refCount " + recycled.getRefCount());
077: sb.append(" ");
078:
079: // Now increment the ref count of recycled
080: // so that it will be shared even after
081: // the interp result is reset.
082:
083: recycled.preserve();
084:
085: sb.append("refCount " + recycled.getRefCount());
086: sb.append(" ");
087:
088: // Reset the result, this drops the second ref to
089: // recycled but it is still shared because of the
090: // preserve call that was just made.
091:
092: interp.resetResult();
093:
094: sb.append("refCount " + recycled.getRefCount());
095:
096: // Release the held ref
097: recycled.release();
098:
099: return sb.toString();
100: }
101:
102: public static String testRecycledInt2(Interp interp)
103: throws TclException {
104: StringBuffer sb = new StringBuffer();
105:
106: // Interp result set to empty shared constant
107: interp.resetResult();
108:
109: // Set interp result to int result that is not
110: // a common value.
111:
112: interp.setResult(100);
113: TclObject recycled = interp.getResult();
114:
115: sb.append("recycled " + recycled.toString());
116: sb.append(" ");
117: sb.append("refCount " + recycled.getRefCount());
118: sb.append(" ");
119:
120: // Now increment the ref count of recycled
121: // so that it will be shared when the
122: // next call to setResult(int) is made.
123:
124: recycled.preserve();
125:
126: sb.append("refCount " + recycled.getRefCount());
127: sb.append(" ");
128:
129: interp.setResult(200);
130:
131: // The recycled value should have been
132: // set to a new TclObject. This new
133: // value is then set as the interp
134: // result.
135:
136: sb.append("new_recycled " + (recycled != interp.getResult()));
137: sb.append(" ");
138: sb.append("refCount " + interp.getResult().getRefCount());
139: sb.append(" ");
140:
141: // The refCount of recycled should now be 1.
142: // The only ref held at this point is the
143: // one from this code. The interp result
144: // was is released when the new TclObject
145: // is set as the interp result. The ref held
146: // inside Interp on the recycled object
147: // was also released just before the
148: // new TclObject was allocated.
149:
150: sb.append("recycled " + recycled.toString());
151: sb.append(" ");
152: sb.append("refCount " + recycled.getRefCount());
153:
154: // Deallocated old recycled TclObject
155: recycled.release();
156:
157: return sb.toString();
158: }
159:
160: public static String testRecycledInt3(Interp interp)
161: throws TclException {
162: StringBuffer sb = new StringBuffer();
163:
164: // Interp result set to empty shared constant
165: interp.resetResult();
166:
167: // Set interp result to int result that is not
168: // a common value.
169:
170: interp.setResult(100);
171: TclObject recycled = interp.getResult();
172:
173: sb.append("recycled " + recycled.toString());
174: sb.append(" ");
175: sb.append("refCount " + recycled.getRefCount());
176: sb.append(" ");
177:
178: // Now reset the interp result, this should
179: // drop the refCount back down to 1.
180:
181: interp.resetResult();
182:
183: sb.append("refCount " + recycled.getRefCount());
184: sb.append(" ");
185:
186: // Set a new int result that is not a common
187: // value. This should use the recycled object
188: // again since the refCount is 1. This will
189: // also set the interp result.
190:
191: interp.setResult(200);
192:
193: sb.append("was_recycled " + (recycled == interp.getResult()));
194: sb.append(" ");
195: sb.append("getResult " + interp.getResult().toString());
196: sb.append(" ");
197: sb.append("refCount " + interp.getResult().getRefCount());
198:
199: return sb.toString();
200: }
201:
202: public static String testRecycledInt4(Interp interp)
203: throws TclException {
204: StringBuffer sb = new StringBuffer();
205:
206: interp.resetResult();
207:
208: // Get recycled int ref
209:
210: interp.setResult(100);
211: TclObject recycled = interp.getResult();
212:
213: sb.append("recycled " + recycled.toString());
214: sb.append(" ");
215: sb.append("refCount " + recycled.getRefCount());
216: sb.append(" ");
217:
218: // Set var "v" to an int value. This operation
219: // will reuse the recycled value and save it
220: // in the variable. A side effect of this
221: // operation will be to change the object
222: // that is current the interp result, but
223: // that is ok. If the caller did not want
224: // the result object to change then the
225: // refCount should have been incremented.
226:
227: interp.setVar("v", null, 200, 0);
228:
229: sb.append("recycled " + recycled.toString());
230: sb.append(" ");
231: sb.append("refCount " + recycled.getRefCount());
232: sb.append(" ");
233:
234: // Reset the interp result, this will drop
235: // the ref held by the interp result.
236:
237: interp.resetResult();
238:
239: sb.append("refCount " + recycled.getRefCount());
240:
241: return sb.toString();
242: }
243:
244: // Recycled double values
245:
246: public static String testRecycledDouble0(Interp interp)
247: throws TclException {
248: StringBuffer sb = new StringBuffer();
249:
250: // Interp result set to empty shared constant
251: interp.resetResult();
252:
253: // Set interp result to double result that is not
254: // a common value.
255:
256: interp.setResult(100.0);
257: TclObject recycled = interp.getResult();
258:
259: sb.append("recycled " + recycled.toString());
260: sb.append(" ");
261: sb.append("refCount " + recycled.getRefCount());
262: sb.append(" ");
263:
264: // Now set interp result to another value
265: // that is not a common value, this should
266: // reuse the recycled TclObject.
267:
268: interp.setResult(200.0);
269:
270: // Test that the recycled object was reused
271: // by getting the double value out of the
272: // original ref. Also check that the
273: // interp result is set to the same
274: // TclObject.
275:
276: sb.append("recycled " + recycled.toString());
277: sb.append(" ");
278: sb.append("refCount " + recycled.getRefCount());
279: sb.append(" ");
280: sb.append("was_recycled " + (recycled == interp.getResult()));
281:
282: return sb.toString();
283: }
284:
285: public static String testRecycledDouble1(Interp interp)
286: throws TclException {
287: StringBuffer sb = new StringBuffer();
288:
289: // Interp result set to empty shared constant
290: interp.resetResult();
291:
292: // Set interp result to int result that is not
293: // a common value.
294:
295: interp.setResult(100.0);
296: TclObject recycled = interp.getResult();
297:
298: sb.append("recycled " + recycled.toString());
299: sb.append(" ");
300: sb.append("refCount " + recycled.getRefCount());
301: sb.append(" ");
302:
303: // Now increment the ref count of recycled
304: // so that it will be shared even after
305: // the interp result is reset.
306:
307: recycled.preserve();
308:
309: sb.append("refCount " + recycled.getRefCount());
310: sb.append(" ");
311:
312: // Reset the result, this drops the second ref to
313: // recycled but it is still shared because of the
314: // preserve call that was just made.
315:
316: interp.resetResult();
317:
318: sb.append("refCount " + recycled.getRefCount());
319:
320: // Release the held ref
321: recycled.release();
322:
323: return sb.toString();
324: }
325:
326: public static String testRecycledDouble2(Interp interp)
327: throws TclException {
328: StringBuffer sb = new StringBuffer();
329:
330: // Interp result set to empty shared constant
331: interp.resetResult();
332:
333: // Set interp result to double result that is not
334: // a common value.
335:
336: interp.setResult(100.0);
337: TclObject recycled = interp.getResult();
338:
339: sb.append("recycled " + recycled.toString());
340: sb.append(" ");
341: sb.append("refCount " + recycled.getRefCount());
342: sb.append(" ");
343:
344: // Now increment the ref count of recycled
345: // so that it will be shared when the
346: // next call to setResult(double) is made.
347:
348: recycled.preserve();
349:
350: sb.append("refCount " + recycled.getRefCount());
351: sb.append(" ");
352:
353: interp.setResult(200.0);
354:
355: // The recycled value should have been
356: // set to a new TclObject. This new
357: // value is then set as the interp
358: // result.
359:
360: sb.append("new_recycled " + (recycled != interp.getResult()));
361: sb.append(" ");
362: sb.append("refCount " + interp.getResult().getRefCount());
363: sb.append(" ");
364:
365: // The refCount of recycled should now be 1.
366: // The only ref held at this point is the
367: // one from this code. The interp result
368: // was is released when the new TclObject
369: // is set as the interp result. The ref held
370: // inside Interp on the recycled object
371: // was also released just before the
372: // new TclObject was allocated.
373:
374: sb.append("recycled " + recycled.toString());
375: sb.append(" ");
376: sb.append("refCount " + recycled.getRefCount());
377:
378: // Deallocated old recycled TclObject
379: recycled.release();
380:
381: return sb.toString();
382: }
383:
384: public static String testRecycledDouble3(Interp interp)
385: throws TclException {
386: StringBuffer sb = new StringBuffer();
387:
388: // Interp result set to empty shared constant
389: interp.resetResult();
390:
391: // Set interp result to double result that is not
392: // a common value.
393:
394: interp.setResult(100.0);
395: TclObject recycled = interp.getResult();
396:
397: sb.append("recycled " + recycled.toString());
398: sb.append(" ");
399: sb.append("refCount " + recycled.getRefCount());
400: sb.append(" ");
401:
402: // Now reset the interp result, this should
403: // drop the refCount back down to 1.
404:
405: interp.resetResult();
406:
407: sb.append("refCount " + recycled.getRefCount());
408: sb.append(" ");
409:
410: // Set a new int result that is not a common
411: // value. This should use the recycled object
412: // again since the refCount is 1. This will
413: // also set the interp result.
414:
415: interp.setResult(200.0);
416:
417: sb.append("was_recycled " + (recycled == interp.getResult()));
418: sb.append(" ");
419: sb.append("getResult " + interp.getResult().toString());
420: sb.append(" ");
421: sb.append("refCount " + interp.getResult().getRefCount());
422:
423: return sb.toString();
424: }
425:
426: public static String testRecycledDouble4(Interp interp)
427: throws TclException {
428: StringBuffer sb = new StringBuffer();
429:
430: interp.resetResult();
431:
432: // Get recycled double ref
433:
434: interp.setResult(100.0);
435: TclObject recycled = interp.getResult();
436:
437: sb.append("recycled " + recycled.toString());
438: sb.append(" ");
439: sb.append("refCount " + recycled.getRefCount());
440: sb.append(" ");
441:
442: // Set var "v" to a double value. This operation
443: // will reuse the recycled value and save it
444: // in the variable. A side effect of this
445: // operation will be to change the object
446: // that is current the interp result, but
447: // that is ok. If the caller did not want
448: // the result object to change then the
449: // refCount should have been incremented.
450:
451: interp.setVar("v", null, 200.0, 0);
452:
453: sb.append("recycled " + recycled.toString());
454: sb.append(" ");
455: sb.append("refCount " + recycled.getRefCount());
456: sb.append(" ");
457:
458: // Reset the interp result, this will drop
459: // the ref held by the interp result.
460:
461: interp.resetResult();
462:
463: sb.append("refCount " + recycled.getRefCount());
464:
465: return sb.toString();
466: }
467:
468: // Recycled String values
469:
470: public static String testRecycledString0(Interp interp)
471: throws TclException {
472: StringBuffer sb = new StringBuffer();
473:
474: // Interp result set to empty shared constant
475: interp.resetResult();
476:
477: // Set interp result to String result that is not
478: // a common value.
479:
480: interp.setResult("100");
481: TclObject recycled = interp.getResult();
482:
483: sb.append("recycled " + recycled.toString());
484: sb.append(" ");
485: sb.append("refCount " + recycled.getRefCount());
486: sb.append(" ");
487:
488: // Now set interp result to another value
489: // that is not a common value, this should
490: // reuse the recycled TclObject.
491:
492: interp.setResult("200");
493:
494: // Test that the recycled object was reused
495: // by getting the double value out of the
496: // original ref. Also check that the
497: // interp result is set to the same
498: // TclObject.
499:
500: sb.append("recycled " + recycled.toString());
501: sb.append(" ");
502: sb.append("refCount " + recycled.getRefCount());
503: sb.append(" ");
504: sb.append("was_recycled " + (recycled == interp.getResult()));
505:
506: return sb.toString();
507: }
508:
509: public static String testRecycledString1(Interp interp)
510: throws TclException {
511: StringBuffer sb = new StringBuffer();
512:
513: // Interp result set to empty shared constant
514: interp.resetResult();
515:
516: // Set interp result to int result that is not
517: // a common value.
518:
519: interp.setResult("100");
520: TclObject recycled = interp.getResult();
521:
522: sb.append("recycled " + recycled.toString());
523: sb.append(" ");
524: sb.append("refCount " + recycled.getRefCount());
525: sb.append(" ");
526:
527: // Now increment the ref count of recycled
528: // so that it will be shared even after
529: // the interp result is reset.
530:
531: recycled.preserve();
532:
533: sb.append("refCount " + recycled.getRefCount());
534: sb.append(" ");
535:
536: // Reset the result, this drops the second ref to
537: // recycled but it is still shared because of the
538: // preserve call that was just made.
539:
540: interp.resetResult();
541:
542: sb.append("refCount " + recycled.getRefCount());
543:
544: // Release the held ref
545: recycled.release();
546:
547: return sb.toString();
548: }
549:
550: public static String testRecycledString2(Interp interp)
551: throws TclException {
552: StringBuffer sb = new StringBuffer();
553:
554: // Interp result set to empty shared constant
555: interp.resetResult();
556:
557: // Set interp result to double result that is not
558: // a common value.
559:
560: interp.setResult("100");
561: TclObject recycled = interp.getResult();
562:
563: sb.append("recycled " + recycled.toString());
564: sb.append(" ");
565: sb.append("refCount " + recycled.getRefCount());
566: sb.append(" ");
567:
568: // Now increment the ref count of recycled
569: // so that it will be shared when the
570: // next call to setResult(double) is made.
571:
572: recycled.preserve();
573:
574: sb.append("refCount " + recycled.getRefCount());
575: sb.append(" ");
576:
577: interp.setResult("200");
578:
579: // The recycled value should have been
580: // set to a new TclObject. This new
581: // value is then set as the interp
582: // result.
583:
584: sb.append("new_recycled " + (recycled != interp.getResult()));
585: sb.append(" ");
586: sb.append("refCount " + interp.getResult().getRefCount());
587: sb.append(" ");
588:
589: // The refCount of recycled should now be 1.
590: // The only ref held at this point is the
591: // one from this code. The interp result
592: // was is released when the new TclObject
593: // is set as the interp result. The ref held
594: // inside Interp on the recycled object
595: // was also released just before the
596: // new TclObject was allocated.
597:
598: sb.append("recycled " + recycled.toString());
599: sb.append(" ");
600: sb.append("refCount " + recycled.getRefCount());
601:
602: // Deallocated old recycled TclObject
603: recycled.release();
604:
605: return sb.toString();
606: }
607:
608: public static String testRecycledString3(Interp interp)
609: throws TclException {
610: StringBuffer sb = new StringBuffer();
611:
612: // Interp result set to empty shared constant
613: interp.resetResult();
614:
615: // Set interp result to double result that is not
616: // a common value.
617:
618: interp.setResult("100");
619: TclObject recycled = interp.getResult();
620:
621: sb.append("recycled " + recycled.toString());
622: sb.append(" ");
623: sb.append("refCount " + recycled.getRefCount());
624: sb.append(" ");
625:
626: // Now reset the interp result, this should
627: // drop the refCount back down to 1.
628:
629: interp.resetResult();
630:
631: sb.append("refCount " + recycled.getRefCount());
632: sb.append(" ");
633:
634: // Set a new int result that is not a common
635: // value. This should use the recycled object
636: // again since the refCount is 1. This will
637: // also set the interp result.
638:
639: interp.setResult("200");
640:
641: sb.append("was_recycled " + (recycled == interp.getResult()));
642: sb.append(" ");
643: sb.append("getResult " + interp.getResult().toString());
644: sb.append(" ");
645: sb.append("refCount " + interp.getResult().getRefCount());
646:
647: return sb.toString();
648: }
649:
650: public static String testRecycledString4(Interp interp)
651: throws TclException {
652: StringBuffer sb = new StringBuffer();
653:
654: interp.resetResult();
655:
656: // Get recycled double ref
657:
658: interp.setResult("100");
659: TclObject recycled = interp.getResult();
660:
661: sb.append("recycled " + recycled.toString());
662: sb.append(" ");
663: sb.append("refCount " + recycled.getRefCount());
664: sb.append(" ");
665:
666: // Set var "v" to a double value. This operation
667: // will reuse the recycled value and save it
668: // in the variable. A side effect of this
669: // operation will be to change the object
670: // that is current the interp result, but
671: // that is ok. If the caller did not want
672: // the result object to change then the
673: // refCount should have been incremented.
674:
675: interp.setVar("v", null, "200", 0);
676:
677: sb.append("recycled " + recycled.toString());
678: sb.append(" ");
679: sb.append("refCount " + recycled.getRefCount());
680: sb.append(" ");
681:
682: // Reset the interp result, this will drop
683: // the ref held by the interp result.
684:
685: interp.resetResult();
686:
687: sb.append("refCount " + recycled.getRefCount());
688:
689: return sb.toString();
690: }
691:
692: }
|