static void test_push_min_minus_one() { int min = elts[count - 1]; int min_minus_one = min - 1; pqueue_push(&pqueue, &min_minus_one); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), elts[0]); }
static void test_push_min() { int min = elts[count - 1]; int elt = min; pqueue_push(&pqueue, &elt); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), elts[0]); }
/* Interactively put/extract queue items. */ int main(int argc, char *const argv[]) { int rc = 0, cap = 0; long lval = 0; queue_t q = 0; char input[MAX_INPUT] = "\0", *p = NULL; FILE *fp = stdin; if (argc < 2) { fprintf(stderr, "Usage: %s capacity\n", argv[0]); return 1; } cap = atoi(argv[1]); if (cap < MIN_CAP || cap > MAX_CAP) { fprintf(stderr, "%s: invalid capacity: %s, stay within %d..%d\n", argv[0], argv[1], MIN_CAP, MAX_CAP); return 1; } q = pqueue_create((size_t)cap); if (-1 == q) { perror("pqueue_create"); return 1; } setvbuf(stdout, (char*)NULL, _IOLBF, 0); printf("Q(%d/%d) ready\n", cap, (int)pqueue_count(q)); for (;;) { if (NULL == fgets(input, sizeof(input)-1, fp)) break; if (NULL != (p = strrchr(input, '\n'))) *p = 0; if (NULL != (p = strrchr(input, '\r'))) *p = 0; if (0 == strcasecmp(input, "get")) { errno = 0; lval = (long) pqueue_get(q); if (-1 == lval && errno) { perror("pqueue_get"); continue; } printf("Q(%d/%d) >> %ld\n", cap, (int)pqueue_count(q), lval); } else if (0 == strcasecmp("dump", input) || 0 == strcasecmp("list", input)) { pqueue_dump(q, stdout); } else { lval = atol(input); if (0 == lval && strcmp("0", input)) { printf("Invalid input, try again\n"); continue; } rc = pqueue_put(q, (void*)lval); if (rc) { perror("pqueue_put"); continue; } printf("Q(%d/%d) << %ld\n", cap, (int)pqueue_count(q), lval); } } pqueue_free(q); printf("%s: exiting with rc=%d\n", argv[0], rc); return rc; }
static void test_count() { assert_int_equal(pqueue_count(&pqueue), count); }