static char *test_q_is_empty() { packet *q = q_create(); packet p = pkt_create(ERROR, 0, 0, 0, 0); mu_assert("FAIL: test_q_is_empty [1]", q_is_empty(0, 0, q, N)); q_write(p, 0, q, N); mu_assert("FAIL: test_q_is_empty [2]", !q_is_empty(0, 0, q, N)); q_read(&p, 0, q, N); mu_assert("FAIL: test_q_is_empty [3]", q_is_empty(0, 0, q, N)); q_destroy(q); return NULL; }
/* Atiende al cliente que actualmente esta usando al servidor * * PRE: !q_is_empty(q) ("hay al menos un cliente en el servidor") * *tsal == tiempo (absoluto) de salida del servidor del cliente actual * * busy = serve_customer (q, &tsal, &wtime) * * POS: *wtime == tiempo total que estuvo en el sistema * el cliente que acaba de ser atendido * * busy => *tsal == tiempo (absoluto) de salida del servidor * del proximo cliente * * !busy => el servidor se quedo sin clientes * */ static bool serve_customer (queue_t q, double *tsal, double *wtime) { bool busy = true; assert (!q_is_empty(q)); /* Sacamos al cliente del servidor y registramos el tiempo total * que estuvo dentro del sistema */ *wtime = *tsal - q_dequeue (q); if (! q_is_empty(q) ) /* Generamos un tiempo (absoluto) de salida del servidor para * el proximo cliente que sera atendido */ *tsal = *tsal + gen_exp (Ts); else busy = false; return busy; }
/* Quita el primer elemento de la cola global 'q', * y devuelve su tiempo absoluto de arribo al sistema 'ta' * PRE: ! q_is_empty () */ double q_dequeue (queue_t q) { assert(!q_is_empty(q)); q->f = (q->f + 1) % q->n; if (q->f == 0) return q->q[q->n-1]; else return q->q[q->f-1]; }
int q_peek(QUEUE *p_q, ELEMENT_TYPE *p_a) { if (p_q == NULL || p_a == NULL) return -1; if (q_is_empty(p_q)) return -1; else { *p_a = p_q->p_data[p_q->front]; return 0; } }
static char *test_q_write() { packet *q = q_create(); packet p = pkt_create(DATA, 1, 2, 3, 4); q_write(p, 0, q, N); mu_assert("FAIL: test_q_write [1]", !q_is_empty(0, 0, q, N)); q_write(p, 0, q, N); mu_assert("FAIL: test_q_write [2]", q_size(0, 0, q, N) == 2); for (int i = 0; i < 14; i++) { q_write(p, 0, q, N); } mu_assert("FAIL: test_q_write [3]", !q_write(p, 0, q, N)); q_destroy(q); return NULL; }
int q_dequeue(QUEUE *p_q, ELEMENT_TYPE *p_a) { if (p_q == NULL || p_a == NULL) return -1; if (q_is_empty(p_q)) return -1; else { *p_a = p_q->p_data[p_q->front]; p_q->p_data[p_q->front] = 0; p_q->front = ++p_q->front % p_q->size; return 0; } }
/* Devuelve el tiempo de arribo absoluto al sistema * del cliente que actualmente esta siendo atendido en el servidor * * PRE: ! q_is_empty (q) */ double q_first (queue_t q) { assert(!q_is_empty(q)); return q->q[q->f]; }
int main(int argc, char *argv[]) { size_t s = sizeof(int); int foo; { /*#Test_0*/ queue q; q_init(&q, s); test(q_size(&q) == 0 && q_is_empty(&q)); } { /*#Test_1*/ queue q; q_init(&q, s); foo = 1; q_push(&q, &foo); test(q_size(&q) == 1 && !q_is_empty(&q) && *(int *)q_front(&q) == 1); } { /*#Test_2*/ queue q; q_init(&q, s); foo = 2; q_push(&q, &foo); foo = 1; q_push(&q, &foo); test( q_size(&q) == 2 && *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 2); } { /*#Test_3*/ queue q; q_init(&q, s); foo = 3; q_push(&q, &foo); foo = 2; q_push(&q, &foo); foo = 1; q_push(&q, &foo); q_pop(&q); BOOL bar = *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 2; q_pop(&q); bar &= *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 1; q_pop(&q); test( bar && q_is_empty(&q)); } { /*#Test_4*/ queue q; q_init(&q, s); BOOL bar = TRUE; for (uint i = 0; i < N; ++i) { foo = i; q_push(&q, &foo); } for (uint i = 0; i < N; ++i) { bar &= *(int *)q_back(&q) == i; if (!bar) break; q_pop(&q); } test( bar& q_is_empty(&q)); } return EXIT_SUCCESS; }
char * test_not_empty(void) { mu_assert("init", q_init() == q_success); mu_assert("insert", q_insert(5) == q_success); mu_assert("is_empty", q_is_empty() == 0); return NULL; }
void cond_variable_signal(cond_variable * c) { if (!q_is_empty(c->q)) { kill(deque(c->q), 1); } }