File: bytes_test.go

package info (click to toggle)
golang-github-savsgio-gotils 0.0~git20250408.196191e-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: makefile: 2
file content (181 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download
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
package bytes

import (
	"reflect"
	"sync"
	"testing"

	"github.com/savsgio/gotils/strings"
)

func testRand(t *testing.T) {
	t.Helper()

	values := make([]string, 0)

	for i := 0; i < 10000; i++ {
		n := 32
		dst := make([]byte, n)

		Rand(dst)

		if strings.Include(values, string(dst)) {
			t.Error("Rand() returns the same value")

			return
		}

		values = append(values, string(dst))

		for i := range dst {
			if string(rune(i)) == "" {
				t.Errorf("RandBytes() invalid char '%v'", dst[i])
			}
		}

		if len(dst) != n {
			t.Errorf("RandBytes() length '%d', want '%d'", len(dst), n)
		}
	}
}

func Test_Rand(t *testing.T) {
	testRand(t)
}

func Test_RandConcurrent(t *testing.T) {
	n := 32
	wg := sync.WaitGroup{}

	for i := 0; i < n; i++ {
		wg.Add(1)

		go func() {
			defer wg.Done()

			testRand(t)
		}()
	}

	wg.Wait()
}

func Test_Copy(t *testing.T) {
	str := []byte("cache")
	result := Copy(str)

	if reflect.ValueOf(&str).Pointer() == reflect.ValueOf(&result).Pointer() {
		t.Errorf("Copy() returns the same pointer (source == %p | result == %p)", &str, &result)
	}
}

func Test_Equal(t *testing.T) {
	foo := []byte("foo")
	bar := []byte("bar")

	if isEqual := Equal(foo, bar); isEqual {
		t.Errorf("Equal(%s, %s) == %v, want %v", foo, bar, isEqual, false)
	}

	if isEqual := Equal(foo, foo); !isEqual {
		t.Errorf("Equal(%s, %s) == %v, want %v", foo, foo, isEqual, true)
	}
}

func Test_Extend(t *testing.T) {
	type args struct {
		b       []byte
		needLen int
	}

	type want struct {
		sliceLen int
	}

	tests := []struct {
		name string
		args args
		want want
	}{
		{
			name: "Initial 0",
			args: args{
				b:       make([]byte, 0),
				needLen: 10,
			},
			want: want{
				sliceLen: 10,
			},
		},
		{
			name: "Initial 10",
			args: args{
				b:       make([]byte, 10),
				needLen: 5,
			},
			want: want{
				sliceLen: 5,
			},
		},
		{
			name: "Initial 69",
			args: args{
				b:       make([]byte, 45),
				needLen: 69,
			},
			want: want{
				sliceLen: 69,
			},
		},
	}

	for _, tt := range tests {
		b := tt.args.b
		needLen := tt.args.needLen
		sliceLen := tt.want.sliceLen

		t.Run(tt.name, func(t *testing.T) {
			got := Extend(b, needLen)

			gotLen := len(got)
			if gotLen != sliceLen {
				t.Errorf("ExtendByteSlice() length = %v, want = %v", gotLen, sliceLen)
			}
		})
	}
}

func Test_Prepend(t *testing.T) {
	foo := []byte("foo")
	bar := []byte("bar")

	expected := []byte("foobar")
	result := Prepend(bar, foo...)

	if isEqual := Equal(result, expected); !isEqual {
		t.Errorf("Prepend() == %s, want %s", result, expected)
	}
}

func Test_PrependString(t *testing.T) {
	foo := "foo"
	bar := []byte("bar")

	expected := []byte("foobar")
	result := PrependString(bar, foo)

	if isEqual := Equal(result, expected); !isEqual {
		t.Errorf("Prepend() == %s, want %s", result, expected)
	}
}

func Benchmark_Rand(b *testing.B) {
	n := 32
	dst := make([]byte, n)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Rand(dst)
		}
	})
}