hash_cons 0.2.0

A type-safe hash-cons library.
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
807
#[cfg(not(feature = "single-threaded"))]
#[cfg(test)]
mod thread_safe_tests {
    use hash_cons::Hc;

    #[derive(Hash, PartialEq, Eq, Clone)]
    enum BoolExpr {
        Const(bool),
        And(Hc<BoolExpr>, Hc<BoolExpr>),
        Or(Hc<BoolExpr>, Hc<BoolExpr>),
        Not(Hc<BoolExpr>),
    }
    mod single_tests {
        use crate::thread_safe_tests::BoolExpr;
        use hash_cons::{Hc, HcTable};
        use rand::Rng;
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        /// Test case for basic hashconsing of a simple constant value.
        #[test]
        fn test_basic_const_hashconsing() {
            let table: HcTable<BoolExpr> = HcTable::new();
            let expr_true = BoolExpr::Const(true);
            let expr_true_v2 = BoolExpr::Const(true);
            let ahc_true: Hc<BoolExpr> = table.hashcons(expr_true);
            let ahc_true_v2: Hc<BoolExpr> = table.hashcons(expr_true_v2);
            let ahc_not_true = table.hashcons(BoolExpr::Not(ahc_true.clone()));

            assert!(
                ahc_true == ahc_true_v2,
                "Identical constants should share the same hashconsed reference."
            );
            assert!(
                ahc_true_v2 != ahc_not_true,
                "Not True and False are not same values"
            );
        }

        /// Test case for hashconsing complex boolean expressions.
        #[test]
        fn test_complex_expression_hashconsing() {
            let table = HcTable::new();
            let expr_true = BoolExpr::Const(true);
            let expr_false = BoolExpr::Const(false);
            let ahc_true = table.hashcons(expr_true);
            let ahc_false = table.hashcons(expr_false);
            let expr_and_v1 = BoolExpr::And(ahc_true.clone(), ahc_false.clone());
            let expr_and_v2 = BoolExpr::And(ahc_true, table.hashcons(BoolExpr::Const(false)));
            let ahc_and_v1 = table.hashcons(expr_and_v1);
            let ahc_and_v2 = table.hashcons(expr_and_v2);

            assert!(
                ahc_and_v1 == ahc_and_v2,
                "Identical complex expressions should share the same hashconsed reference."
            );
        }

        #[cfg(not(feature = "auto-cleanup"))]
        #[test]
        fn test_manual_cleanup_effectiveness() {
            let table = HcTable::<BoolExpr>::new();

            // hash cons several values
            let ahc_true = table.hashcons(BoolExpr::Const(true));
            let ahc_false = table.hashcons(BoolExpr::Const(false));
            let ahc_and = table.hashcons(BoolExpr::And(ahc_true.clone(), ahc_false.clone()));

            // Get the size of the table after interning
            let size_before_cleanup = table.len();
            assert_eq!(
                size_before_cleanup, 3,
                "Table should have 3 items after interning"
            );

            drop(ahc_true);
            drop(ahc_false);
            drop(ahc_and);

            // Call cleanup method on the table
            table.cleanup();

            // Get the size of the table after cleanup
            let size_after_cleanup = table.len();

            // Assert that the size of the table has decreased appropriately
            assert_eq!(
                size_after_cleanup, 0,
                "The table size should be zero after cleanup, because values are dropped"
            );
        }

        #[cfg(feature = "auto-cleanup")]
        #[test]
        fn test_auto_cleanup_behavior() {
            let table = HcTable::<BoolExpr>::new();

            // hash cons a value and keep a reference to it
            let expr_true = table.hashcons(BoolExpr::Const(true));

            // Get the size of the table after hash consing
            let size_after_consing = table.len();
            assert_eq!(
                size_after_consing, 1,
                "Table should have 1 item after interning"
            );

            // Drop the reference to the hash consed value
            drop(expr_true);

            // Get the size of the table after dropping the reference
            let size_after_dropping = table.len();

            // Assert that the table is empty after the value is dropped
            assert_eq!(
                size_after_dropping, 0,
                "Table should be empty after dropping the only reference"
            );
        }

