From 544bbeeaf836436305cbed87ae1019511de62535 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 28 Aug 2022 17:03:38 +0200 Subject: feat: Add init source code For now only `pipe` is feature complete. The order module needs to be changed a lot. --- pipe/pipe_test.go | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 pipe/pipe_test.go (limited to 'pipe/pipe_test.go') diff --git a/pipe/pipe_test.go b/pipe/pipe_test.go new file mode 100644 index 0000000..e71b05b --- /dev/null +++ b/pipe/pipe_test.go @@ -0,0 +1,117 @@ +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 { + return in * Multiplier +} + +func devide(in int) int { + return in / Multiplier +} + +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 { + in.value = in.value * Multiplier + return in +} + +func devideBuffer(in *buffer) { + in.value = in.value / Multiplier +} + +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) + } + } +} -- cgit v1.2.3