thread_aware 0.7.0

Facilities to support thread-isolated state.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::{self};

use crate::affinity::{Affinity, pinned_affinities};
use crate::{ThreadAware, Unaware};

// We don't use PerCore here because we want to test the raw Trc itself.
type PerCore<T> = crate::Arc<T, crate::PerCore>;

#[derive(Clone, Debug)]
struct Counter {
    value: sync::Arc<AtomicI32>,
}

impl Counter {
    fn new() -> Self {
        Self {
            value: sync::Arc::new(AtomicI32::new(0)),
        }
    }
    fn increment_by(&self, v: i32) {
        self.value.fetch_add(v, Ordering::AcqRel);
    }
    fn value(&self) -> i32 {
        self.value.load(Ordering::Acquire)
    }
}

impl ThreadAware for Counter {
    fn relocate(&mut self, _source: Option<Affinity>, _destination: Affinity) {
        self.value = sync::Arc::new(AtomicI32::new(0));
    }
}

#[test]
fn transfer_creates_new_value() {
    let affinities = pinned_affinities(&[2]);
    let source = Some(affinities[0]);
    let destination = affinities[1];

    let pmr = PerCore::new(Counter::new);
    pmr.increment_by(10);
    let mut pmr2 = pmr.clone();
    pmr2.relocate(source, destination);
    assert_eq!(pmr.value(), 10);
    assert_eq!(pmr2.value(), 0);
}

#[test]
fn new_with_works() {
    let pmr = PerCore::new_with((), |()| Counter::new());
    pmr.increment_by(3);
    assert_eq!(pmr.value(), 3);
}

#[test]
fn new_with_relocate_forwards_to_data() {
    // Exercises BoxedRelocate::relocate and the Factory::Closure path.
    // Uses a data value whose relocate changes observable state, so we can
    // verify that the closure's data was actually relocated before call_once.
    #[derive(Clone)]
    struct Seed(bool);

    impl ThreadAware for Seed {
        fn relocate(&mut self, _source: Option<Affinity>, _destination: Affinity) {
            self.0 = true;
        }
    }

    let affinities = pinned_affinities(&[2]);
    let source = Some(affinities[0]);
    let destination = affinities[1];

    let mut pmr = PerCore::new_with(Seed(false), |seed| {
        let c = Counter::new();
        // The factory output depends on whether the seed was relocated.
        if seed.0 {
            c.increment_by(999);
        }
        c
    });
    assert_eq!(pmr.value(), 0, "initial factory should see un-relocated seed");

    pmr.relocate(source, destination);
    assert_eq!(
        pmr.value(),
        999,
        "factory must see relocated seed (BoxedRelocate must forward relocate)"
    );
}

#[test]
fn test_from_unaware() {
    // Create a PerCore from an unaware value (a simple i32)
    // This covers line 191 (from_unaware method)
    let mut per_core = PerCore::from_unaware(42);
    assert_eq!(*per_core, 42);

    // Verify it can be relocated
    let affinities = pinned_affinities(&[2]);
    per_core.relocate(Some(affinities[0]), affinities[1]);
    assert_eq!(*per_core, 42);
}

#[test]
fn test_partialeq() {
    let value1 = PerCore::with_value(42);
    let value2 = PerCore::with_value(42);
    let value3 = PerCore::with_value(43);

    assert_eq!(value1, value2);
    assert_ne!(value1, value3);
}

#[test]
fn test_hash() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let value1 = PerCore::with_value(42);
    let value2 = PerCore::with_value(42);
    let value3 = PerCore::with_value(43);

    let mut hasher1 = DefaultHasher::new();
    value1.hash(&mut hasher1);
    let hash1 = hasher1.finish();

    let mut hasher2 = DefaultHasher::new();
    value2.hash(&mut hasher2);
    let hash2 = hasher2.finish();

    let mut hasher3 = DefaultHasher::new();
    value3.hash(&mut hasher3);
    let hash3 = hasher3.finish();

    assert_eq!(hash1, hash2);
    assert_ne!(hash1, hash3);
}

#[test]
fn test_partialord() {
    let value1 = PerCore::with_value(42);
    let value2 = PerCore::with_value(43);

    assert!(value1 < value2);
    assert!(value2 > value1);
}

