libtests/rbheap01: Simplify

Update #2417.
This commit is contained in:
Sebastian Huber
2015-09-11 10:21:15 +02:00
parent 808230add9
commit 5e4714b032

View File

@@ -1,8 +1,8 @@
/* /*
* Copyright (c) 2012 embedded brains GmbH. All rights reserved. * Copyright (c) 2012-2015 embedded brains GmbH. All rights reserved.
* *
* embedded brains GmbH * embedded brains GmbH
* Obere Lagerstr. 30 * Dornierstr. 4
* 82178 Puchheim * 82178 Puchheim
* Germany * Germany
* <rtems@embedded-brains.de> * <rtems@embedded-brains.de>
@@ -67,10 +67,13 @@ static uintptr_t idx(const rtems_rbheap_chunk *chunk)
} }
typedef struct { typedef struct {
const uintptr_t *index_current; uintptr_t index;
const uintptr_t *index_end; bool free;
const bool *free_current; } chunk_descriptor;
const bool *free_end;
typedef struct {
const chunk_descriptor *chunk_current;
const chunk_descriptor *chunk_end;
} chunk_visitor_context; } chunk_visitor_context;
static bool chunk_visitor( static bool chunk_visitor(
@@ -80,15 +83,14 @@ static bool chunk_visitor(
{ {
rtems_rbheap_chunk *chunk = rtems_rbheap_chunk_of_node(node); rtems_rbheap_chunk *chunk = rtems_rbheap_chunk_of_node(node);
chunk_visitor_context *context = visitor_arg; chunk_visitor_context *context = visitor_arg;
const chunk_descriptor *current = context->chunk_current;
rtems_test_assert(context->index_current != context->index_end); rtems_test_assert(current != context->chunk_end);
rtems_test_assert(context->free_current != context->free_end);
rtems_test_assert(idx(chunk) == *context->index_current); rtems_test_assert(idx(chunk) == current->index);
rtems_test_assert(rtems_rbheap_is_chunk_free(chunk) == *context->free_current); rtems_test_assert(rtems_rbheap_is_chunk_free(chunk) == current->free);
++context->index_current; context->chunk_current = current + 1;
++context->free_current;
return false; return false;
} }
@@ -159,17 +161,13 @@ static void test_init_empty_descriptors(void)
static void test_chunk_tree( static void test_chunk_tree(
const rtems_rbheap_control *control, const rtems_rbheap_control *control,
const uintptr_t *index_begin, const chunk_descriptor *chunk_begin,
const uintptr_t *index_end, size_t chunk_count
const bool *free_begin,
const bool *free_end
) )
{ {
chunk_visitor_context context = { chunk_visitor_context context = {
.index_current = index_begin, .chunk_current = chunk_begin,
.index_end = index_end, .chunk_end = chunk_begin + chunk_count
.free_current = free_begin,
.free_end = free_end
}; };
_RBTree_Iterate( _RBTree_Iterate(
@@ -179,22 +177,17 @@ static void test_chunk_tree(
); );
} }
#define TEST_PAGE_TREE(control, indices, frees) \ #define TEST_PAGE_TREE(control, chunks) \
test_chunk_tree( \ test_chunk_tree( \
control, \ control, \
indices, \ chunks, \
&indices [sizeof(indices) / sizeof(indices [0])], \ RTEMS_ARRAY_SIZE(chunks) \
frees, \
&frees [sizeof(frees) / sizeof(frees [0])] \
) )
static void test_init_successful(rtems_rbheap_control *control) static void test_init_successful(rtems_rbheap_control *control)
{ {
static const uintptr_t indices [] = { static const chunk_descriptor chunks [] = {
0 { 0, true }
};
static const bool frees [] = {
true
}; };
rtems_status_code sc = RTEMS_SUCCESSFUL; rtems_status_code sc = RTEMS_SUCCESSFUL;
@@ -209,24 +202,17 @@ static void test_init_successful(rtems_rbheap_control *control)
); );
rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert(sc == RTEMS_SUCCESSFUL);
TEST_PAGE_TREE(control, indices, frees); TEST_PAGE_TREE(control, chunks);
} }
static void test_alloc_and_free_one(void) static void test_alloc_and_free_one(void)
{ {
static const uintptr_t indices_0 [] = { static const chunk_descriptor chunks_0 [] = {
0, { 0, true },
PAGE_COUNT - 1 { PAGE_COUNT - 1, false }
}; };
static const bool frees_0 [] = { static const chunk_descriptor chunks_1 [] = {
true, { 0, true }
false
};
static const uintptr_t indices_1 [] = {
0
};
static const bool frees_1 [] = {
true,
}; };
rtems_status_code sc = RTEMS_SUCCESSFUL; rtems_status_code sc = RTEMS_SUCCESSFUL;
@@ -238,21 +224,18 @@ static void test_alloc_and_free_one(void)
ptr = rtems_rbheap_allocate(&control, PAGE_SIZE); ptr = rtems_rbheap_allocate(&control, PAGE_SIZE);
rtems_test_assert(ptr != NULL); rtems_test_assert(ptr != NULL);
TEST_PAGE_TREE(&control, indices_0, frees_0); TEST_PAGE_TREE(&control, chunks_0);
sc = rtems_rbheap_free(&control, ptr); sc = rtems_rbheap_free(&control, ptr);
rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert(sc == RTEMS_SUCCESSFUL);
TEST_PAGE_TREE(&control, indices_1, frees_1); TEST_PAGE_TREE(&control, chunks_1);
} }
static void test_alloc_zero(void) static void test_alloc_zero(void)
{ {
static const uintptr_t indices [] = { static const chunk_descriptor chunks [] = {
0 { 0, true }
};
static const bool frees [] = {
true
}; };
rtems_rbheap_control control; rtems_rbheap_control control;
@@ -263,16 +246,13 @@ static void test_alloc_zero(void)
ptr = rtems_rbheap_allocate(&control, 0); ptr = rtems_rbheap_allocate(&control, 0);
rtems_test_assert(ptr == NULL); rtems_test_assert(ptr == NULL);
TEST_PAGE_TREE(&control, indices, frees); TEST_PAGE_TREE(&control, chunks);
} }
static void test_alloc_huge_chunk(void) static void test_alloc_huge_chunk(void)
{ {
static const uintptr_t indices [] = { static const chunk_descriptor chunks [] = {
0 { 0, true }
};
static const bool frees [] = {
true
}; };
rtems_rbheap_control control; rtems_rbheap_control control;
@@ -283,22 +263,16 @@ static void test_alloc_huge_chunk(void)
ptr = rtems_rbheap_allocate(&control, (PAGE_COUNT + 1) * PAGE_SIZE); ptr = rtems_rbheap_allocate(&control, (PAGE_COUNT + 1) * PAGE_SIZE);
rtems_test_assert(ptr == NULL); rtems_test_assert(ptr == NULL);
TEST_PAGE_TREE(&control, indices, frees); TEST_PAGE_TREE(&control, chunks);
} }
static void test_alloc_one_chunk(void) static void test_alloc_one_chunk(void)
{ {
static const uintptr_t indices_0 [] = { static const chunk_descriptor chunks_0 [] = {
0 { 0, false }
}; };
static const bool frees_0 [] = { static const chunk_descriptor chunks_1 [] = {
false { 0, true },
};
static const uintptr_t indices_1 [] = {
0
};
static const bool frees_1 [] = {
true,
}; };
rtems_status_code sc = RTEMS_SUCCESSFUL; rtems_status_code sc = RTEMS_SUCCESSFUL;
@@ -310,41 +284,28 @@ static void test_alloc_one_chunk(void)
ptr = rtems_rbheap_allocate(&control, PAGE_COUNT * PAGE_SIZE); ptr = rtems_rbheap_allocate(&control, PAGE_COUNT * PAGE_SIZE);
rtems_test_assert(ptr != NULL); rtems_test_assert(ptr != NULL);
TEST_PAGE_TREE(&control, indices_0, frees_0); TEST_PAGE_TREE(&control, chunks_0);
sc = rtems_rbheap_free(&control, ptr); sc = rtems_rbheap_free(&control, ptr);
rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert(sc == RTEMS_SUCCESSFUL);
TEST_PAGE_TREE(&control, indices_1, frees_1); TEST_PAGE_TREE(&control, chunks_1);
} }
static void test_alloc_many_chunks(void) static void test_alloc_many_chunks(void)
{ {
static const uintptr_t indices_0 [] = { static const chunk_descriptor chunks_0 [] = {
0, { 0, false },
1, { 1, false },
2, { 2, false },
3, { 3, false },
4, { 4, false },
5, { 5, false },
6, { 6, false },
7 { 7, false }
}; };
static const bool frees_0 [] = { static const chunk_descriptor chunks_1 [] = {
false, { 0, true }
false,
false,
false,
false,
false,
false,
false
};
static const uintptr_t indices_1 [] = {
0
};
static const bool frees_1 [] = {
true,
}; };
rtems_status_code sc = RTEMS_SUCCESSFUL; rtems_status_code sc = RTEMS_SUCCESSFUL;
@@ -360,19 +321,19 @@ static void test_alloc_many_chunks(void)
rtems_test_assert(ptr [i] != NULL); rtems_test_assert(ptr [i] != NULL);
} }
TEST_PAGE_TREE(&control, indices_0, frees_0); TEST_PAGE_TREE(&control, chunks_0);
null = rtems_rbheap_allocate(&control, PAGE_SIZE); null = rtems_rbheap_allocate(&control, PAGE_SIZE);
rtems_test_assert(null == NULL); rtems_test_assert(null == NULL);
TEST_PAGE_TREE(&control, indices_0, frees_0); TEST_PAGE_TREE(&control, chunks_0);
for (i = 0; i < PAGE_COUNT; ++i) { for (i = 0; i < PAGE_COUNT; ++i) {
sc = rtems_rbheap_free(&control, ptr [i]); sc = rtems_rbheap_free(&control, ptr [i]);
rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert(sc == RTEMS_SUCCESSFUL);
} }
TEST_PAGE_TREE(&control, indices_1, frees_1); TEST_PAGE_TREE(&control, chunks_1);
} }
static void test_alloc_misaligned(void) static void test_alloc_misaligned(void)
@@ -464,81 +425,43 @@ enum {
static void test_free_merge_left_or_right(bool left) static void test_free_merge_left_or_right(bool left)
{ {
static const uintptr_t indices_0 [] = { static const chunk_descriptor chunks_0 [] = {
0, { 0, true },
3, { 3, false },
4, { 4, false },
5, { 5, false },
6, { 6, false },
7 { 7, false }
}; };
static const bool frees_0 [] = { static const chunk_descriptor chunks_1_left [] = {
true, { 0, true },
false, { 3, false },
false, { 4, true },
false, { 5, false },
false, { 6, false },
false { 7, false }
}; };
static const uintptr_t indices_1_left [] = { static const chunk_descriptor chunks_1_right [] = {
0, { 0, true },
3, { 3, false },
4, { 4, false },
5, { 5, false },
6, { 6, true },
7 { 7, false }
}; };
static const bool frees_1_left [] = { static const chunk_descriptor chunks_2_left [] = {
true, { 0, true },
false, { 3, false },
true, { 4, true },
false, { 6, false },
false, { 7, false }
false
}; };
static const uintptr_t indices_1_right [] = { static const chunk_descriptor chunks_2_right [] = {
0, { 0, true },
3, { 3, false },
4, { 4, false },
5, { 5, true },
6, { 7, false }
7
};
static const bool frees_1_right [] = {
true,
false,
false,
false,
true,
false
};
static const uintptr_t indices_2_left [] = {
0,
3,
4,
6,
7
};
static const bool frees_2_left [] = {
true,
false,
true,
false,
false
};
static const uintptr_t indices_2_right [] = {
0,
3,
4,
5,
7
};
static const bool frees_2_right [] = {
true,
false,
false,
true,
false
}; };
rtems_status_code sc = RTEMS_SUCCESSFUL; rtems_status_code sc = RTEMS_SUCCESSFUL;
@@ -554,24 +477,24 @@ static void test_free_merge_left_or_right(bool left)
rtems_test_assert(ptr [i] != NULL); rtems_test_assert(ptr [i] != NULL);
} }
TEST_PAGE_TREE(&control, indices_0, frees_0); TEST_PAGE_TREE(&control, chunks_0);
sc = rtems_rbheap_free(&control, ptr [dir]); sc = rtems_rbheap_free(&control, ptr [dir]);
rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert(sc == RTEMS_SUCCESSFUL);
if (left) { if (left) {
TEST_PAGE_TREE(&control, indices_1_left, frees_1_left); TEST_PAGE_TREE(&control, chunks_1_left);
} else { } else {
TEST_PAGE_TREE(&control, indices_1_right, frees_1_right); TEST_PAGE_TREE(&control, chunks_1_right);
} }
sc = rtems_rbheap_free(&control, ptr [MIDDLE]); sc = rtems_rbheap_free(&control, ptr [MIDDLE]);
rtems_test_assert(sc == RTEMS_SUCCESSFUL); rtems_test_assert(sc == RTEMS_SUCCESSFUL);
if (left) { if (left) {
TEST_PAGE_TREE(&control, indices_2_left, frees_2_left); TEST_PAGE_TREE(&control, chunks_2_left);
} else { } else {
TEST_PAGE_TREE(&control, indices_2_right, frees_2_right); TEST_PAGE_TREE(&control, chunks_2_right);
} }
} }