oxidart 0.3.0

A very fast Tree that support get/set/del/ttl operations some variant
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
use bytes::Bytes;

use crate::OxidArt;

#[test]
fn test_get_set_basic() {
    let mut art = OxidArt::new();
    let key = Bytes::from_static(b"Joshua");
    let val = Bytes::from_static(b"BOUCHAT");
    art.set(key.clone(), val.clone());
    assert_eq!(art.get(key), Some(val));
}

#[test]
fn test_empty_key() {
    let mut art = OxidArt::new();
    let key = Bytes::from_static(b"");
    let val = Bytes::from_static(b"root_value");
    art.set(key.clone(), val.clone());
    assert_eq!(art.get(key), Some(val));
}

#[test]
fn test_get_nonexistent() {
    let mut art = OxidArt::new();
    assert_eq!(art.get(Bytes::from_static(b"missing")), None);
}

#[test]
fn test_overwrite_value() {
    let mut art = OxidArt::new();
    let key = Bytes::from_static(b"key");
    let val1 = Bytes::from_static(b"value1");
    let val2 = Bytes::from_static(b"value2");

    art.set(key.clone(), val1.clone());
    assert_eq!(art.get(key.clone()), Some(val1));

    art.set(key.clone(), val2.clone());
    assert_eq!(art.get(key), Some(val2));
}

#[test]
fn test_common_prefix_split() {
    // Test le split: "user" et "uso" partagent "us"
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"user"), Bytes::from_static(b"val_user"));
    art.set(Bytes::from_static(b"uso"), Bytes::from_static(b"val_uso"));

    assert_eq!(
        art.get(Bytes::from_static(b"user")),
        Some(Bytes::from_static(b"val_user"))
    );
    assert_eq!(
        art.get(Bytes::from_static(b"uso")),
        Some(Bytes::from_static(b"val_uso"))
    );
    // "us" n'a pas de valeur
    assert_eq!(art.get(Bytes::from_static(b"us")), None);
}

#[test]
fn test_prefix_is_also_key() {
    // "us" est un préfixe de "user" mais aussi une clé
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"user"), Bytes::from_static(b"val_user"));
    art.set(Bytes::from_static(b"us"), Bytes::from_static(b"val_us"));

    assert_eq!(
        art.get(Bytes::from_static(b"user")),
        Some(Bytes::from_static(b"val_user"))
    );
    assert_eq!(
        art.get(Bytes::from_static(b"us")),
        Some(Bytes::from_static(b"val_us"))
    );
}

#[test]
fn test_multiple_branches() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"apple"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"application"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"banana"), Bytes::from_static(b"3"));
    art.set(Bytes::from_static(b"band"), Bytes::from_static(b"4"));

    assert_eq!(
        art.get(Bytes::from_static(b"apple")),
        Some(Bytes::from_static(b"1"))
    );
    assert_eq!(
        art.get(Bytes::from_static(b"application")),
        Some(Bytes::from_static(b"2"))
    );
    assert_eq!(
        art.get(Bytes::from_static(b"banana")),
        Some(Bytes::from_static(b"3"))
    );
    assert_eq!(
        art.get(Bytes::from_static(b"band")),
        Some(Bytes::from_static(b"4"))
    );

    // Clés partielles qui n'existent pas
    assert_eq!(art.get(Bytes::from_static(b"app")), None);
    assert_eq!(art.get(Bytes::from_static(b"ban")), None);
}

#[test]
fn test_del_basic() {
    let mut art = OxidArt::new();
    let key = Bytes::from_static(b"hello");
    let val = Bytes::from_static(b"world");

    art.set(key.clone(), val.clone());
    assert_eq!(art.get(key.clone()), Some(val.clone()));

    let deleted = art.del(key.clone());
    assert_eq!(deleted, Some(val));
    assert_eq!(art.get(key), None);
}

#[test]
fn test_del_nonexistent() {
    let mut art = OxidArt::new();
    assert_eq!(art.del(Bytes::from_static(b"missing")), None);
}

