summaryrefslogtreecommitdiff
path: root/gr-run-waveform/test_xyzzy.cc
blob: f68c203ac80e4b1ed65f5d9fa38ffb0ccc80b7cd (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
/* -*- 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);

SCM_DEFINE (scm_i_make_gnuradio,
 "%gnuradio", 1, 0, 0,
 (SCM port),
 "Return a new port which reads and writes to @var{port}");
#if 0
#define FUNC_NAME s_scm_i_make_gnuradio
{
    SCM result;
    unsigned long mode = 0;
    
    SCM_VALIDATE_PORT (SCM_ARG1, port);

    if (scm_is_true (scm_output_port_p (port)))
        mode |= SCM_WRTNG;
    else if (scm_is_true (scm_input_port_p (port)))
        mode |= SCM_RDNG;
    
    result = make_tp (port, mode);
    
    return result;
}
#endif

static SCM
test_xyzzy (void)
{
    cerr << "FIXME:" << endl;
    SCM port;
    scm_input_port_p(port);
}

static void
inner_main (void *data, int argc, char **argv)
{
    scm_c_define_gsubr ("test-xyzzy", 0, 0, 0, test_xyzzy);

//    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";
    if (xyzzy_init(fullspec)) {
        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 << "PASSED: xyzzy_file_exists()" << endl;
    } else {
        cout << "FAILED: xyzzy_file_exists()" << endl;
    }    

    // Return a C port that will read the file contents

#if 0
    if (xyzzy_make_read_only_port("gnuradio")) {
        cout << "PASSED: xyzzy_make_read_only_port()" << endl;
    } else {
        cout << "FAILED: xyzzy_make_read_only_port()" << endl;
    }
#endif
    
    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;
}