浏览代码

Shorter names for constants which hold the number of values in UT

Stéphane Raimbault 14 年之前
父节点
当前提交
f2f42c01a9
共有 3 个文件被更改,包括 65 次插入89 次删除
  1. 52 76
      tests/unit-test-client.c
  2. 8 8
      tests/unit-test-server.c
  3. 5 5
      tests/unit-test.h

+ 52 - 76
tests/unit-test-client.c

@@ -87,15 +87,13 @@ int main(int argc, char *argv[])
     }
 
     /* Allocate and initialize the memory to store the bits */
-    nb_points = (UT_BITS_NB_POINTS > UT_INPUT_BITS_NB_POINTS) ?
-        UT_BITS_NB_POINTS : UT_INPUT_BITS_NB_POINTS;
+    nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
     tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
     memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
 
     /* Allocate and initialize the memory to store the registers */
-    nb_points = (UT_REGISTERS_NB_POINTS >
-                 UT_INPUT_REGISTERS_NB_POINTS) ?
-        UT_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
+    nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
+        UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
     tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
     memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
 
@@ -131,14 +129,13 @@ int main(int argc, char *argv[])
 
     /* Multiple bits */
     {
-        uint8_t tab_value[UT_BITS_NB_POINTS];
+        uint8_t tab_value[UT_BITS_NB];
 
-        modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB_POINTS,
-                                   UT_BITS_TAB);
+        modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB);
         rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
-                               UT_BITS_NB_POINTS, tab_value);
+                               UT_BITS_NB, tab_value);
         printf("1/2 modbus_write_bits: ");
-        if (rc == UT_BITS_NB_POINTS) {
+        if (rc == UT_BITS_NB) {
             printf("OK\n");
         } else {
             printf("FAILED\n");
@@ -146,24 +143,22 @@ int main(int argc, char *argv[])
         }
     }
 
-    rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
-                          UT_BITS_NB_POINTS, tab_rp_bits);
+    rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
     printf("2/2 modbus_read_bits: ");
