File: strings_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 (104 lines) | stat: -rw-r--r-- 2,512 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
package strings

import (
	"reflect"
	"testing"
)

func Test_Copy(t *testing.T) {
	str := "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_CopySlice(t *testing.T) {
	slice := []string{"kratgo", "fast", "http", "cache"}

	result := CopySlice(slice)

	if !reflect.DeepEqual(result, slice) {
		t.Errorf("CopySlice() == %v, want %v", result, slice)
	}
}

func Test_IndexOf(t *testing.T) {
	slice := []string{"kratgo", "fast", "http", "cache"}

	s := "fast"
	if i := IndexOf(slice, s); i < 0 {
		t.Errorf("stringSliceIndexOf() = %v, want %v", i, 2)
	}

	s = "slow"
	if i := IndexOf(slice, s); i > -1 {
		t.Errorf("stringSliceIndexOf() = %v, want %v", i, -1)
	}
}

func Test_Include(t *testing.T) {
	slice := []string{"kratgo", "fast", "http", "cache"}

	s := "fast"
	if ok := Include(slice, s); !ok {
		t.Errorf("stringSliceInclude() = %v, want %v", ok, true)
	}

	s = "slow"
	if ok := Include(slice, s); ok {
		t.Errorf("stringSliceInclude() = %v, want %v", ok, false)
	}
}

func Test_UniqueAppend(t *testing.T) {
	slice := []string{"kratgo", "fast", "http", "cache"}
	slice2 := CopySlice(slice)

	result := UniqueAppend(slice, slice[0])
	if len(result) != len(slice) {
		t.Errorf("UniqueAppend() == %v, want %v", result, slice)
	}

	slice2 = append(slice2, "unique")

	result = UniqueAppend(slice, slice2...)
	if len(result) == len(slice) {
		t.Errorf("UniqueAppend() == %v, want %v", result, slice2)
	}
}

func Test_EqualSlices(t *testing.T) {
	slice1 := []string{"kratgo", "fast", "http", "cache"}
	slice2 := []string{"kratgo", "fast", "http", "cache"}

	if !EqualSlices(slice1, slice2) {
		t.Errorf("EqualSlices() == %v, want %v", false, true)
	}

	slice1 = []string{"kratgo", "fast", "http", "cache"}
	slice2 = []string{"kratgo", "fast", "http"}

	if EqualSlices(slice1, slice2) {
		t.Errorf("EqualSlices() == %v, want %v", true, false)
	}

	slice1 = []string{"kratgo", "fast", "http", "cache"}
	slice2 = []string{"kratgo", "fast", "rpc", "cache"}

	if EqualSlices(slice1, slice2) {
		t.Errorf("EqualSlices() == %v, want %v", true, false)
	}
}

func Test_ReverseSlice(t *testing.T) {
	slice := []string{"kratgo", "fast", "http", "cache"}
	reversedSlice := []string{"cache", "http", "fast", "kratgo"}

	result := ReverseSlice(slice)

	if !EqualSlices(result, reversedSlice) {
		t.Errorf("ReverseSlice() == %v, want %v", result, reversedSlice)
	}
}