OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [libcdl/] [testsuite/] [libcdl/] [cdl3.cxx] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      cdl3.cxx
4
//
5
//      Basic testing of the CdlInterpreter class
6
//
7
//==========================================================================
8
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
9
// -------------------------------------------                              
10
// This file is part of the eCos host tools.                                
11
// Copyright (C) 1999, 2000 Free Software Foundation, Inc.                  
12
//
13
// This program is free software; you can redistribute it and/or modify     
14
// it under the terms of the GNU General Public License as published by     
15
// the Free Software Foundation; either version 2 or (at your option) any   
16
// later version.                                                           
17
//
18
// This program is distributed in the hope that it will be useful, but      
19
// WITHOUT ANY WARRANTY; without even the implied warranty of               
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
21
// General Public License for more details.                                 
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with this program; if not, write to the                            
25
// Free Software Foundation, Inc., 51 Franklin Street,                      
26
// Fifth Floor, Boston, MA  02110-1301, USA.                                
27
// -------------------------------------------                              
28
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
29
//==========================================================================
30
//#####DESCRIPTIONBEGIN####                                             
31
//
32
// Author(s):           bartv
33
// Contributors:        bartv
34
// Date:                1999-01-21
35
// Description:         This test case deals with simple uses of the
36
//                      CdlInterpreter class, independent from any
37
//                      configuration data.
38
//
39
//####DESCRIPTIONEND####
40
//==========================================================================
41
 
42
#include <cstdio>
43
#include <cdlconfig.h>
44
#include <cdl.hxx>
45
#include <cyg/infra/cyg_ass.h>
46
#include <cyg/infra/cyg_trac.h>
47
#include <cyg/infra/testcase.h>
48
#include <cstdlib>
49
 
50
static CdlInterpreter interp = 0;
51
 
52
static int
53
extra_command1(ClientData data, Tcl_Interp* tcl_interp, int argc, char** argv)
54
{
55
    if (static_cast<CdlInterpreter>(data) != interp) {
56
        char* msg = "ClientData does not correspond to interpreter";
57
        Tcl_SetResult(tcl_interp, msg, TCL_STATIC);
58
        CYG_TEST_FAIL(msg);
59
        return TCL_ERROR;
60
    }
61
    if ((3 != argc) ||
62
        (0 != strcmp("extra_command1", argv[0])) ||
63
        (0 != strcmp("first_arg",      argv[1])) ||
64
        (0 != strcmp("second_arg",     argv[2]))) {
65
        char* msg = "Wrong arguments passed to extra_command1";
66
        interp->set_result(msg);
67
        CYG_TEST_FAIL(msg);
68
        return TCL_ERROR;
69
    }
70
    interp->set_result("To be or not to be");
71
    return TCL_OK;
72
}
73
 
74
static int
75
extra_command2(ClientData data, Tcl_Interp* tcl_interp, int argc, char** argv)
76
{
77
    if (0 != data) {
78
        char*msg = "ClientData should be zero here";
79
        Tcl_SetResult(tcl_interp, msg, TCL_STATIC);
80
        CYG_TEST_FAIL(msg);
81
        return TCL_ERROR;
82
    }
83
    if ((2 != argc) ||
84
        (0 != strcmp("extra_command2", argv[0])) ||
85
        (0 != strcmp("third_arg",      argv[1]))) {
86
        char* msg = "Wrong arguments passed to extra_command2";
87
        Tcl_SetResult(tcl_interp, msg, TCL_STATIC);
88
        CYG_TEST_FAIL(msg);
89
        return TCL_ERROR;
90
    }
91
    Tcl_SetResult(tcl_interp, "That is the question", TCL_STATIC);
92
    return TCL_OK;
93
}
94
 
