<commit_before>/*
 * Copyright (C) 2001-2008 <NAME> <<EMAIL>>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <modbus/modbus.h>

#include "unit-test.h"

/* Tests based on PI-MBUS-300 documentation */
#define SLAVE    0x11

int main(void)
{
        uint8_t *tab_rp_status;
        uint16_t *tab_rp_registers;
        modbus_param_t mb_param;
        int i;
        uint8_t value;
        uint16_t address;
        uint16_t nb_points;

        /* RTU parity : none, even, odd */
/*      modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */

        /* TCP */
        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
        modbus_set_debug(&mb_param, TRUE);
      
        modbus_connect(&mb_param);

        /* Allocate and initialize the memory to store the status */
        nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
                UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
        tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
        memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
        
        /* Allocate and initialize the memory to store the registers */
        nb_points = (UT_HOLDING_REGISTERS_NB_POINTS > UT_INPUT_REGISTERS_NB_POINTS) ?
                UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
        tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
        memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));

        printf("Unit testing\n");

        /** COIL STATUS **/
        read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
                         UT_COIL_STATUS_NB_POINTS, tab_rp_status);

        i = 0;
        address = UT_COIL_STATUS_ADDRESS;
        nb_points = UT_COIL_STATUS_NB_POINTS;
        while (nb_points > 0) {
                int nb_bits = (nb_points > 8) ? 8 : nb_points;

                value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
                if (value != UT_COIL_STATUS_TAB[i]) {
                        printf("Coil status: FAILED (%0X != %0X)\n",
                               value, UT_COIL_STATUS_TAB[i]);
                        goto close;
                }

                nb_points -= nb_bits;
                i++;
        }
        printf("Coil status: OK\n");


        /** INPUT STATUS **/
        read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
                          UT_INPUT_STATUS_NB_POINTS, tab_rp_status);

        i = 0;
        address = UT_INPUT_STATUS_ADDRESS;
        nb_points = UT_INPUT_STATUS_NB_POINTS;
        while (nb_points > 0) {
                int nb_bits = (nb_points > 8) ? 8 : nb_points;

                value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
                if (value != UT_INPUT_STATUS_TAB[i]) {
                        printf("Input status: FAILED (%0X != %0X)\n",
                               value, UT_INPUT_STATUS_TAB[i]);
                        goto close;
                }

                nb_points -= nb_bits;
                i++;
        }
        printf("Input status: OK\n");

        /** HOLDING REGISTERS **/
        read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
                               UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
        for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
                if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
                        printf("Holding registers: FAILED (%0X != %0X)\n",
                               tab_rp_registers[i], UT_HOLDING_REGISTERS_TAB[i]);
                        goto close;
                }
        }
        printf("Holding registers: OK\n");

        /** INPUT REGISTERS **/
        read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
                               UT_INPUT_REGISTERS_NB_POINTS, tab_rp_registers);
        for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
                if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
                        printf("Input registers: FAILED (%0X != %0X)\n",
                               tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
                        goto close;
                }
        }
        printf("Input registers: OK\n");


        /** ILLEGAL DATA ADDRESSES */
        read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS + 1,
                         UT_COIL_STATUS_NB_POINTS, tab_rp_status);
        
close:
        /* Free the memory */
        free(tab_rp_status);                                           
        free(tab_rp_registers);

        /* Close the connection */
        modbus_close(&mb_param);
        
        return 0;
/*
        force_single_coil(&mb_param, SLAVE, 0xAC, ON);
*/
}<commit_msg>New tests<commit_after>/*
 * Copyright (C) 2001-2008 <NAME> <<EMAIL>>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <modbus/modbus.h>

#include "unit-test.h"

/* Tests based on PI-MBUS-300 documentation */
#define SLAVE    0x11

