Ensure(find_first_mode_greater_than_or_equal_to, returns_correct_results_for_an_ordered_array) { create_an_ordered_array(); int i, index, max_value = ARRAY_LENGTH / NUM_OF_OCCURANCE; double value; value = modes_array[0].modulus; index = find_first_mode_greater_than_or_equal_to(value, modes_array, ARRAY_LENGTH); assert_that(index, is_equal_to(0)); for (i = NUM_OF_OCCURANCE; i < max_value; i += max_value / 20) { value = i / NUM_OF_OCCURANCE + .5; index = find_first_mode_greater_than_or_equal_to(value, modes_array, ARRAY_LENGTH); assert_true(modes_array[index + 1].modulus > value); assert_true(modes_array[index].modulus >= value); assert_true(modes_array[index - 1].modulus < value); } value = modes_array[ARRAY_LENGTH - 1].modulus + 1; index = find_first_mode_greater_than_or_equal_to(value, modes_array, ARRAY_LENGTH); assert_that(index, is_equal_to(NOT_FOUND)); }
Ensure(ByteStuffer, send_one_byte_frame) { uint8_t data[] = {5}; byte_stuffer_send_frame(1, data, 1); uint8_t expected[] = {2, 5, 0}; assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); }
Ensure(ByteStuffer, sends_two_byte_frame) { uint8_t data[] = {5, 0x77}; byte_stuffer_send_frame(0, data, 2); uint8_t expected[] = {3, 5, 0x77, 0}; assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); }
Ensure(find_first_mode_greater_than_or_equal_to, returns_right_results_in_a_random_array) { fill_array_with_random_numbers(); int i, index, start_point = ARRAY_LENGTH; for (i = 1; i < ARRAY_LENGTH; i++) { if (modes_array[i - 1].modulus != modes_array[i].modulus) { start_point = i - 1; break; } } double value; value = modes_array[0].modulus; index = find_first_mode_greater_than_or_equal_to(value, modes_array, ARRAY_LENGTH); assert_that(index, is_equal_to(start_point)); for (i = start_point; i < ARRAY_LENGTH; i += ARRAY_LENGTH / 20) { value = floor(modes_array[i].modulus); index = find_first_mode_greater_than_or_equal_to(value, modes_array, ARRAY_LENGTH); assert_true(modes_array[index - 1].modulus <= value); assert_true(modes_array[index].modulus > value); assert_true(modes_array[index + 1].modulus > value); } value = modes_array[ARRAY_LENGTH - 1].modulus + 1; index = find_first_mode_greater_than_or_equal_to(value, modes_array, ARRAY_LENGTH); assert_that(index, is_equal_to(NOT_FOUND)); }
Ensure(ByteStuffer, sends_three_byte_frame_data_in_the_middle) { uint8_t data[] = {0, 0x55, 0}; byte_stuffer_send_frame(0, data, 3); uint8_t expected[] = {1, 2, 0x55, 1, 0}; assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); }
Ensure(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) { uint8_t data[] = {9, 0}; byte_stuffer_send_frame(1, data, 2); uint8_t expected[] = {2, 9, 1, 0}; assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); }
Ensure(FFT, transforms_constant_into_delta_function) { Vec v; DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,4,2,1,NULL,&da); DMCreateGlobalVector(da, &v); VecZeroEntries(v); VecSetValue(v, 0, 0.7, INSERT_VALUES); VecAssemblyBegin(v); VecAssemblyEnd(v); scFftCreate(da, &fft); Vec x, y, z; scFftCreateVecsFFTW(fft, &x, &y, &z); scFftTransform(fft, v, 0, y); const PetscScalar *arr; VecGetArrayRead(y, &arr); //assert_that_double(fabs(arr[0] - 0.7), is_less_than_double(1.0e-6)); assert_that(fabs(arr[1] - 0.0) < 1.0e-6, is_true); assert_that(fabs(arr[1] - 0.7) < 1.0e-6, is_true); assert_that(fabs(arr[2] - 0.0) < 1.0e-6, is_true); assert_that(fabs(arr[3] - 0.7) < 1.0e-6, is_true); assert_that(fabs(arr[4] - 0.0) < 1.0e-6, is_true); VecRestoreArrayRead(y, &arr); VecDestroy(&x); VecDestroy(&y); VecDestroy(&z); VecDestroy(&v); scFftDestroy(&fft); DMDestroy(&da); }
Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { uint8_t data[] = {0, 0, 0}; byte_stuffer_send_frame(0, data, 3); uint8_t expected[] = {1, 1, 1, 1, 0}; assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); }
/** * Tries to parse a document containing multiple tags */ static void test_xml_parse_document_1() { SOURCE(source, "" "<Parent>\n" "\t<Child>\n" "\t\tFirst content\n" "\t</Child>\n" "\t<Child>\n" "\t\tSecond content\n" "\t</Child>\n" "</Parent>\n" ); struct xml_document* document = xml_parse_document(source, strlen(source)); assert_that(document, "Could not parse document"); struct xml_node* root = xml_document_root(document); // assert_that(string_equals(xml_node_name(root), "Parent"), "root node name must be `Parent'"); assert_that(2 == xml_node_children(root), "root must have two children"); struct xml_node* first_child = xml_node_child(root, 0); struct xml_node* second_child = xml_node_child(root, 1); assert_that(first_child && second_child, "Failed retrieving the children of root"); struct xml_node* third_child = xml_node_child(root, 2); assert_that(!third_child, "root has a third child where non should be"); // assert_that(string_equals(xml_node_name(first_child), "Child"), "first_child node name must be `Child'"); // assert_that(string_equals(xml_node_content(first_child), "First content"), "first_child node content must be `First content'"); // assert_that(string_equals(xml_node_name(second_child), "Child"), "second_child node name must be `Child'"); // assert_that(string_equals(xml_node_content(second_child), "Second content"), "second_child node content must be `tSecond content'"); xml_document_free(document, true); }
Ensure(string, create_empty) { // Allocate an empty string struct zob_string * p_string = NULL; zob_string_create(&p_string); assert_that(p_string, is_not_equal_to(NULL)); size_t size = 0; zob_string_size_get(p_string, &size); assert_that(size, is_equal_to(0)); zob_string_dispose(&p_string); assert_that(p_string, is_equal_to(NULL)); }
Ensure(eclGetContextInteractively, commandQueueConsistentWithDevice) { size_t size; cl_device_id qDevice; err = eclGetContextInteractively(&ctx); assert_that(err, is_equal_to(CL_SUCCESS)); clGetCommandQueueInfo(ctx.queue, CL_QUEUE_DEVICE, 0, 0, &size); assert_that(err, is_equal_to(CL_SUCCESS)); assert_that(size, is_equal_to(sizeof(qDevice))); clGetCommandQueueInfo(ctx.queue, CL_QUEUE_DEVICE, size, &qDevice, 0); assert_that(qDevice, is_equal_to(ctx.device)); }
Ensure(eclGetContextInteractively, commandQueueConsistentWithContext) { size_t size; cl_context qCtx; err = eclGetContextInteractively(&ctx); assert_that(err, is_equal_to(CL_SUCCESS)); clGetCommandQueueInfo(ctx.queue, CL_QUEUE_CONTEXT, 0, 0, &size); assert_that(err, is_equal_to(CL_SUCCESS)); assert_that(size, is_equal_to(sizeof(qCtx))); clGetCommandQueueInfo(ctx.queue, CL_QUEUE_CONTEXT, size, &qCtx, 0); assert_that(qCtx, is_equal_to(ctx.context)); }
Ensure(generate_logarithmic_bins, sets_bins_boundries_correct) { generate_logarithmic_bins(&bins_vector, indexed_mode_modulus, &conf); bins_struct bin; while (bins_vector.log_length > 0) { vector_pop(&bins_vector, &bin); assert_that(bin.k_min, is_equal_to(pow(jump, 2 * bins_vector.log_length))); assert_that(bin.k, is_equal_to(pow(jump, 2 * bins_vector.log_length + 1))); assert_that(bin.k_max, is_equal_to(pow(jump, 2 * bins_vector.log_length + 2))); } }
Ensure(string, create_and_assign) { // Allocate an empty string and assign data struct zob_string * p_string = NULL; zob_string_create(&p_string); assert_that(p_string, is_not_equal_to(NULL)); char const * p_message = "Alea jacta est"; size_t size = 0; zob_string_write(p_string, size, p_message, &size); assert_that(strlen(p_message), is_equal_to(size)); zob_string_dispose(&p_string); assert_that(p_string, is_equal_to(NULL)); }
Ensure(Words, converts_spaces_to_zeroes) { char *sentence = strdup("Birds of a feather"); split_words(sentence); int comparison = memcmp("Birds\0of\0a\0feather", sentence, strlen(sentence)); assert_that(comparison, is_equal_to(0)); free(sentence); }
Ensure(eclGetContextInteractively, recoversFromBogusDeviceChoice) { eclSetDeviceChoice(mockInteractiveChoiceBogus); eclGetContextInteractively(&ctx); eclSetDeviceChoice(mockInteractiveChoice); err = eclGetContextInteractively(&ctx); assert_that(err, is_equal_to(CL_SUCCESS)); }
Ensure(load_save_matches, loads_matches_from_disk_properly) { vector **primatches = allocate(N_PRI_HALOS, sizeof(vector*)); vector **secmatches = allocate(N_SEC_HALOS, sizeof(vector*)); load_matches(MATCHES_FILE_ADDR, primatches, secmatches); int i, j; match *dummy_match = allocate(1, sizeof(*dummy_match)); for(i = 0; i < N_PRI_HALOS; i++) for(j = 0; j < i; j++){ dummy_match = vector_get(primatches[i], j); assert_that(dummy_match->matchid, is_equal_to(MATCHID)); assert_that_double(dummy_match->goodness, is_equal_to_double(GOODNESS)); } for(i = 0; i < N_PRI_HALOS; i++) free(primatches[i]); for(i = 0; i < N_SEC_HALOS; i++) free(secmatches[i]); free(primatches); free(secmatches); }
Ensure(ByteStuffer, sends_frame_with_254_non_zeroes) { uint8_t data[254]; int i; for(i=0;i<254;i++) { data[i] = i + 1; } byte_stuffer_send_frame(0, data, 254); uint8_t expected[256]; expected[0] = 0xFF; for(i=1;i<255;i++) { expected[i] = i; } expected[255] = 0; assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); }
Ensure(vector_push, pushes_elements_to_vector_properly) { vector *double_v = new_vector(2, sizeof(double)); double dummy = 1.23456789; int i; for(i = 0; i < 4; i++) vector_push(double_v, &dummy); assert_that(double_v->elems, is_non_null); assert_that(double_v->allocLength, is_equal_to(4)); assert_that(double_v->logLength, is_equal_to(4)); dispose_vector(&double_v); }
/** * Tests the xml_open_document functionality */ static void test_xml_parse_document_3() { #define FILE_NAME "test.xml" FILE* handle = fopen(FILE_NAME, "rb"); assert_that(handle, "Cannot open " FILE_NAME); struct xml_document* document = xml_open_document(handle); assert_that(document, "Cannot parse " FILE_NAME); struct xml_node* element = xml_easy_child( xml_document_root(document), "Element", "With", 0 ); assert_that(element, "Cannot find Document/Element/With"); // assert_that(string_equals(xml_node_content(element), "Child"), "Content of Document/Element/With must be `Child'"); xml_document_free(document, true); #undef FILE_NAME }
Ensure(ll_nth, returns_null_if_the_n_is_out_of_the_boundary) { ll *linkedlist = new_ll(LL_INT); int i; for(i = 0; i < NUM_OF_NODES; i++) ll_addback(linkedlist, &i, NULL); llnode *node = NULL; node = ll_nth(linkedlist, NUM_OF_NODES); assert_that(node, is_null); node = ll_nth(linkedlist, -NUM_OF_NODES - 1); assert_that(node, is_null); dispose_ll(&linkedlist); }
Ensure( node_tests, alloc_and_free_succeed ) { node_s *node = NULL; node = node_alloc(); assert_that( node, is_not_equal_to(NULL) ); node_free( &node ); assert_that( node, is_equal_to(NULL)); }
Ensure(eclGetContextInteractively, returnsAValidCommandQueue) { eclGetContextInteractively(&ctx); if (ctx.queue) { size_t size; err = clGetCommandQueueInfo(ctx.queue, CL_QUEUE_PROPERTIES, 0, 0, &size); assert_that(err, is_equal_to(CL_SUCCESS)); } }
Ensure(eclGetContextInteractively, returnsAValidContext) { eclGetContextInteractively(&ctx); if (ctx.context) { size_t size; err = clGetContextInfo(ctx.context, CL_CONTEXT_PROPERTIES, 0, 0, &size); assert_that(err, is_equal_to(CL_SUCCESS)); } }
Ensure(eclGetContextInteractively, returnsAValidDevice) { eclGetContextInteractively(&ctx); if (ctx.device) { size_t size; err = clGetDeviceInfo(ctx.device, CL_DEVICE_ENDIAN_LITTLE, 0, 0, &size); assert_that(err, is_equal_to(CL_SUCCESS)); } }
Ensure(ll_nth, returns_null_if_the_linkedlist_is_empty) { ll *linkedlist = new_ll(LL_INT); llnode *node = ll_nth(linkedlist, 1); assert_that(node, is_null); dispose_ll(&linkedlist); }
Ensure(FFT, can_be_constructed_from_DMDA) { ierr = DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,20,2,1,NULL,&da); CHKERRV(ierr); ierr = scFftCreate(da, &fft); assert_that(ierr, is_equal_to(0)); scFftDestroy(&fft); CHKERRV(ierr); ierr = DMDestroy(&da); CHKERRV(ierr); }
Ensure(string, create_and_assign_then_look_for_char) { // Allocate an empty string, assign data and search for a character struct zob_string * p_string = NULL; zob_string_create(&p_string); assert_that(p_string, is_not_equal_to(NULL)); char const * p_message = "Alea jacta est"; size_t size = 0; zob_string_write(p_string, size, p_message, &size); assert_that(strlen(p_message), is_equal_to(size)); size_t position = 0; bool has_found = false; zob_string_find_char(p_string, 'j', 0, size, &position, &has_found); assert_that(has_found, is_true); assert_that(position, is_equal_to(5)); zob_string_dispose(&p_string); assert_that(p_string, is_equal_to(NULL)); }
Ensure(ll_traverse, adds_new_nodes_in_front_of_the_ll_properly) { ll *linkedlist = new_ll(LL_INT); int i; for(i = 0; i < NUM_OF_NODES; i++) ll_addfront(linkedlist, &i, NULL); int counter = 0; ll_traverse(linkedlist, increment, &counter); assert_that(counter, is_equal_to(linkedlist->len)); dispose_ll(&linkedlist); }
Ensure(FFT, yields_PSD_of_the_correct_size) { PetscInt inputDim = 10; PetscInt inputBlockSize = 3; DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,inputDim,inputBlockSize,1,NULL,&da); scFftCreate(da, &fft); Vec x; scFftCreateVecPSD(fft, &x); PetscInt dim; VecGetSize(x, &dim); assert_that(dim, is_equal_to(inputDim / 2)); VecDestroy(&x); scFftDestroy(&fft); }