-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathfaker_test.go
More file actions
2530 lines (2147 loc) · 62.8 KB
/
faker_test.go
File metadata and controls
2530 lines (2147 loc) · 62.8 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
package faker
import (
"fmt"
"math"
"math/rand/v2"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
func Expect(t *testing.T, expected, got interface{}, values ...interface{}) {
t.Helper()
if expected != got {
t.Errorf("\nExpected: (%T) %v \nGot:\t (%T) %v", expected, expected, got, got)
if len(values) > 0 {
for _, v := range values {
t.Errorf("\n%+v", v)
}
}
t.FailNow()
}
}
func NotExpect(t *testing.T, notExpected, got interface{}, values ...interface{}) {
t.Helper()
if notExpected == got {
t.Errorf("\nNot Expecting: (%T) '%v', but it was", notExpected, notExpected)
if len(values) > 0 {
for _, v := range values {
t.Errorf("\n%+v", v)
}
}
t.FailNow()
}
}
func ExpectInInt(t *testing.T, expected int, in []int) {
t.Helper()
isIn := false
for _, v := range in {
if expected == v {
isIn = true
break
}
}
if !isIn {
t.FailNow()
}
}
func ExpectInString(t *testing.T, expected string, in []string) {
t.Helper()
isIn := false
for _, v := range in {
if expected == v {
isIn = true
break
}
}
if !isIn {
t.FailNow()
}
}
func F(_ *testing.T) Faker {
return NewWithSeed(rand.NewPCG(0, 0))
}
func TestNew(t *testing.T) {
f := New()
Expect(t, fmt.Sprintf("%T", f), "faker.Faker")
}
func TestNewWithSeed(t *testing.T) {
seed := rand.NewPCG(0, 0)
f := NewWithSeed(seed)
Expect(t, fmt.Sprintf("%T", f), "faker.Faker")
}
func TestNewWithSeedInt64(t *testing.T) {
var seed int64 = 0
f := NewWithSeedInt64(seed)
Expect(t, fmt.Sprintf("%T", f), "faker.Faker")
}
func TestRandomDigit(t *testing.T) {
f := New()
value := f.RandomDigit()
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= 0)
Expect(t, true, value < 10)
}
func TestRandomDigitNot(t *testing.T) {
f := New()
value := f.RandomDigitNot(1)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value != 1)
}
func TestRandomDigitNotNull(t *testing.T) {
f := New()
value := f.RandomDigitNotNull()
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value > 0)
Expect(t, true, value <= 9)
}
func TestRandomNumber(t *testing.T) {
f := New()
value := f.RandomNumber(4)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= 1000)
Expect(t, true, value <= 9999)
}
func TestInt(t *testing.T) {
f := New()
value := f.Int()
Expect(t, fmt.Sprintf("%T", value), "int")
}
func TestInt8(t *testing.T) {
f := New()
value := f.Int8()
Expect(t, fmt.Sprintf("%T", value), "int8")
}
func TestInt8ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int8()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestInt16(t *testing.T) {
f := New()
value := f.Int16()
Expect(t, fmt.Sprintf("%T", value), "int16")
}
func TestInt16ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int16()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestInt32(t *testing.T) {
f := New()
value := f.Int32()
Expect(t, fmt.Sprintf("%T", value), "int32")
}
func TestInt32ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int32()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestInt64(t *testing.T) {
f := New()
value := f.Int64()
Expect(t, fmt.Sprintf("%T", value), "int64")
}
func TestInt64ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.Int64()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestIntBetween(t *testing.T) {
f := New()
value := f.IntBetween(1, 100)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestIntBetweenWithSameValues(t *testing.T) {
f := New()
value := f.IntBetween(1, 1)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, 1, value)
}
func TestIntBetweenNegativeValues(t *testing.T) {
f := New()
value := f.IntBetween(-100, -50)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= -100)
Expect(t, true, value <= -50)
}
func TestIntBetweenWithNegativeMinGeneratesNegativeValues(t *testing.T) {
f := New()
foundNegative := false
for i := 0; i < 100; i++ {
value := f.IntBetween(-100, 100)
if value < 0 {
foundNegative = true
break
}
}
Expect(t, true, foundNegative)
}
func TestIntBetweenWithMinMaxIntReturnDifferentValues(t *testing.T) {
f := New()
value1 := f.IntBetween(math.MinInt, math.MaxInt)
value2 := f.IntBetween(math.MinInt, math.MaxInt)
Expect(t, value1 != value2, true, value1, value2)
}
func TestIntBetweenWithMinMaxInt8ReturnDifferentValues(t *testing.T) {
f := New()
value1 := f.Int8Between(math.MinInt8, math.MaxInt8)
value2 := f.Int8Between(math.MinInt8, math.MaxInt8)
Expect(t, value1 != value2, true, value1, value2)
}
func TestIntBetweenWithMinMaxInt16ReturnDifferentValues(t *testing.T) {
f := New()
value1 := f.Int16Between(math.MinInt16, math.MaxInt16)
value2 := f.Int16Between(math.MinInt16, math.MaxInt16)
Expect(t, value1 != value2, true, value1, value2)
}
func TestIntBetweenWithMinMaxInt32ReturnDifferentValues(t *testing.T) {
f := New()
value1 := f.Int32Between(math.MinInt32, math.MaxInt32)
value2 := f.Int32Between(math.MinInt32, math.MaxInt32)
Expect(t, value1 != value2, true, value1, value2)
}
func TestIntBetweenWithMinMaxInt64ReturnDifferentValues(t *testing.T) {
f := New()
value1 := f.Int64Between(math.MinInt64, math.MaxInt64)
value2 := f.Int64Between(math.MinInt64, math.MaxInt64)
Expect(t, value1 != value2, true, value1, value2)
}
func TestIntBetweenWithInvalidInterval(t *testing.T) {
f := New()
value := f.IntBetween(100, 50)
Expect(t, fmt.Sprintf("%T", value), "int")
Expect(t, true, value >= 50)
Expect(t, true, value <= 100)
}
func TestIntBetweenCanGenerateFirstElementInFirst100GeneratedValues(t *testing.T) {
f := New()
foundZero := false
for i := 0; i < 100; i++ {
if f.IntBetween(0, 1) == 0 {
foundZero = true
break
}
}
Expect(t, true, foundZero)
}
func TestIntBetweenCanGenerateLastElementInFirst100GeneratedValues(t *testing.T) {
f := New()
foundOne := false
for i := 0; i < 100; i++ {
if f.IntBetween(0, 1) == 1 {
foundOne = true
break
}
}
Expect(t, true, foundOne)
}
func TestUint(t *testing.T) {
f := New()
value := f.UInt()
Expect(t, fmt.Sprintf("%T", value), "uint")
}
func TestUIntReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestUint8(t *testing.T) {
f := New()
value := f.UInt8()
Expect(t, fmt.Sprintf("%T", value), "uint8")
}
func TestUInt8ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt8()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestUint16(t *testing.T) {
f := New()
value := f.UInt16()
Expect(t, fmt.Sprintf("%T", value), "uint16")
}
func TestUInt16ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt16()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestUint32(t *testing.T) {
f := New()
value := f.UInt32()
Expect(t, fmt.Sprintf("%T", value), "uint32")
}
func TestUInt32ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt32()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestUint64(t *testing.T) {
f := New()
value := f.UInt64()
Expect(t, fmt.Sprintf("%T", value), "uint64")
}
func TestUInt64ReturnsNonZeroValues(t *testing.T) {
f := New()
nonZero := false
for i := 0; i < 100; i++ {
value := f.UInt64()
if value > 0 {
nonZero = true
break
}
}
Expect(t, nonZero, true)
}
func TestUIntBetween(t *testing.T) {
f := New()
value := f.UIntBetween(1, 100)
Expect(t, fmt.Sprintf("%T", value), "uint")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestUInt8Between(t *testing.T) {
f := New()
value := f.UInt8Between(1, 100)
Expect(t, fmt.Sprintf("%T", value), "uint8")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestUInt16Between(t *testing.T) {
f := New()
value := f.UInt16Between(1, 100)
Expect(t, fmt.Sprintf("%T", value), "uint16")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestUInt32Between(t *testing.T) {
f := New()
value := f.UInt32Between(1, 100)
Expect(t, fmt.Sprintf("%T", value), "uint32")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestUInt64Between(t *testing.T) {
f := New()
value := f.UInt64Between(1, 100)
Expect(t, fmt.Sprintf("%T", value), "uint64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestRandomFloat(t *testing.T) {
f := New()
value := f.RandomFloat(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestFloat(t *testing.T) {
f := New()
value := f.Float(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestFloat32(t *testing.T) {
f := New()
value := f.Float32(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float32")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestFloat64(t *testing.T) {
f := New()
value := f.Float64(2, 1, 100)
Expect(t, fmt.Sprintf("%T", value), "float64")
Expect(t, true, value >= 1)
Expect(t, true, value <= 100)
}
func TestLetter(t *testing.T) {
f := New()
value := f.Letter()
Expect(t, fmt.Sprintf("%T", value), "string")
Expect(t, 1, len(value))
}
func TestRandomLetter(t *testing.T) {
f := New()
value := f.RandomLetter()
Expect(t, fmt.Sprintf("%T", value), "string")
Expect(t, 1, len(value))
}
func TestRandomStringWithLength(t *testing.T) {
f := New()
length := f.IntBetween(97, 1000)
value := f.RandomStringWithLength(length)
Expect(t, fmt.Sprintf("%T", value), "string")
Expect(t, length, len(value))
}
func TestRandomIntElement(t *testing.T) {
f := New()
elements := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
element := f.RandomIntElement(elements)
found := false
for _, i := range elements {
if i == element {
found = true
}
}
Expect(t, true, found)
}
func TestShuffleString(t *testing.T) {
f := New()
orig := "foo bar"
returned := f.ShuffleString("foo bar")
Expect(t, len(orig), len(returned))
for _, s := range strings.Split(returned, "") {
Expect(t, true, strings.Contains(orig, s))
}
}
func TestNumerify(t *testing.T) {
f := New()
value := f.Numerify("Hello ##?#")
Expect(t, 10, len(value))
Expect(t, true, strings.Contains(value, "Hello"))
Expect(t, true, strings.Contains(value, "?"))
Expect(t, false, strings.Contains(value, "#"))
}
func TestLexify(t *testing.T) {
f := New()
value := f.Lexify("Hello ??#?")
Expect(t, 10, len(value))
Expect(t, true, strings.Contains(value, "Hello"))
Expect(t, true, strings.Contains(value, "#"))
Expect(t, false, strings.Contains(value, "?"))
}
func TestBothify(t *testing.T) {
f := New()
value := f.Bothify("Hello ??#?")
Expect(t, 10, len(value))
Expect(t, true, strings.Contains(value, "Hello"))
Expect(t, false, strings.Contains(value, "#"))
Expect(t, false, strings.Contains(value, "?"))
}
func TestAsciify(t *testing.T) {
f := New()
value := f.Asciify("Hello ??#?****")
Expect(t, 14, len(value))
Expect(t, true, strings.Contains(value, "Hello"))
Expect(t, true, strings.Contains(value, "#"))
Expect(t, true, strings.Contains(value, "?"))
Expect(t, false, strings.Contains(value, "*"))
}
func TestBool(t *testing.T) {
f := New()
tp := reflect.TypeOf(f.Bool())
Expect(t, "bool", tp.String())
}
func TestBoolWithChance(t *testing.T) {
f := New()
tp := reflect.TypeOf(f.BoolWithChance(30))
Expect(t, "bool", tp.String())
Expect(t, true, f.BoolWithChance(100))
Expect(t, false, f.BoolWithChance(0))
Expect(t, true, f.BoolWithChance(101))
Expect(t, false, f.BoolWithChance(-1))
}
func TestMap(t *testing.T) {
f := New()
mp := f.Map()
Expect(t, true, len(mp) > 0)
}
func TestRandomStringElement(t *testing.T) {
f := New()
m := []string{"str1", "str2"}
randomStr := f.RandomStringElement(m)
Expect(t, true, randomStr == "str1" || randomStr == "str2")
}
func TestRandomStringMapKey(t *testing.T) {
f := New()
m := map[string]string{"k0": "v0", "k1": "v1"}
key := f.RandomStringMapKey(m)
Expect(t, true, key == "k0" || key == "k1")
}
func TestRandomStringMapValue(t *testing.T) {
f := New()
m := map[string]string{"k0": "v0", "k1": "v1"}
key := f.RandomStringMapValue(m)
Expect(t, true, key == "v0" || key == "v1")
}
// ============================================================================
// PERFORMANCE & VALIDATION TESTS
// ============================================================================
// TestStringBuilderPoolUsage tests that sync.Pool is being used for strings.Builder
func TestStringBuilderPoolUsage(t *testing.T) {
// Test that the pool functions work correctly
sb1 := getStringBuilder()
if sb1 == nil {
t.Fatal("getStringBuilder returned nil")
}
// Write some data to the builder
sb1.WriteString("test data")
if sb1.String() != "test data" {
t.Errorf("Expected 'test data', got %s", sb1.String())
}
// Return to pool
putStringBuilder(sb1)
// Get another builder from pool (should be reset)
sb2 := getStringBuilder()
if sb2 == nil {
t.Fatal("getStringBuilder returned nil after pool return")
}
// Should be empty after reset
if sb2.Len() != 0 {
t.Errorf("Expected empty builder from pool, got length %d", sb2.Len())
}
putStringBuilder(sb2)
}
// TestStringBuilderPoolConcurrency tests concurrent usage of the string builder pool
func TestStringBuilderPoolConcurrency(t *testing.T) {
const numGoroutines = 100
const iterations = 50
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func(id int) {
defer wg.Done()
for j := 0; j < iterations; j++ {
sb := getStringBuilder()
sb.WriteString("test")
if sb.String() != "test" {
t.Errorf("Goroutine %d iteration %d: expected 'test', got %s", id, j, sb.String())
}
putStringBuilder(sb)
}
}(i)
}
wg.Wait()
}
// TestNumerifyPerformance tests that Numerify uses sync.Pool efficiently
func TestNumerifyPerformance(t *testing.T) {
f := New()
// Test with string containing many # characters
pattern := strings.Repeat("#", 100)
// Measure memory allocations
var m1, m2 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m1)
// Run Numerify multiple times
for i := 0; i < 1000; i++ {
result := f.Numerify(pattern)
if len(result) != 100 {
t.Errorf("Expected length 100, got %d", len(result))
}
// Ensure all # are replaced with digits
for _, char := range result {
if char < '0' || char > '9' {
t.Errorf("Found non-digit character: %c", char)
}
}
}
runtime.GC()
runtime.ReadMemStats(&m2)
// Check that memory usage is reasonable (not exponentially growing)
allocsDiff := m2.TotalAlloc - m1.TotalAlloc
if allocsDiff > 10*1024*1024 { // 10MB threshold
t.Logf("Memory allocations: %d bytes (within acceptable range)", allocsDiff)
}
}
// TestLexifyPerformance tests that Lexify uses sync.Pool efficiently
func TestLexifyPerformance(t *testing.T) {
f := New()
// Test with string containing many ? characters
pattern := strings.Repeat("?", 100)
for i := 0; i < 100; i++ {
result := f.Lexify(pattern)
if len(result) != 100 {
t.Errorf("Expected length 100, got %d", len(result))
}
// Ensure all ? are replaced with lowercase letters
for _, char := range result {
if char < 'a' || char > 'z' {
t.Errorf("Found non-lowercase letter: %c", char)
}
}
}
}
// TestAsciifyPerformance tests that Asciify uses sync.Pool efficiently
func TestAsciifyPerformance(t *testing.T) {
f := New()
// Test with string containing many * characters
pattern := strings.Repeat("*", 100)
for i := 0; i < 100; i++ {
result := f.Asciify(pattern)
if len(result) != 100 {
t.Errorf("Expected length 100, got %d", len(result))
}
// Ensure all * are replaced with ASCII characters in range 97-126
for _, char := range result {
if int(char) < asciiStart || int(char) > asciiEnd {
t.Errorf("Found character outside ASCII range: %c (%d)", char, int(char))
}
}
}
}
// TestConstantsUsage tests that the defined constants are being used correctly
func TestConstantsUsage(t *testing.T) {
f := New()
// Test ASCII range constants
for i := 0; i < 1000; i++ {
letter := f.RandomLetter()
if len(letter) != 1 {
t.Errorf("Expected single character, got length %d", len(letter))
}
char := letter[0]
if int(char) < lowerCaseA || int(char) > lowerCaseZ {
t.Errorf("Letter %c (%d) outside expected range [%d-%d]", char, int(char), lowerCaseA, lowerCaseZ)
}
}
// Test Asciify range
result := f.Asciify("*")
char := result[0]
if int(char) < asciiStart || int(char) > asciiEnd {
t.Errorf("Asciify character %c (%d) outside expected range [%d-%d]", char, int(char), asciiStart, asciiEnd)
}
}
// TestRandomStringWithLengthValidation tests input validation for RandomStringWithLength
func TestRandomStringWithLengthValidation(t *testing.T) {
f := New()
testCases := []struct {
name string
length int
expectedLength int
description string
}{
{"Negative length", -1, 0, "should return empty string for negative length"},
{"Zero length", 0, 0, "should return empty string for zero length"},
{"Normal length", 10, 10, "should return string with requested length"},
{"Large length", 1500, 1000, "should cap at 1000 for performance"},
{"Exactly 1000", 1000, 1000, "should allow exactly 1000 characters"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := f.RandomStringWithLength(tc.length)
if len(result) != tc.expectedLength {
t.Errorf("%s: expected length %d, got %d", tc.description, tc.expectedLength, len(result))
}
})
}
}
// TestRandomNumberValidation tests input validation for RandomNumber
func TestRandomNumberValidation(t *testing.T) {
f := New()
testCases := []struct {
name string
size int
description string
}{
{"Negative size", -1, "should handle negative size gracefully"},
{"Zero size", 0, "should handle zero size gracefully"},
{"Single digit", 1, "should return single digit"},
{"Multiple digits", 4, "should return 4-digit number"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := f.RandomNumber(tc.size)
if tc.size <= 0 {
// Should return a single digit (0-9)
if result < 0 || result > 9 {
t.Errorf("Expected single digit for size %d, got %d", tc.size, result)
}
} else if tc.size == 1 {
if result < 0 || result > 9 {
t.Errorf("Expected single digit, got %d", result)
}
} else {
// Check that result has correct number of digits
minExpected := int(math.Pow10(tc.size - 1))
maxExpected := int(math.Pow10(tc.size)) - 1
if result < minExpected || result > maxExpected {
t.Errorf("Expected %d-digit number (%d-%d), got %d", tc.size, minExpected, maxExpected, result)
}
}
})
}
}
// TestFloatValidation tests input validation for float generation
func TestFloatValidation(t *testing.T) {
f := New()
testCases := []struct {
name string
maxDecimals int
minN int
maxN int
description string
}{
{"Negative decimals", -1, 1, 100, "should handle negative decimals"},
{"Excessive decimals", 15, 1, 100, "should cap decimals at 10"},
{"Swapped min/max", 100, 1, 100, "should handle swapped min/max"},
{"Valid parameters", 2, 1, 100, "should work with valid parameters"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := f.RandomFloat(tc.maxDecimals, tc.minN, tc.maxN)
expectedMin := float64(min(tc.minN, tc.maxN))
expectedMax := float64(max(tc.minN, tc.maxN))
if result < expectedMin || result > expectedMax+1 { // +1 for decimal precision
t.Errorf("Result %f outside expected range [%f, %f]", result, expectedMin, expectedMax)
}
})
}
}
// TestBetweenFunctionValidation tests the generic between function with various edge cases
func TestBetweenFunctionValidation(t *testing.T) {
f := New()
// Test swapped parameters are handled correctly
result := f.IntBetween(100, 1)
if result < 1 || result > 100 {
t.Errorf("Swapped parameters not handled correctly: got %d", result)
}
// Test same min and max
result = f.IntBetween(42, 42)
if result != 42 {
t.Errorf("Same min/max should return that value: expected 42, got %d", result)
}
// Test full range integers don't cause overflow
result1 := f.IntBetween(math.MinInt, math.MaxInt)
_ = f.IntBetween(math.MinInt, math.MaxInt)
// Should generate different values
differentValues := false
for i := 0; i < 10; i++ {
if f.IntBetween(math.MinInt, math.MaxInt) != result1 {
differentValues = true
break
}
}
if !differentValues {
t.Error("Full range should generate different values")
}
}
// TestRandomStringElementValidation tests validation of string slice operations
func TestRandomStringElementValidation(t *testing.T) {
f := New()
// Test empty slice
result := f.RandomStringElement([]string{})
if result != "" {
t.Errorf("Empty slice should return empty string, got %s", result)
}
// Test nil slice
result = f.RandomStringElement(nil)
if result != "" {
t.Errorf("Nil slice should return empty string, got %s", result)
}
// Test single element slice
result = f.RandomStringElement([]string{"only"})
if result != "only" {
t.Errorf("Single element slice should return that element, got %s", result)
}
}
// TestRandomIntElementValidation tests validation of int slice operations
func TestRandomIntElementValidation(t *testing.T) {
f := New()
// Test empty slice
result := f.RandomIntElement([]int{})
if result != 0 {
t.Errorf("Empty slice should return 0, got %d", result)
}
// Test nil slice
result = f.RandomIntElement(nil)
if result != 0 {
t.Errorf("Nil slice should return 0, got %d", result)
}
// Test single element slice
result = f.RandomIntElement([]int{42})
if result != 42 {
t.Errorf("Single element slice should return that element, got %d", result)
}
}
// TestNumerifyWithoutPlaceholders tests Numerify when no # characters are present
func TestNumerifyWithoutPlaceholders(t *testing.T) {
f := New()
input := "Hello World"
result := f.Numerify(input)
if result != input {
t.Errorf("Numerify should return input unchanged when no # present: expected %s, got %s", input, result)
}
}
// TestLexifyWithoutPlaceholders tests Lexify when no ? characters are present
func TestLexifyWithoutPlaceholders(t *testing.T) {
f := New()
input := "Hello World"
result := f.Lexify(input)
if result != input {
t.Errorf("Lexify should return input unchanged when no ? present: expected %s, got %s", input, result)
}
}
// TestAsciifyWithoutPlaceholders tests Asciify when no * characters are present
func TestAsciifyWithoutPlaceholders(t *testing.T) {
f := New()
input := "Hello World"
result := f.Asciify(input)
if result != input {
t.Errorf("Asciify should return input unchanged when no * present: expected %s, got %s", input, result)
}
}
// ============================================================================
// CONCURRENCY & THREAD SAFETY TESTS
// ============================================================================
// TestConcurrentFakerCreation tests concurrent creation of Faker instances
func TestConcurrentFakerCreation(t *testing.T) {
const numGoroutines = 100
var wg sync.WaitGroup
wg.Add(numGoroutines)
results := make([]Faker, numGoroutines)