Commit d11d30a50af5ee688966fcbca525a804dfa6cf94
1 parent
1a9f8867
Better unit tests.
- The coil and registers are wrote by the master and not assigned by the slave anymore - The data used to write are the ones in unit-test.h - The read test is done after the write test
Showing
2 changed files
with
98 additions
and
68 deletions
tests/unit-test-master.c
| ... | ... | @@ -65,12 +65,57 @@ int main(void) |
| 65 | 65 | |
| 66 | 66 | printf("** UNIT TESTING **\n"); |
| 67 | 67 | |
| 68 | - printf("TEST READ FUNCTIONS:\n"); | |
| 68 | + printf("\nTEST WRITE/READ:\n"); | |
| 69 | 69 | |
| 70 | 70 | /** COIL STATUS **/ |
| 71 | + | |
| 72 | + /* Single */ | |
| 73 | + ret = force_single_coil(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, ON); | |
| 74 | + printf("1/2 force_single_coil: "); | |
| 75 | + if (ret == 1) { | |
| 76 | + printf("OK\n"); | |
| 77 | + } else { | |
| 78 | + printf("FAILED\n"); | |
| 79 | + goto close; | |
| 80 | + } | |
| 81 | + | |
| 82 | + ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, 1, | |
| 83 | + tab_rp_status); | |
| 84 | + printf("2/2 read_coil_status: "); | |
| 85 | + if (ret != 1) { | |
| 86 | + printf("FAILED (nb points %d)\n", ret); | |
| 87 | + goto close; | |
| 88 | + } | |
| 89 | + | |
| 90 | + if (tab_rp_status[0] != ON) { | |
| 91 | + printf("FAILED (%0X = != %0X)\n", tab_rp_status[0], ON); | |
| 92 | + goto close; | |
| 93 | + } | |
| 94 | + printf("OK\n"); | |
| 95 | + /* End single */ | |
| 96 | + | |
| 97 | + /* Multiple coils */ | |
| 98 | + { | |
| 99 | + uint8_t tab_value[UT_COIL_STATUS_NB_POINTS]; | |
| 100 | + | |
| 101 | + set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS, | |
| 102 | + UT_COIL_STATUS_TAB); | |
| 103 | + ret = force_multiple_coils(&mb_param, SLAVE, | |
| 104 | + UT_COIL_STATUS_ADDRESS, | |
| 105 | + UT_COIL_STATUS_NB_POINTS, | |
| 106 | + tab_value); | |
| 107 | + printf("1/2 force_multiple_coils: "); | |
| 108 | + if (ret == UT_COIL_STATUS_NB_POINTS) { | |
| 109 | + printf("OK\n"); | |
| 110 | + } else { | |
| 111 | + printf("FAILED\n"); | |
| 112 | + goto close; | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 71 | 116 | ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, |
| 72 | 117 | UT_COIL_STATUS_NB_POINTS, tab_rp_status); |
| 73 | - printf("* coil status: "); | |
| 118 | + printf("2/2 read_coil_status: "); | |
| 74 | 119 | if (ret != UT_COIL_STATUS_NB_POINTS) { |
| 75 | 120 | printf("FAILED (nb points %d)\n", ret); |
| 76 | 121 | goto close; |
| ... | ... | @@ -93,11 +138,12 @@ int main(void) |
| 93 | 138 | i++; |
| 94 | 139 | } |
| 95 | 140 | printf("OK\n"); |
| 141 | + /* End of multiple coils */ | |
| 96 | 142 | |
| 97 | 143 | /** INPUT STATUS **/ |
| 98 | 144 | ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS, |
| 99 | 145 | UT_INPUT_STATUS_NB_POINTS, tab_rp_status); |
| 100 | - printf("* read_input_status: "); | |
| 146 | + printf("1/1 read_input_status: "); | |
| 101 | 147 | |
| 102 | 148 | if (ret != UT_INPUT_STATUS_NB_POINTS) { |
| 103 | 149 | printf("FAILED (nb points %d)\n", ret); |
| ... | ... | @@ -123,11 +169,53 @@ int main(void) |
| 123 | 169 | printf("OK\n"); |
| 124 | 170 | |
| 125 | 171 | /** HOLDING REGISTERS **/ |
| 172 | + | |
| 173 | + /* Single register */ | |
| 174 | + ret = preset_single_register(&mb_param, SLAVE, | |
| 175 | + UT_HOLDING_REGISTERS_ADDRESS, 0x1234); | |
| 176 | + printf("1/2 preset_single_register: "); | |
| 177 | + if (ret == 1) { | |
| 178 | + printf("OK\n"); | |
| 179 | + } else { | |
| 180 | + printf("FAILED\n"); | |
| 181 | + goto close; | |
| 182 | + } | |
| 183 | + | |
| 184 | + ret = read_holding_registers(&mb_param, SLAVE, | |
| 185 | + UT_HOLDING_REGISTERS_ADDRESS, | |
| 186 | + 1, tab_rp_registers); | |
| 187 | + printf("2/2 read_holding_registers: "); | |
| 188 | + if (ret != 1) { | |
| 189 | + printf("FAILED (nb points %d)\n", ret); | |
| 190 | + goto close; | |
| 191 | + } | |
| 192 | + | |
| 193 | + if (tab_rp_registers[0] != 0x1234) { | |
| 194 | + printf("FAILED (%0X != %0X)\n", | |
| 195 | + tab_rp_registers[0], 0x1234); | |
| 196 | + goto close; | |
| 197 | + } | |
| 198 | + printf("OK\n"); | |
| 199 | + /* End of single register */ | |
| 200 | + | |
| 201 | + /* Many registers */ | |
| 202 | + ret = preset_multiple_registers(&mb_param, SLAVE, | |
| 203 | + UT_HOLDING_REGISTERS_ADDRESS, | |
| 204 | + UT_HOLDING_REGISTERS_NB_POINTS, | |
| 205 | + UT_HOLDING_REGISTERS_TAB); | |
| 206 | + printf("1/2 preset_multiple_registers: "); | |
| 207 | + if (ret == UT_HOLDING_REGISTERS_NB_POINTS) { | |
| 208 | + printf("OK\n"); | |
| 209 | + } else { | |
| 210 | + printf("FAILED\n"); | |
| 211 | + goto close; | |
| 212 | + } | |
| 213 | + | |
| 126 | 214 | ret = read_holding_registers(&mb_param, |
| 127 | 215 | SLAVE, UT_HOLDING_REGISTERS_ADDRESS, |
| 128 | 216 | UT_HOLDING_REGISTERS_NB_POINTS, |
| 129 | 217 | tab_rp_registers); |
| 130 | - printf("* read_holding_registers: "); | |
| 218 | + printf("2/2 read_holding_registers: "); | |
| 131 | 219 | if (ret != UT_HOLDING_REGISTERS_NB_POINTS) { |
| 132 | 220 | printf("FAILED (nb points %d)\n", ret); |
| 133 | 221 | goto close; |
| ... | ... | @@ -142,13 +230,15 @@ int main(void) |
| 142 | 230 | } |
| 143 | 231 | } |
| 144 | 232 | printf("OK\n"); |
| 233 | + /* End of many registers */ | |
| 234 | + | |
| 145 | 235 | |
| 146 | 236 | /** INPUT REGISTERS **/ |
| 147 | 237 | ret = read_input_registers(&mb_param, |
| 148 | 238 | SLAVE, UT_INPUT_REGISTERS_ADDRESS, |
| 149 | 239 | UT_INPUT_REGISTERS_NB_POINTS, |
| 150 | 240 | tab_rp_registers); |
| 151 | - printf("* read_input_registers: "); | |
| 241 | + printf("1/1 read_input_registers: "); | |
| 152 | 242 | if (ret != UT_INPUT_REGISTERS_NB_POINTS) { |
| 153 | 243 | printf("FAILED (nb points %d)\n", ret); |
| 154 | 244 | goto close; |
| ... | ... | @@ -162,57 +252,7 @@ int main(void) |
| 162 | 252 | } |
| 163 | 253 | } |
| 164 | 254 | printf("OK\n"); |
| 165 | - | |
| 166 | - /** WRITE FUNCTIONS **/ | |
| 167 | - printf("\nTEST WRITE FUNCTIONS:\n"); | |
| 168 | 255 | |
| 169 | - ret = force_single_coil(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS, ON); | |
| 170 | - printf("* force_single_coil: "); | |
| 171 | - if (ret == 1) { | |
| 172 | - printf("OK\n"); | |
| 173 | - } else { | |
| 174 | - printf("FAILED\n"); | |
| 175 | - goto close; | |
| 176 | - } | |
| 177 | - | |
| 178 | - ret = preset_single_register(&mb_param, SLAVE, 0x01, 0x03); | |
| 179 | - printf("* preset_single_register: "); | |
| 180 | - if (ret == 1) { | |
| 181 | - printf("OK\n"); | |
| 182 | - } else { | |
| 183 | - printf("FAILED\n"); | |
| 184 | - goto close; | |
| 185 | - } | |
| 186 | - | |
| 187 | - { | |
| 188 | - int nb_points = 0x0A; | |
| 189 | - uint8_t tab_value[] = { ON, OFF, ON, ON, | |
| 190 | - OFF, OFF, ON, ON, | |
| 191 | - ON, OFF }; | |
| 192 | - ret = force_multiple_coils(&mb_param, SLAVE, 0x13, | |
| 193 | - nb_points, tab_value); | |
| 194 | - printf("* force_multiple_coils: "); | |
| 195 | - if (ret == nb_points) { | |
| 196 | - printf("OK\n"); | |
| 197 | - } else { | |
| 198 | - printf("FAILED\n"); | |
| 199 | - goto close; | |
| 200 | - } | |
| 201 | - } | |
| 202 | - | |
| 203 | - { | |
| 204 | - int nb_points = 2; | |
| 205 | - uint16_t tab_value[] = { 0x000A, 0x0102 }; | |
| 206 | - ret = preset_multiple_registers(&mb_param, SLAVE, | |
| 207 | - 0x01, nb_points, tab_value); | |
| 208 | - printf("* preset_multiple_registers: "); | |
| 209 | - if (ret == nb_points) { | |
| 210 | - printf("OK\n"); | |
| 211 | - } else { | |
| 212 | - printf("FAILED\n"); | |
| 213 | - goto close; | |
| 214 | - } | |
| 215 | - } | |
| 216 | 256 | |
| 217 | 257 | /** ILLEGAL DATA ADDRESS */ |
| 218 | 258 | printf("\nTEST ILLEGAL DATA ADDRESS:\n"); | ... | ... |
tests/unit-test-slave.c
| ... | ... | @@ -45,24 +45,14 @@ int main(void) |
| 45 | 45 | exit(1); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - /* Examples from PI_MODBUS_300.pdf */ | |
| 49 | - | |
| 50 | - /** COIL STATUS **/ | |
| 51 | - set_bits_from_bytes(mb_mapping.tab_coil_status, | |
| 52 | - UT_COIL_STATUS_ADDRESS, UT_COIL_STATUS_NB_POINTS, | |
| 53 | - UT_COIL_STATUS_TAB); | |
| 54 | - | |
| 48 | + /* Examples from PI_MODBUS_300.pdf. | |
| 49 | + Only the read-only input values are assigned. */ | |
| 50 | + | |
| 55 | 51 | /** INPUT STATUS **/ |
| 56 | 52 | set_bits_from_bytes(mb_mapping.tab_input_status, |
| 57 | 53 | UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS, |
| 58 | 54 | UT_INPUT_STATUS_TAB); |
| 59 | 55 | |
| 60 | - /** HOLDING REGISTERS **/ | |
| 61 | - for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) { | |
| 62 | - mb_mapping.tab_holding_registers[UT_HOLDING_REGISTERS_ADDRESS+i] = | |
| 63 | - UT_HOLDING_REGISTERS_TAB[i];; | |
| 64 | - } | |
| 65 | - | |
| 66 | 56 | /** INPUT REGISTERS **/ |
| 67 | 57 | for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) { |
| 68 | 58 | mb_mapping.tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] = | ... | ... |