summaryrefslogtreecommitdiffstats
path: root/src/util/util_test.go
blob: 8aeaeac59eb6c86b845efc28c13e50c78073bc4b (plain)
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
package util

import "testing"

func TestMax(t *testing.T) {
	if Max(-2, 5, 1, 4, 3) != 5 {
		t.Error("Invalid result")
	}
}

func TestContrain(t *testing.T) {
	if Constrain(-3, -1, 3) != -1 {
		t.Error("Expected", -1)
	}
	if Constrain(2, -1, 3) != 2 {
		t.Error("Expected", 2)
	}

	if Constrain(5, -1, 3) != 3 {
		t.Error("Expected", 3)
	}
}

func TestTrimLen(t *testing.T) {
	check := func(str string, exp int) {
		trimmed := TrimLen([]rune(str))
		if trimmed != exp {
			t.Errorf("Invalid TrimLen result for '%s': %d (expected %d)",
				str, trimmed, exp)
		}
	}
	check("hello", 5)
	check("hello ", 5)
	check("hello  ", 5)
	check(" hello", 5)
	check("  hello", 5)
	check(" hello ", 5)
	check("  hello  ", 5)
	check("h   o", 5)
	check("  h   o  ", 5)
	check("         ", 0)
}