summaryrefslogtreecommitdiff
path: root/gr-run-waveform/test_xyzzy.cc
blob: a44666833b741a7ccb22d716b3c6828752a68862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* -*- c++ -*- */
/*
 * Copyright 2004 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <libguile.h>
#include <boost/cstdint.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>

// Include our definitions
#include "xyzzy.h"

using namespace std;

boost::uint8_t hex2digit(boost::uint8_t digit);
boost::shared_array<boost::uint8_t> hex2mem(const std::string &str);

static void
inner_main (void *data, int argc, char **argv)
{
    fprintf(stderr, "TRACE %s: %d\n", __FUNCTION__, __LINE__);
    scm_xyzzy_init();

    string srcdir = SRCDIR;

    // Lasd readline, as it makes life on he guile command lne
    string file = srcdir;
    file += "/guile/readline.scm";    
    scm_c_primitive_load (file.c_str());
    
    file = srcdir;
    file += "/guile/simple.scm";
    SCM simple = scm_c_primitive_load (file.c_str());
    if (scm_is_true(scm_c_primitive_load (file.c_str()))) {
        fprintf(stderr, "PASSED: loading simple.scm\n");
    } else {
        fprintf(stderr, "FAILED: loading simple.scm\n" );
    }
    SCM s_symbol = scm_c_lookup("result1");
    SCM s_value = scm_variable_ref(s_symbol);
    if (scm_to_locale_string(s_value) == string("/usr/share/guile/1.8/ice-9/boot-9.scm")) {
        fprintf(stderr, "PASSED: search-path from C\n");
    } else {
        fprintf(stderr, "FAILED: search-path from C\n" );
    }
    
    s_symbol = scm_c_lookup("result2");
    s_value = scm_variable_ref(s_symbol);
    if (scm_to_locale_string(s_value) == string("/usr/share/guile/1.8/ice-9/boot-9.scm")) {
        fprintf(stderr, "PASSED: search-path path-with-xyzzy from C\n");
    } else {
        fprintf(stderr, "FAILED: search-path path-with-xyzzy from C\n" );
    }
    s_symbol = scm_c_lookup("result3");
    s_value = scm_variable_ref(s_symbol);
    if (scm_to_locale_string(s_value) == string("/usr/share/guile/1.8/ice-9/boot-9.scm")) {
        fprintf(stderr, "PASSED: xyzzy-search from C\n");
    } else {
        fprintf(stderr, "FAILED: xyzzy-search from C\n" );
    }
    s_symbol = scm_c_lookup("result4");
    s_value = scm_variable_ref(s_symbol);
    if (scm_to_locale_string(s_value) == string("/-xyzzy-/ice-9/boot-9.scm")) {
        fprintf(stderr, "PASSED: xyzzy-search-path path-with-xyzzy from C\n");
    } else {
        fprintf(stderr, "FAILED: xyzzy-search-path path-with-xyzzy from C\n" );
    }
    
    // This test loads a scheme test case that defines a 'cat' function to
    // spew the contents of the file from our fake file system.
    file = srcdir;
    file += "/guile/cat.scm";
    if (scm_is_true(scm_c_primitive_load (file.c_str()))) {
        fprintf(stderr, "PASSED: loading cat.scm\n");
    } else {
        fprintf(stderr, "FAILED: loading cat.scm\n" );
    }
    // It tacky, but the test case defines the name of this input
    // port as 'foo'. So make sure that's what we got...
    s_symbol = scm_c_lookup("foo");
    // Get the handle to the port
    s_value = scm_variable_ref(s_symbol);
    SCM result = scm_input_port_p (s_value);
    if (scm_is_true(result)) {
        fprintf(stderr, "PASSED: make-gnuradio-port()\n");
    } else {
        fprintf(stderr, "FAILED: make-gnuradio-port()\n" );
    }

    if (scm_char_ready_p (s_value)) {
        fprintf(stderr, "PASSED: scm_char_ready_p()\n");
    } else {
        fprintf(stderr, "FAILED: scm_char_ready_p()\n" );
    }
    
    char *bar[20];    
    if (scm_c_read(s_value, bar, 10)) {
        fprintf(stderr, "PASSED: read from port\n");
    } else {
        fprintf(stderr, "FAILED: read from port\n" );
    }
    
    result = scm_output_port_p (s_value);
    if (scm_is_true(result)) {
        fprintf(stderr, "FAILED: make-gnuradio-port()\n");
    } else {
        fprintf(stderr, "PASSED: make-gnuradio-port()\n" );
    }
    
    scm_flush_all_ports();
    scm_shell (argc, argv);
}

int
main(int argc, char *argv[])
{
    // Test static methods
    string hello("Hello World!");
    boost::uint8_t *hi = reinterpret_cast<boost::uint8_t *>(const_cast<char *>
                                                            (hello.c_str()));
    struct string_entry entry = {
        12,
        hi
    };
    
    if (XYZZY::read_string(entry) == hello) {
        cout << "PASSED: XYZZY::read_string(struct string_entry &)" << endl;
    } else {
        cout << "FAILED: XYZZY::read_string(struct string_entry &)" << endl;
    }
    
    if (XYZZY::read_string(hi, hello.size()) == hello) {
        cout << "PASSED: XYZZY::read_string(struct string_entry &)" << endl;
    } else {
        cout << "FAILED: XYZZY::read_string(struct string_entry &)" << endl;
    }

    // Test other methods
    XYZZY xyzzy;

    string fake_magic("-XyZzY-");
    boost::shared_array<boost::uint8_t> fake_header_data = hex2mem(
        "2d 58 79 5a 7a 59 2d 00 00 00 00 1c 00 00 05 e8 00 00 00 bd 00 00 06 04 00 21 ee 58");
    boost::shared_ptr<struct header> head = xyzzy.read_header(fake_header_data.get());
    if ((head->magic == fake_magic)
        && (head->offset_to_directory == 28)
        && (head->size_of_directory == 1512)
        && (head->number_of_dir_entries == 189)
        && (head->offset_to_strings == 1540)
        && (head->size_of_strings == 2223704)) {
        cout << "PASSED: XYZZY::read_header()" << endl;
    } else {
        cout << "FAILED: XYZZY::read_header()" << endl;
    }

#if 0
    if (xyzzy.init()) {
        cout << "PASSED: XYZZY::init()" << endl;
    } else {
        cout << "FAILED: XYZZY::init()" << endl;
    }
#endif

    // Look for a file to exist, which shouldn't as we haven't loaded the data
    if (xyzzy_file_exists("srfi/srfi-35.scm")) {
        cout << "FAILED: xyzzy_file_exists(not yet)" << endl;
    } else {
        cout << "PASSED: xyzzy_file_exists(not yet)" << endl;
    }    

    // Initialize with the data file produced by gen-xyzzy.
    string fullspec = "./filesystem.dat";
    char *name = const_cast<char *>(fullspec.c_str());
    if (xyzzy_init(name)) {
        cout << "PASSED: xyzzy_init()" << endl;
    } else {
        cout << "FAILED: xyzzy_init()" << endl;
    }
     
    // Does a file with name 'filename' exist in magic filesystem?
    if (xyzzy_file_exists("srfi/srfi-35.scm")) {
        cout << "FAILED: xyzzy_file_exists(shouldn't exist)" << endl;
    } else {
        cout << "PASSED: xyzzy_file_exists(shouldn't exist)" << endl;
    }
    
    // Does a file with name 'filename' exist in magic filesystem?
    if (xyzzy_file_exists("/-xyzzy-/srfi/srfi-35.scm")) {
        cout << "PASSED: xyzzy_file_exists(should exist)" << endl;
    } else {
        cout << "FAILED: xyzzy_file_exists(should exist)" << endl;
    }
    
    // Does a file with name 'filename' exist in magic filesystem?
    if (xyzzy_file_exists("/-xyzzy-/srfi/srfi-99.scm")) {
        cout << "FAILED: xyzzy_file_exists(shouldn't exist)" << endl;
    } else {
        cout << "PASSED: xyzzy_file_exists(shouldn't exist)" << endl;
    }
    
    scm_boot_guile (argc, argv, inner_main, 0);   
}


/// \brief Convert a Hex digit into it's decimal value.
///
/// @param digit The digit as a hex value
///
/// @return The byte as a decimal value.
boost::uint8_t
hex2digit (boost::uint8_t digit)
{  
    if (digit == 0)
        return 0;
    
    if (digit >= '0' && digit <= '9')
        return digit - '0';
    if (digit >= 'a' && digit <= 'f')
        return digit - 'a' + 10;
    if (digit >= 'A' && digit <= 'F')
        return digit - 'A' + 10;
    
    // shouldn't ever get this far
    return -1;
}

/// \brief Encode a Buffer from a hex string.
///
/// @param str A hex string, ex... "00 03 05 0a"
///
/// @return A reference to a Buffer in host endian format. This is
///		primary used only for testing to create binary data
///		from an easy to read and edit format.
boost::shared_array<boost::uint8_t>
hex2mem(const std::string &str)
{
//    GNASH_REPORT_FUNCTION;
    size_t count = str.size();
    
    size_t size = (count/3) + 4;
    boost::uint8_t ch = 0;
    
    boost::shared_array<boost::uint8_t> data(new boost::uint8_t[count]);

    size_t j = 0;
    for (size_t i=0; i<count; i++) {
        if (str[i] == ' ') {      // skip spaces.
            continue;
        }
        ch = hex2digit(str[i]) << 4;
        ch |= hex2digit(str[i+1]);
        data[j++] = ch;
        i++;
    }
    
    return data;
}