-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
1762 lines (1356 loc) · 58.4 KB
/
test.cpp
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <shared_mutex>
#include <chrono>
#include <cstddef>
#include <cassert>
#include <sstream>
#include <utility>
#include <cmath>
#include <initializer_list>
#include "GarbageCollection.h"
std::mutex __io_mutex;
// prints stuff to cerr in a thread-safe fashion
template<typename ...Args>
void sync_err_print(Args &&...args)
{
std::lock_guard<std::mutex> io_lock(__io_mutex);
(std::cout << ... << std::forward<Args>(args));
}
struct alignas(16) sse_t { char d[16]; };
struct ptr_set
{
GC::ptr<int> a, b, c, d, e, f, g, h;
int val;
std::shared_ptr<int> thingy;
std::tuple<int, int, int, long, long, int, GC::ptr<int>, int> tuple_thing;
GC::ptr<GC::unordered_map<std::string, unsigned int>> mapydoo1;
GC::ptr<GC::unordered_map<std::string, unsigned int>> mapydoo2;
GC::ptr<int[]> doodle_dud_0;
GC::ptr<int[]> doodle_dud_1;
ptr_set() : val(0)
{
auto ptr = new std::unordered_map<std::string, unsigned int>;
ptr->emplace("hello", 67);
ptr->emplace("world", 4765);
mapydoo1 = GC::adopt(ptr);
mapydoo2 = GC::adopt<std::unordered_map<std::string, unsigned int>>(nullptr);
doodle_dud_0 = GC::adopt(new int[1024], 1024);
doodle_dud_0 = GC::adopt(new int[2048], 2048);
}
};
template<> struct GC::router<ptr_set>
{
static void route(const ptr_set &set, GC::router_fn func)
{
GC::route(set.a, func);
GC::route(set.b, func);
GC::route(set.c, func);
GC::route(set.d, func);
GC::route(set.e, func);
GC::route(set.f, func);
GC::route(set.g, func);
GC::route(set.h, func);
GC::route(set.val, func);
GC::route(set.thingy, func);
GC::route(set.tuple_thing, func);
GC::route(set.mapydoo1, func);
GC::route(set.mapydoo2, func);
GC::route(set.doodle_dud_0, func);
GC::route(set.doodle_dud_1, func);
//GC::route(set.a, set.a);
}
static void route(const ptr_set &set, GC::mutable_router_fn func) {} // no mutable things to route to
};
struct ListNode
{
GC::ptr<ListNode> prev;
GC::ptr<ListNode> next;
ptr_set set;
// show a message that says we called ctor
ListNode() { std::this_thread::sleep_for(std::chrono::microseconds(4)); }
// show a message that says we called dtor
~ListNode() { std::this_thread::sleep_for(std::chrono::microseconds(4)); }
};
template<> struct GC::router<ListNode>
{
static void route(const ListNode &node, GC::router_fn func)
{
GC::route(node.prev, func);
GC::route(node.next, func);
GC::route(node.set, func);
}
static void route(const ListNode &node, GC::mutable_router_fn func) // this is correct
//static void route(ListNode node, GC::mutable_router_fn func) // this is wrong - by-value T could cause deadlocking
{
// only routing to set for future-proofing
GC::route(node.set, func);
}
};
// creates a linked list that has a cycle
void foo()
{
{
//GC::collect();
// create the first node
GC::ptr<ListNode> root = GC::make<ListNode>();
// we'll make 10 links in the chain
GC::ptr<ListNode> *prev = &root;
for (int i = 0; i < 10; ++i)
{
(*prev)->next = GC::make<ListNode>();
(*prev)->next->prev = *prev;
prev = &(*prev)->next;
}
// then we'll merege the ends into a cycle
root->prev = *prev;
(*prev)->next = root;
using std::swap;
root.swap(*prev);
swap(root, *prev);
//GC::collect();
//std::cerr << "\n\n";
}
}
template<typename T>
struct wrap
{
GC::ptr<T> ptr;
};
template<typename T> struct GC::router<wrap<T>>
{
template<typename F> static void route(const wrap<T> &w, F fn)
{
GC::route(w.ptr, fn);
}
};
struct virtual_type
{
virtual ~virtual_type() {}
};
struct non_virtual_type
{
};
struct base1
{
int a;
virtual ~base1() {}
};
struct base2
{
int b;
virtual ~base2() {}
};
struct derived : base1, base2
{
};
struct TreeNode
{
// left and right sub-trees
GC::ptr<TreeNode> left;
GC::ptr<TreeNode> right;
double value;
enum op_t { val, add, sub, mul, div } op;
};
template<> struct GC::router<TreeNode>
{
template<typename F> static void route(const TreeNode &node, F func)
{
// route to our GC::ptr instances
GC::route(node.left, func);
GC::route(node.right, func);
// no need to route to anything else
}
};
class SymbolTable
{
private:
std::unordered_map<std::string, GC::ptr<TreeNode>> symbols;
// we need to route to the contents of symbols, but symbols is a mutable collection.
// we therefore need insert/delete to be synchronous with respect to the router:
mutable std::mutex symbols_mutex;
// make the particular router class a friend so it can use our private data
friend struct GC::router<SymbolTable>;
public:
// wrapped symbols table type that is safe to modify directly
GC::unordered_map<std::string, GC::ptr<TreeNode>> better_symbols;
public:
SymbolTable() = default;
SymbolTable(const SymbolTable &other) : symbols(other.symbols), better_symbols(other.better_symbols) {}
SymbolTable &operator=(const SymbolTable &other)
{
if (this != &other)
{
{
std::lock_guard lock(symbols_mutex);
symbols = other.symbols;
}
better_symbols = other.better_symbols;
}
return *this;
}
SymbolTable &operator=(SymbolTable &&other)
{
if (this != &other)
{
{
std::scoped_lock locks(symbols_mutex, other.symbols_mutex);
symbols = std::move(other.symbols);
}
better_symbols = std::move(other.better_symbols);
}
return *this;
}
public:
void update(std::string name, GC::ptr<TreeNode> new_value)
{
{
// modification of the mutable collection of GC::ptr and router must be mutually exclusive
std::lock_guard<std::mutex> lock(symbols_mutex);
symbols[name] = new_value;
}
}
void clear()
{
{
std::lock_guard<std::mutex> lock(symbols_mutex);
symbols.clear();
}
better_symbols.clear();
}
};
template<> struct GC::router<SymbolTable>
{
template<typename F> static void route(const SymbolTable &table, F func)
{
{
// modification of the mutable collection of GC::ptr and router must be mutually exclusive
std::lock_guard<std::mutex> lock(table.symbols_mutex);
GC::route(table.symbols, func);
}
GC::route(table.better_symbols, func);
}
};
class MaybeTreeNode
{
private:
// the buffer for the object
alignas(TreeNode) char buf[sizeof(TreeNode)];
bool contains_tree_node = false;
// because we'll be constructing destucting it on the fly,
// buf is a mutable container of a type we need to route to.
// thus we need to synchronize "re-pointing" it and the router function.
mutable std::mutex buf_mutex;
// friend the particular router class so it can access our private data
friend struct GC::router<MaybeTreeNode>;
public:
void construct()
{
if (contains_tree_node) throw std::runtime_error("baka");
// we need to synchronize with the router
std::lock_guard<std::mutex> lock(buf_mutex);
// construct the object
new (buf) TreeNode;
contains_tree_node = true;
}
void destruct()
{
if (!contains_tree_node) throw std::runtime_error("baka");
// we need to synchronize with the router
std::lock_guard<std::mutex> lock(buf_mutex);
// destroy the object
reinterpret_cast<TreeNode*>(buf)->~TreeNode();
contains_tree_node = false;
}
};
template<> struct GC::router<MaybeTreeNode>
{
template<typename F> static void route(const MaybeTreeNode &maybe, F func)
{
// this must be synchronized with constructing/destucting the buffer object.
std::lock_guard<std::mutex> lock(maybe.buf_mutex);
// because TreeNode contains GC::ptr objects, we need to route to it,
// but only if the MaybeTreeNode actually has it constructed in the buffer.
if (maybe.contains_tree_node) GC::route(*reinterpret_cast<const TreeNode*>(maybe.buf), func);
}
};
#define COMMA ,
struct alert_t
{
alert_t() { std::cerr << "ctor\n"; }
~alert_t() { std::cerr << "dtor\n"; }
};
struct atomic_container
{
GC::atomic_ptr<double> atomic_1;
GC::atomic_ptr<double> atomic_2;
GC::atomic_ptr<double> atomic_3;
GC::atomic_ptr<double> atomic_4;
};
template<>
struct GC::router<atomic_container>
{
template<typename F> static void route(const atomic_container &atomic, F func)
{
GC::route(atomic.atomic_1, func);
GC::route(atomic.atomic_2, func);
GC::route(atomic.atomic_3, func);
GC::route(atomic.atomic_4, func);
}
};
GC::vector<GC::ptr<int>> global_vec_ptr;
GC::atomic_ptr<atomic_container> global_atomic_ptr;
thread_local GC::vector<GC::ptr<int>> thread_local_vec_ptr;
template<typename T>
std::string tostr(T &&v)
{
std::ostringstream ostr;
ostr << std::forward<T>(v);
return ostr.str();
}
struct self_ptr
{
std::unique_ptr<GC::ptr<self_ptr>> p;
self_ptr() { std::cerr << "self_ptr ctor\n"; }
~self_ptr() { std::cerr << "self_ptr dtor\n"; }
};
template<>
struct GC::router<self_ptr>
{
template<typename F>
static void route(const self_ptr &p, F func)
{
// we should really be using a mutex here for the std::unique_ptr but this is just a test and i know it's safe ...
GC::route(p.p, func);
}
};
struct gc_self_ptr
{
GC::unique_ptr<GC::ptr<gc_self_ptr>> p;
gc_self_ptr() { std::cerr << "gc_self_ptr ctor\n"; }
~gc_self_ptr() { std::cerr << "gc_self_ptr dtor\n"; }
};
template<>
struct GC::router<gc_self_ptr>
{
template<typename F>
static void route(const gc_self_ptr &p, F func)
{
GC::route(p.p, func);
}
};
struct bool_alerter
{
std::atomic<bool> &flag;
explicit bool_alerter(std::atomic<bool> &d) : flag(d) { flag = false; }
~bool_alerter() { flag = true; }
};
struct bool_alerter_self_ptr
{
bool_alerter alerter;
GC::ptr<bool_alerter_self_ptr> self_p;
explicit bool_alerter_self_ptr(std::atomic<bool> &d) : alerter(d) {}
};
template<>
struct GC::router< bool_alerter_self_ptr>
{
template<typename F>
static void route(const bool_alerter_self_ptr &obj, F func)
{
GC::route(obj.self_p, func);
}
};
// runs statement and asserts that it throws the right type of exception
#define assert_throws(statement, exception) \
try { statement; std::cerr << "did not throw\n"; assert(false); } \
catch (const exception&) {} \
catch (...) { std::cerr << "threw wrong type\n"; assert(false); }
// runs statement and asserts that it throws (anything)
#define assert_throws_any(statement) \
try { statement; std::cerr << "did no throw\n"; assert(false); } \
catch (...) {}
// runs statement and asserts that it throws (anything)
#define assert_throws_disjunction(statement) \
try { statement; std::cerr << "did no throw\n"; assert(false); } \
catch (const GC::disjunction_error&) {} \
catch (...) { std::cerr << "threw wrong type\n"; assert(false); }
// runs statement and asserts that it doesn't throw (anything)
#define assert_nothrow(statement) \
try { statement; } \
catch (...) { std::cerr << "threw an exception\n"; assert(false); }
// wraps type T but doesn't allow copy/move
template<typename T>
class stationary
{
public:
T value;
template<typename ...Args>
stationary(Args &&...args) : value(std::forward<Args>(args)...) {}
stationary(const stationary&) = delete;
stationary &operator=(const stationary&) = delete;
};
template<typename T>
struct GC::router<stationary<T>>
{
static constexpr bool is_trivial = GC::has_trivial_router<T>::value;
template<typename F>
static void route(const stationary<T> &stat, F func) { GC::route(stat.value, func); }
};
void spooky_scary_dont_do_this()
{
GC::collect();
#if DRAGAZO_GARBAGE_COLLECT_USE_IGNORE_COLLECT_IN_WRAPPERS
std::thread([] { GC::collect(); }).join();
#endif
}
struct ctor_collect_t
{
ctor_collect_t() { spooky_scary_dont_do_this(); }
};
template<>
struct GC::router<ctor_collect_t>
{
template<typename F>
static void route(const ctor_collect_t &v, F f) {}
};
struct dtor_collect_t
{
~dtor_collect_t() { spooky_scary_dont_do_this(); }
};
template<>
struct GC::router<dtor_collect_t>
{
template<typename F>
static void route(const dtor_collect_t &v, F f) {}
};
struct ctor_dtor_collect_t
{
ctor_dtor_collect_t() { spooky_scary_dont_do_this(); }
~ctor_dtor_collect_t() { spooky_scary_dont_do_this(); }
};
template<>
struct GC::router<ctor_dtor_collect_t>
{
template<typename F>
static void route(const ctor_dtor_collect_t &v, F f) {}
};
struct cpy_mov_intrin
{
bool src = false;
cpy_mov_intrin() { std::cerr << "ctor\n"; }
~cpy_mov_intrin() { std::cerr << (src ? "SRC dtor\n" : "dtor\n"); }
cpy_mov_intrin(const cpy_mov_intrin &other) { std::cerr << "cpy ctor\n"; }
cpy_mov_intrin(cpy_mov_intrin &&other) { std::cerr << "mov ctor\n"; }
cpy_mov_intrin &operator=(const cpy_mov_intrin &other) { std::cerr << "cpy asgn\n"; return *this; }
cpy_mov_intrin &operator=(cpy_mov_intrin &&other) { std::cerr << "mov asgn\n"; return *this; }
};
void intrin_printer(cpy_mov_intrin, cpy_mov_intrin)
{
std::cerr << "in printer\n";
}
void vector_printer(std::vector<int> vec)
{
for (int i : vec) std::cerr << i << ' ';
std::cerr << '\n';
}
void print_str(const GC::ptr<std::string> &p)
{
if (p) std::cerr << *p << '\n';
}
struct access_gc_at_ctor_t
{
GC::ptr<access_gc_at_ctor_t> p;
access_gc_at_ctor_t() { std::cerr << "@@@@ ctor gc accessor\n"; }
~access_gc_at_ctor_t() { std::cerr << "@@@@ dtor gc accessor\n"; GC::collect(); std::cerr << " -- safe\n"; }
};
template<>
struct GC::router<access_gc_at_ctor_t>
{
template<typename F>
static void route(const access_gc_at_ctor_t &obj, F func)
{
GC::route(obj.p, func);
}
};
GC::ptr<access_gc_at_ctor_t> access_gc_at_ctor;
struct router_allocator
{
mutable GC::ptr<int> p;
GC::ptr<router_allocator> self;
};
template<>
struct GC::router<router_allocator>
{
template<typename F>
static void route(const router_allocator &r, F func)
{
// simulate router locking a mutex and a mutator in the class locking and allocating
r.p = GC::make<int>(45);
GC::route(r.p, func);
GC::route(r.self, func);
}
};
struct thread_func_t
{
int v;
void foo() {}
};
// begins a timer in a new scope - requires a matching timer end point.
#define TIMER_BEGIN() { const auto __timer_begin = std::chrono::high_resolution_clock::now();
// ends the timer in the current scope - should not be used if there is no timer in the current scope.
// units is the (unquoted) name of a std::chrono time unit. name is the name of the timer (a c-style string).
#define TIMER_END(units, name) const auto __timer_end = std::chrono::high_resolution_clock::now(); \
std::cerr << "\ntimer elapsed - " name ": " << std::chrono::duration_cast<std::chrono::units>(__timer_end - __timer_begin).count() << " " #units "\n"; }
// represents a trivial gc type - used for forcing wrapped type conversions
struct gc_t {};
template<> struct GC::router<gc_t> { static void route(const gc_t&) {} };
constexpr bool operator==(const gc_t&, const gc_t&) { return true; }
constexpr bool operator!=(const gc_t&, const gc_t&) { return true; }
constexpr bool operator<(const gc_t&, const gc_t&) { return true; }
constexpr bool operator<=(const gc_t&, const gc_t&) { return true; }
constexpr bool operator>(const gc_t&, const gc_t&) { return true; }
constexpr bool operator>=(const gc_t&, const gc_t&) { return true; }
int main() try
{
std::cerr << "\nstart main: " << std::this_thread::get_id() << "\n\n";
struct _end_logger_t
{
~_end_logger_t() { std::cerr << "\nend main: " << std::this_thread::get_id() << "\n\n"; }
} _end_logger;
TIMER_BEGIN();
// -- tests that require no background collector to work -- //
// these tests make the assumption that no other thread is currently performing a collection action.
// these aren't considered binding tests - i.e. these test conditions aren't actually defined to work properly.
// they're merely to make sure that, given no interference, the desired behavior is taking effect.
GC::strategy(GC::strategies::manual);
GC::sleep_time(std::chrono::milliseconds(1));
// make sure that ref count decrements to zero result in the object being deleted.
// additionally, make sure that if a collection is happening in another thread the ref count guarantee is satisfied.
// i.e. if the ref count decs to zero during a collection the object will be destroyed at least by the end of collection.
// this is in the no-background collect section so we can join the parallel collector thread before the assertion.
{
std::atomic<bool> flag;
for (int i = 0; i < 4096; ++i)
{
std::thread test_thread([]()
{
try { GC::collect(); }
catch (...) { std::cerr << "\n\nFLAG TESTER EXCEPTION!!\n\n"; assert(false); }
});
{
GC::ptr<bool_alerter> a = GC::make<bool_alerter>(flag);
}
test_thread.join();
assert(flag);
}
}
// -- all other tests -- //
GC::strategy(GC::strategies::timed);
GC::thread(GC::new_disjunction, [] {
GC::ptr<router_allocator> p_router_allocator = GC::make<router_allocator>();
p_router_allocator->self = p_router_allocator;
std::this_thread::sleep_for(std::chrono::seconds(2));
}).detach();
access_gc_at_ctor = GC::make<access_gc_at_ctor_t>();
access_gc_at_ctor->p = access_gc_at_ctor;
{
thread_func_t tt;
std::invoke(&thread_func_t::foo, tt);
std::thread(&thread_func_t::foo, tt).join();
GC::thread(GC::inherit_disjunction, &thread_func_t::foo, tt).join();
}
#if DRAGAZO_GARBAGE_COLLECT_DISJUNCTION_SAFETY_CHECKS
GC::thread(GC::new_disjunction, []
{
try
{
std::cerr << "\nstarting disjunction exception checks\n";
auto ptr_a = GC::make<int>();
auto ptr_b = GC::make<int>();
// -------------------------------------------------
std::cerr << "starting asgn test\n";
GC::thread(GC::primary_disjunction, [](GC::ptr<int> &a, GC::ptr<int> &b)
{
assert_nothrow(a = b);
}, std::ref(ptr_a), std::ref(ptr_b)).join();
GC::thread(GC::inherit_disjunction, [](GC::ptr<int> &a, GC::ptr<int> &b)
{
assert_nothrow(a = b);
}, std::ref(ptr_a), std::ref(ptr_b)).join();
GC::thread(GC::new_disjunction, [](GC::ptr<int> &a, GC::ptr<int> &b)
{
assert_nothrow(a = b);
}, std::ref(ptr_a), std::ref(ptr_b)).join();
// -------------------------------------------------
std::cerr << "starting asgn new obj test\n";
GC::thread(GC::primary_disjunction, [](GC::ptr<int> &a)
{
assert_throws_disjunction(a = GC::make<int>());
}, std::ref(ptr_a)).join();
GC::thread(GC::inherit_disjunction, [](GC::ptr<int> &a)
{
assert_nothrow(a = GC::make<int>());
}, std::ref(ptr_a)).join();
GC::thread(GC::new_disjunction, [](GC::ptr<int> &a)
{
assert_throws_disjunction(a = GC::make<int>());
}, std::ref(ptr_a)).join();
// --------------------------------------------------
std::cerr << "starting swap test a\n";
GC::thread(GC::primary_disjunction, [](GC::ptr<int> &a)
{
auto b = GC::make<int>();
assert_throws_disjunction(a.swap(b));
}, std::ref(ptr_a)).join();
GC::thread(GC::inherit_disjunction, [](GC::ptr<int> &a)
{
auto b = GC::make<int>();
assert_nothrow(a.swap(b));
}, std::ref(ptr_a)).join();
GC::thread(GC::new_disjunction, [](GC::ptr<int> &a)
{
auto b = GC::make<int>();
assert_throws_disjunction(a.swap(b));
}, std::ref(ptr_a)).join();
// --------------------------------------------------
std::cerr << "starting swap test b\n";
GC::thread(GC::primary_disjunction, [](GC::ptr<int> &a)
{
auto b = GC::make<int>();
assert_throws_disjunction(b.swap(a));
}, std::ref(ptr_a)).join();
GC::thread(GC::inherit_disjunction, [](GC::ptr<int> &a)
{
auto b = GC::make<int>();
assert_nothrow(b.swap(a));
}, std::ref(ptr_a)).join();
GC::thread(GC::new_disjunction, [](GC::ptr<int> &a)
{
auto b = GC::make<int>();
assert_throws_disjunction(b.swap(a));
}, std::ref(ptr_a)).join();
// --------------------------------------------------
std::cerr << "starting ctor alias test\n";
GC::thread(GC::primary_disjunction, [](GC::ptr<int> &a)
{
assert_throws_disjunction(GC::ptr<int> temp(a));
}, std::ref(ptr_a)).join();
GC::thread(GC::inherit_disjunction, [](GC::ptr<int> &a)
{
assert_nothrow(GC::ptr<int> temp(a));
}, std::ref(ptr_a)).join();
GC::thread(GC::new_disjunction, [](GC::ptr<int> &a)
{
assert_throws_disjunction(GC::ptr<int> temp(a));
}, std::ref(ptr_a)).join();
// --------------------------------------------------
std::cerr << "starting value to reference thread pass - expecting 3:\n";
GC::thread(GC::primary_disjunction, [](const GC::ptr<std::string> &a)
{
assert_nothrow(print_str(a));
}, GC::make<std::string>(" -- primary disj")).join();
GC::thread(GC::inherit_disjunction, [](const GC::ptr<std::string> &a)
{
assert_nothrow(print_str(a));
}, GC::make<std::string>(" -- inherit disj")).join();
GC::thread(GC::new_disjunction, [](const GC::ptr<std::string> &a)
{
assert_nothrow(print_str(a));
}, GC::make<std::string>(" -- new disj")).join();
// --------------------------------------------------
std::cerr << "starting value to value thread pass - expecting 1:\n";
GC::thread(GC::inherit_disjunction, [](GC::ptr<std::string> a)
{
assert_nothrow(print_str(a));
}, GC::make<std::string>(" -- inherit disj")).join();
}
catch (...)
{
std::cerr << "\n\nAN EXCEPTION SHOULD NOT HAVE GOTTEN HERE!!\n\n";
assert(false);
}
}).join();
#endif
{
std::cerr << "starting disjunction deletion test\n";
std::atomic<bool> disjunction_deletion_flag;
for (int i = 0; i < 16; ++i)
{
GC::thread(GC::new_disjunction, [](std::atomic<bool> &flag)
{
try
{
auto p = GC::make<bool_alerter_self_ptr>(flag);
p->self_p = p;
}
catch (...) { std::cerr << "DISJUNCTION DEL TEST EXCEPTION!!\n"; assert(false); }
}, std::ref(disjunction_deletion_flag)).join();
assert(disjunction_deletion_flag);
}
}
// -----------------------------------------------------------------------------------------------------
static_assert(GC::has_trivial_router<int>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<char>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<double>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::string>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::pair<int, std::string>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::tuple<int, std::string>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::unique_ptr<int>>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<std::unique_ptr<self_ptr>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<int[16]>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<int[16][7]>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<self_ptr[16][7][1]>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::array<int, 12>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::array<std::array<int, 12>, 12>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::vector<int>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<GC::vector<int>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<std::unordered_map<int, char*>>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<GC::unordered_map<int, char*>>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<self_ptr>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<GC::ptr<int>>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<GC::ptr<self_ptr>>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<GC::ptr<GC::ptr<self_ptr>>>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<std::pair<int, GC::ptr<int>>>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<std::tuple<int, GC::ptr<int>>>::value, "trivial assumption failure");
// -----------------------------------------------------------------------------------------------------
static_assert(GC::has_trivial_router<int>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<int&>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<int&&>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<const int>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<const int&>::value, "trivial assumption failure");
static_assert(GC::has_trivial_router<const int&&>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<self_ptr>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<self_ptr&>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<self_ptr&&>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<const self_ptr>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<const self_ptr&>::value, "trivial assumption failure");
static_assert(!GC::has_trivial_router<const self_ptr&&>::value, "trivial assumption failure");
// -----------------------------------------------------------------------------------------------------
static_assert(GC::all_have_trivial_routers<>::value, "trivial assumption failure");
// -----------------------------------------------------------------------------------------------------
static_assert(std::is_same<GC::vector<bool>, std::vector<bool>>::value, "smart wrapper opt check");
static_assert(std::is_same<GC::vector<bool_alerter>, std::vector<bool_alerter>>::value, "smart wrapper opt check");
static_assert(std::is_same<GC::unique_ptr<long double>, std::unique_ptr<long double>>::value, "smart wrapper opt check");
static_assert(std::is_same<GC::unique_ptr<bool_alerter>, std::unique_ptr<bool_alerter>>::value, "smart wrapper opt check");
static_assert(std::is_same<GC::stack<bool_alerter>, std::stack<bool_alerter>>::value, "smart wrapper opt check");
static_assert(std::is_same<GC::queue<std::pair<double, int>>, std::queue<std::pair<double, int>>>::value, "smart wrapper opt check");
static_assert(std::is_same<GC::priority_queue<std::tuple<double, int>>, std::priority_queue<std::tuple<double, int>>>::value, "smart wrapper opt check");
static_assert(!std::is_same<GC::unique_ptr<TreeNode>, std::unique_ptr<TreeNode>>::value, "smart wrapper opt check");
static_assert(!std::is_same<GC::list<SymbolTable>, std::list<SymbolTable>>::value, "smart wrapper opt check");
static_assert(!std::is_same<GC::stack<TreeNode>, std::stack<TreeNode>>::value, "smart wrapper opt check");
static_assert(!std::is_same<GC::queue<std::pair<double, TreeNode>>, std::queue<std::pair<double, TreeNode>>>::value, "smart wrapper opt check");
static_assert(!std::is_same<GC::priority_queue<std::tuple<SymbolTable>>, std::priority_queue<std::tuple<SymbolTable>>>::value, "smart wrapper opt check");
// -----------------------------------------------------------------------------------------------------
static_assert(std::is_same<const GC::make_wrapped_t<GC::vector<int>>, GC::make_wrapped_t<const std::vector<int>>>::value, "wrapped const test");
static_assert(std::is_same<const GC::make_wrapped_t<std::vector<TreeNode>>, GC::make_wrapped_t<const std::vector<TreeNode>>>::value, "wrapped const test");
static_assert(std::is_same<volatile GC::make_wrapped_t<std::vector<TreeNode>>, GC::make_wrapped_t<volatile GC::vector<TreeNode>>>::value, "wrapped const test");
static_assert(std::is_same<const volatile GC::make_wrapped_t<std::vector<TreeNode>>, const GC::make_wrapped_t<volatile std::vector<TreeNode>>>::value, "wrapped const test");
static_assert(std::is_same<const GC::make_wrapped_t<volatile std::vector<TreeNode>>, const volatile GC::make_wrapped_t<volatile GC::vector<TreeNode>>>::value, "wrapped const test");
static_assert(std::is_same<const GC::make_wrapped_t<const volatile std::vector<TreeNode>>, const volatile GC::make_wrapped_t<const volatile std::vector<TreeNode>>>::value, "wrapped const test");
static_assert(std::is_same<GC::make_wrapped_t<const volatile GC::vector<TreeNode>>, GC::make_wrapped_t<const volatile std::vector<TreeNode>>>::value, "wrapped const test");
// -----------------------------------------------------------------------------------------------------
static_assert(std::is_same<int, GC::make_unwrapped_t<int>>::value, "unwrapped primitive test");
static_assert(std::is_same<const int, GC::make_unwrapped_t<const int>>::value, "unwrapped primitive test");
static_assert(std::is_same<volatile int, GC::make_unwrapped_t<volatile int>>::value, "unwrapped primitive test");
static_assert(std::is_same<const volatile int, GC::make_unwrapped_t<const volatile int>>::value, "unwrapped primitive test");
static_assert(std::is_same<int, GC::make_wrapped_t<int>>::value, "wrapped primitive test");
static_assert(std::is_same<const int, GC::make_wrapped_t<const int>>::value, "wrapped primitive test");
static_assert(std::is_same<volatile int, GC::make_wrapped_t<volatile int>>::value, "wrapped primitive test");
static_assert(std::is_same<const volatile int, GC::make_wrapped_t<const volatile int>>::value, "wrapped primitive test");
static_assert(std::is_same<float, GC::make_unwrapped_t<float>>::value, "unwrapped primitive test");
static_assert(std::is_same<const float, GC::make_unwrapped_t<const float>>::value, "unwrapped primitive test");
static_assert(std::is_same<volatile float, GC::make_unwrapped_t<volatile float>>::value, "unwrapped primitive test");
static_assert(std::is_same<const volatile float, GC::make_unwrapped_t<const volatile float>>::value, "unwrapped primitive test");
static_assert(std::is_same<bool, GC::make_wrapped_t<bool>>::value, "wrapped primitive test");
static_assert(std::is_same<const bool, GC::make_wrapped_t<const bool>>::value, "wrapped primitive test");
static_assert(std::is_same<volatile bool, GC::make_wrapped_t<volatile bool>>::value, "wrapped primitive test");
static_assert(std::is_same<const volatile bool, GC::make_wrapped_t<const volatile bool>>::value, "wrapped primitive test");
static_assert(std::is_same<std::pair<float, std::string>, GC::make_unwrapped_t<std::pair<float, std::string>>>::value, "unwrapped trivial test");
static_assert(std::is_same<const std::pair<float, std::string>, GC::make_unwrapped_t<const std::pair<float, std::string>>>::value, "unwrapped trivial test");
static_assert(std::is_same<volatile std::pair<float, std::string>, GC::make_unwrapped_t<volatile std::pair<float, std::string>>>::value, "unwrapped trivial test");
static_assert(std::is_same<const volatile std::pair<float, std::string>, GC::make_unwrapped_t<const volatile std::pair<float, std::string>>>::value, "unwrapped trivial test");
static_assert(std::is_same<std::tuple<void*, const char*, float, float, double>, GC::make_wrapped_t<std::tuple<void*, const char*, float, float, double>>>::value, "wrapped trivial test");
static_assert(std::is_same<const std::tuple<void*, const char*, float, float, double>, GC::make_wrapped_t<const std::tuple<void*, const char*, float, float, double>>>::value, "wrapped trivial test");
static_assert(std::is_same<volatile std::tuple<void*, const char*, float, float, double>, GC::make_wrapped_t<volatile std::tuple<void*, const char*, float, float, double>>>::value, "wrapped trivial test");
static_assert(std::is_same<const volatile std::tuple<void*, const char*, float, float, double>, GC::make_wrapped_t<const volatile std::tuple<void*, const char*, float, float, double>>>::value, "wrapped trivial test");
// -----------------------------------------------------------------------------------------------------
static_assert(GC::has_trivial_router<GC::variant<int>>::value, "trivial test");
static_assert(GC::has_trivial_router<GC::variant<int, float>>::value, "trivial test");
static_assert(GC::has_trivial_router<GC::variant<int, float, double>>::value, "trivial test");
static_assert(GC::has_trivial_router<GC::variant<int, float, double, void*>>::value, "trivial test");
static_assert(!GC::has_trivial_router<SymbolTable>::value, "trivial test");
static_assert(!GC::has_trivial_router<std::variant<SymbolTable>>::value, "trivial test");
static_assert(!GC::has_trivial_router<std::variant<int, SymbolTable>>::value, "trivial test");
static_assert(!GC::has_trivial_router<std::variant<SymbolTable, int>>::value, "trivial test");
static_assert(!GC::has_trivial_router<std::variant<int, SymbolTable, float>>::value, "trivial test");
static_assert(!GC::has_trivial_router<std::variant<int, double, SymbolTable, void*>>::value, "trivial test");
static_assert(!GC::has_trivial_router<std::variant<int, char, volatile int*, SymbolTable, long>>::value, "trivial test");
static_assert(std::is_same<std::variant<int, double>, GC::make_wrapped_t<std::variant<int, double>>>::value, "wrapped variant equivalence");
static_assert(std::is_same<std::variant<int, double>, GC::make_unwrapped_t<std::variant<int, double>>>::value, "wrapped variant equivalence");
static_assert(!std::is_same<std::variant<int, SymbolTable>, GC::make_wrapped_t<std::variant<int, SymbolTable>>>::value, "wrapped variant equivalence");
static_assert(std::is_same<std::variant<SymbolTable, std::string>, GC::make_unwrapped_t<GC::variant<SymbolTable, std::string>>>::value, "wrapped variant equivalence");