From 3c5ca89152f962e0887bd5cf607c69a52c3dd575 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周二, 31 5月 2016 21:50:42 +0800
Subject: [PATCH] Rework compiler detection, fix SCPI_dtostre length limitation

---
 libscpi/test/test_scpi_utils.c |   59 ++++++-
 libscpi/inc/scpi/config.h      |  148 ++++++++++-----------
 libscpi/src/utils_private.h    |    4 
 libscpi/test/test_parser.c     |   10 
 libscpi/src/utils.c            |   29 +++-
 libscpi/inc/scpi/cc.h          |  166 +++++++++++++++++++++++
 6 files changed, 315 insertions(+), 101 deletions(-)

diff --git a/libscpi/inc/scpi/cc.h b/libscpi/inc/scpi/cc.h
new file mode 100644
index 0000000..0ad081e
--- /dev/null
+++ b/libscpi/inc/scpi/cc.h
@@ -0,0 +1,166 @@
+/*-
+ * Copyright (c) 2012-2013 Jan Breuer,
+ *
+ * All Rights Reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @file   cc.h
+ *
+ * @brief  compiler detection
+ *
+ *
+ */
+
+#ifndef __SCPI_CC_H_
+#define __SCPI_CC_H_
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#if defined(__STDC__)
+# define C89 1
+# if defined(__STDC_VERSION__)
+#  define C90 1
+#  if (__STDC_VERSION__ >= 199409L)
+#   define C94 1
+#  endif
+#  if (__STDC_VERSION__ >= 199901L)
+#   define C99 1
+#  endif
+# endif
+#endif
+
+#if POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
+    #define HAVE_STRNDUP 1
+    #define HAVE_STRNLEN 1
+#endif
+
+#if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || C99
+#define HAVE_SNPRINTF 1
+#endif
+
+#if _POSIX_C_SOURCE >= 200112L
+#define HAVE_STRNCASECMP 1
+#endif
+
+#if _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || C99
+    #define HAVE_ISNAN 1
+#endif
+
+#if _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || C99
+    #define HAVE_ISFINITE 1
+    #define HAVE_SIGNBIT 1
+#endif
+
+#if XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
+    #define HAVE_STRTOLL 1
+#endif
+
+#if _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || C99
+    #define HAVE_STRTOF 1
+#endif
+
+/* Compiler specific */
+/* RealView/Keil ARM Compiler, e.g. Cortex-M CPUs */
+#if defined(__CC_ARM)
+#define HAVE_STRNCASECMP        1
+#endif
+
+/* National Instruments (R) CVI x86/x64 PC platform */
+#if defined(_CVI_)
+#define HAVE_STRNICMP           1
+#define HAVE_STDBOOL            0
+#endif
+
+/* 8bit PIC - PIC16, etc */
+#if defined(_MPC_)
+#define HAVE_STRNICMP           1
+#endif
+
+/* PIC24 */
+#if defined(__C30__)
+#endif
+
+/* PIC32mx */
+#if defined(__C32__)
+#define HAVE_FINITE             1
+#endif
+
+/* AVR libc */
+#if defined(__AVR__)
+#include <stdlib.h>
+#define HAVE_DTOSTRE            1
+#endif
+
+#ifndef HAVE_STRNLEN
+#define HAVE_STRNLEN            0
+#endif
+
+#ifndef HAVE_STRDUP
+#define HAVE_STRDUP             0
+#endif
+
+#ifndef HAVE_STRNICMP
+#define HAVE_STRNICMP           0
+#endif
+
+#ifndef HAVE_STDBOOL
+#define HAVE_STDBOOL            1
+#endif
+
+#ifndef HAVE_SNPRINTF
+#define HAVE_SNPRINTF 0
+#endif
+
+#ifndef HAVE_STRNCASECMP
+#define HAVE_STRNCASECMP 0
+#endif
+
+#ifndef HAVE_ISNAN
+#define HAVE_ISNAN 0
+#endif
+
+#ifndef HAVE_ISFINITE
+#define HAVE_ISFINITE 0
+#endif
+
+#ifndef HAVE_SIGNBIT
+#define HAVE_SIGNBIT 0
+#endif
+
+#ifndef HAVE_STRTOLL
+#define HAVE_STRTOLL 0
+#endif
+
+#ifndef HAVE_STRTOF
+#define HAVE_STRTOF 0
+#endif
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif /* __SCPI_CC_H_ */
\ No newline at end of file
diff --git a/libscpi/inc/scpi/config.h b/libscpi/inc/scpi/config.h
index 7473d9d..debe03d 100644
--- a/libscpi/inc/scpi/config.h
+++ b/libscpi/inc/scpi/config.h
@@ -41,6 +41,8 @@
 extern "C" {
 #endif
 
+#include "cc.h"
+
 #ifdef SCPI_USER_CONFIG
 #include "scpi_user_config.h"
 #endif
@@ -167,68 +169,6 @@
 #define USE_UNITS_ELECTRIC_CHARGE_CONDUCTANCE SYSTEM_TYPE
 #endif
 
-/* Compiler specific */
-/* RealView/Keil ARM Compiler, e.g. Cortex-M CPUs */
-#if defined(__CC_ARM)
-#define HAVE_STRNLEN            0
-#define HAVE_STRNCASECMP        1
-#define HAVE_STRNICMP           0
-#endif
-
-/* National Instruments (R) CVI x86/x64 PC platform */
-#if defined(_CVI_)
-#define HAVE_STRNLEN            0
-#define HAVE_STRNCASECMP        0
-#define HAVE_STRNICMP           1
-#define HAVE_STDBOOL            0
-#endif
-
-/* 8bit PIC - PIC16, etc */
-#if defined(_MPC_)
-#define HAVE_STRNLEN            0
-#define HAVE_STRNCASECMP        0
-#define HAVE_STRNICMP           1
-#endif
-
-/* PIC24 */
-#if defined(__C30__)
-#define HAVE_STRNLEN            0
-#define HAVE_STRNCASECMP        0
-#define HAVE_STRNICMP           0
-#endif
-
-/* PIC32mx */
-#if defined(__C32__)
-#define HAVE_STRNLEN            0
-#define HAVE_STRNCASECMP        0
-#define HAVE_STRNICMP           0
-#define isfinite                finite
-#define signbit(x)              ((x)<0)
-#endif
-
-/* AVR libc */
-#if defined(__AVR__)
-#include <stdlib.h>
-#define HAVE_DTOSTRE            1
-#endif
-
-/* ======== test strnlen ======== */
-#ifndef HAVE_STRNLEN
-#define HAVE_STRNLEN            1
-#endif
-/* ======== test strncasecmp ======== */
-#ifndef HAVE_STRNCASECMP
-#define HAVE_STRNCASECMP        1
-#endif
-/* ======== test strnicmp ======== */
-#ifndef HAVE_STRNICMP
-#define HAVE_STRNICMP           0
-#endif
-
-#ifndef HAVE_STDBOOL
-#define HAVE_STDBOOL            1
-#endif
-
 /* define local macros depending on existance of strnlen */
 #if HAVE_STRNLEN
 #define SCPIDEFINE_strnlen(s, l)	strnlen((s), (l))
@@ -249,36 +189,90 @@
 #define SCPIDEFINE_floatToStr(v, s, l) dtostre((double)(v), (s), 6, DTOSTR_PLUS_SIGN | DTOSTR_ALWAYS_SIGN | DTOSTR_UPPERCASE)
 #elif USE_CUSTOM_DTOSTRE
 #define SCPIDEFINE_floatToStr(v, s, l) SCPI_dtostre((v), (s), (l), 6, 0)
-#else
+#elif HAVE_SNPRINTF
 #define SCPIDEFINE_floatToStr(v, s, l) snprintf((s), (l), "%g", (v))
+#else
+#define SCPIDEFINE_floatToStr(v, s, l) SCPI_dtostre((v), (s), (l), 6, 0)
 #endif
 
 #if HAVE_DTOSTRE
 #define SCPIDEFINE_doubleToStr(v, s, l) dtostre((v), (s), 15, DTOSTR_PLUS_SIGN | DTOSTR_ALWAYS_SIGN | DTOSTR_UPPERCASE)
 #elif USE_CUSTOM_DTOSTRE
 #define SCPIDEFINE_doubleToStr(v, s, l) SCPI_dtostre((v), (s), (l), 15, 0)
-#else
+#elif HAVE_SNPRINTF
 #define SCPIDEFINE_doubleToStr(v, s, l) snprintf((s), (l), "%.15lg", (v))
+#else
+#define SCPIDEFINE_doubleToStr(v, s, l) SCPI_dtostre((v), (s), (l), 15, 0)
 #endif
 
 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
 
-#if USE_MEMORY_ALLOCATION_FREE
-#include <stdlib.h>
-#include <string.h>
-#define SCPIDEFINE_DESCRIPTION_MAX_PARTS		2
-#define SCPIDEFINE_strndup(h, s, l)                     strndup((s), (l))
-#define SCPIDEFINE_free(h, s, r)                        free((s))
+  #if USE_MEMORY_ALLOCATION_FREE
+    #include <stdlib.h>
+    #include <string.h>
+    #define SCPIDEFINE_DESCRIPTION_MAX_PARTS            2
+    #if HAVE_STRNDUP
+      #define SCPIDEFINE_strndup(h, s, l)               strndup((s), (l))
+    #else
+      #define SCPIDEFINE_strndup(h, s, l)               OUR_strndup((s), (l))
+    #endif
+    #define SCPIDEFINE_free(h, s, r)                    free((s))
+  #else
+    #define SCPIDEFINE_DESCRIPTION_MAX_PARTS            3
+    #define SCPIDEFINE_strndup(h, s, l)                 scpiheap_strndup((h), (s), (l))
+    #define SCPIDEFINE_free(h, s, r)                    scpiheap_free((h), (s), (r))
+    #define SCPIDEFINE_get_parts(h, s, l1, s2, l2)      scpiheap_get_parts((h), (s), (l1), (s2), (l2))
+  #endif
 #else
-#define SCPIDEFINE_DESCRIPTION_MAX_PARTS                3
-#define SCPIDEFINE_strndup(h, s, l)                     scpiheap_strndup((h), (s), (l))
-#define SCPIDEFINE_free(h, s, r)                        scpiheap_free((h), (s), (r))
-#define SCPIDEFINE_get_parts(h, s, l1, s2, l2)          scpiheap_get_parts((h), (s), (l1), (s2), (l2))
+  #define SCPIDEFINE_DESCRIPTION_MAX_PARTS              1
+  #define SCPIDEFINE_strndup(h, s, l)                   NULL
+  #define SCPIDEFINE_free(h, s, r)
 #endif
+
+#if HAVE_SIGNBIT
+  #define SCPIDEFINE_signbit(n)                         signbit(n)
 #else
-#define SCPIDEFINE_DESCRIPTION_MAX_PARTS                1
-#define SCPIDEFINE_strndup(h, s, l)                     NULL
-#define SCPIDEFINE_free(h, s, r)
+  #define SCPIDEFINE_signbit(n)                         ((n)<0)
+#endif
+
+#if HAVE_ISFINITE
+  #define SCPIDEFINE_isfinite(n)                        isfinite(n)
+#elif HAVE_FINITE
+  #define SCPIDEFINE_isfinite(n)                        finite(n)
+#else
+  #define SCPIDEFINE_isfinite(n)                        (!SCPIDEFINE_isnan((n)) && ((n) < INFINITY) && ((n) > -INFINITY))
+#endif
+
+#if HAVE_STRTOF
+  #define SCPIDEFINE_strtof(n, p)                       strtof((n), (p))
+#else
+  #define SCPIDEFINE_strtof(n, p)                       strtod((n), (p))
+#endif
+
+#if HAVE_STRTOLL
+  #define SCPIDEFINE_strtoll(n, p, b)                   strtoll((n), (p), (b))
+  #define SCPIDEFINE_strtoull(n, p, b)                  strtoull((n), (p), (b))
+#else
+  #define SCPIDEFINE_strtoll(n, p, b)                   strtoll((n), (p), (b))
+  #define SCPIDEFINE_strtoull(n, p, b)                  strtoull((n), (p), (b))
+  extern long long int strtoll(const char *nptr, char **endptr, int base);
+  extern unsigned long long int strtoull(const char *nptr, char **endptr, int base);
+  /* TODO: implement OUR_strtoll and OUR_strtoull */
+  /* #warning "64bit string to int conversion not implemented" */
+#endif
+
+#if HAVE_ISNAN
+  #define SCPIDEFINE_isnan(n)                           isnan((n))
+#else
+  #define SCPIDEFINE_isnan(n)                           ((n) != (n))
+#endif
+
+#ifndef NAN
+  #define NAN                                           (0.0 / 0.0)
+#endif
+
+#ifndef INFINITY
+  #define INFINITY                                      (1.0 / 0.0)
 #endif
 
 #ifdef	__cplusplus
diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index ba64d07..b77b339 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -297,7 +297,7 @@
  */
 size_t strBaseToInt64(const char * str, int64_t * val, int8_t base) {
     char * endptr;
-    *val = strtoll(str, &endptr, base);
+    *val = SCPIDEFINE_strtoll(str, &endptr, base);
     return endptr - str;
 }
 
@@ -309,7 +309,7 @@
  */
 size_t strBaseToUInt64(const char * str, uint64_t * val, int8_t base) {
     char * endptr;
-    *val = strtoull(str, &endptr, base);
+    *val = SCPIDEFINE_strtoull(str, &endptr, base);
     return endptr - str;
 }
 
@@ -321,7 +321,7 @@
  */
 size_t strToFloat(const char * str, float * val) {
     char * endptr;
-    *val = strtof(str, &endptr);
+    *val = SCPIDEFINE_strtof(str, &endptr);
     return endptr - str;
 }
 
@@ -752,6 +752,19 @@
 }
 #endif
 
