static char* clear_multiple(void) { linked_list_append(root, create_object(1)); linked_list_append(root, create_object(2)); linked_list_append(root, create_object(3)); linked_list_clear(root); mu_assert(0 == linked_list_count(root), "List is not empty after clearing multiple item list."); return 0; }
static char* insert_invalid_index(void) { linked_list_append(root, create_object(1)); linked_list_append(root, create_object(2)); linked_list_insert(root, create_object(3), 4); Object* object = (Object*)linked_list_get(root, 2); mu_assert(NULL != object && 3 == object->id, "Cannot insert item at an invalid index."); return 0; }
static char* append_count_multiple(void) { linked_list_append(root, create_object(1)); linked_list_append(root, create_object(2)); linked_list_append(root, create_object(3)); linked_list_append(root, create_object(4)); mu_assert(4 == linked_list_count(root), "Appending four item doesn't set size to 4."); return 0; }
static char* append_get_multiple(void) { linked_list_append(root, create_object(1)); linked_list_append(root, create_object(2)); Object* object1 = (Object*)linked_list_get(root, 0); Object* object2 = (Object*)linked_list_get(root, 1); mu_assert(NULL != object1 && 1 == object1->id, "Cannot get first appended item."); mu_assert(NULL != object2 && 2 == object2->id, "Cannot get second appended item."); return 0; }
static char* remove_multiple_items(void) { linked_list_append(root, create_object(1)); linked_list_append(root, create_object(2)); linked_list_append(root, create_object(3)); linked_list_remove(root, 2); linked_list_remove(root, 1); mu_assert(1 == get_id(linked_list_get(root, 0)), "Removing multiple items failed."); mu_assert(1 == linked_list_count(root), "Removing multiple items failed."); return 0; }
int main() { MemoryPool *pool = memory_pool_init(sizeof(100) * 100); LinkedList *list = NULL; list = linked_list_append(list, memory_pool_alloc_int(pool, 1)); list = linked_list_append(list, memory_pool_alloc_int(pool, 2)); list = linked_list_append(list, memory_pool_alloc_int(pool, 3)); list = linked_list_append(list, memory_pool_alloc_int(pool, 2)); list = linked_list_append(list, memory_pool_alloc_int(pool, 4)); linked_list_print(list); remove_duplicates_in_place(list); linked_list_print(list); list = linked_list_free(list); memory_pool_free(pool); return 0; }
LinkedList* get_ventes(char* filename) { // Ouvrir le fichier FILE* flot = fopen(filename, "rb"); // Tester si flot n'est pas NULL if (!flot) return NULL; // Declarer une liste chainées de ventes LinkedList* ventes = NULL; // Lire les données du fichier do { Vente* vente = (Vente*)malloc(sizeof(Vente)); if (!fread(vente, sizeof(Vente), 1, flot)) break; linked_list_append(&ventes, vente); } while (1); // Fermer le fichier fclose(flot); // Return return ventes; }
GBitmap* bitmaps_get_bitmap_in_group(uint32_t res_id, uint8_t group) { if (! bitmaps) { return NULL; } if (group <= 0) { return NULL; } AppBitmap* app_bmp = get_app_bitmap_by_group(group); if (NULL == app_bmp) { app_bmp = malloc(sizeof(AppBitmap)); app_bmp->res_id = res_id; app_bmp->bitmap = gbitmap_create_with_resource(app_bmp->res_id); app_bmp->group = group; app_bmp->is_sub = false; linked_list_append(bitmaps, app_bmp); } else { if (res_id != app_bmp->res_id) { gbitmap_destroy(app_bmp->bitmap); app_bmp->res_id = res_id; app_bmp->bitmap = gbitmap_create_with_resource(app_bmp->res_id); } } return app_bmp->bitmap; }
LinkedList* get_fournisseurs(char* filename) { // Ouvrir le fichier FILE* flot = fopen(filename, "rb"); // Tester si flot n'est pas NULL if (!flot) return NULL; // Declarer une liste chainées de fournisseurs LinkedList* fournisseurs = NULL; // Lire les données du fichier do { Fournisseur* fournisseur = (Fournisseur*)malloc(sizeof(Fournisseur)); if (!fread(fournisseur, sizeof(Fournisseur), 1, flot)) break; linked_list_append(&fournisseurs, fournisseur); } while (1); // Fermer le fichier fclose(flot); // Return return fournisseurs; }
LinkedList* get_ventes_from_date(char* filename, char* date, DateComponents ignore_date_components) { // Ouvrir le fichier FILE* flot = fopen(filename, "rb"); // Tester si flot n'est pas NULL if (!flot) return NULL; // Liste chainée de ventes LinkedList* ventes = NULL; do { Vente* vente = (Vente*)malloc(sizeof(Vente)); if(!fread(vente, sizeof(Vente), 1, flot)) break; if (vente -> vente_id == 0) break; // Ajouter que les ventes dont la date égale à // La date donnée par l'utilisateur struct tm vente_time = *localtime(&(vente -> date_time)); struct tm user_time; strptime(date, "%Y-%m-%d", &user_time); if ( (( user_time.tm_year == vente_time.tm_year || (Year & ignore_date_components) ) && ( user_time.tm_mon == vente_time.tm_mon || (Month & ignore_date_components) ) && user_time.tm_mday == vente_time.tm_mday) || (Day & ignore_date_components) ) { linked_list_append(&ventes, vente); } } while (1); // Return return ventes; }
static void * tpool_worker(void * arg) { thread_pool_t * pool = (thread_pool_t * ) arg; pthread_mutex_lock(&pool->pj_mutex); while (!pool->shutdown) { while (pool->pending_jobs->len == 0) { pthread_cond_wait(&pool->pj_cond, &pool->pj_mutex); if (pool->shutdown) goto SHUTDOWN; } /* we have the mutex & len > 0 */ tpjob_t * job = linked_list_remove(pool->pending_jobs, 0); if (!job) continue; pthread_mutex_unlock(&pool->pj_mutex); void * ret_val = (job->job)(job->arg); if (job->use_arg_free) job->arg_free(job->arg); if (job->add_rv) { pthread_mutex_lock(job->cj_mutex); linked_list_append(job->completed_jobs, ret_val); pthread_cond_signal(job->cj_cond); pthread_mutex_unlock(job->cj_mutex); } free(job); pthread_mutex_lock(&pool->pj_mutex); } SHUTDOWN: pthread_mutex_unlock(&pool->pj_mutex); pthread_exit(NULL); }
void hash_map_put(hash_map *map, void *key, void *value) { linked_list *list = map->table[map->hash_func(key, map->capacity)]; if (!list) { list = (linked_list *) safe_malloc(sizeof(linked_list)); linked_list_init(list, (linked_list_destructor) safe_free); map->table[map->hash_func(key, map->capacity)] = list; } linked_list_node *head = linked_list_head(list); while (head) { hash_map_pair *pair = (hash_map_pair *) head->data; // if key already exists, update the value if (map->comparator(pair->key, key) == 0) { pair->value = value; return; } head = head->next; } // or else insert new one hash_map_pair *pair = (hash_map_pair *) safe_malloc(sizeof(hash_map_pair)); pair->key = key; pair->value = value; linked_list_prepend(list, pair); linked_list_append(map->keys, key); map->size++; }
static char* contains_compare_true(void) { Object* object1 = create_object(1); Object* object2 = create_object(1); linked_list_append(root, object1); mu_assert(true == linked_list_contains_compare(root, object2, object_compare), "Could not find object with compare."); return 0; }
static char* contains_compare_false(void) { Object* object1 = create_object(1); Object* object2 = create_object(2); linked_list_append(root, object1); mu_assert(false == linked_list_contains_compare(root, object2, object_compare), "Found invalid object with compare."); return 0; }
static char* prepend_not_empty(void) { linked_list_append(root, create_object(1)); linked_list_prepend(root, create_object(2)); Object* object = (Object*)linked_list_get(root, 0); mu_assert(NULL != object && 2 == object->id, "Cannot get item prepended on list."); return 0; }
int test_list_int() { linked_list_t *list; linked_list_cursor_t *cursor; int i, data; // create a new list list = linked_list_create( NULL, /* original list */ linked_list_int_cmp, /* default int comparator */ LINKED_LIST_SORTED /* sort list */ ); // add 10 entries for (i = 0; i < 10; i++) { linked_list_append(list, (void *)i, (void *)i); } // create a cursor for cycling/searching cursor = linked_list_cursor_create(NULL, list); // walk trough listh for (LINKED_LIST_FORLOOP(cursor, &data)) { printf("%d\n", data); } }
int tpool_add_job(thread_pool_t * pool, job_fun_t call, void * arg, int opt, delete_t arg_free, completed_jobs_t * cjl) { tpjob_t * job = (tpjob_t * ) malloc(sizeof(tpjob_t)); job->job = call; job->arg = arg; job->use_arg_free = ((opt & TPOOL_FREE_ARG) == TPOOL_FREE_ARG) ? TRUE : FALSE; job->arg_free = arg_free; job->add_rv = ((opt & TPOOL_NO_RV) == TPOOL_NO_RV) ? FALSE : TRUE; if ((opt & TPOOL_USE_CJL) == TPOOL_USE_CJL) { job->cj_mutex = &cjl->mutex; job->cj_cond = &cjl->cond; job->completed_jobs = cjl->completed; job->add_rv = TRUE; } else { job->cj_mutex = &pool->cj_mutex; job->cj_cond = &pool->cj_cond; job->completed_jobs = pool->completed_jobs; } pthread_mutex_lock(&pool->pj_mutex); linked_list_append(pool->pending_jobs, job); pthread_cond_signal(&pool->pj_cond); pthread_mutex_unlock(&pool->pj_mutex); return 0; }
static char* find_compare_invalid(void) { Object* object1 = create_object(1); Object* object2 = create_object(2); linked_list_append(root, object1); mu_assert(-1 == linked_list_find_compare(root, object2, object_compare), "Found incorrect object index by comparison."); return 0; }
static char* find_compare_valid(void) { Object* object1 = create_object(1); Object* object2 = create_object(1); linked_list_append(root, object1); mu_assert(0 == linked_list_find_compare(root, object2, object_compare), "Could not find object index by comparison."); return 0; }
static void append_parsed_rule(struct linked_list *parsed_rules, int rule_index) { int *rule_ptr; rule_ptr = malloc(sizeof(*rule_ptr)); *rule_ptr = rule_index; linked_list_append(parsed_rules, rule_ptr); }
status_t persist_write_int(const uint32_t key, const int32_t value) { persist_delete(key); Persist* persist = malloc(sizeof(Persist)); persist->key = key; persist->type = NUMBER; persist->number = value; linked_list_append(persistence, persist); return 0; }
EventHandle events_app_message_subscribe_handlers(EventAppMessageHandlers handlers, void *context) { prv_init(); AppMessageHandlerState *state = malloc(sizeof(AppMessageHandlerState)); state->handlers = handlers; state->context = context; linked_list_append(s_handler_list, state); return state; }
EventHandle events_battery_state_service_subscribe(BatteryStateHandler handler) { prv_init(); BatteryHandler *our_handler = malloc(sizeof(BatteryHandler)); our_handler->has_context = false; our_handler->handler = handler; our_handler->context = NULL; linked_list_append(s_handler_list, our_handler); return our_handler; }
int persist_write_data(const uint32_t key, const void *data, const size_t size) { persist_delete(key); Persist* persist = malloc(sizeof(Persist)); persist->key = key; persist->type = DATA; persist->data = malloc(size); memcpy(persist->data, data, size); linked_list_append(persistence, persist); return size; }
int persist_write_string(const uint32_t key, const char *cstring) { persist_delete(key); Persist* persist = malloc(sizeof(Persist)); persist->key = key; persist->type = STRING; persist->str = malloc(strlen(cstring) + 1); strcpy(persist->str, cstring); linked_list_append(persistence, persist); return strlen(cstring); }
EventHandle events_battery_state_service_subscribe_context(EventBatteryStateHandler handler, void *context) { prv_init(); BatteryHandler *our_handler = malloc(sizeof(BatteryHandler)); our_handler->has_context = true; our_handler->ctx_handler = handler; our_handler->context = context; linked_list_append(s_handler_list, our_handler); return our_handler; }
void test_linked_list_add_and_remove () { linked_list *list = create_linked_list (); TEST_ASSERT_NOT_NULL (list); // try to append an item and get it back later char *str1 = "hello"; linked_list_append (list, str1); TEST_ASSERT_EQUAL_INT (list->item_count, 1); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1); // try to append multiple items and get one back later char *strs1[] = {"world", "!"}; linked_list_append_all (list, (void **) strs1, 2); TEST_ASSERT_EQUAL_INT (list->item_count, 3); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), strs1[0]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs1[1]); // try to add an item at a specific position char *str2 = " "; linked_list_add (list, 1, str2); TEST_ASSERT_EQUAL_INT (list->item_count, 4); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), str2); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs1[0]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 3), strs1[1]); // try to add multiple items at a specific position char *strs2[] = {"WORLD", "?", "\n", "HELLO "}; linked_list_add_all (list, 2, (void **) strs2, 4); TEST_ASSERT_EQUAL_INT (list->item_count, 8); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), str2); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs2[0]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 3), strs2[1]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 4), strs2[2]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 5), strs2[3]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 6), strs1[0]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 7), strs1[1]); // try to remove an item linked_list_remove (list, 6); TEST_ASSERT_EQUAL_INT (list->item_count, 7); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 0), str1); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 1), str2); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 2), strs2[0]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 3), strs2[1]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 4), strs2[2]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 5), strs2[3]); TEST_ASSERT_EQUAL_STR (linked_list_get (list, 6), strs1[1]); delete_linked_list (list); }
int test_list_char() { linked_list_t *list; linked_list_cursor_t *cursor; int i; char *data; // create a new list list = linked_list_create( NULL, /* original list */ linked_list_char_cmp, /* default char comparator */ LINKED_LIST_SORTED /* sort list */ ); // add 5 entries for (i = 0; i < 5; i++) { data = (char *)malloc(10); sprintf(data, "item %i", i); linked_list_append(list, (void *)data, (void *)data); } // create a cursor for cycling/searching cursor = linked_list_cursor_create(NULL, list); // walk trough listh for (LINKED_LIST_FORLOOP(cursor, &data)) printf("%s\n", data); // scroll to item 4 printf("finding 'item 4'\n"); if (linked_list_cursor_find(cursor, "item 4") != 1) { printf("didn't find item item 4\n"); return; } printf("replacing item 'item 4' with 'item 4 (new)'\n"); // replace it linked_list_cursor_replace(cursor, "item 4", "item 4 (new)"); printf("done\n"); // show it again for (LINKED_LIST_FORLOOP(cursor, &data)) printf("%s\n", data); printf("deleting 'item 2'\n"); linked_list_cursor_find(cursor, "item 2"); linked_list_cursor_delete(cursor); // and again for (LINKED_LIST_FORLOOP(cursor, &data)) printf("%s\n", data); }
GBitmap* bitmaps_get_bitmap(uint32_t res_id) { if (! bitmaps) { return NULL; } AppBitmap* app_bmp = get_app_bitmap_by_res_id(res_id); if (app_bmp == NULL) { app_bmp = malloc(sizeof(AppBitmap)); app_bmp->res_id = res_id; app_bmp->bitmap = gbitmap_create_with_resource(app_bmp->res_id); app_bmp->group = 0; app_bmp->is_sub = false; linked_list_append(bitmaps, app_bmp); } return app_bmp->bitmap; }
void linked_list_insert(LinkedRoot* root, void* object, uint16_t after) { if (NULL == root) { return; } LinkedList* child = create_list_item(object); LinkedList* list = list_get(root, after); if (NULL == list) { linked_list_append(root, object); } else { child->next = list->next; list->next = child; child->prev = list; } }