#[test]
fn test_ord() {
    let value1 = PerCore::with_value(42);
    let value2 = PerCore::with_value(43);
    let value3 = PerCore::with_value(42);

    assert_eq!(value1.cmp(&value2), std::cmp::Ordering::Less);
    assert_eq!(value2.cmp(&value1), std::cmp::Ordering::Greater);
    assert_eq!(value1.cmp(&value3), std::cmp::Ordering::Equal);
}

#[test]
fn test_trc_clone() {
    let value = PerCore::with_value(42);
    let cloned_value = value.clone();
    assert_eq!(*value, *cloned_value);
}

#[test]
fn test_into_arc() {
    let trc = PerCore::new(|| 42);
    let _arc = trc.into_arc();

    let trc = PerCore::with_value(42);
    let _arc = trc.into_arc();

    let trc = PerCore::with_value(Unaware(42));
    let _arc = trc.into_arc();
}

#[test]
fn test_from() {
    let trc = PerCore::new(|| 42);
    let _arc = trc.into_arc();

    let trc = PerCore::with_value(42);
    let _arc = trc.into_arc();

    let trc = PerCore::with_value(Unaware(42));
    let _arc = trc.into_arc().into_arc();
}

#[test]
fn test_trc_relocated_with_factory_data() {
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create a Trc with a value that implements ThreadAware + Clone
    // This will use Factory::Data
    let trc_affinity1 = PerCore::with_value(42);
    assert_eq!(*trc_affinity1, 42);

    // Relocate to another affinity, which should trigger Factory::Data path
    // and call data.relocate(source, destination) at line 219
    let mut trc_affinity2 = trc_affinity1;
    trc_affinity2.relocate(affinity1, affinity2);
    assert_eq!(*trc_affinity2, 42);
}

#[test]
fn test_trc_relocated_reuses_existing_value() {
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create a Trc and clone it before relocating
    let trc1 = PerCore::with_value(42);
    let trc2 = trc1.clone();

    // Relocate the first Trc to affinity2
    // This creates a new value in the destination storage
    let mut trc1_relocated = trc1;
    trc1_relocated.relocate(affinity1, affinity2);
    assert_eq!(*trc1_relocated, 42);

    // Relocate the cloned Trc to the same destination
    // This should hit line 428 where it finds the existing value in storage
    // and reuses it instead of creating a new one
    let mut trc2_relocated = trc2;
    trc2_relocated.relocate(affinity1, affinity2);
    assert_eq!(*trc2_relocated, 42);

    // Both relocated Trcs should point to the same sync::Arc (deduplication)
    assert!(std::sync::Arc::ptr_eq(&trc1_relocated.into_arc(), &trc2_relocated.into_arc()));
}

#[test]
fn test_from_storage() {
    use std::sync::{Arc, RwLock};

    let affinities = pinned_affinities(&[2]);
    let affinity1 = affinities[0];

    // Create a storage and populate it with a value for affinity1
    let mut storage = super::storage::Storage::new();
    let value = Arc::new(100);
    storage.replace(affinity1, Arc::clone(&value));

    let storage_arc = Arc::new(RwLock::new(storage));

    // Create a Trc from the storage at affinity1
    // This should call line 400 (from_storage method)
    let trc = PerCore::from_storage(Arc::clone(&storage_arc), affinity1);

    // Verify the value is correct
    assert_eq!(*trc, 100);

    // Verify it points to the same Arc we put in storage
    assert!(Arc::ptr_eq(&trc.into_arc(), &value));
}

#[test]
fn test_factory_clone_with_data() {
    // This test covers line 142: Self::Data(data_fn) => Self::Data(*data_fn)
    // We create a Trc with Factory::Data, clone it, and verify the factory is properly cloned
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create a Trc with a value that uses Factory::Data (ThreadAware + Clone)
    let trc1 = PerCore::with_value(42);

    // Clone the Trc - this should exercise line 142 in the Factory::clone method
    let trc2 = trc1.clone();

    // Verify both Trcs work correctly
    assert_eq!(*trc1, 42);
    assert_eq!(*trc2, 42);

    // Relocate both to verify the cloned factory works properly
    let mut trc1_relocated = trc1;
    trc1_relocated.relocate(affinity1, affinity2);
    let mut trc2_relocated = trc2;
    trc2_relocated.relocate(affinity1, affinity2);

    assert_eq!(*trc1_relocated, 42);
    assert_eq!(*trc2_relocated, 42);
}

