aboutsummaryrefslogtreecommitdiff
path: root/pipe/pipe_test.go
blob: 950d039d0aead7e1d0debf57ae5b6295f31561ae (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
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
package pipe

import (
	"testing"
)

const Values = 999
const Multiplier = 1000

func createCountPipe() <-chan int {
	cout := make(chan int)

	go func(<-chan int) {
		for i := 1; i < Values; i++ {
			cout <- i
		}
		close(cout)
	}(cout)

	return cout
}

func multiply(in int) (int, error) {
	return in * Multiplier, nil
}

func devide(in int) (int, error) {
	return in / Multiplier, nil
}

func TestProc(t *testing.T) {
	c := createCountPipe()
	am := Proc(c, 8, multiply)
	ad := Proc(am, 8, devide)
	a := Proc(ad, 8, multiply)

	out := make([]int, Values-1)

	count := 0
	for i := range a {
		out[count] = i
		count++
	}

	for _, v := range out {
		if v < Multiplier {
			t.Errorf("Invalid output %d", v)
		}
	}
}

func TestYield(t *testing.T) {
	lin := make([]int, Values)

	for i := 0; i < Values; i++ {
		lin[i] = i
	}

	cout := Yield(lin)

	count := 0
	for out := range cout {
		if out != lin[count] {
			t.Errorf("Invalid output %d", out)
		}
		count++
	}
}

type buffer struct {
	value int
}

func createBuffers() []*buffer {
	buffers := make([]*buffer, Values)

	for i := 0; i < Values; i++ {
		buffers[i] = &buffer{value: i}
	}

	return buffers
}

func multiplyBuffer(in *buffer) (*buffer, error) {
	in.value = in.value * Multiplier
	return in, nil
}

func devideBuffer(in *buffer) error {
	in.value = in.value / Multiplier
	return nil
}

func TestTailProc(t *testing.T) {
	buffers := createBuffers()
	c := Yield(buffers)
	am := Proc(c, 8, multiplyBuffer)
	TailProc(am, 8, devideBuffer)

	for i, v := range buffers {
		if v.value != i {
			t.Errorf("Invalid output %d != %d", v.value, i)
		}
	}
}

func TestWait(t *testing.T) {
	buffers := createBuffers()
	c := Yield(buffers)
	am := Proc(c, 8, multiplyBuffer)
	Wait(am)

	for i, v := range buffers {
		if v.value != (i * Multiplier) {
			t.Errorf("Invalid output %d != %d", v.value, i)
		}
	}
}