From ede8aee02d4991fbe70ff76af3903f797e0e4c9f Mon Sep 17 00:00:00 2001
From: Iztok Jeras <iztok.jeras@redpitaya.com>
Date: 摹曛, 08 10月 2015 03:00:34 +0800
Subject: [PATCH] integer parser: added LOCAL function declarations to utils_private.h

---
 libscpi/test/test_scpi_utils.c |  175 ++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 145 insertions(+), 30 deletions(-)

diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c
index 0e9a8d5..84b382b 100644
--- a/libscpi/test/test_scpi_utils.c
+++ b/libscpi/test/test_scpi_utils.c
@@ -36,6 +36,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
 #include "CUnit/Basic.h"
 
 #include "scpi/scpi.h"
@@ -66,33 +69,142 @@
 
 }
 
-static void test_longToStr() {
-    char str[32];
+static void test_Int32ToStr() {
+    const size_t max=32+1;
+    int32_t val[] = {0, 1, -1, INT32_MIN, INT32_MAX, 0x01234567, 0x89abcdef};
+    char str[max];
+    char ref[max];
     size_t len;
 
-    len = SCPI_LongToStr(10, str, 32, 10);
-    CU_ASSERT(len == 2);
-    CU_ASSERT_STRING_EQUAL(str, "10");
-    CU_ASSERT(str[len] == '\0');
+    // test signed conversion to decimal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_Int32ToStr(val[i], str, max);
+        snprintf(ref, max, "%"PRIi32, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+}
 
-    len = SCPI_LongToStr(10, str, 32, 2);
-    CU_ASSERT(len == 4);
-    CU_ASSERT(str[0] == '1');
-    CU_ASSERT(str[1] == '0');
-    CU_ASSERT(str[2] == '1');
-    CU_ASSERT(str[3] == '0');
-    CU_ASSERT(str[4] == '\0');
+static void test_UInt32ToStrBase() {
+    const size_t max=32+1;
+    uint32_t val[] = {0, 1, -1, INT32_MIN, INT32_MAX, 0x01234567, 0x89abcdef};
+    char str[max];
+    char ref[max];
+    size_t len;
 
-    len = SCPI_LongToStr(10, str, 32, 16);
+    // test conversion to decimal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_UInt32ToStrBase(val[i], str, max, 10);
+        snprintf(ref, max, "%"PRIu32, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    // test conversion to hexadecimal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_UInt32ToStrBase(val[i], str, max, 16);
+        snprintf(ref, max, "%"PRIX32, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    // test conversion to octal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_UInt32ToStrBase(val[i], str, max, 8);
+        snprintf(ref, max, "%"PRIo32, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    // test conversion to binary numbers
+    len = SCPI_UInt32ToStrBase(0, str, max, 2);
     CU_ASSERT(len == 1);
-    CU_ASSERT(str[0] == 'A');
-    CU_ASSERT(str[1] == '\0');
-    
-    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');    
+    CU_ASSERT_STRING_EQUAL(str, "0");
+
+    len = SCPI_UInt32ToStrBase(1, str, max, 2);
+    CU_ASSERT(len == 1);
+    CU_ASSERT_STRING_EQUAL(str, "1");
+
+    len = SCPI_UInt32ToStrBase(-1, str, max, 2);
+    CU_ASSERT(len == 32);
+    CU_ASSERT_STRING_EQUAL(str, "11111111111111111111111111111111");
+
+    len = SCPI_UInt32ToStrBase(0x01234567, str, max, 2);
+    CU_ASSERT(len == 25);
+    CU_ASSERT_STRING_EQUAL(str, "1001000110100010101100111");
+
+    len = SCPI_UInt32ToStrBase(0x89abcdef, str, max, 2);
+    CU_ASSERT(len == 32);
+    CU_ASSERT_STRING_EQUAL(str, "10001001101010111100110111101111");
+}
+
+static void test_Int64ToStr() {
+    const size_t max=64+1;
+    int64_t val[] = {0, 1, -1, INT64_MIN, INT64_MAX, 0x0123456789abcdef, 0xfedcba9876543210};
+    char str[max];
+    char ref[max];
+    size_t len;
+
+    // test conversion to decimal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_Int64ToStr(val[i], str, max);
+        snprintf(ref, max, "%"PRIi64, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+}
+
+static void test_UInt64ToStrBase() {
+    const size_t max=64+1;
+    uint64_t val[] = {0, 1, -1, INT64_MIN, INT64_MAX, 0x0123456789abcdef, 0xfedcba9876543210};
+    char str[max];
+    char ref[max];
+    size_t len;
+
+    // test conversion to decimal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_UInt64ToStrBase(val[i], str, max, 10);
+        snprintf(ref, max, "%"PRIu64, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    // test conversion to hexadecimal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_UInt64ToStrBase(val[i], str, max, 16);
+        snprintf(ref, max, "%"PRIX64, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    // test conversion to octal numbers
+    for (uintptr_t i=0; i<7; i++) {
+        len = SCPI_UInt64ToStrBase(val[i], str, max, 8);
+        snprintf(ref, max, "%"PRIo64, val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    // test conversion to binary numbers
+    len = SCPI_UInt64ToStrBase(0, str, max, 2);
+    CU_ASSERT(len == 1);
+    CU_ASSERT_STRING_EQUAL(str, "0");
+
+    len = SCPI_UInt64ToStrBase(1, str, max, 2);
+    CU_ASSERT(len == 1);
+    CU_ASSERT_STRING_EQUAL(str, "1");
+
+    len = SCPI_UInt64ToStrBase(-1, str, max, 2);
+    CU_ASSERT(len == 64);
+    CU_ASSERT_STRING_EQUAL(str, "1111111111111111111111111111111111111111111111111111111111111111");
+
+    len = SCPI_UInt64ToStrBase(0x0123456789abcdef, str, max, 2);
+    CU_ASSERT(len == 57);
+    CU_ASSERT_STRING_EQUAL(str, "100100011010001010110011110001001101010111100110111101111");
+
+    len = SCPI_UInt64ToStrBase(0xfedcba9876543210, str, max, 2);
+    CU_ASSERT(len == 64);
+    CU_ASSERT_STRING_EQUAL(str, "1111111011011100101110101001100001110110010101000011001000010000");
 }
 
 static void test_doubleToStr() {
@@ -117,17 +229,17 @@
     TEST_DOUBLE_TO_STR(-1.3e-30, 8, "-1.3e-30");
 }
 
-static void test_strToLong() {
+static void test_strToInt32() {
     size_t result;
     int32_t val;
 
 #define TEST_STR_TO_LONG(s, r, v, b)                    \
     do {                                                \
-        result = strToLong(s, &val, b);                 \
+        result = strToInt32(s, &val, b);                 \
         CU_ASSERT_EQUAL(val, v);                        \
         CU_ASSERT_EQUAL(result, r);                     \
     } while(0)                                          \
-    
+
     TEST_STR_TO_LONG("", 0, 0, 10);
     TEST_STR_TO_LONG("1", 1, 1, 10);
     TEST_STR_TO_LONG("10", 2, 10, 10);
@@ -142,13 +254,13 @@
     TEST_STR_TO_LONG("18", 1, 1, 8); // octal 1, 8 is ignored
 }
 
-static void test_strToULong() {
+static void test_strToUInt32() {
     size_t result;
     uint32_t val;
 
 #define TEST_STR_TO_ULONG(s, r, v, b)                    \
     do {                                                \
-        result = strToULong(s, &val, b);                 \
+        result = strToUInt32(s, &val, b);                 \
         CU_ASSERT_EQUAL(val, v);                        \
         CU_ASSERT_EQUAL(result, r);                     \
     } while(0)                                          \
@@ -479,10 +591,13 @@
     /* Add the tests to the suite */
     if (0
             || (NULL == CU_add_test(pSuite, "strnpbrk", test_strnpbrk))
-            || (NULL == CU_add_test(pSuite, "longToStr", test_longToStr))
+            || (NULL == CU_add_test(pSuite, "Int32ToStr", test_Int32ToStr))
+            || (NULL == CU_add_test(pSuite, "UInt32ToStrBase", test_UInt32ToStrBase))
+            || (NULL == CU_add_test(pSuite, "Int64ToStr", test_Int64ToStr))
+            || (NULL == CU_add_test(pSuite, "UInt64ToStrBase", test_UInt64ToStrBase))
             || (NULL == CU_add_test(pSuite, "doubleToStr", test_doubleToStr))
-            || (NULL == CU_add_test(pSuite, "strToLong", test_strToLong))
-            || (NULL == CU_add_test(pSuite, "strToULong", test_strToULong))
+            || (NULL == CU_add_test(pSuite, "strToInt32", test_strToInt32))
+            || (NULL == CU_add_test(pSuite, "strToUInt32", test_strToUInt32))
             || (NULL == CU_add_test(pSuite, "strToDouble", test_strToDouble))
             || (NULL == CU_add_test(pSuite, "compareStr", test_compareStr))
             || (NULL == CU_add_test(pSuite, "compareStrAndNum", test_compareStrAndNum))

--
Gitblit v1.9.1