-    if (rc != UT_BITS_NB_POINTS) {
+    if (rc != UT_BITS_NB) {
         printf("FAILED (nb points %d)\n", rc);
         goto close;
     }
 
     i = 0;
     address = UT_BITS_ADDRESS;
-    nb_points = UT_BITS_NB_POINTS;
+    nb_points = UT_BITS_NB;
     while (nb_points > 0) {
         int nb_bits = (nb_points > 8) ? 8 : nb_points;
 
         value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
         if (value != UT_BITS_TAB[i]) {
-            printf("FAILED (%0X != %0X)\n",
-                   value, UT_BITS_TAB[i]);
+            printf("FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
             goto close;
         }
 
@@ -175,24 +170,23 @@ int main(int argc, char *argv[])
 
     /** DISCRETE INPUTS **/
     rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
-                                UT_INPUT_BITS_NB_POINTS, tab_rp_bits);
+                                UT_INPUT_BITS_NB, tab_rp_bits);
     printf("1/1 modbus_read_input_bits: ");
 
-    if (rc != UT_INPUT_BITS_NB_POINTS) {
+    if (rc != UT_INPUT_BITS_NB) {
         printf("FAILED (nb points %d)\n", rc);
         goto close;
     }
 
     i = 0;
     address = UT_INPUT_BITS_ADDRESS;
-    nb_points = UT_INPUT_BITS_NB_POINTS;
+    nb_points = UT_INPUT_BITS_NB;
     while (nb_points > 0) {
         int nb_bits = (nb_points > 8) ? 8 : nb_points;
 
         value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
         if (value != UT_INPUT_BITS_TAB[i]) {
-            printf("FAILED (%0X != %0X)\n",
-                   value, UT_INPUT_BITS_TAB[i]);
+            printf("FAILED (%0X != %0X)\n", value, UT_INPUT_BITS_TAB[i]);
             goto close;
         }
 
@@ -231,10 +225,9 @@ int main(int argc, char *argv[])
 
     /* Many registers */
     rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
-                                UT_REGISTERS_NB_POINTS,
-                                UT_REGISTERS_TAB);
+                                UT_REGISTERS_NB, UT_REGISTERS_TAB);
     printf("1/5 modbus_write_registers: ");
-    if (rc == UT_REGISTERS_NB_POINTS) {
+    if (rc == UT_REGISTERS_NB) {
         printf("OK\n");
     } else {
         printf("FAILED\n");
@@ -242,15 +235,14 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_POINTS,
-                               tab_rp_registers);
+                               UT_REGISTERS_NB, tab_rp_registers);
     printf("2/5 modbus_read_registers: ");
-    if (rc != UT_REGISTERS_NB_POINTS) {
+    if (rc != UT_REGISTERS_NB) {
         printf("FAILED (nb points %d)\n", rc);
         goto close;
     }
 
-    for (i=0; i < UT_REGISTERS_NB_POINTS; i++) {
+    for (i=0; i < UT_REGISTERS_NB; i++) {
         if (tab_rp_registers[i] != UT_REGISTERS_TAB[i]) {
             printf("FAILED (%0X != %0X)\n",
                    tab_rp_registers[i],
@@ -269,24 +261,23 @@ int main(int argc, char *argv[])
     }
     printf("OK\n");
 
-    nb_points = (UT_REGISTERS_NB_POINTS >
-                 UT_INPUT_REGISTERS_NB_POINTS) ?
-        UT_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
+    nb_points = (UT_REGISTERS_NB >
+                 UT_INPUT_REGISTERS_NB) ?
+        UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
     memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
 
     /* Write registers to zero from tab_rp_registers and store read registers
        into tab_rp_registers. So the read registers must set to 0, except the
        first one because there is an offset of 1 register on write. */
     rc = modbus_read_and_write_registers(ctx,
-                                         UT_REGISTERS_ADDRESS,
-                                         UT_REGISTERS_NB_POINTS,
+                                         UT_REGISTERS_ADDRESS, UT_REGISTERS_NB,
                                          tab_rp_registers,
                                          UT_REGISTERS_ADDRESS + 1,
-                                         UT_REGISTERS_NB_POINTS - 1,
+                                         UT_REGISTERS_NB - 1,
                                          tab_rp_registers);
     printf("4/5 modbus_read_and_write_registers: ");
-    if (rc != UT_REGISTERS_NB_POINTS) {
-        printf("FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB_POINTS);
+    if (rc != UT_REGISTERS_NB) {
+        printf("FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
         goto close;
     }
 
@@ -295,7 +286,7 @@ int main(int argc, char *argv[])
                tab_rp_registers[0], UT_REGISTERS_TAB[0]);
     }
 
-    for (i=1; i < UT_REGISTERS_NB_POINTS; i++) {
+    for (i=1; i < UT_REGISTERS_NB; i++) {
         if (tab_rp_registers[i] != 0) {
             printf("FAILED (%0X != %0X)\n",
                    tab_rp_registers[i], 0);
@@ -309,15 +300,14 @@ int main(int argc, char *argv[])
 
     /** INPUT REGISTERS **/
     rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
-                                     UT_INPUT_REGISTERS_NB_POINTS,
-                                     tab_rp_registers);
+                                     UT_INPUT_REGISTERS_NB, tab_rp_registers);
     printf("1/1 modbus_read_input_registers: ");
-    if (rc != UT_INPUT_REGISTERS_NB_POINTS) {
+    if (rc != UT_INPUT_REGISTERS_NB) {
         printf("FAILED (nb points %d)\n", rc);
         goto close;
     }
 
-    for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
+    for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
         if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
             printf("FAILED (%0X != %0X)\n",
                    tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
@@ -357,8 +347,7 @@ int main(int argc, char *argv[])
      * the addresses are not valid. */
 
     rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
-                          UT_BITS_NB_POINTS + 1,
-                          tab_rp_bits);
+                          UT_BITS_NB + 1, tab_rp_bits);
     printf("* modbus_read_bits: ");
     if (rc == -1 && errno == EMBXILADD) {
         printf("OK\n");
@@ -368,8 +357,7 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
-                                UT_INPUT_BITS_NB_POINTS + 1,
-                                tab_rp_bits);
+                                UT_INPUT_BITS_NB + 1, tab_rp_bits);
     printf("* modbus_read_input_bits: ");
     if (rc == -1 && errno == EMBXILADD)
         printf("OK\n");
@@ -379,8 +367,7 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_POINTS + 1,
-                               tab_rp_registers);
+                               UT_REGISTERS_NB + 1, tab_rp_registers);
     printf("* modbus_read_registers: ");
     if (rc == -1 && errno == EMBXILADD)
         printf("OK\n");
@@ -390,8 +377,7 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
-                                     UT_INPUT_REGISTERS_NB_POINTS + 1,
-                                     tab_rp_registers);
+                                     UT_INPUT_REGISTERS_NB + 1, tab_rp_registers);
     printf("* modbus_read_input_registers: ");
     if (rc == -1 && errno == EMBXILADD)
         printf("OK\n");
@@ -400,7 +386,7 @@ int main(int argc, char *argv[])
         goto close;
     }
 
-    rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB_POINTS, ON);
+    rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
     printf("* modbus_write_bit: ");
     if (rc == -1 && errno == EMBXILADD) {
         printf("OK\n");
@@ -409,9 +395,8 @@ int main(int argc, char *argv[])
         goto close;
     }
 