#[test]
fn test_factory_clone_with_closure_boxed() {
    // This test covers line 141: Self::Closure(closure, closure_source) => Self::Closure(sync::Arc::clone(closure), *closure_source)
    // We create a Trc with Factory::Closure via with_closure, clone it, and verify the factory is properly cloned
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create a Trc with a closure that uses Factory::Closure
    let trc1 = PerCore::new(|| 100);

    // Clone the Trc - this should exercise line 141 in the Factory::clone method
    let trc2 = trc1.clone();

    // Verify both Trcs work correctly
    assert_eq!(*trc1, 100);
    assert_eq!(*trc2, 100);

    // Relocate both to verify the cloned factory (closure) works properly
    let mut trc1_relocated = trc1;
    trc1_relocated.relocate(affinity1, affinity2);
    let mut trc2_relocated = trc2;
    trc2_relocated.relocate(affinity1, affinity2);

    assert_eq!(*trc1_relocated, 100);
    assert_eq!(*trc2_relocated, 100);

    // Both relocated Trcs should point to the same sync::Arc due to deduplication
    assert!(std::sync::Arc::ptr_eq(&trc1_relocated.into_arc(), &trc2_relocated.into_arc()));
}

#[test]
fn test_factory_clone_with_manual() {
    // This test covers line 143: Self::Manual => Self::Manual
    // We create a Trc from storage (Factory::Manual), clone it, and verify the factory is properly cloned
    use std::sync::{Arc, RwLock};

    let affinities = pinned_affinities(&[2]);
    let affinity1 = affinities[0];

    // Create a storage and populate it with a value for affinity1
    let mut storage = super::storage::Storage::new();
    let value = Arc::new(200);
    storage.replace(affinity1, Arc::clone(&value));

    let storage_arc = Arc::new(RwLock::new(storage));

    // Create a Trc from storage - this uses Factory::Manual
    let trc1 = PerCore::from_storage(Arc::clone(&storage_arc), affinity1);

    // Clone the Trc - this should exercise line 143 in the Factory::clone method
    let trc2 = trc1.clone();

    // Verify both Trcs work correctly
    assert_eq!(*trc1, 200);
    assert_eq!(*trc2, 200);

    // Both should point to the same Arc
    assert!(Arc::ptr_eq(&trc1.into_arc(), &trc2.into_arc()));
}

#[test]
fn test_factory_manual_relocated() {
    // This test covers line 453: Factory::Manual branch in relocated()
    // When a Trc is created from storage (Factory::Manual) and relocated to a new affinity,
    // it should behave like sync::Arc<T> and just clone the value without creating new data
    use std::sync::{Arc, RwLock};

    let affinities = pinned_affinities(&[2]);
    let affinity1 = affinities[0];
    let affinity2 = affinities[1];

    // Create a storage with a value at affinity1
    let mut storage = super::storage::Storage::new();
    let value = Arc::new(100);
    storage.replace(affinity1, Arc::clone(&value));

    let storage_arc = Arc::new(RwLock::new(storage));

    // Create a Trc from storage - this uses Factory::Manual
    let trc = PerCore::from_storage(Arc::clone(&storage_arc), affinity1);
    assert_eq!(*trc, 100);

    // Relocate to affinity2 where no data exists
    // This should trigger line 453 (Factory::Manual branch)
    // and behave like Arc<T> by just cloning the reference
    let mut trc_relocated = trc;
    trc_relocated.relocate(Some(affinity1), affinity2);

    // The value should still be 100
    assert_eq!(*trc_relocated, 100);

    // The relocated Trc should point to the same Arc as the original
    // because Factory::Manual just clones the Arc
    assert!(Arc::ptr_eq(&trc_relocated.into_arc(), &value));
}