int main(void)
{
        uint8_t *tab_rp_status;
        uint16_t *tab_rp_registers;
        modbus_param_t mb_param;
        int i;
        uint8_t value;
        uint16_t address;
        uint16_t nb_points;
        int ret;

        /* RTU parity : none, even, odd */
/*      modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */

        /* TCP */
        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
//        modbus_set_debug(&mb_param, TRUE);
      
        modbus_connect(&mb_param);

        /* Allocate and initialize the memory to store the status */
        nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
                UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
        tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
        memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
        
        /* Allocate and initialize the memory to store the registers */
        nb_points = (UT_HOLDING_REGISTERS_NB_POINTS > UT_INPUT_REGISTERS_NB_POINTS) ?
                UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
        tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
        memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));

        printf("UNIT TESTING\n");

        printf("Test read functions:\n");

        /** COIL STATUS **/
        ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
                               UT_COIL_STATUS_NB_POINTS, tab_rp_status);
        printf("* coil status: ");
        if (ret != UT_COIL_STATUS_NB_POINTS) {
                printf("FAILED (nb points %d)\n", ret); 
                goto close;
        }

        i = 0;
        address = UT_COIL_STATUS_ADDRESS;
        nb_points = UT_COIL_STATUS_NB_POINTS;
        while (nb_points > 0) {
                int nb_bits = (nb_points > 8) ? 8 : nb_points;

                value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
                if (value != UT_COIL_STATUS_TAB[i]) {
                        printf("FAILED (%0X != %0X)\n",
                               value, UT_COIL_STATUS_TAB[i]);
                        goto close;
                }

                nb_points -= nb_bits;
                i++;
        }
        printf("OK\n");

        /** INPUT STATUS **/
        ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
                                UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
        printf("* input status :");

        if (ret != UT_INPUT_STATUS_NB_POINTS) {
                printf("FAILED (nb points %d)\n", ret); 
                goto close;
        }

        i = 0;
        address = UT_INPUT_STATUS_ADDRESS;
        nb_points = UT_INPUT_STATUS_NB_POINTS;
        while (nb_points > 0) {
                int nb_bits = (nb_points > 8) ? 8 : nb_points;

                value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
                if (value != UT_INPUT_STATUS_TAB[i]) {
                        printf("FAILED (%0X != %0X)\n",
                               value, UT_INPUT_STATUS_TAB[i]);
                        goto close;
                }

                nb_points -= nb_bits;
                i++;
        }
        printf("OK\n");

        /** HOLDING REGISTERS **/
        ret = read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
                                     UT_HOLDING_REGISTERS_NB_POINTS, tab_rp_registers);
        printf("* holding registers: ");
        if (ret != UT_HOLDING_REGISTERS_NB_POINTS) {
                printf("FAILED (nb points %d)\n", ret); 
                goto close;
        }

        for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
                if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
                        printf("FAILED (%0X != %0X)\n",
                               tab_rp_registers[i], UT_HOLDING_REGISTERS_TAB[i]);
                        goto close;
                }
        }
        printf("OK\n");

        /** INPUT REGISTERS **/
        ret = read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
                                   UT_INPUT_REGISTERS_NB_POINTS, tab_rp_registers);
        printf("* input registers: ");
        if (ret != UT_INPUT_REGISTERS_NB_POINTS) {
                printf("FAILED (nb points %d)\n", ret); 
                goto close;
        }
         
        for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; 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]);
                        goto close;
                }
        }
        printf("OK\n");


        /** ILLEGAL DATA ADDRESS */
        printf("\nTest illegal data address:");

        /* The mapping begins at 0 and end at adresse + nb_points so
         * the adresses above are not valid. */ 
        
        ret = read_coil_status(&mb_param, SLAVE, UT_COIL_STATUS_ADDRESS,
                               UT_COIL_STATUS_NB_POINTS + 1, tab_rp_status);
        printf("* coil status: ");
        if (ret == ILLEGAL_DATA_ADDRESS)
                printf("OK");
        else
                printf("FAILED");

        ret = read_input_status(&mb_param, SLAVE, UT_INPUT_STATUS_ADDRESS,
                                UT_INPUT_STATUS_NB_POINTS + 1, tab_rp_status);
        printf("* input status: ");
        if (ret == ILLEGAL_DATA_ADDRESS)
                printf("OK");
        else
                printf("FAILED");

        ret = read_holding_registers(&mb_param, SLAVE, UT_HOLDING_REGISTERS_ADDRESS,
                                     UT_HOLDING_REGISTERS_NB_POINTS + 1, tab_rp_registers);
        printf("* holding registers: ");
        if (ret == ILLEGAL_DATA_ADDRESS)
                printf("OK");
        else
                printf("FAILED");
                
        ret = read_input_registers(&mb_param, SLAVE, UT_INPUT_REGISTERS_ADDRESS,
                                   UT_INPUT_REGISTERS_NB_POINTS + 1, tab_rp_registers);
        printf("* input registers: ");
        if (ret == ILLEGAL_DATA_ADDRESS)
                printf("OK");
        else
                printf("FAILED");

close:
        /* Free the memory */
        free(tab_rp_status);                                           
        free(tab_rp_registers);

        /* Close the connection */
        modbus_close(&mb_param);
        
        return 0;
/*
        force_single_coil(&mb_param, SLAVE, 0xAC, ON);
*/
}
{'fraction_non_alphanumeric': 0.10022988505747127, 'fraction_numerical': 0.011954022988505748, 'mean_word_length': 1.82795232936078, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7524566', 'n_tokens_mistral': 4399, 'n_tokens_neox': 4073, 'n_words': 888}