95
int
96
main(int argc, char** argv)
97
{
98
    // Start by creating a simple interpreter using default settings
99
    bool ok = true;
100
    interp = CdlInterpreterBody::make();
101
    if (0 == interp) {
102
        CYG_TEST_FAIL_FINISH("Unable to create a new interpreter with default settings");
103
    }
104
    if ((!interp->check_this(cyg_quick)) ||
105
        (!interp->check_this(cyg_extreme))) {
106
        CYG_TEST_FAIL("check_this() failed");
107
        ok = false;
108
    }
109
    if ((0 != interp->get_configuration()) ||
110
        (0 != interp->get_package())) {
111
        CYG_TEST_FAIL("a new interpreter should not be associated with any package or configuration");
112
        ok = false;
113
    }
114
 
115
    // Now try evaluating a very simple script
116
    std::string str_result;
117
    if ((TCL_OK != interp->eval("expr 2 * 2 * 2\n", str_result)) ||
118
        ("8" != str_result)) {
119
        CYG_TEST_FAIL("simple command execution failed");
120
        ok = false;
121
    }
122
    // And something a bit more interesting
123
    std::string fibonaci = "                                                            \n\
124
proc fibonaci { arg } {                                                                 \n\
125
    if { $arg < 3 } {                                                                   \n\
126
        return 1                                                                        \n\
127
    } else {                                                                            \n\
128
        set result [expr [fibonaci [expr $arg - 1]] + [fibonaci [expr $arg - 2]]]       \n\
129
        return $result                                                                  \n\
130
    }                                                                                   \n\
131
}                                                                                       \n\
132
return [fibonaci 10]                                                                    \n\
133
";
134
    if ((TCL_OK != interp->eval(fibonaci, str_result)) ||
135
        ("55" != str_result)) {
136
        CYG_TEST_FAIL("full script execution failed");
137
        ok = false;
138
    }
139
 
140
    // A new interpreter should not know about "extra_command1"
141
    if ((TCL_OK != interp->eval("info command extra_command1", str_result)) ||
142
        ("" != str_result)) {
143
        CYG_TEST_FAIL("new interpreter should not have an extra_command1 command");
144
        ok = false;
145
    }
146
    try {
147
        interp->add_command("extra_command1", &extra_command1, 0);
148
        if ((TCL_OK != interp->eval("info command extra_command1", str_result)) ||
149
            ("extra_command1" != str_result)) {
150
            CYG_TEST_FAIL("interpreter does not know the new extra_command1 command");
151
            ok = false;
152
        }
153
        if ((TCL_OK != interp->eval("extra_command1 first_arg second_arg", str_result)) ||
154
            ("To be or not to be" != str_result)) {
155
            CYG_TEST_FAIL("execution of a new command failed");
156
            ok = false;
157
        }
158
        interp->remove_command("extra_command1");
159
    }
160
    catch(std::bad_alloc e) {
161
        CYG_TEST_FAIL("unable to add a new command to the interpreter");
162
        ok = false;
163
    }
164
 
165
    // Check the variables support
166
    try {
167
        interp->set_variable("some_variable", "random_value");
168
    }
169
    catch(std::bad_alloc e) {
170
        CYG_TEST_FAIL("unable to set a global variable inside the interpreter");
171
        ok = false;
172
    }
173
    if ("random_value" != interp->get_variable("some_variable")) {
174
        CYG_TEST_FAIL("unable to retrieve global variable setting");
175
        ok = false;
176
    }
177
    // Make absolutely sure the variable exists
178
    if ((TCL_OK != interp->eval("info exists ::some_variable", str_result)) ||
179
        ("1" != str_result)) {
180
        CYG_TEST_FAIL("Tcl code does not know about new global variable");
181
    }
182
    interp->unset_variable("some_variable");
183
    if ("" != interp->get_variable("some_variable")) {
184
        CYG_TEST_FAIL("variable still exists after it has been removed");
185
        ok = false;
186
    }
187
 
188
    // Check the AssocData support
189
    if (0 != interp->get_assoc_data("some_key")) {
190
        CYG_TEST_FAIL("unexpected assoc data already present");
191
        ok = false;
192
    }
193
    interp->set_assoc_data("some_key", static_cast<ClientData>(&ok), 0);
194
    if (&ok != static_cast<bool*>(interp->get_assoc_data("some_key"))) {
195
        CYG_TEST_FAIL("unable to retrieve assoc data");
196
        ok = false;
197
    }
198
    interp->delete_assoc_data("some_key");
199
    if (0 != interp->get_assoc_data("some_key")) {
200
        CYG_TEST_FAIL("assoc data still present after retrieval");
201
        ok = false;
202
    }
203
 
204
    // Make the interpreter safe. This should remove the file command.
205
    if ((TCL_OK != interp->eval("info command file", str_result)) ||
206
        ("file" != str_result)) {
207
        CYG_TEST_FAIL("interpreter should still have the file command");
208
        ok = false;
209
    }
210
    interp->make_safe();
211
    if ((TCL_OK != interp->eval("info command file", str_result)) ||
212
        ("" != str_result)) {
213
        CYG_TEST_FAIL("interpreter should no longer have the file command");
214
        ok = false;
215
    }
216
 
217
    // This is just a compilation check to make sure that interpreters can
218
    // be deleted.
219
#ifdef CYGBLD_LIBCDL_USE_SMART_POINTERS
220
    interp.destroy();
221
#else
222
    delete interp;
223
    interp = 0;
224
#endif    
225
 
226
    if (ok) {
227
        CYG_TEST_PASS("interpreter with default settings is functional");
228
    }
229
    ok = true;
230
 
231
    // Now try to create a new Tcl interpreter, register the extra command
232
    // at the Tcl level, and turn it into a CdlInterpreter
233
    Tcl_Interp* tcl_interp = Tcl_CreateInterp();
234
    if (0 == tcl_interp) {
235
        CYG_TEST_FAIL_FINISH("unable to create new Tcl interpreter");
236
    }
237
    if (0 == Tcl_CreateCommand(tcl_interp, "extra_command2", &extra_command2, 0, 0)) {
238
        CYG_TEST_FAIL_FINISH("unable to add new command to Tcl interpreter");
239
    }
240
 
241
    // And turn the Tcl interpreter into a Cdl one.
242
    interp = CdlInterpreterBody::make(tcl_interp);
243
    if (0 == interp) {
244
        CYG_TEST_FAIL_FINISH("unable to create new CDL interpreter using existing Tcl one");
245
        ok = false;
246
    }
247
    if (!interp->check_this(cyg_quick)) {
248
        CYG_TEST_FAIL_FINISH("new CDL interpreter fails checks");
249
        ok = false;
250
    }
251
 
252
    // The new interpreter should not have an extra_command1 command.
253
    if ((TCL_OK != interp->eval("info command extra_command1", str_result)) ||
254
        ("" != str_result)) {
255
        CYG_TEST_FAIL("new interpreter should not have an extra_command1 command");
256
        ok = false;
257
    }
258
    // But it should still have an extra_command2 command
259
    if ((TCL_OK != interp->eval("info command extra_command2", str_result)) ||
260
        ("extra_command2" != str_result)) {
261
        CYG_TEST_FAIL("new interpreter should have an extra_command2 command");
262
        ok = false;
263
    }
264
    // And that command should work
265
    if ((TCL_OK != interp->eval("extra_command2 third_arg", str_result)) ||
266
        ("That is the question" != str_result)) {
267
        CYG_TEST_FAIL("extra_command2 command not functional");
268
        ok = false;
269
    }
270
    // Get rid of the CdlInterpreter object. The Tcl interpreter should still
271
    // be valid.
272
#ifdef CYGBLD_LIBCDL_USE_SMART_POINTERS
273
    interp.destroy();
274
#else
275
    delete interp;
276
    interp = 0;
277
#endif
278
    if (Tcl_InterpDeleted(tcl_interp)) {
279
        CYG_TEST_FAIL("Tcl interpreter deleted when it should still be around");
280
        ok = false;
281
    }
282
 
283
    if (ok) {
284
        CYG_TEST_PASS("custom interpreter is functional");
285
    }
286
 
287
    return EXIT_SUCCESS;
288
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.