File: utils_test.go

package info (click to toggle)
mirrorbits 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 984 kB
  • sloc: sh: 675; makefile: 93
file content (257 lines) | stat: -rw-r--r-- 5,743 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2014-2019 Ludovic Fauvet
// Licensed under the MIT license

package utils

import (
	"testing"
	"time"

	"github.com/etix/mirrorbits/core"
)

func TestHasAnyPrefix(t *testing.T) {
	var b bool

	b = HasAnyPrefix("http://test.com", "http://", "https://")
	if !b {
		t.Fatal("Expected true, got false")
	}
	b = HasAnyPrefix("https://test.com", "http://", "https://")
	if !b {
		t.Fatal("Expected true, got false")
	}
	b = HasAnyPrefix("test.com", "http://", "https://")
	if b {
		t.Fatal("Expected false, got true")
	}
}

func TestNormalizeURL(t *testing.T) {
	s := []string{
		"", "",
		"rsync://test.com", "rsync://test.com/",
		"rsync://test.com/", "rsync://test.com/",
	}

	if len(s)%2 != 0 {
		t.Fatal("not multiple of 2")
	}

	for i := 0; i < len(s); i += 2 {
		if r := NormalizeURL(s[i]); r != s[i+1] {
			t.Fatalf("%q: expected %q, got %q", s[i], s[i+1], r)
		}
	}
}

func TestGetDistanceKm(t *testing.T) {
	if r := GetDistanceKm(48.8567, 2.3508, 40.7127, 74.0059); int(r) != 5514 {
		t.Fatalf("Expected 5514, got %f", r)
	}
	if r := GetDistanceKm(48.8567, 2.3508, 48.8567, 2.3508); int(r) != 0 {
		t.Fatalf("Expected 0, got %f", r)
	}
}

func TestMin(t *testing.T) {
	if r := Min(-10, 5); r != -10 {
		t.Fatalf("Expected -10, got %d", r)
	}
}

func TestMax(t *testing.T) {
	if r := Max(-10, 5); r != 5 {
		t.Fatalf("Expected 5, got %d", r)
	}
}

func TestAdd(t *testing.T) {
	if r := Add(2, 40); r != 42 {
		t.Fatalf("Expected 42, got %d", r)
	}
}

func TestVersion(t *testing.T) {
	if r := Version(); len(r) == 0 || r != core.VERSION {
		t.Fatalf("Expected %s, got %s", core.VERSION, r)
	}
}

func TestHostname(t *testing.T) {
	if r := Hostname(); len(r) == 0 {
		t.Fatalf("Expected a valid hostname")
	}
}

func TestIsInSlice(t *testing.T) {
	var b bool
	list := []string{"aaa", "bbb", "ccc"}

	b = IsInSlice("ccc", list)
	if !b {
		t.Fatal("Expected true, got false")
	}
	b = IsInSlice("b", list)
	if b {
		t.Fatal("Expected false, got true")
	}
	b = IsInSlice("", list)
	if b {
		t.Fatal("Expected false, got true")
	}
}

func TestIsStopped(t *testing.T) {
	stop := make(chan struct{}, 1)

	if IsStopped(stop) {
		t.Fatal("Expected false, got true")
	}

	close(stop)

	if !IsStopped(stop) {
		t.Fatal("Expected true, got false")
	}
}

func TestReadableSize(t *testing.T) {
	ivalues := []int64{0, 1, 1024, 1000000}
	svalues := []string{"0.0 bytes", "1.0 bytes", "1.0 KB", "976.6 KB"}

	for i := range ivalues {
		if r := ReadableSize(ivalues[i]); r != svalues[i] {
			t.Fatalf("Expected %q, got %q", svalues[i], r)
		}
	}
}

func TestElapsedSec(t *testing.T) {
	now := time.Now().UTC().Unix()

	lastTimestamp := now - 1000

	if ElapsedSec(lastTimestamp, 500) == false {
		t.Fatalf("Expected true, got false")
	}
	if ElapsedSec(lastTimestamp, 5000) == true {
		t.Fatalf("Expected false, got true")
	}
}

