BOOL init_randfile() { FILE *rf; char *randfile; char *homedir; char tmp[256]; /* See if the environmental variable RANDFILE is defined * and the file exists */ randfile = getenv("RANDFILE"); if (randfile != NULL) { rf = fopen(randfile, "rb"); if (rf != NULL) { fclose(rf); return (TRUE); } else { /* The environmental variable exists but not the file */ return (create_random_file(randfile)); } } /* * If the RANDFILE environmental variable does not exist, * see if the HOME enviromental variable exists and * a .rnd file is in there. */ homedir = getenv("HOME"); if (homedir != NULL && (strlen(homedir) + 5 /* \.rnd */) < sizeof(tmp)) { snprintf(tmp, sizeof(tmp), "%s\\.rnd", homedir); rf = fopen(tmp, "rb"); if (rf != NULL) { fclose(rf); return (TRUE); } else { /* The HOME environmental variable exists but not the file */ return (create_random_file(tmp)); } } /* * Final try. Look for it on the C:\ directory * NOTE: This is a really bad place for it security-wise * However, OpenSSL looks for it there if it can't find it elsewhere */ rf = fopen("C:\\.rnd", "rb"); if (rf != NULL) { fclose(rf); return (TRUE); } /* The file does not exist */ return (create_random_file("C:\\.rnd")); }
static void test_create_random_file(void) { struct output_writter *output = get_console_writter(); TEST_ASSERT_NOT_NULL(output); struct memory_allocator *allocator = get_default_allocator(); TEST_ASSERT_NOT_NULL(allocator); struct random_generator *random = get_default_random_generator(); TEST_ASSERT_NOT_NULL(random); char *path auto_free = NULL; uint64_t size = 0; int32_t rtrn = create_random_file("/tmp", ".txt", &path, &size); TEST_ASSERT(rtrn == 0); TEST_ASSERT_NOT_NULL(path); TEST_ASSERT(size > 0); struct stat sb; rtrn = stat(path, &sb); TEST_ASSERT(rtrn == 0); TEST_ASSERT(sb.st_mode & S_IFREG); return; }
static void test_get_extension(void) { uint32_t i; int32_t rtrn = 0; char *path = NULL; uint64_t size = 0; char *extension = NULL; for(i = 0; i < number_of_extensions; i++) { /* Create a random file. */ rtrn = create_random_file("/tmp", ext[i], &path, &size); TEST_ASSERT(rtrn == 0); TEST_ASSERT(path != NULL); /* Check the extension of the file against the extension it should be. */ rtrn = get_extension(path, &extension); TEST_ASSERT(rtrn == 0); TEST_ASSERT(extension != NULL); TEST_ASSERT(strncmp(extension, ext[i], size_array[i]) == 0); // mem_free((void **)&path); } return; }
static void test_get_file_size(void) { uint32_t i; int32_t fd = 0; int32_t rtrn = 0; char *path = NULL; uint64_t size = 0; uint64_t file_size = 0; for(i = 0; i < 100; i++) { rtrn = create_random_file("/tmp", ".txt", &path, &size); TEST_ASSERT(rtrn == 0); TEST_ASSERT_NOT_NULL(path); fd = open(path, O_RDONLY); TEST_ASSERT(fd > -1); rtrn = get_file_size(fd, &file_size); TEST_ASSERT(rtrn == 0); TEST_ASSERT(size == file_size); /* Clean up. */ unlink(path); } return; }
int main(int argc, char *argv[]){ printf("\n"); if (argc < 2) { printf("Worng number of arguments.\nYou may execute with problem size.\n\n./riemann problem_size\n\nYou can also execute with data file\n\n./riemann problem_size filename\n\n"); return -1; } int problem_size = atoi(argv[1]); char *filename = "data.dat"; if (argc == 2) create_random_file(filename, problem_size); else filename = argv[2]; double * data = malloc(sizeof(double) * problem_size * 2); read_data_from_file(filename, problem_size, data); //printf("Problem size: %d\n", problem_size); //print_data(data, problem_size); double result; clock_t time = clock (); result = left_riemann(data, problem_size); printf("Left Riemann: %f\n", result); result = right_riemann(data, problem_size); printf("Right Riemann: %f\n", result); result = trapezoidal_riemann(data, problem_size); printf("Trapezoidal Riemann: %f\n", result); time = clock() - time; printf("\nTime spent: %f seconds\n\n", ((double) time / CLOCKS_PER_SEC)); return 0; }
int hcez_mcellqry_simpletest_exec(test_case_t *tc) { char **argv = NULL; char *test_vip = NULL; hcerr_t expected_res, res; hc_test_result_t test_res; int port; int32_t response_code; hc_system_record_t sys_rec, sys_rec2; hc_random_file_t *stored_r_file = NULL; hctest_env_t *test_env; hc_nvr_t *nvr = NULL; hc_session_t *session; hc_oid oid; hc_value_t value = HC_EMPTY_VALUE_INIT; hc_long_t long_return; int i, j, nPerFetch, nStored, nGotten; time_t t, t1, t2; hc_hashlist_t *stored = NULL; char query[1024]; hc_query_result_set_t *rset = NULL; int finished; argv = tc->argv; expected_res = atoi(argv[0]); test_res = TEST_PASS; test_env = tc->test_env; if (test_env->n_cells == 0) { printf("Multicell cells not defined: SKIPPING\n"); return TEST_PASS; } test_vip = test_env->data_vip; port = test_env->data_vip_port; hc_init(malloc,free,realloc); res = hc_session_create_ez(test_vip,port,&session); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Session Create failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } res = hc_nvr_create(session, 4, &nvr); // Initialize test metadata value time(&t); res = hc_nvr_add_long(nvr, LONG_NAME, t); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_nvr_add_long failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } res = hc_nvr_add_string(nvr, STRING_NAME, "hcezMulticellQuery.c all"); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_nvr_add_string failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } sprintf(query, "%s=%d", LONG_NAME, t); // do stores printf("storing %d to each cell\n", MAX_PER_CELL); stored = create_hashlist(1024); nStored = 0; for (i=0; i<test_env->n_cells; i++) { for (j=0; j<MAX_PER_CELL; j++) { stored_r_file = create_random_file(4); res = hc_store_both_cell_ez(session, test_env->cell_ids[i], read_from_random_data_generator, stored_r_file, nvr, &sys_rec); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Store of file to cell %d failed: %s\n", test_env->cell_ids[i], hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } hash_put(sys_rec.oid, "noval", stored); if (j == 0) printf("first in cell %d (hex %x): %s\n", test_env->cell_ids[i], test_env->cell_ids[i], sys_rec.oid); free_r_file(stored_r_file); stored_r_file = NULL; nStored++; } } printf("Total stored %d\n", nStored); // do queries printf("Query: [%s]\n", query); for (nPerFetch=1; nPerFetch<MAX_PER_CELL+2; nPerFetch++) { hc_hashlist_t *gotten = create_hashlist(1024); printf("Fetch size = %d\n", nPerFetch); nGotten = 0; res = hc_query_ez(session, query, NULL, 0, nPerFetch, &rset); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Query failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; goto done; } finished = 0; while (1) { hc_oid return_oid; res = hc_qrs_next_ez(rset, &return_oid, NULL, &finished); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_qrs_next_ez failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; goto done; } if (finished) break; if (!hashlist_contains_key(return_oid, stored)) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Unexpected oid returned [%s]\n", return_oid); test_res = TEST_FAIL; goto done; } //printf("got %s\n", return_oid); if (hashlist_contains_key(return_oid, gotten)) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Duplicate oid returned (oid %d) [%s]\n", nGotten, return_oid); test_res = TEST_FAIL; } hash_put(return_oid, "noval", gotten); nGotten++; } res = hc_qrs_free(rset); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_qrs_free failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; } free_hashlist(gotten); if (nGotten != nStored) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Stored %d but got %d\n", nStored, nGotten); test_res = TEST_FAIL; } if (test_res != TEST_PASS) goto done; } free_hashlist(stored); stored = NULL; // Initialize test metadata value time(&t); res = hc_nvr_add_long(nvr, LONG_NAME, t); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_nvr_add_long failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } res = hc_nvr_add_string(nvr, STRING_NAME, "hcezMulticellQuery.c odd"); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_nvr_add_string failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } sprintf(query, "%s=%d", LONG_NAME, t); // do stores printf("storing %d to odd cells\n", MAX_PER_CELL); stored = create_hashlist(1024); nStored = 0; for (i=0; i<test_env->n_cells; i++) { if (i % 2 == 0) continue; for (j=0; j<MAX_PER_CELL; j++) { stored_r_file = create_random_file(4); res = hc_store_both_cell_ez(session, test_env->cell_ids[i], read_from_random_data_generator, stored_r_file, nvr, &sys_rec); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Store of file to cell %d failed: %s\n", test_env->cell_ids[i], hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } hash_put(sys_rec.oid, "noval", stored); if (j == 0) printf("first in cell %d (hex %x): %s\n", test_env->cell_ids[i], test_env->cell_ids[i], sys_rec.oid); free_r_file(stored_r_file); stored_r_file = NULL; nStored++; } } printf("Total stored %d\n", nStored); // do queries printf("Query: [%s]\n", query); for (nPerFetch=1; nPerFetch<MAX_PER_CELL+2; nPerFetch++) { hc_hashlist_t *gotten = create_hashlist(1024); printf("Fetch size = %d\n", nPerFetch); nGotten = 0; res = hc_query_ez(session, query, NULL, 0, nPerFetch, &rset); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Query failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; goto done; } finished = 0; while (1) { hc_oid return_oid; res = hc_qrs_next_ez(rset, &return_oid, NULL, &finished); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_qrs_next_ez failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; goto done; } if (finished) break; if (!hashlist_contains_key(return_oid, stored)) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Unexpected oid returned [%s]\n", return_oid); test_res = TEST_FAIL; goto done; } //printf("got %s\n", return_oid); if (hashlist_contains_key(return_oid, gotten)) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Duplicate oid returned (oid %d) [%s]\n", nGotten, return_oid); test_res = TEST_FAIL; } hash_put(return_oid, "noval", gotten); nGotten++; } free_hashlist(gotten); res = hc_qrs_free(rset); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_qrs_free failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; } if (nGotten != nStored) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Stored %d but got %d\n", nStored, nGotten); test_res = TEST_FAIL; } if (test_res != TEST_PASS) goto done; } // Initialize test metadata value time(&t); res = hc_nvr_add_long(nvr, LONG_NAME, t); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_nvr_add_long failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } res = hc_nvr_add_string(nvr, STRING_NAME, "hcezMulticellQuery.c even"); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_nvr_add_string failed. Returned code of %s\n", hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } sprintf(query, "%s=%d", LONG_NAME, t); // do stores printf("storing %d to even cells\n", MAX_PER_CELL); stored = create_hashlist(1024); nStored = 0; for (i=0; i<test_env->n_cells; i++) { if (i % 2 != 0) continue; for (j=0; j<MAX_PER_CELL; j++) { stored_r_file = create_random_file(4); res = hc_store_both_cell_ez(session, test_env->cell_ids[i], read_from_random_data_generator, stored_r_file, nvr, &sys_rec); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Store of file to cell %d failed: %s\n", test_env->cell_ids[i], hc_decode_hcerr(res)); test_res = TEST_ERROR; goto done; } hash_put(sys_rec.oid, "noval", stored); if (j == 0) printf("first in cell %d (hex %x): %s\n", test_env->cell_ids[i], test_env->cell_ids[i], sys_rec.oid); free_r_file(stored_r_file); stored_r_file = NULL; nStored++; } } printf("Total stored %d\n", nStored); // do queries printf("Query: [%s]\n", query); for (nPerFetch=1; nPerFetch<MAX_PER_CELL+2; nPerFetch++) { hc_hashlist_t *gotten = create_hashlist(1024); printf("Fetch size = %d\n", nPerFetch); nGotten = 0; res = hc_query_ez(session, query, NULL, 0, nPerFetch, &rset); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Query failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; goto done; } finished = 0; while (1) { hc_oid return_oid; res = hc_qrs_next_ez(rset, &return_oid, NULL, &finished); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_qrs_next_ez failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; goto done; } if (finished) break; if (!hashlist_contains_key(return_oid, stored)) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Unexpected oid returned [%s]\n", return_oid); test_res = TEST_FAIL; goto done; } //printf("got %s\n", return_oid); if (hashlist_contains_key(return_oid, gotten)) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Duplicate oid returned (oid %d) [%s]\n", nGotten, return_oid); test_res = TEST_FAIL; } hash_put(return_oid, "noval", gotten); nGotten++; } res = hc_qrs_free(rset); if (res != HCERR_OK) { hc_test_log(LOG_ERROR_LEVEL, test_env, "hc_qrs_free failed: %s\n", hc_decode_hcerr(res)); test_res = TEST_FAIL; } free_hashlist(gotten); if (nGotten != nStored) { hc_test_log(LOG_ERROR_LEVEL, test_env, "Stored %d but got %d\n", nStored, nGotten); test_res = TEST_FAIL; } if (test_res != TEST_PASS) goto done; } done: if (nvr != NULL) hc_nvr_free(nvr); if (stored_r_file != NULL) free_r_file(stored_r_file); free_hashlist(stored); hc_session_free(session); return test_res; }
static void test_random_files( CuTest* tc ) { // Setup the test segment char* segment = "seq_file"; int chunk = 1024, i = 0; // internal buffer size used by the stream lcn_directory_t *dir; char *data[] = { ".zero", ".one", ".ten", ".hundred", ".big1", ".big2", ".big3", ".big4", ".big5", ".big6", ".big7" }; int data_size = 11; lcn_compound_file_writer_t *cfw; lcn_compound_file_reader_t *cfr; apr_pool_t *pool; apr_pool_create( &pool, NULL ); LCN_TEST( lcn_fs_directory_create( &dir, dir_test, LCN_TRUE, pool ) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".zero", NULL), 0, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".one", NULL), 1, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".ten", NULL), 10, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".hundred", NULL), 100, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big1", NULL), chunk, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big2", NULL), chunk - 1, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big3", NULL), chunk + 1, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big4", NULL), 3 * chunk, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big5", NULL), 3 * chunk - 1, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big6", NULL), 3 * chunk + 1, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".big7", NULL), 1000 * chunk, pool) ); // Setup extraneous files LCN_TEST( create_random_file(tc, dir, "onetwothree", 100, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".notIn", NULL), 50, pool) ); LCN_TEST( create_random_file(tc, dir, apr_pstrcat(pool, segment, ".notIn2", NULL), 51, pool) ); LCN_TEST( lcn_compound_file_writer_create ( &cfw, dir, cf_name, pool ) ); for (i = 0; i < data_size; i++) { lcn_compound_file_writer_add_file( cfw, apr_pstrcat(pool, segment, data[i], NULL ) ); } lcn_compound_file_writer_close(cfw); LCN_TEST( lcn_compound_file_reader_create ( &cfr, dir, cf_name, pool ) ); for (i = 0; i < data_size; i++) { lcn_index_input_t *expected = NULL; lcn_index_input_t *actual = NULL; LCN_TEST( lcn_directory_open_input( dir, &expected, apr_pstrcat(pool, segment, data[i], NULL ), LCN_IO_CONTEXT_READONCE, pool ) ); LCN_TEST( lcn_compound_file_reader_open_input( cfr, &actual, apr_pstrcat(pool, segment, data[i], NULL ) ) ); assert_same_stream ( tc, expected, actual); assert_same_seek_behavior ( tc, expected, actual ); LCN_TEST ( lcn_index_input_close( expected ) ); LCN_TEST ( lcn_index_input_close( actual ) ); } LCN_TEST ( lcn_compound_file_reader_close ( cfr ) ); delete_files( tc, dir_test ); }
static void test_delete_directory(void) { int32_t rtrn = 0; char *dir auto_free = NULL; char *name auto_free = NULL; rtrn = generate_directory_name(&name); TEST_ASSERT(rtrn == 0); rtrn = asprintf(&dir, "/tmp/%s\n", name); TEST_ASSERT(rtrn > -1); /* Let's make a directory in /tmp for testing delete_directory(). */ rtrn = mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); TEST_ASSERT(rtrn == 0); uint32_t total_files = 25; uint32_t i; /* Loop and create a bunch of random text files. */ for(i = 0; i < total_files; i++) { uint64_t size = 0; char *path auto_free = NULL; /* Create random file. */ rtrn = create_random_file(dir, ".txt", &path, &size); TEST_ASSERT(rtrn == 0); } /* Now that were done setting up a test directory let's start testing delete_directory(). Pass a NULL path and make sure delete_directory returns an error. */ char *dir_path = NULL; rtrn = delete_directory(dir_path); TEST_ASSERT(rtrn < 0); char *file_path auto_free = NULL; uint64_t size = 0; /* Create a file path. */ rtrn = create_random_file("/tmp", ".txt", &file_path, &size); TEST_ASSERT(rtrn == 0); /* If we pass delete_directory() a file path then it should fail. */ rtrn = delete_directory(file_path); TEST_ASSERT(rtrn < 0); /* Create a directory path that does not exist. */ char *random_path auto_free = NULL; rtrn = generate_directory_name(&random_path); TEST_ASSERT(rtrn == 0); rtrn = delete_directory(random_path); TEST_ASSERT(rtrn < 0); /* Now let's try deleting the directory we made. */ rtrn = delete_directory(dir); TEST_ASSERT(rtrn == 0); struct stat buf; /* Stat should fail on the directory. */ TEST_ASSERT(stat(dir, &buf) < 0); return; }