+#if !HAVE_STRNDUP
+char *OUR_strndup(const char *s, size_t n) {
+    size_t len = SCPIDEFINE_strnlen(s, n);
+    char * result = malloc(len + 1);
+    if (!result) {
+        return NULL;
+    }
+    memcpy(result, s, len);
+    result[len] = '\0';
+    return result;
+}
+#endif
+
 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE
 
 /**
@@ -995,14 +1008,14 @@
 char * SCPI_dtostre(double __val, char * __s, size_t __ssize, unsigned char __prec, unsigned char __flags) {
     char buffer[SCPI_DTOSTRE_BUFFER_SIZE];
 
-    int sign = signbit(__val);
+    int sign = SCPIDEFINE_signbit(__val);
     char * s = buffer;
     int decpt;
     if (sign) {
         __val = -__val;
         s[0] = '-';
         s++;
-    } else if (!isnan(__val)) {
+    } else if (!SCPIDEFINE_isnan(__val)) {
         if (SCPI_DTOSTRE_PLUS_SIGN & __flags) {
             s[0] = '+';
             s++;
@@ -1012,13 +1025,14 @@
         }
     }
 
-    if (!isfinite(__val)) {
-        if (isnan(__val)) {
+    if (!SCPIDEFINE_isfinite(__val)) {
+        if (SCPIDEFINE_isnan(__val)) {
             strcpy(s, (__flags & SCPI_DTOSTRE_UPPERCASE) ? "NAN" : "nan");
         } else {
             strcpy(s, (__flags & SCPI_DTOSTRE_UPPERCASE) ? "INF" : "inf");
         }
         strncpy(__s, buffer, __ssize);
+        __s[__ssize - 1] = '\0';
         return __s;
     }
 
@@ -1072,6 +1086,7 @@
     }
 
     strncpy(__s, buffer, __ssize);
+    __s[__ssize - 1] = '\0';
     return __s;
 }
 
diff --git a/libscpi/src/utils_private.h b/libscpi/src/utils_private.h
index ba1f8fc..f83c6d4 100644
--- a/libscpi/src/utils_private.h
+++ b/libscpi/src/utils_private.h
@@ -94,6 +94,10 @@
     scpi_bool_t scpiheap_get_parts(scpi_error_info_heap_t * heap, const char *s1, size_t * len1, const char ** s2, size_t * len2) LOCAL;
 #endif
 
+#if !HAVE_STRNDUP
+    char *OUR_strndup(const char *s, size_t n);
+#endif
+
 #ifndef min
 #define min(a, b)  (((a) < (b)) ? (a) : (b))
 #endif
diff --git a/libscpi/test/test_parser.c b/libscpi/test/test_parser.c
index 61fda2d..94ec818 100644
--- a/libscpi/test/test_parser.c
+++ b/libscpi/test/test_parser.c
@@ -350,7 +350,7 @@
 }
 
 #define TEST_IEEE4882_REG(reg, expected) {                                     \
-    CU_ASSERT_EQUAL(SCPI_RegGet(&scpi_context, reg), expected);                \
+    CU_ASSERT_EQUAL(SCPI_RegGet(&scpi_context, (scpi_reg_name_t)(reg)), expected);\
 }
 
 
@@ -1203,8 +1203,10 @@
     TEST_Result(Double, 2147483647, "2147483647");
     /* TEST_Result(Double, -2147483648, "-2147483648"); bug in GCC */
     TEST_Result(Double, -2147483647, "-2147483647");
