001: // These tests check for things that require access the
002: // the tcl.lang.* package. It is easier to test this
003: // stuff in one file that to add special access methods
004: // to the TJC class to support testing.
005:
006: package tcl.lang;
007:
008: public class TestTcl {
009:
010: public static String internalRepToString(TclObject to) {
011: InternalRep rep = to.getInternalRep();
012:
013: if (rep instanceof TclString) {
014: return "TclString";
015: } else if (rep instanceof TclBoolean) {
016: return "TclBoolean";
017: } else if (rep instanceof TclInteger) {
018: return "TclInteger";
019: } else if (rep instanceof TclDouble) {
020: return "TclDouble";
021: } else if (rep instanceof TclList) {
022: return "TclList";
023: } else {
024: return "Unknown";
025: }
026: }
027:
028: public static String toString(TclObject to) {
029: return to.toString();
030: }
031:
032: public static boolean hasNoStringRep(TclObject to) {
033: return to.hasNoStringRep();
034: }
035:
036: // Test code that checks the boolean value of an object.
037: // In this case, the object is not shared so we can
038: // toss out the string rep and regenerate it from the
039: // internal boolean rep.
040:
041: public static String testBoolQuery1(Interp interp)
042: throws TclException {
043: StringBuffer results = new StringBuffer(64);
044: TclObject ival;
045: boolean bval;
046:
047: ival = TclInteger.newInstance(2); // int internal rep
048: ival.preserve(); // bump refCount to 1
049:
050: results.append(ival.toString());
051: results.append(" ");
052: results.append("refCount");
053: results.append(" ");
054: results.append(ival.getRefCount());
055: results.append(" ");
056: results.append(internalRepToString(ival));
057: results.append(" ");
058:
059: bval = TclBoolean.get(interp, ival);
060: // The object is not shared, so pretend we
061: // changed it and throw out the string rep
062: ival.invalidateStringRep();
063:
064: results.append(ival.getRefCount());
065: results.append(" ");
066: results.append(internalRepToString(ival));
067: results.append(" ");
068: results.append(bval);
069: results.append(" ");
070: results.append(ival.toString());
071: results.append(" ");
072:
073: int ival2 = TclInteger.get(interp, ival);
074:
075: results.append(internalRepToString(ival));
076:
077: results.append(" ");
078:
079: results.append(ival2);
080:
081: return results.toString();
082: }
083:
084: // Test code that checks the boolean value of an object.
085: // A shared object can have its internal rep changed
086: // from TclString to TclBoolean and then to TclInteger.
087:
088: public static String testBoolQuery2(Interp interp)
089: throws TclException {
090: StringBuffer results = new StringBuffer(64);
091: TclObject ival;
092: boolean bval;
093:
094: ival = TclInteger.newInstance(2); // int internal rep
095: ival.preserve(); // hold refCount at 1
096: ival.preserve(); // bump refCount to 2 (shared)
097: ival.toString(); // generate string rep from integer
098:
099: results.append(ival.toString());
100: results.append(" ");
101: results.append("refCount");
102: results.append(" ");
103: results.append(ival.getRefCount());
104: results.append(" ");
105: results.append(internalRepToString(ival));
106: results.append(" ");
107:
108: bval = TclBoolean.get(interp, ival);
109: //ival.invalidateStringRep(); // Can't invalidate with refCount == 2
110:
111: results.append(ival.getRefCount());
112: results.append(" ");
113: results.append(internalRepToString(ival));
114: results.append(" ");
115: results.append(bval);
116: results.append(" ");
117: results.append(ival.toString());
118: results.append(" ");
119:
120: // The string rep is valid at this point, so the
121: // string should ba parsed back to 2 here.
122: int ival2 = TclInteger.get(interp, ival);
123:
124: results.append(internalRepToString(ival));
125: results.append(" ");
126: results.append(ival2);
127:
128: return results.toString();
129: }
130:
131: // Use TJC.getBoolean(), this method will change the
132: // internal rep from TclString to TclInteger, but
133: // nothing is done in this case because the object
134: // is already a TclInteger and that is a valid boolean
135: // value.
136:
137: public static String testBoolQuery3(Interp interp)
138: throws TclException {
139: StringBuffer results = new StringBuffer(64);
140: TclObject ival;
141: boolean bval;
142:
143: ival = TclInteger.newInstance(2); // int internal rep
144: ival.preserve(); // hold refCount at 1
145:
146: results.append(ival.toString());
147: results.append(" ");
148: results.append(ival.getRefCount());
149: results.append(" ");
150: results.append(internalRepToString(ival));
151: results.append(" ");
152:
153: // Use TJC.getBoolean() instead of TclBoolean.get()
154: //bval = TclBoolean.get(interp, ival);
155: bval = TJC.getBoolean(interp, ival);
156:
157: results.append(ival.getRefCount());
158: results.append(" ");
159: results.append(internalRepToString(ival));
160: results.append(" ");
161: results.append(ival.toString());
162: results.append(" ");
163: results.append(TclInteger.get(interp, ival));
164: results.append(" ");
165: results.append(bval);
166:
167: return results.toString();
168: }
169:
170: // Use TJC.getBoolean(), this method will change the
171: // internal rep from TclString to TclInteger.
172:
173: public static String testBoolQuery4(Interp interp)
174: throws TclException {
175: StringBuffer results = new StringBuffer(64);
176: TclObject ival;
177: boolean bval;
178:
179: ival = TclString.newInstance("2"); // string internal rep
180: ival.preserve(); // hold refCount at 1
181:
182: results.append(ival.toString());
183: results.append(" ");
184: results.append(ival.getRefCount());
185: results.append(" ");
186: results.append(internalRepToString(ival));
187: results.append(" ");
188:
189: // Use TJC.getBoolean() instead of TclBoolean.get()
190: //bval = TclBoolean.get(interp, ival);
191: bval = TJC.getBoolean(interp, ival);
192:
193: results.append(ival.getRefCount());
194: results.append(" ");
195: results.append(internalRepToString(ival));
196: results.append(" ");
197: results.append(ival.toString());
198: results.append(" ");
199: results.append(TclInteger.get(interp, ival));
200: results.append(" ");
201: results.append(bval);
202:
203: return results.toString();
204: }
205:
206: // Invoking TclBoolean.get() on a TclInteger that has
207: // the value 0 or 1 will return a boolean condition
208: // but it will not change the internal rep to TclBoolean.
209:
210: public static String testBoolQuery5(Interp interp)
211: throws TclException {
212: StringBuffer results = new StringBuffer(64);
213: TclObject ival;
214: boolean bval;
215:
216: ival = TclInteger.newInstance(0); // string internal rep
217: ival.preserve();
218: ival.preserve(); // bump refCount to 2 (shared)
219:
220: results.append(ival.getRefCount());
221: results.append(" ");
222: results.append(internalRepToString(ival));
223: results.append(" ");
224:
225: bval = TclBoolean.get(interp, ival);
226:
227: results.append(ival.getRefCount());
228: results.append(" ");
229: results.append(internalRepToString(ival));
230: results.append(" ");
231: results.append(ival.toString());
232: results.append(" ");
233: results.append(TclInteger.get(interp, ival));
234: results.append(" ");
235: results.append(bval);
236:
237: return results.toString();
238: }
239:
240: // Invoke TclDouble related methods.
241:
242: public static String testDouble1(Interp interp) throws TclException {
243: TclObject tobj = TclDouble.newInstance(1.0);
244: return tobj.toString();
245: }
246:
247: public static String testDouble2(Interp interp) throws TclException {
248: TclObject tobj = TclDouble.newInstance(0);
249: return tobj.toString();
250: }
251:
252: public static String testDouble3(Interp interp) throws TclException {
253: StringBuffer results = new StringBuffer();
254:
255: TclObject tobj = TclString.newInstance("1");
256: double d = TclDouble.get(interp, tobj);
257:
258: results.append(d);
259: results.append(' ');
260: results.append(internalRepToString(tobj));
261:
262: return results.toString();
263: }
264:
265: public static String testDouble4(Interp interp) throws TclException {
266: StringBuffer results = new StringBuffer();
267:
268: TclObject tobj = TclString.newInstance("0");
269: double d = TclDouble.get(interp, tobj);
270:
271: results.append(d);
272: results.append(' ');
273: results.append(internalRepToString(tobj));
274:
275: return results.toString();
276: }
277:
278: public static String testDouble5(Interp interp) throws TclException {
279: StringBuffer results = new StringBuffer();
280:
281: String srep = "1";
282:
283: TclObject tobj = TclString.newInstance(srep);
284: double d = TclDouble.get(interp, tobj);
285:
286: results.append(d);
287: results.append(' ');
288: results.append(internalRepToString(tobj));
289: results.append(' ');
290: // Parse double directly without going through TclDouble logic.
291: results.append(Util.getDouble(interp, srep));
292: results.append(' ');
293: // Parse int directly
294: results.append(Util.getInt(interp, srep));
295:
296: return results.toString();
297: }
298:
299: public static String testDouble6(Interp interp) throws TclException {
300: StringBuffer results = new StringBuffer();
301:
302: // Tcl's behavior related to parsing of integers vs doubles
303: // is confusing. Numbers with a leading zero are parsed as
304: // an octal integer but the leading zeros are ignored when
305: // parsing as a double.
306:
307: String srep = "040"; // parsed as int 32 but double 40.0
308:
309: TclObject tobj = TclString.newInstance(srep);
310: double d = TclDouble.get(interp, tobj);
311:
312: results.append(d);
313: results.append(' ');
314: results.append(internalRepToString(tobj));
315: results.append(' ');
316: // Parse double directly without going through TclDouble logic.
317: results.append(Util.getDouble(interp, srep));
318: results.append(' ');
319: // Parse int directly
320: results.append(Util.getInt(interp, srep));
321:
322: return results.toString();
323: }
324:
325: public static String testDouble7(Interp interp) throws TclException {
326: StringBuffer results = new StringBuffer();
327:
328: String srep = "0xFF";
329: TclObject tobj = TclString.newInstance(srep);
330:
331: // Try to parse as a TclDouble
332: try {
333: double dval = TclDouble.get(interp, tobj);
334: results.append(dval);
335: } catch (TclException te) {
336: results.append("TclException");
337: }
338:
339: results.append(' ');
340:
341: // Try to parse as a TclInteger
342: try {
343: int ival = TclInteger.get(interp, tobj);
344: results.append(ival);
345: } catch (TclException te) {
346: results.append("TclException");
347: }
348:
349: return results.toString();
350: }
351:
352: public static String testDouble8(Interp interp) throws TclException {
353: StringBuffer results = new StringBuffer();
354:
355: String srep = "1.0";
356: TclObject tobj = TclString.newInstance(srep);
357:
358: // Try to parse as a TclInteger
359: try {
360: int ival = TclInteger.get(interp, tobj);
361: results.append(ival);
362: } catch (TclException te) {
363: results.append("TclException");
364: }
365:
366: results.append(' ');
367:
368: // Try to parse as a TclDouble
369: try {
370: double dval = TclDouble.get(interp, tobj);
371: results.append(dval);
372: } catch (TclException te) {
373: results.append("TclException");
374: }
375:
376: return results.toString();
377: }
378:
379: // Test logic related to TclObject.ivalue field. This
380: // is either an int value or a bit field based on the
381: // internal rep type.
382:
383: public static String testIvalueUtil(TclObject tobj) {
384: StringBuffer results = new StringBuffer();
385:
386: results.append("isIntType" + " ");
387: results.append(tobj.isIntType());
388: results.append(" ");
389:
390: results.append("isStringType" + " ");
391: results.append(tobj.isStringType());
392: results.append(" ");
393:
394: results.append("isDoubleType" + " ");
395: results.append(tobj.isDoubleType());
396: results.append(" ");
397:
398: results.append("isListType" + " ");
399: results.append(tobj.isListType());
400:
401: return results.toString();
402: }
403:
404: public static String testIvalue0(Interp interp) throws TclException {
405: TclObject tobj = TclInteger.newInstance(0);
406: return testIvalueUtil(tobj);
407: }
408:
409: public static String testIvalue1(Interp interp) throws TclException {
410: TclObject tobj = TclInteger.newInstance(1);
411: return testIvalueUtil(tobj);
412: }
413:
414: public static String testIvalue2(Interp interp) throws TclException {
415: TclObject tobj = TclInteger.newInstance(2);
416: TclObject dup = tobj.duplicate();
417: return testIvalueUtil(dup);
418: }
419:
420: public static String testIvalue3(Interp interp) throws TclException {
421: TclObject tobj = TclInteger.newInstance(2);
422: // TclInteger -> TclString
423: TclString.append(tobj, "");
424: return testIvalueUtil(tobj);
425: }
426:
427: public static String testIvalue4(Interp interp) throws TclException {
428: TclObject tobj = TclInteger.newInstance(2);
429: // TclInteger -> TclString
430: TclString.append(tobj, "");
431: TclObject dup = tobj.duplicate();
432: return testIvalueUtil(dup);
433: }
434:
435: public static String testIvalue5(Interp interp) throws TclException {
436: TclObject tobj = TclString.newInstance("foo");
437: return testIvalueUtil(tobj);
438: }
439:
440: public static String testIvalue6(Interp interp) throws TclException {
441: TclObject tobj = TclString.newInstance("foo");
442: TclObject dup = tobj.duplicate();
443: return testIvalueUtil(dup);
444: }
445:
446: public static String testIvalue7(Interp interp) throws TclException {
447: TclObject tobj = TclString.newInstance("");
448: // TclString -> TclList
449: TclList.getLength(interp, tobj);
450: return testIvalueUtil(tobj);
451: }
452:
453: public static String testIvalue8(Interp interp) throws TclException {
454: TclObject tobj = TclString.newInstance("1");
455: // TclString -> TclInteger
456: TclInteger.get(interp, tobj);
457: return testIvalueUtil(tobj);
458: }
459:
460: public static String testIvalue9(Interp interp) throws TclException {
461: TclObject tobj = TclString.newInstance("1.0");
462: // TclString -> TclDouble
463: TclDouble.get(interp, tobj);
464: return testIvalueUtil(tobj);
465: }
466:
467: public static String testIvalue10(Interp interp)
468: throws TclException {
469: TclObject tobj = TclDouble.newInstance(1.0);
470: // TclDouble -> TclString
471: TclString.append(tobj, "");
472: return testIvalueUtil(tobj);
473: }
474:
475: public static String testIvalue11(Interp interp)
476: throws TclException {
477: TclObject tobj = TclList.newInstance();
478: TclObject dup = tobj.duplicate();
479: return testIvalueUtil(dup);
480: }
481:
482: public static String testIvalue12(Interp interp)
483: throws TclException {
484: TclObject tobj = TclDouble.newInstance(1.0);
485: TclObject dup = tobj.duplicate();
486: return testIvalueUtil(dup);
487: }
488:
489: public static String testIvalue13(Interp interp)
490: throws TclException {
491: byte[] bytes = new byte[1];
492: TclObject tba = TclByteArray.newInstance(bytes);
493:
494: // Int value matches default ivalue for new TclObject with an internal rep.
495: TclObject tobj = TclInteger.newInstance(tba.ivalue);
496: return testIvalueUtil(tobj);
497: }
498:
499: public static String testIvalue14(Interp interp)
500: throws TclException {
501: TclObject td = TclDouble.newInstance(1.0);
502:
503: // Int value matches default double ivalue.
504: TclObject tobj = TclInteger.newInstance(td.ivalue);
505: return testIvalueUtil(tobj);
506: }
507:
508: public static String testIvalue15(Interp interp)
509: throws TclException {
510: TclObject tstr = TclString.newInstance("foo");
511:
512: // Int value matches default string ivalue.
513: TclObject tobj = TclInteger.newInstance(tstr.ivalue);
514: return testIvalueUtil(tobj);
515: }
516:
517: public static String testIvalue16(Interp interp)
518: throws TclException {
519: TclObject tlist = TclList.newInstance();
520:
521: // Int value matches default list ivalue.
522: TclObject tobj = TclInteger.newInstance(tlist.ivalue);
523: return testIvalueUtil(tobj);
524: }
525:
526: }
|