void prepare(int index) { q.clear(); r.clear(); fill_heap(q, index, size); fill_heap(r, index, size, size); }
int main () { int i; in = fopen ("billboard.in", "rt"); out = fopen ("billboard.out", "wt"); fscanf (in, "%d %d %d\n", &h, &w, &n); round_h = round_deg (h); fill_heap (h, w); for (i = 0; i < n; i++) { int _tmp; int sz; fscanf (in, "%d\n", &sz); fprintf (out, "%d\n", use (0, sz, &_tmp)); } fclose (in); fclose (out); return 0; }
void main(){ int i; int a[]={2,8}; int n=sizeof(a)/sizeof(a[0]); h=create_heap(); fill_heap(a,n); display_heap(); printf("Top element is %d\n",top()); insert(77); display_heap(); printf("Top element is %d",top()); pop(); display_heap(); printf("Top element is %d",top()); }
void cvs_conn(void) { do_auth(); fill_heap(); }
/* Check allocation failures for int arrays (without an element free function). */ static void test_int_fail (void) { /* Exercise failure in add/emplace. do_add: Use emplace (false) or add (true) to add elements. do_finalize: Perform finalization at the end (instead of free). */ for (int do_add = 0; do_add < 2; ++do_add) for (int do_finalize = 0; do_finalize < 2; ++do_finalize) { struct dynarray_int dyn; dynarray_int_init (&dyn); size_t count = 0; while (true) { if (do_add) { dynarray_int_add (&dyn, 0); if (dynarray_int_has_failed (&dyn)) break; } else { int *place = dynarray_int_emplace (&dyn); if (place == NULL) break; TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn)); *place = 0; } ++count; } printf ("info: %s: failure after %zu elements\n", __func__, count); TEST_VERIFY_EXIT (dynarray_int_has_failed (&dyn)); if (do_finalize) { struct int_array result = { (int *) (uintptr_t) -1, -1 }; TEST_VERIFY_EXIT (!dynarray_int_finalize (&dyn, &result)); TEST_VERIFY_EXIT (result.array == (int *) (uintptr_t) -1); TEST_VERIFY_EXIT (result.length == (size_t) -1); } else dynarray_int_free (&dyn); CHECK_INIT_STATE (int, &dyn); } /* Exercise failure in finalize. */ for (int do_add = 0; do_add < 2; ++do_add) { struct dynarray_int dyn; dynarray_int_init (&dyn); for (unsigned int i = 0; i < 10000; ++i) { if (do_add) { dynarray_int_add (&dyn, i); TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn)); } else { int *place = dynarray_int_emplace (&dyn); TEST_VERIFY_EXIT (place != NULL); *place = i; } } TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn)); struct heap_filler *heap_filler = fill_heap (); struct int_array result = { (int *) (uintptr_t) -1, -1 }; TEST_VERIFY_EXIT (!dynarray_int_finalize (&dyn, &result)); TEST_VERIFY_EXIT (result.array == (int *) (uintptr_t) -1); TEST_VERIFY_EXIT (result.length == (size_t) -1); CHECK_INIT_STATE (int, &dyn); free_fill_heap (heap_filler); } /* Exercise failure in resize. */ { struct dynarray_int dyn; dynarray_int_init (&dyn); struct heap_filler *heap_filler = fill_heap (); TEST_VERIFY (!dynarray_int_resize (&dyn, 1000)); TEST_VERIFY (dynarray_int_has_failed (&dyn)); free_fill_heap (heap_filler); dynarray_int_init (&dyn); TEST_VERIFY (dynarray_int_resize (&dyn, 1)); heap_filler = fill_heap (); TEST_VERIFY (!dynarray_int_resize (&dyn, 1000)); TEST_VERIFY (dynarray_int_has_failed (&dyn)); free_fill_heap (heap_filler); dynarray_int_init (&dyn); TEST_VERIFY (dynarray_int_resize (&dyn, 1000)); heap_filler = fill_heap (); TEST_VERIFY (!dynarray_int_resize (&dyn, 2000)); TEST_VERIFY (dynarray_int_has_failed (&dyn)); free_fill_heap (heap_filler); } }
/* Check allocation failures for char * arrays (which automatically free the pointed-to strings). */ static void test_str_fail (void) { /* Exercise failure in add/emplace. do_add: Use emplace (false) or add (true) to add elements. do_finalize: Perform finalization at the end (instead of free). */ for (int do_add = 0; do_add < 2; ++do_add) for (int do_finalize = 0; do_finalize < 2; ++do_finalize) { struct dynarray_str dyn; dynarray_str_init (&dyn); size_t count = 0; while (true) { char **place; if (do_add) { dynarray_str_add (&dyn, NULL); if (dynarray_str_has_failed (&dyn)) break; else place = dynarray_str_at (&dyn, dynarray_str_size (&dyn) - 1); } else { place = dynarray_str_emplace (&dyn); if (place == NULL) break; } TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn)); TEST_VERIFY_EXIT (*place == NULL); *place = strdup ("placeholder"); if (*place == NULL) { /* Second loop to wait for failure of dynarray_str_emplace. */ while (true) { if (do_add) { dynarray_str_add (&dyn, NULL); if (dynarray_str_has_failed (&dyn)) break; } else { char **place = dynarray_str_emplace (&dyn); if (place == NULL) break; TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn)); *place = NULL; } ++count; } break; } ++count; } printf ("info: %s: failure after %zu elements\n", __func__, count); TEST_VERIFY_EXIT (dynarray_str_has_failed (&dyn)); if (do_finalize) { struct str_array result = { (char **) (uintptr_t) -1, -1 }; TEST_VERIFY_EXIT (!dynarray_str_finalize (&dyn, &result)); TEST_VERIFY_EXIT (result.array == (char **) (uintptr_t) -1); TEST_VERIFY_EXIT (result.length == (size_t) -1); } else dynarray_str_free (&dyn); TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn)); TEST_VERIFY_EXIT (dyn.dynarray_header.array == dyn.scratch); TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == 0); TEST_VERIFY_EXIT (dyn.dynarray_header.allocated > 0); } /* Exercise failure in finalize. */ for (int do_add = 0; do_add < 2; ++do_add) { struct dynarray_str dyn; dynarray_str_init (&dyn); for (unsigned int i = 0; i < 1000; ++i) { if (do_add) dynarray_str_add (&dyn, xstrdup ("placeholder")); else { char **place = dynarray_str_emplace (&dyn); TEST_VERIFY_EXIT (place != NULL); TEST_VERIFY_EXIT (*place == NULL); *place = xstrdup ("placeholder"); } } TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn)); struct heap_filler *heap_filler = fill_heap (); struct str_array result = { (char **) (uintptr_t) -1, -1 }; TEST_VERIFY_EXIT (!dynarray_str_finalize (&dyn, &result)); TEST_VERIFY_EXIT (result.array == (char **) (uintptr_t) -1); TEST_VERIFY_EXIT (result.length == (size_t) -1); TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn)); TEST_VERIFY_EXIT (dyn.dynarray_header.array == dyn.scratch); TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == 0); TEST_VERIFY_EXIT (dyn.dynarray_header.allocated > 0); free_fill_heap (heap_filler); } /* Exercise failure in resize. */ { struct dynarray_str dyn; dynarray_str_init (&dyn); struct heap_filler *heap_filler = fill_heap (); TEST_VERIFY (!dynarray_str_resize (&dyn, 1000)); TEST_VERIFY (dynarray_str_has_failed (&dyn)); free_fill_heap (heap_filler); dynarray_str_init (&dyn); TEST_VERIFY (dynarray_str_resize (&dyn, 1)); *dynarray_str_at (&dyn, 0) = xstrdup ("allocated"); heap_filler = fill_heap (); TEST_VERIFY (!dynarray_str_resize (&dyn, 1000)); TEST_VERIFY (dynarray_str_has_failed (&dyn)); free_fill_heap (heap_filler); dynarray_str_init (&dyn); TEST_VERIFY (dynarray_str_resize (&dyn, 1000)); *dynarray_str_at (&dyn, 0) = xstrdup ("allocated"); heap_filler = fill_heap (); TEST_VERIFY (!dynarray_str_resize (&dyn, 2000)); TEST_VERIFY (dynarray_str_has_failed (&dyn)); free_fill_heap (heap_filler); } }