77 "testing"
88)
99
10+ const x64 = 64
11+
1012func ConvertSliceManual (from []int64 ) []int32 {
1113 to := make ([]int32 , len (from ))
1214 for i , v := range from {
@@ -15,6 +17,91 @@ func ConvertSliceManual(from []int64) []int32 {
1517 return to
1618}
1719
20+ func TestCopyUnsafe (t * testing.T ) {
21+ // Test 1: Standard case
22+ source := []byte ("Hello, World!" )
23+ destination := make ([]byte , len (source ))
24+ n := CopyUnsafe (destination , source )
25+ if n != len (source ) {
26+ t .Errorf ("Expected %d bytes copied, got %d" , len (source ), n )
27+ }
28+ if string (destination ) != string (source ) {
29+ t .Errorf ("Expected destination %q, got %q" , string (source ), string (destination ))
30+ }
31+
32+ // Test 2: Different lengths (source longer than destination)
33+ source = []byte ("Hello, Go!" )
34+ destination = make ([]byte , len (source )- 2 ) // Smaller destination
35+ defer func () {
36+ if r := recover (); r == nil {
37+ t .Errorf ("Expected panic for size mismatch, but none occurred" )
38+ }
39+ }()
40+ _ = CopyUnsafe (destination , source )
41+
42+ // Test 3: Empty slices
43+ source = []byte {}
44+ destination = []byte {}
45+ n = CopyUnsafe (destination , source )
46+ if n != 0 {
47+ t .Errorf ("Expected 0 bytes copied, got %d" , n )
48+ }
49+ }
50+
51+ func TestStrings (t * testing.T ) {
52+ // Test 1: Standard case
53+ source := []byte ("Hello, World!" )
54+
55+ n := String (source )
56+ if n != string (source ) {
57+ t .Errorf ("Expected %s string copied, got %s" , string (source ), n )
58+ }
59+
60+ source1 := "Hello,world!"
61+
62+ n1 := StringToBytes (source1 )
63+ if string (n1 ) != source1 {
64+ t .Errorf ("Expected %s string copied, got %s" , source , n1 )
65+ }
66+
67+ }
68+
69+ func BenchmarkCopyUnsafe (b * testing.B ) {
70+ source := []byte ("Benchmarking Unsafe Copy!" )
71+ destination := make ([]byte , len (source ))
72+ b .ResetTimer ()
73+ for i := 0 ; i < b .N ; i ++ {
74+ CopyUnsafe (destination , source )
75+ }
76+ }
77+
78+ func BenchmarkCopyStandart (b * testing.B ) {
79+ source := []byte ("Benchmarking Standart Copy!" )
80+ destination := make ([]byte , len (source ))
81+ b .ResetTimer ()
82+ for i := 0 ; i < b .N ; i ++ {
83+ copy (destination , source )
84+ }
85+ }
86+
87+ func BenchmarkCopy_currentBytes (b * testing.B ) {
88+ source := []byte ("Benchmarking Current Copy!" )
89+ destination := make ([]byte , len (source ))
90+ b .ResetTimer ()
91+ for i := 0 ; i < b .N ; i ++ {
92+ copy (destination [:26 ], source )
93+ }
94+ }
95+
96+ func BenchmarkCopy_currentBytes_UNSAFE (b * testing.B ) {
97+ source := []byte ("Benchmarking Current Copy!" )
98+ destination := make ([]byte , len (source ))
99+ b .ResetTimer ()
100+ for i := 0 ; i < b .N ; i ++ {
101+ CopyUnsafe (destination [:26 ], source )
102+ }
103+ }
104+
18105func generateTestStrings (count , minLength , maxLength int ) []string {
19106 var data []string
20107 for i := 0 ; i < count ; i ++ {
@@ -58,13 +145,27 @@ func BenchmarkStringsBuilder(b *testing.B) {
58145
59146func BenchmarkString (b * testing.B ) {
60147 data := []byte ("This is a benchmark test for String conversion." )
148+ length := len (data )
61149
62150 b .Run ("Custom String" , func (b * testing.B ) {
63151 for i := 0 ; i < b .N ; i ++ {
64152 _ = String (data )
65153 }
66154 })
67155
156+ b .Run ("Custom string 2" , func (b * testing.B ) {
157+ for i := 0 ; i < b .N ; i ++ {
158+ _ = string2 (data , length )
159+ }
160+
161+ })
162+
163+ b .Run ("Custom string 3" , func (b * testing.B ) {
164+ for i := 0 ; i < b .N ; i ++ {
165+ _ = string3 (data )
166+ }
167+
168+ })
68169 b .Run ("Standard String" , func (b * testing.B ) {
69170 for i := 0 ; i < b .N ; i ++ {
70171 _ = string (data )
@@ -211,15 +312,38 @@ func BenchmarkStringToBytes(b *testing.B) {
211312 })
212313}
213314
315+ func TestEquals (t * testing.T ) {
316+ t .Run ("TestEqualsTrue" , func (t * testing.T ) {
317+ a := []byte ("This is a benchmark test for Equal....." )
318+ bb := []byte ("This is a benchmark test for Equal....." )
319+ lengthA := uintptr (len (a ))
320+ boole := Equal (a , bb , lengthA )
321+ if boole == false {
322+ t .Log ("IsEqual: " , Equal (a , bb , lengthA ))
323+ }
324+ })
325+ t .Run ("TestEqualsFalse" , func (t * testing.T ) {
326+ a := []byte ("This is a benchmark test for Equal....." )
327+ bb := []byte ("This is a benchmark test for Equal.....1" )
328+ lengthA := uintptr (len (a ))
329+ boole := Equal (a , bb , lengthA )
330+ if boole == true {
331+ t .Log ("IsEqual: " , Equal (a , bb , lengthA ))
332+ }
333+
334+ })
335+ }
336+
214337func BenchmarkEqualTrue (b * testing.B ) { // true
215338 a := []byte ("This is a benchmark test for Equal....." )
216339 bb := []byte ("This is a benchmark test for Equal....." )
340+ lengthA := uintptr (len (a ))
217341
218342 b .Run ("Custom Equal" , func (b * testing.B ) {
219343 for i := 0 ; i < b .N ; i ++ {
220- boole := Equal (a , bb )
344+ boole := Equal (a , bb , lengthA )
221345 if boole == false {
222- b .Log ("IsEqual: " , Equal (a , bb ))
346+ b .Log ("IsEqual: " , Equal (a , bb , lengthA ))
223347 }
224348 }
225349 })
@@ -233,6 +357,15 @@ func BenchmarkEqualTrue(b *testing.B) { // true
233357
234358 }
235359 })
360+
361+ b .Run ("TEST GENERIC EQUAL" , func (b * testing.B ) {
362+ for i := 0 ; i < b .N ; i ++ {
363+ boole := String (a ) == String (bb )
364+ if boole == false {
365+ b .Fatal ("GenericEqual: " , Equal (a , bb , lengthA ))
366+ }
367+ }
368+ })
236369}
237370
238371func BenchmarkConvertSlice (b * testing.B ) {
0 commit comments