-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuffer_test.go
1169 lines (1084 loc) · 28.6 KB
/
buffer_test.go
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 2024 Paolo Fabio Zaino
//
// 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.
// Package buffer provides a generic buffer data structure.
package buffer_test
import (
"fmt"
"reflect"
"sync"
"testing"
buffer "github.com/pzaino/gods/pkg/buffer"
)
const (
errUnexpectedErr = "unexpected error: %v"
errExpectedLength = "expected length %v, got %v"
errExpectedErr = "expected error %v, got %v"
errExpectedValue = "expected value %v, got %v"
errBlitterErr = "Blitter should not return an error, got %v"
)
// Helper function to create a buffer with given elements and capacity
func createBufferWithElements(t *testing.T, elements []int, capacity uint64) *buffer.Buffer[int] {
b := buffer.New[int]()
b.SetCapacity(capacity)
for _, elem := range elements {
err := b.Append(elem)
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
}
return b
}
// TestNewBuffer tests the creation of a new buffer
func TestNewBuffer(t *testing.T) {
b := buffer.New[int]()
if b == nil {
t.Error("NewBuffer should not return nil")
}
if !b.IsEmpty() {
t.Error("NewBuffer should create an empty buffer")
}
}
// TestIsEmpty tests the IsEmpty method
func TestIsEmpty(t *testing.T) {
b := buffer.New[int]()
if !b.IsEmpty() {
t.Error("IsEmpty should return true for an empty buffer")
}
err := b.Append(1)
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
if b.IsEmpty() {
t.Error("IsEmpty should return false for a non-empty buffer")
}
b.Clear()
if !b.IsEmpty() {
t.Error("IsEmpty should return true for a cleared buffer")
}
b.Destroy()
if !b.IsEmpty() {
t.Error("IsEmpty should return true for a destroyed buffer")
}
}
// TestIsFull tests the IsFull method
func TestIsFull(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
if !b.IsFull() {
t.Error("IsFull should return true when buffer is full")
}
b = createBufferWithElements(t, []int{1, 2}, 3)
if b.IsFull() {
t.Error("IsFull should return false when buffer is not full")
}
}
// TestAppend tests the Append method
func TestAppend(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2}, 3)
err := b.Append(3)
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
if !b.IsFull() {
t.Error("Buffer should be full after appending to its capacity")
}
err = b.Append(4)
if err == nil {
t.Error("Append should return an error when the buffer is full")
}
if err.Error() != buffer.ErrBufferOverflow {
t.Errorf(errExpectedErr, buffer.ErrBufferOverflow, err)
}
}
// TestGet tests the Get method
func TestGet(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
elem, err := b.Get(1)
if err != nil {
t.Errorf("Get should not return an error, got %v", err)
}
if elem != 2 {
t.Errorf("Expected element 2, got %v", elem)
}
_, err = b.Get(3)
if err == nil {
t.Error("Get should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestSet tests the Set method
func TestSet(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
err := b.Set(1, 4)
if err != nil {
t.Errorf("Set should not return an error, got %v", err)
}
elem, _ := b.Get(1)
if elem != 4 {
t.Errorf("Expected element 4, got %v", elem)
}
err = b.Set(3, 5)
if err == nil {
t.Error("Set should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestRemove tests the Remove method
func TestRemove(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
err := b.Remove(1)
if err != nil {
t.Errorf("Remove should not return an error, got %v", err)
}
if b.Size() != 2 {
t.Errorf("Expected size 2, got %v", b.Size())
}
elem, _ := b.Get(1)
if elem != 3 {
t.Errorf("Expected element 3, got %v", elem)
}
err = b.Remove(2)
if err == nil {
t.Error("Remove should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestClear tests the Clear method
func TestClear(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
b.Clear()
if !b.IsEmpty() {
t.Error("Clear should empty the buffer")
}
}
// TestValues tests the Values method
func TestValues(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
values := b.Values()
if len(values) != 3 {
t.Errorf("Expected length 3, got %v", len(values))
}
for i, v := range values {
if v != i+1 {
t.Errorf(errExpectedValue, i+1, v)
}
}
}
// TestSize tests the Size method
func TestSize(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
if b.Size() != 3 {
t.Errorf("Expected size 3, got %v", b.Size())
}
}
// TestCapacity tests the Capacity method
func TestCapacity(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
if b.Capacity() != 3 {
t.Errorf("Expected capacity 3, got %v", b.Capacity())
}
}
// TestSetCapacity tests the SetCapacity method
func TestSetCapacity(t *testing.T) {
b := buffer.New[int]()
b.SetCapacity(5)
if b.Capacity() != 5 {
t.Errorf("Expected capacity 5, got %v", b.Capacity())
}
}
// TestEquals tests the Equals method
func TestEquals(t *testing.T) {
b1 := createBufferWithElements(t, []int{1, 2, 3}, 3)
b2 := createBufferWithElements(t, []int{1, 2, 3}, 3)
if !b1.Equals(b2) {
t.Error("Buffers should be equal")
}
b3 := createBufferWithElements(t, []int{1, 2}, 3)
if b1.Equals(b3) {
t.Error("Buffers should not be equal")
}
}
// TestToSlice tests the ToSlice method
func TestToSlice(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
slice := b.ToSlice()
if len(slice) != 3 {
t.Errorf("Expected length 3, got %v", len(slice))
}
for i, v := range slice {
if v != i+1 {
t.Errorf(errExpectedValue, i+1, v)
}
}
}
// TestReverse tests the Reverse method
func TestReverse(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
b.Reverse()
expected := []int{3, 2, 1}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestFind tests the Find method
func TestFind(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
index, err := b.Find(2)
if err != nil {
t.Errorf("Find should not return an error, got %v", err)
}
if index != 1 {
t.Errorf("Expected index 1, got %v", index)
}
_, err = b.Find(4)
if err == nil {
t.Error("Find should return an error for a non-existent value")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestContains tests the Contains method
func TestContains(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
if !b.Contains(2) {
t.Error("Buffer should contain value 2")
}
if b.Contains(4) {
t.Error("Buffer should not contain value 4")
}
}
// TestCopy tests the Copy method
func TestCopy(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 4)
copy := b.Copy()
if !b.Equals(copy) {
t.Error("Copy should create an equal buffer")
}
err := copy.Append(4)
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
if b.Equals(copy) {
t.Error("Modifying copy should not affect original buffer")
}
}
// TestMerge tests the Merge method
func TestMerge(t *testing.T) {
b1 := createBufferWithElements(t, []int{1, 2}, 3)
b2 := createBufferWithElements(t, []int{3}, 3)
b1.Merge(b2)
if b1.Size() != 3 {
t.Errorf("Expected size 3, got %v", b1.Size())
}
if !b1.Contains(3) {
t.Error("Buffer should contain merged elements")
}
if !b2.IsEmpty() {
t.Error("Merged buffer should be empty")
}
}
// TestPopN tests the PopN method
func TestPopN(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
values, err := b.PopN(2)
if err != nil {
t.Errorf("PopN should not return an error, got %v", err)
}
expected := []int{2, 3}
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
if b.Size() != 1 {
t.Errorf("Expected size 1, got %v", b.Size())
}
_, err = b.PopN(2)
if err == nil {
t.Error("PopN should return an error when popping more elements than present")
}
if err.Error() != buffer.ErrBufferEmpty {
t.Errorf(errExpectedErr, buffer.ErrBufferEmpty, err)
}
}
// TestPushN tests the PushN method
func TestPushN(t *testing.T) {
b := createBufferWithElements(t, []int{1}, 3)
err := b.PushN(2, 3)
if err != nil {
t.Errorf("PushN should not return an error, got %v", err)
}
if !b.IsFull() {
t.Error("Buffer should be full after pushing elements to its capacity")
}
err = b.PushN(4)
if err == nil {
t.Error("PushN should return an error when pushing beyond capacity")
}
if err.Error() != buffer.ErrBufferOverflow {
t.Errorf(errExpectedErr, buffer.ErrBufferOverflow, err)
}
}
// TestShiftLeft tests the ShiftLeft method
func TestShiftLeft(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
b.ShiftLeft(2)
expected := []int{3, 0, 0}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestShiftRight tests the ShiftRight method
func TestShiftRight(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
b.ShiftRight(2)
expected := []int{0, 0, 1}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestFilter tests the Filter method
func TestFilter(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.Filter(func(x int) bool {
return x%2 == 0
})
expected := []int{2, 4}
values := b.Values()
if len(values) != len(expected) {
t.Errorf(errExpectedLength, len(expected), len(values))
}
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestMap tests the Map method
func TestMap(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
newB, err := b.Map(func(x int) int {
return x * 2
})
if err != nil {
t.Errorf("Map should not return an error, got %v", err)
}
expected := []int{2, 4, 6}
values := newB.Values()
if len(values) != len(expected) {
t.Errorf(errExpectedLength, len(expected), len(values))
}
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestMapFrom tests the MapFrom method
func TestMapFrom(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
newB, err := b.MapFrom(1, func(x int) int {
return x * 2
})
if err != nil {
t.Errorf("MapFrom should not return an error, got %v", err)
}
expected := []int{4, 6}
values := newB.Values()
if len(values) != len(expected) {
t.Errorf(errExpectedLength, len(expected), len(values))
}
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
_, err = b.MapFrom(3, func(x int) int {
return x * 2
})
if err == nil {
t.Error("MapFrom should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrInvalidBuffer {
t.Errorf(errExpectedErr, buffer.ErrInvalidBuffer, err)
}
}
// TestMapRange tests the MapRange method
func TestMapRange(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
newB, err := b.MapRange(1, 3, func(x int) int {
return x * 2
})
if err != nil {
t.Errorf("MapRange should not return an error, got %v", err)
}
expected := []int{4, 6}
values := newB.Values()
if len(values) != len(expected) {
t.Errorf(errExpectedLength, len(expected), len(values))
}
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
_, err = b.MapRange(3, 3, func(x int) int {
return x * 2
})
if err == nil {
t.Error("MapRange should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrInvalidBuffer {
t.Errorf(errExpectedErr, buffer.ErrInvalidBuffer, err)
}
}
// TestReduce tests the Reduce method
func TestReduce(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
result, err := b.Reduce(func(x, y int) int {
return x + y
})
if err != nil {
t.Errorf("Reduce should not return an error, got %v", err)
}
if result != 6 {
t.Errorf("Expected result 6, got %v", result)
}
b.Clear()
_, err = b.Reduce(func(x, y int) int {
return x + y
})
if err == nil {
t.Error("Reduce should return an error for an empty buffer")
}
if err.Error() != buffer.ErrBufferEmpty {
t.Errorf(errExpectedErr, buffer.ErrBufferEmpty, err)
}
}
// TestReduceFrom tests the ReduceFrom method
func TestReduceFrom(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
result, err := b.ReduceFrom(1, func(x, y int) int {
return x + y
})
if err != nil {
t.Errorf("ReduceFrom should not return an error, got %v", err)
}
if result != 5 {
t.Errorf("Expected result 5, got %v", result)
}
_, err = b.ReduceFrom(3, func(x, y int) int {
return x + y
})
if err == nil {
t.Error("ReduceFrom should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrInvalidBuffer {
t.Errorf(errExpectedErr, buffer.ErrInvalidBuffer, err)
}
}
// TestReduceRange tests the ReduceRange method
func TestReduceRange(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
result, err := b.ReduceRange(1, 3, func(x, y int) int {
return x + y
})
if err != nil {
t.Errorf("ReduceRange should not return an error, got %v", err)
}
if result != 5 {
t.Errorf("Expected result 5, got %v", result)
}
_, err = b.ReduceRange(3, 3, func(x, y int) int {
return x + y
})
if err == nil {
t.Error("ReduceRange should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrInvalidBuffer {
t.Errorf(errExpectedErr, buffer.ErrInvalidBuffer, err)
}
}
// TestForEach tests the ForEach method
func TestForEach(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
err := b.ForEach(func(x *int) error {
*x *= 2
return nil
})
if err != nil {
t.Errorf("ForEach should not return an error, got %v", err)
}
expected := []int{2, 4, 6}
values := b.Values()
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestForRange tests the ForRange method
func TestForRange(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
err := b.ForRange(1, 3, func(x *int) error {
*x *= 2
return nil
})
if err != nil {
t.Errorf("ForRange should not return an error, got %v", err)
}
expected := []int{1, 4, 6}
values := b.Values()
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
err = b.ForRange(3, 3, func(x *int) error {
*x *= 2
return nil
})
if err == nil {
t.Error("ForRange should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrInvalidBuffer {
t.Errorf(errExpectedErr, buffer.ErrInvalidBuffer, err)
}
}
// TestForFrom tests the ForFrom method
func TestForFrom(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
err := b.ForFrom(1, func(x *int) error {
*x *= 2
return nil
})
if err != nil {
t.Errorf("ForFrom should not return an error, got %v", err)
}
expected := []int{1, 4, 6}
values := b.Values()
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
err = b.ForFrom(3, func(x *int) error {
*x *= 2
return nil
})
if err == nil {
t.Error("ForFrom should return an error for an out-of-bounds index")
}
if err.Error() != buffer.ErrInvalidBuffer {
t.Errorf(errExpectedErr, buffer.ErrInvalidBuffer, err)
}
}
// TestAny tests the Any method
func TestAny(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
if !b.Any(func(x int) bool {
return x == 2
}) {
t.Error("Any should return true if any element matches the predicate")
}
if b.Any(func(x int) bool {
return x == 4
}) {
t.Error("Any should return false if no element matches the predicate")
}
}
// TestAll tests the All method
func TestAll(t *testing.T) {
b := createBufferWithElements(t, []int{2, 4, 6}, 0)
if !b.All(func(x int) bool {
return x%2 == 0
}) {
t.Error("All should return true if all elements match the predicate")
}
err := b.Append(3)
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
if b.All(func(x int) bool {
return x%2 == 0
}) {
t.Error("All should return false if any element does not match the predicate")
}
}
// TestFindIndex tests the FindIndex method
func TestFindIndex(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
index, err := b.FindIndex(func(x int) bool {
return x == 2
})
if err != nil {
t.Errorf("FindIndex should not return an error, got %v", err)
}
if index != 1 {
t.Errorf("Expected index 1, got %v", index)
}
_, err = b.FindIndex(func(x int) bool {
return x == 4
})
if err == nil {
t.Error("FindIndex should return an error for a non-existent value")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestFindLast tests the FindLast method
func TestFindLast(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 2}, 4)
value, err := b.FindLast(func(x int) bool {
return x == 2
})
if err != nil {
t.Errorf("FindLast should not return an error, got %v", err)
}
if *value != 2 {
t.Errorf("Expected value 2, got %v", *value)
}
_, err = b.FindLast(func(x int) bool {
return x == 4
})
if err == nil {
t.Error("FindLast should return an error for a non-existent value")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestFindLastIndex tests the FindLastIndex method
func TestFindLastIndex(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 2}, 4)
index, err := b.FindLastIndex(func(x int) bool {
return x == 2
})
if err != nil {
t.Errorf("FindLastIndex should not return an error, got %v", err)
}
if index != 3 {
t.Errorf("Expected index 3, got %v", index)
}
_, err = b.FindLastIndex(func(x int) bool {
return x == 4
})
if err == nil {
t.Error("FindLastIndex should return an error for a non-existent value")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestFindAll tests the FindAll method
func TestFindAll(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 2}, 4)
newB := b.FindAll(func(x int) bool {
return x == 2
})
expected := []int{2, 2}
values := newB.Values()
if len(values) != len(expected) {
t.Errorf(errExpectedLength, len(expected), len(values))
}
for i, v := range values {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestFindIndices tests the FindIndices method
func TestFindIndices(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 2}, 4)
indices := b.FindIndices(func(x int) bool {
return x == 2
})
expected := []uint64{1, 3}
if len(indices) != len(expected) {
t.Errorf(errExpectedLength, len(expected), len(indices))
}
for i, v := range indices {
if v != expected[i] {
t.Errorf("Expected index %v, got %v", expected[i], v)
}
}
}
// TestLastIndexOf tests the LastIndexOf method
func TestLastIndexOf(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 2}, 4)
index, err := b.LastIndexOf(2)
if err != nil {
t.Errorf("LastIndexOf should not return an error, got %v", err)
}
if index != 3 {
t.Errorf("Expected index 3, got %v", index)
}
_, err = b.LastIndexOf(4)
if err == nil {
t.Error("LastIndexOf should return an error for a non-existent value")
}
if err.Error() != buffer.ErrValueNotFound {
t.Errorf(errExpectedErr, buffer.ErrValueNotFound, err)
}
}
// TestBlit tests the Blitter method
func TestBlit(t *testing.T) {
b1 := createBufferWithElements(t, []int{1, 2, 3}, 3)
b2 := createBufferWithElements(t, []int{4, 5, 6}, 3)
err := b1.Blit(b2, func(a, b int) int {
return a + b
})
if err != nil {
t.Errorf(errBlitterErr, err)
}
expected := []int{5, 7, 9}
for i, v := range b1.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
// Test with different buffer sizes
b3 := createBufferWithElements(t, []int{1, 2, 3, 4}, 4)
b4 := createBufferWithElements(t, []int{5, 6, 7}, 3)
err = b3.Blit(b4, func(a, b int) int {
return a + b
})
if err != nil {
t.Errorf(errBlitterErr, err)
}
expected = []int{6, 8, 10, 4}
for i, v := range b3.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
// Test with different buffer capacities
b5 := createBufferWithElements(t, []int{1, 2, 3}, 3)
b6 := createBufferWithElements(t, []int{4, 5, 6, 7}, 4)
err = b5.Blit(b6, func(a, b int) int {
return a + b
})
if err != nil {
t.Errorf(errBlitterErr, err)
}
expected = []int{5, 7, 9}
for i, v := range b5.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
// Test with different buffer sizes and capacities
b7 := createBufferWithElements(t, []int{1, 2, 3, 4}, 4)
b8 := createBufferWithElements(t, []int{5, 6, 7, 8}, 4)
err = b7.Blit(b8, func(a, b int) int {
return a + b
})
if err != nil {
t.Errorf(errBlitterErr, err)
}
expected = []int{6, 8, 10, 12}
for i, v := range b7.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestBlitFrom tests the BlitFrom method
func TestBlitFrom(t *testing.T) {
b1 := createBufferWithElements(t, []int{1, 2, 3}, 3)
b2 := createBufferWithElements(t, []int{4, 5, 6}, 3)
err := b1.BlitFrom(0, b2, func(a, b int) int {
return a + b
})
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
expected := []int{5, 7, 9}
for i, v := range b1.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
err = b1.BlitFrom(2, b2, func(a, b int) int {
return a - b
})
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
expected = []int{5, 7, 3}
for i, v := range b1.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
err = b1.BlitFrom(0, b2, func(a, b int) int {
return a * b
})
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
expected = []int{20, 35, 18}
for i, v := range b1.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestRotateLeft tests the RotateLeft method
func TestRotateLeft(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.RotateLeft(2)
expected := []int{3, 4, 5, 1, 2}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
b = createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.RotateLeft(5)
expected = []int{1, 2, 3, 4, 5}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
b = createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.RotateLeft(7)
expected = []int{3, 4, 5, 1, 2}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestRotateRight tests the RotateRight method
func TestRotateRight(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.RotateRight(2)
expected := []int{4, 5, 1, 2, 3}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
b = createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.RotateRight(5)
expected = []int{1, 2, 3, 4, 5}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
b = createBufferWithElements(t, []int{1, 2, 3, 4, 5}, 5)
b.RotateRight(7)
expected = []int{4, 5, 1, 2, 3}
for i, v := range b.Values() {
if v != expected[i] {
t.Errorf(errExpectedValue, expected[i], v)
}
}
}
// TestNewReference tests the NewReference method
func TestNewReference(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 4)
ref := b.NewReference()
if !b.Equals(ref) {
t.Error("NewReference should create a buffer with the same elements")
}
err := ref.Append(4)
if err != nil {
t.Errorf(errUnexpectedErr, err)
}
if b.Equals(ref) {
t.Error("Modifying the reference should not affect the original buffer")
}
}
// TestInsertAt tests the InsertAt method
func TestInsertAt(t *testing.T) {
b := createBufferWithElements(t, []int{1, 2, 3}, 3)
err := b.InsertAt(1, 4)
if err == nil {
t.Errorf("InsertAt should return an error when trying to add above capacity for a buffer with fixed capacity")