ruchy 4.2.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Conservative Garbage Collector Implementation
//!
//! EXTREME TDD: Full test coverage, zero entropy, <10 complexity per function
//! Extracted from interpreter.rs to eliminate duplication with gc.rs stub
//!
//! This module implements a conservative mark-and-sweep garbage collector
//! with statistics tracking and configurable collection thresholds.

use crate::runtime::Value;

/// Conservative garbage collector with mark-and-sweep algorithm
#[derive(Debug, Clone)]
pub struct ConservativeGC {
    /// Objects currently tracked by the GC
    tracked_objects: Vec<GCObject>,
    /// Collection statistics
    collections_performed: u64,
    /// Total objects collected
    objects_collected: u64,
    /// Memory pressure threshold (bytes)
    collection_threshold: usize,
    /// Current allocated bytes estimate
    allocated_bytes: usize,
    /// Enable/disable automatic collection
    auto_collect_enabled: bool,
}

/// A garbage-collected object with metadata
#[derive(Debug, Clone)]
pub struct GCObject {
    /// Object identifier (address-like)
    pub id: usize,
    /// Object size in bytes
    pub size: usize,
    /// Mark bit for mark-and-sweep
    pub marked: bool,
    /// Object generation (for future generational GC)
    pub generation: u8,
    /// Reference to the actual value
    pub value: Value,
}

impl ConservativeGC {
    /// Create a new garbage collector
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn new() -> Self {
        Self {
            tracked_objects: Vec::new(),
            collections_performed: 0,
            objects_collected: 0,
            collection_threshold: 10 * 1024 * 1024, // 10MB default
            allocated_bytes: 0,
            auto_collect_enabled: true,
        }
    }

    /// Track a new object for garbage collection
    ///
    /// # Complexity
    /// Cyclomatic complexity: 3 (within Toyota Way limits)
    pub fn track_object(&mut self, value: Value) -> usize {
        let size = Self::estimate_object_size(&value);
        let id = self.next_object_id();

        let obj = GCObject {
            id,
            size,
            marked: false,
            generation: 0,
            value,
        };

        self.tracked_objects.push(obj);
        self.allocated_bytes += size;

        // Trigger collection if threshold exceeded
        if self.auto_collect_enabled && self.allocated_bytes > self.collection_threshold {
            self.collect();
        }

        id
    }

    /// Perform garbage collection
    ///
    /// # Complexity
    /// Cyclomatic complexity: 2 (within Toyota Way limits)
    pub fn collect(&mut self) {
        // Mark phase
        self.mark_phase();

        // Sweep phase
        let collected = self.sweep_phase();

        // Update statistics
        self.collections_performed += 1;
        self.objects_collected += collected as u64;
    }

    /// Mark phase of mark-and-sweep
    ///
    /// # Complexity
    /// Cyclomatic complexity: 4 (within Toyota Way limits)
    fn mark_phase(&mut self) {
        // Collect root object IDs first to avoid borrowing conflicts
        let root_ids: Vec<usize> = self
            .tracked_objects
            .iter()
            .filter(|obj| self.is_root_object(obj.id))
            .map(|obj| obj.id)
            .collect();

        // Mark all root objects
        for id in root_ids {
            self.mark_object(id);
        }
    }

    /// Check if an object is a root
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    fn is_root_object(&self, _id: usize) -> bool {
        // Conservative: treat all objects as potential roots
        // In real implementation: check stack, registers, global vars
        true
    }

    /// Mark an object and its references
    ///
    /// # Complexity
    /// Cyclomatic complexity: 8 (within Toyota Way limits)
    fn mark_object(&mut self, id: usize) {
        // Find and mark the object
        let value_clone = {
            if let Some(obj) = self.tracked_objects.iter_mut().find(|o| o.id == id) {
                if obj.marked {
                    return; // Already marked
                }
                obj.marked = true;
                obj.value.clone()
            } else {
                return;
            }
        };

        // Mark referenced objects based on value type
        match &value_clone {
            Value::Array(arr) => {
                let ref_ids: Vec<usize> = arr
                    .iter()
                    .filter_map(|item| self.find_object_id(item))
                    .collect();
                for ref_id in ref_ids {
                    self.mark_object(ref_id);
                }
            }
            Value::Tuple(elements) => {
                let ref_ids: Vec<usize> = elements
                    .iter()
                    .filter_map(|item| self.find_object_id(item))
                    .collect();
                for ref_id in ref_ids {
                    self.mark_object(ref_id);
                }
            }
            _ => {} // Other types don't contain references
        }
    }