#[test]
fn test_relocated_unknown_source() {
    let affinities = pinned_affinities(&[2]);

    let source = None;
    let destination = affinities[1];

    let mut trc = PerCore::with_value(42);

    trc.relocate(source, destination);
    assert_eq!(*trc, 42);
}

#[test]
fn test_strong_count() {
    // Test strong_count with a single reference
    let arc = PerCore::new(Counter::new);
    assert_eq!(PerCore::strong_count(&arc), 1);

    // Test strong_count with multiple references
    let arc2 = arc.clone();
    assert_eq!(PerCore::strong_count(&arc), 2);
    assert_eq!(PerCore::strong_count(&arc2), 2);

    let arc3 = arc.clone();
    assert_eq!(PerCore::strong_count(&arc), 3);
    assert_eq!(PerCore::strong_count(&arc2), 3);
    assert_eq!(PerCore::strong_count(&arc3), 3);

    // Test strong_count after dropping a reference
    drop(arc2);
    assert_eq!(PerCore::strong_count(&arc), 2);
    assert_eq!(PerCore::strong_count(&arc3), 2);

    drop(arc3);
    assert_eq!(PerCore::strong_count(&arc), 1);
}

#[test]
fn test_strong_count_after_relocation() {
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create an Arc with multiple strong references
    let arc1 = PerCore::new(Counter::new);
    let arc2 = arc1.clone();
    assert_eq!(PerCore::strong_count(&arc1), 2);

    // Relocate one of them
    let mut arc1_relocated = arc1;
    arc1_relocated.relocate(affinity1, affinity2);

    // After relocation:
    // - arc1_relocated holds a reference to a new Arc created for affinity2
    // - The storage at affinity2 also holds a reference, but strong_count excludes internal refs
    // - Therefore, strong_count for arc1_relocated is 1
    assert_eq!(PerCore::strong_count(&arc1_relocated), 1);

    // arc2 refers to the original Arc at affinity1
    // - arc2 itself holds a reference
    // - The storage at affinity1 also holds a reference, but strong_count excludes internal refs
    // - Therefore, strong_count for arc2 is 1
    assert_eq!(PerCore::strong_count(&arc2), 1);
}

#[test]
fn test_strong_count_with_deduplication() {
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create an Arc and clone it
    let arc1 = PerCore::new(Counter::new);
    let arc2 = arc1.clone();

    // Relocate both to the same destination
    // They should share the same underlying Arc in the destination
    let mut arc1_relocated = arc1;
    arc1_relocated.relocate(affinity1, affinity2);
    let mut arc2_relocated = arc2;
    arc2_relocated.relocate(affinity1, affinity2);

    // Both should point to the same underlying Arc (deduplication)
    // The strong count includes:
    // - arc1_relocated (1)
    // - arc2_relocated (1)
    // Storage reference at affinity2 is excluded by strong_count
    assert_eq!(PerCore::strong_count(&arc1_relocated), 2);
    assert_eq!(PerCore::strong_count(&arc2_relocated), 2);
}

#[test]
fn test_strong_count_independent_across_affinities() {
    let affinities = pinned_affinities(&[2]);
    let affinity1 = Some(affinities[0]);
    let affinity2 = affinities[1];

    // Create an Arc on affinity1 with strong_count = 1
    let arc_a = PerCore::new(Counter::new);
    assert_eq!(PerCore::strong_count(&arc_a), 1);

    // Relocate to affinity2, creating a separate instance there
    let mut arc_b = arc_a.clone();
    arc_b.relocate(affinity1, affinity2);
    assert_eq!(PerCore::strong_count(&arc_b), 1); // arc_b only; storage ref excluded

    // Clone arc_a on affinity1 - this should NOT affect arc_b on affinity2
    let arc_a2 = arc_a.clone();
    // arc_a is now referenced by:
    // - arc_a itself
    // - arc_a2
    // Storage at affinity1 also holds a reference, but strong_count excludes internal refs
    assert_eq!(PerCore::strong_count(&arc_a), 2);
    assert_eq!(PerCore::strong_count(&arc_a2), 2);
    // arc_b on affinity2 is unaffected by the clone on affinity1
    assert_eq!(PerCore::strong_count(&arc_b), 1); // Still 1; unaffected by clone on affinity1
}

