Skip to content

Commit

Permalink
test lockedModel
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 7, 2023
1 parent 6a238f3 commit 4dcc37d
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions peamodbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

func TestReadHoldingRegisters_loopback(t *testing.T) {
model := &BlockedModel{}
var model DataModel = &BlockedModel{}
model = ConcurrencySafeDataModel(model)
model.SetHoldingRegister(0, 0)
var reqBuf, responseBuf [256]byte
tx := &Tx{}
Expand Down Expand Up @@ -293,18 +294,19 @@ func TestInferTxRequestLength(t *testing.T) {

func TestDataInterpreter_inverted(t *testing.T) {
data := &BlockedModel{}
model := ConcurrencySafeDataModel(data)
interpret, _ := NewDataInterpreter(DataInterpreterConfig{
InvertWordOrder: true,
})
rng := rand.New(rand.NewSource(1))
var buf [8]byte
for addr := 0; addr < 122; addr++ {
f32 := rng.Float32()
exc := interpret.PutFloat32Holding(data, addr, f32)
exc := interpret.PutFloat32Holding(model, addr, f32)
if exc != 0 {
t.Fatal("putf32", addr, exc)
}
got, exc := interpret.Float32Holding(data, addr)
got, exc := interpret.Float32Holding(model, addr)
if exc != 0 {
t.Fatal("getf32", addr, exc)
}
Expand All @@ -314,18 +316,18 @@ func TestDataInterpreter_inverted(t *testing.T) {

// 64 bit test.
f64 := rng.Float64()
exc = interpret.PutFloat64Holding(data, addr, f64)
exc = interpret.PutFloat64Holding(model, addr, f64)
if exc != 0 {
t.Fatal("putf64", addr, exc)
}
got64, exc := interpret.Float64Holding(data, addr)
got64, exc := interpret.Float64Holding(model, addr)
if exc != 0 {
t.Fatal("getf64", addr, exc)
}
if got64 != f64 {
t.Fatalf("read back fail: got=%x want=%x", math.Float64bits(got64), math.Float64bits(f64))
}
n, exc := interpret.ReadBytesHolding(data, buf[:8], addr)
n, exc := interpret.ReadBytesHolding(model, buf[:8], addr)
if n != 8 || exc != ExceptionNone {
t.Error("ReadBytesHolding failed", n, exc)
}
Expand All @@ -334,11 +336,11 @@ func TestDataInterpreter_inverted(t *testing.T) {
t.Fatalf("ReadBytesInput: input read back fail: got=%x want=%x", math.Float64bits(got64), math.Float64bits(f64))
}
buf = [8]byte{}
n, exc = interpret.WriteBytesHolding(data, buf[:8], addr)
n, exc = interpret.WriteBytesHolding(model, buf[:8], addr)
if n != 8 || exc != ExceptionNone {
t.Error("WriteBytesInput failed")
}
got64, exc = interpret.Float64Holding(data, addr)
got64, exc = interpret.Float64Holding(model, addr)
if exc != ExceptionNone {
t.Fatal("unexpected exception")
}
Expand All @@ -350,11 +352,11 @@ func TestDataInterpreter_inverted(t *testing.T) {
data = &BlockedModel{} // Reset model for input test.
for addr := 0; addr < 122; addr++ {
f32 := rng.Float32()
exc := interpret.PutFloat32Input(data, addr, f32)
exc := interpret.PutFloat32Input(model, addr, f32)
if exc != 0 {
t.Fatal("input putf32", addr, exc)
}
got, exc := interpret.Float32Input(data, addr)
got, exc := interpret.Float32Input(model, addr)
if exc != 0 {
t.Fatal("input getf32", addr, exc)
}
Expand All @@ -364,19 +366,19 @@ func TestDataInterpreter_inverted(t *testing.T) {

// 64 bit test.
f64 := rng.Float64()
exc = interpret.PutFloat64Input(data, addr, f64)
exc = interpret.PutFloat64Input(model, addr, f64)
if exc != 0 {
t.Fatal("input putf64", addr, exc)
}
got64, exc := interpret.Float64Input(data, addr)
got64, exc := interpret.Float64Input(model, addr)
if exc != 0 {
t.Fatal("input getf64", addr, exc)
}
if got64 != f64 {
t.Fatalf("input read back fail: got=%x want=%x", math.Float64bits(got64), math.Float64bits(f64))
}

n, exc := interpret.ReadBytesInput(data, buf[:8], addr)
n, exc := interpret.ReadBytesInput(model, buf[:8], addr)
if n != 8 || exc != ExceptionNone {
t.Error("ReadBytesInput failed")
}
Expand All @@ -385,11 +387,11 @@ func TestDataInterpreter_inverted(t *testing.T) {
t.Fatalf("ReadBytesInput: input read back fail: got=%x want=%x", math.Float64bits(got64), math.Float64bits(f64))
}
buf = [8]byte{}
n, exc = interpret.WriteBytesInput(data, buf[:8], addr)
n, exc = interpret.WriteBytesInput(model, buf[:8], addr)
if n != 8 || exc != ExceptionNone {
t.Error("WriteBytesInput failed", n, exc)
}
got64, exc = interpret.Float64Input(data, addr)
got64, exc = interpret.Float64Input(model, addr)
if exc != ExceptionNone {
t.Fatal("unexpected exception")
}
Expand All @@ -401,18 +403,19 @@ func TestDataInterpreter_inverted(t *testing.T) {

func TestDataInterpreter_notinverted(t *testing.T) {
data := &BlockedModel{}
model := ConcurrencySafeDataModel(data)
interpret, _ := NewDataInterpreter(DataInterpreterConfig{
InvertWordOrder: false,
})
rng := rand.New(rand.NewSource(1))
var buf [8]byte
for addr := 0; addr < 122; addr++ {
f32 := rng.Float32()
exc := interpret.PutFloat32Holding(data, addr, f32)
exc := interpret.PutFloat32Holding(model, addr, f32)
if exc != 0 {
t.Fatal("putf32", addr, exc)
}
got, exc := interpret.Float32Holding(data, addr)
got, exc := interpret.Float32Holding(model, addr)
if exc != 0 {
t.Fatal("getf32", addr, exc)
}
Expand All @@ -422,23 +425,23 @@ func TestDataInterpreter_notinverted(t *testing.T) {

// 64 bit test.
f64 := rng.Float64()
exc = interpret.PutFloat64Holding(data, addr, f64)
exc = interpret.PutFloat64Holding(model, addr, f64)
if exc != 0 {
t.Fatal("putf64", addr, exc)
}
got64, exc := interpret.Float64Holding(data, addr)
got64, exc := interpret.Float64Holding(model, addr)
if exc != 0 {
t.Fatal("getf64", addr, exc)
}
if got64 != f64 {
t.Fatalf("read back fail: got=%x want=%x", math.Float64bits(got64), math.Float64bits(f64))
}

n, exc := interpret.WriteBytesHolding(data, buf[:8], addr)
n, exc := interpret.WriteBytesHolding(model, buf[:8], addr)
if n != 8 || exc != ExceptionNone {
t.Error("WriteBytesInput failed")
}
got64, exc = interpret.Float64Holding(data, addr)
got64, exc = interpret.Float64Holding(model, addr)
if exc != ExceptionNone {
t.Fatal("unexpected exception")
}
Expand All @@ -450,11 +453,11 @@ func TestDataInterpreter_notinverted(t *testing.T) {
data = &BlockedModel{} // Reset model for input test.
for addr := 0; addr < 122; addr++ {
f32 := rng.Float32()
exc := interpret.PutFloat32Input(data, addr, f32)
exc := interpret.PutFloat32Input(model, addr, f32)
if exc != 0 {
t.Fatal("input putf32", addr, exc)
}
got, exc := interpret.Float32Input(data, addr)
got, exc := interpret.Float32Input(model, addr)
if exc != 0 {
t.Fatal("input getf32", addr, exc)
}
Expand All @@ -464,23 +467,23 @@ func TestDataInterpreter_notinverted(t *testing.T) {

// 64 bit test.
f64 := rng.Float64()
exc = interpret.PutFloat64Input(data, addr, f64)
exc = interpret.PutFloat64Input(model, addr, f64)
if exc != 0 {
t.Fatal("input putf64", addr, exc)
}
got64, exc := interpret.Float64Input(data, addr)
got64, exc := interpret.Float64Input(model, addr)
if exc != 0 {
t.Fatal("input getf64", addr, exc)
}
if got64 != f64 {
t.Fatalf("input read back fail: got=%x want=%x", math.Float64bits(got64), math.Float64bits(f64))
}

n, exc := interpret.WriteBytesInput(data, buf[:8], addr)
n, exc := interpret.WriteBytesInput(model, buf[:8], addr)
if n != 8 || exc != ExceptionNone {
t.Error("WriteBytesInput failed", n, exc)
}
got64, exc = interpret.Float64Input(data, addr)
got64, exc = interpret.Float64Input(model, addr)
if exc != ExceptionNone {
t.Fatal("unexpected exception")
}
Expand Down

0 comments on commit 4dcc37d

Please sign in to comment.