    /// Find object ID for a value
    ///
    /// # Complexity
    /// Cyclomatic complexity: 3 (within Toyota Way limits)
    fn find_object_id(&self, target: &Value) -> Option<usize> {
        for obj in &self.tracked_objects {
            if std::ptr::eq(&raw const obj.value, std::ptr::from_ref::<Value>(target)) {
                return Some(obj.id);
            }
        }
        None
    }

    /// Sweep phase - remove unmarked objects
    ///
    /// # Complexity
    /// Cyclomatic complexity: 5 (within Toyota Way limits)
    fn sweep_phase(&mut self) -> usize {
        let before_count = self.tracked_objects.len();
        let mut freed_bytes = 0;

        // Remove unmarked objects
        self.tracked_objects.retain(|obj| {
            if obj.marked {
                true
            } else {
                freed_bytes += obj.size;
                false
            }
        });

        // Reset marks for next collection
        for obj in &mut self.tracked_objects {
            obj.marked = false;
        }

        self.allocated_bytes = self.allocated_bytes.saturating_sub(freed_bytes);
        before_count - self.tracked_objects.len()
    }

    /// Estimate object size in bytes
    ///
    /// # Complexity
    /// Cyclomatic complexity: 8 (within Toyota Way limits)
    fn estimate_object_size(value: &Value) -> usize {
        match value {
            Value::Integer(_) => 8,
            Value::Float(_) => 8,
            Value::Bool(_) => 1,
            Value::Byte(_) => 1,
            Value::Nil => 0,
            Value::String(s) => 24 + s.len(), // Rc overhead + string data
            Value::Array(arr) => {
                24 + arr.len() * 8 + arr.iter().map(Self::estimate_object_size).sum::<usize>()
            }
            Value::Tuple(elements) => {
                24 + elements.len() * 8
                    + elements
                        .iter()
                        .map(Self::estimate_object_size)
                        .sum::<usize>()
            }
            Value::Closure { params, .. } => 48 + params.len() * 16,
            Value::DataFrame { columns } => {
                48 + columns
                    .iter()
                    .map(|c| 24 + c.name.len() + c.values.len() * 8)
                    .sum::<usize>()
            }
            Value::Object(map) => {
                48 + map.len() * 32
                    + map
                        .iter()
                        .map(|(k, v)| k.len() + Self::estimate_object_size(v))
                        .sum::<usize>()
            }
            Value::ObjectMut(cell) => {
                let map = cell.lock().expect(
                    "Mutex poisoned in estimate_object_size - indicates panic in another thread",
                );
                56 + map.len() * 32 // Extra 8 bytes for RefCell borrow counter
                    + map
                        .iter()
                        .map(|(k, v)| k.len() + Self::estimate_object_size(v))
                        .sum::<usize>()
            }
            Value::Range { .. } => 24,
            Value::EnumVariant {
                variant_name, data, ..
            } => 24 + variant_name.len() + data.as_ref().map_or(0, |d| d.len() * 8),
            Value::BuiltinFunction(name) => 24 + name.len(),
            Value::Struct { name, fields } => {
                48 + name.len()
                    + fields.len() * 32
                    + fields
                        .iter()
                        .map(|(k, v)| k.len() + Self::estimate_object_size(v))
                        .sum::<usize>()
            }
            Value::Class {
                class_name,
                fields,
                methods,
            } => {
                let fields_read = fields.read().expect(
                    "RwLock poisoned in estimate_object_size - indicates panic in another thread",
                );
                48 + class_name.len()
                    + fields_read.len() * 32
                    + fields_read
                        .iter()
                        .map(|(k, v)| k.len() + Self::estimate_object_size(v))
                        .sum::<usize>()
                    + methods.len() * 32
            }
            #[cfg(not(target_arch = "wasm32"))]
            Value::HtmlDocument(_) => 128, // Estimated HTML document overhead
            #[cfg(not(target_arch = "wasm32"))]
            Value::HtmlElement(_) => 64,
            Value::Atom(s) => std::mem::size_of::<Value>() + s.len(),
        }
    }

    /// Get next object ID
    ///
    /// # Complexity
    /// Cyclomatic complexity: 2 (within Toyota Way limits)
    fn next_object_id(&self) -> usize {
        self.tracked_objects.len()
    }

