forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
120 lines (101 loc) · 2.01 KB
/
util_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
package main
import (
"testing"
)
func TestContains(t *testing.T) {
items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
validItem := 7
invalidItem := 42
conditions := []bool{
Contains[int](items, validItem),
!Contains[int](items, invalidItem),
}
for _, condition := range conditions {
if !condition {
t.Fail()
}
}
}
func TestIfElse(t *testing.T) {
IfElse(true, func() {}, func() { t.Fail() })()
IfElse(false, func() { t.Fail() }, func() {})()
}
func TestPlural(t *testing.T) {
conditions := []bool{
Plural("word", 2) == "words",
Plural("name", 29485) == "names",
Plural("apple", 1) == "apple",
Plural("dog", 0) == "dogs",
}
for _, condition := range conditions {
if !condition {
t.Fail()
}
}
}
func TestIsUnique(t *testing.T) {
conditions := []bool{
IsUnique([]int{1, 2, 3, 4}),
!IsUnique([]int{1, 2, 3, 1}),
IsUnique([]string{"Hello", "hello"}),
}
for _, condition := range conditions {
if !condition {
t.Fail()
}
}
}
func TestFind(t *testing.T) {
integers := []int{1, 2, 3, 4, 10, 27, -258925}
if found, ok := Find(integers, func(i int) bool {
return i == 10
}); ok {
if found != 10 {
t.Error("Wrong element was found")
}
} else {
t.Error("Element was not found")
}
if _, ok := Find(integers, func(i int) bool {
return i == 0
}); ok {
t.Error("ok is expected to be false for the non-existing element")
}
type person struct {
name string
age int
}
structs := []person{
{
name: "name 1",
age: 100,
},
{
name: "name 2",
age: -1,
},
}
if found, ok := Find(structs, func(p person) bool {
return p.age < 0
}); ok {
if found.age != -1 {
t.Error("Wrong element was found")
}
} else {
t.Error("Element was not found")
}
}
func TestMap(t *testing.T) {
square := func(n int) int { return n * n }
nums := []int{0, 1, 2, 3, 4}
squared := Map(nums, square)
if len(nums) != len(squared) {
t.Error("Different lengths")
}
for i, s := range squared {
n := nums[i]
if square(n) != s {
t.Error("Invalid value")
}
}
}