-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_arrayView.cpp
More file actions
1008 lines (769 loc) · 29 KB
/
test_arrayView.cpp
File metadata and controls
1008 lines (769 loc) · 29 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 2016 Ivan Ryabov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*******************************************************************************
* libSolace Unit Test Suit
* @file test/test_arrayView.cpp
* @brief Test set of Solace::ArrayView
******************************************************************************/
#include <solace/arrayView.hpp> // Class being tested.
#include <gtest/gtest.h>
#include "mockTypes.hpp"
using namespace Solace;
template <typename T, size_t N>
typename ArrayView<T>::size_type nativeArrayLength(const T (& SOLACE_UNUSED(t))[N]) { return N; }
template <typename T, size_t N, typename F>
T* generateTestArray(T (&carray)[N], F generator) {
for (size_t i = 0; i < N; ++i) {
carray[i] = generator(i);
}
return carray;
}
int fillOdd(size_t i) {
return static_cast<int>(2*i) - 1;
}
int fillEven(size_t i) {
static constexpr int bias = 2928;
return 2*static_cast<int>(i) + bias;
}
template <typename T>
std::ostream& operator<< (std::ostream& ostr, const Solace::ArrayView<T>& a) {
ostr << '[';
for (typename Solace::ArrayView<T>::size_type end = a.size(), i = 0; i < end; ++i) {
ostr << a[i];
if (i < end - 1) {
ostr << ',' << ' ';
}
}
ostr << ']';
return ostr;
}
struct MisalignedType {
byte b;
uint32 u32;
uint64 u64;
};
class TestArrayView : public ::testing::Test {
protected:
static const ArrayView<int>::size_type ZERO;
static const ArrayView<int>::size_type TEST_SIZE_0;
static const ArrayView<int>::size_type TEST_SIZE_1;
struct NonPodStruct {
static ArrayView<int>::size_type TotalCount;
static const int IVALUE_DEFAULT;
static const char* STR_DEFAULT;
int iValue;
std::string str;
virtual ~NonPodStruct() {
--TotalCount;
}
NonPodStruct(int i, const char* inStr) :
iValue(i), str(inStr)
{
++TotalCount;
}
NonPodStruct(int i, std::string&& inStr) :
iValue(i), str(mv(inStr))
{
++TotalCount;
}
NonPodStruct() : iValue(IVALUE_DEFAULT), str(STR_DEFAULT)
{
++TotalCount;
}
NonPodStruct(NonPodStruct&& other) : iValue(other.iValue), str(mv(other.str))
{
++TotalCount;
}
NonPodStruct(const NonPodStruct& other) : iValue(other.iValue), str(other.str)
{
++TotalCount;
}
NonPodStruct& operator= (const NonPodStruct& rhs) {
iValue = rhs.iValue;
str = rhs.str;
return (*this);
}
NonPodStruct& operator= (NonPodStruct&& rhs) {
iValue = mv(rhs.iValue);
str = mv(rhs.str);
return (*this);
}
bool operator== (const NonPodStruct& other) const {
return iValue == other.iValue && str == other.str;
}
};
struct DerivedNonPodStruct : public NonPodStruct {
float fValue;
~DerivedNonPodStruct() override = default;
DerivedNonPodStruct() :
NonPodStruct(312, "Derived String"), fValue(3.1415f)
{
}
DerivedNonPodStruct(int x, float f, const char* inStr) :
NonPodStruct(x, inStr), fValue(f)
{
}
};
static NonPodStruct fillOddNonPods(size_t i) {
auto const index = static_cast<int>(i)*2 - 1;
auto str = std::string("Some Odd string: ") + std::to_string(index);
return NonPodStruct(index, mv(str));
}
static NonPodStruct fillEvenNonPods(size_t i) {
auto const index = static_cast<int>(i)*2 + 1;
auto str = std::string("Some Event string: ") + std::to_string(index);
return NonPodStruct(index, mv(str));
}
template<typename T>
class NonPodGuard {
public:
~NonPodGuard() {
for (size_t i = 0; i < _size; ++i) {
reinterpret_cast<T*>(_mem + i * sizeof(T))->~T();
}
}
NonPodGuard(byte* mem, size_t size) :
_mem(mem),
_size(size)
{
for (size_t i = 0; i < _size; ++i) {
new ((_mem + i * sizeof(T))) T();
}
}
private:
byte* _mem;
size_t _size;
};
public:
void SetUp() override {
// TODO(abbyssoul): Debug::BeginMemCheck();
EXPECT_EQ(ZERO, NonPodStruct::TotalCount);
EXPECT_EQ(0, SometimesConstructable::InstanceCount);
}
void TearDown() override {
// TODO(abbyssoul): Debug::EndMemCheck();
EXPECT_EQ(ZERO, NonPodStruct::TotalCount);
EXPECT_EQ(0, SometimesConstructable::InstanceCount);
}
};
TEST_F(TestArrayView, testEmpty) {
{
const ArrayView<int> empty_array;
EXPECT_TRUE(empty_array.empty());
EXPECT_EQ(ZERO, empty_array.size());
EXPECT_EQ(empty_array.begin(), empty_array.end());
EXPECT_EQ(nullptr, empty_array);
}
{
const ArrayView<NonPodStruct> empty_array(nullptr);
EXPECT_TRUE(empty_array.empty());
EXPECT_EQ(ZERO, empty_array.size());
EXPECT_EQ(empty_array.begin(), empty_array.end());
EXPECT_EQ(nullptr, empty_array);
}
{
const ArrayView<DerivedNonPodStruct> empty_array;
EXPECT_TRUE(empty_array.empty());
EXPECT_EQ(ZERO, empty_array.size());
EXPECT_EQ(empty_array.begin(), empty_array.end());
EXPECT_EQ(nullptr, empty_array);
}
}
TEST_F(TestArrayView, testConstructionFromMemoryResource) {
byte buffer[32]; // 32 bytes is 8 u32.
EXPECT_EQ(32U, arrayView(buffer).size());
EXPECT_EQ(8U, arrayView(reinterpret_cast<uint32*>(buffer), sizeof (buffer) / sizeof (uint32)).size());
}
TEST_F(TestArrayView, testCopyConstruction) {
int src[16];
const ArrayView<int>::size_type srcSize = sizeof(src) / sizeof(int);
generateTestArray(src, fillOdd);
ArrayView<int> a2(src);
EXPECT_TRUE(!a2.empty());
EXPECT_EQ(srcSize, a2.size());
// Create a copy:
ArrayView<int> a1(a2);
EXPECT_TRUE(!a1.empty());
EXPECT_EQ(a1.size(), a2.size());
// Check that the data is the same:
for (ArrayView<int>::size_type i = 0; i < a1.size(); ++i) {
EXPECT_EQ(static_cast<int>(2*i) - 1, a1[i]);
}
// Check that changing values in the original C-array changes ArrayView values:
auto newGen = [](size_t i) { return static_cast<int>(2*i + 3); };
generateTestArray(src, newGen);
EXPECT_EQ(a1.size(), a2.size());
// Check that the data is the same:
for (ArrayView<int>::size_type i = 0; i < a1.size(); ++i) {
EXPECT_EQ(newGen(i), a1[i]);
EXPECT_EQ(newGen(i), a2[i]);
}
}
TEST_F(TestArrayView, testCopy) {
{
int src[16];
generateTestArray(src, fillOdd);
ArrayView<int> a1;
ArrayView<int> a2(src);
EXPECT_TRUE(a1.empty());
EXPECT_TRUE(!a2.empty());
// Copy arrays
a1 = a2;
EXPECT_TRUE(!a1.empty());
EXPECT_TRUE(!a2.empty());
EXPECT_EQ(a1.size(), a2.size());
for (ArrayView<int>::size_type i = 0; i < a1.size(); ++i) {
EXPECT_EQ(fillOdd(i), a1[i]);
}
// Make sure that if underlaying memory changed - this is reflected in arrayView:
generateTestArray(src, fillEven);
for (ArrayView<int>::size_type i = 0; i < a1.size(); ++i) {
EXPECT_EQ(fillEven(i), a1[i]);
EXPECT_EQ(fillEven(i), a2[i]);
}
}
}
TEST_F(TestArrayView, dataTypesFitness) {
byte buf[sizeof(SimpleType)];
auto arr = arrayView<SimpleType>(wrapMemory(buf));
ASSERT_EQ(1U, arr.size());
arr[0].y = 3;
EXPECT_EQ(3, arr[0].y);
}
TEST_F(TestArrayView, dataTypeNotFitting) {
byte buf[sizeof(SimpleType) + sizeof(SimpleType) /2];
auto arr = arrayView<SimpleType>(wrapMemory(buf));
ASSERT_EQ(1U, arr.size());
arr[0].y = 3;
EXPECT_EQ(3, arr[0].y);
}
/*
TEST_F(TestArrayView, testBasics) {
ArrayView<uint> array(TEST_SIZE_0);
EXPECT_TRUE(!array.empty());
EXPECT_EQ(TEST_SIZE_0, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(0U, array[i]);
}
uint count = 0;
for (auto i = array.begin(), end = array.end(); i != end; ++i) {
EXPECT_EQ(0U, *i);
*i = count++;
}
EXPECT_EQ(static_cast<ArrayView<uint>::size_type>(count), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(static_cast<uint>(i), array[i]);
}
// TODO(abbyssoul): Test access pass end!!
}
TEST_F(TestArrayView, testString) {
ArrayView<String> array(TEST_SIZE_0);
EXPECT_TRUE(!array.empty());
EXPECT_EQ(TEST_SIZE_0, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(String::Empty, array[i]);
}
auto count = ZERO;
for (auto& i : array) {
EXPECT_TRUE(i.empty());
i = "Item " + std::to_string(count++);
}
EXPECT_EQ(count, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(String("Item " + std::to_string(i)), array[i]);
}
}
TEST_F(TestArrayView, testNonPods) {
EXPECT_EQ(ZERO, NonPodStruct::TotalCount);
{
ArrayView<NonPodStruct> array(TEST_SIZE_1);
EXPECT_EQ(TEST_SIZE_1, array.size());
EXPECT_EQ(NonPodStruct::TotalCount, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(NonPodStruct::IVALUE_DEFAULT, array[i].iValue);
EXPECT_EQ(NonPodStruct::STR_DEFAULT, array[i].str);
}
decltype(NonPodStruct::iValue) count = ZERO;
for (auto &i : array) {
EXPECT_EQ(NonPodStruct::IVALUE_DEFAULT, i.iValue);
EXPECT_EQ(NonPodStruct::STR_DEFAULT, i.str);
i.iValue = count++;
i.str = "Item " + std::to_string(i.iValue);
}
EXPECT_EQ(static_cast<ArrayView<NonPodStruct>::size_type>(count), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(static_cast<int>(i), array[i].iValue);
EXPECT_EQ(String("Item " + std::to_string(i)), array[i].str);
}
}
EXPECT_EQ(ZERO, NonPodStruct::TotalCount);
}
TEST_F(TestArrayView, testInitializerList) {
{
const int native_array[] = {0, 1, 2, 3};
const ArrayView<int> array = {0, 1, 2, 3};
EXPECT_EQ(nativeArrayLength(native_array), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(native_array[i], array[i]);
}
}
{
const String native_array[] = {"Abc", "", "dfe", "_xyz3"};
const ArrayView<String> array = {"Abc", "", "dfe", "_xyz3"};
EXPECT_EQ(nativeArrayLength(native_array), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(native_array[i], array[i]);
}
}
EXPECT_EQ(ZERO, NonPodStruct::TotalCount);
{
const NonPodStruct native_array[] = {
NonPodStruct(0, "yyyz"),
NonPodStruct(),
NonPodStruct(-321, "yyx"),
NonPodStruct(990, "x^hhf")
};
EXPECT_EQ(nativeArrayLength(native_array), NonPodStruct::TotalCount);
const ArrayView<NonPodStruct> array = {
NonPodStruct(0, "yyyz"),
NonPodStruct(),
NonPodStruct(-321, "yyx"),
NonPodStruct(990, "x^hhf")
};
EXPECT_EQ(nativeArrayLength(native_array), array.size());
EXPECT_EQ(nativeArrayLength(native_array) + array.size(), NonPodStruct::TotalCount);
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(native_array[i].iValue, array[i].iValue);
EXPECT_EQ(native_array[i].str, array[i].str);
}
}
EXPECT_EQ(ZERO, NonPodStruct::TotalCount);
}
TEST_F(TestArrayView, testFromNativeConvertion) {
{
const int native_array[] = {0, 1, 2, 3};
const ArrayView<int> array(nativeArrayLength(native_array), native_array);
EXPECT_EQ(nativeArrayLength(native_array), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(native_array[i], array[i]);
}
}
{
const String native_array[] = {"Abc", "", "dfe", "_xyz3"};
const ArrayView<String> array(nativeArrayLength(native_array), native_array);
EXPECT_EQ(nativeArrayLength(native_array), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(native_array[i], array[i]);
}
}
{
const NonPodStruct native_array[] = {
NonPodStruct(0, "yyyz"),
NonPodStruct(),
NonPodStruct(-321, "yyx"),
NonPodStruct(990, "x^hhf")
};
const ArrayView<NonPodStruct> array(nativeArrayLength(native_array), native_array);
EXPECT_EQ(nativeArrayLength(native_array), array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(native_array[i].iValue, array[i].iValue);
EXPECT_EQ(native_array[i].str, array[i].str);
}
}
}
template <typename T>
ArrayView<T> moveArray(std::initializer_list<T> list) {
return {list};
}
TEST_F(TestArrayView, testMoveAssignment) {
{// Test on integral types
ArrayView<int> array(0);
EXPECT_TRUE(array.empty());
EXPECT_EQ(ZERO, array.size());
array = moveArray<int>({1, 2, 3});
EXPECT_TRUE(!array.empty());
const int src1[] = {1, 2, 3};
EXPECT_EQ(3, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(src1[i], array[i]);
}
}
{ // Test on strings types
ArrayView<String> array(0);
EXPECT_TRUE(array.empty());
array = moveArray<String>({"tasrd", "", "hhha", "asd"});
EXPECT_TRUE(!array.empty());
const String src[] = {"tasrd", "", "hhha", "asd"};
EXPECT_EQ(4, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(src[i], array[i]);
}
}
{ // Test on non-pod types
ArrayView<NonPodStruct> array(0);
const NonPodStruct src[] = {
NonPodStruct(0, "yyyz"),
NonPodStruct(),
NonPodStruct(-321, "yyx"),
NonPodStruct(990, "x^hhf")
};
EXPECT_TRUE(array.empty());
array = moveArray<NonPodStruct>({
NonPodStruct(0, "yyyz"),
NonPodStruct(),
NonPodStruct(-321, "yyx"),
NonPodStruct(990, "x^hhf")
});
EXPECT_TRUE(!array.empty());
EXPECT_EQ(4, array.size());
for (auto i = ZERO, end = array.size(); i < end; ++i) {
EXPECT_EQ(src[i].iValue, array[i].iValue);
EXPECT_EQ(src[i].str, array[i].str);
}
}
}
*/
TEST_F(TestArrayView, testEquals_EmptyArray) {
ArrayView<int> emptyArray;
EXPECT_TRUE(emptyArray == nullptr);
EXPECT_TRUE(!(emptyArray != nullptr));
EXPECT_TRUE(emptyArray.equals(emptyArray));
{ // Check that array views of the same memory are equal:
ArrayView<int> differenEmptyArray;
EXPECT_TRUE(emptyArray.equals(differenEmptyArray));
EXPECT_TRUE(differenEmptyArray.equals(emptyArray));
EXPECT_TRUE(emptyArray == differenEmptyArray);
EXPECT_TRUE(differenEmptyArray == emptyArray);
EXPECT_TRUE(!(emptyArray != differenEmptyArray));
EXPECT_TRUE(!(differenEmptyArray != emptyArray));
}
}
TEST_F(TestArrayView, testEquals_IntegralType) {
int src[32];
generateTestArray(src, fillOdd);
auto array = arrayView(src);
// Make sure it is not equals empty array if it is not empty
EXPECT_TRUE(!array.equals(ArrayView<int>()));
EXPECT_TRUE(!(array == ArrayView<int>()));
EXPECT_TRUE(!(array == nullptr));
EXPECT_TRUE(array != ArrayView<int>());
EXPECT_TRUE(array != nullptr);
// Self equality is important
EXPECT_TRUE(array.equals(array));
{ // Check that array views of the same memory are equal:
ArrayView<int> arraySharingMemory(src);
EXPECT_TRUE(array.equals(arraySharingMemory));
EXPECT_TRUE(arraySharingMemory.equals(array));
EXPECT_TRUE(array == arraySharingMemory);
EXPECT_TRUE(arraySharingMemory == array);
EXPECT_TRUE(!(array != arraySharingMemory));
EXPECT_TRUE(!(arraySharingMemory != array));
}
{ // Unrelated memory buffer array equals by value:
byte byteSrc[32 * sizeof (int)];
ArrayView<int> arrayBytes(wrapMemory(byteSrc));
arrayBytes.fill(fillOdd);
EXPECT_TRUE(array.equals(arrayBytes));
EXPECT_TRUE(arrayBytes.equals(array));
EXPECT_TRUE(array == arrayBytes);
EXPECT_TRUE(arrayBytes == array);
EXPECT_TRUE(!(array != arrayBytes));
EXPECT_TRUE(!(arrayBytes != array));
}
{ // Unrelated smaller memory buffer array filled with the same values no equals:
byte byteSrc[24 * sizeof (int)];
ArrayView<int> arrayBytes(wrapMemory(byteSrc));
arrayBytes.fill(fillOdd);
EXPECT_TRUE(!array.equals(arrayBytes));
EXPECT_TRUE(!arrayBytes.equals(array));
EXPECT_TRUE(array != arrayBytes);
EXPECT_TRUE(arrayBytes != array);
EXPECT_TRUE(!(array == arrayBytes));
EXPECT_TRUE(!(arrayBytes == array));
}
{ // Unrelated memory buffer array filled with different values not equal by value:
byte byteSrc[32 * sizeof (int)];
ArrayView<int> arrayBytes(wrapMemory(byteSrc));
arrayBytes.fill(fillEven);
EXPECT_TRUE(!array.equals(arrayBytes));
EXPECT_TRUE(!arrayBytes.equals(array));
EXPECT_TRUE(array != arrayBytes);
EXPECT_TRUE(arrayBytes != array);
EXPECT_TRUE(!(array == arrayBytes));
EXPECT_TRUE(!(arrayBytes == array));
}
}
TEST_F(TestArrayView, testEquals_NonPodType) {
static constexpr size_t kNonPodStruct = 81;
NonPodStruct src[kNonPodStruct];
generateTestArray(src, fillOddNonPods);
auto array = arrayView(src);
// Make sure it is not equals empty array if it is not empty
EXPECT_TRUE(!array.equals(ArrayView<NonPodStruct>()));
EXPECT_TRUE(!(array == ArrayView<NonPodStruct>()));
EXPECT_TRUE(!(array == nullptr));
EXPECT_TRUE(array != ArrayView<NonPodStruct>());
EXPECT_TRUE(array != nullptr);
// Self equality is important
EXPECT_TRUE(array.equals(array));
{ // Check that array views of the same memory are equal:
ArrayView<NonPodStruct> arraySharingMemory(src);
EXPECT_TRUE(array.equals(arraySharingMemory));
EXPECT_TRUE(arraySharingMemory.equals(array));
EXPECT_TRUE(array == arraySharingMemory);
EXPECT_TRUE(arraySharingMemory == array);
EXPECT_TRUE(!(array != arraySharingMemory));
EXPECT_TRUE(!(arraySharingMemory != array));
}
{ // Unrelated memory buffer array equals by value:
byte byteSrc[kNonPodStruct * sizeof(NonPodStruct)];
NonPodGuard<NonPodStruct> guard(byteSrc, kNonPodStruct);
ArrayView<NonPodStruct> arrayBytes(wrapMemory(byteSrc));
arrayBytes.fill(fillOddNonPods);
EXPECT_TRUE(array.equals(arrayBytes));
EXPECT_TRUE(arrayBytes.equals(array));
EXPECT_TRUE(array == arrayBytes);
EXPECT_TRUE(arrayBytes == array);
EXPECT_TRUE(!(array != arrayBytes));
EXPECT_TRUE(!(arrayBytes != array));
}
{ // Unrelated smaller memory buffer array filled with the same values no equals:
static constexpr size_t kOtherNonPodStruct = 112;
byte byteSrc[kOtherNonPodStruct * sizeof(NonPodStruct)];
NonPodGuard<NonPodStruct> guard(byteSrc, kOtherNonPodStruct);
ArrayView<NonPodStruct> arrayBytes(wrapMemory(byteSrc));
arrayBytes.fill(fillOddNonPods);
EXPECT_TRUE(!array.equals(arrayBytes));
EXPECT_TRUE(!arrayBytes.equals(array));
EXPECT_TRUE(array != arrayBytes);
EXPECT_TRUE(arrayBytes != array);
EXPECT_TRUE(!(array == arrayBytes));
EXPECT_TRUE(!(arrayBytes == array));
}
{ // Unrelated memory buffer array filled with different values not equal by value:
byte byteSrc[kNonPodStruct * sizeof(NonPodStruct)];
NonPodGuard<NonPodStruct> guard(byteSrc, kNonPodStruct);
ArrayView<NonPodStruct> arrayBytes(wrapMemory(byteSrc));
arrayBytes.fill(fillEvenNonPods);
EXPECT_TRUE(!array.equals(arrayBytes));
EXPECT_TRUE(!arrayBytes.equals(array));
EXPECT_TRUE(array != arrayBytes);
EXPECT_TRUE(arrayBytes != array);
EXPECT_TRUE(!(array == arrayBytes));
EXPECT_TRUE(!(arrayBytes == array));
}
}
TEST_F(TestArrayView, testIndexOf) {
int src[16];
generateTestArray(src, [](size_t i) { return (2*static_cast<int>(i) - 1); });
auto array = arrayView(src);
{ // Test for existing value:
auto const maybeIndex = array.indexOf(2*4 - 1);
EXPECT_TRUE(maybeIndex.isSome());
EXPECT_EQ(ArrayView<int>::size_type(4), maybeIndex.get());
}
{ // Can we find this sequance? Yes we can
auto const view = ArrayView<SimpleType>(wrapMemory(src));
EXPECT_TRUE(view.indexOf(SimpleType(5, 7, 9)).isSome());
}
{ // Test for non-existing value:
EXPECT_TRUE(array.indexOf(3*4 + 128).isNone());
}
{ // Test empty array contains nothing
EXPECT_TRUE(ArrayView<int>().indexOf(2*3 - 1).isNone());
}
{ // Can we find this sequance? Nope
auto const view = ArrayView<SimpleType>(wrapMemory(src));
EXPECT_TRUE(view.indexOf(SimpleType(3, 2, 1)).isNone());
}
}
TEST_F(TestArrayView, testContains) {
{ // Test empty array contains nothing
EXPECT_TRUE(!ArrayView<int>().contains(2*3 - 1));
}
int src[24];
generateTestArray(src, [](size_t i) { return static_cast<int>(i)*2 + 3; });
auto array = arrayView(src);
{ // Test for an existing value:
EXPECT_TRUE(array.contains(2*9 + 3));
}
{ // Test for non-existing value:
EXPECT_TRUE(!array.contains(-41));
}
{ // Can we find this sequance? Yes we can
auto const view = ArrayView<SimpleType>(wrapMemory(src));
EXPECT_TRUE(view.contains(SimpleType(15, 17, 19)));
}
}
TEST_F(TestArrayView, testFillWithConstValue) {
int src[24];
auto array = arrayView(src);
array.fill(42);
for (auto const i : array) {
EXPECT_EQ(42, i);
}
}
TEST_F(TestArrayView, testFillWithConstExplosiveValue) {
EXPECT_EQ(0, SometimesConstructable::InstanceCount);
{
static constexpr size_t kNonPodStruct = 24;
byte src[kNonPodStruct * sizeof (SometimesConstructable)];
SometimesConstructable::BlowUpEveryInstance = 0;
NonPodGuard<SometimesConstructable> guard(src, kNonPodStruct);
SometimesConstructable::BlowUpEveryInstance = 9;
ArrayView<SometimesConstructable> array(wrapMemory(src));
// This should not throw as we don't create any new instances apart from +1 used as a temp template
array.fill(SometimesConstructable(99));
EXPECT_EQ(static_cast<int>(kNonPodStruct), SometimesConstructable::InstanceCount);
for (int i = 0; i < SometimesConstructable::InstanceCount; ++i) {
EXPECT_EQ(99, array[i].someValue);
}
}
EXPECT_EQ(0, SometimesConstructable::InstanceCount);
}
TEST_F(TestArrayView, testFillWithGenerator) {
int src[24];
auto array = arrayView(src);
array.fill([](ArrayView<int>::size_type i) { return static_cast<int>(i)*2 - 187; });
for (ArrayView<int>::size_type i = 0; i < array.size(); ++i) {
EXPECT_EQ(static_cast<int>(i)*2 - 187, array[i]);
}
}
TEST_F(TestArrayView, slice) {
int src[24];
auto array = arrayView(src);
array.fill([](ArrayView<int>::size_type i) { return static_cast<int>(i); });
EXPECT_EQ(array.size(), array.slice(0, array.size()).size());
EXPECT_EQ(array, array.slice(0, array.size()));
auto halfView = array.slice(12, 22);
EXPECT_EQ(10U, halfView.size());
for (ArrayView<int>::size_type i = 0; i < halfView.size(); ++i) {
EXPECT_EQ(static_cast<int>(12 + i), halfView[i]);
}
EXPECT_TRUE(array.slice(12, 12).empty());
EXPECT_TRUE(array.slice(128, 300).empty());
EXPECT_EQ(14U, array.slice(10, 300).size());
EXPECT_TRUE(array.slice(128, 21).empty());
EXPECT_TRUE(array.slice(21, 7).empty());
}
TEST_F(TestArrayView, testFillWithGeneratorOfExplosiveValue) {
EXPECT_EQ(0, SometimesConstructable::InstanceCount);
{
static constexpr size_t kNonPodStruct = 81;
byte src[kNonPodStruct * sizeof(SometimesConstructable)];
SometimesConstructable::BlowUpEveryInstance = 0;
NonPodGuard<SometimesConstructable> guard(src, kNonPodStruct);
SometimesConstructable::BlowUpEveryInstance = 13;
ArrayView<SometimesConstructable> array(wrapMemory(src));
// This should not throw as we don't create any new instances apart from +1 used as a temp template
array.fill([](size_t i ) { return SometimesConstructable(fillOdd(i)); });
EXPECT_EQ(static_cast<int>(kNonPodStruct), SometimesConstructable::InstanceCount);
for (int i = 0; i < SometimesConstructable::InstanceCount; ++i) {
EXPECT_EQ(fillOdd(i), array[i].someValue);
}
}
// Make sure that after the array has been destroyed no instances of SometimesConstructable exist.
EXPECT_EQ(0, SometimesConstructable::InstanceCount);
}
TEST_F(TestArrayView, testForEach_byValue) {
int baseArray[] = {1, 2, 3, 4, 5, 6};
const ArrayView<int> array{baseArray};
int acc = 0;
array.forEach([&acc](int x) {
acc += x;
});
EXPECT_EQ(21, acc);
}
TEST_F(TestArrayView, testForEach_byConstRef) {
EXPECT_EQ(0, SimpleType::InstanceCount);
{
SimpleType baseArray[] = {{3, 2, 1}, {2, 1, 3}, {0, -1, 2}, {-1, 0, -4}};
const ArrayView<SimpleType> array{baseArray};
SimpleType acc;
array.forEach([&acc](const SimpleType& x) {
acc.x += x.x;
acc.y += x.y;
acc.z += x.z;
});
EXPECT_EQ(SimpleType(4, 2, 2), acc);
}
EXPECT_EQ(0, SimpleType::InstanceCount);
}
TEST_F(TestArrayView, testForEach_byValueConversion) {
int baseArray[] = {1, 2, 3, 4, 5, 6};
const ArrayView<int> array{baseArray};
double acc = 0;
array.forEach([&acc](double x) {
acc += x;
});
EXPECT_FLOAT_EQ(21.0, acc);
}
TEST_F(TestArrayView, testForEachIndexed) {
int baseArray[] = {1, 2, 3, 4, 5, 6};
const ArrayView<int> array{baseArray};
bool allEq = true;
array.forEach([&allEq](ArrayView<int>::size_type i, uint x) {
allEq &= (i + 1 == x);
});
EXPECT_EQ(true, allEq);
}
TEST_F(TestArrayView, sizeNarrowing) {
byte buffer[sizeof (MisalignedType) * 3 + sizeof (MisalignedType) / 2];
// FIXME: Maybe we should return Err() in this case as it is a narrowing
auto value = arrayView<MisalignedType>(wrapMemory(buffer));
ASSERT_EQ(3U, value.size());
}
TEST_F(TestArrayView, viewConstPointerList) {
SimpleType list[] = { {3, 2, 1}, {0, -1, -2} };
auto array = arrayView(std::begin(list), std::end(list));
EXPECT_EQ(2U, array.size());
EXPECT_EQ(1, array[0].z);
EXPECT_EQ(0, array[1].x);
}
/*
TEST_F(TestArrayView, testMap) {
DerivedNonPodStruct baseArray[] = {
DerivedNonPodStruct(32, 2.4, "hello"),
DerivedNonPodStruct(-24, 2.4, " "),
DerivedNonPodStruct(10, 2.4, "world")
};
const ArrayView<DerivedNonPodStruct> array{baseArray};
{
auto r = array.map([](const DerivedNonPodStruct& content) {
return content.iValue;
});
EXPECT_EQ(array.size(), r.size());
for (ArrayView<int>::size_type i = 0; i < array.size(); ++i) {
EXPECT_EQ(r[i], array[i].iValue);
}
}
{
auto r = array.map([](const DerivedNonPodStruct& content) {
return content.str;
});
EXPECT_EQ(array.size(), r.size());
for (ArrayView<int>::size_type i = 0; i < array.size(); ++i) {
EXPECT_EQ(r[i], array[i].str);
}
}
}
*/
const ArrayView<int>::size_type TestArrayView::ZERO = 0;