-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpointer.test.cpp
More file actions
executable file
·2021 lines (1600 loc) · 67.4 KB
/
Copy pathpointer.test.cpp
File metadata and controls
executable file
·2021 lines (1600 loc) · 67.4 KB
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
// Copyright (c) 2025 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <ranges>
#include <stdexcept>
#include <unordered_set>
#include <utility>
#include <vector>
#ifdef MODULE_BUILD
import tcb.pointer;
#else
# include <tcb/pointer.hpp>
#endif
/*
* MARK: Test machinery
*/
// Clang 19 and earlier with libstdc++ seems to have a bug when
// using spaceship with pointers in constexpr
#if defined(__clang_major__) && defined(__GLIBCXX__)
constexpr bool clang19_with_libstdcxx = (__clang_major__ < 20);
#else
constexpr bool clang19_with_libstdcxx = false;
#endif
// MSVC seems to have a bug where NAN <=> NAN evaluates to
// std::partial_ordering::less, not ::unordered as it should
// See https://developercommunity.visualstudio.com/t/float-f--NAN;-f--f-gives-incorrect-r/10973680
#if defined(_MSC_VER)
constexpr bool compiler_is_msvc = true;
#else
constexpr bool compiler_is_msvc = false;
#endif
struct test_failure : std::runtime_error {
using std::runtime_error::runtime_error;
};
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#define REQUIRE(...) \
if (!(__VA_ARGS__)) \
throw test_failure( \
__FILE__ ":" STRINGIFY(__LINE__) ": Test \"" STRINGIFY(__VA_ARGS__) "\" failed");
#define REQUIRE_THROWS_AS(type, ...) \
do { \
bool caught = false; \
try { \
(void)__VA_ARGS__; \
} catch (type const&) { \
caught = true; \
} catch (...) { \
throw test_failure(__FILE__ ":" STRINGIFY(__LINE__) ": Test \"" STRINGIFY( \
__VA_ARGS__) "\" threw an exception of unexpected type"); \
} \
if (!caught) { \
throw test_failure(__FILE__ ":" STRINGIFY(__LINE__) ": Test \"" STRINGIFY( \
__VA_ARGS__) "\" did not throw an exception when one was expected"); \
} \
} while (0)
#define REQUIRE_ERROR(...) REQUIRE_THROWS_AS(std::runtime_error, __VA_ARGS__)
/*
* MARK: Test types
*/
struct IncompleteClass;
struct BaseClass {
virtual char fn() const { return 'B'; };
virtual ~BaseClass() = default;
};
struct DerivedClass : BaseClass {
char fn() const override { return 'D'; }
};
union Union {
};
union IncompleteUnion;
enum Enum { };
enum class EnumClass { };
enum IncompleteEnum : int;
enum class IncompleteEnumClass;
struct ConvertibleToIntPtr {
operator int*() const;
};
// Multiple inheritance types
namespace MI {
struct A {
virtual ~A() = default;
};
struct B : A { };
struct C : A { };
struct D : B, C { };
} // namespace MI
/*
* MARK: Static tests
*/
template <typename T>
constexpr bool test_pointer_static_properties()
{
using P = tcb::pointer<T>;
// I know arrays of unknown bound are technically objects, but...
constexpr bool is_object = std::is_object_v<T> && !std::is_unbounded_array_v<T>;
constexpr bool is_array = std::is_unbounded_array_v<T>;
// pointer to object is the same size as T*
if constexpr (is_object) {
static_assert(sizeof(P) == sizeof(T*));
}
// array pointers are default constructible, but object pointers are not
if constexpr (is_array) {
static_assert(std::is_default_constructible_v<P>);
static_assert(std::default_initializable<P>);
} else {
static_assert(not std::is_default_constructible_v<P>);
static_assert(not std::default_initializable<P>);
}
// pointer<T> is copyable, movable, etc (type traits)
static_assert(std::is_copy_constructible_v<P>);
static_assert(std::is_move_constructible_v<P>);
static_assert(std::is_copy_assignable_v<P>);
static_assert(std::is_move_assignable_v<P>);
static_assert(std::is_destructible_v<P>);
static_assert(std::is_swappable_v<P>);
// pointer<T> is copyable, movable, etc (concepts)
static_assert(std::copyable<P>);
static_assert(std::movable<P>);
static_assert(std::destructible<P>);
static_assert(std::swappable<P>);
// pointer<T> special members are all trivial
static_assert(std::is_trivially_copyable_v<P>);
static_assert(std::is_trivially_copy_constructible_v<P>);
static_assert(std::is_trivially_move_constructible_v<P>);
static_assert(std::is_trivially_copy_assignable_v<P>);
static_assert(std::is_trivially_move_assignable_v<P>);
static_assert(std::is_trivially_destructible_v<P>);
// pointer<T> special members are all noexcept
static_assert(std::is_nothrow_copy_constructible_v<P>);
static_assert(std::is_nothrow_move_constructible_v<P>);
static_assert(std::is_nothrow_copy_assignable_v<P>);
static_assert(std::is_nothrow_move_assignable_v<P>);
static_assert(std::is_nothrow_destructible_v<P>);
// pointer<T> is equality comparable and strongly totally ordered
static_assert(std::equality_comparable<P>);
static_assert(std::totally_ordered<P>);
static_assert(std::three_way_comparable<P, std::strong_ordering>);
// pointer_to object is explicitly but not implicitly convertible to T*
static_assert(not std::is_convertible_v<P, T*>);
if constexpr (is_object) {
static_assert(requires(P& p) {
{ static_cast<T*>(p) } -> std::same_as<T*>;
});
}
// P::to_address() returns the correct type for non-arrays
if constexpr (!is_array) {
static_assert(std::same_as<decltype(std::declval<P&>().to_address()), T*>);
}
// pointer to object dereferences to the correct type
if constexpr (is_object) {
static_assert(std::same_as<decltype(*std::declval<P&>()), T&>);
static_assert(std::same_as<decltype(std::declval<P&>().operator->()), T*>);
}
// std::pointer_traits tests
{
using Traits = std::pointer_traits<P>;
static_assert(std::same_as<typename Traits::pointer, P>);
if constexpr (!std::is_unbounded_array_v<T>) {
static_assert(std::same_as<typename Traits::element_type, T>);
}
static_assert(std::same_as<typename Traits::difference_type, std::ptrdiff_t>);
static_assert(std::same_as<typename Traits::template rebind<int>, tcb::pointer<int>>);
}
return true;
}
static_assert(test_pointer_static_properties<int>());
static_assert(test_pointer_static_properties<int const>());
static_assert(test_pointer_static_properties<std::vector<int>>()); // non-trivial type
static_assert(test_pointer_static_properties<std::vector<int> const>());
static_assert(test_pointer_static_properties<BaseClass>()); // polymorphic type
static_assert(test_pointer_static_properties<BaseClass const>());
static_assert(test_pointer_static_properties<DerivedClass>());
static_assert(test_pointer_static_properties<DerivedClass const>());
static_assert(test_pointer_static_properties<IncompleteClass>()); // incomplete type
static_assert(test_pointer_static_properties<IncompleteClass const>());
static_assert(test_pointer_static_properties<Union>());
static_assert(test_pointer_static_properties<Union const>());
static_assert(test_pointer_static_properties<IncompleteUnion>());
static_assert(test_pointer_static_properties<IncompleteUnion const>());
static_assert(test_pointer_static_properties<Enum>());
static_assert(test_pointer_static_properties<Enum const>());
static_assert(test_pointer_static_properties<EnumClass>());
static_assert(test_pointer_static_properties<IncompleteEnum>());
static_assert(test_pointer_static_properties<IncompleteEnum const>());
static_assert(test_pointer_static_properties<IncompleteEnumClass>());
static_assert(test_pointer_static_properties<IncompleteEnumClass const>());
static_assert(test_pointer_static_properties<int[5]>()); // array of known bound
static_assert(test_pointer_static_properties<int const[5]>());
static_assert(test_pointer_static_properties<int[2][3]>()); // array of array of known bound
static_assert(test_pointer_static_properties<int const[2][3]>());
static_assert(test_pointer_static_properties<int*>()); // pointer to raw pointer
static_assert(test_pointer_static_properties<int const*>()); // pointer to raw pointer to const
static_assert(test_pointer_static_properties<int* const>()); // pointer to const raw pointer
static_assert(
test_pointer_static_properties<int const* const>()); // pointer to const raw pointer to const
static_assert(test_pointer_static_properties<tcb::ptr<int>>()); // pointer to tcb::pointer
static_assert(
test_pointer_static_properties<tcb::ptr<int const>>()); // pointer to tcb::pointer to const
static_assert(
test_pointer_static_properties<tcb::ptr<int> const>()); // pointer to const tcb::pointer
static_assert(test_pointer_static_properties<tcb::ptr<int const> const>()); // pointer to const
// tcb::pointer to const
static_assert(test_pointer_static_properties<void>());
static_assert(test_pointer_static_properties<void const>());
static_assert(test_pointer_static_properties<void volatile>()); // why not
static_assert(test_pointer_static_properties<void const volatile>());
static_assert(test_pointer_static_properties<int[]>()); // array of unknown bound
static_assert(test_pointer_static_properties<int const[]>());
static_assert(test_pointer_static_properties<int[][5]>());
static_assert(test_pointer_static_properties<int const[][5]>());
/*
* MARK: pointer_to() tests
*/
constexpr bool test_pointer_to()
{
using namespace tcb;
// pointer_to() is not callable with rvalues, returns a pointer-to-const
{
using F = decltype((pointer_to));
static_assert(std::invocable<F, int&>);
static_assert(std::invocable<F, int const&>);
static_assert(not std::invocable<F, int>);
static_assert(not std::invocable<F, int&&>);
static_assert(not std::invocable<F, int const&&>);
int i = 0;
const int c = 0;
static_assert(std::is_same_v<decltype(pointer_to(i)), pointer<int const>>);
static_assert(std::is_same_v<decltype(pointer_to(c)), pointer<int const>>);
}
// pointer_to pointer works as expected, with all combinations of const and mutable
{
int i = 0;
auto mut = pointer_to_mut(i);
auto const_ = pointer_to(i);
auto mut_to_mut = pointer_to_mut(mut);
auto const_to_mut = pointer_to(mut);
auto mut_to_const = pointer_to_mut(const_);
auto const_to_const = pointer_to(const_);
static_assert(std::same_as<decltype(mut_to_mut), ptr<ptr<int>>>);
static_assert(std::same_as<decltype(const_to_mut), ptr<ptr<int> const>>);
static_assert(std::same_as<decltype(mut_to_const), ptr<ptr<int const>>>);
static_assert(std::same_as<decltype(const_to_const), ptr<ptr<int const> const>>);
**mut_to_mut = 99;
REQUIRE(i == 99);
**const_to_mut = 100;
REQUIRE(i == 100);
// These should not compile
// **mut_to_const = 101;
// **const_to_const = 102;
}
// pointer_to_mut() is not callable with rvalues or const lvalues, returns a
// pointer-to-non-const
{
using F = decltype((pointer_to_mut));
static_assert(std::invocable<F, int&>);
static_assert(not std::invocable<F, int const&>);
static_assert(not std::invocable<F, int>);
static_assert(not std::invocable<F, int&&>);
static_assert(not std::invocable<F, int const&&>);
int i = 0;
static_assert(std::is_same_v<decltype(pointer_to_mut(i)), pointer<int>>);
}
return true;
}
static_assert(test_pointer_to());
/*
* MARK: Object ptr tests
*/
constexpr bool test_pointer_to_object()
{
using namespace tcb;
// Basic pointer<T>
{
int i = 0;
auto p = pointer_to_mut(i);
// member to_address() returns the right address
REQUIRE(p.to_address() == std::addressof(i));
// so does free to_address()
REQUIRE(to_address(p) == std::addressof(i));
// so does std::to_address()
REQUIRE(std::to_address(p) == std::addressof(i));
// explicit cast to int* works correctly
REQUIRE(static_cast<int*>(p) == std::addressof(i));
// dereferencing works correctly
REQUIRE(*p == 0);
*p = 1;
REQUIRE(i == 1);
REQUIRE(p.operator->() == std::addressof(i));
}
// Derived-to-base works
if (!std::is_constant_evaluated()) {
DerivedClass d;
ptr<BaseClass> pb = pointer_to_mut(d);
// Virtual call works, not somehow slicing
REQUIRE(pb->fn() == 'D');
}
// from_address()
{
// The following should not compile (try uncommenting them)
// ptr<int>::from_address(nullptr);
// ptr<int>::from_address(0);
// ptr<int>::from_address(NULL);
// ptr<int>::from_address((float*)nullptr);
// ptr<int>::from_address((void*)nullptr);
// ptr<int>::from_address((int const*)nullptr);
// ptr<int>::from_address(ConvertibleToIntPtr{});
auto make_ptr = [](auto p) -> decltype(ptr<int>::from_address(p)) {
return ptr<int>::from_address(p);
};
using F = decltype(make_ptr);
static_assert(std::invocable<F, int*>);
static_assert(not std::invocable<F, std::nullptr_t>);
static_assert(not std::invocable<F, int>);
static_assert(not std::invocable<F, decltype(NULL)>);
static_assert(not std::invocable<F, float*>);
static_assert(not std::invocable<F, void*>);
static_assert(not std::invocable<F, int const*>);
static_assert(not std::invocable<F, ConvertibleToIntPtr>);
int i = 0;
int* r = &i;
auto p1 = ptr<int>::from_address(r);
REQUIRE(p1.to_address() == r);
auto p2 = ptr<int const>::from_address(r);
REQUIRE(p2.to_address() == r);
if (!std::is_constant_evaluated()) {
REQUIRE_ERROR(ptr<int>::from_address((int*)nullptr));
}
DerivedClass d;
auto pb = ptr<BaseClass>::from_address(&d);
REQUIRE(pb.to_address() == &d);
{
MI::D obj;
// Should NOT compile, A base is ambiguous
// auto p = tcb::ptr<MI::A>::from_address(&obj);
// Okay:
[[maybe_unused]] auto p3 = tcb::ptr<MI::A>::from_address(&static_cast<MI::B&>(obj));
[[maybe_unused]] auto p4 = tcb::ptr<MI::A>::from_address(static_cast<MI::C*>(&obj));
}
}
// Comparisons
if constexpr (!clang19_with_libstdcxx) {
std::array arr{1, 2, 3, 4, 5};
auto p0 = tcb::pointer_to(arr[0]);
auto p4 = tcb::pointer_to(arr[4]);
REQUIRE(p0 == p0);
REQUIRE(p0 != p4);
REQUIRE(p0 <=> p0 == std::strong_ordering::equal);
REQUIRE(p0 <=> p4 == std::strong_ordering::less);
REQUIRE(p4 <=> p0 == std::strong_ordering::greater);
// Everything else is generated from <=>
REQUIRE(p0 < p4);
REQUIRE(p4 > p0);
}
// std::pointer_traits (weirdly, not constexpr)
if (!std::is_constant_evaluated()) {
// non-const
{
using Traits = std::pointer_traits<ptr<int>>;
int i = 0;
auto p = Traits::pointer_to(i);
static_assert(std::same_as<decltype(p), ptr<int>>);
auto p3 = std::to_address(p);
static_assert(std::same_as<decltype(p3), int*>);
REQUIRE(std::to_address(p) == std::addressof(i));
}
// const
{
using Traits = std::pointer_traits<ptr<int const>>;
int i = 0;
auto p = Traits::pointer_to(i);
static_assert(std::same_as<decltype(p), ptr<int const>>);
auto p3 = std::to_address(p);
static_assert(std::same_as<decltype(p3), int const*>);
REQUIRE(std::to_address(p) == std::addressof(i));
}
}
return true;
}
static_assert(test_pointer_to_object());
/*
* MARK: void ptr tests
*/
static bool test_pointer_to_void()
{
using namespace tcb;
// pointer<void>
{
int i = 0;
auto p = tcb::pointer_to_mut(i);
// Can convert lvalue pointer<int> to pointer<void>
static_assert(std::convertible_to<ptr<int>, ptr<void>>);
pointer<void> v = p;
REQUIRE(v.to_address() == std::addressof(i));
// Can convert rvalue pointer<int> to pointer<void>
pointer<void> v2 = tcb::pointer_to_mut(i);
REQUIRE(v2.to_address() == std::addressof(i));
// *Cannot* convert pointer<int const> to pointer<void>
static_assert(not std::convertible_to<ptr<int const>, ptr<void>>);
// ptr<void> v3 = tcb::addr(i);
// ptr<void> can be explicitly converted back to ptr<original-type>
ptr<int> p2 = static_cast<ptr<int>>(v);
REQUIRE(p2.to_address() == std::addressof(i));
// ptr<void> can be explicitly converted back to original-type*
int* p3 = static_cast<int*>(v.to_address());
REQUIRE(p3 == std::addressof(i));
// Converting ptr<void> to pointer of the wrong type is a runtime error
REQUIRE_ERROR(static_cast<ptr<float>>(v));
// Can increase const-ness in conversion from ptr<void>
ptr<int const> p4 = static_cast<ptr<int const>>(v);
REQUIRE(p4.to_address() == std::addressof(i));
}
// pointer<void const>
{
int const i = 0;
auto p = tcb::pointer_to(i);
// Can convert lvalue pointer<int> to pointer<void>
static_assert(std::convertible_to<ptr<int const>, ptr<void const>>);
pointer<void const> v = p;
REQUIRE(v.to_address() == std::addressof(i));
// Can convert rvalue pointer<int> to pointer<void>
pointer<void const> v2 = tcb::pointer_to(i);
REQUIRE(v2.to_address() == std::addressof(i));
// ptr<void const> can be explicitly converted back to ptr<original-type>
ptr<int const> p2 = static_cast<ptr<int const>>(v);
REQUIRE(p2.to_address() == std::addressof(i));
// ptr<void> can be explicitly converted back to original-type*
int const* p3 = static_cast<int const*>(v.to_address());
REQUIRE(p3 == std::addressof(i));
// Converting ptr<void> to pointer of the wrong type is a runtime error
REQUIRE_ERROR(static_cast<ptr<float const>>(v));
// Cannot remove const in conversion from ptr<void const>
static_assert(not std::convertible_to<ptr<void const>, ptr<int>>);
// ptr<int> p4 = static_cast<ptr<int>>(v);
}
// std::to_address
{
// non-const
{
int i = 0;
auto p = ptr<void>::pointer_to(i);
auto p2 = std::to_address(p);
static_assert(std::same_as<decltype(p2), void*>);
REQUIRE(std::to_address(p) == std::addressof(i));
}
// const
{
int i = 0;
auto p = ptr<void const>::pointer_to(i);
auto p2 = std::to_address(p);
static_assert(std::same_as<decltype(p2), void const*>);
REQUIRE(std::to_address(p) == std::addressof(i));
}
}
return true;
}
/*
* MARK: Checked iter tests
*/
// checked_iterator is not exported from the module
template <typename T>
using checked_iterator_t = decltype(std::declval<tcb::pointer<T[]>&>()->begin());
constexpr bool test_checked_iterator()
{
using Iter = checked_iterator_t<int>;
using CIter = checked_iterator_t<int const>;
static_assert(std::contiguous_iterator<Iter>);
static_assert(std::same_as<std::iter_value_t<Iter>, int>);
static_assert(std::same_as<std::iter_reference_t<Iter>, int&>);
static_assert(std::same_as<std::iter_rvalue_reference_t<Iter>, int&&>);
static_assert(std::same_as<std::iter_difference_t<Iter>, std::ptrdiff_t>);
static_assert(std::contiguous_iterator<CIter>);
static_assert(std::same_as<std::iter_value_t<CIter>, int>);
static_assert(std::same_as<std::iter_reference_t<CIter>, int const&>);
static_assert(std::same_as<std::iter_rvalue_reference_t<CIter>, int const&&>);
static_assert(std::same_as<std::iter_difference_t<CIter>, std::ptrdiff_t>);
// Basic iteration
{
std::array arr{1, 2, 3, 4, 5};
auto start = Iter::to_start_of({arr.data(), arr.size()});
auto end = Iter::to_end_of({arr.data(), arr.size()});
REQUIRE(std::ranges::equal(arr, std::ranges::subrange(start, end)));
REQUIRE(std::ranges::equal(arr | std::views::reverse,
std::ranges::subrange(start, end) | std::views::reverse));
}
// Comparisons
{
std::array arr{1, 2, 3, 4, 5};
auto start = Iter::to_start_of({arr.data(), arr.size()});
auto next = std::next(start);
REQUIRE(start == start);
REQUIRE(start != next);
REQUIRE(start < next);
REQUIRE(next > start);
REQUIRE(start <=> start == std::strong_ordering::equal);
REQUIRE(start <=> next == std::strong_ordering::less);
}
// Random-access jumps
{
std::array arr{1, 2, 3, 4, 5};
auto start = Iter::to_start_of({arr.data(), arr.size()});
auto end = Iter::to_end_of({arr.data(), arr.size()});
REQUIRE(start + 5 == end);
REQUIRE(end - 5 == start);
REQUIRE(start[1] == 2);
}
// Other bits
{
std::array arr{1, 2, 3, 4, 5};
Iter start = Iter::to_start_of({arr.data(), arr.size()});
++start;
CIter copy = start;
REQUIRE(*copy == 2);
REQUIRE(std::to_address(start) == arr.data() + 1);
}
return true;
}
static_assert(test_checked_iterator());
bool test_checked_iterator_bounds_checking()
{
using Iter = checked_iterator_t<int>;
std::array arr{1, 2, 3, 4, 5};
auto start = Iter::to_start_of({arr.data(), arr.size()});
auto end = Iter::to_end_of({arr.data(), arr.size()});
// Cannot deref end iterator
REQUIRE_ERROR(*end);
// Cannot deref advanced end iterator
REQUIRE_ERROR(*++Iter(end));
REQUIRE_ERROR(*Iter(end)++);
// Cannot deref decremented start iterator
REQUIRE_ERROR(*--Iter(start));
// REQUIRE_ERROR(*Iter(start)--);
// Cannot deref after out-of-bounds RA jumps
REQUIRE_ERROR(*(start + -1));
REQUIRE_ERROR(*(start - 1));
REQUIRE_ERROR(*(start + std::ssize(arr) + 1));
REQUIRE_ERROR(*(end + 1));
REQUIRE_ERROR(*(end - std::ssize(arr) - 1));
REQUIRE_ERROR(start[-1]);
REQUIRE_ERROR(start[std::ssize(arr)]);
REQUIRE_ERROR(start[std::ssize(arr) + 1]);
REQUIRE_ERROR(end[0]);
REQUIRE_ERROR(end[-std::ssize(arr) - 1]);
// Integer overflow checks
REQUIRE_ERROR(*(start + PTRDIFF_MAX));
REQUIRE_ERROR(*(start + PTRDIFF_MIN));
REQUIRE_ERROR(*(start - PTRDIFF_MAX));
REQUIRE_ERROR(*(start - PTRDIFF_MIN));
REQUIRE_ERROR(*(end + PTRDIFF_MAX));
REQUIRE_ERROR(*(end + PTRDIFF_MIN));
REQUIRE_ERROR(*(end - PTRDIFF_MAX));
REQUIRE_ERROR(*(end - PTRDIFF_MIN));
REQUIRE_ERROR(start[PTRDIFF_MAX]);
REQUIRE_ERROR(start[PTRDIFF_MIN]);
REQUIRE_ERROR(end[PTRDIFF_MAX]);
REQUIRE_ERROR(end[PTRDIFF_MIN]);
return true;
}
/*
* MARK: Slice tests
*/
template <typename S>
constexpr bool test_slice_traits()
{
// Slices are not default constructible, copyable or movable
static_assert(not std::is_default_constructible_v<S>);
static_assert(not std::is_copy_constructible_v<S>);
static_assert(not std::is_copy_assignable_v<S>);
static_assert(not std::is_move_constructible_v<S>);
static_assert(not std::is_move_assignable_v<S>);
static_assert(std::is_trivially_destructible_v<S>);
static_assert(not std::default_initializable<S>);
static_assert(not std::copyable<S>);
static_assert(not std::movable<S>);
static_assert(std::destructible<S>);
// Slices are contiguous, sized, and common ranges
static_assert(std::ranges::contiguous_range<S>);
static_assert(std::ranges::sized_range<S>);
static_assert(std::ranges::common_range<S>);
static_assert(std::ranges::contiguous_range<S const>);
static_assert(std::ranges::sized_range<S const>);
static_assert(std::ranges::common_range<S const>);
// Slices are not views, but they are (weirdly) borrowed ranges,
// even though you can't ever get one as an rvalue
static_assert(not std::ranges::view<S>); // not movable
static_assert(std::ranges::borrowed_range<S>);
static_assert(not std::ranges::view<S const>);
static_assert(std::ranges::borrowed_range<S const>);
// Associated range types are as expected
static_assert(std::same_as<std::ranges::range_reference_t<S>, int&>);
static_assert(std::same_as<std::ranges::range_value_t<S>, int>);
static_assert(std::same_as<std::ranges::range_rvalue_reference_t<S>, int&&>);
static_assert(std::same_as<std::ranges::range_reference_t<S const>, int const&>);
static_assert(std::same_as<std::ranges::range_value_t<S const>, int>);
static_assert(std::same_as<std::ranges::range_rvalue_reference_t<S const>, int const&&>);
return true;
}
static_assert(test_slice_traits<tcb::unchecked_slice<int>>());
static_assert(test_slice_traits<tcb::slice<int>>());
struct no_spaceship {
int i;
constexpr bool operator==(no_spaceship other) const { return i == other.i; }
constexpr bool operator<(no_spaceship other) const { return i < other.i; }
constexpr bool operator>(no_spaceship other) const { return other < *this; }
constexpr bool operator<=(no_spaceship other) const { return !(*this > other); }
constexpr bool operator>=(no_spaceship other) const { return !(*this < other); }
};
struct equality_only {
bool operator==(equality_only const&) const = default;
};
struct spaceship_only {
int i;
friend constexpr auto operator<=>(spaceship_only a, spaceship_only b) { return a.i <=> b.i; }
};
constexpr bool test_slice()
{
// Basic slice functionality
{
std::array arr{0, 1, 2, 3, 4};
auto ptr = tcb::ptr<int[]>::pointer_to(arr);
auto& slice = *ptr;
REQUIRE(&slice[0] == &arr[0]);
REQUIRE(&slice.at(1) == &arr[1]);
REQUIRE(&slice.front() == &arr.front());
REQUIRE(&slice.back() == &arr.back());
REQUIRE(slice.size() == arr.size());
REQUIRE(slice.empty() == arr.empty());
REQUIRE(slice.data() == arr.data());
REQUIRE(std::ranges::equal(slice, arr));
REQUIRE(std::ranges::equal(slice.cbegin(), slice.cend(), arr.cbegin(), arr.cend()));
REQUIRE(std::ranges::equal(slice | std::views::reverse, arr | std::views::reverse));
REQUIRE(std::ranges::equal(slice.crbegin(), slice.crend(), arr.crbegin(), arr.crend()));
}
// Same again, but const this time
{
std::array const arr{0, 1, 2, 3, 4};
auto ptr = tcb::ptr<int const[]>::pointer_to(arr);
auto& slice = *ptr;
REQUIRE(&slice[0] == &arr[0]);
REQUIRE(&slice.at(1) == &arr[1]);
REQUIRE(&slice.front() == &arr.front());
REQUIRE(&slice.back() == &arr.back());
REQUIRE(slice.size() == arr.size());
REQUIRE(slice.empty() == arr.empty());
REQUIRE(slice.data() == arr.data());
REQUIRE(std::ranges::equal(slice, arr));
REQUIRE(std::ranges::equal(slice.cbegin(), slice.cend(), arr.cbegin(), arr.cend()));
REQUIRE(std::ranges::equal(slice | std::views::reverse, arr | std::views::reverse));
REQUIRE(std::ranges::equal(slice.crbegin(), slice.crend(), arr.crbegin(), arr.crend()));
}
// Empty ranges are handled correctly
{
std::array<int, 0> arr{};
auto ptr = tcb::ptr<int[]>::pointer_to(arr);
auto& slice = *ptr;
REQUIRE(slice.size() == 0);
REQUIRE(slice.empty());
REQUIRE(slice.data() == arr.data());
REQUIRE(std::ranges::equal(slice, arr));
}
// Slice comparisons work as expected
{
auto array = std::array{1, 2, 3, 4, 5};
auto same_array = array;
auto shorter_array = std::array{1, 2, 3, 4};
auto different_array = std::array{1, 2, 99, 4, 5};
auto p_array = tcb::ptr<int[]>::pointer_to(array);
auto p_same_array = tcb::ptr<int[]>::pointer_to(same_array);
auto p_shorter_array = tcb::ptr<int[]>::pointer_to(shorter_array);
auto p_different_array = tcb::ptr<int[]>::pointer_to(different_array);
REQUIRE(*p_array == *p_same_array);
REQUIRE(*p_array != *p_shorter_array);
REQUIRE(*p_array != *p_different_array);
REQUIRE(*p_array <=> *p_same_array == std::strong_ordering::equal);
REQUIRE(*p_array <=> *p_shorter_array == std::strong_ordering::greater);
REQUIRE(*p_shorter_array <=> *p_array == std::strong_ordering::less);
// Float comparison should be partially ordered, and handle nans
if (!(compiler_is_msvc && std::is_constant_evaluated())) {
float nan = std::numeric_limits<float>::quiet_NaN();
float floats[] = {1.0f, nan, 3.0f};
auto p_floats = tcb::ptr<float const[]>::pointer_to(floats);
auto float_cmp = *p_floats <=> *p_floats;
static_assert(std::same_as<decltype(float_cmp), std::partial_ordering>);
REQUIRE(float_cmp == std::partial_ordering::unordered);
}
// We can compare types without a spaceship operator
{
no_spaceship ns[] = {{1}, {2}, {3}};
auto ptr = tcb::ptr_to_array(ns);
auto cmp = *ptr <=> *ptr;
static_assert(std::same_as<decltype(cmp), std::weak_ordering>);
REQUIRE(cmp == std::weak_ordering::equivalent);
}
// Comparison operators are constrained as expected
{
using incomparable = std::span<int>;
static_assert(std::equality_comparable<tcb::slice<int>>);
static_assert(std::equality_comparable<tcb::slice<equality_only>>);
static_assert(not std::equality_comparable<tcb::slice<spaceship_only>>);
static_assert(not std::equality_comparable<tcb::slice<incomparable>>);
static_assert(std::three_way_comparable<tcb::slice<int>>);
static_assert(not std::three_way_comparable<tcb::slice<equality_only>>);
static_assert(not std::three_way_comparable<tcb::slice<spaceship_only>>);
static_assert(not std::three_way_comparable<tcb::slice<incomparable>>);
}
}
// Bounds checking works correctly
if (!std::is_constant_evaluated()) {
std::array array{1, 2, 3, 4, 5};
auto p_array = tcb::ptr<int[]>::pointer_to(array);
auto p_const_array = tcb::ptr<int const[]>::pointer_to(array);
// op[]
REQUIRE_ERROR((*p_array)[5]);
REQUIRE_ERROR((*p_const_array)[5]);
// at()
REQUIRE_THROWS_AS(std::out_of_range, p_array->at(5));
REQUIRE_THROWS_AS(std::out_of_range, p_const_array->at(5));
auto empty_arr = std::array<int, 0>{};
tcb::ptr<int[]> p_empty_array = tcb::pointer_to_mut(empty_arr);
tcb::ptr<int const[]> p_const_empty_array = tcb::pointer_to(empty_arr);
// front()
REQUIRE_ERROR(p_empty_array->front());
REQUIRE_ERROR(p_const_empty_array->front());
// back()
REQUIRE_ERROR(p_empty_array->back());
REQUIRE_ERROR(p_const_empty_array->back());
}
return true;
}
static_assert(test_slice());
constexpr bool test_unchecked_slice()
{
// Basic slice functionality
{
std::array arr{0, 1, 2, 3, 4};
auto ptr = tcb::ptr<int[]>::pointer_to(arr);
auto& slice = ptr->unchecked;
REQUIRE(&slice[0] == &arr[0]);
REQUIRE(&slice.front() == &arr.front());
REQUIRE(&slice.back() == &arr.back());
REQUIRE(slice.size() == arr.size());
REQUIRE(slice.empty() == arr.empty());
REQUIRE(slice.data() == arr.data());
REQUIRE(std::ranges::equal(slice, arr));
REQUIRE(std::ranges::equal(slice.cbegin(), slice.cend(), arr.cbegin(), arr.cend()));
REQUIRE(std::ranges::equal(slice | std::views::reverse, arr | std::views::reverse));
REQUIRE(std::ranges::equal(slice.crbegin(), slice.crend(), arr.crbegin(), arr.crend()));
}
// Same again, but const this time
{
std::array const arr{0, 1, 2, 3, 4};
auto ptr = tcb::ptr<int const[]>::pointer_to(arr);
auto& slice = ptr->unchecked;
REQUIRE(&slice[0] == &arr[0]);
REQUIRE(&slice.front() == &arr.front());
REQUIRE(&slice.back() == &arr.back());
REQUIRE(slice.size() == arr.size());
REQUIRE(slice.empty() == arr.empty());
REQUIRE(slice.data() == arr.data());
REQUIRE(std::ranges::equal(slice, arr));
REQUIRE(std::ranges::equal(slice.cbegin(), slice.cend(), arr.cbegin(), arr.cend()));
REQUIRE(std::ranges::equal(slice | std::views::reverse, arr | std::views::reverse));
REQUIRE(std::ranges::equal(slice.crbegin(), slice.crend(), arr.crbegin(), arr.crend()));
}
// Empty ranges are handled correctly
{
std::array<int, 0> arr{};
auto ptr = tcb::ptr<int[]>::pointer_to(arr);
auto& slice = ptr->unchecked;
REQUIRE(slice.size() == 0);
REQUIRE(slice.empty());
REQUIRE(slice.data() == arr.data());
REQUIRE(std::ranges::equal(slice, arr));
}
// Slice comparisons work as expected
{
auto array = std::array{1, 2, 3, 4, 5};
auto same_array = array;
auto shorter_array = std::array{1, 2, 3, 4};
auto different_array = std::array{1, 2, 99, 4, 5};
auto p_array = tcb::ptr<int[]>::pointer_to(array);
auto p_same_array = tcb::ptr<int[]>::pointer_to(same_array);
auto p_shorter_array = tcb::ptr<int[]>::pointer_to(shorter_array);
auto p_different_array = tcb::ptr<int[]>::pointer_to(different_array);
auto& s_array = p_array->unchecked;
auto& s_same_array = p_same_array->unchecked;
auto& s_shorter_array = p_shorter_array->unchecked;
auto& s_different_array = p_different_array->unchecked;
REQUIRE(s_array == s_same_array);
REQUIRE(s_array != s_shorter_array);
REQUIRE(s_array != s_different_array);
REQUIRE(s_array <=> s_same_array == std::strong_ordering::equal);
REQUIRE(s_array <=> s_shorter_array == std::strong_ordering::greater);
REQUIRE(s_shorter_array <=> s_array == std::strong_ordering::less);
// Float comparison should be partially ordered, and handle nans
if (!(compiler_is_msvc && std::is_constant_evaluated())) {
float nan = std::numeric_limits<float>::quiet_NaN();