#[test]
fn test_del_empty_key() {
    let mut art = OxidArt::new();
    let val = Bytes::from_static(b"root");

    art.set(Bytes::from_static(b""), val.clone());
    assert_eq!(art.del(Bytes::from_static(b"")), Some(val));
    assert_eq!(art.get(Bytes::from_static(b"")), None);
}

#[test]
fn test_del_with_recompression() {
    // us -> {er, o}  après del("uso") -> "user"
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"user"), Bytes::from_static(b"val_user"));
    art.set(Bytes::from_static(b"uso"), Bytes::from_static(b"val_uso"));

    // Supprimer "uso"
    let deleted = art.del(Bytes::from_static(b"uso"));
    assert_eq!(deleted, Some(Bytes::from_static(b"val_uso")));

    // "user" doit toujours exister
    assert_eq!(
        art.get(Bytes::from_static(b"user")),
        Some(Bytes::from_static(b"val_user"))
    );
    // "uso" n'existe plus
    assert_eq!(art.get(Bytes::from_static(b"uso")), None);
}

#[test]
fn test_del_intermediate_node_with_children() {
    // Supprimer un node intermédiaire qui a des enfants
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"a"), Bytes::from_static(b"val_a"));
    art.set(Bytes::from_static(b"ab"), Bytes::from_static(b"val_ab"));
    art.set(Bytes::from_static(b"abc"), Bytes::from_static(b"val_abc"));

    // Supprimer "ab" qui est intermédiaire
    let deleted = art.del(Bytes::from_static(b"ab"));
    assert_eq!(deleted, Some(Bytes::from_static(b"val_ab")));

    // "a" et "abc" doivent toujours exister
    assert_eq!(
        art.get(Bytes::from_static(b"a")),
        Some(Bytes::from_static(b"val_a"))
    );
    assert_eq!(
        art.get(Bytes::from_static(b"abc")),
        Some(Bytes::from_static(b"val_abc"))
    );
    assert_eq!(art.get(Bytes::from_static(b"ab")), None);
}

#[test]
fn test_many_keys_same_prefix() {
    let mut art = OxidArt::new();

    // Beaucoup de clés avec le même préfixe pour tester les huge_childs
    for i in 0..20u8 {
        let key = Bytes::from(vec![b'x', i]);
        let val = Bytes::from(vec![i]);
        art.set(key, val);
    }

    for i in 0..20u8 {
        let key = Bytes::from(vec![b'x', i]);
        let expected = Bytes::from(vec![i]);
        assert_eq!(art.get(key), Some(expected));
    }
}

#[test]
fn test_long_keys() {
    let mut art = OxidArt::new();

    let key1 = Bytes::from(vec![b'a'; 100]);
    let key2 = Bytes::from(vec![b'a'; 50]);
    let val1 = Bytes::from_static(b"long");
    let val2 = Bytes::from_static(b"medium");

    art.set(key1.clone(), val1.clone());
    art.set(key2.clone(), val2.clone());

    assert_eq!(art.get(key1), Some(val1));
    assert_eq!(art.get(key2), Some(val2));
}

#[test]
fn test_del_then_reinsert() {
    let mut art = OxidArt::new();
    let key = Bytes::from_static(b"key");
    let val1 = Bytes::from_static(b"val1");
    let val2 = Bytes::from_static(b"val2");

    art.set(key.clone(), val1.clone());
    art.del(key.clone());
    art.set(key.clone(), val2.clone());

    assert_eq!(art.get(key), Some(val2));
}

#[test]
fn test_del_all_keys() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"a"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"b"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"c"), Bytes::from_static(b"3"));

    art.del(Bytes::from_static(b"a"));
    art.del(Bytes::from_static(b"b"));
    art.del(Bytes::from_static(b"c"));

    assert_eq!(art.get(Bytes::from_static(b"a")), None);
    assert_eq!(art.get(Bytes::from_static(b"b")), None);
    assert_eq!(art.get(Bytes::from_static(b"c")), None);
}

