void do_stop_idle(int s, struct sockaddr *slave, int calibrate) { struct nc_request req; struct nc_reply reply; long long res_idle_count; long long adj_count; int idle, res_idle_ticks; req.type = htonl(NC_REQUEST_STOP_IDLE); nc_message(s, &req, &reply, slave); if (reply.response == ntohl(NC_REPLY_ACK)) { res_idle_ticks = ntohl(reply.misc.idle_results.elapsed_time); res_idle_count = ((long long)ntohl(reply.misc.idle_results.count[0]) << 32) | ntohl(reply.misc.idle_results.count[1]); test_printf("IDLE - ticks: %d, count: %ld", res_idle_ticks, res_idle_count); if (calibrate) { idle_count = res_idle_count; idle_ticks = res_idle_ticks; } else { adj_count = res_idle_count / res_idle_ticks; adj_count *= idle_ticks; idle = (int) ((adj_count * 100) / idle_count); test_printf(", %d%% idle", idle); } test_printf("\n"); } else { test_printf("Slave failed on IDLE\n"); } }
int basic_test(int protocol_type) { test_printf("Test: Starting %s test.\n", protocol_type == msg_udp ? "udp" : "tcp"); int status; pid_t child_pid = fork(); if (child_pid == -1) return test_failure; if (child_pid == 0) { // Child process. exit(server(protocol_type)); // TODO Test memory cleanliness (use net_allocs_for_class). } else { // Parent process. test_printf("Client pid=%d server pid=%d\n", getpid(), child_pid); test_printf("Client: starting up.\n"); // tmp int client_failed = client(protocol_type, child_pid); int server_status; wait(&server_status); int server_failed = WEXITSTATUS(server_status); // Help check for memory leaks. test_that(net_allocs_for_class(0) == 0); // TODO Add deeper memory-leak checks via memprofile. test_printf("Test: client_failed=%d server_failed=%d.\n", client_failed, server_failed); return client_failed || server_failed; } }
int nc_message(int s, struct nc_request *req, struct nc_reply *reply, struct sockaddr *slave) { fd_set fds; struct timeval timeout; req->seq = htonl(test_seq); if (sendto(s, req, sizeof(*req), 0, slave, sa_len(slave)) < 0) { perror("sendto"); return false; } FD_ZERO(&fds); FD_SET(s, &fds); timeout.tv_sec = NC_REPLY_TIMEOUT; timeout.tv_usec = 0; if (select(s+1, &fds, 0, 0, &timeout) <= 0) { test_printf("No response to command\n"); return false; } if (recvfrom(s, reply, sizeof(*reply), 0, 0, 0) < 0) { perror("recvfrom"); return false; } if (reply->seq != req->seq) { test_printf("Response out of order - sent: %d, recvd: %d\n", ntohl(req->seq), ntohl(reply->seq)); return false; } return true; }
static void thread_hello(void *arg) { int old_rank, cur_rank; ABT_thread self; ABT_thread_id id; char *msg; ABT_xstream_self_rank(&cur_rank); ABT_thread_self(&self); ABT_thread_get_id(self, &id); ABT_thread_release(self); test_printf("[U%lu:E%d] Hello, world!\n", id, cur_rank); ABT_thread_yield(); old_rank = cur_rank; ABT_xstream_self_rank(&cur_rank); msg = (cur_rank == old_rank) ? "" : " (stolen)"; test_printf("[U%lu:E%d] Hello again #1.%s\n", id, cur_rank, msg); ABT_thread_yield(); old_rank = cur_rank; ABT_xstream_self_rank(&cur_rank); msg = (cur_rank == old_rank) ? "" : " (stolen)"; test_printf("[U%lu:E%d] Hello again #2.%s\n", id, cur_rank, msg); ABT_thread_yield(); old_rank = cur_rank; ABT_xstream_self_rank(&cur_rank); msg = (cur_rank == old_rank) ? "" : " (stolen)"; test_printf("[U%lu:E%d] Goodbye, world!%s\n", id, cur_rank, msg); }
void show_test_results(struct nc_test_results *results) { if ((ntohl(results->key1) == NC_TEST_RESULT_KEY1) && (ntohl(results->key2) == NC_TEST_RESULT_KEY2) && (ntohl(results->seq) == test_seq)) { test_printf(" slave sent %d, recvd %d\n", ntohl(results->nsent), ntohl(results->nrecvd)); } else { test_printf(" ... invalid results - keys: %x/%x, seq: %d/%d\n", ntohl(results->key1), ntohl(results->key2), ntohl(results->seq), test_seq); } }
int main() { pthread_t thread; for (int i = 0; i < 1000; i++) { if (pthread_create(&thread, NULL, fn, NULL)) { test_printf("failed: can't create new posix thread.\n"); TEST_EXIT(1); } pthread_detach(thread); sched_yield(); } test_printf("\n"); TEST_EXIT(0); }
/* * 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; }
static int tls_create_trust_from_certs(const SSLCertificate *cert, SecTrustRef *trustRef) { int err; CFMutableArrayRef certArray = NULL; CFDataRef certData = NULL; SecCertificateRef cfCert = NULL; if(cert==NULL) { test_printf("No certs, do not create SecTrustRef\n"); *trustRef = NULL; return 0; } certArray = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); while(cert) { base64_dump(cert->derCert, "CERTIFICATE"); require_action((certData = CFDataCreate(kCFAllocatorDefault, cert->derCert.data, cert->derCert.length)), out, err = errSecAllocate); require_action((cfCert = SecCertificateCreateWithData(kCFAllocatorDefault, certData)), out, err = errSecAllocate); CFArrayAppendValue(certArray, cfCert); CFReleaseNull(cfCert); CFReleaseNull(certData); cert=cert->next; } require_noerr((err=SecTrustCreateWithCertificates(certArray, NULL, trustRef)), out); out: CFReleaseSafe(certData); CFReleaseSafe(cfCert); CFReleaseSafe(certArray); return err; }
/* * 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; }
/* * 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; }
/* * 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 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; }
/** * 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; }
void net_test(test_param_t param) { // int i; if (param == 0) { test_printf("Start Network Characterization - SLAVE\n"); #ifdef __ECOS init_all_network_interfaces(); calibrate_load(DESIRED_BACKGROUND_LOAD); #if 0 // I can see what this is trying to do, but I get "bind: Address already in // use" errors from the 2nd interface - and the parameter is not used // anyway, so one thread does quite well enough (but only tests one i/f at // once). // Comment in the 'int i' above too. for (i = 1; i < CYGHWR_NET_DRIVERS; i++) { cyg_thread_resume(main_thread_handle[i]); // Start other threads } #endif #endif } nc_slave(param); #ifdef CYGDBG_NET_TIMING_STATS show_net_times(); #endif cyg_test_exit(); }
/* * 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; }
static void base64_dump(tls_buffer data, const char *header) { char line[65]; uint32_t c; size_t n, m; test_printf("-----BEGIN %s-----\n", header); m = n = 0; while (n < data.length) { c = data.data[n]; n++; c = c << 8; if (n < data.length) c |= data.data[n]; n++; c = c << 8; if (n < data.length) c |= data.data[n]; n++; line[m++] = base64_chars[(c & 0x00fc0000) >> 18]; line[m++] = base64_chars[(c & 0x0003f000) >> 12]; if (n > data.length + 1) line[m++] = '='; else line[m++] = base64_chars[(c & 0x00000fc0) >> 6]; if (n > data.length) line[m++] = '='; else line[m++] = base64_chars[(c & 0x0000003f) >> 0]; if (m == sizeof(line) - 1) { line[sizeof(line) - 1] = '\0'; test_printf("%s\n", line); m = 0; } assert(m < sizeof(line) - 1); } if (m) { line[m] = '\0'; test_printf("%s\n", line); } test_printf("-----END %s-----\n", header); }
int main() { for (int i = 1; i < 4; i++) { test_printf("%d...\n", i); msleep(999); } TEST_EXIT(0); }
bool test_vcbprintf() { test_printf("%+08i\n", 1337); int x; test_printf("%llp\n", &x); std::cout << std::hex << &x << std::endl; test_printf("Characters: %c %c \n", 'a', 65); test_printf("Decimals: %d %ld\n", 1977, 650000L); test_printf("Preceding with blanks: %10d \n", 1977); test_printf("Preceding with zeros: %010d \n", 1977); test_printf("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100); test_printf("floats: %4.2f %+.0e %E %a \n", 3.1416, 3.1416, 3.1416, -5.42); test_printf("Width trick: %*d \n", 5, 10); test_printf("%s \n", "A string"); return true; }
/* * 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; }
boolean test_all(unsigned verbose, FILE *fp) { boolean success = TRUE; test_printf(verbose, fp, NULL); return success; }
void net_test(test_param_t p) { test_printf("Start Network Characterization - MASTER\n"); #ifdef __ECOS init_all_network_interfaces(); #endif nc_master((struct test_params *)p); cyg_test_exit(); }
void test_log(const char *name) { int result; if (!run_tests) return; result = open(name, O_RDONLY); if (result != -1) close(result); test_printf("open(\"%s\", O_RDONLY) = %d\n", name, result); }
void client_update(msg_Conn *conn, msg_Event event, msg_Data data) { // We expect to hear events in this order: int expected_events[] = { msg_connection_ready, msg_connection_closed}; Context *ctx = (Context *)conn->conn_context; test_printf("Client: Received event %s\n", event_names[event]); if (event == msg_error) { char *err_str = msg_as_str(data); test_printf("Client: Error: %s\n", err_str); if (strcmp(err_str, "connect: Connection refused") == 0) { if (ctx->num_tries < max_tries) { test_printf("Client: Will wait briefly and try again at address %s.\n", ctx->address); sleep(5); ctx->num_tries++; msg_connect(ctx->address, client_update, ctx); return; // Don't count this as a server event. } else { test_printf("Client: max_tries reached; giving up connecting (at %s).\n", ctx->address); } } } if (event == msg_error) test_printf("Client: Error: %s\n", msg_as_str(data)); test_that(client_event_num < array_size(expected_events)); test_that(event == expected_events[client_event_num]); if (event == msg_connection_ready) { msg_Data data = msg_new_data("why hello"); msg_send(conn, data); msg_send(conn, data); msg_send(conn, data); msg_delete_data(data); client_done = true; } client_event_num++; }
int main(void) { test_printf(); test_output(); test_space(); test_no_placeholder(); test_default_out(); test_custom_type(); test_follower(); return 0; }
main() { # ifdef THINK_C printf("cordtest:\n"); # endif test_basics(); test_extras(); test_printf(); CORD_fprintf(stderr, "SUCCEEDED\n"); return(0); }
int forktest(void) { pid_t myid; /* 16 forks */ for (int i = 0; i < 3; i++) { test_printf("%d: Forking...\n", getpid()); if (fork() < 0) goto out_err; } myid = getpid(); if (global != 0) { test_printf("Global not zero.\n"); test_printf("-- FAILED --\n"); goto out_err; } global += myid; if (global != myid) goto out_err; if (getpid() != parent_of_all) { /* Exit here to exit successful children */ //_exit(0); //BUG(); } if (getpid() == parent_of_all) printf("FORK TEST -- PASSED --\n"); return 0; /* Any erroneous child or parent comes here */ out_err: printf("FORK TEST -- FAILED --\n"); return 0; }
int main(void) { # ifdef THINK_C printf("cordtest:\n"); # endif GC_INIT(); test_basics(); test_extras(); test_printf(); CORD_fprintf(stdout, "SUCCEEDED\n"); return(0); }
void show_results(const char *msg, struct timeval *start, struct timeval *end, int nbufs, int buflen, int lost, int seq_errors) { struct timeval tot_time; #ifndef __ECOS double real_time, thru; long tot_bytes = nbufs * buflen; #endif timersub(end, start, &tot_time); test_printf("%s - %d bufs of %d bytes in %d.%02d seconds", msg, nbufs, buflen, tot_time.tv_sec, tot_time.tv_usec / 10000); #ifndef __ECOS real_time = tot_time.tv_sec + ((tot_time.tv_usec / 10000) * .01); // Compute bytes / second (rounded up) thru = tot_bytes / real_time; // Convert to Mb / second test_printf(" - %.2f KB/S", thru / 1024.0); test_printf(" - %.4f Mbit/S (M = 10^6)", thru * 8.0 / 1000000.0); #endif if (lost) { test_printf(", %d lost", lost); } if (seq_errors) { test_printf(", %d out of sequence", seq_errors); } test_printf("\n"); }
static void print_trace() { // Windows doesn't give us easy access to stack traces. For more debug power, // turn off the seg fault handler and run the test with visual // studio's debug tools. Another option would be to integrate this class: // http://stackwalker.codeplex.com/ #ifndef _WIN32 void *array[10]; size_t size = backtrace(array, 10); char **strings = backtrace_symbols(array, size); for (size_t i = 0; i < size; ++i) test_printf("%s\n", strings[i]); free(strings); #endif }
void server_update(msg_Conn *conn, msg_Event event, msg_Data data) { // We expect to hear events in this order. int expected_events[] = { msg_listening, msg_connection_ready, msg_message, msg_message, msg_message, msg_connection_closed}; test_printf("Server: Received event %s\n", event_names[event]); if (event == msg_error) { char *err_str = msg_as_str(data); test_printf("Server: Error: %s\n", err_str); if (strcmp(err_str, "bind: Address already in use") == 0) { if (server_ctx.num_tries < max_tries) { test_printf("Will wait briefly and try again at address %s.\n", server_ctx.address); sleep(5); server_ctx.num_tries++; msg_listen(server_ctx.address, server_update); return; // Don't count this as a server event. } else { test_printf("Server: max_tries reached; giving up listening (at %s).\n", server_ctx.address); } } } test_that(server_event_num < array_size(expected_events)); test_that(event == expected_events[server_event_num]); if (event == msg_message) { char *str = msg_as_str(data); test_str_eq(str, "why hello"); server_done = true; num_msg_recd++; } server_event_num++; }