| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package utils |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/etix/mirrorbits/core" |
| 11 |
) |
| 12 |
|
| 13 |
func TestHasAnyPrefix(t *testing.T) { |
| 14 |
var b bool |
| 15 |
|
| 16 |
b = HasAnyPrefix("http://test.com", "http://", "https://") |
| 17 |
if !b { |
| 18 |
t.Fatal("Expected true, got false") |
| 19 |
} |
| 20 |
b = HasAnyPrefix("https://test.com", "http://", "https://") |
| 21 |
if !b { |
| 22 |
t.Fatal("Expected true, got false") |
| 23 |
} |
| 24 |
b = HasAnyPrefix("test.com", "http://", "https://") |
| 25 |
if b { |
| 26 |
t.Fatal("Expected false, got true") |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestNormalizeURL(t *testing.T) { |
| 31 |
s := []string{ |
| 32 |
"", "", |
| 33 |
"rsync://test.com", "rsync://test.com/", |
| 34 |
"rsync://test.com/", "rsync://test.com/", |
| 35 |
} |
| 36 |
|
| 37 |
if len(s)%2 != 0 { |
| 38 |
t.Fatal("not multiple of 2") |
| 39 |
} |
| 40 |
|
| 41 |
for i := 0; i < len(s); i += 2 { |
| 42 |
if r := NormalizeURL(s[i]); r != s[i+1] { |
| 43 |
t.Fatalf("%q: expected %q, got %q", s[i], s[i+1], r) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestGetDistanceKm(t *testing.T) { |
| 49 |
if r := GetDistanceKm(48.8567, 2.3508, 40.7127, 74.0059); int(r) != 5514 { |
| 50 |
t.Fatalf("Expected 5514, got %f", r) |
| 51 |
} |
| 52 |
if r := GetDistanceKm(48.8567, 2.3508, 48.8567, 2.3508); int(r) != 0 { |
| 53 |
t.Fatalf("Expected 0, got %f", r) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestMin(t *testing.T) { |
| 58 |
if r := Min(-10, 5); r != -10 { |
| 59 |
t.Fatalf("Expected -10, got %d", r) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestMax(t *testing.T) { |
| 64 |
if r := Max(-10, 5); r != 5 { |
| 65 |
t.Fatalf("Expected 5, got %d", r) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestAdd(t *testing.T) { |
| 70 |
if r := Add(2, 40); r != 42 { |
| 71 |
t.Fatalf("Expected 42, got %d", r) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestVersion(t *testing.T) { |
| 76 |
if r := Version(); len(r) == 0 || r != core.VERSION { |
| 77 |
t.Fatalf("Expected %s, got %s", core.VERSION, r) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestHostname(t *testing.T) { |
| 82 |
if r := Hostname(); len(r) == 0 { |
| 83 |
t.Fatalf("Expected a valid hostname") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestIsInSlice(t *testing.T) { |
| 88 |
var b bool |
| 89 |
list := []string{"aaa", "bbb", "ccc"} |
| 90 |
|
| 91 |
b = IsInSlice("ccc", list) |
| 92 |
if !b { |
| 93 |
t.Fatal("Expected true, got false") |
| 94 |
} |
| 95 |
b = IsInSlice("b", list) |
| 96 |
if b { |
| 97 |
t.Fatal("Expected false, got true") |
| 98 |
} |
| 99 |
b = IsInSlice("", list) |
| 100 |
if b { |
| 101 |
t.Fatal("Expected false, got true") |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestIsStopped(t *testing.T) { |
| 106 |
stop := make(chan struct{}, 1) |
| 107 |
|
| 108 |
if IsStopped(stop) { |
| 109 |
t.Fatal("Expected false, got true") |
| 110 |
} |
| 111 |
|
| 112 |
close(stop) |
| 113 |
|
| 114 |
if !IsStopped(stop) { |
| 115 |
t.Fatal("Expected true, got false") |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestReadableSize(t *testing.T) { |
| 120 |
ivalues := []int64{0, 1, 1024, 1000000} |
| 121 |
svalues := []string{"0.0 bytes", "1.0 bytes", "1.0 KiB", "976.6 KiB"} |
| 122 |
|
| 123 |
for i := range ivalues { |
| 124 |
if r := ReadableSize(ivalues[i]); r != svalues[i] { |
| 125 |
t.Fatalf("Expected %q, got %q", svalues[i], r) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestElapsedSec(t *testing.T) { |
| 131 |
now := time.Now().UTC().Unix() |
| 132 |
|
| 133 |
lastTimestamp := now - 1000 |
| 134 |
|
| 135 |
if ElapsedSec(lastTimestamp, 500) == false { |
| 136 |
t.Fatalf("Expected true, got false") |
| 137 |
} |
| 138 |
if ElapsedSec(lastTimestamp, 5000) == true { |
| 139 |
t.Fatalf("Expected false, got true") |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestPlural(t *testing.T) { |
| 144 |
if Plural(2) != "s" { |
| 145 |
t.Fatalf("Expected 's', got ''") |
| 146 |
} |
| 147 |
if Plural(10000000) != "s" { |
| 148 |
t.Fatalf("Expected 's', got ''") |
| 149 |
} |
| 150 |
if Plural(-2) != "s" { |
| 151 |
t.Fatalf("Expected 's', got ''") |
| 152 |
} |
| 153 |
if Plural(1) != "" { |
| 154 |
t.Fatalf("Expected '', got 's'") |
| 155 |
} |
| 156 |
if Plural(-1) != "" { |
| 157 |
t.Fatalf("Expected '', got 's'") |
| 158 |
} |
| 159 |
if Plural(0) != "" { |
| 160 |
t.Fatalf("Expected '', got 's'") |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestConcatURL(t *testing.T) { |
| 165 |
part1 := "http://test.example/somedir/" |
| 166 |
part2 := "/somefile.bin" |
| 167 |
result := "http://test.example/somedir/somefile.bin" |
| 168 |
if r := ConcatURL(part1, part2); r != result { |
| 169 |
t.Fatalf("Expected %s, got %s", result, r) |
| 170 |
} |
| 171 |
|
| 172 |
part1 = "http://test.example/somedir" |
| 173 |
part2 = "/somefile.bin" |
| 174 |
result = "http://test.example/somedir/somefile.bin" |
| 175 |
if r := ConcatURL(part1, part2); r != result { |
| 176 |
t.Fatalf("Expected %s, got %s", result, r) |
| 177 |
} |
| 178 |
|
| 179 |
part1 = "http://test.example/somedir" |
| 180 |
part2 = "somefile.bin" |
| 181 |
result = "http://test.example/somedir/somefile.bin" |
| 182 |
if r := ConcatURL(part1, part2); r != result { |
| 183 |
t.Fatalf("Expected %s, got %s", result, r) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func TestTimeKeyCoverage(t *testing.T) { |
| 188 |
date1Start := time.Date(2015, 10, 30, 12, 42, 11, 0, time.UTC) |
| 189 |
date1End := time.Date(2015, 12, 2, 13, 42, 11, 0, time.UTC) |
| 190 |
result1 := []string{"2015_10_30", "2015_10_31", "2015_11", "2015_12_01"} |
| 191 |
|
| 192 |
result := TimeKeyCoverage(date1Start, date1End) |
| 193 |
|
| 194 |
if len(result) != len(result1) { |
| 195 |
t.Fatalf("Expect %d elements, got %d", len(result1), len(result)) |
| 196 |
} |
| 197 |
|
| 198 |
for i, r := range result { |
| 199 |
if r != result1[i] { |
| 200 |
t.Fatalf("Expect %#v, got %#v", result1, result) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/* */ |
| 205 |
|
| 206 |
date2Start := time.Date(2015, 12, 2, 12, 42, 11, 0, time.UTC) |
| 207 |
date2End := time.Date(2015, 12, 2, 13, 42, 11, 0, time.UTC) |
| 208 |
result2 := []string{"2015_12_02"} |
| 209 |
|
| 210 |
result = TimeKeyCoverage(date2Start, date2End) |
| 211 |
|
| 212 |
if len(result) != len(result2) { |
| 213 |
t.Fatalf("Expect %d elements, got %d", len(result2), len(result)) |
| 214 |
} |
| 215 |
|
| 216 |
for i, r := range result { |
| 217 |
if r != result2[i] { |
| 218 |
t.Fatalf("Expect %#v, got %#v", result2, result) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/* */ |
| 223 |
|
| 224 |
date3Start := time.Date(2015, 1, 1, 12, 42, 11, 0, time.UTC) |
| 225 |
date3End := time.Date(2017, 1, 1, 13, 42, 11, 0, time.UTC) |
| 226 |
result3 := []string{"2015", "2016"} |
| 227 |
|
| 228 |
result = TimeKeyCoverage(date3Start, date3End) |
| 229 |
|
| 230 |
if len(result) != len(result3) { |
| 231 |
t.Fatalf("Expect %d elements, got %d", len(result3), len(result)) |
| 232 |
} |
| 233 |
|
| 234 |
for i, r := range result { |
| 235 |
if r != result3[i] { |
| 236 |
t.Fatalf("Expect %#v, got %#v", result3, result) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/* */ |
| 241 |
|
| 242 |
date4Start := time.Date(2015, 12, 31, 12, 42, 11, 0, time.UTC) |
| 243 |
date4End := time.Date(2016, 1, 2, 13, 42, 11, 0, time.UTC) |
| 244 |
result4 := []string{"2015_12_31", "2016_01_01"} |
| 245 |
|
| 246 |
result = TimeKeyCoverage(date4Start, date4End) |
| 247 |
|
| 248 |
if len(result) != len(result4) { |
| 249 |
t.Fatalf("Expect %d elements, got %d", len(result4), len(result)) |
| 250 |
} |
| 251 |
|
| 252 |
for i, r := range result { |
| 253 |
if r != result4[i] { |
| 254 |
t.Fatalf("Expect %#v, got %#v", result4, result) |
| 255 |
} |
| 256 |
} |
| 257 |
} |