    /// Force a garbage collection
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn force_collect(&mut self) -> GCStats {
        // Mark phase
        self.mark_phase();

        // Sweep phase
        let collected = self.sweep_phase();

        // Update statistics
        self.collections_performed += 1;
        self.objects_collected += collected as u64;

        // Return statistics
        GCStats {
            collections: self.collections_performed,
            objects_collected: self.objects_collected,
            current_objects: self.tracked_objects.len(),
            allocated_bytes: self.allocated_bytes,
        }
    }

    /// Get GC statistics
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn get_stats(&self) -> GCStats {
        GCStats {
            collections: self.collections_performed,
            objects_collected: self.objects_collected,
            current_objects: self.tracked_objects.len(),
            allocated_bytes: self.allocated_bytes,
        }
    }

    /// Get GC info
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn get_info(&self) -> GCInfo {
        GCInfo {
            threshold: self.collection_threshold,
            auto_collect_enabled: self.auto_collect_enabled,
            tracked_count: self.tracked_objects.len(),
        }
    }

    /// Set collection threshold
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn set_collection_threshold(&mut self, threshold: usize) {
        self.collection_threshold = threshold;
    }

    /// Enable/disable automatic collection
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn set_auto_collect(&mut self, enabled: bool) {
        self.auto_collect_enabled = enabled;
    }

    /// Clear all tracked objects
    ///
    /// # Complexity
    /// Cyclomatic complexity: 1 (within Toyota Way limits)
    pub fn clear(&mut self) {
        self.tracked_objects.clear();
        self.allocated_bytes = 0;
    }
}

impl Default for ConservativeGC {
    fn default() -> Self {
        Self::new()
    }
}

/// GC statistics
#[derive(Debug, Clone)]
pub struct GCStats {
    pub collections: u64,
    pub objects_collected: u64,
    pub current_objects: usize,
    pub allocated_bytes: usize,
}

/// GC configuration info
#[derive(Debug, Clone)]
pub struct GCInfo {
    pub threshold: usize,
    pub auto_collect_enabled: bool,
    pub tracked_count: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gc_creation() {
        let gc = ConservativeGC::new();
        assert_eq!(gc.collections_performed, 0);
        assert_eq!(gc.objects_collected, 0);
        assert!(gc.auto_collect_enabled);
    }

    #[test]
    fn test_track_object() {
        let mut gc = ConservativeGC::new();
        let val = Value::Integer(42);
        let id = gc.track_object(val);
        assert_eq!(id, 0);
        assert_eq!(gc.tracked_objects.len(), 1);
    }

