summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/disintegration/imaging/utils_test.go
blob: 99aa8c82feac2a837cb43bf75b0a1b5699ce69a9 (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
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
package imaging

import (
	"runtime"
	"testing"
)

func testParallelN(enabled bool, n, procs int) bool {
	data := make([]bool, n)
	before := runtime.GOMAXPROCS(0)
	runtime.GOMAXPROCS(procs)
	parallel(n, func(start, end int) {
		for i := start; i < end; i++ {
			data[i] = true
		}
	})
	for i := 0; i < n; i++ {
		if data[i] != true {
			return false
		}
	}
	runtime.GOMAXPROCS(before)
	return true
}

func TestParallel(t *testing.T) {
	for _, e := range []bool{true, false} {
		for _, n := range []int{1, 10, 100, 1000} {
			for _, p := range []int{1, 2, 4, 8, 16, 100} {
				if testParallelN(e, n, p) != true {
					t.Errorf("test [parallel %v %d %d] failed", e, n, p)
				}
			}
		}
	}
}

func TestClamp(t *testing.T) {
	td := []struct {
		f float64
		u uint8
	}{
		{0, 0},
		{255, 255},
		{128, 128},
		{0.49, 0},
		{0.50, 1},
		{254.9, 255},
		{254.0, 254},
		{256, 255},
		{2500, 255},
		{-10, 0},
		{127.6, 128},
	}

	for _, d := range td {
		if clamp(d.f) != d.u {
			t.Errorf("test [clamp %v %v] failed: %v", d.f, d.u, clamp(d.f))
		}
	}
}

func TestClampint32(t *testing.T) {
	td := []struct {
		i int32
		u uint8
	}{
		{0, 0},
		{255, 255},
		{128, 128},
		{256, 255},
		{2500, 255},
		{-10, 0},
	}

	for _, d := range td {
		if clampint32(d.i) != d.u {
			t.Errorf("test [clampint32 %v %v] failed: %v", d.i, d.u, clampint32(d.i))
		}
	}
}