func TestPlural(t *testing.T) {
	if Plural(2) != "s" {
		t.Fatalf("Expected 's', got ''")
	}
	if Plural(10000000) != "s" {
		t.Fatalf("Expected 's', got ''")
	}
	if Plural(-2) != "s" {
		t.Fatalf("Expected 's', got ''")
	}
	if Plural(1) != "" {
		t.Fatalf("Expected '', got 's'")
	}
	if Plural(-1) != "" {
		t.Fatalf("Expected '', got 's'")
	}
	if Plural(0) != "" {
		t.Fatalf("Expected '', got 's'")
	}
}

func TestConcatURL(t *testing.T) {
	part1 := "http://test.example/somedir/"
	part2 := "/somefile.bin"
	result := "http://test.example/somedir/somefile.bin"
	if r := ConcatURL(part1, part2); r != result {
		t.Fatalf("Expected %s, got %s", result, r)
	}

	part1 = "http://test.example/somedir"
	part2 = "/somefile.bin"
	result = "http://test.example/somedir/somefile.bin"
	if r := ConcatURL(part1, part2); r != result {
		t.Fatalf("Expected %s, got %s", result, r)
	}

	part1 = "http://test.example/somedir"
	part2 = "somefile.bin"
	result = "http://test.example/somedir/somefile.bin"
	if r := ConcatURL(part1, part2); r != result {
		t.Fatalf("Expected %s, got %s", result, r)
	}
}

func TestTimeKeyCoverage(t *testing.T) {
	date1Start := time.Date(2015, 10, 30, 12, 42, 11, 0, time.UTC)
	date1End := time.Date(2015, 12, 2, 13, 42, 11, 0, time.UTC)
	result1 := []string{"2015_10_30", "2015_10_31", "2015_11", "2015_12_01"}

	result := TimeKeyCoverage(date1Start, date1End)

	if len(result) != len(result1) {
		t.Fatalf("Expect %d elements, got %d", len(result1), len(result))
	}

	for i, r := range result {
		if r != result1[i] {
			t.Fatalf("Expect %#v, got %#v", result1, result)
		}
	}

	/* */

	date2Start := time.Date(2015, 12, 2, 12, 42, 11, 0, time.UTC)
	date2End := time.Date(2015, 12, 2, 13, 42, 11, 0, time.UTC)
	result2 := []string{"2015_12_02"}

	result = TimeKeyCoverage(date2Start, date2End)

	if len(result) != len(result2) {
		t.Fatalf("Expect %d elements, got %d", len(result2), len(result))
	}

	for i, r := range result {
		if r != result2[i] {
			t.Fatalf("Expect %#v, got %#v", result2, result)
		}
	}

	/* */

	date3Start := time.Date(2015, 1, 1, 12, 42, 11, 0, time.UTC)
	date3End := time.Date(2017, 1, 1, 13, 42, 11, 0, time.UTC)
	result3 := []string{"2015", "2016"}

	result = TimeKeyCoverage(date3Start, date3End)

	if len(result) != len(result3) {
		t.Fatalf("Expect %d elements, got %d", len(result3), len(result))
	}

	for i, r := range result {
		if r != result3[i] {
			t.Fatalf("Expect %#v, got %#v", result3, result)
		}
	}

	/* */

	date4Start := time.Date(2015, 12, 31, 12, 42, 11, 0, time.UTC)
	date4End := time.Date(2016, 1, 2, 13, 42, 11, 0, time.UTC)
	result4 := []string{"2015_12_31", "2016_01_01"}

	result = TimeKeyCoverage(date4Start, date4End)

	if len(result) != len(result4) {
		t.Fatalf("Expect %d elements, got %d", len(result4), len(result))
	}

	for i, r := range result {
		if r != result4[i] {
			t.Fatalf("Expect %#v, got %#v", result4, result)
		}
	}
}