#[test]
fn test_partial_key_not_found() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"hello_world"),
        Bytes::from_static(b"val"),
    );

    // Clés partielles ne doivent pas matcher
    assert_eq!(art.get(Bytes::from_static(b"hello")), None);
    assert_eq!(art.get(Bytes::from_static(b"hello_")), None);
    assert_eq!(art.get(Bytes::from_static(b"hello_worl")), None);
    // Clé trop longue non plus
    assert_eq!(art.get(Bytes::from_static(b"hello_world!")), None);
}

// ============ Tests pour getn ============

#[test]
fn test_getn_basic() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"alice_data"),
    );
    art.set(
        Bytes::from_static(b"user:bob"),
        Bytes::from_static(b"bob_data"),
    );
    art.set(
        Bytes::from_static(b"user:charlie"),
        Bytes::from_static(b"charlie_data"),
    );
    art.set(Bytes::from_static(b"post:1"), Bytes::from_static(b"post_1"));

    let results = art.getn(Bytes::from_static(b"user:"));

    assert_eq!(results.len(), 3);
    assert!(results.contains(&(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"alice_data")
    )));
    assert!(results.contains(&(
        Bytes::from_static(b"user:bob"),
        Bytes::from_static(b"bob_data")
    )));
    assert!(results.contains(&(
        Bytes::from_static(b"user:charlie"),
        Bytes::from_static(b"charlie_data")
    )));
}

#[test]
fn test_getn_empty_prefix() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"a"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"b"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"c"), Bytes::from_static(b"3"));

    let results = art.getn(Bytes::from_static(b""));

    assert_eq!(results.len(), 3);
}

#[test]
fn test_getn_no_match() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"data"),
    );

    let results = art.getn(Bytes::from_static(b"post:"));

    assert!(results.is_empty());
}

#[test]
fn test_getn_exact_key() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"user"), Bytes::from_static(b"user_val"));
    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"alice_val"),
    );

    // Préfixe exact "user" doit retourner "user" et "user:alice"
    let results = art.getn(Bytes::from_static(b"user"));

    assert_eq!(results.len(), 2);
    assert!(results.contains(&(Bytes::from_static(b"user"), Bytes::from_static(b"user_val"))));
    assert!(results.contains(&(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"alice_val")
    )));
}

#[test]
fn test_getn_prefix_in_compression() {
    // Test quand le préfixe se termine au milieu d'une compression
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"application"),
        Bytes::from_static(b"app_val"),
    );

    // "app" est un préfixe de "application"
    let results = art.getn(Bytes::from_static(b"app"));

    assert_eq!(results.len(), 1);
    assert!(results.contains(&(
        Bytes::from_static(b"application"),
        Bytes::from_static(b"app_val")
    )));
}

#[test]
fn test_getn_with_nested_keys() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"a"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"ab"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"abc"), Bytes::from_static(b"3"));
    art.set(Bytes::from_static(b"abcd"), Bytes::from_static(b"4"));
    art.set(Bytes::from_static(b"abd"), Bytes::from_static(b"5"));

    let results = art.getn(Bytes::from_static(b"ab"));

    assert_eq!(results.len(), 4); // ab, abc, abcd, abd
    assert!(results.contains(&(Bytes::from_static(b"ab"), Bytes::from_static(b"2"))));
    assert!(results.contains(&(Bytes::from_static(b"abc"), Bytes::from_static(b"3"))));
    assert!(results.contains(&(Bytes::from_static(b"abcd"), Bytes::from_static(b"4"))));
    assert!(results.contains(&(Bytes::from_static(b"abd"), Bytes::from_static(b"5"))));
}

#[test]
fn test_getn_single_char_prefix() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"aa"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"ab"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"ba"), Bytes::from_static(b"3"));

    let results = art.getn(Bytes::from_static(b"a"));

    assert_eq!(results.len(), 2);
    assert!(results.contains(&(Bytes::from_static(b"aa"), Bytes::from_static(b"1"))));
    assert!(results.contains(&(Bytes::from_static(b"ab"), Bytes::from_static(b"2"))));
}