#[test]
fn test_relocated_source_equals_destination_does_not_corrupt_storage() {
    // Regression test: when source == destination, Arc::relocated() must NOT overwrite the
    // newly-created value in storage with the stale pre-relocation value.
    let affinities = pinned_affinities(&[2]);
    let affinity = affinities[0];

    // Create an Arc whose Counter starts at zero, then advance it.
    let arc = PerCore::new(Counter::new);
    arc.increment_by(42);
    assert_eq!(arc.value(), 42);

    // Relocate with source == destination.  The ThreadAware impl always creates a *new*
    // Counter (value resets to 0), so `relocate` must result in 0 and must also leave
    // storage holding 0 (not the stale 42).
    let mut arc = arc;
    arc.relocate(Some(affinity), affinity);
    assert_eq!(arc.value(), 0, "relocated value should come from factory");

    // A second relocation from the same slot must find the factory-created value (0) in
    // storage, not the stale pre-relocation value (42).  Before the bug fix, the first
    // relocated() call wrote the stale Arc<Counter(42)> back into the storage slot,
    // so the second call's `get_clone` fast-path would return 42 instead of 0.
    arc.relocate(Some(affinity), affinity);
    assert_eq!(
        arc.value(),
        0,
        "subsequent relocation must not see stale pre-relocation value from storage"
    );
}

#[test]
fn with_clone_fn_relocates_clone() {
    let affinities = pinned_affinities(&[2]);
    let source = Some(affinities[0]);
    let destination = affinities[1];

    // Counter::relocated resets value to 0, so we can detect if it was called.
    let arc = super::Arc::<Counter, crate::PerCore>::with_clone_fn(Counter::new(), |c: &Counter| Box::new(c.clone()));

    arc.increment_by(42);
    assert_eq!(arc.value(), 42);

    // Relocating should clone the Counter and call relocated() on the clone,
    // which resets the value to 0.
    let mut arc = arc;
    arc.relocate(source, destination);
    assert_eq!(arc.value(), 0, "relocated() must be called on the clone");
}

#[test]
fn with_clone_fn_dyn_trait_relocates_correctly() {
    // Exercises the ErasedCloneFn path with a dyn Trait object, which is the
    // primary use case. The unsafe &T -> &V cast inside CloneAdapter is
    // exercised here and validated under Miri.
    trait Plugin: ThreadAware + Send + Sync {
        fn name(&self) -> &str;
    }

    #[derive(Clone)]
    struct MyPlugin(String);

    impl Plugin for MyPlugin {
        fn name(&self) -> &str {
            &self.0
        }
    }

    impl ThreadAware for MyPlugin {
        fn relocate(&mut self, _source: Option<Affinity>, _destination: Affinity) {
            self.0 = format!("{}-relocated", self.0);
        }
    }

    let affinities = pinned_affinities(&[2]);
    let source = Some(affinities[0]);
    let destination = affinities[1];

    let arc = super::Arc::<dyn Plugin, crate::PerCore>::with_clone_fn(MyPlugin("orig".into()), |p: &MyPlugin| Box::new(p.clone()));

    assert_eq!(arc.name(), "orig");

    let mut arc = arc;
    arc.relocate(source, destination);
    assert_eq!(arc.name(), "orig-relocated");
}

#[test]
fn with_clone_fn_clone_and_relocate_independently() {
    // Cloning an Arc backed by ErasedCloneFn should produce independent
    // clones that can each be relocated separately.
    let affinities = pinned_affinities(&[3]);
    let source = Some(affinities[0]);
    let dest1 = affinities[1];
    let dest2 = affinities[2];

    let arc = super::Arc::<Counter, crate::PerCore>::with_clone_fn(Counter::new(), |c: &Counter| Box::new(c.clone()));
    arc.increment_by(10);

    let mut clone1 = arc.clone();
    #[expect(clippy::redundant_clone, reason = "testing independent clones")]
    let mut clone2 = arc.clone();

    clone1.relocate(source, dest1);
    clone2.relocate(source, dest2);

    // Both should have been reset by Counter::relocate
    assert_eq!(clone1.value(), 0);
    assert_eq!(clone2.value(), 0);
}

