void _list_test() { int a = 6, b = 12, c = 1234, d = 4321, i = 0, len; struct _list *list = NULL, *tmp; /* The order of data should be 4321, 12, 6, 1234 */ list = list_append(list, &a); list = list_prepend(list, &b); list = list_append(list, &c); list = list_prepend(list, &d); len = list_length(list); assert(len == 4); for (tmp = list; tmp != NULL; tmp = tmp->next, i++) { int *data = (int *)tmp->data; switch (i) { case 0: assert(*data == 4321); break; case 1: assert(*data == 12); break; case 2: assert(*data == 6); break; case 3: assert(*data == 1234); break; default: fprintf(stdout, "%s: unknown condition while checking list data\n", __func__); } } list_free(list); fprintf(stderr, "%s: all tests passed\n", __func__); }
} END_TEST // Ensure a two values can be prepended to a list as expected // ✔ Head should be updated // ✔ Tail should be updated // ✔ Data should be the same // ✔ Size should be updated START_TEST (test_prepend_2_shift_1_list) { kld_list_t * list = (kld_list_t *) new_list(); char * buf2 = "test data2"; list_prepend(list, buf2); char * buf = "test data"; list_prepend(list, buf); list_shift(list); fail_if(list->head == NULL, "Head is NULL"); fail_if(list->tail == NULL, "Tail is NULL"); fail_if(list->head->data != list->tail->data, "Head and Tail data have diverged"); fail_if(list->head->data != "test data2", "Unexecpted tail node value"); fail_if(list->size != 1, "Unexpected list size"); } END_TEST
void main(int argc, char* argv[]) { list * l; list_iter it; intptr_t i; char* sv[] = { "fff", "zzz", "rrr", "bbb", "ggg", "hhh", "lll", "ttt" }; //list test l = list_new(); for (i=0; i<10; ++i) { (i%2 ? list_append(l, (void*)i): list_prepend(l, (void*)i)); } list_insert_at(l, (void*)20,9); list_append(l, (void*)22); list_delete_item(l, (void*)22); list_delete_at(l, 10); list_dump(l, "%d\n"); list_free(l); //string list test l = list_new_full(free); for(i=0; i<8; ++i) list_append(l, strdup(sv[i])); list_prepend(l, strdup("nine")); list_delete_item_comp(l, strdup("seven"), (list_comparator)strcmp); list_delete_item_comp(l, "eee", (list_comparator)strcmp); list_dump(l, "%s\n"); list_free(l); l = list_new(); for(i=0; i<8; ++i) list_insert_sorted_comp(l, sv[i], (list_comparator)strcmp); list_dump(l, "%s\n"); list_free(l); }
void test_list_prepend(void) { ListEntry *list = NULL; assert(list_prepend(&list, &variable1) != NULL); check_list_integrity(list); assert(list_prepend(&list, &variable2) != NULL); check_list_integrity(list); assert(list_prepend(&list, &variable3) != NULL); check_list_integrity(list); assert(list_prepend(&list, &variable4) != NULL); check_list_integrity(list); assert(list_nth_data(list, 0) == &variable4); assert(list_nth_data(list, 1) == &variable3); assert(list_nth_data(list, 2) == &variable2); assert(list_nth_data(list, 3) == &variable1); /* Test out of memory scenario */ alloc_test_set_limit(0); assert(list_length(list) == 4); assert(list_prepend(&list, &variable1) == NULL); assert(list_length(list) == 4); check_list_integrity(list); list_free(list); }
} END_TEST // Ensure a two values can be prepended to a list as expected // ✔ Head should be updated // ✔ Tail should be updated // ✔ Data should be different // ✔ Size should be updated START_TEST (test_prepend_2_list) { kld_list_t * list = (kld_list_t *) new_list(); char * buf2 = "test data2"; list_prepend(list, buf2); char * buf = "test data"; list_prepend(list, buf); fail_if(list->head == NULL, "Head is NULL"); fail_if(list->tail == NULL, "Tail is NULL"); fail_if(list->head->data == list->tail->data, "Head and Tail data have not diverged"); fail_if(list->head->data != "test data", "Unexecpted head node value"); fail_if(list->tail->data != "test data2", "Unexecpted tail node value"); fail_if(list->head->next != list->tail, "Incorrect next node for Head"); fail_if(list->tail->prev != list->head, "Incorrect prev node for Tail"); fail_if(list->size != 2, "Unexpected list size"); } END_TEST
int main () { int res; res = NCDStringIndex_Init(&string_index); ASSERT_FORCE(res) NCDValMem_Init(&mem, &string_index); res = NCDValCons_Init(&cons, &mem); ASSERT_FORCE(res) NCDValRef val1 = complete(list_prepend(list_prepend(list_prepend(make_list(), make_string("hello")), make_string("world")), make_list())); char *str1 = NCDValGenerator_Generate(val1); ASSERT_FORCE(str1) ASSERT_FORCE(!strcmp(str1, "{{}, \"world\", \"hello\"}")) free(str1); NCDValRef val2 = complete(map_insert(map_insert(map_insert(make_map(), make_list(), make_list()), make_string("A"), make_list()), make_string("B"), make_list())); char *str2 = NCDValGenerator_Generate(val2); ASSERT_FORCE(str2) printf("%s\n", str2); ASSERT_FORCE(!strcmp(str2, "[\"A\":{}, \"B\":{}, {}:{}]")) free(str2); NCDValCons_Free(&cons); NCDValMem_Free(&mem); NCDStringIndex_Free(&string_index); return 0; }
static slab_cache_t * slab_cache_alloc() { slab_t *slab; void *obj; u32_t *p; DBG("%s\n", __FUNCTION__); if (list_empty(&slab_cache_cache.partial_slabs)) { // spinlock_unlock(&cache->slablock); // slab = slab_create(); void *data; unsigned int i; data = (void*)(PA2KA(alloc_page())); if (!data) { return NULL; } slab = (slab_t*)((u32_t)data + PAGE_SIZE - sizeof(slab_t)); /* Fill in slab structures */ frame_set_parent(ADDR2PFN(KA2PA(data)), slab); slab->start = data; slab->available = slab_cache_cache.objects; slab->nextavail = (void*)data; slab->cache = &slab_cache_cache; for (i = 0,p = (u32_t*)slab->start;i < slab_cache_cache.objects; i++) { *p = (u32_t)p+slab_cache_cache.size; p = (u32_t*)((u32_t)p+slab_cache_cache.size); }; atomic_inc(&slab_cache_cache.allocated_slabs); // spinlock_lock(&cache->slablock); } else { slab = list_get_instance(slab_cache_cache.partial_slabs.next, slab_t, link); list_remove(&slab->link); } obj = slab->nextavail; slab->nextavail = *((void**)obj); slab->available--; if (!slab->available) list_prepend(&slab->link, &slab_cache_cache.full_slabs); else list_prepend(&slab->link, &slab_cache_cache.partial_slabs); // spinlock_unlock(&cache->slablock); return (slab_cache_t*)obj; }
void test_list_foreach(void) { List *l = list_prepend(NULL, INT_TO_POINTER(1)); l = list_prepend(l, INT_TO_POINTER(2)); l = list_prepend(l, INT_TO_POINTER(3)); int sum = 0; list_foreach(l, foreach_cb, &sum); cut_assert_equal_int(6, sum); }
static void test_list (void) { ListEntry* list = NULL; int a, b, c; list_prepend (&list, &a); list_prepend (&list, &b); list_prepend (&list, &c); list_free (list); }
void decode (FILE *fin, int size, FILE *fout) { Decoder *dec = dec_new (); List *word, *entry; unsigned int code; uchar chr; chr = fgetc (fin); fputc (chr, fout); word = list_new (chr); for (; size > 1; size--) { chr = fgetc (fin); if (dec->nbits > MAX_BITS) { dec = dec_reset (dec); list_destroy (word); word = list_new (chr); fputc (chr, fout); } else { /* Se agrega el nuevo caracter al buffer. */ dec->bitbuffer.buffer = (dec->bitbuffer.buffer << 8) | chr; dec->bitbuffer.length += 8; while (dec->bitbuffer.length >= dec->nbits) { dec->bitbuffer.length -= dec->nbits; code = dec->bitbuffer.buffer >> dec->bitbuffer.length; dec->bitbuffer.buffer &= (1 << dec->bitbuffer.length) - 1; if (code < dec->charbuffer.nelem) { /* El código está en el diccionario. */ entry = dec->charbuffer.buffer[code]; fputl (entry, fout); /* Agrega word + entry[0] al diccionario. */ word = list_prepend (word, entry->data); dec_add_word (dec, word); list_destroy (word); word = NULL; for (; entry != NULL; entry = entry->next) word = list_prepend (word, entry->data); } else { /* Agrega word + word[0] al diccionario. */ word = list_prepend (word, list_last (word)); dec_add_word (dec, word); fputl (dec->charbuffer.buffer[dec->charbuffer.nelem-1], fout); } } } } list_destroy (word); dec_destroy (dec); }
void test_two_prepends(void) { int x, y; List *l = list_prepend(NULL, &x); l = list_prepend(l, &y); cut_assert_not_null(l); cut_assert_equal_pointer(&y, l->data); cut_assert_not_null(l->next); cut_assert_equal_pointer(&x, l->next->data); cut_assert_null(l->next->next); }
static int discovery_usb_scan(struct jaylink_context *ctx) { ssize_t ret; struct libusb_device **devs; struct jaylink_device *dev; size_t num; size_t i; ret = libusb_get_device_list(ctx->usb_ctx, &devs); if (ret < 0) { log_err(ctx, "Failed to retrieve device list: %s.", libusb_error_name(ret)); return JAYLINK_ERR; } num = 0; for (i = 0; devs[i]; i++) { dev = probe_device(ctx, devs[i]); if (!dev) continue; ctx->discovered_devs = list_prepend(ctx->discovered_devs, dev); num++; } libusb_free_device_list(devs, true); log_dbg(ctx, "Found %zu USB device(s).", num); return JAYLINK_OK; }
void list_insert(int index, LIST_TYPE value, List *list){ if(index > list->size || index < 0){ printf("index is out of bounds.\n"); assert(0); return; } Node* newNode = node_constructor(value); // this wud break if it wasnt the case that the only way to insert something to a empty list is to insert it into the 0 index. // whcih means prepend will handle this fine. if(index == 0){ list_prepend(value, list); } else if(index == list->size){ list_append(value, list); } else{ int counter = 0; Node* ptr; for(ptr = list->head; ptr != NULL; ptr = ptr->next){ if(counter == index){ Node* left = ptr->prev; Node* right = ptr; left->next = newNode; newNode->prev = left; right->prev = newNode; newNode->next = right; list->size++; return; } counter++; } } }
TEST_F(ListTest, test_simple_list_prepend) { list_t *list = list_new(NULL); EXPECT_TRUE(list_prepend(list, &list)); EXPECT_FALSE(list_is_empty(list)); EXPECT_EQ(list_length(list), 1U); list_free(list); }
static list * create_type_list(mvc *sql, dlist *params, int param) { sql_subtype *par_subtype; list * type_list = sa_list(sql->sa); dnode * n = NULL; if (params) { for (n = params->h; n; n = n->next) { dnode *an = n; if (param) { an = n->data.lval->h; par_subtype = &an->next->data.typeval; if (par_subtype && !par_subtype->type) /* var arg */ return type_list; list_append(type_list, par_subtype); } else { par_subtype = &an->data.typeval; list_prepend(type_list, par_subtype); } } } return type_list; }
// Must be called with monitor held static void schedule_next_instance(alarm_t *alarm, bool force_reschedule) { // If the alarm is currently set and it's at the start of the list, // we'll need to re-schedule since we've adjusted the earliest deadline. bool needs_reschedule = (!list_is_empty(alarms) && list_front(alarms) == alarm); if (alarm->callback) list_remove(alarms, alarm); // Calculate the next deadline for this alarm period_ms_t just_now = now(); period_ms_t ms_into_period = alarm->is_periodic ? ((just_now - alarm->created) % alarm->period) : 0; alarm->deadline = just_now + (alarm->period - ms_into_period); // Add it into the timer list sorted by deadline (earliest deadline first). if (list_is_empty(alarms) || ((alarm_t *)list_front(alarms))->deadline >= alarm->deadline) list_prepend(alarms, alarm); else for (list_node_t *node = list_begin(alarms); node != list_end(alarms); node = list_next(node)) { list_node_t *next = list_next(node); if (next == list_end(alarms) || ((alarm_t *)list_node(next))->deadline >= alarm->deadline) { list_insert_after(alarms, node, alarm); break; } } // If the new alarm has the earliest deadline, we need to re-evaluate our schedule. if (force_reschedule || needs_reschedule || (!list_is_empty(alarms) && list_front(alarms) == alarm)) reschedule_root_alarm(); }
void kmem_cache_free(struct kmem_cache *cache, void *object) { struct page *page = mm_page_from_virt((uintptr_t)object); page = get_head_page(page); ASSERT_MSG(cache == page->slab_cache, "attempt to free object from another cache"); add_free_object(page, object); if (page->num_allocated == 0) { list_remove(&cache->partial_slabs, page); free_pages(page, cache->pages_per_slab); } else if (page->num_allocated == cache->num_objects - 1) { list_prepend(&cache->partial_slabs, page); } else { struct page *next_page = list_next(&cache->partial_slabs, page); if (next_page && (next_page->num_allocated > page->num_allocated)) { list_remove(&cache->partial_slabs, page); list_insert(&cache->partial_slabs, next_page, page); } } }
static BOOL on_connect_timer(void *udata) { OpnSession *session; OpnNode *node; List *l; if (opn_connection_count() >= OPN_MAX_CONNECTIONS) return TRUE; for (l = OPENNAP->nodelist->nodes; l; l = l->next) { node = l->data; /* Check whether we are already connected to that node */ if (node->state != OPN_NODE_STATE_DISCONNECTED) continue; if (!(session = opn_session_new())) return TRUE; OPENNAP->sessions = list_prepend(OPENNAP->sessions, session); if (!opn_session_connect(session, node)) { OPENNAP->sessions = list_remove(OPENNAP->sessions, session); opn_session_free(session); } } return TRUE; }
static struct router_info *router_image_router_add(struct router_image *router_image, char *router_desc) { struct list *list; struct router_info *router_info; router_info = router_image_router_get(router_image, router_desc); if (router_info) goto out; router_info = malloc(sizeof(struct router_info)); if (!router_info) goto out; list = malloc(sizeof(struct list)); if (!list) goto free_router; memset(list, 0, sizeof(struct list)); memset(router_info, 0, sizeof(struct router_info)); strcpy(router_info->router_name, router_desc); list->data = router_info; list->next = NULL; list_prepend(&router_image->router_list, list); goto out; free_router: free(router_info); router_info = NULL; out: return router_info; }
static struct file_info *_router_image_add_file(struct router_image *router_image, char *file_name) { struct list *list; struct file_info *file_info; file_info = _router_image_get_file(router_image->file_list, file_name); if (file_info) goto out; file_info = malloc(sizeof(struct file_info)); if (!file_info) goto out; list = malloc(sizeof(struct list)); if (!list) goto free_node; memset(list, 0, sizeof(struct list)); memset(file_info, 0, sizeof(struct file_info)); strcpy(file_info->file_name, file_name); list->data = file_info; list->next = NULL; list_prepend(&router_image->file_list, list); goto out; free_node: free(file_info); file_info = NULL; out: return file_info; }
static void cb_notify(PNode *node, NodeNotifyEvent ev) { List *iter, *next; if(ev != NODEDB_NOTIFY_CREATE) return; for(iter = sync_info.queue_create_pend; iter != NULL; iter = next) { PNode *n; next = list_next(iter); n = list_data(iter); if(n->type == node->type) { LOG_MSG(("Node at %p has host-side ID %u, we can now sync it", n, node->id)); sync_info.queue_create_pend = list_unlink(sync_info.queue_create_pend, iter); list_destroy(iter); n->id = node->id; /* To-sync copy now has known ID. Excellent. */ sync_info.queue_sync = list_prepend(sync_info.queue_sync, n); /* Re-add to other queue. */ if(n->creator.port != NULL) { n->creator.remote = node; /* Fill in the remote version field. */ graph_port_output_create_notify(n); } break; } } }
void list_append(List *list, void *data) { assert(list != NULL); if (!list->first) { list_prepend(list, data); assert(list->first->next == NULL); return; } ListItem *item = malloc(sizeof(ListItem)); ListItem *current = list->first; ListItem *last = NULL; if (item == NULL) { exit(EXIT_FAILURE); } item->data = data; item->next = NULL; while (current != NULL){ if (!current->next) { last = current; } current = current->next; } last->next = item; }
List * db_load_products (void) { FILE *fp; char line[LINE_BUFFER_SIZE]; Product *product; List *ret = NULL; fp = fopen (PRODUCTS_DB_FILE, "r"); if (fp == NULL) { perror ("Error opening " PRODUCTS_DB_FILE); return NULL; } while (fgets (line, LINE_BUFFER_SIZE, fp)) { product = get_product_from_line (line); if (product) { ret = list_prepend (ret, product); } } fclose (fp); ret = list_reverse (ret); return ret; }
// Runs in exclusion with alarm_cancel and timer_callback. void alarm_set(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *data) { assert(alarms != NULL); assert(alarm != NULL); assert(cb != NULL); pthread_mutex_lock(&monitor); // If the alarm is currently set and it's at the start of the list, // we'll need to re-schedule since we've adjusted the earliest deadline. bool needs_reschedule = (!list_is_empty(alarms) && list_front(alarms) == alarm); if (alarm->callback) list_remove(alarms, alarm); alarm->deadline = now() + deadline; alarm->callback = cb; alarm->data = data; // Add it into the timer list sorted by deadline (earliest deadline first). if (list_is_empty(alarms)) list_prepend(alarms, alarm); else for (list_node_t *node = list_begin(alarms); node != list_end(alarms); node = list_next(node)) { list_node_t *next = list_next(node); if (next == list_end(alarms) || ((alarm_t *)list_node(next))->deadline >= alarm->deadline) { list_insert_after(alarms, node, alarm); break; } } // If the new alarm has the earliest deadline, we need to re-evaluate our schedule. if (needs_reschedule || (!list_is_empty(alarms) && list_front(alarms) == alarm)) reschedule(); pthread_mutex_unlock(&monitor); }
/* Returns a pointer to key in list, changes state of list */ void *list_find_MTF(bool (*eq)(void *key, void *node), List *list, void *key) { void *data = list_del(eq, list, key); if (data) { list_prepend(list, data); } return data; }
/** * Inserts a new element into the list at the given position. * * @param list a list * @param data the data for the new element * @param position the position to insert the element * * @returns the new start of the list */ List * list_insert(List *list, void *data, int position) { List *link; List *tmp; // If the position is below zero, we will append the element instead if (position < 0) return list_append(list, data); // If the position is 0, it's the same as prepending if (position == 0) return list_prepend(list, data); // Create the new link link = _list_alloc_link(); link->data = data; // Get the element which is currently at the position tmp = list_nth(list, position); // If we couldn't get it, position must be out of range. So let's append // the new link instead if (!tmp) return list_append(list, data); // Set up the new links tmp->prev->next = link; link->prev = tmp->prev; link->next = tmp; tmp->prev = link; // The list remains the same, because if we are here, the element is // not the first and there were already elements in the list. return list; }
static void iter_traverse(const XmlNode *node, List **list) { const List *iter; *list = list_prepend(*list, (void *) node); /* Prepend, for more speed. */ for(iter = node->children; iter != NULL; iter = list_next(iter)) iter_traverse(list_data(iter), list); }
list_item_t* list_prepend_new(list_t* list, void* data) { list_item_t* item = list_alloc_item(data); list_prepend(list, item); return item; }
void test_reverse_list(void) { List *l = list_prepend(NULL, INT_TO_POINTER(1)); l = list_prepend(l, INT_TO_POINTER(2)); l = list_prepend(l, INT_TO_POINTER(3)); l = list_reverse(l); cut_assert_not_null(l); cut_assert_equal_pointer(INT_TO_POINTER(1), l->data); cut_assert_not_null(l->next); cut_assert_equal_pointer(INT_TO_POINTER(2), l->next->data); cut_assert_not_null(l->next->next); cut_assert_equal_pointer(INT_TO_POINTER(3), l->next->next->data); cut_assert_null(l->next->next->next); }
struct list *int_to_list(int a) { struct list *l = list_new(); while ( a != 0 ) { list_prepend(l, a % 10); a /= 10; } return l; }