• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * Unit test for pool manager.
18  */
19 
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <string.h>
23 
24 #include "jmem.h"
25 
26 #define JMEM_ALLOCATOR_INTERNAL
27 #include "jmem-allocator-internal.h"
28 
29 #include "test-common.h"
30 
31 /* Iterations count. */
32 const uint32_t test_iters = 1024;
33 
34 /* Subiterations count. */
35 #define TEST_MAX_SUB_ITERS  1024
36 #define TEST_CHUNK_SIZE 8
37 
38 uint8_t *ptrs[TEST_MAX_SUB_ITERS];
39 uint8_t data[TEST_MAX_SUB_ITERS][TEST_CHUNK_SIZE];
40 
41 int
main(void)42 main (void)
43 {
44   TEST_INIT ();
45 
46   jmem_init ();
47 
48   for (uint32_t i = 0; i < test_iters; i++)
49   {
50     const size_t subiters = ((size_t) rand () % TEST_MAX_SUB_ITERS) + 1;
51 
52     for (size_t j = 0; j < subiters; j++)
53     {
54       ptrs[j] = (uint8_t *) jmem_pools_alloc (TEST_CHUNK_SIZE);
55 
56       if (ptrs[j] != NULL)
57       {
58         for (size_t k = 0; k < TEST_CHUNK_SIZE; k++)
59         {
60           ptrs[j][k] = (uint8_t) (rand () % 256);
61         }
62 
63         memcpy (data[j], ptrs[j], TEST_CHUNK_SIZE);
64       }
65     }
66 
67     /* jmem_heap_print (false); */
68 
69     for (size_t j = 0; j < subiters; j++)
70     {
71       if (rand () % 256 == 0)
72       {
73         jmem_pools_collect_empty ();
74       }
75 
76       if (ptrs[j] != NULL)
77       {
78         TEST_ASSERT (!memcmp (data[j], ptrs[j], TEST_CHUNK_SIZE));
79 
80         jmem_pools_free (ptrs[j], TEST_CHUNK_SIZE);
81       }
82     }
83   }
84 
85 #ifdef JMEM_STATS
86   jmem_heap_stats_print ();
87 #endif /* JMEM_STATS */
88 
89   jmem_finalize ();
90 
91   return 0;
92 } /* main */
93