-    TEST_Result(Double, 9223372036854775807LL, "9.22337203685478e+18");
-    TEST_Result(Double, -9223372036854775807LL, "-9.22337203685478e+18");
+    /* TEST_Result(Double, 9223372036854775807LL, "9.22337203685478e+18"); */
+    /* TEST_Result(Double, -9223372036854775807LL, "-9.22337203685478e+18"); */
+    TEST_Result(Double, 9223372036854700000LL, "9.2233720368547e+18");
+    TEST_Result(Double, -9223372036854700000LL, "-9.2233720368547e+18");
 
     TEST_Result(Double, 1.256e-17, "1.256e-17");
     TEST_Result(Double, -1.256e-17, "-1.256e-17");
@@ -1435,7 +1437,7 @@
     memset(buffer, 0xaa, 100);\
     size_t res_len;\
     res_len = SCPI_NumberToStr(&scpi_context, scpi_special_numbers_def, &number, buffer, limit);\
-    size_t expected_len = strnlen(expected_result, limit - 1);\
+    size_t expected_len = SCPIDEFINE_strnlen(expected_result, limit - 1);\
     CU_ASSERT_NSTRING_EQUAL(buffer, expected_result, expected_len);\
     CU_ASSERT_EQUAL(buffer[expected_len], 0);\
     CU_ASSERT_EQUAL((unsigned char)buffer[limit], 0xaa);\
diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c
index bb68bb8..fbc31e1 100644
--- a/libscpi/test/test_scpi_utils.c
+++ b/libscpi/test/test_scpi_utils.c
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <inttypes.h>
+#include <math.h>
 
 #include "CUnit/Basic.h"
 
@@ -81,7 +82,7 @@
     /* test signed conversion to decimal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_Int32ToStr(val[i], str, max);
-        snprintf(ref, max, "%"PRIi32, val[i]);
+        sprintf(ref, "%"PRIi32, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -91,7 +92,7 @@
     /* test signed conversion to decimal numbers */
     for (i = 0; i < N16; i++) {
         len = SCPI_Int32ToStr((int32_t) val16[i], str, max);
-        snprintf(ref, max, "%"PRIi16, val16[i]);
+        sprintf(ref, "%"PRIi16, val16[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -109,7 +110,7 @@
     /* test conversion to decimal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_UInt32ToStrBase(val[i], str, max, 10);
-        snprintf(ref, max, "%"PRIu32, val[i]);
+        sprintf(ref, "%"PRIu32, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -117,7 +118,7 @@
     /* test conversion to hexadecimal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_UInt32ToStrBase(val[i], str, max, 16);
-        snprintf(ref, max, "%"PRIX32, val[i]);
+        sprintf(ref, "%"PRIX32, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -125,7 +126,7 @@
     /* test conversion to octal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_UInt32ToStrBase(val[i], str, max, 8);
-        snprintf(ref, max, "%"PRIo32, val[i]);
+        sprintf(ref, "%"PRIo32, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -164,7 +165,7 @@
     /* test conversion to decimal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_Int64ToStr(val[i], str, max);
-        snprintf(ref, max, "%"PRIi64, val[i]);
+        sprintf(ref, "%"PRIi64, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -182,7 +183,7 @@
     /* test conversion to decimal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_UInt64ToStrBase(val[i], str, max, 10);
-        snprintf(ref, max, "%"PRIu64, val[i]);
+        sprintf(ref, "%"PRIu64, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -190,7 +191,7 @@
     /* test conversion to hexadecimal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_UInt64ToStrBase(val[i], str, max, 16);
-        snprintf(ref, max, "%"PRIX64, val[i]);
+        sprintf(ref, "%"PRIX64, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -198,7 +199,7 @@
     /* test conversion to octal numbers */
     for (i = 0; i < N; i++) {
         len = SCPI_UInt64ToStrBase(val[i], str, max, 8);
-        snprintf(ref, max, "%"PRIo64, val[i]);
+        sprintf(ref, "%"PRIo64, val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -227,7 +228,11 @@
 
 static void test_scpi_dtostre() {
     const size_t strsize = 49 + 1;
-    double val[] = {NAN, INFINITY, -INFINITY, 0,
+    double val[] = {
+#ifdef INFINITY
+        INFINITY, -INFINITY,
+#endif
+        0,
         1, 1.1, 1.01, 1.001, 1.0001, 1.00001, 1.000001, 1.0000001, 1.00000001,
         1.000000001, 1.0000000001, 1.00000000001, 1.000000000001,
         1.0000000000001, 1e-5, 1.1e-5, 1.01e-5, 1.001e-5, 1.0001e-5, 1.00001e-5,
@@ -260,10 +265,38 @@
 
     for (i = 0; i < N; i++) {
         len = strlen(SCPI_dtostre(val[i], str, strsize, 15, 0));
-        snprintf(ref, strsize, "%.15lg", val[i]);
+        sprintf(ref, "%.15lg", val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
+
+    for (i = 0; i < N; i++) {
+        len = strlen(SCPI_dtostre(val[i], str, 2, 15, 0));
+        snprintf(ref, 2, "%.15lg", val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+    for (i = 0; i < N; i++) {
+        len = strlen(SCPI_dtostre(val[i], str, 12, 15, 0));
+        snprintf(ref, 12, "%.15lg", val[i]);
+        CU_ASSERT(len == strlen(ref));
+        CU_ASSERT_STRING_EQUAL(str, ref);
+    }
+
+#ifdef NAN
+    len = strlen(SCPI_dtostre(NAN, str, strsize, 15, 0));
+    strncpy(ref, "nan", strsize);
+    CU_ASSERT(len == strlen(ref));
+    CU_ASSERT_STRING_EQUAL(str, ref);
+
+    len = strlen(SCPI_dtostre(NAN, str, 2, 15, 0));
+    strncpy(ref, "nan", 2);
+    ref[2 - 1] = '\0';
+    CU_ASSERT(len == strlen(ref));
+    CU_ASSERT_STRING_EQUAL(str, ref);
+#endif
+
 }
 
 static void test_floatToStr() {
@@ -277,7 +310,7 @@
 
     for (i = 0; i < N; i++) {
         len = SCPI_FloatToStr(val[i], str, max);
-        snprintf(ref, max, "%g", val[i]);
+        sprintf(ref, "%g", val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }
@@ -294,7 +327,7 @@
 
     for (i = 0; i < N; i++) {
         len = SCPI_DoubleToStr(val[i], str, max);
-        snprintf(ref, max, "%.15lg", val[i]);
+        sprintf(ref, "%.15lg", val[i]);
         CU_ASSERT(len == strlen(ref));
         CU_ASSERT_STRING_EQUAL(str, ref);
     }

--
Gitblit v1.9.1