nancy.liao
2025-04-15 4c11ea0639b923a171e72a47b8006734ba184b3e
libscpi/test/test_scpi_utils.c
@@ -1,43 +1,36 @@
/*-
 * Copyright (c) 2013 Jan Breuer
 *                    Richard.hmm
 * Copyright (c) 2012 Jan Breuer
 * BSD 2-Clause License
 *
 * All Rights Reserved
 * Copyright (c) 2012-2018, Jan Breuer, Richard.hmm
 * 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.
 * modification, are permitted provided that the following conditions are met:
 *
 * 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:   test_scpi_utils.c
 * Author: Jan Breuer
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * Created on 26.11.2012, 11:22:00
 * * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include "CUnit/Basic.h"
@@ -71,7 +64,7 @@
static void test_Int32ToStr() {
    const size_t max = 32 + 1;
    int32_t val[] = {0, 1, -1, INT32_MIN, INT32_MAX, 0x01234567, 0x89abcdef};
    int32_t val[] = {0, 1, -1, INT32_MIN, INT32_MAX, 0x01234567, (int32_t)0x89abcdef};
    int N = sizeof (val) / sizeof (int32_t);
    int i;
    char str[max];
@@ -81,17 +74,17 @@
    /* 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);
    }
    int16_t val16[] = {0, 1, -1, INT16_MIN, INT16_MAX, 0x0123, 0x4567, 0x89ab, 0xcdef};
    int16_t val16[] = {0, 1, -1, INT16_MIN, INT16_MAX, 0x0123, 0x4567, (int16_t)0x89ab, (int16_t)0xcdef};
    int N16 = sizeof (val16) / sizeof (int16_t);
    /* 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);
    }
@@ -99,7 +92,7 @@
static void test_UInt32ToStrBase() {
    const size_t max = 32 + 1;
    uint32_t val[] = {0, 1, -1, INT32_MIN, INT32_MAX, 0x01234567, 0x89abcdef};
    uint32_t val[] = {0, 1, (uint32_t)-1, (uint32_t)INT32_MIN, INT32_MAX, 0x01234567, 0x89abcdef};
    int N = sizeof (val) / sizeof (uint32_t);
    int i;
    char str[max];
@@ -109,7 +102,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 +110,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 +118,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);
    }
@@ -154,7 +147,7 @@
static void test_Int64ToStr() {
    const size_t max = 64 + 1;
    int64_t val[] = {0, 1, -1, INT64_MIN, INT64_MAX, 0x0123456789abcdef, 0xfedcba9876543210};
    int64_t val[] = {0, 1, -1, INT64_MIN, INT64_MAX, 0x0123456789abcdef, (int64_t)0xfedcba9876543210};
    int N = sizeof (val) / sizeof (int64_t);
    int i;
    char str[max];
@@ -164,7 +157,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);
    }
@@ -172,7 +165,7 @@
static void test_UInt64ToStrBase() {
    const size_t max = 64 + 1;
    uint64_t val[] = {0, 1, -1, INT64_MIN, INT64_MAX, 0x0123456789abcdef, 0xfedcba9876543210};
    uint64_t val[] = {0, 1, (uint64_t)-1, (uint64_t)INT64_MIN, INT64_MAX, 0x0123456789abcdef, 0xfedcba9876543210};
    int N = sizeof (val) / sizeof (uint64_t);
    int i;
    char str[max];
@@ -182,7 +175,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 +183,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 +191,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 +220,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,
@@ -235,8 +232,8 @@
        1.0000000001e-5, 1.00000000001e-5, 1.000000000001e-5,
        1.0000000000001e-5, 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678,
        123456789, 1234567890, 12345678901, 123456789012, 1234567890123,
        12345678901234, 123456789012345, 1234567890123456, 12345678901234567,
        123456789012345678, 1234567890123456789, 1234567890123456789e1, 1.1,
        12345678901234, 123456789012345, 1234567890123456, (double)12345678901234567,
        (double)123456789012345678, (double)1234567890123456789, (double)1234567890123456789e1, 1.1,
        10.1, 100.1, 1000.1, 10000.1, 100000.1, 1000000.1, 10000000.1,
        100000000.1, 1000000000.1, 0.1234567890123456789,
        0.01234567890123456789, 0.001234567890123456789,
@@ -260,10 +257,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 +302,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 +319,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);
    }