#[test]
fn test_getn_many_children() {
    let mut art = OxidArt::new();

    // Plus de 10 enfants pour tester huge_childs
    for i in 0..20u8 {
        let key = Bytes::from(vec![b'x', b':', i]);
        let val = Bytes::from(vec![i]);
        art.set(key, val);
    }

    let results = art.getn(Bytes::from_static(b"x:"));

    assert_eq!(results.len(), 20);
}

// ============ Tests pour deln ============

#[test]
fn test_deln_basic() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"alice_data"),
    );
    art.set(
        Bytes::from_static(b"user:bob"),
        Bytes::from_static(b"bob_data"),
    );
    art.set(
        Bytes::from_static(b"user:charlie"),
        Bytes::from_static(b"charlie_data"),
    );
    art.set(Bytes::from_static(b"post:1"), Bytes::from_static(b"post_1"));

    let deleted = art.deln(Bytes::from_static(b"user:"));

    assert_eq!(deleted, 3);
    assert_eq!(art.get(Bytes::from_static(b"user:alice")), None);
    assert_eq!(art.get(Bytes::from_static(b"user:bob")), None);
    assert_eq!(art.get(Bytes::from_static(b"user:charlie")), None);
    // post:1 doit toujours exister
    assert_eq!(
        art.get(Bytes::from_static(b"post:1")),
        Some(Bytes::from_static(b"post_1"))
    );
}

#[test]
fn test_deln_empty_prefix() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"a"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"b"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"c"), Bytes::from_static(b"3"));

    let deleted = art.deln(Bytes::from_static(b""));

    assert_eq!(deleted, 3);
    assert_eq!(art.get(Bytes::from_static(b"a")), None);
    assert_eq!(art.get(Bytes::from_static(b"b")), None);
    assert_eq!(art.get(Bytes::from_static(b"c")), None);
}

#[test]
fn test_deln_no_match() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"data"),
    );

    let deleted = art.deln(Bytes::from_static(b"post:"));

    assert_eq!(deleted, 0);
    // user:alice doit toujours exister
    assert_eq!(
        art.get(Bytes::from_static(b"user:alice")),
        Some(Bytes::from_static(b"data"))
    );
}

#[test]
fn test_deln_exact_key_with_children() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"user"), Bytes::from_static(b"user_val"));
    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"alice_val"),
    );
    art.set(
        Bytes::from_static(b"user:bob"),
        Bytes::from_static(b"bob_val"),
    );

    // Supprimer "user" et tous ses descendants
    let deleted = art.deln(Bytes::from_static(b"user"));

    assert_eq!(deleted, 3);
    assert_eq!(art.get(Bytes::from_static(b"user")), None);
    assert_eq!(art.get(Bytes::from_static(b"user:alice")), None);
    assert_eq!(art.get(Bytes::from_static(b"user:bob")), None);
}

#[test]
fn test_deln_prefix_in_compression() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"application"),
        Bytes::from_static(b"app_val"),
    );
    art.set(
        Bytes::from_static(b"apple"),
        Bytes::from_static(b"apple_val"),
    );

    // "app" est un préfixe commun
    let deleted = art.deln(Bytes::from_static(b"app"));

    assert_eq!(deleted, 2);
    assert_eq!(art.get(Bytes::from_static(b"application")), None);
    assert_eq!(art.get(Bytes::from_static(b"apple")), None);
}

#[test]
fn test_deln_with_nested_keys() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"a"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"ab"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"abc"), Bytes::from_static(b"3"));
    art.set(Bytes::from_static(b"abcd"), Bytes::from_static(b"4"));
    art.set(Bytes::from_static(b"abd"), Bytes::from_static(b"5"));
    art.set(Bytes::from_static(b"b"), Bytes::from_static(b"6"));

    let deleted = art.deln(Bytes::from_static(b"ab"));

    assert_eq!(deleted, 4); // ab, abc, abcd, abd
    assert_eq!(
        art.get(Bytes::from_static(b"a")),
        Some(Bytes::from_static(b"1"))
    );
    assert_eq!(art.get(Bytes::from_static(b"ab")), None);
    assert_eq!(art.get(Bytes::from_static(b"abc")), None);
    assert_eq!(
        art.get(Bytes::from_static(b"b")),
        Some(Bytes::from_static(b"6"))
    );
}

