void test_queue_peek_tail(void) { Queue *queue; /* Check peeking into an empty queue */ queue = queue_new(); assert(queue_peek_tail(queue) == NULL); queue_free(queue); /* Pop off all the values from the queue, making sure that peek * has the correct value beforehand */ queue = generate_queue(); while (!queue_is_empty(queue)) { assert(queue_peek_tail(queue) == &variable1); assert(queue_pop_tail(queue) == &variable1); assert(queue_peek_tail(queue) == &variable2); assert(queue_pop_tail(queue) == &variable2); assert(queue_peek_tail(queue) == &variable3); assert(queue_pop_tail(queue) == &variable3); assert(queue_peek_tail(queue) == &variable4); assert(queue_pop_tail(queue) == &variable4); } assert(queue_peek_tail(queue) == NULL); queue_free(queue); }
void test_queue_is_empty(void) { Queue *queue; queue = queue_new(); assert(queue_is_empty(queue)); queue_push_head(queue, &variable1); assert(!queue_is_empty(queue)); queue_pop_head(queue); assert(queue_is_empty(queue)); queue_push_tail(queue, &variable1); assert(!queue_is_empty(queue)); queue_pop_tail(queue); assert(queue_is_empty(queue)); queue_free(queue); }
void *queue_remove(struct queue *queue, void *data) { struct list *entry; if ( !queue->head ) return NULL; entry = list_find(queue->head, data); if ( NULL == entry ) { // We didnt find this entry return NULL; } // This entry is there in the list if ( queue->head == entry ) { // This is the first node data = queue_pop_head(queue); return data; } else if ( queue->tail == entry ) { // This is the tail node data = queue_pop_tail(queue); return data; } // This is and arbitary location, between head and tail queue->length--; __list_remove(entry, entry); __list_free(entry); return data; }
int get_moving_average(Moving_Average *self, int data_entry_) { int *data_entry; //puts("created variable"); data_entry = (int *) malloc(sizeof(int)); //puts("allocated memory"); *data_entry = data_entry_; //printf("Set to %d", *data_entry); queue_push_head(self->data, data_entry); //puts("push to head successful"); self->data_sum += data_entry_; self->cur_length += 1; if (self->cur_length > self->max_length) { int *popped = (int *)queue_pop_tail(self->data); self->data_sum -= *popped; free(popped); self->cur_length -= 1; } self->data_avg = self->data_sum / self->cur_length; if (self->cur_length < (self->max_length / 8)) { //printf("%d\n", data_entry_); return data_entry_; } else { //printf("%d\n", self->data_avg); return self->data_avg; } }
void test_queue_push_head(void) { Queue *queue; int i; queue = queue_new(); /* Add some values */ for (i=0; i<1000; ++i) { queue_push_head(queue, &variable1); queue_push_head(queue, &variable2); queue_push_head(queue, &variable3); queue_push_head(queue, &variable4); } assert(!queue_is_empty(queue)); /* Check values come out of the tail properly */ assert(queue_pop_tail(queue) == &variable1); assert(queue_pop_tail(queue) == &variable2); assert(queue_pop_tail(queue) == &variable3); assert(queue_pop_tail(queue) == &variable4); /* Check values come back out of the head properly */ assert(queue_pop_head(queue) == &variable4); assert(queue_pop_head(queue) == &variable3); assert(queue_pop_head(queue) == &variable2); assert(queue_pop_head(queue) == &variable1); queue_free(queue); /* Test behavior when running out of memory. */ queue = queue_new(); alloc_test_set_limit(0); assert(!queue_push_head(queue, &variable1)); queue_free(queue); }
bool empty_queue(Queue *self) { if (!self) { return false; } while (!queue_is_empty(self)) { int *popped = queue_pop_tail(self); //printf("popped %d from queue\n", *popped); free(popped); } return true; }
void test_queue_pop_tail(void) { Queue *queue; /* Check popping off an empty queue */ queue = queue_new(); assert(queue_pop_tail(queue) == NULL); queue_free(queue); /* Pop off all the values from the queue */ queue = generate_queue(); while (!queue_is_empty(queue)) { assert(queue_pop_tail(queue) == &variable1); assert(queue_pop_tail(queue) == &variable2); assert(queue_pop_tail(queue) == &variable3); assert(queue_pop_tail(queue) == &variable4); } assert(queue_pop_tail(queue) == NULL); queue_free(queue); }