        #[test]
        fn test_memory_usage_hash_consing() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();
            let mut data = Vec::new();

            for _ in 0..100 {
                // Add repetitive BoolExpr values
                let expr_true = BoolExpr::Const(true);
                let expr_false = BoolExpr::Const(false);
                data.push(expr_true.clone());
                data.push(expr_false.clone());
                ahc_data.push(table.hashcons(expr_true));
                ahc_data.push(table.hashcons(expr_false));
            }

            assert!(
                table.len() < data.len(),
                "Table length should be way less than the data length"
            );

            assert_eq!(
                table.len(),
                2,
                "Table should only have two values(Const True and Const False)"
            );
        }

        #[test]
        fn test_hash_collision() {
            #[derive(PartialEq, Eq)]
            enum BoolExprHc {
                Const(bool),
                Not(Hc<BoolExprHc>),
            }

            impl Hash for BoolExprHc {
                fn hash<H: Hasher>(&self, state: &mut H) {
                    match self {
                        BoolExprHc::Const(flag) => {
                            // Hash 1 for true, 0 for false
                            let value = if *flag { 1 } else { 0 };
                            value.hash(state);
                        }
                        BoolExprHc::Not(val) => {
                            let mut temp_hasher = DefaultHasher::new();
                            val.hash(&mut temp_hasher);
                            1.hash(state);
                        }
                    }
                }
            }

            let table = HcTable::<BoolExprHc>::new();
            let expr_true = BoolExprHc::Const(true);
            let expr_not_false = BoolExprHc::Not(table.hashcons(BoolExprHc::Const(false)));
            let ahc_true = table.hashcons(expr_true);
            let ahc_not_false = table.hashcons(expr_not_false);

            let mut hasher = DefaultHasher::new();
            ahc_true.hash(&mut hasher);
            let hash_value_ahc_true = hasher.finish();

            // Reinitialize the hasher for a new calculation
            hasher = DefaultHasher::new();
            ahc_not_false.hash(&mut hasher);
            let hash_value_ahc_not_false = hasher.finish();

            assert_eq!(
                hash_value_ahc_true, hash_value_ahc_not_false,
                "Hash values should be equal"
            );
            assert_eq!(table.len(), 3, "Table should have 3 items");
        }

        /*
                #[derive(Debug, Clone, Hash, PartialEq, Eq)]
                struct PanicOnDrop;

                impl Drop for PanicOnDrop {
                    fn drop(&mut self) {
                        panic!("Panic on drop");
                    }
                }

                #[test]
                fn test_reentrant_drop() {
                    let table = HcTable::<PanicOnDrop>::new();

                    // Intern the PanicOnDrop
                    let ahc = table.hashcons(PanicOnDrop);

                    // Use catch_unwind to handle the panic
                    let result = panic::catch_unwind(AssertUnwindSafe(|| {
                        drop(ahc);
                    }));

                    // Assert that a panic occurred
                    assert!(result.is_err(), "A panic should occur on drop");
                }
        */
        #[test]
        fn test_large_unique() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();

            let expr_true = BoolExpr::Const(true);
            let expr_false = BoolExpr::Const(false);

            ahc_data.push(table.hashcons(expr_true));
            ahc_data.push(table.hashcons(expr_false));