#[test]
fn test_deln_many_children() {
    let mut art = OxidArt::new();

    // Plus de 10 enfants pour tester huge_childs
    for i in 0..20u8 {
        let key = Bytes::from(vec![b'x', b':', i]);
        let val = Bytes::from(vec![i]);
        art.set(key, val);
    }

    let deleted = art.deln(Bytes::from_static(b"x:"));

    assert_eq!(deleted, 20);

    // Vérifier qu'ils sont tous supprimés
    for i in 0..20u8 {
        let key = Bytes::from(vec![b'x', b':', i]);
        assert_eq!(art.get(key), None);
    }
}

#[test]
fn test_deln_then_insert() {
    let mut art = OxidArt::new();

    art.set(
        Bytes::from_static(b"user:alice"),
        Bytes::from_static(b"old"),
    );
    art.deln(Bytes::from_static(b"user:"));

    // Réinsérer après suppression
    art.set(Bytes::from_static(b"user:bob"), Bytes::from_static(b"new"));

    assert_eq!(art.get(Bytes::from_static(b"user:alice")), None);
    assert_eq!(
        art.get(Bytes::from_static(b"user:bob")),
        Some(Bytes::from_static(b"new"))
    );
}

#[test]
fn test_deln_partial_match() {
    let mut art = OxidArt::new();

    art.set(Bytes::from_static(b"hello"), Bytes::from_static(b"1"));
    art.set(Bytes::from_static(b"help"), Bytes::from_static(b"2"));
    art.set(Bytes::from_static(b"world"), Bytes::from_static(b"3"));

    // "hel" matche "hello" et "help"
    let deleted = art.deln(Bytes::from_static(b"hel"));

    assert_eq!(deleted, 2);
    assert_eq!(art.get(Bytes::from_static(b"hello")), None);
    assert_eq!(art.get(Bytes::from_static(b"help")), None);
    assert_eq!(
        art.get(Bytes::from_static(b"world")),
        Some(Bytes::from_static(b"3"))
    );
}

// ============ Tests TTL ============

#[cfg(feature = "ttl")]
#[test]
fn test_ttl_expired_on_get() {
    use std::time::Duration;

    let mut art = OxidArt::new();
    art.set_now(0);

    // Insert with short TTL (1 second)
    art.set_ttl(Bytes::from_static(b"expired"), Duration::from_secs(1), Bytes::from_static(b"old"));
    // Insert with longer TTL (100 seconds)
    art.set_ttl(Bytes::from_static(b"valid"), Duration::from_secs(100), Bytes::from_static(b"new"));
    // Insert with no expiry
    art.set(Bytes::from_static(b"forever"), Bytes::from_static(b"eternal"));

    // Move time forward past first TTL
    art.set_now(50);

    // Expired key should return None and be cleaned up
    assert_eq!(art.get(Bytes::from_static(b"expired")), None);
    // Valid key should still work
    assert_eq!(
        art.get(Bytes::from_static(b"valid")),
        Some(Bytes::from_static(b"new"))
    );
    // No expiry key should work
    assert_eq!(
        art.get(Bytes::from_static(b"forever")),
        Some(Bytes::from_static(b"eternal"))
    );

    // Move time forward, valid should expire
    art.set_now(150);
    assert_eq!(art.get(Bytes::from_static(b"valid")), None);
    // No expiry still works
    assert_eq!(
        art.get(Bytes::from_static(b"forever")),
        Some(Bytes::from_static(b"eternal"))
    );
}

