File size: 7,918 Bytes
5610573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <pocketsphinx.h>
#include "pocketsphinx_internal.h"
#include "test_macros.h"

#define LEN(array) (sizeof(array) / sizeof(array[0]))
static char *good_argv[] = {
    "pocketsphinx",
    "-hmm",
    "en-us",
    "-samprate",
    "16000",
    "-beam",
    "0.005",
    "-backtrace",
    "yes"
};
static int good_argc = LEN(good_argv);
static char *good_json = \
    "{ \"hmm\": \"en-us\",\n"
    "  \"samprate\": 16000,\n"
    "  \"beam\": 5e-3,\n"
    "  \"backtrace\": true\n"
    "}";
static char *cmd_argv[] = {
    "pocketsphinx",
    "-hmm",
    "en-us",
    "-samprate",
    "16000",
    "-beam",
    "0.005",
    "-backtrace",
    "yes"
};
static int cmd_argc = LEN(cmd_argv);
static char *bad_argv[] = {
    "pocketsphinx",
    "-hmm",
    "en-us",
    "-samprate",
    "forty-two",
    "beam",
    "1e-80",
    "-backtrace",
    "WTF"
};
static int bad_argc = LEN(bad_argv);
static char *bad_json = \
    "{ \"hmm\": en us,\n"
    "  \"samprate\": 16000,\n"
    "  \"beam\": 5e-3,\n"
    "  \"backtrace\": true\n"
    "}";
static char *ugly_json = \
    "hmm: en-us samprate: 16000 beam: 0.005 backtrace: true";
static char *hard_json = \
    "{ \"hmm\": \"\\\\model\\\\en-us\",\n"
    "  \"keyphrase\": \"spam\tspam \\\"spam\\\" eggs\\nand spam\"\n"
    "}";
/* FIXME: Someday this will be supported */
static char *bad_hard_json = \
    "{ \"hmm\": \"\\\\model\\\\en-us\",\n"
    "  \"keyphrase\": \"spam\tspam \\\"spam\\\"\\u0020eggs\\nand spam\"\n"
    "}";

static void
check_live_args(ps_config_t *config)
{
    /* Check parsed values */
    TEST_EQUAL(0, strcmp("en-us", ps_config_str(config, "hmm")));
    TEST_EQUAL(16000, ps_config_int(config, "samprate"));
    TEST_EQUAL_FLOAT(5e-3, ps_config_float(config, "beam"));
    
    /* Set new values */
    TEST_ASSERT(ps_config_set_str(config, "hmm", "fr-fr"));
    TEST_EQUAL(0, strcmp("fr-fr", ps_config_str(config, "hmm")));
    TEST_ASSERT(ps_config_set_int(config, "samprate", 8000));
    TEST_EQUAL(8000, ps_config_int(config, "samprate"));
    TEST_ASSERT(ps_config_set_float(config, "beam", 1e-40));
    TEST_EQUAL_FLOAT(1e-40, ps_config_float(config, "beam"));
}

static void
test_config_init(void)
{
    ps_config_t *config;

    TEST_ASSERT(config = ps_config_init(NULL));
    ps_config_retain(config);
    TEST_EQUAL(1, ps_config_free(config));
    TEST_EQUAL(0, ps_config_free(config));

    TEST_ASSERT(config = ps_config_init(NULL));
    TEST_ASSERT(ps_config_set_str(config, "hmm", "en-us"));
    TEST_ASSERT(ps_config_set_int(config, "samprate", 16000));
    TEST_ASSERT(ps_config_set_float(config, "beam", 0.005));
    check_live_args(config);
    TEST_EQUAL(0, ps_config_free(config));
}

static void
test_config_args(void)
{
    ps_config_t *config;
    TEST_ASSERT(config = ps_config_parse_args(NULL, good_argc, good_argv));
    TEST_EQUAL(0, ps_config_free(config));
    TEST_EQUAL(NULL, ps_config_parse_args(NULL, bad_argc, bad_argv));

    TEST_ASSERT(config = ps_config_parse_args(NULL, cmd_argc, cmd_argv));
    check_live_args(config);
    TEST_EQUAL(0, ps_config_free(config));
}