    #[test]
    fn test_gc_collect() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Integer(1));
        gc.track_object(Value::Integer(2));
        gc.track_object(Value::Integer(3));

        let stats_before = gc.get_stats();
        assert_eq!(stats_before.current_objects, 3);

        gc.collect();

        let stats_after = gc.get_stats();
        assert_eq!(stats_after.collections, 1);
        // Conservative GC marks everything as root, so nothing collected
        assert_eq!(stats_after.current_objects, 3);
    }

    #[test]
    fn test_gc_threshold() {
        let mut gc = ConservativeGC::new();
        gc.set_collection_threshold(100);
        assert_eq!(gc.collection_threshold, 100);

        gc.set_auto_collect(false);
        assert!(!gc.auto_collect_enabled);
    }

    #[test]
    fn test_estimate_object_size() {
        assert_eq!(ConservativeGC::estimate_object_size(&Value::Integer(42)), 8);
        assert_eq!(ConservativeGC::estimate_object_size(&Value::Float(3.15)), 8);
        assert_eq!(ConservativeGC::estimate_object_size(&Value::Bool(true)), 1);
        assert_eq!(ConservativeGC::estimate_object_size(&Value::Nil), 0);

        let s = Value::from_string("hello".to_string());
        assert_eq!(ConservativeGC::estimate_object_size(&s), 24 + 5);
    }

    #[test]
    fn test_gc_clear() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Integer(1));
        gc.track_object(Value::Integer(2));

        assert_eq!(gc.tracked_objects.len(), 2);

        gc.clear();
        assert_eq!(gc.tracked_objects.len(), 0);
        assert_eq!(gc.allocated_bytes, 0);
    }

    #[test]
    fn test_gc_stats_and_info() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Integer(42));

        let stats = gc.get_stats();
        assert_eq!(stats.current_objects, 1);
        assert_eq!(stats.allocated_bytes, 8);

        let info = gc.get_info();
        assert_eq!(info.tracked_count, 1);
        assert!(info.auto_collect_enabled);
    }

    // COVERAGE-95: Additional tests for complete coverage

    #[test]
    fn test_gc_default() {
        let gc = ConservativeGC::default();
        assert_eq!(gc.collections_performed, 0);
        assert_eq!(gc.tracked_objects.len(), 0);
    }

    #[test]
    fn test_force_collect() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Integer(1));
        gc.track_object(Value::Integer(2));

        let stats = gc.force_collect();
        assert_eq!(stats.collections, 1);
        assert_eq!(stats.current_objects, 2); // Conservative GC keeps all
    }

    #[test]
    fn test_estimate_object_size_byte() {
        assert_eq!(ConservativeGC::estimate_object_size(&Value::Byte(255)), 1);
    }

    #[test]
    fn test_estimate_object_size_array() {
        let arr = Value::Array(vec![Value::Integer(1), Value::Integer(2)].into());
        let size = ConservativeGC::estimate_object_size(&arr);
        // 24 + 2*8 + 8 + 8 = 24 + 16 + 16 = 56
        assert!(size > 24);
    }

    #[test]
    fn test_estimate_object_size_tuple() {
        let tuple = Value::Tuple(vec![Value::Integer(1), Value::Bool(true)].into());
        let size = ConservativeGC::estimate_object_size(&tuple);
        assert!(size > 24);
    }

    #[test]
    fn test_estimate_object_size_closure() {
        use crate::frontend::ast::{Expr, ExprKind, Literal, Span};
        use std::cell::RefCell;
        use std::rc::Rc;
        use std::sync::Arc;
        let closure = Value::Closure {
            params: vec![("x".to_string(), None), ("y".to_string(), None)],
            body: Arc::new(Expr::new(
                ExprKind::Literal(Literal::Integer(0, None)),
                Span::default(),
            )),
            env: Rc::new(RefCell::new(std::collections::HashMap::new())),
        };
        let size = ConservativeGC::estimate_object_size(&closure);
        assert!(size >= 48);
    }

    #[test]
    fn test_estimate_object_size_range() {
        let range = Value::Range {
            start: Box::new(Value::Integer(0)),
            end: Box::new(Value::Integer(10)),
            inclusive: false,
        };
        assert_eq!(ConservativeGC::estimate_object_size(&range), 24);
    }

    #[test]
    fn test_estimate_object_size_enum_variant() {
        let variant = Value::EnumVariant {
            enum_name: "MyEnum".to_string(),
            variant_name: "Variant".to_string(),
            data: Some(vec![Value::Integer(42)]),
        };
        let size = ConservativeGC::estimate_object_size(&variant);
        assert!(size > 24);
    }

    #[test]
    fn test_estimate_object_size_enum_variant_no_data() {
        let variant = Value::EnumVariant {
            enum_name: "MyEnum".to_string(),
            variant_name: "Empty".to_string(),
            data: None,
        };
        let size = ConservativeGC::estimate_object_size(&variant);
        assert!(size >= 24);
    }

    #[test]
    fn test_estimate_object_size_builtin_function() {
        let builtin = Value::BuiltinFunction("print".to_string());
        let size = ConservativeGC::estimate_object_size(&builtin);
        assert_eq!(size, 24 + 5);
    }

    #[test]
    fn test_estimate_object_size_object() {
        use std::sync::Arc;
        let mut map = std::collections::HashMap::new();
        map.insert("key".to_string(), Value::Integer(42));
        let obj = Value::Object(Arc::new(map));
        let size = ConservativeGC::estimate_object_size(&obj);
        assert!(size > 48);
    }

    #[test]
    fn test_estimate_object_size_object_mut() {
        use std::sync::Arc;
        use std::sync::Mutex;
        let mut map = std::collections::HashMap::new();
        map.insert("key".to_string(), Value::Integer(42));
        let obj = Value::ObjectMut(Arc::new(Mutex::new(map)));
        let size = ConservativeGC::estimate_object_size(&obj);
        assert!(size > 56);
    }

    #[test]
    fn test_estimate_object_size_struct() {
        use std::sync::Arc;
        let mut fields = std::collections::HashMap::new();
        fields.insert("field1".to_string(), Value::Integer(1));
        let s = Value::Struct {
            name: "MyStruct".to_string(),
            fields: Arc::new(fields),
        };
        let size = ConservativeGC::estimate_object_size(&s);
        assert!(size > 48);
    }

    #[test]
    fn test_estimate_object_size_class() {
        use std::sync::{Arc, RwLock};
        let fields = Arc::new(RwLock::new(std::collections::HashMap::new()));
        let class = Value::Class {
            class_name: "MyClass".to_string(),
            fields,
            methods: Arc::new(std::collections::HashMap::new()),
        };
        let size = ConservativeGC::estimate_object_size(&class);
        assert!(size > 48);
    }

    #[test]
    fn test_estimate_object_size_dataframe() {
        use crate::runtime::DataFrameColumn;
        let df = Value::DataFrame {
            columns: vec![DataFrameColumn {
                name: "col1".to_string(),
                values: vec![Value::Integer(1), Value::Integer(2)],
            }],
        };
        let size = ConservativeGC::estimate_object_size(&df);
        assert!(size > 48);
    }

    #[test]
    fn test_estimate_object_size_atom() {
        let atom = Value::Atom("my_atom".to_string());
        let size = ConservativeGC::estimate_object_size(&atom);
        assert!(size > 0);
    }

    #[test]
    fn test_gc_track_multiple_objects() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Integer(1));
        gc.track_object(Value::from_string("hello".to_string()));
        gc.track_object(Value::Bool(true));
        gc.track_object(Value::Float(3.14));

        assert_eq!(gc.tracked_objects.len(), 4);
        let stats = gc.get_stats();
        assert!(stats.allocated_bytes > 0);
    }

    #[test]
    fn test_gc_stats_clone() {
        let stats = GCStats {
            collections: 5,
            objects_collected: 100,
            current_objects: 50,
            allocated_bytes: 1024,
        };
        let cloned = stats.clone();
        assert_eq!(cloned.collections, 5);
        assert_eq!(cloned.objects_collected, 100);
    }

    #[test]
    fn test_gc_info_clone() {
        let info = GCInfo {
            threshold: 1024,
            auto_collect_enabled: true,
            tracked_count: 10,
        };
        let cloned = info.clone();
        assert_eq!(cloned.threshold, 1024);
        assert!(cloned.auto_collect_enabled);
    }

    #[test]
    fn test_gc_object_fields() {
        let obj = GCObject {
            id: 42,
            size: 100,
            marked: false,
            generation: 0,
            value: Value::Integer(99),
        };
        assert_eq!(obj.id, 42);
        assert_eq!(obj.size, 100);
        assert!(!obj.marked);
        assert_eq!(obj.generation, 0);
    }

    #[test]
    fn test_gc_object_clone() {
        let obj = GCObject {
            id: 1,
            size: 8,
            marked: true,
            generation: 1,
            value: Value::Integer(42),
        };
        let cloned = obj.clone();
        assert_eq!(cloned.id, 1);
        assert!(cloned.marked);
    }

    #[test]
    fn test_gc_track_array_triggers_mark() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Array(
            vec![Value::Integer(1), Value::Integer(2)].into(),
        ));
        gc.collect();
        let stats = gc.get_stats();
        assert_eq!(stats.collections, 1);
    }

    #[test]
    fn test_gc_track_tuple_triggers_mark() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Tuple(
            vec![Value::Integer(1), Value::Bool(true)].into(),
        ));
        gc.collect();
        let stats = gc.get_stats();
        assert_eq!(stats.collections, 1);
    }

    #[test]
    fn test_gc_auto_collect_trigger() {
        let mut gc = ConservativeGC::new();
        gc.set_collection_threshold(10); // Very low threshold
        gc.set_auto_collect(true);

        // Track large string to trigger collection
        gc.track_object(Value::from_string("a".repeat(100)));

        // Collection should have been triggered
        let stats = gc.get_stats();
        assert!(stats.collections > 0);
    }

    #[test]
    fn test_gc_clone() {
        let mut gc = ConservativeGC::new();
        gc.track_object(Value::Integer(42));
        gc.collect();

        let cloned = gc.clone();
        assert_eq!(cloned.collections_performed, gc.collections_performed);
        assert_eq!(cloned.tracked_objects.len(), gc.tracked_objects.len());
    }
}