@@ -148,3 +148,63 @@ test_normal_realloc_works(void **state)
148148 mem_allocator_free (allocator , new_ptr );
149149 mem_allocator_destroy (allocator );
150150}
151+
152+ /* Test: Invalid alignments (not power of 2 or zero) */
153+ static void
154+ test_aligned_alloc_invalid_not_power_of_2 (void * * state )
155+ {
156+ mem_allocator_t allocator ;
157+ char heap_buf [64 * 1024 ];
158+ void * ptr ;
159+
160+ allocator = mem_allocator_create (heap_buf , sizeof (heap_buf ));
161+ assert_non_null (allocator );
162+
163+ /* These should all fail (zero or not power of 2) */
164+ int invalid_alignments [] = {0 , 3 , 5 , 7 , 9 , 15 , 17 , 100 };
165+ for (int i = 0 ; i < sizeof (invalid_alignments ) / sizeof (invalid_alignments [0 ]); i ++ ) {
166+ ptr = mem_allocator_malloc_aligned (allocator , 128 , invalid_alignments [i ]);
167+ assert_null (ptr );
168+ }
169+
170+ /* Small powers of 2 should succeed (adjusted to GC_MIN_ALIGNMENT) */
171+ ptr = mem_allocator_malloc_aligned (allocator , 8 , 1 );
172+ assert_non_null (ptr );
173+ mem_allocator_free (allocator , ptr );
174+
175+ ptr = mem_allocator_malloc_aligned (allocator , 8 , 2 );
176+ assert_non_null (ptr );
177+ mem_allocator_free (allocator , ptr );
178+
179+ ptr = mem_allocator_malloc_aligned (allocator , 8 , 4 );
180+ assert_non_null (ptr );
181+ mem_allocator_free (allocator , ptr );
182+
183+ mem_allocator_destroy (allocator );
184+ }
185+
186+ /* Test: Size must be multiple of alignment */
187+ static void
188+ test_aligned_alloc_size_not_multiple (void * * state )
189+ {
190+ mem_allocator_t allocator ;
191+ char heap_buf [64 * 1024 ];
192+ void * ptr ;
193+
194+ allocator = mem_allocator_create (heap_buf , sizeof (heap_buf ));
195+ assert_non_null (allocator );
196+
197+ /* Size not multiple of alignment - should fail */
198+ ptr = mem_allocator_malloc_aligned (allocator , 100 , 64 );
199+ assert_null (ptr );
200+
201+ ptr = mem_allocator_malloc_aligned (allocator , 65 , 64 );
202+ assert_null (ptr );
203+
204+ /* Size is multiple - should succeed */
205+ ptr = mem_allocator_malloc_aligned (allocator , 128 , 64 );
206+ assert_non_null (ptr );
207+ mem_allocator_free (allocator , ptr );
208+
209+ mem_allocator_destroy (allocator );
210+ }
0 commit comments