Jan Breuer
2016-04-24 71c6c18189be9b7bf995d6e71b7ec5eb88161823
Add support for array parameters represened as text (#52)
3个文件已修改
189 ■■■■■ 已修改文件
libscpi/inc/scpi/parser.h 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/parser.c 97 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/test/test_parser.c 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/inc/scpi/parser.h
@@ -117,6 +117,13 @@
    scpi_bool_t SCPI_ParamBool(scpi_t * context, scpi_bool_t * value, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamChoice(scpi_t * context, const scpi_choice_def_t * options, int32_t * value, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamArrayInt32(scpi_t * context, int32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamArrayUInt32(scpi_t * context, uint32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamArrayInt64(scpi_t * context, int64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamArrayUInt64(scpi_t * context, uint64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamArrayFloat(scpi_t * context, float *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory);
    scpi_bool_t SCPI_ParamArrayDouble(scpi_t * context, double *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory);
    scpi_bool_t SCPI_IsCmd(scpi_t * context, const char * cmd);
#if USE_COMMAND_TAGS
    int32_t SCPI_CmdTag(scpi_t * context);
libscpi/src/parser.c
@@ -280,6 +280,7 @@
}
#if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE
/**
 * Initialize context's
 * @param context
@@ -1545,7 +1546,7 @@
 * @param format
 * @return
 */
static size_t parserResultArrayBinary(scpi_t * context, const void * array, size_t count, size_t item_size, scpi_array_format_t format) {
static size_t produceResultArrayBinary(scpi_t * context, const void * array, size_t count, size_t item_size, scpi_array_format_t format) {
    if (SCPI_GetNativeFormat() == format) {
        switch (item_size) {
@@ -1610,7 +1611,7 @@
            result += func(context, array[i]);\
        }\
    } else {\
        result = parserResultArrayBinary(context, array, count, sizeof(*array), format);\
        result = produceResultArrayBinary(context, array, count, sizeof(*array), format);\
    }\
    return result;\
} while(0)
@@ -1734,3 +1735,95 @@
size_t SCPI_ResultArrayDouble(scpi_t * context, const double * array, size_t count, scpi_array_format_t format) {
    RESULT_ARRAY(SCPI_ResultDouble);
}
/*
 * Template macro to generate all SCPI_ParamArrayXYZ function
 */
#define PARAM_ARRAY_TEMPLATE(func) do{\
    if (format != SCPI_FORMAT_ASCII) return FALSE;\
    for (*o_count = 0; *o_count < i_count; (*o_count)++) {\
        if (!func(context, &data[*o_count], mandatory)) {\
            break;\
        }\
        mandatory = FALSE;\
    }\
    return mandatory ? FALSE : TRUE;\
}while(0)
/**
 * Read list of values up to i_count
 * @param context
 * @param data - array to fill
 * @param i_count - number of elements of data
 * @param o_count - real number of filled elements
 * @param mandatory
 * @return TRUE on success
 */
scpi_bool_t SCPI_ParamArrayInt32(scpi_t * context, int32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) {
    PARAM_ARRAY_TEMPLATE(SCPI_ParamInt32);
}
/**
 * Read list of values up to i_count
 * @param context
 * @param data - array to fill
 * @param i_count - number of elements of data
 * @param o_count - real number of filled elements
 * @param mandatory
 * @return TRUE on success
 */
scpi_bool_t SCPI_ParamArrayUInt32(scpi_t * context, uint32_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) {
    PARAM_ARRAY_TEMPLATE(SCPI_ParamUInt32);
}
/**
 * Read list of values up to i_count
 * @param context
 * @param data - array to fill
 * @param i_count - number of elements of data
 * @param o_count - real number of filled elements
 * @param mandatory
 * @return TRUE on success
 */
scpi_bool_t SCPI_ParamArrayInt64(scpi_t * context, int64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) {
    PARAM_ARRAY_TEMPLATE(SCPI_ParamInt64);
}
/**
 * Read list of values up to i_count
 * @param context
 * @param data - array to fill
 * @param i_count - number of elements of data
 * @param o_count - real number of filled elements
 * @param mandatory
 * @return TRUE on success
 */
scpi_bool_t SCPI_ParamArrayUInt64(scpi_t * context, uint64_t *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) {
    PARAM_ARRAY_TEMPLATE(SCPI_ParamUInt64);
}
/**
 * Read list of values up to i_count
 * @param context
 * @param data - array to fill
 * @param i_count - number of elements of data
 * @param o_count - real number of filled elements
 * @param mandatory
 * @return TRUE on success
 */
scpi_bool_t SCPI_ParamArrayFloat(scpi_t * context, float *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) {
    PARAM_ARRAY_TEMPLATE(SCPI_ParamFloat);
}
/**
 * Read list of values up to i_count
 * @param context
 * @param data - array to fill
 * @param i_count - number of elements of data
 * @param o_count - real number of filled elements
 * @param mandatory
 * @return TRUE on success
 */
scpi_bool_t SCPI_ParamArrayDouble(scpi_t * context, double *data, size_t i_count, size_t *o_count, scpi_array_format_t format, scpi_bool_t mandatory) {
    PARAM_ARRAY_TEMPLATE(SCPI_ParamDouble);
}
libscpi/test/test_parser.c
@@ -1325,6 +1325,90 @@
    TEST_Result(ArrayDoubleSWAPPED, double_arr, "#216" "\x40\x8c\x16\xd3\x66\x67\xd1\x42" "\x1c\xbc\x6e\xf2\x54\x8b\x11\x43");
}
#define _countof(a) (sizeof(a)/sizeof(*(a)))
#define TEST_ParamArrayDouble(T, func, data, mandatory, _expected_value, expected_result, expected_error_code) \
{                                                                                       \
    T value[10];                                                                        \
    scpi_bool_t result;                                                                 \
    scpi_error_t errCode;                                                               \
    T expected_value[] = {NOPAREN _expected_value};                                     \
    size_t o_count;                                                                     \
    size_t i_count = _countof(expected_value);                                          \
                                                                                        \
    SCPI_CoreCls(&scpi_context);                                                        \
    scpi_context.input_count = 0;                                                       \
    scpi_context.param_list.lex_state.buffer = data;                                    \
    scpi_context.param_list.lex_state.len = strlen(scpi_context.param_list.lex_state.buffer);\
    scpi_context.param_list.lex_state.pos = scpi_context.param_list.lex_state.buffer;   \
    result = func(&scpi_context, value, 10, &o_count, SCPI_FORMAT_ASCII, mandatory);    \
                                                                                        \
    SCPI_ErrorPop(&scpi_context, &errCode);                                             \
    CU_ASSERT_EQUAL(result, expected_result);                                           \
    if (expected_result) {                                                              \
        CU_ASSERT_EQUAL(i_count, o_count);                                              \
        size_t i;                                                                       \
        for(i = 0; i < o_count; i++) {                                                  \
            CU_ASSERT_DOUBLE_EQUAL(value[i], expected_value[i], 0.000001);              \
        }                                                                               \
    }                                                                                   \
    CU_ASSERT_EQUAL(errCode.error_code, expected_error_code);                           \
}
#define TEST_ParamArrayInt(T, func, data, mandatory, _expected_value, expected_result, expected_error_code) \
{                                                                                       \
    T value[10];                                                                        \
    scpi_bool_t result;                                                                 \
    scpi_error_t errCode;                                                               \
    T expected_value[] = {NOPAREN _expected_value};                                     \
    size_t o_count;                                                                     \
    size_t i_count = _countof(expected_value);                                          \
                                                                                        \
    SCPI_CoreCls(&scpi_context);                                                        \
    scpi_context.input_count = 0;                                                       \
    scpi_context.param_list.lex_state.buffer = data;                                    \
    scpi_context.param_list.lex_state.len = strlen(scpi_context.param_list.lex_state.buffer);\
    scpi_context.param_list.lex_state.pos = scpi_context.param_list.lex_state.buffer;   \
    result = func(&scpi_context, value, 10, &o_count, SCPI_FORMAT_ASCII, mandatory);    \
                                                                                        \
    SCPI_ErrorPop(&scpi_context, &errCode);                                             \
    CU_ASSERT_EQUAL(result, expected_result);                                           \
    if (expected_result) {                                                              \
        CU_ASSERT_EQUAL(i_count, o_count);                                              \
        size_t i;                                                                       \
        for(i = 0; i < o_count; i++) {                                                  \
            CU_ASSERT_EQUAL(value[i], expected_value[i]);                               \
        }                                                                               \
    }                                                                                   \
    CU_ASSERT_EQUAL(errCode.error_code, expected_error_code);                           \
}
static void testParamArray(void) {
    TEST_ParamArrayDouble(double, SCPI_ParamArrayDouble, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayDouble(double, SCPI_ParamArrayDouble, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER);
    TEST_ParamArrayDouble(double, SCPI_ParamArrayDouble, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayDouble(float, SCPI_ParamArrayFloat, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayDouble(float, SCPI_ParamArrayFloat, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER);
    TEST_ParamArrayDouble(float, SCPI_ParamArrayFloat, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(int32_t, SCPI_ParamArrayInt32, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(int32_t, SCPI_ParamArrayInt32, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER);
    TEST_ParamArrayInt(int32_t, SCPI_ParamArrayInt32, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(uint32_t, SCPI_ParamArrayUInt32, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(uint32_t, SCPI_ParamArrayUInt32, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER);
    TEST_ParamArrayInt(uint32_t, SCPI_ParamArrayUInt32, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(int64_t, SCPI_ParamArrayInt64, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(int64_t, SCPI_ParamArrayInt64, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER);
    TEST_ParamArrayInt(int64_t, SCPI_ParamArrayInt64, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(uint64_t, SCPI_ParamArrayUInt64, "1, 2, 3", TRUE, (1, 2, 3), TRUE, SCPI_ERROR_NO_ERROR);
    TEST_ParamArrayInt(uint64_t, SCPI_ParamArrayUInt64, "", TRUE, (0), FALSE, SCPI_ERROR_MISSING_PARAMETER);
    TEST_ParamArrayInt(uint64_t, SCPI_ParamArrayUInt64, "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11", TRUE, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), TRUE, SCPI_ERROR_NO_ERROR);
}
static void testNumberToStr(void) {
#define TEST_SCPI_NumberToStr(_special, _value, _unit, expected_result) do {\
@@ -1551,6 +1635,7 @@
            || (NULL == CU_add_test(pSuite, "SCPI_ResultText", testResultText))
            || (NULL == CU_add_test(pSuite, "SCPI_ResultArbitraryBlock", testResultArbitraryBlock))
            || (NULL == CU_add_test(pSuite, "SCPI_ResultArray", testResultArray))
            || (NULL == CU_add_test(pSuite, "SCPI_ParamArray", testParamArray))
            || (NULL == CU_add_test(pSuite, "SCPI_NumberToStr", testNumberToStr))
            || (NULL == CU_add_test(pSuite, "SCPI_ErrorQueue", testErrorQueue))
            || (NULL == CU_add_test(pSuite, "Incomplete arbitrary parameter", testIncompleteArbitraryParameter))