more robust id heap test; mix allocs and frees and churn aplenty
#define MAX_PAGE_ORDER 12
#define LENGTH_ORDER 16
+#define RANDOM_TEST_PASSES 100
static boolean basic_test(heap h)
{
@@ -50,7 +51,7 @@ static boolean basic_test(heap h)
}
#define VEC_LEN 64
-static boolean random_test(heap h, u64 page_order)
+static boolean random_test(heap h, u64 page_order, int churn)
{
int max_order = page_order + 4; /* up to 16 pages; arbitrary */
int alloc_size_vec[VEC_LEN];
@@ -71,10 +72,19 @@ static boolean random_test(heap h, u64 page_order)
msg_debug("*** allocated id heap %p at length %d (%d pages), pagesize %d\n",
id, length, length / pagesize, pagesize);
+ do {
+ int start;
+
+ start = random_u64() % VEC_LEN; /* change up start and end points */
for (int i=0; i < VEC_LEN; i++) {
- alloc_result_vec[i] = allocate_u64(id, alloc_size_vec[i]);
- if (alloc_result_vec[i] == INVALID_PHYSICAL) {
- msg_err("alloc of size %d failed\n", alloc_size_vec[i]);
+ int o = (i + start) % VEC_LEN;
+ if (alloc_result_vec[o])
+ continue;
+
+ alloc_result_vec[o] = allocate_u64(id, alloc_size_vec[o]);
+// msg_debug("alloc %d, size %d, result %P\n", o, alloc_size_vec[o], alloc_result_vec[o]);
+ if (alloc_result_vec[o] == INVALID_PHYSICAL) {
+ msg_err("alloc of size %d failed\n", alloc_size_vec[o]);
goto fail;
}
}
@@ -99,9 +109,19 @@ static boolean random_test(heap h, u64 page_order)
}
/* free */
+ start = random_u64() % VEC_LEN;
for (int i=0; i < VEC_LEN; i++) {
- deallocate_u64(id, alloc_result_vec[i], alloc_size_vec[i]);
+ /* selectively deallocate if multiple passes */
+ if (churn > 0 && (random_u64() & 0x1))
+ continue;
+ int o = (i + start) % VEC_LEN;
+ if (alloc_result_vec[o]) {
+// msg_debug("dealloc %d, size %d\n", o, alloc_size_vec[o]);
+ deallocate_u64(id, alloc_result_vec[o], alloc_size_vec[o]);
+ alloc_result_vec[o] = 0;
+ }
}
+ } while(churn-- > 0);
id->destroy(id);
@@ -122,7 +142,7 @@ int main(int argc, char **argv)
goto fail;
for (int order=0; order < 13; order++) {
- if (!random_test(h, order))
+ if (!random_test(h, order, RANDOM_TEST_PASSES))
goto fail;
}
