/** * gsound_context_play_simplev: * @context: A #GSoundContext * @attrs: (element-type utf8 utf8): Attributes * @cancellable: (allow-none): A #GCancellable * @error: Return location for error * * Returns: %TRUE on success, %FALSE on error * * Rename to: gsound_context_play_simple */ gboolean gsound_context_play_simplev(GSoundContext *self, GHashTable *attrs, GCancellable *cancellable, GError **error) { ca_proplist *proplist; int res = ca_proplist_create(&proplist); if (!test_return (res, error)) return FALSE; hash_table_to_prop_list (attrs, proplist); res = ca_context_play_full(self->priv->ca, GPOINTER_TO_INT(cancellable), proplist, NULL, NULL); if (cancellable) g_cancellable_connect(cancellable, G_CALLBACK(on_cancellable_cancelled), g_object_ref(self), g_object_unref); ca_proplist_destroy(proplist); return test_return (res, error); }
/** * gsound_context_play_simple: (skip) * @context: A #GSoundContext * @cancellable: (allow-none): A #GCancellable, or %NULL * @error: Return location for error, or %NULL * @...: Arguments * * Returns: %TRUE on success, or %FALSE, populating @error */ gboolean gsound_context_play_simple (GSoundContext *self, GCancellable *cancellable, GError **error, ...) { ca_proplist *pl; va_list args; int res; g_return_val_if_fail(GSOUND_IS_CONTEXT(self), FALSE); if ((res = ca_proplist_create(&pl)) != CA_SUCCESS) return test_return(res, error); va_start(args, error); var_args_to_prop_list (args, pl); va_end(args); res = ca_context_play_full(self->priv->ca, GPOINTER_TO_INT(cancellable), pl, NULL, NULL); if (cancellable) g_cancellable_connect(cancellable, G_CALLBACK(on_cancellable_cancelled), g_object_ref(self), g_object_unref); ca_proplist_destroy(pl); return test_return(res, error); }
/** * gsound_context_play_full: (skip) * @context: A #GSoundContext * @cancellable: (allow-none): A #GCancellable, or %NULL * @callback: (scope async): callback * @user_data: User data passed to @callback * @...: Attributes * */ void gsound_context_play_full(GSoundContext *self, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data, ...) { GError *inner_error = NULL; ca_proplist *proplist; va_list args; int res = ca_proplist_create(&proplist); if (!test_return (res, &inner_error)) { g_simple_async_report_take_gerror_in_idle (G_OBJECT(self), callback, user_data, inner_error); } va_start(args, user_data); var_args_to_prop_list (args, proplist); va_end(args); GSimpleAsyncResult *result = g_simple_async_result_new(G_OBJECT(self), callback, user_data, NULL /*FIXME*/); res = ca_context_play_full(self->priv->ca, GPOINTER_TO_INT(cancellable), proplist, on_ca_play_full_finished, result); if (cancellable) g_cancellable_connect(cancellable, G_CALLBACK(on_cancellable_cancelled), g_object_ref(self), g_object_unref); ca_proplist_destroy(proplist); if (!test_return (res, &inner_error)) { g_simple_async_report_take_gerror_in_idle (G_OBJECT(self), callback, user_data, inner_error); g_object_unref(result); } }
/** * gsound_context_open: * @context: A #GSoundContext * @error: Return location for error, or %NULL * * Returns: %TRUE if the output device was opened successfully, or %FALSE * (populating @error) */ gboolean gsound_context_open(GSoundContext *self, GError **error) { g_return_val_if_fail(GSOUND_IS_CONTEXT(self), FALSE); return test_return (ca_context_open(self->priv->ca), error); }
int main() { static_assert(noexcept(ex::new_delete_resource()), "Must be noexcept"); test_return(); test_equality(); test_allocate_deallocate(); }
/* * A simple test for the binary embedding */ int test_embed_bin() { int i, j, n, err = 0; string_t strs[10]; input_config("lines"); char *test_file = getenv("TEST_FILE"); n = input_open(test_file); input_read(strs, n); test_printf("Testing binary embedding"); config_set_string(&cfg, "features.vect_embed", "bin"); config_set_string(&cfg, "features.vect_norm", "none"); for (i = 0, err = 0; i < n; i++) { fvec_t *fv = fvec_extract(strs[i].str, strs[i].len); double n = 0; for (j = 0; j < fv->len; j++) n += fv->val[j]; err += fabs(n - fv->len) > 1e-6; fvec_destroy(fv); } test_return(err, n); input_free(strs, n); input_close(); return err; }
/* * A simple test for the binary embedding */ int test_embed_tfidf() { int i, j, n, err = 0; string_t strs[10]; config_set_string(&cfg, "features.vect_norm", "none"); config_set_string(&cfg, "features.tfidf_file", TEST_TFIDF); unlink(TEST_TFIDF); char *test_file = getenv("TEST_FILE"); idf_create(test_file); test_printf("Testing TFIDF embedding"); input_config("lines"); n = input_open(test_file); input_read(strs, n); /* Compute IDF manually */ config_set_string(&cfg, "features.vect_embed", "bin"); fvec_t *w = fvec_zero(); for (i = 0, err = 0; i < n; i++) { fvec_t *fv = fvec_extract(strs[i].str, strs[i].len); fvec_add(w, fv); fvec_destroy(fv); } fvec_invert(w); fvec_mul(w, n); fvec_log2(w); if (!idf_check(w)) { err++; test_error("(%d) internal idf values seem to be wrong", i); } /* Invert w for multiplying out IDFs */ fvec_invert(w); config_set_string(&cfg, "features.vect_embed", "tfidf"); for (i = 0, err = 0; i < n; i++) { fvec_t *fv = fvec_extract(strs[i].str, strs[i].len); fvec_times(fv, w); /* Check if rest tf */ double d = 0; for (j = 0; j < fv->len; j++) d += fv->val[j]; err += fabs(d - 1.0) > 1e-6; fvec_destroy(fv); } test_return(err, n); fvec_destroy(w); input_free(strs, n); input_close(); idf_destroy(); unlink(TEST_TFIDF); return err; }
/* * A simple test for the l2 norm */ int test_norm_l2() { int i, j, n, err = 0; string_t strs[10]; input_config("lines"); char *test_file = getenv("TEST_FILE"); n = input_open(test_file); input_read(strs, n); test_printf("Testing L2 normalization"); config_set_string(&cfg, "features.vect_norm", "l2"); for (i = 0, err = 0; i < n; i++) { fvec_t *fv = fvec_extract(strs[i].str, strs[i].len); double n = 0; for (j = 0; j < fv->len; j++) n += fv->val[j] * fv->val[j]; err += fabs(sqrt(n) - 1.0) > 1e-6; fvec_destroy(fv); } test_return(err, n); input_free(strs, n); input_close(); return err; }
/* * A simple static test for the feature vectors */ int test_static() { int i, err = 0; fvec_t *f; test_printf("Extraction of feature vectors"); for (i = 0; tests[i].str; i++) { fvec_reset_delim(); config_set_string(&cfg, "features.ngram_delim", tests[i].dlm); config_set_int(&cfg, "features.ngram_len", tests[i].nlen); /* Extract features */ f = fvec_extract(tests[i].str, strlen(tests[i].str), "test"); /* Check for correct number of dimensions */ if (f->len != tests[i].len) { test_error("(%d) len %d != %d", i, f->len, tests[i].len); err++; } fvec_destroy(f); } test_return(err, i); return err; }
/* * A simple stress test for the feature table */ int test_stress() { int i, j, err = 0; fvec_t *f; char buf[STR_LENGTH + 1]; test_printf("Stress test for feature vectors"); config_set_string(&cfg, "features.ngram_delim", "0"); ftable_init(); for (i = 0; i < STRESS_RUNS; i++) { config_set_int(&cfg, "features.ngram_len", rand() % 10 + 1); /* Create random key and string */ for (j = 0; j < STR_LENGTH; j++) buf[j] = rand() % 10 + '0'; buf[j] = 0; /* Extract features */ f = fvec_extract(buf, strlen(buf), "test"); /* Destroy features */ fvec_destroy(f); } ftable_destroy(); test_return(err, STRESS_RUNS); return err; }
int main(int argc, char **argv) { struct timeval stv, etv; const char * const *engines; for (unsigned int i = START; i <= END; i *= 10) { gettimeofday(&stv, NULL); for (unsigned int j = 0; j < i; j++) i += (uintptr_t) test_return(NULL, NULL); gettimeofday(&etv, NULL); printf(RFMT, "return", i, TIME(stv, etv), 0); } engines = ql_engine_list(); assert(engines); for (unsigned int i = 0; engines[i]; i++) { qlParameter param; qlState *state; for (unsigned int j = START; j <= END; j *= 10) { gettimeofday(&stv, NULL); for (unsigned int k = 0; k < j / YIELDS; k++) { state = ql_state_new(NULL, engines[i], test_yield, 0); while (ql_state_step(state, ¶m)) continue; } gettimeofday(&etv, NULL); printf(FMT, engines[i], "yield", j, TIME(stv, etv), j / YIELDS * 2); } } return 0; }
int main() { test_return(); test_void_return(); test_funcall(); test_void_arg(); return 0; }
/* * A simple stress test for classification */ int test_stress() { int i, j, k, err = 0; fvec_t *f; farray_t *fa; char buf[STR_LENGTH + 1], label[32]; test_printf("Stress test for classification"); for (i = 0; i < STRESS_RUNS; i++) { /* Create array */ fa = farray_create("test"); for (j = 0; j < NUM_VECTORS; j++) { for (k = 0; k < STR_LENGTH; k++) buf[k] = rand() % 10 + '0'; buf[k] = 0; /* Extract features */ f = fvec_extract(buf, strlen(buf), "test"); snprintf(label, 32, "label%.2d", rand() % 10); /* Add to array */ farray_add(fa, f, label); } assign_t *a = class_assign(fa, fa); assign_destroy(a); farray_destroy(fa); } test_return(err, STRESS_RUNS); return err; }
/** * Test clustering */ int test_cluster_single() { int i, j, k, err = 0; test_printf("Clustering using prototypes (single)"); /* Prepare test data */ ; farray_t *fa = farray_create("test"); for (i = 0; i < DATA_LEN; i++) { fvec_t *f = fvec_extract(test_data[i], strlen(test_data[i]), NULL); farray_add(fa, f, "test"); } /* Get clustering */ config_set_string(&cfg, "cluster.link_mode", "single"); cluster_t *c = cluster_linkage(fa, 0); /* Check number of clusters */ err += (c->num != DATA_CLUSTER); /* Check position of prototypes */ for (k = 0; k < DATA_LEN; k += DATA_LEN / DATA_CLUSTER) for (j = 0; j < DATA_LEN / DATA_CLUSTER - 1; j++) err += c->cluster[k + j] != c->cluster[k + j + 1]; /* Clean up */ cluster_destroy(c); farray_destroy(fa); test_return(err, 1 + DATA_CLUSTER * (DATA_LEN / DATA_CLUSTER - 1)); return err; }
/** * gsound_context_set_driver: * @context: A #GSoundContext * @driver: libcanberra driver to use * @error: Return location for error, or %NULL * * Returns: %TRUE if the libcanberra driver was set successfully */ gboolean gsound_context_set_driver(GSoundContext *self, const char *driver, GError **error) { g_return_val_if_fail (GSOUND_IS_CONTEXT(self), FALSE); return test_return (ca_context_set_driver(self->priv->ca, driver), error); }
/* * A simple load and save test case */ int test_load_save() { int i, j, err = 0; fvec_t *f, *g; gzFile *z; test_printf("Loading and saving of feature vectors"); fvec_reset_delim(); config_set_string(&cfg, "features.ngram_delim", " "); config_set_int(&cfg, "features.ngram_len", 2); /* Create and save feature vectors */ z = gzopen(TEST_FILE, "wb9"); if (!z) { printf("Could not create file (ignoring)\n"); return FALSE; } for (i = 0; tests[i].str; i++) { f = fvec_extract(tests[i].str, strlen(tests[i].str), "test"); fvec_save(f, z); fvec_destroy(f); } gzclose(z); /* Load and compare feature vectors */ z = gzopen(TEST_FILE, "r"); for (i = 0; tests[i].str; i++) { f = fvec_extract(tests[i].str, strlen(tests[i].str), "test"); g = fvec_load(z); /* Check dimensions and values */ for (j = 0; j < f->len && j < g->len; j++) { if (f->dim[j] != g->dim[j]) { test_error("(%d) f->dim[%d] != g->dim[%d]", i, j, j); break; } if (fabs(f->val[j] - g->val[j]) > 10e-10) { test_error("(%d) f->val[%d] != g->val[%d]", i, j, j); break; } } err += (j < f->len || j < g->len); fvec_destroy(f); fvec_destroy(g); } gzclose(z); unlink(TEST_FILE); test_return(err, i); return err; }
/** * gsound_context_cachev: * @context: A #GSoundContext * @attrs: (element-type utf8 utf8): Hash table of attrerties * @error: Return location for error, or %NULL * * Returns: %TRUE on success * * Rename to: gsound_context_cache */ gboolean gsound_context_cachev(GSoundContext *self, GHashTable *attrs, GError **error) { ca_proplist *proplist; int res = ca_proplist_create(&proplist); if (!test_return (res, error)) return FALSE; hash_table_to_prop_list (attrs, proplist); res = ca_context_cache_full(self->priv->ca, proplist); ca_proplist_destroy(proplist); return test_return(res, error); }
/** * gsound_context_change_attrsv: * @context: A #GSoundContext * @attrs: (element-type utf8 utf8): Hash table of attributes to set * @error: Return location for error, or %NULL * * Returns: %TRUE if attributes were updated successfully */ gboolean gsound_context_change_attrsv (GSoundContext *self, GHashTable *attrs, GError **error) { ca_proplist *pl; g_return_val_if_fail(GSOUND_IS_CONTEXT(self), FALSE); int res = ca_proplist_create(&pl); if (!test_return (res, error)) return FALSE; hash_table_to_prop_list (attrs, pl); res = ca_context_change_props_full(self->priv->ca, pl); ca_proplist_destroy(pl); return test_return(res, error); }
static gboolean gsound_context_real_init(GInitable *initable, GCancellable *cancellable, GError **error) { GSoundContext *self = GSOUND_CONTEXT(initable); if (self->priv->ca) return TRUE; int success = ca_context_create(&self->priv->ca); if (!test_return(success, error)) return FALSE; /* Set a couple of attributes here if we can */ ca_proplist *pl; ca_proplist_create(&pl); ca_proplist_sets(pl, CA_PROP_APPLICATION_NAME, g_get_application_name()); if (g_application_get_default()) { GApplication *app = g_application_get_default (); ca_proplist_sets(pl, CA_PROP_APPLICATION_ID, g_application_get_application_id(app)); } success = ca_context_change_props_full(self->priv->ca, pl); ca_proplist_destroy(pl); if (!test_return(success, error)) { ca_context_destroy(self->priv->ca); self->priv->ca = NULL; } return TRUE; }
/** * gsound_context_cache: (skip) * @context: A #GSoundContext * @error: Return location for error, or %NULL * @...: attributes * * Returns: %TRUE on success, %FALSE otherwise */ gboolean gsound_context_cache(GSoundContext *self, GError **error, ...) { ca_proplist *pl; va_list args; int res; g_return_val_if_fail(GSOUND_IS_CONTEXT(self), FALSE); if ((res = ca_proplist_create(&pl)) != CA_SUCCESS) return test_return(res, error); va_start(args, error); var_args_to_prop_list (args, pl); va_end(args); res = ca_context_cache_full(self->priv->ca, pl); ca_proplist_destroy(pl); return test_return(res, error); }
int main() { adam_init(); test_move(); test_return(); test_const(); test_monitor(); test_packed(); test_sparse(); test_arrayops(); test_instanceops(); test_invoke(); adam_finalize(); return 0; }
int test_pos_ngrams() { int i, err = 0; fvec_t *f; /* Test for positional n-grams */ test_t t[] = { {"b b b b b", 3, 0, 1}, {"b b b b b", 3, 1, 3}, {"b b b b b", 2, 0, 1}, {"b b b b b", 2, 1, 4}, {NULL, 0, 0, 0} }; test_printf("Testing positional n-grams"); /* Hack to set delimiters */ config_set_string(&cfg, "features.granularity", "tokens"); config_set_string(&cfg, "features.token_delim", " "); fvec_delim_set(" "); for (i = 0; t[i].str; i++) { config_set_int(&cfg, "features.ngram_len", t[i].nlen); config_set_bool(&cfg, "features.ngram_pos", t[i].flag); config_set_int(&cfg, "features.pos_shift", 0); /* Extract features */ f = fvec_extract(t[i].str, strlen(t[i].str)); /* Check for correct number of dimensions */ if (f->len != t[i].len) { test_error("(%d) len %d != %d", i, f->len, t[i].len); err++; } fvec_destroy(f); } config_set_bool(&cfg, "features.ngram_pos", 0); test_return(err, i); return err; }
/** * Simple test cases classification */ int test_classify() { int i, k, err = 0; fvec_t *f; test_printf("Classification using prototypes"); /* Prepare training data */ farray_t *fa1 = farray_create("train"); for (i = 0; train_data[i].str; i++) { f = fvec_extract(train_data[i].str, strlen(train_data[i].str), NULL); farray_add(fa1, f, train_data[i].label); } /* Prepare testing data */ farray_t *fa2 = farray_create("train"); for (i = 0; test_data[i].str; i++) { f = fvec_extract(test_data[i].str, strlen(test_data[i].str), NULL); farray_add(fa2, f, test_data[i].label); } /* Classification of test data */ config_set_float(&cfg, "classify.max_dist", 1.41); assign_t *a = class_assign(fa2, fa1); /* Check predicted labels */ for (k = 0; test_data[k].str; k++) { char *l = farray_get_label(fa1, a->proto[k]); err += strcmp(l, test_data[k].label) != 0; } /* Clean up */ assign_destroy(a); farray_destroy(fa1); farray_destroy(fa2); test_return(err, i); return err; }
/* * A stres test for the addition of feature vectors */ int test_stress_add() { int i, j, err = 0; fvec_t *fx, *fy, *fz; char buf[STR_LENGTH + 1]; test_printf("Stress test for addition of feature vectors"); /* Create empty vector */ fz = fvec_extract("aa0bb0cc", 8, "zero"); for (i = 0; i < NUM_VECTORS; i++) { /* Create random key and string */ for (j = 0; j < STR_LENGTH; j++) buf[j] = rand() % 10 + '0'; buf[j] = 0; /* Extract features */ fx = fvec_extract(buf, strlen(buf), "test"); /* Add fx to fz */ fy = fvec_add(fz, fx); fvec_destroy(fz); err += fabs(fvec_norm2(fy) - 1.4142135623) > 1e-7; /* Substract fx from fz */ fz = fvec_sub(fy, fx); fvec_sparsify(fz); /* Clean up */ fvec_destroy(fy); fvec_destroy(fx); } fvec_destroy(fz); test_return(err, i); return err; }
/* * A simple static test for the dot-product of feature vectors */ int test_static_dot() { int i, err = 0; fvec_t *fx, *fy; test_printf("Dot product of feature vectors"); for (i = 0; test_dot[i].x; i++) { /* Extract features */ fx = fvec_extract(test_dot[i].x, strlen(test_dot[i].x), "test"); fy = fvec_extract(test_dot[i].y, strlen(test_dot[i].y), "test"); /* Compute dot product */ double d = fvec_dot(fx, fy); err += fabs(d - test_dot[i].res) > 1e-6; fvec_destroy(fx); fvec_destroy(fy); } test_return(err, i); return err; }
/* * A simple stress test for clustering */ int test_stress() { int i, j, k, err = 0; fvec_t *f; farray_t *fa; char buf[STR_LENGTH + 1], label[32]; test_printf("Stress test for clustering"); for (i = 0; i < STRESS_RUNS; i++) { /* Create array */ fa = farray_create("test"); for (j = 0; j < NUM_VECTORS; j++) { for (k = 0; k < STR_LENGTH; k++) buf[k] = rand() % 10 + '0'; buf[k] = 0; /* Extract features */ f = fvec_extract(buf, strlen(buf), "test"); snprintf(label, 32, "label%.2d", rand() % 10); /* Add to array */ farray_add(fa, f, label); } /* Extract prototypes */ cluster_t *c = cluster_linkage(fa, 0); /* Destroy features */ cluster_destroy(c); farray_destroy(fa); } test_return(err, STRESS_RUNS); return err; }
/* * A stres test for the addition of feature vectors */ int test_stress_dot() { int i, j, err = 0; fvec_t *fx, *fy; char buf[STR_LENGTH + 1]; test_printf("Stress test for dot product of feature vectors"); /* Create empty vector */ for (i = 0; i < NUM_VECTORS; i++) { /* Create random key and string */ for (j = 0; j < STR_LENGTH; j++) buf[j] = rand() % 10 + '0'; buf[j] = 0; fx = fvec_extract(buf, strlen(buf), "test"); /* Create random key and string */ for (j = 0; j < STR_LENGTH; j++) buf[j] = rand() % 10 + '0'; buf[j] = 0; fy = fvec_extract(buf, strlen(buf), "test"); double nx = fvec_dot(fx, fx); double ny = fvec_dot(fy, fy); err += fabs(fvec_norm2(fx) - sqrt(nx)) > 1e-7; err += fabs(fvec_norm2(fy) - sqrt(ny)) > 1e-7; err += fabs(fvec_dot(fx, fy) > nx + ny); /* Clean up */ fvec_destroy(fx); fvec_destroy(fy); } test_return(err, 3 * i); return err; }
/* * A simple static test for the addition of feature vectors */ int test_static_add() { int i, err = 0; fvec_t *fx, *fy, *fz; test_printf("Addition of feature vectors"); for (i = 0; test_add[i].x; i++) { /* Extract features */ fx = fvec_extract(test_add[i].x, strlen(test_add[i].x), "test"); fy = fvec_extract(test_add[i].y, strlen(test_add[i].y), "test"); /* Add test vectors */ fz = fvec_add(fx, fy); err += fabs(fvec_norm1(fz) - test_add[i].res) > 1e-7; fvec_destroy(fz); fvec_destroy(fx); fvec_destroy(fy); } test_return(err, i); return err; }
int main(int argc, char **argv) { struct timeval stv, etv; struct bundle bundle, *rbundle; ctx_state *rstate; ctx_stack stk; stk.size = 1; assert(ctx_new(&stk) == 0); gettimeofday(&stv, NULL); for (j = 0; j < ITERATIONS; ) test_return(NULL); gettimeofday(&etv, NULL); printf(RFMT, "return", TIME(stv, etv)); gettimeofday(&stv, NULL); for (j = 0; ctx_mark(&bundle.state, NULL, &rstate) < ITERATIONS; ) { if (j == 0) ctx_call(&bundle.state, stk, test_noext); else ctx_jump(rstate, NULL, &bundle.state); } gettimeofday(&etv, NULL); printf(RFMT, "noexts", TIME(stv, etv)); gettimeofday(&stv, NULL); for (j = 0; j < ITERATIONS; ) { if (ctx_mark(&bundle.state, NULL, &rstate) == 0) { if (j == 0) ctx_call(&bundle.state, stk, test_noext); else ctx_jump(rstate, NULL, &bundle.state); } } gettimeofday(&etv, NULL); printf(RFMT, "noextd", TIME(stv, etv)); gettimeofday(&stv, NULL); for (j = 0; ctx_mark(&bundle.state, &bundle.extra, &rbundle) < ITERATIONS; ) { if (j == 0) ctx_call(&bundle, stk, test_ext); else ctx_jump(&rbundle->state, &rbundle->extra, &bundle); } gettimeofday(&etv, NULL); printf(RFMT, "exts", TIME(stv, etv)); gettimeofday(&stv, NULL); for (j = 0; j < ITERATIONS; ) { if (ctx_mark(&bundle.state, &bundle.extra, &rbundle) == 0) { if (j == 0) ctx_call(&bundle, stk, test_ext); else ctx_jump(&rbundle->state, &rbundle->extra, &bundle); } } gettimeofday(&etv, NULL); printf(RFMT, "extd", TIME(stv, etv)); ctx_free(stk); return 0; }