-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata_test.go
92 lines (83 loc) · 2.44 KB
/
data_test.go
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
// Copyright 2019 Sergey Soldatov. All rights reserved.
// This software may be modified and distributed under the terms
// of the Apache license. See the LICENSE file for details.
package modbus
import (
"sync"
"testing"
)
type testcheckOutsidepair struct {
addr, cnt uint16
datasize int
res bool
}
var testscheckOutside = []testcheckOutsidepair{
{0, 2, 100, true},
{0, 3, 200, true},
{0, 4, 3, false},
}
func TestModbusData_checkOutside(t *testing.T) {
md := &ModbusData{
coils: []bool{false, false, false},
discrete_inputs: []bool{false, false, false},
holding_reg: []uint16{0, 0, 0},
input_reg: []uint16{0, 0, 0}}
for _, pair := range testscheckOutside {
res, _ := md.isNotOutside(pair.addr, pair.cnt, pair.datasize)
if res != pair.res {
t.Error(
"For datasize", pair.datasize, "addr=", pair.addr, "cnt=", pair.cnt,
"expected", pair.res,
"got", res,
)
}
}
}
func TestModbusData_PresetMultipleRegisters(t *testing.T) {
test_addr := uint16(5)
test_data := []uint16{10, 20, 30}
md := &ModbusData{holding_reg: make([]uint16, 10), mu_holding_regs: &sync.Mutex{}}
md.PresetMultipleRegisters(test_addr, test_data...)
for i, v := range test_data {
if v != md.holding_reg[test_addr+uint16(i)] {
t.Error("Expected", test_data[i], "got", md.holding_reg[test_addr+uint16(i)])
}
}
}
func TestModbusData_ForceMultipleCoils(t *testing.T) {
test_addr := uint16(5)
test_data := []bool{true, false, true}
md := &ModbusData{coils: make([]bool, 10), mu_coils: &sync.Mutex{}}
md.ForceMultipleCoils(test_addr, test_data...)
for i, v := range test_data {
if v != md.coils[test_addr+uint16(i)] {
t.Error("Expected", test_data[i], "got", md.holding_reg[test_addr+uint16(i)])
}
}
}
func TestModbusData_ReadHoldingRegisters(t *testing.T) {
test_addr := uint16(0)
test_data := []uint16{10, 20, 30}
test_cnt := uint16(3)
md := new(ModbusData)
md.holding_reg = test_data
res_data, _ := md.ReadHoldingRegisters(test_addr, test_cnt)
for i, v := range test_data {
if v != res_data[i] {
t.Error("Expected", test_data[i], "got", res_data[i])
}
}
}
func TestModbusData_ReadCoilStatus(t *testing.T) {
test_addr := uint16(0)
test_data := []bool{true, false, true}
test_cnt := uint16(3)
md := new(ModbusData)
md.coils = test_data
res_data, _ := md.ReadCoilStatus(test_addr, test_cnt)
for i, v := range test_data {
if v != res_data[i] {
t.Error("Expected", test_data[i], "got", res_data[i])
}
}
}