static void
test_config_json(void)
{
    ps_config_t *config;
    char *json;
    
    TEST_ASSERT(config = ps_config_parse_json(NULL, good_json));
    TEST_EQUAL(0, ps_config_free(config));
    TEST_EQUAL(NULL, ps_config_parse_json(NULL, bad_json));

    TEST_ASSERT(config = ps_config_parse_json(NULL, good_json));
    check_live_args(config);
    TEST_EQUAL(0, ps_config_free(config));

    TEST_ASSERT(config = ps_config_parse_json(NULL, ugly_json));
    /* Make a copy now, before it changes in check_live_args */
    json = ckd_salloc(ps_config_serialize_json(config));
    check_live_args(config);
    TEST_EQUAL(0, ps_config_free(config));

    /* Check that serialized JSON gives the same result */
    TEST_ASSERT(config = ps_config_parse_json(NULL, json));
    check_live_args(config);
    TEST_EQUAL(0, ps_config_free(config));
    ckd_free(json);

    TEST_ASSERT(config = ps_config_parse_json(NULL, hard_json));
    printf("%s\n", ps_config_str(config, "hmm"));
    printf("%s\n", ps_config_str(config, "keyphrase"));
    TEST_EQUAL(0, strcmp(ps_config_str(config, "hmm"),
                         "\\model\\en-us"));
    TEST_EQUAL(0, strcmp(ps_config_str(config, "keyphrase"),
                         "spam\tspam \"spam\" eggs\nand spam"));
    json = ckd_salloc(ps_config_serialize_json(config));
    ps_config_free(config);

    TEST_ASSERT(config = ps_config_parse_json(NULL, json));
    printf("%s\n", ps_config_str(config, "hmm"));
    printf("%s\n", ps_config_str(config, "keyphrase"));
    TEST_EQUAL(0, strcmp(ps_config_str(config, "hmm"),
                         "\\model\\en-us"));
    TEST_EQUAL(0, strcmp(ps_config_str(config, "keyphrase"),
                         "spam\tspam \"spam\" eggs\nand spam"));
    ps_config_free(config);
    ckd_free(json);
    TEST_EQUAL(NULL, ps_config_parse_json(NULL, bad_hard_json));
}

static void
test_validate_config(void)
{
    ps_config_t *config;
    TEST_ASSERT(config =
                ps_config_parse_json(
                    NULL,
                    "hmm: \"" MODELDIR "/en-us/en-us\","
                    "lm: \"" MODELDIR "/en-us/en-us.lm.bin\","
                    "dict: \"" MODELDIR "/en-us/cmudict-en-us.dict\","
                    "fwdtree: true,"
                    "fwdflat: false,"
                    "bestpath: false,"
                    "samprate: 16000"));
    TEST_EQUAL(0, ps_config_validate(config));
    ps_config_free(config);
    TEST_ASSERT(config =
                ps_config_parse_json(
                    NULL,
                    "hmm: \"" MODELDIR "/en-us/en-us\","
                    "lm: \"" MODELDIR "/en-us/en-us.lm.bin\","
                    "jsgf: \"" DATADIR "/goforward.gram\","
                    "dict: \"" MODELDIR "/en-us/cmudict-en-us.dict\","
                    "fwdtree: true,"
                    "fwdflat: false,"
                    "bestpath: false,"
                    "samprate: 16000"));
    TEST_ASSERT(ps_config_validate(config) < 0);
    ps_config_free(config);
    TEST_ASSERT(config =
                ps_config_parse_json(
                    NULL,
                    "hmm: \"" MODELDIR "/en-us/en-us\","
                    "kws: \"" DATADIR "/goforward.kws\","
                    "jsgf: \"" DATADIR "/goforward.gram\","
                    "fsg: \"" DATADIR "/goforward.fsg\","
                    "dict: \"" MODELDIR "/en-us/cmudict-en-us.dict\","
                    "fwdtree: true,"
                    "fwdflat: false,"
                    "bestpath: false,"
                    "samprate: 16000"));
    TEST_ASSERT(ps_config_validate(config) < 0);
    ps_config_free(config);
    TEST_ASSERT(config =
                ps_config_parse_json(
                    NULL,
                    "hmm: \"" MODELDIR "/en-us/en-us\","
                    "keyphrase: \"bonjour alexis\","
                    "fwdtree: true,"
                    "fwdflat: false,"
                    "bestpath: false,"
                    "samprate: 16000"));
    TEST_EQUAL(0, ps_config_validate(config));
    ps_config_free(config);
}

static void
test_config_default(void)
{
    ps_config_t *config;
    TEST_ASSERT(config = ps_config_init(NULL));
    setenv("POCKETSPHINX_PATH", MODELDIR, 1);
    ps_default_search_args(config);
    TEST_EQUAL(0, strcmp(ps_config_str(config, "hmm"),
                         MODELDIR "/en-us/en-us"));
    TEST_EQUAL(0, strcmp(ps_config_str(config, "lm"),
                         MODELDIR "/en-us/en-us.lm.bin"));
    TEST_EQUAL(0, strcmp(ps_config_str(config, "dict"),
                         MODELDIR "/en-us/cmudict-en-us.dict"));
    ps_config_free(config);
}

int
main(int argc, char *argv[])
{
    (void)argc; (void)argv;

    test_config_init();
    test_config_args();
    test_config_json();
    test_validate_config();
    test_config_default();
    
    return 0;
}