summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorguozhanxin <[email protected]>2021-06-03 14:57:26 +0800
committerguozhanxin <[email protected]>2021-06-03 15:00:13 +0800
commit2c119a5c481d16c416358f62bd5fde8210a96c46 (patch)
tree04cd1b6b34a041bb8d49ed3af324bd7169a9476f /examples
parentaa13a78b3ffa30b1bdcfcc734a2be6874b2e53c6 (diff)
add memheap testcase for ac6 Oz optimization.
Diffstat (limited to 'examples')
-rw-r--r--examples/utest/testcases/Kconfig1
-rw-r--r--examples/utest/testcases/kernel/Kconfig8
-rw-r--r--examples/utest/testcases/kernel/SConscript15
-rw-r--r--examples/utest/testcases/kernel/memheap_tc.c100
-rw-r--r--examples/utest/testcases/utest/pass_tc.c2
5 files changed, 125 insertions, 1 deletions
diff --git a/examples/utest/testcases/Kconfig b/examples/utest/testcases/Kconfig
index da80eb30ae..a9b556f1e3 100644
--- a/examples/utest/testcases/Kconfig
+++ b/examples/utest/testcases/Kconfig
@@ -8,6 +8,7 @@ config RT_USING_UTESTCASES
if RT_USING_UTESTCASES
source "$RTT_DIR/examples/utest/testcases/utest/Kconfig"
+source "$RTT_DIR/examples/utest/testcases/kernel/Kconfig"
endif
diff --git a/examples/utest/testcases/kernel/Kconfig b/examples/utest/testcases/kernel/Kconfig
new file mode 100644
index 0000000000..14334af16f
--- /dev/null
+++ b/examples/utest/testcases/kernel/Kconfig
@@ -0,0 +1,8 @@
+menu "Kernel Testcase"
+
+config UTEST_MEMHEAP_TC
+ bool "memheap stability test"
+ default y
+ depends on RT_USING_MEMHEAP
+
+endmenu
diff --git a/examples/utest/testcases/kernel/SConscript b/examples/utest/testcases/kernel/SConscript
new file mode 100644
index 0000000000..9411cdd9a0
--- /dev/null
+++ b/examples/utest/testcases/kernel/SConscript
@@ -0,0 +1,15 @@
+Import('rtconfig')
+from building import *
+
+cwd = GetCurrentDir()
+src = Split('''
+''')
+
+if GetDepend(['UTEST_MEMHEAP_TC']):
+ src += ['memheap_tc.c']
+
+CPPPATH = [cwd]
+
+group = DefineGroup('utestcases', src, depend = [], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/examples/utest/testcases/kernel/memheap_tc.c b/examples/utest/testcases/kernel/memheap_tc.c
new file mode 100644
index 0000000000..ee52bdb185
--- /dev/null
+++ b/examples/utest/testcases/kernel/memheap_tc.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2019-01-16 flybreak the first version
+ */
+
+#include <rtthread.h>
+#include <stdlib.h>
+#include "utest.h"
+
+#define HEAP_SIZE (64 * 1024)
+#define HEAP_ALIGN (4)
+#define SLICE_NUM (40)
+#define TEST_TIMES (100000)
+#define HEAP_NAME "heap1"
+#define SLICE_SIZE_MAX (HEAP_SIZE/SLICE_NUM)
+
+static void memheap_test(void)
+{
+ struct rt_memheap heap1;
+ rt_uint32_t ptr_start;
+ void *ptr[SLICE_NUM];
+ int i, cnt = 0;
+
+ /* init heap */
+ ptr_start = (rt_uint32_t)rt_malloc_align(HEAP_SIZE, HEAP_ALIGN);
+ if (ptr_start == RT_NULL)
+ {
+ rt_kprintf("totle size too big,can not malloc memory!");
+ return;
+ }
+
+ rt_memheap_init(&heap1, HEAP_NAME, (void *)ptr_start, HEAP_SIZE);
+
+ /* test start */
+ for (i = 0; i < SLICE_NUM; i++)
+ {
+ ptr[i] = 0;
+ }
+ /* test alloc */
+ for (i = 0; i < SLICE_NUM; i++)
+ {
+ rt_uint32_t slice_size = rand() % SLICE_SIZE_MAX;
+ ptr[i] = rt_memheap_alloc(&heap1, slice_size);
+ }
+ /* test realloc */
+ while (cnt < TEST_TIMES)
+ {
+ rt_uint32_t slice_size = rand() % SLICE_SIZE_MAX;
+ rt_uint32_t ptr_index = rand() % SLICE_NUM;
+ rt_uint32_t operation = rand() % 2;
+
+ if (ptr[ptr_index])
+ {
+ if (operation == 0) /* free and malloc */
+ {
+ if (ptr[ptr_index])
+ {
+ rt_memheap_free(ptr[ptr_index]);
+ }
+ ptr[ptr_index] = rt_memheap_alloc(&heap1, slice_size);
+ }
+ else /* realloc */
+ {
+ ptr[ptr_index] = rt_memheap_realloc(&heap1, ptr[ptr_index], slice_size);
+ }
+ }
+ cnt ++;
+ if (cnt % (TEST_TIMES / 10) == 0)
+ {
+ rt_kprintf(">");
+ }
+ }
+
+ rt_kprintf("test OK!\n");
+
+ /* test end */
+ rt_memheap_detach(&heap1);
+ rt_free_align((void *)ptr_start);
+}
+
+static rt_err_t utest_tc_init(void)
+{
+ return RT_EOK;
+}
+
+static rt_err_t utest_tc_cleanup(void)
+{
+ return RT_EOK;
+}
+
+static void testcase(void)
+{
+ UTEST_UNIT_RUN(memheap_test);
+}
+UTEST_TC_EXPORT(testcase, "testcases.kernel.memheap_tc", utest_tc_init, utest_tc_cleanup, 10);
diff --git a/examples/utest/testcases/utest/pass_tc.c b/examples/utest/testcases/utest/pass_tc.c
index 8e93e35b9b..c9ee912fbf 100644
--- a/examples/utest/testcases/utest/pass_tc.c
+++ b/examples/utest/testcases/utest/pass_tc.c
@@ -43,4 +43,4 @@ static void testcase(void)
{
UTEST_UNIT_RUN(test_assert_pass);
}
-UTEST_TC_EXPORT(testcase, "testcases.utest.pass_tc", utest_tc_init, utest_tc_cleanup, 10); \ No newline at end of file
+UTEST_TC_EXPORT(testcase, "testcases.utest.pass_tc", utest_tc_init, utest_tc_cleanup, 10);