            for i in 2..10_000 {
                ahc_data.push(table.hashcons(BoolExpr::Or(
                    ahc_data[i - 1].clone(),
                    ahc_data[i - 2].clone(),
                )));
                ahc_data.push(table.hashcons(BoolExpr::Or(
                    ahc_data[i - 1].clone(),
                    ahc_data[i - 2].clone(),
                )));
            }
            assert_eq!(
                table.len(),
                10_000,
                "table length should be equal to the  data length"
            );
        }

        // Tests hashconsing with a large set of non-unique boolean expressions.
        #[test]
        fn test_large_non_unique() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();

            let expr_true = BoolExpr::Const(true);
            let expr_false = BoolExpr::Const(false);

            ahc_data.push(table.hashcons(expr_true.clone()));
            ahc_data.push(table.hashcons(expr_false.clone()));
            ahc_data.push(table.hashcons(expr_true));
            ahc_data.push(table.hashcons(expr_false));

            for i in 4..10_000 {
                let mut rng = rand::thread_rng();
                let first = rng.gen_range(0..i);
                let second = rng.gen_range(0..i);

                ahc_data.push(table.hashcons(BoolExpr::And(
                    ahc_data[first].clone(),
                    ahc_data[second].clone(),
                )));
            }

            assert!(
                table.len() < 10_000,
                "Data length should always be greater than length of the table"
            );
        }

        /*   #[test]
        fn stress_test_ahc_table() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();
            let expr_true = BoolExpr::Const(true);
            let expr_false = BoolExpr::Const(false);
            ahc_data.push(table.hashcons(expr_true.clone()));
            ahc_data.push(table.hashcons(expr_false.clone()));
            ahc_data.push(table.hashcons(expr_true.clone()));
            ahc_data.push(table.hashcons(expr_false.clone()));

            for i in 3..1_000 {
                let mut rng = rand::thread_rng();
                let first = rng.gen_range(0..i);
                let second = rng.gen_range(0..i);

                if i % 5 == 0 {
                    ahc_data.push(table.hashcons(BoolExpr::And(
                        ahc_data[first].clone(),
                        ahc_data[second].clone(),
                    )));
                } else if i % 3 == 0 {
                    ahc_data.push(table.hashcons(BoolExpr::Or(
                        ahc_data[first].clone(),
                        ahc_data[second].clone(),
                    )));
                } else {
                    ahc_data.push(table.hashcons(BoolExpr::Not(ahc_data[first].clone())));
                }
            }
            drop(ahc_data);
            table.cleanup();

            // Consistency checks
            assert_eq!(
                table.len(),
                0,
                "Table size should be zero because all values are dropped"
            );
        }
        */
    }

    mod multi_threaded_tests {
        use crate::thread_safe_tests::BoolExpr;
        use hash_cons::{Hc, HcTable};
        use rand::Rng;
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        use std::thread;

        #[test]
        fn test_multi_threaded_basic_const_hashconsing() {
            let table = HcTable::new();

            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let expr_true = BoolExpr::Const(true);
                let ahc_true = table_clone.hashcons(expr_true);
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let expr_true_v2 = BoolExpr::Const(true);

            let table_clone = table.clone();
            let thread_handle_ahc_true_v2 = thread::spawn(move || {
                let ahc_true_v2 = table_clone.hashcons(expr_true_v2);
                ahc_true_v2
            });
            let ahc_true_v2 = thread_handle_ahc_true_v2
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let table_clone = table.clone();
            let ahc_true_clone = ahc_true.clone();
            let thread_handle_ahc_not_true = thread::spawn(move || {
                let ahc_not_true = table_clone.hashcons(BoolExpr::Not(ahc_true_clone));
                ahc_not_true
            });
            let ahc_not_true = thread_handle_ahc_not_true.join().expect(
                "Thread should finish and return `Hc<Not <Hc <Const <true>>>>` without panicking",
            );

            assert!(
                ahc_true == ahc_true_v2,
                "Identical constants should share the same hashconsed reference."
            );

            assert!(
                ahc_true_v2 != ahc_not_true,
                "Not True and False are not same values"
            );
        }

        #[test]
        fn test_multi_threaded_complex_expression_hashconsing() {
            let table = HcTable::<BoolExpr>::new();

            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let expr_true = BoolExpr::Const(true);
                let ahc_true = table_clone.hashcons(expr_true);
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let table_clone = table.clone();
            let thread_handle_ahc_false = thread::spawn(move || {
                let expr_false = BoolExpr::Const(false);
                let ahc_false = table_clone.hashcons(expr_false);
                ahc_false
            });
            let ahc_false = thread_handle_ahc_false
                .join()
                .expect("Thread should finish and return `Hc<Const <false>>` without panicking");

            let table_clone = table.clone();
            let thread_handle_ahc_not_false = thread::spawn(move || {
                let expr_not_false = BoolExpr::Not(ahc_false.clone());
                let ahc_not_false = table_clone.hashcons(expr_not_false);
                ahc_not_false
            });
            let ahc_not_false = thread_handle_ahc_not_false.join().expect(
                "Thread should finish and return `Hc<Not <Hc <Const <false>>>>` without panicking",
            );

            let table_clone = table.clone();
            let thread_handle_and = thread::spawn(move || {
                let expr_and = BoolExpr::And(ahc_true.clone(), ahc_not_false);
                table_clone.hashcons(expr_and)
            });
            let _ahc_and = thread_handle_and
                .join()
                .expect("Thread should finish and return `Hc<And <Hc<Const <true>>>, Hc<Not <Hc <Const <false>>>>> `without panicking");

            assert_eq!(
                table.len(),
                4,
                "Table should have 4 item after threading operations"
            );
        }

        #[cfg(not(feature = "auto-cleanup"))]
        #[test]
        fn test_multi_threaded_manual_cleanup_effectiveness() {
            let table = HcTable::<BoolExpr>::new();

            // hash cons several values
            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let ahc_true = table_clone.hashcons(BoolExpr::Const(true));
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let table_clone = table.clone();
            let thread_handle_ahc_false = thread::spawn(move || {
                let ahc_false = table_clone.hashcons(BoolExpr::Const(false));
                ahc_false
            });
            let ahc_false = thread_handle_ahc_false
                .join()
                .expect("Thread should finish and return `Hc<Const <false>>` without panicking");

            let table_clone = table.clone();
            let ahc_true_clone = ahc_true.clone();
            let ahc_false_clone = ahc_false.clone();
            let thread_handle_ahc_and = thread::spawn(move || {
                let ahc_and = table_clone.hashcons(BoolExpr::And(ahc_true_clone, ahc_false_clone));
                ahc_and
            });
            let ahc_and = thread_handle_ahc_and.join().expect(
                "Thread should finish and return `Hc<And <Hc<Const <true>>>, \
                Hc<Const <false>>>>` without panicking",
            );

            // Get the size of the table after interning
            let size_before_cleanup = table.len();
            assert_eq!(
                size_before_cleanup, 3,
                "Table should have 3 items after interning"
            );

            drop(ahc_true);
            drop(ahc_false);
            drop(ahc_and);

            // Call cleanup method on the table
            table.cleanup();

            // Get the size of the table after cleanup
            let size_after_cleanup = table.len();

            // Assert that the size of the table has decreased appropriately
            assert_eq!(
                size_after_cleanup, 0,
                "The table size should be zero after cleanup, because values are dropped"
            );
        }

        #[cfg(feature = "auto-cleanup")]
        #[test]
        fn test_multi_threaded_auto_cleanup_behavior() {
            let table = HcTable::<BoolExpr>::new();

            // hash cons a value and keep a reference to it
            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let ahc_true = table_clone.hashcons(BoolExpr::Const(true));
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            // Get the size of the table after interning
            let size_after_interning = table.len();
            assert_eq!(
                size_after_interning, 1,
                "Table should have 1 item after interning"
            );

            // Drop the reference to the interned value
            drop(ahc_true);

            // Get the size of the table after dropping the reference
            let size_after_dropping = table.len();

            // Assert that the table is empty after the value is dropped
            assert_eq!(
                size_after_dropping, 0,
                "Table should be empty after dropping the only reference"
            );
        }

        #[test]
        fn test_multi_threaded_memory_usage_hash_consing() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();
            let mut data = Vec::new();

            for _ in 0..100 {
                // Add repetitive BoolExpr values
                let table_clone = table.clone();
                let expr_true = BoolExpr::Const(true);
                let thread_handle_ahc_true = thread::spawn(move || {
                    let ahc_true = table_clone.hashcons(BoolExpr::Const(true));
                    ahc_true
                });
                let ahc_true = thread_handle_ahc_true
                    .join()
                    .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

                let expr_false = BoolExpr::Const(false);
                let table_clone = table.clone();
                let thread_handle_ahc_false = thread::spawn(move || {
                    let ahc_false = table_clone.hashcons(BoolExpr::Const(false));
                    ahc_false
                });
                let ahc_false = thread_handle_ahc_false.join().expect(
                    "Thread should finish and return `Hc<Const <false>>` without panicking",
                );

                data.push(expr_true.clone());
                data.push(expr_false.clone());
                ahc_data.push(ahc_true);
                ahc_data.push(ahc_false);
            }

            assert!(
                table.len() < data.len(),
                "Table length should be way less than the data length"
            );

            assert_eq!(
                table.len(),
                2,
                "Table should only have two values(Const True and Const False)"
            );
        }

        #[test]
        fn test_multi_threaded_hash_collision() {
            #[derive(PartialEq, Eq)]
            enum BoolExprHc {
                Const(bool),
                Not(Hc<BoolExprHc>),
            }

            impl Hash for BoolExprHc {
                fn hash<H: Hasher>(&self, state: &mut H) {
                    match self {
                        BoolExprHc::Const(flag) => {
                            // Hash 1 for true, 0 for false
                            let value = if *flag { 1 } else { 0 };
                            value.hash(state);
                        }
                        BoolExprHc::Not(val) => {
                            let mut temp_hasher = DefaultHasher::new();
                            val.hash(&mut temp_hasher);
                            1.hash(state);
                        }
                    }
                }
            }

            let table = HcTable::<BoolExprHc>::new();

            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let expr_true = BoolExprHc::Const(true);
                let ahc_true = table_clone.hashcons(expr_true);
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let table_clone = table.clone();
            let thread_handle_ahc_not_false = thread::spawn(move || {
                let expr_not_false =
                    BoolExprHc::Not(table_clone.hashcons(BoolExprHc::Const(false)));
                let ahc_not_false = table_clone.hashcons(expr_not_false);
                ahc_not_false
            });
            let ahc_not_false = thread_handle_ahc_not_false.join().expect(
                "Thread should finish and return `Hc<Not <Hc <Const <false>>>>` without panicking",
            );

            let mut hasher = DefaultHasher::new();
            ahc_true.hash(&mut hasher);
            let hash_value_ahc_true = hasher.finish();

            // Reinitialize the hasher for a new calculation
            hasher = DefaultHasher::new();
            ahc_not_false.hash(&mut hasher);
            let hash_value_ahc_not_false = hasher.finish();

            assert_eq!(
                hash_value_ahc_true, hash_value_ahc_not_false,
                "Hash values should be equal"
            );
            assert_eq!(table.len(), 3, "Table should have 3 items");
        }

        #[test]
        fn test_multi_threaded_large_unique() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();

            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let expr_true = BoolExpr::Const(true);
                let ahc_true = table_clone.hashcons(expr_true);
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let table_clone = table.clone();
            let thread_handle_false = thread::spawn(move || {
                let expr_false = BoolExpr::Const(false);
                let ahc_false = table_clone.hashcons(expr_false);
                ahc_false
            });
            let ahc_false = thread_handle_false
                .join()
                .expect("Thread should finish and return `Hc<Const <false>>` without panicking");

            ahc_data.push(ahc_true);
            ahc_data.push(ahc_false);

            for i in 2..10_000 {
                let table_clone = table.clone();
                let first = ahc_data[i - 1].clone();
                let second = ahc_data[i - 2].clone();
                let thread_handle_ahc_or = thread::spawn(move || {
                    let ahc_or = table_clone.hashcons(BoolExpr::Or(first, second));
                    ahc_or
                });
                let ahc_or = thread_handle_ahc_or
                    .join()
                    .expect("Thread should finish and return `Hc<Or <Hc<Const <true>>>, Hc<Const <false>>>>` without panicking");
                ahc_data.push(ahc_or.clone());
                ahc_data.push(ahc_or);
            }
            assert_eq!(
                table.len(),
                10_000,
                "table length should be equal to the  data length"
            );
        }

        #[test]
        fn test_multi_threaded_large_non_unique() {
            let table = HcTable::<BoolExpr>::new();
            let mut ahc_data = Vec::new();

            let table_clone = table.clone();
            let thread_handle_ahc_true = thread::spawn(move || {
                let expr_true = BoolExpr::Const(true);
                let ahc_true = table_clone.hashcons(expr_true);
                ahc_true
            });
            let ahc_true = thread_handle_ahc_true
                .join()
                .expect("Thread should finish and return `Hc<Const <true>>` without panicking");

            let table_clone = table.clone();
            let thread_handle_false = thread::spawn(move || {
                let expr_false = BoolExpr::Const(false);
                let ahc_false = table_clone.hashcons(expr_false);
                ahc_false
            });
            let ahc_false = thread_handle_false
                .join()
                .expect("Thread should finish and return `Hc<Const <false>>` without panicking");

            ahc_data.push(ahc_true);
            ahc_data.push(ahc_false);

            for i in 2..10_000 {
                let table_clone = table.clone();

                let mut rng = rand::thread_rng();
                let first_index = rng.gen_range(0..i);
                let second_index = rng.gen_range(0..i);
                let first = ahc_data[first_index].clone();
                let second = ahc_data[second_index].clone();
                let thread_handle_ahc_or = thread::spawn(move || {
                    let ahc_or = table_clone.hashcons(BoolExpr::Or(first, second));
                    ahc_or
                });
                let ahc_or = thread_handle_ahc_or
                    .join()
                    .expect("Thread should finish and return `Hc<Or <Hc<Const <true>>>, Hc<Const <false>>>>` without panicking");
                ahc_data.push(ahc_or.clone());
                ahc_data.push(ahc_or);
            }
            assert!(
                table.len() < 10_000,
                "table length should be equal to the  data length"
            );
        }

        /*
            #[test]
            fn test_multi_threaded_stress_test_ahc_table() {
                let table = HcTable::<BoolExpr>::new();
                let mut ahc_data = Vec::new();

                let expr_true = BoolExpr::Const(true);
                let expr_false = BoolExpr::Const(false);
                ahc_data.push(table.hashcons(expr_true.clone()));
                ahc_data.push(table.hashcons(expr_false.clone()));
                ahc_data.push(table.hashcons(expr_true.clone()));
                ahc_data.push(table.hashcons(expr_false.clone()));

                for i in 3..1_000 {
                    let mut rng = rand::thread_rng();
                    let first = rng.gen_range(0..i);
                    let second = rng.gen_range(0..i);

                    if i % 5 == 0 {
                        let table_clone = table.clone();
                        let first_ahc = ahc_data[first].clone();
                        let second_ahc = ahc_data[second].clone();

                        let thread_handle_ahc_and = thread::spawn(move || {
                            let ahc_and = table_clone.hashcons(BoolExpr::And(first_ahc, second_ahc));
                            ahc_and
                        });
                        let ahc_and = thread_handle_ahc_and
                            .join()
                            .expect("Thread should finish and return `Hc<And <Hc<Const <true>>>, Hc<Const <false>>>>` without panicking");

                        ahc_data.push(ahc_and);
                    } else if i % 3 == 0 {
                        let table_clone = table.clone();
                        let first_ahc = ahc_data[first].clone();
                        let second_ahc = ahc_data[second].clone();

                        let thread_handle_ahc_or = thread::spawn(move || {
                            let ahc_or = table_clone.hashcons(BoolExpr::Or(first_ahc, second_ahc));
                            ahc_or
                        });

                        let ahc_or = thread_handle_ahc_or
                            .join()
                            .expect("Thread should finish and return `Hc<Or <Hc<Const <true>>>, Hc<Const <false>>>>` without panicking");

                        ahc_data.push(ahc_or);
                    } else {
                        let table_clone = table.clone();
                        let first_ahc = ahc_data[first].clone();
                        let thread_handle_ahc_not = thread::spawn(move || {
                            let ahc_not = table_clone.hashcons(BoolExpr::Not(first_ahc));
                            ahc_not
                        });
                        let ahc_not = thread_handle_ahc_not
                            .join()
                            .expect("Thread should finish and return `Hc<Not <Hc<Const <true>>>>` without panicking");

                        ahc_data.push(ahc_not);
                    }
                }
                drop(ahc_data);
                // table.cleanup();

                // Consistency checks
                assert_eq!(
                    table.len(),
                    0,
                    "Table size should be zero because all values are dropped"
                );
            }
        */
    }
}