int test_encoding_message_length() { uint8_t* buffer = vw_encode(test_data, 4); // Byte 8 and 9 are the packet length (7) encoded 4-to-6 mu_assert_equal(buffer[8], 0xd); // 0 mu_assert_equal(buffer[9], 0x1c); // 7 return 0; }
char *test_read_complex_dataset() { int i,rank = 1; hsize_t dims[1]; hid_t dataspace_id, dset_id, dtr_id, dti_id, file_id; size_t type_size; hid_t type_id; herr_t status, status_2; float *real_part, *imag_part; const char* path = "dataset_name"; AH5_complex_t cplx[2]; AH5_complex_t * rdata; file_id = AH5_auto_test_file(); cplx[0].re=10.; cplx[0].im=20.; cplx[1].re=10.5; cplx[1].im=20.5; //first write complex array set with hdf5 lib real_part = (float *)malloc(2 * sizeof(float)); imag_part = (float *)malloc(2 * sizeof(float)); for( i=0;i<2;i++) { real_part[i] = cplx[i].re; imag_part[i] = cplx[i].im; } type_id = create_type_id(H5T_NATIVE_FLOAT); dims[0] = 2; dataspace_id = H5Screate_simple(rank, dims, NULL); dset_id = H5Dcreate(file_id,path,type_id,dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); type_size = H5Tget_size(H5T_NATIVE_FLOAT); dtr_id = H5Tcreate(H5T_COMPOUND,type_size); status = H5Tinsert(dtr_id,"r",0, H5T_NATIVE_FLOAT); dti_id = H5Tcreate(H5T_COMPOUND,type_size); status = H5Tinsert(dti_id,"i",0, H5T_NATIVE_FLOAT); status = H5Dwrite(dset_id,dtr_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,real_part); status = H5Dwrite(dset_id,dti_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,imag_part); status = H5Tclose(dtr_id); status = H5Tclose(dti_id); free(real_part); free(imag_part); mu_assert("Read complex dataset.", AH5_read_cpx_dataset(file_id,"dataset_name", 2, &rdata)); for (i = 0; i < 2; i++) { printf("Real parts : %f %f\n", cplx[i].re, rdata[i].re); printf("Imaginary parts : %f %f\n", cplx[i].im, rdata[i].im); mu_assert_equal("Check the real values.", cplx[i].re, rdata[i].re); mu_assert_equal("Check the imaginary value.", cplx[i].im, rdata[i].im); } return MU_FINISHED_WITHOUT_ERRORS; }
int test_encoding_message_fcs() { uint8_t* buffer = vw_encode(test_data, 4); // Bytes 18 - 21 are the ones complement of the CCITT CRC-16, which is 0x5cb5. // Byte order is little endian. mu_assert_equal(buffer[18], 0x29); // b mu_assert_equal(buffer[19], 0x19); // 5 mu_assert_equal(buffer[20], 0x19); // 5 mu_assert_equal(buffer[21], 0x2a); // c return 0; }
//! Test write dataset. // Data extracted from http://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5T/h5ex_t_string.c char *test_write_complex_dataset() { hid_t file_id, filetype, memtype, space, dset; size_t sdim; hsize_t dims[1] = {2}; int ndims, i, j; int rank; int length; size_t type_size; float * buf; hid_t real_id_type; herr_t status; hsize_t *newdims; AH5_complex_t cplx[2]; file_id = AH5_auto_test_file(); cplx[0].re=10.; cplx[0].im=20.; cplx[1].re=10.5; cplx[1].im=20.5; mu_assert("Write complex dataset.", AH5_write_cpx_dataset(file_id,"dataset_name", 2, cplx)); // Test the written data using hdf5 API. real_id_type = create_type_id(H5T_NATIVE_FLOAT); status = H5LTget_dataset_ndims(file_id, "dataset_name", &rank); newdims = (hsize_t *) malloc(rank * sizeof(hsize_t)); status = H5LTget_dataset_info(file_id,"dataset_name" , newdims, NULL, NULL); length = newdims[0]; for (i = 1; i < rank; i++) length = length * newdims[i]; buf = (float *) malloc(2 * length * sizeof(float)); status = H5LTread_dataset(file_id, "dataset_name", real_id_type, buf); j = 0; for (i = 0; i < length; i++) { printf("Real parts : %f %f\n", cplx[i].re, buf[j]); printf("Imaginary parts : %f %f\n", cplx[i].im, buf[j+1]); mu_assert_equal("Check the real values.", cplx[i].re, buf[j]); mu_assert_equal("Check the imaginary value.", cplx[i].im, buf[j+1]); j = j + 2; } free(buf); return MU_FINISHED_WITHOUT_ERRORS; }
int test_encoding_null_terminated() { uint8_t* buffer = vw_encode(test_data, 4); mu_assert_equal(buffer[22], 0x0); return 0; }
char *test_different_functions() { // create + length bstring string1 = bfromcstr("string"); mu_assert_equal(6, blength(string1)); // bassign, biseq bstring string2 = bfromcstr("foo"); bassign(string2, string1); mu_assert(strcmp(bdata(string1), bdata(string2)) == 0, "both strings should be equal"); mu_assert(biseq(string1, string2) == 1, "both strings should be equal"); // bconcat bstring string2 = bfromcstr("foo"); string1 = bfromcstr("Hello"); string2 = bfromcstr(", World!"); bconcat(string1, string2); mu_assert(strcmp("Hello, World!", bdata(string1)) == 0, "returned string not as expected"); // bsplit string1 = bfromcstr("foo, bar, baz"); struct bstrList *words; words = bsplit(string1, ','); mu_assert(strcmp("foo", bdata(words->entry[0])) == 0, "returned string not as expected"); return NULL; }
int test_setup_timer() { mock_avr_reset(); vw_avr_setup(VW_AVR_MODE_TX | VW_AVR_MODE_RX); // prescaler set mu_assert_equal(TCCR0B, VW_AVR_PRESCALER); // CTC mode enabled mu_assert_equal(TCCR0A & (1 << WGM01), 0x2); // compare value set mu_assert_equal(OCR0A, VW_AVR_COMPARE) // interrupt enabled mu_assert_equal(TIMSK & (1 << OCIE0A), 0x10); return 0; }
int test_encoding_preamble() { uint8_t* buffer = vw_encode(test_data, 4); // Training preamble mu_assert_equal(buffer[0], 0x2a); // 101010 mu_assert_equal(buffer[1], 0x2a); // 101010 mu_assert_equal(buffer[2], 0x2a); // 101010 mu_assert_equal(buffer[3], 0x2a); // 101010 mu_assert_equal(buffer[4], 0x2a); // 101010 mu_assert_equal(buffer[5], 0x2a); // 101010 // Sync vector mu_assert_equal(buffer[6], 0x3a); // 111010 mu_assert_equal(buffer[7], 0x2c); // 101100 return 0; }
int test_encoding_message_data() { uint8_t* buffer = vw_encode(test_data, 4); // Bytes 10 - 17 are the data encoded 4-to-6 mu_assert_equal(buffer[10], 0x15); // 3 mu_assert_equal(buffer[11], 0x26); // a mu_assert_equal(buffer[12], 0x25); // 9 mu_assert_equal(buffer[13], 0x19); // 5 mu_assert_equal(buffer[14], 0x23); // 8 mu_assert_equal(buffer[15], 0x15); // 3 mu_assert_equal(buffer[16], 0x1a); // 6 mu_assert_equal(buffer[17], 0x16); // 4 return 0; }
char *test_write_string_dataset() { #define DIM0 4 #define SDIM 8 hid_t file_id, filetype, memtype, space, dset; size_t sdim; hsize_t dims[1] = {DIM0}; int ndims, i, j; /*char wdata[DIM0][SDIM] =*/ char *wdata[] = {"Parting", "is such", "sweet ", "sorrow."}; char **rdata; // Write a simple mesh test. file_id = AH5_auto_test_file(); mu_assert("Write string dataset.", AH5_write_str_dataset(file_id, "dataset_name", DIM0, SDIM, wdata)); // Test the written data using hdf5 API. dset = H5Dopen(file_id, "/dataset_name", H5P_DEFAULT); filetype = H5Dget_type(dset); sdim = H5Tget_size(filetype); space = H5Dget_space(dset); ndims = H5Sget_simple_extent_dims(space, dims, NULL); rdata = (char **) malloc(dims[0] * sizeof (char *)); rdata[0] = (char *) malloc(dims[0] * sdim * sizeof (char)); for (i=1; i<dims[0]; i++) rdata[i] = rdata[0] + i * sdim; memtype = H5Tcopy(H5T_C_S1); mu_assert("HDF5 error in H5Tset_size.", H5Tset_size(memtype, sdim) >= 0); mu_assert("HDF5 error in H5Dread.", H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]) >= 0); for (i = 0; i < dims[0]; i++) { printf("%s %s\n", wdata[i], rdata[i]); /*mu_assert_str_equal("Check the first str dataset values.", wdata[i], rdata[i]);*/ j = 0; while (wdata[i][j] != ' ' && wdata[i][j] != '\0') { mu_assert_equal("Check the first str dataset values.", wdata[i][j], rdata[i][j]); ++j; } } // Release resources. free(rdata[0]); free(rdata); mu_assert("HDF5 error in H5Dclose.", H5Dclose(dset) >= 0); mu_assert("HDF5 error in H5Sclose.", H5Sclose(space) >= 0); mu_assert("HDF5 error in H5Tclose.", H5Tclose(filetype) >= 0); mu_assert("HDF5 error in H5Tclose.", H5Tclose(memtype) >= 0); // Write a string dataset using strlen. mu_assert("Write string dataset using strlen.", AH5_write_str_dataset(file_id, "dataset_name_2", DIM0, strlen(wdata[0]) + 1, wdata)); // Test the written data using hdf5 API. dset = H5Dopen(file_id, "/dataset_name", H5P_DEFAULT); filetype = H5Dget_type(dset); sdim = H5Tget_size(filetype); space = H5Dget_space(dset); ndims = H5Sget_simple_extent_dims(space, dims, NULL); rdata = (char **) malloc(dims[0] * sizeof (char *)); rdata[0] = (char *) malloc(dims[0] * sdim * sizeof (char)); for (i=1; i<dims[0]; i++) rdata[i] = rdata[0] + i * sdim; memtype = H5Tcopy(H5T_C_S1); mu_assert("HDF5 error in H5Tset_size.", H5Tset_size(memtype, sdim) >= 0); mu_assert("HDF5 error in H5Dread.", H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]) >= 0); for (i = 0; i < dims[0]; i++) { /*mu_assert_str_equal("Check the first str dataset values.", wdata[i], rdata[i]);*/ j = 0; while (wdata[i][j] != ' ' && wdata[i][j] != '\0') { mu_assert_equal("Check the first str dataset values.", wdata[i][j], rdata[i][j]); ++j; } } // Release resources. free(rdata[0]); free(rdata); mu_assert("HDF5 error in H5Dclose.", H5Dclose(dset) >= 0); mu_assert("HDF5 error in H5Sclose.", H5Sclose(space) >= 0); mu_assert("HDF5 error in H5Tclose.", H5Tclose(filetype) >= 0); mu_assert("HDF5 error in H5Tclose.", H5Tclose(memtype) >= 0); // Close file. AH5_close_test_file(file_id); return MU_FINISHED_WITHOUT_ERRORS; }
int test_send() { uint8_t* buffer = malloc(4); buffer[0] = 0xd; buffer[1] = 0x19; buffer[2] = 0x15; buffer[3] = 0; mock_avr_reset(); vw_avr_setup(VW_AVR_MODE_TX); mu_assert_equal(vw_avr_tx_in_progress(), 0); vw_avr_send(buffer); mu_assert_equal(vw_avr_tx_in_progress(), 1); // each bit is transmitted on the timer interrupt // 0xd - 101100 TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); // 0x19 - 100110 TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); // 0x15 - 101010 TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); // 0x0 - null terminated mu_assert_equal(vw_avr_tx_in_progress(), 1); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); mu_assert_equal(vw_avr_tx_in_progress(), 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); TIMER0_COMPA_vect(); mu_assert_bit(VW_AVR_TX_PORT, VW_AVR_TX_PIN, 0); return 0; }