#[cfg(feature = "ttl")]
#[test]
fn test_ttl_getn_filters_expired() {
    use std::time::Duration;

    let mut art = OxidArt::new();
    art.set_now(0);

    // Short TTL - will expire
    art.set_ttl(Bytes::from_static(b"user:expired"), Duration::from_secs(1), Bytes::from_static(b"old"));
    // Longer TTL - won't expire
    art.set_ttl(Bytes::from_static(b"user:valid"), Duration::from_secs(100), Bytes::from_static(b"new"));
    // No expiry
    art.set(Bytes::from_static(b"user:forever"), Bytes::from_static(b"eternal"));

    // Move time forward past first TTL
    art.set_now(50);

    let results = art.getn(Bytes::from_static(b"user:"));

    // Should only return 2 (valid and forever), not the expired one
    assert_eq!(results.len(), 2);
    assert!(!results.iter().any(|(k, _)| k == &Bytes::from_static(b"user:expired")));
    assert!(results.iter().any(|(k, _)| k == &Bytes::from_static(b"user:valid")));
    assert!(results.iter().any(|(k, _)| k == &Bytes::from_static(b"user:forever")));
}

#[cfg(feature = "ttl")]
#[test]
fn test_ttl_cleanup_on_expired_get() {
    use std::time::Duration;

    let mut art = OxidArt::new();
    art.set_now(0);

    // Create a path: user -> er (with short TTL)
    art.set_ttl(Bytes::from_static(b"user"), Duration::from_secs(1), Bytes::from_static(b"expired_user"));
    // Longer TTL
    art.set_ttl(Bytes::from_static(b"username"), Duration::from_secs(100), Bytes::from_static(b"valid"));

    // Move time forward
    art.set_now(50);

    // Get the expired key - should trigger cleanup
    assert_eq!(art.get(Bytes::from_static(b"user")), None);

    // The valid key should still work
    assert_eq!(
        art.get(Bytes::from_static(b"username")),
        Some(Bytes::from_static(b"valid"))
    );
}

#[cfg(feature = "ttl")]
#[test]
fn test_evict_expired_basic() {
    use std::time::Duration;

    let mut art = OxidArt::new();
    art.set_now(0);

    // Insert 50 keys with short TTL
    for i in 0..50u8 {
        let key = Bytes::from(vec![b'k', i]);
        art.set_ttl(key, Duration::from_secs(1), Bytes::from_static(b"val"));
    }

    // Insert 10 keys with long TTL
    for i in 0..10u8 {
        let key = Bytes::from(vec![b'l', i]);
        art.set_ttl(key, Duration::from_secs(1000), Bytes::from_static(b"val"));
    }

    // Insert 10 keys without TTL
    for i in 0..10u8 {
        let key = Bytes::from(vec![b'n', i]);
        art.set(key, Bytes::from_static(b"val"));
    }

    // Move time forward - short TTL keys are now expired
    art.set_now(100);

    // Evict expired entries (may need multiple calls due to probabilistic sampling)
    let mut total_evicted = 0;
    for _ in 0..10 {
        let evicted = art.evict_expired();
        total_evicted += evicted;
        if evicted == 0 {
            break;
        }
    }

    // Should have evicted all 50 expired keys
    assert_eq!(total_evicted, 50);

    // Long TTL keys should still exist
    for i in 0..10u8 {
        let key = Bytes::from(vec![b'l', i]);
        assert_eq!(art.get(key), Some(Bytes::from_static(b"val")));
    }

    // No TTL keys should still exist
    for i in 0..10u8 {
        let key = Bytes::from(vec![b'n', i]);
        assert_eq!(art.get(key), Some(Bytes::from_static(b"val")));
    }
}

#[cfg(feature = "ttl")]
#[test]
fn test_evict_expired_partial() {
    use std::time::Duration;

    let mut art = OxidArt::new();
    art.set_now(0);

    // Insert 10 keys with short TTL (will expire)
    for i in 0..10u8 {
        let key = Bytes::from(vec![b'e', i]);
        art.set_ttl(key, Duration::from_secs(1), Bytes::from_static(b"val"));
    }

    // Insert 90 keys with long TTL (won't expire)
    for i in 0..90u8 {
        let key = Bytes::from(vec![b'v', i]);
        art.set_ttl(key, Duration::from_secs(1000), Bytes::from_static(b"val"));
    }

    // Move time forward
    art.set_now(100);

    // Evict - should stop after one round since < 25% expired
    let evicted = art.evict_expired();

    // Should have evicted at most the 10 expired keys
    assert!(evicted <= 10);
}

// ============ Tests avec dictionnaire français ============