001: /*
002: * JavaTestExtension.java --
003: *
004: * This file contains loads classes needed by the Jacl
005: * test suite.
006: *
007: * Copyright (c) 1997 Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and
010: * redistribution of this file, and for a DISCLAIMER OF ALL
011: * WARRANTIES.
012: *
013: * RCS: @(#) $Id: JavaTestExtension.java,v 1.9 2006/08/03 23:24:03 mdejong Exp $
014: *
015: */
016:
017: package tcl.lang;
018:
019: import java.util.*;
020:
021: /*
022: * This Extension class contains commands used by the Jacl
023: * test suite.
024: */
025:
026: public class JavaTestExtension extends Extension {
027:
028: /*
029: *----------------------------------------------------------------------
030: *
031: * init --
032: *
033: * Initializes the JavaTestExtension.
034: *
035: * Results:
036: * None.
037: *
038: * Side effects:
039: * Commands are created in the interpreter.
040: *
041: *----------------------------------------------------------------------
042: */
043:
044: public void init(Interp interp) {
045: interp.createCommand("jtest", new JtestCmd());
046: interp.createCommand("testevalex", new TestEvalExCmd());
047: interp.createCommand("testparser", new TestParserCmd());
048: interp.createCommand("testparsevar", new TestParsevarCmd());
049: interp.createCommand("testparsevarname",
050: new TestParsevarnameCmd());
051: interp.createCommand("testexprparser", new TestExprParserCmd());
052: interp.createCommand("testevalobjv", new TestEvalObjvCmd());
053: interp.createCommand("testcompcode", new TestcompcodeCmd());
054: interp.createCommand("testsetplatform",
055: new TestsetplatformCmd());
056: interp.createCommand("testtranslatefilename",
057: new TesttranslatefilenameCmd());
058: interp.createCommand("testchannel", new TestChannelCmd());
059: interp.createCommand("testvarframe", new TestVarFrameCmd());
060: interp.createCommand("testtclobjectmemory",
061: new TclObjectMemory());
062: interp.createCommand("testinterpdelete",
063: new TestInterpDeleteCmd());
064:
065: // Create "testobj" and friends
066: TestObjCmd.init(interp);
067:
068: // Create "T1" and "T2" test expr functions
069: createExprTestFunctions(interp);
070: }
071:
072: void createExprTestFunctions(Interp interp) {
073: Expression expr = interp.expr;
074: expr.registerMathFunction("T1", new TestMathFunc(123));
075: expr.registerMathFunction("T2", new TestMathFunc(345));
076: expr.registerMathFunction("T3", new TestMathFunc2());
077: }
078:
079: } // JavaTestExtension
080:
081: // Implement "T1" and "T2" expr math functions
082:
083: class TestMathFunc extends NoArgMathFunction {
084: int clientData;
085:
086: TestMathFunc(int clientData) {
087: this .clientData = clientData;
088: }
089:
090: void apply(Interp interp, ExprValue value) throws TclException {
091: value.setIntValue(clientData);
092: }
093: }
094:
095: // Implement "T3" expr math function
096:
097: class TestMathFunc2 extends MathFunction {
098:
099: TestMathFunc2() {
100: argTypes = new int[2];
101: argTypes[0] = EITHER;
102: argTypes[1] = EITHER;
103: }
104:
105: // Return the maximum of the two arguments with the correct type.
106:
107: void apply(Interp interp, ExprValue[] values) throws TclException {
108: ExprValue arg0 = values[0];
109: ExprValue arg1 = values[1];
110:
111: if (arg0.isIntType()) {
112: int i0 = arg0.getIntValue();
113:
114: if (arg1.isIntType()) {
115: int i1 = arg1.getIntValue();
116: arg0.setIntValue(((i0 > i1) ? i0 : i1));
117: } else if (arg1.isDoubleType()) {
118: double d0 = (double) i0;
119: double d1 = arg1.getDoubleValue();
120: arg0.setDoubleValue(((d0 > d1) ? d0 : d1));
121: } else {
122: throw new TclException(interp,
123: "T3: wrong type for arg 2");
124: }
125: } else if (arg0.isDoubleType()) {
126: double d0 = arg0.getDoubleValue();
127:
128: if (arg1.isIntType()) {
129: double d1 = (double) arg1.getIntValue();
130: arg0.setDoubleValue(((d0 > d1) ? d0 : d1));
131: } else if (arg1.isDoubleType()) {
132: double d1 = arg1.getDoubleValue();
133: arg0.setDoubleValue(((d0 > d1) ? d0 : d1));
134: } else {
135: throw new TclException(interp,
136: "T3: wrong type for arg 2");
137: }
138: } else {
139: throw new TclException(interp, "T3: wrong type for arg 1");
140: }
141: }
142: }
143:
144: class TestAssocData implements AssocData {
145: Interp interp;
146: String testCmd;
147: String removeCmd;
148:
149: public TestAssocData(Interp i, String tcmd, String rcmd) {
150: interp = i;
151: testCmd = tcmd;
152: removeCmd = rcmd;
153: }
154:
155: public void disposeAssocData(Interp interp) {
156: try {
157: interp.eval(removeCmd);
158: } catch (TclException e) {
159: throw new TclRuntimeError("unexpected TclException: " + e);
160: }
161: }
162:
163: public void test() throws TclException {
164: interp.eval(testCmd);
165: }
166:
167: public String getData() throws TclException {
168: return testCmd;
169: }
170:
171: } // end TestAssocData
172:
173: class Test2AssocData implements AssocData {
174: String internalData;
175:
176: public Test2AssocData(String data) {
177: internalData = data;
178: }
179:
180: public void disposeAssocData(Interp interp) {
181: }
182:
183: public String getData() throws TclException {
184: return internalData;
185: }
186:
187: } // end Test2AssocData
|