From ed07df16da675c4c123e02a996822daf13d69c63 Mon Sep 17 00:00:00 2001
From: lhoerl <coder@lolux.de>
Date: 周一, 03 8月 2015 22:42:07 +0800
Subject: [PATCH] added full SCPI error messages added list for device dependent error messages some minor changes to get rid of compiler warnings added support for Keil ARM compiler added support for National Instruments CVI compiler removed bug if(c = '\0')...

---
 libscpi/test/test_scpi_utils.c |  160 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 127 insertions(+), 33 deletions(-)

diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c
index a6f882c..af5aadf 100644
--- a/libscpi/test/test_scpi_utils.c
+++ b/libscpi/test/test_scpi_utils.c
@@ -45,15 +45,15 @@
  * CUnit Test Suite
  */
 
-int init_suite(void) {
+static int init_suite(void) {
     return 0;
 }
 
-int clean_suite(void) {
+static int clean_suite(void) {
     return 0;
 }
 
-void test_strnpbrk() {
+static void test_strnpbrk() {
     char str[] = "ahoj";
 
     CU_ASSERT(strnpbrk(str, 4, "a") == (str + 0));
@@ -61,20 +61,21 @@
     CU_ASSERT(strnpbrk(str, 4, "b") == NULL);
     CU_ASSERT(strnpbrk(str, 1, "h") == NULL);
     CU_ASSERT(strnpbrk(str, 4, "xo") == (str + 2));
+
+    CU_ASSERT(strnpbrk(str, 4, "j") == (str + 3));
+
 }
 
-
-
-void test_longToStr() {
+static void test_longToStr() {
     char str[32];
     size_t len;
 
-    len = longToStr(10, str, 32, 10);
+    len = SCPI_LongToStr(10, str, 32, 10);
     CU_ASSERT(len == 2);
     CU_ASSERT_STRING_EQUAL(str, "10");
     CU_ASSERT(str[len] == '\0');
 
-    len = longToStr(10, str, 32, 2);
+    len = SCPI_LongToStr(10, str, 32, 2);
     CU_ASSERT(len == 4);
     CU_ASSERT(str[0] == '1');
     CU_ASSERT(str[1] == '0');
@@ -82,25 +83,25 @@
     CU_ASSERT(str[3] == '0');
     CU_ASSERT(str[4] == '\0');
 
-    len = longToStr(10, str, 32, 16);
+    len = SCPI_LongToStr(10, str, 32, 16);
     CU_ASSERT(len == 1);
     CU_ASSERT(str[0] == 'A');
     CU_ASSERT(str[1] == '\0');
     
-    len = longToStr(10, str, 32, 8);
+    len = SCPI_LongToStr(10, str, 32, 8);
     CU_ASSERT(len == 2);
     CU_ASSERT(str[0] == '1');
     CU_ASSERT(str[1] == '2');    
     CU_ASSERT(str[2] == '\0');    
 }
 
-void test_doubleToStr() {
+static void test_doubleToStr() {
     size_t result;
     char str[50];
 
 #define TEST_DOUBLE_TO_STR(v, r, s)                     \
     do {                                                \
-        result = doubleToStr(v, str, sizeof(str));      \
+        result = SCPI_DoubleToStr(v, str, sizeof(str)); \
         CU_ASSERT_EQUAL(result, r);                     \
         CU_ASSERT_STRING_EQUAL(str, s);                 \
     } while(0)                                          \
@@ -116,7 +117,7 @@
     TEST_DOUBLE_TO_STR(-1.3e-30, 8, "-1.3e-30");
 }
 
-void test_strToLong() {
+static void test_strToLong() {
     size_t result;
     int32_t val;
 
@@ -141,7 +142,7 @@
     TEST_STR_TO_LONG("18", 1, 1, 8); // octal 1, 8 is ignored
 }
 
-void test_strToDouble() {
+static void test_strToDouble() {
     double val;
     size_t result;
 
@@ -174,7 +175,7 @@
 
 }
 
-void test_compareStr() {
+static void test_compareStr() {
 
     CU_ASSERT_TRUE(compareStr("abcd", 1, "afgh", 1));
     CU_ASSERT_TRUE(compareStr("ABCD", 4, "abcd", 4));
@@ -185,30 +186,44 @@
     CU_ASSERT_FALSE(compareStr("ABCD", 4, "abcd", 3));
 }
 
-void test_compareStrAndNum() {
+static void test_compareStrAndNum() {
+    int32_t num;
 
-    CU_ASSERT_TRUE(compareStrAndNum("abcd", 1, "afgh", 1));
-    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 4, "abcd", 4));
-    CU_ASSERT_TRUE(compareStrAndNum("AbCd", 3, "AbCE", 3));
-    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 1, "a", 1));
+    CU_ASSERT_TRUE(compareStrAndNum("abcd", 1, "afgh", 1, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 4, "abcd", 4, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("AbCd", 3, "AbCE", 3, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 1, "a", 1, NULL));
 
-    CU_ASSERT_FALSE(compareStrAndNum("abcd", 1, "efgh", 1));
-    CU_ASSERT_FALSE(compareStrAndNum("ABCD", 4, "abcd", 3));
+    CU_ASSERT_FALSE(compareStrAndNum("abcd", 1, "efgh", 1, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("ABCD", 4, "abcd", 3, NULL));
 
-    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd1", 5));
-    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd123", 7));
-    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcd12A", 7));
-    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcdB12", 7));
-    CU_ASSERT_FALSE(compareStrAndNum("abdd", 4, "abcd132", 7));
+    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd1", 5, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd123", 7, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcd12A", 7, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcdB12", 7, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("abdd", 4, "abcd132", 7, NULL));
 
+#define TEST_COMPARE_STR_AND_NUM(s1, l1, s2, l2, v, r)              \
+    do {                                                            \
+        num = 0;                                                    \
+        CU_ASSERT_EQUAL(compareStrAndNum(s1, l1, s2, l2, &num),r);  \
+        CU_ASSERT_EQUAL(num, v);                                    \
+    } while(0);                                                     \
+
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd", 4, 1, TRUE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd1", 5, 1, TRUE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd123", 7, 123, TRUE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd12A", 7, 0, FALSE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcdB12", 7, 0, FALSE);
+    TEST_COMPARE_STR_AND_NUM("abdd", 4, "abcd132", 7, 0, FALSE);
 }
 
-void test_matchPattern() {
+static void test_matchPattern() {
     scpi_bool_t result;
 
 #define TEST_MATCH_PATTERN(p, s, r)                             \
     do {                                                        \
-        result = matchPattern(p, strlen(p), s, strlen(s));      \
+        result = matchPattern(p, strlen(p), s, strlen(s), NULL);\
         CU_ASSERT_EQUAL(result, r);                             \
     } while(0)                                                  \
 
@@ -217,15 +232,34 @@
     TEST_MATCH_PATTERN("Ab", "ab", TRUE);
     TEST_MATCH_PATTERN("Ab", "aB", TRUE);
     TEST_MATCH_PATTERN("AB", "a", FALSE);
+    TEST_MATCH_PATTERN("Ab#", "aB", TRUE);
+    TEST_MATCH_PATTERN("Ab#", "aB10", TRUE);
+    TEST_MATCH_PATTERN("Ab#", "a10", TRUE);
 }
 
-void test_matchCommand() {
+static void test_matchCommand() {
     scpi_bool_t result;
+    int32_t values[20];
 
-#define TEST_MATCH_COMMAND(p, s, r)                         \
+    #define TEST_MATCH_COMMAND(p, s, r)                         \
     do {                                                        \
-        result = matchCommand(p, s, strlen(s));                 \
+        result = matchCommand(p, s, strlen(s), NULL, 0);        \
         CU_ASSERT_EQUAL(result, r);                             \
+    } while(0)                                                  \
+
+    #define TEST_MATCH_COMMAND2(p, s, r, ...)                   \
+    do {                                                        \
+        int32_t evalues[] = {__VA_ARGS__};                      \
+        unsigned int cnt = (sizeof(evalues)/4);                 \
+        result = matchCommand(p, s, strlen(s), values, 20);     \
+        CU_ASSERT_EQUAL(result, r);                             \
+        if (cnt > 0) CU_ASSERT_EQUAL(evalues[0], values[0]);    \
+        if (cnt > 1) CU_ASSERT_EQUAL(evalues[1], values[1]);    \
+        if (cnt > 2) CU_ASSERT_EQUAL(evalues[2], values[2]);    \
+        if (cnt > 3) CU_ASSERT_EQUAL(evalues[3], values[3]);    \
+        if (cnt > 4) CU_ASSERT_EQUAL(evalues[4], values[4]);    \
+        if (cnt > 5) CU_ASSERT_EQUAL(evalues[5], values[5]);    \
+        if (cnt > 6) CU_ASSERT_EQUAL(evalues[6], values[6]);    \
     } while(0)                                                  \
 
     TEST_MATCH_COMMAND("A", "a", TRUE);
@@ -342,7 +376,66 @@
     TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:fm", TRUE); // test numeric parameter
     TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:mod10:fm", TRUE); // test numeric parameter
     TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:fm2", TRUE); // test numeric parameter
-    TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE); // test numeric parameter    
+    TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE); // test numeric parameter
+
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp3:mod10:fm", TRUE, 3, 10, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output3:mod10:fm", TRUE, 3, 10, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp30:modulation:fm5", TRUE, 30, 1, 5); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output:mod:fm", TRUE, 1, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm", TRUE, 3, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:mod10:fm", TRUE, 3, 10, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm2", TRUE, 3, 1, 2); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE, 1, 1, 1); // test numeric parameter
+
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp3:mod:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output3:mod:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp30:modulation:fm5", TRUE, 30, 5); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output:mod:fm", TRUE, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:mod:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm2", TRUE, 3, 2); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "output:fm", TRUE, 1, 1); // test numeric parameter
+
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp3:mod10:fm", TRUE, 3, 10); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output3:mod10:fm", TRUE, 3, 10); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp30:modulation:fm", TRUE, 30, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output:mod:fm", TRUE, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:mod10:fm", TRUE, 3, 10); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "output:fm", TRUE, 1, 1); // test numeric parameter
+}
+
+static void test_composeCompoundCommand(void) {
+
+#define TEST_COMPOSE_COMMAND(b, c1_len, c2_pos, c2_len, c2_final, r)    \
+    {                                                                   \
+        char buffer[100];                                               \
+        scpi_token_t cmd_prev, cmd_curr;                                \
+        cmd_prev.ptr = buffer;                                          \
+        cmd_prev.len = c1_len;                                          \
+        cmd_curr.ptr = buffer + c2_pos;                                 \
+        cmd_curr.len = c2_len;                                          \
+        scpi_bool_t res;                                                \
+                                                                        \
+        strcpy(buffer, b);                                              \
+        res = composeCompoundCommand(&cmd_prev, &cmd_curr);             \
+        CU_ASSERT_EQUAL(res, r);                                        \
+        CU_ASSERT_EQUAL(cmd_curr.len, strlen(c2_final));                \
+        CU_ASSERT_STRING_EQUAL(cmd_curr.ptr, c2_final);                 \
+    }\
+
+    TEST_COMPOSE_COMMAND("A:B;C", 3, 4, 1, "A:C", TRUE);
+    TEST_COMPOSE_COMMAND("A:B;DD", 3, 4, 2, "A:DD", TRUE);
+    TEST_COMPOSE_COMMAND("A:B", 0, 0, 3, "A:B", TRUE);
+    TEST_COMPOSE_COMMAND("*IDN? ; ABC", 5, 8, 3, "ABC", TRUE);
+    TEST_COMPOSE_COMMAND("A:B;*IDN?", 3, 4, 5, "*IDN?", TRUE);
+    TEST_COMPOSE_COMMAND("A:B;:C", 3, 4, 2, ":C", TRUE);
+    TEST_COMPOSE_COMMAND("B;C", 1, 2, 1, "C", TRUE);
+    TEST_COMPOSE_COMMAND("A:B;C:D", 3, 4, 3, "A:C:D", TRUE);
+    TEST_COMPOSE_COMMAND(":A:B;C", 4, 5, 1, ":A:C", TRUE);
+    TEST_COMPOSE_COMMAND(":A:B;:C", 4, 5, 2, ":C", TRUE);
+    TEST_COMPOSE_COMMAND(":A;C", 2, 3, 1, ":C", TRUE);
 }
 
 int main() {
@@ -370,6 +463,7 @@
             || (NULL == CU_add_test(pSuite, "compareStrAndNum", test_compareStrAndNum))
             || (NULL == CU_add_test(pSuite, "matchPattern", test_matchPattern))
             || (NULL == CU_add_test(pSuite, "matchCommand", test_matchCommand))
+            || (NULL == CU_add_test(pSuite, "composeCompoundCommand", test_composeCompoundCommand))
             ) {
         CU_cleanup_registry();
         return CU_get_error();

--
Gitblit v1.9.1