test_npl_mempool.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include "test_util.h"
  20. #include "nimble/nimble_npl.h"
  21. #define TEST_MEMPOOL_BLOCKS 4
  22. #define TEST_MEMPOOL_BLOCK_SIZE 128
  23. static struct ble_npl_mempool s_mempool;
  24. static os_membuf_t s_mempool_mem[OS_MEMPOOL_SIZE(TEST_MEMPOOL_BLOCKS,
  25. TEST_MEMPOOL_BLOCK_SIZE)];
  26. static void *s_memblock[TEST_MEMPOOL_BLOCKS];
  27. /**
  28. * Unit test for initializing a mempool.
  29. *
  30. * ble_npl_error_t ble_npl_mempool_init(struct ble_npl_mempool *mp, int blocks,
  31. * int block_size, void *membuf, char *name);
  32. *
  33. */
  34. int test_init(void)
  35. {
  36. int err;
  37. err = ble_npl_mempool_init(NULL,
  38. TEST_MEMPOOL_BLOCKS,
  39. TEST_MEMPOOL_BLOCK_SIZE,
  40. NULL,
  41. "Null mempool");
  42. VerifyOrQuit(err, "ble_npl_mempool_init accepted NULL parameters.");
  43. err = ble_npl_mempool_init(&s_mempool,
  44. TEST_MEMPOOL_BLOCKS,
  45. TEST_MEMPOOL_BLOCK_SIZE,
  46. s_mempool_mem,
  47. "s_mempool");
  48. return err;
  49. }
  50. /**
  51. * Test integrity check of a mempool.
  52. *
  53. * bool ble_npl_mempool_is_sane(const struct ble_npl_mempool *mp);
  54. */
  55. int test_is_sane(void)
  56. {
  57. return (ble_npl_mempool_is_sane(&s_mempool)) ? PASS : FAIL;
  58. }
  59. /**
  60. * Test getting a memory block from the pool, putting it back,
  61. * and checking if it is still valid.
  62. *
  63. * void *ble_npl_memblock_get(struct ble_npl_mempool *mp);
  64. *
  65. * ble_npl_error_t ble_npl_memblock_put(struct ble_npl_mempool *mp, void *block_addr);
  66. *
  67. * int ble_npl_memblock_from(const struct ble_npl_mempool *mp, const void *block_addr);
  68. */
  69. int test_stress(void)
  70. {
  71. int loops = 3;
  72. while(loops--)
  73. {
  74. for (int i = 0; i < 4; i++)
  75. {
  76. s_memblock[i] = ble_npl_memblock_get(&s_mempool);
  77. VerifyOrQuit(ble_npl_memblock_from(&s_mempool, s_memblock[i]),
  78. "ble_npl_memblock_get return invalid block.");
  79. }
  80. for (int i = 0; i < 4; i++)
  81. {
  82. SuccessOrQuit(ble_npl_memblock_put(&s_mempool, s_memblock[i]),
  83. "ble_npl_memblock_put refused to take valid block.");
  84. //VerifyOrQuit(!ble_npl_memblock_from(&s_mempool, s_memblock[i]),
  85. // "Block still valid after ble_npl_memblock_put.");
  86. }
  87. }
  88. return PASS;
  89. }
  90. int main(void)
  91. {
  92. SuccessOrQuit(test_init(), "Failed: ble_npl_mempool_init");
  93. SuccessOrQuit(test_is_sane(), "Failed: ble_npl_mempool_is_sane");
  94. SuccessOrQuit(test_stress(), "Failed: ble_npl_mempool stree test");
  95. SuccessOrQuit(test_is_sane(), "Failed: ble_npl_mempool_is_sane");
  96. printf("All tests passed\n");
  97. return PASS;
  98. }