summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/btree2.c187
1 files changed, 178 insertions, 9 deletions
diff --git a/test/btree2.c b/test/btree2.c
index d86e2de..bcbff18 100644
--- a/test/btree2.c
+++ b/test/btree2.c
@@ -6509,6 +6509,166 @@ error:
/*-------------------------------------------------------------------------
+ * Function: test_remove_by_idx
+ *
+ * Purpose: Basic tests for the B-tree v2 code. This test inserts many
+ * records in random order, enough to make at a level 4 B-tree
+ * and then removes them all, by index.
+ *
+ * Return: Success: 0
+ * Failure: 1
+ *
+ * Programmer: Quincey Koziol
+ * Tuesday, November 14, 2006
+ *
+ *-------------------------------------------------------------------------
+ */
+static int
+test_remove_by_idx(hid_t fapl)
+{
+ hid_t file = -1;
+ char filename[1024];
+ H5F_t *f = NULL;
+ hsize_t record; /* Record to insert into tree */
+ hsize_t rrecord; /* Record to remove from tree */
+ haddr_t bt2_addr; /* Address of B-tree created */
+ haddr_t root_addr; /* Address of root of B-tree created */
+ time_t curr_time; /* Current time, for seeding random number generator */
+ hsize_t *records; /* Record #'s for random insertion */
+ unsigned u; /* Local index variable */
+ unsigned swap_idx; /* Location to swap with when shuffling */
+ unsigned rem_idx; /* Location to remove */
+ hsize_t temp_rec; /* Temporary record */
+ H5B2_stat_t bt2_stat; /* Statistics about B-tree created */
+ hsize_t nrec; /* Number of records in B-tree */
+
+ /* Initialize random number seed */
+ curr_time = HDtime(NULL);
+#ifndef QAK
+curr_time = 1163537969;
+HDfprintf(stderr, "curr_time = %lu\n", (unsigned long)curr_time);
+#endif /* QAK */
+ HDsrandom((unsigned long)curr_time);
+
+ /*
+ * Test removing many records into v2 B-tree
+ */
+ TESTING("B-tree remove: create random level 4 B-tree and delete all records by index");
+
+ /* Allocate space for the records */
+ if(NULL == (records = HDmalloc(sizeof(hsize_t) * INSERT_MANY)))
+ TEST_ERROR
+
+ /* Initialize record #'s */
+ for(u = 0; u < INSERT_MANY; u++)
+ records[u] = u;
+
+ /* Shuffle record #'s */
+ for(u = 0; u < INSERT_MANY; u++) {
+ swap_idx = (unsigned)(HDrandom() % (INSERT_MANY - u)) + u;
+ temp_rec = records[u];
+ records[u] = records[swap_idx];
+ records[swap_idx] = temp_rec;
+ } /* end for */
+
+ h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
+
+ /* Create the file to work on */
+ if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
+ STACK_ERROR
+
+ /* Get a pointer to the internal file object */
+ if(NULL == (f = H5I_object(file)))
+ STACK_ERROR
+
+ /*
+ * Create v2 B-tree
+ */
+ if(H5B2_create(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, 512, 8, 100, 40, &bt2_addr/*out*/) < 0)
+ FAIL_STACK_ERROR
+
+ /* Insert random records */
+ for(u = 0; u < INSERT_MANY; u++) {
+ record = records[u];
+ if(H5B2_insert(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &record) < 0)
+ FAIL_STACK_ERROR
+ } /* end for */
+
+ /* Check up on B-tree */
+ if(H5B2_stat_info(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &bt2_stat) < 0)
+ FAIL_STACK_ERROR
+ if(bt2_stat.depth != 4)
+ TEST_ERROR
+
+ /* Close file */
+ if(H5Fclose(file) < 0)
+ STACK_ERROR
+
+ /* Re-open the file */
+ if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
+ FAIL_STACK_ERROR
+
+ /* Get a pointer to the internal file object */
+ if(NULL == (f = H5I_object(file)))
+ FAIL_STACK_ERROR
+
+ /* Remove all records */
+ for(u = 0; u < INSERT_MANY; u++) {
+ /* Pick a record index to remove from randomly */
+ rem_idx = (unsigned)(HDrandom() % (INSERT_MANY - u));
+ rrecord = HSIZET_MAX;
+ if(H5B2_remove_by_idx(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, H5_ITER_INC, (hsize_t)rem_idx, remove_cb, &rrecord) < 0)
+ FAIL_STACK_ERROR
+
+ /* Make certain that the record value is correct */
+ if(rrecord >= INSERT_MANY)
+{
+HDfprintf(stderr, "u = %u\n", u);
+HDfprintf(stderr, "rem_idx = %u\n", rem_idx);
+HDfprintf(stderr, "rrecord = %Hu\n", rrecord);
+HDfprintf(stderr, "curr_time = %lu\n", (unsigned long)curr_time);
+ TEST_ERROR
+}
+
+ /* Query the number of records in the B-tree */
+ if(H5B2_get_nrec(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &nrec) < 0)
+ FAIL_STACK_ERROR
+
+ /* Make certain that the # of records is correct */
+ if(nrec != (INSERT_MANY - (u + 1)))
+ TEST_ERROR
+ } /* end for */
+
+ /* Query the address of the root node in the B-tree */
+ if(H5B2_get_root_addr_test(f, H5P_DATASET_XFER_DEFAULT, H5B2_TEST, bt2_addr, &root_addr) < 0)
+ FAIL_STACK_ERROR
+
+ /* Make certain that the address of the root node is defined */
+ if(H5F_addr_defined(root_addr))
+ TEST_ERROR
+
+ /* Close file */
+ if(H5Fclose(file) < 0)
+ STACK_ERROR
+
+ PASSED();
+
+ HDfree(records);
+
+ return 0;
+
+error:
+ H5E_BEGIN_TRY {
+ H5Fclose(file);
+ } H5E_END_TRY;
+
+ HDfree(records);
+
+ return 1;
+} /* test_remove_by_idx() */
+
+
+/*-------------------------------------------------------------------------
* Function: test_find_neighbor
*
* Purpose: Basic tests for the B-tree v2 code. This test exercises
@@ -7272,6 +7432,13 @@ main(void)
else
nerrors += test_remove_lots(fapl);
+#ifdef QAK
+ /* Test removing records by index */
+ nerrors += test_remove_by_idx(fapl);
+#else /* QAK */
+HDfprintf(stderr, "Uncomment tests!\n");
+#endif /* QAK */
+
/* Test more complex B-tree queries */
nerrors += test_find_neighbor(fapl);
@@ -7283,21 +7450,23 @@ main(void)
if(nerrors)
goto error;
+
puts("All v2 B-tree tests passed.");
+
h5_cleanup(FILENAME, fapl);
- }
+ } /* end if */
else
- {
puts("All v2 B-tree tests skipped - Incompatible with current Virtual File Driver");
- }
+
return 0;
- error:
- puts("*** TESTS FAILED ***");
- H5E_BEGIN_TRY {
- H5Pclose(fapl);
- } H5E_END_TRY;
- return 1;
+error:
+ puts("*** TESTS FAILED ***");
+ H5E_BEGIN_TRY {
+ H5Pclose(fapl);
+ } H5E_END_TRY;
+
+ return 1;
} /* end main() */