1+ const std = @import ("std" );
2+ const testing = std .testing ;
3+ const mimalloc = @import ("mimalloc" ).Allocator {};
4+ const allocator = mimalloc .allocator ();
5+
6+ test "basic allocation and deallocation" {
7+ // Test simple allocation
8+ const slice = try allocator .alloc (u8 , 100 );
9+ defer allocator .free (slice );
10+ try testing .expectEqual (@as (usize , 100 ), slice .len );
11+
12+ // Fill with data to ensure memory is writable
13+ @memset (slice , 0xAA );
14+ for (slice ) | byte | {
15+ try testing .expectEqual (@as (u8 , 0xAA ), byte );
16+ }
17+ }
18+
19+ test "zero-sized allocation" {
20+ // Test allocation of zero bytes
21+ const empty = try allocator .alloc (u8 , 0 );
22+ defer allocator .free (empty );
23+ try testing .expectEqual (@as (usize , 0 ), empty .len );
24+ }
25+
26+ test "realloc behavior" {
27+ // Initial allocation
28+ var slice = try allocator .alloc (u8 , 50 );
29+ defer allocator .free (slice );
30+
31+ // Fill with pattern
32+ @memset (slice , 0xBB );
33+
34+ // Grow
35+ slice = try allocator .realloc (slice , 100 );
36+ try testing .expectEqual (@as (usize , 100 ), slice .len );
37+
38+ // Verify original data is preserved
39+ for (slice [0.. 50]) | byte | {
40+ try testing .expectEqual (@as (u8 , 0xBB ), byte );
41+ }
42+
43+ // Shrink
44+ slice = try allocator .realloc (slice , 25 );
45+ try testing .expectEqual (@as (usize , 25 ), slice .len );
46+
47+ // Verify data is still preserved
48+ for (slice ) | byte | {
49+ try testing .expectEqual (@as (u8 , 0xBB ), byte );
50+ }
51+ }
52+
53+ test "multiple allocations" {
54+ var slices = std .ArrayList ([]u8 ).init (testing .allocator );
55+ defer {
56+ for (slices .items ) | slice | {
57+ allocator .free (slice );
58+ }
59+ slices .deinit ();
60+ }
61+
62+ // Perform multiple allocations of varying sizes
63+ const sizes = [_ ]usize { 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 };
64+ for (sizes ) | size | {
65+ const slice = try allocator .alloc (u8 , size );
66+ try slices .append (slice );
67+ @memset (slice , @as (u8 , @truncate (size % 256 )));
68+ }
69+
70+ // Verify all allocations
71+ for (slices .items , 0.. ) | slice , i | {
72+ try testing .expectEqual (sizes [i ], slice .len );
73+ for (slice ) | byte | {
74+ try testing .expectEqual (@as (u8 , @truncate (sizes [i ] % 256 )), byte );
75+ }
76+ }
77+ }
78+
79+ test "alignment requirements" {
80+ const AlignedStruct = struct {
81+ a : u32 align (16 ),
82+ b : u64 ,
83+ };
84+
85+ var slice = try allocator .alloc (AlignedStruct , 1 );
86+ defer allocator .free (slice );
87+
88+ // Verify alignment
89+ try testing .expectEqual (@as (usize , 0 ), @intFromPtr (& slice [0 ]) & 15 );
90+ }
0 commit comments