#[test]
fn with_clone_fn_repeated_relocations() {
    // Multiple sequential relocations through the same ErasedCloneFn factory
    // must all produce correct clones.
    let affinities = pinned_affinities(&[4]);

    let arc = super::Arc::<Counter, crate::PerCore>::with_clone_fn(Counter::new(), |c: &Counter| Box::new(c.clone()));
    arc.increment_by(99);

    let mut current = arc;
    for i in 0..3 {
        let source = Some(affinities[i]);
        let dest = affinities[i + 1];
        current.relocate(source, dest);
        // Counter resets to 0 on relocate
        assert_eq!(current.value(), 0, "relocation {i} should reset counter");
        current.increment_by(i32::try_from(i + 1).expect("loop index fits in i32"));
    }
    assert_eq!(current.value(), 3);
}

#[test]
fn with_clone_fn_debug_format() {
    // Exercises Debug formatting of the ErasedCloneFn factory path.
    let arc = super::Arc::<Counter, crate::PerCore>::with_clone_fn(Counter::new(), |c: &Counter| Box::new(c.clone()));
    let debug = format!("{arc:?}");
    assert!(!debug.is_empty());
}

#[test]
fn with_clone_fn_deduplication_across_clones() {
    // Two clones relocated to the same destination should share the same
    // underlying value via storage deduplication.
    let affinities = pinned_affinities(&[2]);
    let source = Some(affinities[0]);
    let dest = affinities[1];

    let arc = super::Arc::<Counter, crate::PerCore>::with_clone_fn(Counter::new(), |c: &Counter| Box::new(c.clone()));
    let clone1 = arc.clone();
    #[expect(clippy::redundant_clone, reason = "testing independent clones")]
    let clone2 = arc.clone();

    let mut r1 = clone1;
    r1.relocate(source, dest);
    let mut r2 = clone2;
    r2.relocate(source, dest);

    assert!(sync::Arc::ptr_eq(&r1.clone().into_arc(), &r2.clone().into_arc()));
}

#[test]
fn with_clone_fn_debug_includes_erased() {
    // Exercises Debug for ErasedCloneFn (clone_fn.rs) and Factory::ErasedCloneFn (factory.rs)
    let arc = super::Arc::<Counter, crate::PerCore>::with_clone_fn(Counter::new(), |c: &Counter| Box::new(c.clone()));
    let dbg = format!("{arc:?}");
    assert!(
        dbg.contains("factory: Clone"),
        "Debug output should mention factory Clone variant: {dbg}"
    );
}

#[test]
fn factory_data_debug() {
    // Exercises Factory::Data debug branch
    let arc = PerCore::from_unaware(42);
    let dbg = format!("{arc:?}");
    assert!(dbg.contains("Data"), "Debug output should mention Data variant: {dbg}");
}

#[test]
fn factory_manual_debug() {
    // Exercises Factory::Manual debug branch (from_storage)
    use std::sync::{self, RwLock};

    let affinities = pinned_affinities(&[1]);
    let storage = sync::Arc::new(RwLock::new(super::storage::Storage::new()));
    storage
        .write()
        .expect("lock should not be poisoned")
        .replace(affinities[0], sync::Arc::new(42));
    let arc = super::Arc::<i32, crate::PerCore>::from_storage(storage, affinities[0]);
    let dbg = format!("{arc:?}");
    assert!(dbg.contains("Manual"), "Debug output should mention Manual variant: {dbg}");
}

#[test]
fn factory_closure_debug() {
    // Exercises Factory::Closure debug branch (from Arc::new)
    let arc = PerCore::new(Counter::new);
    let dbg = format!("{arc:?}");
    assert!(dbg.contains("Closure"), "Debug output should mention Closure variant: {dbg}");
}

#[test]
fn new_boxed_relocate() {
    // Exercises Ctor<T>::relocate (the no-op ThreadAware impl inside new_boxed)
    let affinities = pinned_affinities(&[2]);
    let mut arc = super::Arc::<Counter, crate::PerCore>::new_boxed(|| Box::new(Counter::new()));
    arc.relocate(Some(affinities[0]), affinities[1]);
    assert_eq!(arc.value(), 0, "new_boxed relocate should create a fresh counter");
}