| | |
| | | /*- |
| | | * 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" |
| | | |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | /* 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); |
| | | } |
| | |
| | | |
| | | 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, |
| | |
| | | |
| | | 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() { |
| | |
| | | |
| | | 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); |
| | | } |
| | |
| | | |
| | | 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); |
| | | } |