-    rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
-                           UT_BITS_NB_POINTS,
-                           tab_rp_bits);
+    rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
+                           UT_BITS_NB, tab_rp_bits);
     printf("* modbus_write_coils: ");
     if (rc == -1 && errno == EMBXILADD) {
         printf("OK\n");
@@ -420,10 +405,8 @@ int main(int argc, char *argv[])
         goto close;
     }
 
-    rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS +
-                                UT_REGISTERS_NB_POINTS,
-                                UT_REGISTERS_NB_POINTS,
-                                tab_rp_registers);
+    rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
+                                UT_REGISTERS_NB, tab_rp_registers);
     printf("* modbus_write_registers: ");
     if (rc == -1 && errno == EMBXILADD) {
         printf("OK\n");
@@ -436,8 +419,8 @@ int main(int argc, char *argv[])
     /** TOO MANY DATA **/
     printf("\nTEST TOO MANY DATA ERROR:\n");
 
-    rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1,
-                          tab_rp_bits);
+    rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
+                          MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
     printf("* modbus_read_bits: ");
     if (rc == -1 && errno == EMBMDATA) {
         printf("OK\n");
@@ -447,8 +430,7 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
-                                MODBUS_MAX_READ_BITS + 1,
-                                tab_rp_bits);
+                                MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
     printf("* modbus_read_input_bits: ");
     if (rc == -1 && errno == EMBMDATA) {
         printf("OK\n");
@@ -480,8 +462,7 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
-                           MODBUS_MAX_WRITE_BITS + 1,
-                           tab_rp_bits);
+                           MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
     printf("* modbus_write_bits: ");
     if (rc == -1 && errno == EMBMDATA) {
         printf("OK\n");
@@ -505,8 +486,7 @@ int main(int argc, char *argv[])
     printf("\nTEST SLAVE REPLY:\n");
     modbus_set_slave(ctx, 18);
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_POINTS,
-                               tab_rp_registers);
+                               UT_REGISTERS_NB, tab_rp_registers);
     if (use_backend == RTU) {
         /* No response in RTU mode */
         printf("1/4 No response from slave %d: ", 18);
@@ -521,7 +501,7 @@ int main(int argc, char *argv[])
         /* Response in TCP mode */
         printf("1/4 Response from slave %d: ", 18);
 
-        if (rc == UT_REGISTERS_NB_POINTS) {
+        if (rc == UT_REGISTERS_NB) {
             printf("OK\n");
         } else {
             printf("FAILED\n");
@@ -536,10 +516,9 @@ int main(int argc, char *argv[])
     }
 
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_POINTS,
-                               tab_rp_registers);
+                               UT_REGISTERS_NB, tab_rp_registers);
     printf("2/4 Reply after a broadcast query: ");
