This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 8361fa309 testing/testsuites: add mempool coverage cases
8361fa309 is described below

commit 8361fa3092d5972d6ec61e32ddcce11a7fb0d713
Author: anjiahao <[email protected]>
AuthorDate: Thu Nov 26 20:37:11 2026 +0800

    testing/testsuites: add mempool coverage cases
    
    Add three mm test cases:
    - mempool initialization, expansion, throttling, waiting, record
      iterators and the /proc/mempool info/dump commands.
    - mempool record and iterator paths.
    - mempool_multiple in pool-count, dictionary, binary-search and chunk
      modes, including allocation failure handling.
    
    Register the cases in the mm test suite.
    
    Signed-off-by: anjiahao <[email protected]>
---
 testing/testsuites/kernel/mm/cases/mm_test_010.c | 158 +++++++++++++++++++++++
 testing/testsuites/kernel/mm/cases/mm_test_011.c | 122 +++++++++++++++++
 testing/testsuites/kernel/mm/cases/mm_test_012.c | 129 ++++++++++++++++++
 testing/testsuites/kernel/mm/cmocka_mm_test.c    |   6 +
 testing/testsuites/kernel/mm/include/MmTest.h    |  15 +++
 5 files changed, 430 insertions(+)

diff --git a/testing/testsuites/kernel/mm/cases/mm_test_010.c 
b/testing/testsuites/kernel/mm/cases/mm_test_010.c
new file mode 100644
index 000000000..9fef9f77a
--- /dev/null
+++ b/testing/testsuites/kernel/mm/cases/mm_test_010.c
@@ -0,0 +1,158 @@
+/****************************************************************************
+ * apps/testing/testsuites/kernel/mm/cases/mm_test_010.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <errno.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cmocka.h>
+
+#include <nuttx/mm/mempool.h>
+#include <nuttx/mm/mm.h>
+
+#include "MmTest.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MM10_BLKSIZE   32
+#define MM10_PREALLOC  4
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Static pool with four preallocated blocks.  maxalloc stops the pool
+ * from growing, so the next allocation after all blocks are taken must
+ * fail.
+ */
+
+MEMPOOL_DEFINE(g_mm10_static, MM10_BLKSIZE, MM10_PREALLOC, MM10_PREALLOC,
+               0);
+
+/* Dynamic pool without a static buffer: the initial blocks are allocated
+ * from the user heap by mempool_init().
+ */
+
+static struct mempool_s g_mm10_dyn =
+{
+  .initialbase = NULL,
+  .initialsize = MM10_PREALLOC * MEMPOOL_REALBLOCKSIZE(MM10_BLKSIZE) +
+                 MEMPOOL_HEADER_SIZE,
+  .blocksize   = MM10_BLKSIZE,
+  .expandsize  = 0,
+  .maxalloc    = 0,
+  .wait        = false,
+  .name        = "mmtest_mm10",
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_nuttx_mm10
+ ****************************************************************************/
+
+void test_nuttx_mm10(FAR void **state)
+{
+  FAR void *blk[MM10_PREALLOC];
+  FAR void *extra;
+  struct mempoolinfo_s info;
+  int i;
+  int j;
+
+  /* Static pool: take every block, then hit the maxalloc limit. */
+
+  assert_int_equal(mempool_init(&g_mm10_static), 0);
+  assert_int_equal(mempool_info(&g_mm10_static, &info), 0);
+  assert_int_equal(info.ordblks, MM10_PREALLOC);
+  assert_int_equal(info.aordblks, 0);
+  assert_int_equal(info.sizeblks,
+                   MEMPOOL_REALBLOCKSIZE(MM10_BLKSIZE));
+
+  for (i = 0; i < MM10_PREALLOC; i++)
+    {
+      blk[i] = mempool_allocate(&g_mm10_static, 0);
+      assert_non_null(blk[i]);
+    }
+
+  assert_int_equal(mempool_navail(&g_mm10_static), 0);
+
+  extra = mempool_allocate(&g_mm10_static, 0);
+  assert_null(extra);
+
+  for (i = 0; i < MM10_PREALLOC; i++)
+    {
+      mempool_release(&g_mm10_static, blk[i]);
+    }
+
+  assert_int_equal(mempool_navail(&g_mm10_static), MM10_PREALLOC);
+  assert_int_equal(mempool_deinit(&g_mm10_static), 0);
+
+  /* Dynamic pool: the initial blocks come from the heap. */
+
+  assert_int_equal(mempool_init(&g_mm10_dyn), 0);
+  assert_int_equal(mempool_info(&g_mm10_dyn, &info), 0);
+  assert_int_equal(info.ordblks, MM10_PREALLOC);
+  assert_int_equal(info.aordblks, 0);
+
+  for (i = 0; i < MM10_PREALLOC; i++)
+    {
+      blk[i] = mempool_zallocate(&g_mm10_dyn, 0);
+      assert_non_null(blk[i]);
+    }
+
+  for (i = 0; i < MM10_PREALLOC; i++)
+    {
+      for (j = 0; j < MM10_BLKSIZE; j++)
+        {
+          assert_int_equal(((FAR uint8_t *)blk[i])[j], 0);
+        }
+    }
+
+  /* The pool cannot expand and does not wait. */
+
+  extra = mempool_allocate(&g_mm10_dyn, 0);
+  assert_null(extra);
+
+  /* Deinit is refused while blocks are in use. */
+
+  assert_int_equal(mempool_deinit(&g_mm10_dyn), -EBUSY);
+
+  for (i = 0; i < MM10_PREALLOC; i++)
+    {
+      mempool_release(&g_mm10_dyn, blk[i]);
+    }
+
+  assert_int_equal(mempool_deinit(&g_mm10_dyn), 0);
+}
diff --git a/testing/testsuites/kernel/mm/cases/mm_test_011.c 
b/testing/testsuites/kernel/mm/cases/mm_test_011.c
new file mode 100644
index 000000000..82b1b762a
--- /dev/null
+++ b/testing/testsuites/kernel/mm/cases/mm_test_011.c
@@ -0,0 +1,122 @@
+/****************************************************************************
+ * apps/testing/testsuites/kernel/mm/cases/mm_test_011.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <errno.h>
+#include <malloc.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <cmocka.h>
+
+#include <nuttx/mm/mempool.h>
+
+#include "MmTest.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MM11_CHUNKSIZE   4
+#define MM11_EXPANDSIZE  4096
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static FAR void *mm11_alloc(FAR void *arg, size_t alignment, size_t size)
+{
+  return memalign(alignment, size);
+}
+
+static void mm11_free(FAR void *arg, FAR void *addr)
+{
+  free(addr);
+}
+
+static size_t mm11_alloc_size(FAR void *arg, FAR void *addr)
+{
+  return malloc_size(addr);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_nuttx_mm11
+ ****************************************************************************/
+
+void test_nuttx_mm11(FAR void **state)
+{
+  static const size_t poolsize[] =
+  {
+    16, 32, 64
+  };
+
+  FAR struct mempool_multiple_s *mpool;
+  FAR void *blk;
+  FAR char outside[8];
+  ssize_t size;
+
+  mpool = mempool_multiple_init("mmtest_mm11", poolsize,
+                                sizeof(poolsize) / sizeof(poolsize[0]),
+                                mm11_alloc, mm11_alloc_size, mm11_free,
+                                NULL, MM11_CHUNKSIZE, MM11_EXPANDSIZE,
+                                MM11_CHUNKSIZE);
+  assert_non_null(mpool);
+
+  /* A small request is served by the smallest pool. */
+
+  blk = mempool_multiple_alloc(mpool, 8);
+  assert_non_null(blk);
+  size = mempool_multiple_alloc_size(mpool, blk);
+  assert_int_equal(size, 16);
+  assert_true(mempool_multiple_member(mpool, blk));
+  assert_false(mempool_multiple_member(mpool, outside));
+  assert_int_equal(mempool_multiple_free(mpool, blk), 0);
+
+  /* Larger requests move to the matching block size. */
+
+  blk = mempool_multiple_alloc(mpool, 20);
+  assert_non_null(blk);
+  assert_int_equal(mempool_multiple_alloc_size(mpool, blk), 32);
+  assert_int_equal(mempool_multiple_free(mpool, blk), 0);
+
+  blk = mempool_multiple_alloc(mpool, 50);
+  assert_non_null(blk);
+  assert_int_equal(mempool_multiple_alloc_size(mpool, blk), 64);
+  assert_int_equal(mempool_multiple_free(mpool, blk), 0);
+
+  /* Releasing a foreign pointer is refused. */
+
+  assert_int_equal(mempool_multiple_free(mpool, outside), -EINVAL);
+
+  mempool_multiple_deinit(mpool);
+}
diff --git a/testing/testsuites/kernel/mm/cases/mm_test_012.c 
b/testing/testsuites/kernel/mm/cases/mm_test_012.c
new file mode 100644
index 000000000..d3bf0f7f4
--- /dev/null
+++ b/testing/testsuites/kernel/mm/cases/mm_test_012.c
@@ -0,0 +1,129 @@
+/****************************************************************************
+ * apps/testing/testsuites/kernel/mm/cases/mm_test_012.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <malloc.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <cmocka.h>
+
+#include <nuttx/mm/mempool.h>
+#include <nuttx/mm/mm.h>
+
+#include "MmTest.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MM12_BLKSIZE   64
+#define MM12_PREALLOC  6
+#define MM12_NALLOC    2
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct mempool_s g_mm12_pool =
+{
+  .initialbase = NULL,
+  .initialsize = MM12_PREALLOC * MEMPOOL_REALBLOCKSIZE(MM12_BLKSIZE) +
+                 MEMPOOL_HEADER_SIZE,
+  .blocksize   = MM12_BLKSIZE,
+  .expandsize  = 0,
+  .maxalloc    = 0,
+  .wait        = false,
+  .name        = "mmtest_mm12",
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_nuttx_mm12
+ ****************************************************************************/
+
+void test_nuttx_mm12(FAR void **state)
+{
+  FAR void *blk[MM12_NALLOC];
+  struct mempoolinfo_s info;
+  struct mallinfo_task taskinfo;
+  struct malltask task;
+  struct mm_memdump_s dump;
+  int i;
+
+  assert_int_equal(mempool_init(&g_mm12_pool), 0);
+
+  for (i = 0; i < MM12_NALLOC; i++)
+    {
+      blk[i] = mempool_allocate(&g_mm12_pool, 0);
+      assert_non_null(blk[i]);
+    }
+
+  /* mempool_info reports the current allocation counters. */
+
+  assert_int_equal(mempool_info(&g_mm12_pool, &info), 0);
+  assert_int_equal(info.aordblks, MM12_NALLOC);
+  assert_int_equal(info.ordblks, MM12_PREALLOC - MM12_NALLOC);
+  assert_int_equal(info.maxalloc, 0);
+  assert_int_equal(info.sizeblks,
+                   MEMPOOL_REALBLOCKSIZE(MM12_BLKSIZE));
+
+  /* mempool_info_task: allocated and free counters. */
+
+  task.pid = PID_MM_ALLOC;
+#if CONFIG_MM_BACKTRACE >= 0
+  task.seqmin = 0;
+  task.seqmax = ULONG_MAX;
+#endif
+  taskinfo = mempool_info_task(&g_mm12_pool, &task);
+  assert_int_equal(taskinfo.aordblks, MM12_NALLOC);
+
+  task.pid = PID_MM_FREE;
+  taskinfo = mempool_info_task(&g_mm12_pool, &task);
+  assert_int_equal(taskinfo.aordblks, MM12_PREALLOC - MM12_NALLOC);
+
+  /* Exercise the dump paths for allocated and free blocks. */
+
+  dump.pid = PID_MM_ALLOC;
+  mempool_memdump(&g_mm12_pool, &dump);
+  dump.pid = PID_MM_FREE;
+  mempool_memdump(&g_mm12_pool, &dump);
+
+  for (i = 0; i < MM12_NALLOC; i++)
+    {
+      mempool_release(&g_mm12_pool, blk[i]);
+    }
+
+  assert_int_equal(mempool_deinit(&g_mm12_pool), 0);
+}
diff --git a/testing/testsuites/kernel/mm/cmocka_mm_test.c 
b/testing/testsuites/kernel/mm/cmocka_mm_test.c
index c6ec08fd8..54e3c1200 100644
--- a/testing/testsuites/kernel/mm/cmocka_mm_test.c
+++ b/testing/testsuites/kernel/mm/cmocka_mm_test.c
@@ -64,6 +64,12 @@ int main(int argc, char *argv[])
                                       test_nuttx_mm_teardown),
       cmocka_unit_test_setup_teardown(test_nuttx_mm09, test_nuttx_mm_setup,
                                       test_nuttx_mm_teardown),
