static char* test_cbuffer_pop(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); Buffer* bucky = cbuffer_pop(mybuf, 5); mu_assert_eq("size", cbuffer_size(mybuf), 6); mu_assert_eq("content", memcmp(bucky->data, test_data, 5*sizeof(uint32_t)), 0); Buffer* badger = cbuffer_pop(mybuf, 6); mu_assert_eq("size2", cbuffer_size(mybuf), 0); mu_assert_eq("content2", memcmp(badger->data, test_data + 5, 6), 0); // if we pop an empty collection, we get nothing. Buffer* empty = cbuffer_pop(mybuf, 10); mu_assert_eq("size3", empty->size, 0); cbuffer_free(mybuf); buffer_free(bucky); buffer_free(badger); buffer_free(empty); return 0; }
static char* test_cbuffer_transfer_data(void) { CircularBuffer* src = cbuffer_new(); CircularBuffer* dst = cbuffer_new(); for (int i = 0; i < 200; ++i) { cbuffer_push_back(src, i); } mu_assert_eq("Src buffer size", cbuffer_size(src), 200); mu_assert_eq("Dst buffer size", cbuffer_size(dst), 0); // transfer data from src -> dst cbuffer_transfer_data(src, dst); mu_assert_eq("Src buffer size post transfer", cbuffer_size(src), 0); mu_assert_eq("Dst buffer size post transfer", cbuffer_size(dst), 200); // check content for (int i = 0; i < 200; ++i) { mu_assert_eq("Dst content", cbuffer_pop_front(dst), i); } cbuffer_free(src); cbuffer_free(dst); return 0; }
int t_servhistory_getlist__multiple_items__retrieved() { FILE *f = fopen(TMP_FSFILE, "w"); int retval; struct serv_info list[5]; mu_assert("fopen", f); fprintf(f, "localhost 6667 4\n"); fprintf(f, "otherhost 612 3\n"); fclose(f); f = fopen(TMP_FSFILE, "r"); mu_assert("fopen", f); retval = serv_getlist(f, list, 5); fclose(f); mu_assert_eq(retval, 2, "list size"); mu_assert_streq(list[0].servname, "localhost", "host 1"); mu_assert_streq(list[1].servname, "otherhost", "host 2"); mu_assert_streq(list[0].port, "6667", "port 1"); mu_assert_streq(list[1].port, "612", "port 1"); mu_assert_eq(list[0].times_used, 4, "times_used 1"); mu_assert_eq(list[1].times_used, 3, "times_used 1"); mu_end; }
static char* test_spi_stream_construct_empty_tx_packet(void) { CircularBuffer* mybuf = cbuffer_new(); uint32_t my_pkt_expected[10] = {0xBEEF, 0, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, -1}; add_checksum(my_pkt_expected, 10); int checksum_err = -1; spi_stream_verify_packet(my_pkt_expected, 10, &checksum_err); mu_assert_eq("no cksum err", checksum_err, 0); uint32_t my_pkt[10]; spi_stream_construct_tx_packet(0xBEEF, my_pkt, 10, mybuf); // for (int i = 0; i < 10; ++i) { // printf("%i %lx %lx\n", i, my_pkt_expected[i], my_pkt[i]); // } mu_assert_eq("packet", memcmp(my_pkt_expected, my_pkt, 10 * sizeof(uint32_t)), 0); spi_stream_verify_packet(my_pkt, 10, &checksum_err); mu_assert_eq("no cksum actual", checksum_err, 0); return 0; }
char *test_init_vector() { AH5_vector_t vec; mu_assert("bad input", !AH5_init_ft_vector(NULL, NULL, 0, H5T_FLOAT)); mu_assert("set path", AH5_init_ft_vector(&vec, "name", 0, H5T_FLOAT)); mu_assert_str_equal("set path", vec.path, "name"); mu_assert_eq("empty vec", vec.nb_values, 0); mu_assert_eq_ptr("empty vec", vec.values.f, NULL); mu_assert_eq_ptr("empty vec", vec.values.i, NULL); free(vec.path); vec.path = NULL; mu_assert("init without name", AH5_init_ft_vector(&vec, NULL, 10, H5T_FLOAT)); mu_assert_eq_ptr("vec name", vec.path, NULL); mu_assert_eq("vec size", vec.nb_values, 10); mu_assert_ne("vec value", vec.values.f, NULL); free(vec.values.f); mu_assert("init", AH5_init_ft_vector(&vec, "name", 10, H5T_FLOAT)); AH5_free_ft_vector(&vec); return MU_FINISHED_WITHOUT_ERRORS; }
bool test_ht_general(void) { int retval = MU_PASSED; bool found = false; Person *p, *person1 = malloc (sizeof (Person)); if (!person1) { mu_cleanup_fail(err_malloc, "person1 malloc"); } person1->name = strdup ("radare"); person1->age = 10; Person *person2 = malloc (sizeof (Person)); if (!person2) { mu_cleanup_fail(err_free_person1, "person2 malloc"); } person2->name = strdup ("pancake"); person2->age = 9000; HtPP *ht = ht_pp_new ((HtPPDupValue)duplicate_person, free_kv, (HtPPCalcSizeV)calcSizePerson); if (!ht) { mu_cleanup_fail(err_free_persons, "ht alloc"); } ht_pp_insert (ht, "radare", (void *)person1); ht_pp_insert (ht, "pancake", (void *)person2); p = ht_pp_find (ht, "radare", &found); mu_assert ("radare not found", found); mu_assert_streq (p->name, "radare", "wrong person"); mu_assert_eq (p->age, 10, "wrong radare age"); p = ht_pp_find (ht, "pancake", &found); mu_assert ("radare not found", found); mu_assert_streq (p->name, "pancake", "wrong person"); mu_assert_eq (p->age, 9000, "wrong pancake age"); (void)ht_pp_find (ht, "not", &found); mu_assert ("found but it should not exists", !found); ht_pp_delete (ht, "pancake"); p = ht_pp_find (ht, "pancake", &found); mu_assert ("pancake was deleted", !found); ht_pp_insert (ht, "pancake", (void *)person2); ht_pp_delete (ht, "radare"); ht_pp_update (ht, "pancake", (void *)person1); p = ht_pp_find (ht, "pancake", &found); mu_assert ("pancake was updated", found); mu_assert_streq (p->name, "radare", "wrong person"); mu_assert_eq (p->age, 10, "wrong age"); ht_pp_free (ht); err_free_persons: free (person2->name); free (person2); err_free_person1: free (person1->name); free (person1); err_malloc: mu_cleanup_end; }
static char* test_ipbus_detect_packet_header() { uint32_t aheader = ipbus_packet_header(0xBEEF, 0); mu_assert_eq("hdr", ipbus_detect_packet_header(aheader), IPBUS_ISTREAM_PACKET); mu_assert_eq("hdr swapped", ipbus_detect_packet_header(__bswap_32(aheader)), IPBUS_ISTREAM_PACKET_SWP_ORD); mu_assert_eq("not a hdr", ipbus_detect_packet_header(0xDEADBEEF), 0); mu_assert_eq("a transaction", ipbus_detect_packet_header(ipbus_transaction_header(2, 0xEEF, IPBUS_READ, 2, 0xf)), 0); return 0; }
bool test_r_list_length(void) { RList* list = r_list_new (); RList* list2 = r_list_new (); RListIter *iter; int count = 0; int test1 = 33508; int test2 = 33480; int test3 = 33964; // Put in not sorted order. r_list_append (list, (void*)&test1); r_list_append (list, (void*)&test3); r_list_append (list, (void*)&test2); iter = list->head; while (iter) { count++; iter = iter->n; } mu_assert_eq (list->length, 3, "First length check"); r_list_delete_data (list, (void*)&test1); mu_assert_eq (list->length, 2, "Second length check"); r_list_append (list, (void*)&test1); mu_assert_eq (list->length, 3, "Third length check"); r_list_pop (list); mu_assert_eq (list->length, 2, "Fourth length check"); r_list_pop_head (list); mu_assert_eq (list->length, 1, "Fifth length check"); r_list_insert (list, 2, (void*)&test2); mu_assert_eq (list->length, 2, "Sixth length check"); r_list_prepend (list, (void*)&test3); mu_assert_eq (list->length, 3, "Seventh length check"); r_list_del_n (list, 2); mu_assert_eq (list->length, 2, "Eighth length check"); r_list_append (list2, (void*)&test1); r_list_append (list2, (void*)&test3); r_list_append (list2, (void*)&test2); r_list_join (list, list2); mu_assert_eq (list->length, 5, "Ninth length check"); iter = list->head; count = 0; while (iter) { count++; iter = iter->n; } mu_assert_eq (list->length, count, "Tenth length check"); r_list_free (list); r_list_free (list2); mu_end; }
int t_pollfds_setcapacity__capacity_is_inferior__notchanged() { struct pollfds* pfds = pollfds_init(0); int retval = pollfds_setcapacity(pfds, DEFAULT_PFDS_LEN - 1); mu_assert_eq(retval, OK, "operation failed"); mu_assert_eq(pfds->capacity, DEFAULT_PFDS_LEN, "capacity modified"); pollfds_destroy(pfds); mu_end; }
int t_pollfds_setcapacity__capacity_is_greater__realloc_mem() { struct pollfds* pfds = pollfds_init(0); int retval = pollfds_setcapacity(pfds, DEFAULT_PFDS_LEN + 20); mu_assert_eq(retval, OK, "operation failed"); mu_assert_eq(pfds->capacity, DEFAULT_PFDS_LEN + 20, "capacity not modified"); pollfds_destroy(pfds); mu_end; }
static char* test_cbuffer_new(void) { CircularBuffer* mybuf = cbuffer_new(); mu_assert_eq("size", cbuffer_size(mybuf), 0); mu_assert_eq("pos", mybuf->pos, 0); mu_assert_eq("freespace", cbuffer_freespace(mybuf), IO_BUFFER_SIZE - 1); mu_assert_eq("init is zero", (mybuf->data[0]), 0); cbuffer_free(mybuf); return 0; }
bool test_r_list_values(void) { RList* list = r_list_new (); intptr_t test1 = 0x12345; intptr_t test2 = 0x88888; r_list_append (list, (void*)test1); r_list_append (list, (void*)test2); int top1 = (intptr_t)r_list_pop (list); int top2 = (intptr_t)r_list_pop (list); mu_assert_eq(top1, 0x88888, "first value not 0x88888"); mu_assert_eq(top2, 0x12345, "first value not 0x12345"); r_list_free (list); mu_end; }
static char* test_cbuffer_size(void) { CircularBuffer* mybuf = cbuffer_new(); mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("size0", cbuffer_size(mybuf), 0); for (int i = 0; i < 15; ++i) { cbuffer_push_back(mybuf, i); mu_assert_eq("size", cbuffer_size(mybuf), i + 1); } cbuffer_free(mybuf); return 0; }
int t_pollfds_add__normal_add__adds_fds() { int flags = 12; struct pollfds* pfds = pollfds_init(flags); int fd = 5; int retval = pollfds_add(pfds, fd); mu_assert_eq(retval, OK, "operation failed"); mu_assert_eq(pfds->len, 1, "len is not correct"); mu_assert_eq(pfds->fds[0].fd, fd, "fd is not the same"); mu_assert_eq(pfds->fds[0].events, flags, "flags are not correctly set"); pollfds_destroy(pfds); mu_end; }
bool test_r_list_join(void) { RList* list1 = r_list_new (); RList* list2 = r_list_new (); intptr_t test1 = 0x12345; intptr_t test2 = 0x88888; r_list_append (list1, (void*)test1); r_list_append (list2, (void*)test2); int joined = r_list_join (list1, list2); mu_assert_eq(joined, 1, "r_list_join of two lists"); mu_assert_eq(r_list_length (list1), 2, "r_list_join two single element lists result length is 1"); r_list_free (list1); r_list_free (list2); mu_end; }
static char* test_cbuffer_contiguous_data_size(void) { CircularBuffer* mybuf = cbuffer_new(); mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("size0", cbuffer_contiguous_data_size(mybuf), 0); mybuf->tail = 10; mu_assert_eq("size5", cbuffer_contiguous_data_size(mybuf), 5); mybuf->pos = 3; mu_assert_eq("size7", cbuffer_contiguous_data_size(mybuf), 7); cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_freespace(void) { CircularBuffer* mybuf = cbuffer_new(); mybuf->tail = IO_BUFFER_SIZE - 5; mu_assert_eq("freespace", cbuffer_freespace(mybuf), 4); mu_assert_eq("size", cbuffer_size(mybuf), IO_BUFFER_SIZE - 5); for (int i = 1; i < 5; ++i) { cbuffer_push_back(mybuf, i); mu_assert_eq("freespace", cbuffer_freespace(mybuf), 4 - i); } cbuffer_free(mybuf); return 0; }
static char* test_cbuffer_net_features(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer cbuffer_push_back_net(mybuf, 0xDEADBEEF); cbuffer_push_back_net(mybuf, 0xBEEFFACE); cbuffer_push_back_net(mybuf, 0xDEADFACE); mu_assert_eq("item0", cbuffer_value_at_net(mybuf, 0), 0xDEADBEEF); mu_assert_eq("item1", cbuffer_value_at_net(mybuf, 1), 0xBEEFFACE); mu_assert_eq("item2", cbuffer_value_at_net(mybuf, 2), 0xDEADFACE); cbuffer_free(mybuf); return 0; }
int t_dic_add__new_item__added() { dictionary *dic = dic_new_withint(str_duplicator, free); int key = 3; char str[] = "test1"; int retval; retval = dic_add(dic, &key, str); mu_assert_eq(retval, OK, "dic_add"); mu_assert_eq(dic_count(dic), 1, "count"); dic_destroy(dic, NULL); mu_end; }
/* BEGIN TESTS */ int t_pollfds_remove__moves_last() { int i; struct pollfds* pfds = pollfds_init(0); for(i = 0; i < 5; i++) pollfds_add(pfds, i); pollfds_remove(pfds, 2); mu_assert_eq(pfds->fds[2].fd, 4, "last message is not moved"); mu_assert_eq(pfds->len, 4, "len was not modified"); pollfds_destroy(pfds); mu_end; }
bool test_r_list_del_n(void) { RList* list = r_list_new (); intptr_t test1 = 0x12345; intptr_t test2 = 0x88888; r_list_append (list, (void*)test1); r_list_append (list, (void*)test2); mu_assert_eq (r_list_length (list), 2, "list is of length 2 when adding 2 values"); r_list_del_n (list, 0); int top1 = (intptr_t)r_list_pop (list); mu_assert_eq(top1, 0x88888, "error, first value not 0x88888"); r_list_free (list); mu_end; }
int t_parse_servinfo__line__parsed() { char line[] = "localhost 6667 1"; struct serv_info parsed; int retval; retval = parse_servinfo(line, &parsed); mu_assert_eq(retval, OK, "parse retval"); mu_assert_streq(parsed.port, "6667", "port"); mu_assert_streq(parsed.servname, "localhost", "servname"); mu_assert_eq(parsed.times_used, 1, "times_used"); mu_end; }
bool test_r_queue_zero_size(void) { // Create queue with max size 0. RQueue* queue = r_queue_new (0); mu_assert_eq ((int)(intptr_t)queue, (int)(intptr_t)NULL, "Create queue of size zero."); mu_end; }
static char* test_ipbus_decode_encode_write_transaction(void) { CircularBuffer* packet = cbuffer_new(); // a read request. uint32_t header = ipbus_transaction_header( 2, // protocol 0xACE, // transaction id 5, // number of words to write IPBUS_WRITE, IPBUS_INFO_REQUEST); uint32_t baseaddr = 0xBEEFFACE; cbuffer_push_back_net(packet, header); cbuffer_push_back_net(packet, baseaddr); for (size_t i = 0; i < 5; ++i) { cbuffer_push_back_net(packet, i); } ipbus_transaction_t decoded = ipbus_decode_transaction(packet, 0); CircularBuffer* encoded = cbuffer_new(); ipbus_encode_transaction(encoded, &decoded, 0); mu_assert_eq("encode-decode length", cbuffer_size(encoded), 1 + 1 + 1 * 5); mu_assert("encode-decode", memcmp(encoded->data, packet->data, 7*4)==0); return 0; }
// Test decoding + re-encoding a transaction stream static char* test_ipbus_decode_encode_transaction(void) { CircularBuffer* packet = cbuffer_new(); // a read request. uint32_t header = ipbus_transaction_header( 2, // protocol 0xACE, // transaction id 5, // number of words to read IPBUS_READ, IPBUS_INFO_REQUEST); uint32_t payload = 0xBEEFFACE; cbuffer_push_back_net(packet, header); cbuffer_push_back_net(packet, payload); ipbus_transaction_t decoded = ipbus_decode_transaction(packet, 0); CircularBuffer* encoded = cbuffer_new(); ipbus_encode_transaction(encoded, &decoded, 0); mu_assert_eq("encode-decode length", cbuffer_size(encoded), 2); mu_assert("encode-decode", memcmp(encoded->data, packet->data, 8)==0); return 0; }
int t_pollfds_add__no_more_capacity__asks_more_memory() { int i, retval; struct pollfds* pfds = pollfds_init(0); for(i = 0; i < DEFAULT_PFDS_LEN; i++) pollfds_add(pfds, i); retval = pollfds_add(pfds, i); mu_assert_eq(retval, OK, "operation failed"); mu_assert("capacity is not increased", pfds->capacity > DEFAULT_PFDS_LEN); mu_assert_eq(pfds->len, DEFAULT_PFDS_LEN + 1, "len is not correct"); mu_assert_eq(pfds->fds[DEFAULT_PFDS_LEN].fd, i, "fd is not the same"); pollfds_destroy(pfds); mu_end; }
static char* test_verify_packet(void) { uint32_t my_data[10] = {0xBEEF, 2, 3, 4, 5, 6, 7, 8, 9, 10}; add_checksum(my_data, 10); mu_assert_eq("not clobbered", my_data[8], 9); mu_assert("chksum", my_data[9] != 10); int cksum_error = -1; uint32_t packet_id = spi_stream_verify_packet(my_data, 10, &cksum_error); mu_assert_eq("pkt_id", packet_id, 0xBEEF); mu_assert_eq("error", cksum_error, 0); // simulate error my_data[5] = 1; packet_id = spi_stream_verify_packet(my_data, 10, &cksum_error); mu_assert_eq("pkt_id", packet_id, 0xBEEF); mu_assert_eq("error", cksum_error, 1); return 0; }
int t_serv_save_connection__existing_connection__times_used_increased() { FILE *f = fopen(TMP_FSFILE, "w"); struct serv_info info = { "localhost", "6667", 0 }; int retval; char line1[200]; mu_assert("fopen", f); fprintf(f, "localhost 6667 4\n"); fclose(f); retval = serv_save_connection_to(TMP_FSFILE, &info); mu_assert_eq(retval, OK, "add failed"); f = fopen(TMP_FSFILE, "r"); mu_assert("fopen r", f); if (fgets(line1, 100, f) == NULL) { mu_cleanup_sysfail(cleanup, "fgets 1"); } fclose(f); f = NULL; mu_assert_streq(line1, "localhost 6667 5\n", "line 1"); retval = MU_PASSED; cleanup: if (f) fclose(f); mu_cleanup_end; }
static char* test_cbuffer_read_wraps(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); Buffer* readout = buffer_new(NULL, 11); cbuffer_read(mybuf, readout->data, 11); mu_assert_eq("size", readout->size, 11); mu_assert_eq("content", memcmp(readout->data, test_data, 11 * sizeof(uint32_t)), 0); cbuffer_free(mybuf); buffer_free(readout); return 0; }
int t_dic_remove__existing__removed() { dictionary *dic = dic_new_withint(str_duplicator, free); int key = 3; char str[] = "test1"; int retval, count; dic_add(dic, &key, str); retval = dic_remove(dic, &key); count = dic_count(dic); mu_assert_eq(retval, OK, "retval is not ok"); mu_assert_eq(count, 0, "number of elements is not correct"); dic_destroy(dic, NULL); mu_end; }