-    if (rc == UT_REGISTERS_NB_POINTS) {
+    if (rc == UT_REGISTERS_NB) {
         printf("OK\n");
     } else {
         printf("FAILED\n");
@@ -578,8 +557,7 @@ int main(int argc, char *argv[])
     modbus_set_timeout_begin(ctx, &timeout_begin_new);
 
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_POINTS,
-                               tab_rp_registers);
+                               UT_REGISTERS_NB, tab_rp_registers);
     printf("4/4 Too short timeout: ");
     if (rc == -1 && errno == ETIMEDOUT) {
         printf("OK\n");
@@ -600,10 +578,9 @@ int main(int argc, char *argv[])
 
     /* Allocate only the required space */
     tab_rp_registers_bad = (uint16_t *) malloc(
-        UT_REGISTERS_NB_POINTS_SPECIAL * sizeof(uint16_t));
+        UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_POINTS_SPECIAL,
-                               tab_rp_registers_bad);
+                               UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
     printf("* modbus_read_registers: ");
     if (rc == -1 && errno == EMBBADDATA) {
         printf("OK\n");
@@ -618,8 +595,7 @@ int main(int argc, char *argv[])
     printf("\nTEST MANUAL EXCEPTION:\n");
 
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
-                               UT_REGISTERS_NB_POINTS,
-                               tab_rp_registers);
+                               UT_REGISTERS_NB, tab_rp_registers);
     printf("* modbus_read_registers at special address: ");
     if (rc == -1 && errno == EMBXSBUSY) {
         printf("OK\n");

+ 8 - 8
tests/unit-test-server.c

@@ -74,10 +74,10 @@ int main(int argc, char*argv[])
     modbus_set_error_recovery(ctx, TRUE);
 
     mb_mapping = modbus_mapping_new(
-        UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
-        UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB_POINTS,
-        UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_POINTS,
-        UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
+        UT_BITS_ADDRESS + UT_BITS_NB,
+        UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
+        UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
+        UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
     if (mb_mapping == NULL) {
         fprintf(stderr, "Failed to allocate the mapping: %s\n",
                 modbus_strerror(errno));
@@ -90,11 +90,11 @@ int main(int argc, char*argv[])
 
     /** INPUT STATUS **/
     modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
-                               UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB_POINTS,
+                               UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
                                UT_INPUT_BITS_TAB);
 
     /** INPUT REGISTERS **/
-    for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
+    for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
         mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
             UT_INPUT_REGISTERS_TAB[i];;
     }
@@ -124,10 +124,10 @@ int main(int argc, char*argv[])
         /* Read holding registers */
         if (query[header_length] == 0x03) {
             if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
-                == UT_REGISTERS_NB_POINTS_SPECIAL) {
+                == UT_REGISTERS_NB_SPECIAL) {
                 printf("Set an incorrect number of values\n");
                 MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
-                                         UT_REGISTERS_NB_POINTS);
+                                         UT_REGISTERS_NB);
             } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                 == UT_REGISTERS_ADDRESS_SPECIAL) {
                 printf("Reply to this special register address by an exception\n");

+ 5 - 5
tests/unit-test.h

@@ -34,25 +34,25 @@
 #define SERVER_ID 17
 
 const uint16_t UT_BITS_ADDRESS = 0x13;
-const uint16_t UT_BITS_NB_POINTS = 0x25;
+const uint16_t UT_BITS_NB = 0x25;
 const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B };
 
 const uint16_t UT_INPUT_BITS_ADDRESS = 0xC4;
-const uint16_t UT_INPUT_BITS_NB_POINTS = 0x16;
+const uint16_t UT_INPUT_BITS_NB = 0x16;
 const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 };
 
 const uint16_t UT_REGISTERS_ADDRESS = 0x6B;
 /* Raise a manual exception when this adress is used for the first byte */
 const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x6C;
-const uint16_t UT_REGISTERS_NB_POINTS = 0x3;
+const uint16_t UT_REGISTERS_NB = 0x3;
 const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 };
 /* If the following value is used, a bad response is sent.
    It's better to test with a lower value than
    UT_REGISTERS_NB_POINTS to try to raise a segfault. */
-const uint16_t UT_REGISTERS_NB_POINTS_SPECIAL = 0x2;
+const uint16_t UT_REGISTERS_NB_SPECIAL = 0x2;
 
 const uint16_t UT_INPUT_REGISTERS_ADDRESS = 0x08;
-const uint16_t UT_INPUT_REGISTERS_NB_POINTS = 0x1;
+const uint16_t UT_INPUT_REGISTERS_NB = 0x1;
 const uint16_t UT_INPUT_REGISTERS_TAB[] = { 0x000A };
 
 const float UT_REAL = 916.540649;