-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_test.go
1007 lines (903 loc) · 25 KB
/
math_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
package g
import (
"math"
"reflect"
"runtime"
"testing"
)
// TestDoMiniMaxInt tests the doMiniMax function with integer values.
func TestDoMiniMaxInt(t *testing.T) {
// Test case where m is true (finding maximum)
values := []int{1, 3, 2, 5, 4}
expectedMax := 5
result := doMiniMax(true, values...)
if result != expectedMax {
t.Errorf("Expected max %d, got %d", expectedMax, result)
}
// Test case where m is false (finding minimum)
expectedMin := 1
result = doMiniMax(false, values...)
if result != expectedMin {
t.Errorf("Expected min %d, got %d", expectedMin, result)
}
}
// Test with float64 values
func TestDoMiniMaxFloat64(t *testing.T) {
// Test case where m is true (finding maximum)
values := []float64{1.1, 3.3, 2.2, 5.5, 4.4}
expectedMax := 5.5
result := doMiniMax(true, values...)
if result != expectedMax {
t.Errorf("Expected max %f, got %f", expectedMax, result)
}
// Test case where m is false (finding minimum)
expectedMin := 1.1
result = doMiniMax(false, values...)
if result != expectedMin {
t.Errorf("Expected min %f, got %f", expectedMin, result)
}
}
// Test with a single element
func TestDoMiniMaxSingleElement(t *testing.T) {
value := []int{42}
expected := 42
result := doMiniMax(true, value...)
if result != expected {
t.Errorf("Expected %d, got %d", expected, result)
}
result = doMiniMax(false, value...)
if result != expected {
t.Errorf("Expected %d, got %d", expected, result)
}
}
// Test with an empty slice
func TestDoMiniMaxEmptySlice(t *testing.T) {
var values []int
var expected int // Zero value of int
result := doMiniMax(true, values...)
if result != expected {
t.Errorf("Expected zero value %d, got %d", expected, result)
}
result = doMiniMax(false, values...)
if result != expected {
t.Errorf("Expected zero value %d, got %d", expected, result)
}
}
// Test with all elements equal
func TestDoMiniMaxEqualElements(t *testing.T) {
values := []int{2, 2, 2, 2}
expected := 2
result := doMiniMax(true, values...)
if result != expected {
t.Errorf("Expected %d, got %d", expected, result)
}
result = doMiniMax(false, values...)
if result != expected {
t.Errorf("Expected %d, got %d", expected, result)
}
}
// Test with negative values
func TestDoMiniMaxNegativeValues(t *testing.T) {
values := []int{-1, -3, -2, -5, -4}
expectedMax := -1
expectedMin := -5
result := doMiniMax(true, values...)
if result != expectedMax {
t.Errorf("Expected max %d, got %d", expectedMax, result)
}
result = doMiniMax(false, values...)
if result != expectedMin {
t.Errorf("Expected min %d, got %d", expectedMin, result)
}
}
// Test with large data to trigger parallel execution
func TestDoMiniMaxLargeData(t *testing.T) {
size := minLoadPerGoroutine * 2
values := make([]int, size)
for i := 0; i < size; i++ {
values[i] = i
}
expectedMax := size - 1
expectedMin := 0
result := doMiniMax(true, values...)
if result != expectedMax {
t.Errorf("Expected max %d, got %d", expectedMax, result)
}
result = doMiniMax(false, values...)
if result != expectedMin {
t.Errorf("Expected min %d, got %d", expectedMin, result)
}
}
// Test when the number of elements isn't a multiple of the number of goroutines
func TestDoMiniMaxChunkBoundary(t *testing.T) {
numGoroutines := runtime.GOMAXPROCS(0)
size := numGoroutines*10 + 3 // Not a multiple of numGoroutines
values := make([]int, size)
for i := 0; i < size; i++ {
values[i] = i
}
expectedMax := size - 1
expectedMin := 0
result := doMiniMax(true, values...)
if result != expectedMax {
t.Errorf("Expected max %d, got %d", expectedMax, result)
}
result = doMiniMax(false, values...)
if result != expectedMin {
t.Errorf("Expected min %d, got %d", expectedMin, result)
}
}
// TestAbs tests the Abs function.
func TestAbs(t *testing.T) {
// Test positive integer.
n1 := 5
expected1 := 5
result1 := Abs(n1)
if result1 != expected1 {
t.Errorf("Abs of positive integer: expected %v, but got %v",
expected1, result1)
}
// Test negative integer.
n2 := -8
expected2 := 8
result2 := Abs(n2)
if result2 != expected2 {
t.Errorf("Abs of negative integer: expected %v, but got %v",
expected2, result2)
}
// Test positive floating-point number.
n3 := 3.14
expected3 := 3.14
result3 := Abs(n3)
if result3 != expected3 {
t.Errorf("Psitive floating-point number: expected %v, but got %v",
expected3, result3)
}
// Test negative floating-point number.
n4 := -2.718
expected4 := 2.718
result4 := Abs(n4)
if result4 != expected4 {
t.Errorf("Negative floating-point number: expected %v, but got %v",
expected4, result4)
}
// Test zero value.
n5 := 0
expected5 := 0
result5 := Abs(n5)
if result5 != expected5 {
t.Errorf("Abs of zero value: expected %v, but got %v",
expected5, result5)
}
}
func TestAbs_Generic(t *testing.T) {
// Test positive integer.
n1 := 5
expected1 := 5
result1 := Abs(n1)
if !reflect.DeepEqual(result1, expected1) {
t.Errorf("Abs of positive integer (G): expected %v, but got %v",
expected1, result1)
}
// Test negative integer.
n2 := -8
expected2 := 8
result2 := Abs(n2)
if !reflect.DeepEqual(result2, expected2) {
t.Errorf("Abs of negative integer (G): expected %v, but got %v",
expected2, result2)
}
// Test positive floating-point number.
n3 := 3.14
expected3 := 3.14
result3 := Abs(n3)
if !reflect.DeepEqual(result3, expected3) {
t.Errorf("Positive floating-point number (G): expected %v, but got %v",
expected3, result3)
}
// Test negative floating-point number.
n4 := -2.718
expected4 := 2.718
result4 := Abs(n4)
if !reflect.DeepEqual(result4, expected4) {
t.Errorf("Negative floating-point number (G): expected %v, but got %v",
expected4, result4)
}
// Test zero value.
n5 := 0
expected5 := 0
result5 := Abs(n5)
if !reflect.DeepEqual(result5, expected5) {
t.Errorf("Abs of zero value (G): expected %v, but got %v",
expected5, result5)
}
}
// TestAverage tests the Average function.
func TestAverage(t *testing.T) {
// Define test cases
tests := []struct {
name string
v []int
want float64
}{
{"Numbers including zero", []int{1, 0, 3}, 1.3333333333333333},
{"Non-zero numbers", []int{2, 2, 6}, 3.3333333333333335},
{"Numbers including negative", []int{-1, 0, 3}, 0.6666666666666666},
{"All zeros", []int{0, 0, 0}, 0},
{"Single element", []int{5}, 5},
}
// Iterate over each test case
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Check if output is as expected.
if got := Average(tt.v...); math.Abs(got-tt.want) > 1e-9 {
t.Errorf("Average() = %v, want %v", got, tt.want)
}
})
}
// Empty slice
if got := Average[int](); got != 0 {
t.Errorf("Average() = %v, want 0", got)
}
}
// TestMedian calculates the median of a list of integers.
func TestMedian(t *testing.T) {
// Test case 1: Odd number of values.
numbers1 := []int{3, 5, 7, 1, 9, 2}
median1 := Median(numbers1...)
expected1 := 4.0
if median1 != expected1 {
t.Errorf("Median of %v: expected %f, but got %f",
numbers1, expected1, median1)
}
// Test case 2: Even number of values.
numbers2 := []int{4, 6, 2, 8}
median2 := Median(numbers2...)
expected2 := 5.0
if median2 != expected2 {
t.Errorf("Median of %v: expected %f, but got %f",
numbers2, expected2, median2)
}
// Test case 3: Empty slice.
var numbers3 []int
median3 := Median(numbers3...)
expected3 := 0.0
if median3 != expected3 {
t.Errorf("Median of %v: expected %f, but got %f",
numbers3, expected3, median3)
}
// Test case with odd number of values
//valuesOdd := []int{3, 5, 7, 1, 9, 2}
//expectedOdd := 4.0
//resultOdd := Median(valuesOdd...)
//if resultOdd != expectedOdd {
// t.Errorf("Expected median: %f, but got: %f", expectedOdd, resultOdd)
//}
valuesOdd := []int{1, 3, 5, 7, 9}
expectedOdd := 5.0
resultOdd := Median(valuesOdd...)
if resultOdd != expectedOdd {
t.Errorf("Expected median: %f, but got: %f", expectedOdd, resultOdd)
}
// Test case with even number of values
valuesEven := []int{1, 2, 3, 4}
expectedEven := 2.5
resultEven := Median(valuesEven...)
if resultEven != expectedEven {
t.Errorf("Expected median: %f, but got: %f", expectedEven, resultEven)
}
// Test case with empty input
valuesEmpty := []int{}
expectedEmpty := 0.0
resultEmpty := Median(valuesEmpty...)
if resultEmpty != expectedEmpty {
t.Errorf("Expected median: %f, but got: %f", expectedEmpty, resultEmpty)
}
}
// TestMax tests the Max function.
func TestMax(t *testing.T) {
tests := []struct {
name string
v int
more []int
want int
}{
{
name: "All positive numbers",
more: []int{5, 7, 2, 4, 9},
want: 9,
},
{
name: "Includes negative numbers",
more: []int{0, -7, 2, -3, 9},
want: 9,
},
{
name: "Single number",
more: []int{1},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Max(tt.more...); got != tt.want {
t.Errorf("Max() = %v, want %v", got, tt.want)
}
})
}
// Empty slice.
// Return zero if no arguments are passed.
tmp := []int{}
if got := Max(tmp...); got != 0 {
t.Errorf("Max() = %v, want 0", got)
}
}
// TestMaxList tests the MaxList function.
func TestMaxList(t *testing.T) {
// Define test cases.
tests := []struct {
name string
list []int
defaults []int
want int
}{
{
name: "Non-empty list, no defaults",
list: []int{3, 5, 7, 1, 9, 2},
defaults: nil,
want: 9,
},
{
name: "Empty list, no defaults",
list: []int{},
defaults: nil,
want: 0,
},
{
name: "Empty list, with defaults",
list: []int{},
defaults: []int{20, 10},
want: 20,
},
{
name: "Non-empty list with defaults",
list: []int{3, 5, 7, 1, 9, 2},
defaults: []int{20, 10},
want: 9,
},
}
// Iterate over each test case.
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the MaxList function.
got := MaxList(tt.list, tt.defaults...)
// Check if the result matches the expected value.
if got != tt.want {
t.Errorf("MaxList() = %v, want %v", got, tt.want)
}
})
}
}
// TestMin tests the Min function.
func TestMin(t *testing.T) {
tests := []struct {
name string
more []int
want int
}{
{
name: "All positive numbers",
more: []int{5, 7, 2, 4, 9},
want: 2,
},
{
name: "Includes negative numbers",
more: []int{0, -7, 2, -3, 9},
want: -7,
},
{
name: "Single number",
more: []int{1},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Min(tt.more...); got != tt.want {
t.Errorf("Min() = %v, want %v", got, tt.want)
}
})
}
// Empty slice.
tmp := []int{}
if got := Min(tmp...); got != 0 {
t.Errorf("Min() = %v, want 0", got)
}
}
// TestMinList tests the MinList function.
func TestMinList(t *testing.T) {
// Define test cases.
tests := []struct {
name string
list []int
defaults []int
want int
}{
{
name: "Non-empty list, no defaults",
list: []int{3, 5, 7, 1, 9, 2},
defaults: nil,
want: 1,
},
{
name: "Empty list, no defaults",
list: []int{},
defaults: nil,
want: 0,
},
{
name: "Empty list, with defaults",
list: []int{},
defaults: []int{20, 10},
want: 10,
},
{
name: "Non-empty list with defaults",
list: []int{3, 5, 7, 1, 9, 2},
defaults: []int{20, 10},
want: 1,
},
}
// Iterate over each test case.
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the MinList function.
got := MinList(tt.list, tt.defaults...)
// Check if the result matches the expected value.
if got != tt.want {
t.Errorf("MinList() = %v, want %v", got, tt.want)
}
})
}
}
func TestSafeSum(t *testing.T) {
// Test integer types
valuesInt := []int{1, 2, 3, 4}
sumInt, errInt := SafeSum(valuesInt...)
if errInt != nil || sumInt != 10 {
t.Errorf("SafeSum for int failed, got: %d, error: %v, want: 10", sumInt, errInt)
}
valuesInt8 := []int8{1, 2, 3, 4}
sumInt8, errInt8 := SafeSum(valuesInt8...)
if errInt8 != nil || sumInt8 != 10 {
t.Errorf("SafeSum for int8 failed, got: %d, error: %v, want: 10", sumInt8, errInt8)
}
valuesInt16 := []int16{1, 2, 3, 4}
sumInt16, errInt16 := SafeSum(valuesInt16...)
if errInt16 != nil || sumInt16 != 10 {
t.Errorf("SafeSum for int16 failed, got: %d, error: %v, want: 10", sumInt16, errInt16)
}
valuesInt32 := []int32{1, 2, 3, 4}
sumInt32, errInt32 := SafeSum(valuesInt32...)
if errInt32 != nil || sumInt32 != 10 {
t.Errorf("SafeSum for int32 failed, got: %d, error: %v, want: 10", sumInt32, errInt32)
}
valuesInt64 := []int64{1, 2, 3, 4}
sumInt64, errInt64 := SafeSum(valuesInt64...)
if errInt64 != nil || sumInt64 != 10 {
t.Errorf("SafeSum for int64 failed, got: %d, error: %v, want: 10", sumInt64, errInt64)
}
// Test unsigned integer types
valuesUint := []uint{1, 2, 3, 4}
sumUint, errUint := SafeSum(valuesUint...)
if errUint != nil || sumUint != 10 {
t.Errorf("SafeSum for uint failed, got: %d, error: %v, want: 10", sumUint, errUint)
}
valuesUint8 := []uint8{1, 2, 3, 4}
sumUint8, errUint8 := SafeSum(valuesUint8...)
if errUint8 != nil || sumUint8 != 10 {
t.Errorf("SafeSum for uint8 failed, got: %d, error: %v, want: 10", sumUint8, errUint8)
}
valuesUint16 := []uint16{1, 2, 3, 4}
sumUint16, errUint16 := SafeSum(valuesUint16...)
if errUint16 != nil || sumUint16 != 10 {
t.Errorf("SafeSum for uint16 failed, got: %d, error: %v, want: 10", sumUint16, errUint16)
}
valuesUint32 := []uint32{1, 2, 3, 4}
sumUint32, errUint32 := SafeSum(valuesUint32...)
if errUint32 != nil || sumUint32 != 10 {
t.Errorf("SafeSum for uint32 failed, got: %d, error: %v, want: 10", sumUint32, errUint32)
}
valuesUint64 := []uint64{1, 2, 3, 4}
sumUint64, errUint64 := SafeSum(valuesUint64...)
if errUint64 != nil || sumUint64 != 10 {
t.Errorf("SafeSum for uint64 failed, got: %d, error: %v, want: 10", sumUint64, errUint64)
}
// Test floating point types
valuesFloat32 := []float32{1.5, 2.5, 3.5}
sumFloat32, errFloat32 := SafeSum(valuesFloat32...)
if errFloat32 != nil || sumFloat32 != 7.5 {
t.Errorf("SafeSum for float32 failed, got: %f, error: %v, want: 7.5", sumFloat32, errFloat32)
}
valuesFloat64 := []float64{1.5, 2.5, 3.5}
sumFloat64, errFloat64 := SafeSum(valuesFloat64...)
if errFloat64 != nil || sumFloat64 != 7.5 {
t.Errorf("SafeSum for float64 failed, got: %f, error: %v, want: 7.5", sumFloat64, errFloat64)
}
// Test empty slice
emptySum, emptyErr := SafeSum[int]()
if emptyErr != nil || emptySum != 0 {
t.Errorf("SafeSum for empty slice failed, got: %d, error: %v, want: 0", emptySum, emptyErr)
}
}
func TestSafeSumOverflow(t *testing.T) {
// Test integer types overflow
valuesInt := []int{math.MaxInt, 1}
sumInt, errInt := SafeSum(valuesInt...)
if errInt == nil {
t.Errorf("Expected overflow error for int, got sum: %d", sumInt)
}
valuesInt8 := []int8{math.MaxInt8, 1}
sumInt8, errInt8 := SafeSum(valuesInt8...)
if errInt8 == nil {
t.Errorf("Expected overflow error for int8, got sum: %d", sumInt8)
}
valuesInt16 := []int16{math.MaxInt16, 1}
sumInt16, errInt16 := SafeSum(valuesInt16...)
if errInt16 == nil {
t.Errorf("Expected overflow error for int16, got sum: %d", sumInt16)
}
valuesInt32 := []int32{math.MaxInt32, 1}
sumInt32, errInt32 := SafeSum(valuesInt32...)
if errInt32 == nil {
t.Errorf("Expected overflow error for int32, got sum: %d", sumInt32)
}
valuesInt64 := []int64{math.MaxInt64, 1}
sumInt64, errInt64 := SafeSum(valuesInt64...)
if errInt64 == nil {
t.Errorf("Expected overflow error for int64, got sum: %d", sumInt64)
}
// Test unsigned integer types overflow
valuesUint := []uint{math.MaxUint, 1}
sumUint, errUint := SafeSum(valuesUint...)
if errUint == nil {
t.Errorf("Expected overflow error for uint, got sum: %d", sumUint)
}
valuesUint8 := []uint8{math.MaxUint8, 1}
sumUint8, errUint8 := SafeSum(valuesUint8...)
if errUint8 == nil {
t.Errorf("Expected overflow error for uint8, got sum: %d", sumUint8)
}
valuesUint16 := []uint16{math.MaxUint16, 1}
sumUint16, errUint16 := SafeSum(valuesUint16...)
if errUint16 == nil {
t.Errorf("Expected overflow error for uint16, got sum: %d", sumUint16)
}
valuesUint32 := []uint32{math.MaxUint32, 1}
sumUint32, errUint32 := SafeSum(valuesUint32...)
if errUint32 == nil {
t.Errorf("Expected overflow error for uint32, got sum: %d", sumUint32)
}
valuesUint64 := []uint64{math.MaxUint64, 1}
sumUint64, errUint64 := SafeSum(valuesUint64...)
if errUint64 == nil {
t.Errorf("Expected overflow error for uint64, got sum: %d", sumUint64)
}
// Test floating point types overflow
valuesFloat32 := []float32{float32(math.Inf(1)), 1}
sumFloat32, errFloat32 := SafeSum(valuesFloat32...)
if errFloat32 == nil {
t.Errorf("Expected overflow error for float32, got sum: %f", sumFloat32)
}
valuesFloat64 := []float64{math.Inf(1), 1}
sumFloat64, errFloat64 := SafeSum(valuesFloat64...)
if errFloat64 == nil {
t.Errorf("Expected overflow error for float64, got sum: %f", sumFloat64)
}
}
func TestSum(t *testing.T) {
values := []int{1, 2, 3, 4, 5}
result := Sum(values...)
if result != 15 {
t.Errorf("Sum failed, got: %d, want: 15", result)
}
emptySum := Sum[int]()
if emptySum != 0 {
t.Errorf("Sum for empty slice failed, got: %d, want: 0", emptySum)
}
}
func TestSumOverflow(t *testing.T) {
values := []int{math.MaxInt, 1}
result := Sum(values...)
if result != 0 {
t.Errorf("Sum overflow failed, got: %d, want: 0", result)
}
}
// TestIsEven tests the IsEven function.
func TestIsEven(t *testing.T) {
// Test cases for integer values.
// Expected results: true if the value is even, false otherwise.
tests := []struct {
value float64
expected bool
}{
{4, true},
{3, false},
{-6, true},
{-5, false},
}
for _, test := range tests {
result := IsEven(test.value)
if result != test.expected {
t.Errorf("IsEven(%v): expected %v, but got %v",
test.value, test.expected, result)
}
}
// Test cases for floating-point values.
// Expected results: true if the integer part is even, false otherwise.
testsFloat := []struct {
value float64
expected bool
f []bool
}{
{4.2, false, []bool{}},
{4.2, true, []bool{true}},
{3.8, false, []bool{true, false}},
{-5.5, false, []bool{}},
{-6.5, true, []bool{true}},
}
for _, test := range testsFloat {
result := IsEven(test.value, test.f...)
if result != test.expected {
t.Errorf("IsEven(%v): expected %v, but got %v",
test.value, test.expected, result)
}
}
}
// TestIsOdd tests the IsOdd function.
func TestIsOdd(t *testing.T) {
// Test cases for integer values.
// Expected results: true if the value is odd, false otherwise.
tests := []struct {
value int
expected bool
}{
{4, false}, // even number
{3, true}, // odd number
{-6, false}, // negative even number
{-5, true}, // negative odd number
{-10, false}, // negative even number
}
for _, test := range tests {
result := IsOdd(test.value)
if result != test.expected {
t.Errorf("IsOdd(%v): expected %v, but got %v",
test.value, test.expected, result)
}
}
// Test cases for floating-point values.
// Expected results: true if the integer part is odd, false otherwise.
testsFloat := []struct {
value float64
expected bool
f []bool
}{
{3.2, false, []bool{}},
{3.2, true, []bool{true}},
{3.0, true, []bool{}},
{-5.5, true, []bool{true}},
{-6.5, false, []bool{true, false}},
{-11.0, true, []bool{false}},
}
for _, test := range testsFloat {
result := IsOdd(test.value, test.f...)
if result != test.expected {
t.Errorf("IsOdd(%v): expected %v, but got %v",
test.value, test.expected, result)
}
}
}
// TestIsWhole tests the IsWhole function.
func TestIsWhole(t *testing.T) {
// Test cases for integer values
// Expected results: true if the value has no fractional part,
// false otherwise
tests := []struct {
value int
expected bool
}{
{4, true},
{3, true},
{-6, true},
{-5, true},
{-10, true},
{0, true},
{100, true},
{5, true},
{5, true},
{-2, true},
}
for _, test := range tests {
result := IsWhole(test.value)
if result != test.expected {
t.Errorf("IsWhole(%v): expected %v, but got %v",
test.value, test.expected, result)
}
}
// Test cases for floating-point values
// Expected results: true if the value has no fractional part,
// false otherwise
testsFloat := []struct {
value float64
expected bool
}{
{4.2, false},
{3.8, false},
{-5.5, false},
{-11.0, true},
{0.0, true},
{100.0, true},
{5.0, true},
{-2.0, true},
{-2.1, false},
}
for _, test := range testsFloat {
result := IsWhole(test.value)
if result != test.expected {
t.Errorf("IsWhole(%v): expected %v, but got %v",
test.value, test.expected, result)
}
}
}
// TestRandom tests the Random function.
func TestRandom(t *testing.T) {
// No arguments.
result := Random[int]()
if result != 0 {
t.Errorf("Expected 0, got %v", result)
}
// One argument.
result = Random[int](5)
if result < 0 || result >= 5 {
t.Errorf("Expected a random int between 0 and 4, got %v", result)
}
// Two arguments.
result = Random[int](1, 5)
if result < 1 || result >= 5 {
t.Errorf("Expected a random int between 1 and 4, got %v", result)
}
// Two equal arguments.
result = Random[int](5, 5)
if result != 5 {
t.Errorf("Expected 5, got %v", result)
}
// Two arguments when max < min.
result = Random[int](15, 5)
if result < 5 || result >= 15 {
t.Errorf("Expected a random int between 5 and 14, got %v", result)
}
// Multiple arguments.
result = Random[int](1, 2, 3)
if result != 1 && result != 2 && result != 3 {
t.Errorf("Expected 1, 2, or 3, got %v", result)
}
// Additional test cases for different types.
// One argument of type float32.
resultFloat32 := Random[float32](10.5)
if resultFloat32 < 0.0 || resultFloat32 >= 10.5 {
t.Errorf("Expected a random float32 between 0.0 and 10.4, got %v",
resultFloat32)
}
// Two arguments of type float64.
resultFloat64 := Random[float64](1.5, 5.5)
if resultFloat64 < 1.5 || resultFloat64 >= 5.5 {
t.Errorf("Expected a random float64 between 1.5 and 5.4, got %v",
resultFloat64)
}
}
// Helper function to check if a list containsListValue a given value.
func containsListValue[T comparable](list []T, value T) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}
// Helper function to check if a map contains a given value.
func containsMapValue[K comparable, T comparable](m map[K]T, value T) bool {
for _, v := range m {
if v == value {
return true
}
}
return false
}
// TestRandomList tests the RandomList function.
func TestRandomList(t *testing.T) {
// Test case 1: Non-empty list
list := []int{1, 2, 3, 4, 5}
value := RandomList(list)
if !containsListValue(list, value) {
t.Errorf("Expected a random element from the list, got %v", value)
}
// Test case 2: Empty list
emptyList := []string{}
zeroValue := RandomList(emptyList)
if zeroValue != "" {
t.Errorf("Expected the zero value of string type, got %v", zeroValue)
}
}
// TestRandomMap tests the RandomMap function.
func TestRandomMap(t *testing.T) {
// Test case 1: Non-empty map
myMap := map[string]int{
"apple": 1,
"banana": 2,
"cherry": 3,
}
value := RandomMap(myMap)
if !containsMapValue(myMap, value) {
t.Errorf("Expected a random value from the map, got %v", value)
}
// Test case 2: Empty map
emptyMap := map[string]bool{}
zeroValue := RandomMap(emptyMap)
if zeroValue != reflect.Zero(reflect.TypeOf((*bool)(nil)).Elem()).Interface().(bool) {
t.Errorf("Expected the zero value of bool type (false), got %v", zeroValue)
}
}
// TestRandomListPlural tests the RandomListPlural function.
func TestRandomListPlural(t *testing.T) {
// Test case 1: Non-empty list, n > 0
list := []int{1, 2, 3, 4, 5}
values := RandomListPlural(3, list)
if len(values) != 3 {
t.Errorf("Expected a slice of 3 random elements, got %v", values)
}
for _, value := range values {
if !containsListValue(list, value) {
t.Errorf("Expected a random element from the list, got %v", value)
}
}
// Test case 2: Empty list, n > 0
emptyList := []string{}
emptyValues := RandomListPlural(2, emptyList)
if len(emptyValues) != 0 {
t.Errorf("Expected an empty slice, got %v", emptyValues)
}
// Test case 3: Non-empty list, n <= 0
valuesZero := RandomListPlural(0, list)
if len(valuesZero) != 0 {
t.Errorf("Expected an empty slice, got %v", valuesZero)
}
}
// TestRandomMapPlural tests the RandomMapPlural function.
func TestRandomMapPlural(t *testing.T) {
// Test case 1: Non-empty map, n > 0
myMap := map[string]int{
"apple": 1,
"banana": 2,
"cherry": 3,
}
values := RandomMapPlural(2, myMap)
if len(values) != 2 {
t.Errorf("Expected a slice of 2 random values, got %v", values)
}
for _, value := range values {
if !containsMapValue(myMap, value) {
t.Errorf("Expected a random value from the map, got %v", value)
}
}
// Test case 2: Empty map, n > 0
emptyMap := map[string]bool{}
emptyValues := RandomMapPlural(3, emptyMap)
if len(emptyValues) != 0 {
t.Errorf("Expected an empty slice, got %v", emptyValues)
}