parent
4b952c945c
commit
db0f786777
@ -0,0 +1,26 @@ |
|||||||
|
package hal |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
) |
||||||
|
|
||||||
|
func NewCommandLineIO(index int) *CommandLineIO { |
||||||
|
return &CommandLineIO{ |
||||||
|
index: index, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
type CommandLineIO struct { |
||||||
|
index int |
||||||
|
} |
||||||
|
|
||||||
|
func (io *CommandLineIO) Read() (result float64, err error) { |
||||||
|
fmt.Printf("Input[%d]: ", io.index) |
||||||
|
_, err = fmt.Scanf("%f", &result) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
func (io *CommandLineIO) Write(output float64) error { |
||||||
|
fmt.Printf("Output[%d]: %f\n", io.index, output) |
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
package hal |
||||||
|
|
||||||
|
import "fmt" |
||||||
|
|
||||||
|
func NewMockIO(inputs ...float64) *MockIO { |
||||||
|
return &MockIO{ |
||||||
|
Inputs: inputs, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
type MockIO struct { |
||||||
|
currentInputIndex int |
||||||
|
Inputs []float64 |
||||||
|
Outputs []float64 |
||||||
|
} |
||||||
|
|
||||||
|
func (io *MockIO) increaseInputIndex() { |
||||||
|
io.currentInputIndex++ |
||||||
|
} |
||||||
|
|
||||||
|
func (io *MockIO) Read() (float64, error) { |
||||||
|
if len(io.Inputs) <= io.currentInputIndex { |
||||||
|
return 0, fmt.Errorf("no mock data for index %d", io.currentInputIndex) |
||||||
|
} |
||||||
|
defer io.increaseInputIndex() |
||||||
|
return io.Inputs[io.currentInputIndex], nil |
||||||
|
} |
||||||
|
|
||||||
|
func (io *MockIO) Write(output float64) error { |
||||||
|
io.Outputs = append(io.Outputs, output) |
||||||
|
return nil |
||||||
|
} |
Loading…
Reference in new issue