+      cmocka_unit_test_setup_teardown(test_nuttx_mm10, test_nuttx_mm_setup,
+                                      test_nuttx_mm_teardown),
+      cmocka_unit_test_setup_teardown(test_nuttx_mm11, test_nuttx_mm_setup,
+                                      test_nuttx_mm_teardown),
+      cmocka_unit_test_setup_teardown(test_nuttx_mm12, test_nuttx_mm_setup,
+                                      test_nuttx_mm_teardown),
   };
 
   /* Run Test cases */
diff --git a/testing/testsuites/kernel/mm/include/MmTest.h 
b/testing/testsuites/kernel/mm/include/MmTest.h
index 82152190a..6f23c5910 100644
--- a/testing/testsuites/kernel/mm/include/MmTest.h
+++ b/testing/testsuites/kernel/mm/include/MmTest.h
@@ -102,4 +102,19 @@ void test_nuttx_mm08(FAR void **state);
 
 void test_nuttx_mm09(FAR void **state);
 
+/* cases/mm_test_010.c
+ * *****************************************************/
+
+void test_nuttx_mm10(FAR void **state);
+
+/* cases/mm_test_011.c
+ * *****************************************************/
+
+void test_nuttx_mm11(FAR void **state);
+
+/* cases/mm_test_012.c
+ * *****************************************************/
+
+void test_nuttx_mm12(FAR void **state);
+
 #endif

Reply via email to