diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-08-28 17:03:38 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-08-28 17:07:40 +0200 |
commit | 544bbeeaf836436305cbed87ae1019511de62535 (patch) | |
tree | 8c7b020bba75d4d850d4a1d538e7bc7db2f01c33 /pipe/pipe_test.go | |
download | porg-544bbeeaf836436305cbed87ae1019511de62535.tar.gz porg-544bbeeaf836436305cbed87ae1019511de62535.tar.bz2 porg-544bbeeaf836436305cbed87ae1019511de62535.zip |
feat: Add init source code
For now only `pipe` is feature complete. The order module needs to be
changed a lot.
Diffstat (limited to 'pipe/pipe_test.go')
-rw-r--r-- | pipe/pipe_test.go | 117 |
1 files changed, 117 insertions, 0 deletions
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) + } + } +} |