1+ #[ macro_use]
12mod shared;
23
3- use memtrack:: EventType ;
44use rstest:: rstest;
5- use runner_shared:: artifacts:: MemtrackEventKind ;
65use std:: fs;
76use std:: path:: Path ;
87use std:: process:: Command ;
@@ -32,58 +31,43 @@ fn compile_c_source(
3231 Ok ( binary_path)
3332}
3433
35- // ============================================================================
36- // PARAMETERIZED ALLOCATION TESTS
37- // ============================================================================
38-
39- /// Test case definition for allocation tracking tests
4034struct AllocationTestCase {
4135 name : & ' static str ,
4236 source : & ' static str ,
43- assertions : & ' static [ ( EventType , usize ) ] ,
44- allow_excess : bool , // Whether to allow >= instead of exact == for expected counts
4537}
4638
4739const ALLOCATION_TEST_CASES : & [ AllocationTestCase ] = & [
4840 AllocationTestCase {
4941 name : "double_malloc" ,
5042 source : include_str ! ( "../testdata/double_malloc.c" ) ,
51- assertions : & [ ( EventType :: Malloc , 2 ) ] ,
52- allow_excess : false ,
5343 } ,
5444 AllocationTestCase {
5545 name : "malloc_free" ,
5646 source : include_str ! ( "../testdata/malloc_free.c" ) ,
57- assertions : & [ ( EventType :: Malloc , 1 ) , ( EventType :: Free , 1 ) ] ,
58- allow_excess : false ,
5947 } ,
6048 AllocationTestCase {
6149 name : "calloc_test" ,
6250 source : include_str ! ( "../testdata/calloc_test.c" ) ,
63- assertions : & [ ( EventType :: Calloc , 1 ) , ( EventType :: Free , 1 ) ] ,
64- allow_excess : false ,
6551 } ,
6652 AllocationTestCase {
6753 name : "realloc_test" ,
6854 source : include_str ! ( "../testdata/realloc_test.c" ) ,
69- assertions : & [
70- ( EventType :: Malloc , 1 ) ,
71- ( EventType :: Realloc , 1 ) ,
72- ( EventType :: Free , 1 ) ,
73- ] ,
74- allow_excess : false ,
7555 } ,
7656 AllocationTestCase {
7757 name : "aligned_alloc_test" ,
7858 source : include_str ! ( "../testdata/aligned_alloc_test.c" ) ,
79- assertions : & [ ( EventType :: AlignedAlloc , 1 ) , ( EventType :: Free , 1 ) ] ,
80- allow_excess : false ,
8159 } ,
8260 AllocationTestCase {
8361 name : "many_allocs" ,
8462 source : include_str ! ( "../testdata/many_allocs.c" ) ,
85- assertions : & [ ( EventType :: Malloc , 100 ) , ( EventType :: Free , 100 ) ] ,
86- allow_excess : true , // Allow >= because we allocate ptrs array + 100 allocations
63+ } ,
64+ AllocationTestCase {
65+ name : "fork_test" ,
66+ source : include_str ! ( "../testdata/fork_test.c" ) ,
67+ } ,
68+ AllocationTestCase {
69+ name : "alloc_size" ,
70+ source : include_str ! ( "../testdata/alloc_size.c" ) ,
8771 } ,
8872] ;
8973
@@ -95,6 +79,8 @@ const ALLOCATION_TEST_CASES: &[AllocationTestCase] = &[
9579#[ case( & ALLOCATION_TEST_CASES [ 3 ] ) ]
9680#[ case( & ALLOCATION_TEST_CASES [ 4 ] ) ]
9781#[ case( & ALLOCATION_TEST_CASES [ 5 ] ) ]
82+ #[ case( & ALLOCATION_TEST_CASES [ 6 ] ) ]
83+ #[ case( & ALLOCATION_TEST_CASES [ 7 ] ) ]
9884#[ test_log:: test]
9985fn test_allocation_tracking (
10086 #[ case] test_case : & AllocationTestCase ,
@@ -104,108 +90,7 @@ fn test_allocation_tracking(
10490
10591 let ( events, thread_handle) = shared:: track_binary ( & binary) ?;
10692
107- for ( event_type, expected_count) in test_case. assertions {
108- let actual_count = shared:: count_events_by_type ( & events, * event_type) ;
109-
110- if test_case. allow_excess {
111- assert ! (
112- actual_count >= * expected_count,
113- "Test '{}': Expected at least {} {:?} events, got {}" ,
114- test_case. name,
115- expected_count,
116- event_type,
117- actual_count
118- ) ;
119- } else {
120- assert_eq ! (
121- actual_count, * expected_count,
122- "Test '{}': Expected {} {:?} events, got {}" ,
123- test_case. name, expected_count, event_type, actual_count
124- ) ;
125- }
126- }
127-
128- thread_handle. join ( ) . unwrap ( ) ;
129-
130- Ok ( ( ) )
131- }
132-
133- #[ test]
134- #[ test_with:: env( GITHUB_ACTIONS ) ]
135- fn test_fork_tracking ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
136- let temp_dir = TempDir :: new ( ) ?;
137- let source = include_str ! ( "../testdata/fork_test.c" ) ;
138- let binary = compile_c_source ( source, "fork_test" , temp_dir. path ( ) ) ?;
139-
140- let ( events, thread_handle) = shared:: track_binary ( & binary) ?;
141-
142- let malloc_count = shared:: count_events_by_type ( & events, EventType :: Malloc ) ;
143- let free_count = shared:: count_events_by_type ( & events, EventType :: Free ) ;
144-
145- // Should have at least 2 mallocs (parent + child)
146- assert ! (
147- malloc_count >= 2 ,
148- "Expected at least 2 malloc events (parent + child), got {malloc_count}"
149- ) ;
150-
151- // Should have at least 2 frees (parent + child)
152- assert ! (
153- free_count >= 2 ,
154- "Expected at least 2 free events (parent + child), got {free_count}"
155- ) ;
156-
157- // Verify we have events from different PIDs (parent and child)
158- let pids: std:: collections:: HashSet < u32 > = events. iter ( ) . map ( |e| e. pid as u32 ) . collect ( ) ;
159- assert ! (
160- !pids. is_empty( ) ,
161- "Expected to track at least parent process"
162- ) ;
163-
164- thread_handle. join ( ) . unwrap ( ) ;
165-
166- Ok ( ( ) )
167- }
168-
169- #[ test]
170- #[ test_with:: env( GITHUB_ACTIONS ) ]
171- fn test_allocation_sizes ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
172- let temp_dir = TempDir :: new ( ) ?;
173- let source = include_str ! ( "../testdata/alloc_size.c" ) ;
174- let binary = compile_c_source ( source, "alloc_size" , temp_dir. path ( ) ) ?;
175-
176- let ( events, thread_handle) = shared:: track_binary ( & binary) ?;
177-
178- // Filter malloc events and collect their sizes
179- let malloc_events: Vec < u64 > = events
180- . iter ( )
181- . filter_map ( |e| match e. kind {
182- MemtrackEventKind :: Malloc { size } => Some ( size) ,
183- _ => None ,
184- } )
185- . collect ( ) ;
186-
187- // Expected sizes from alloc_size.c: 1024, 2048, 512, 4096
188- let expected_sizes = vec ! [ 1024u64 , 2048 , 512 , 4096 ] ;
189-
190- // Check that we have exactly 4 malloc events
191- assert_eq ! (
192- malloc_events. len( ) ,
193- 4 ,
194- "Expected 4 malloc events, got {}" ,
195- malloc_events. len( )
196- ) ;
197-
198- // Check that all expected sizes are present
199- for expected_size in & expected_sizes {
200- assert ! (
201- malloc_events. contains( expected_size) ,
202- "Expected allocation size {expected_size} not found in malloc events: {malloc_events:?}"
203- ) ;
204- }
205-
206- // Check that we have 4 free events
207- let free_count = shared:: count_events_by_type ( & events, EventType :: Free ) ;
208- assert_eq ! ( free_count, 4 , "Expected 4 free events, got {free_count}" ) ;
93+ assert_events_snapshot ! ( test_case. name, events) ;
20994
21095 thread_handle. join ( ) . unwrap ( ) ;
21196
0 commit comments