This is an automated email from the ASF dual-hosted git repository. cederom pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push: new f139e56cd testing/libc/wcstombs: Add testing application for wcstombs f139e56cd is described below commit f139e56cd62a30d6edcd7207c7e4cbc6e9b8b7d1 Author: Tiago Medicci <tiago.medi...@espressif.com> AuthorDate: Wed Feb 5 10:39:19 2025 -0300 testing/libc/wcstombs: Add testing application for wcstombs This application test the libc's `wcstombs` function for different len sizes (bigger than the converted string, exactly the size of it and smaller than it). --- testing/libc/wcstombs/CMakeLists.txt | 35 ++++++ testing/libc/wcstombs/Kconfig | 30 +++++ testing/libc/wcstombs/Make.defs | 25 ++++ testing/libc/wcstombs/Makefile | 36 ++++++ testing/libc/wcstombs/wcstombs_main.c | 224 ++++++++++++++++++++++++++++++++++ 5 files changed, 350 insertions(+) diff --git a/testing/libc/wcstombs/CMakeLists.txt b/testing/libc/wcstombs/CMakeLists.txt new file mode 100644 index 000000000..c58cc1f2d --- /dev/null +++ b/testing/libc/wcstombs/CMakeLists.txt @@ -0,0 +1,35 @@ +# ############################################################################## +# apps/testing/libc/wcstombs/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_TESTING_WCSTOMBS) + nuttx_add_application( + NAME + ${CONFIG_TESTING_WCSTOMBS_PROGNAME} + PRIORITY + ${CONFIG_TESTING_WCSTOMBS_PRIORITY} + STACKSIZE + ${CONFIG_TESTING_WCSTOMBS_STACKSIZE} + MODULE + ${CONFIG_TESTING_WCSTOMBS} + SRCS + wcstombs_main.c) +endif() diff --git a/testing/libc/wcstombs/Kconfig b/testing/libc/wcstombs/Kconfig new file mode 100644 index 000000000..41d72b507 --- /dev/null +++ b/testing/libc/wcstombs/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_WCSTOMBS + tristate "wcstombs() test" + default n + depends on LIBC_LOCALE + ---help--- + Enable wcstombs() test + +if TESTING_WCSTOMBS + +config TESTING_WCSTOMBS_PROGNAME + string "Program name" + default "wcstombs" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config TESTING_WCSTOMBS_PRIORITY + int "wcstombs task priority" + default 100 + +config TESTING_WCSTOMBS_STACKSIZE + int "wcstombs stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/testing/libc/wcstombs/Make.defs b/testing/libc/wcstombs/Make.defs new file mode 100644 index 000000000..3c1ac4c83 --- /dev/null +++ b/testing/libc/wcstombs/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/testing/libc/wcstombs/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_TESTING_WCSTOMBS),) +CONFIGURED_APPS += $(APPDIR)/testing/libc/wcstombs +endif diff --git a/testing/libc/wcstombs/Makefile b/testing/libc/wcstombs/Makefile new file mode 100644 index 000000000..a5fb15ddc --- /dev/null +++ b/testing/libc/wcstombs/Makefile @@ -0,0 +1,36 @@ +############################################################################ +# apps/testing/libc/wcstombs/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# wcstombs() test built-in application info + +PROGNAME = $(CONFIG_TESTING_WCSTOMBS_PROGNAME) +PRIORITY = $(CONFIG_TESTING_WCSTOMBS_PRIORITY) +STACKSIZE = $(CONFIG_TESTING_WCSTOMBS_STACKSIZE) +MODULE = $(CONFIG_TESTING_WCSTOMBS) + +# wcstombs test files + +MAINSRC = wcstombs_main.c + +include $(APPDIR)/Application.mk diff --git a/testing/libc/wcstombs/wcstombs_main.c b/testing/libc/wcstombs/wcstombs_main.c new file mode 100644 index 000000000..73f5e504e --- /dev/null +++ b/testing/libc/wcstombs/wcstombs_main.c @@ -0,0 +1,224 @@ +/**************************************************************************** + * apps/testing/libc/wcstombs/wcstombs_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <wchar.h> +#include <locale.h> +#include <stdint.h> +#include <string.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(void) +{ + /* Local variable declarations */ + + const wchar_t *src; + size_t dst_size; + char *dst; + size_t ret; + size_t i; + + printf("wcstombs Test application:\n"); + + /* Set the locale to the user's default locale */ + + setlocale(LC_ALL, ""); + + /* Example wide character array (source) */ + + src = L"Hello, world!"; + + /* Calculate the required size for the dst buffer */ + + dst_size = wcstombs(NULL, src, 0) + 1; /* +1 for the null terminator */ + dst = (char *)malloc(dst_size); + + if (dst == NULL) + { + printf("ERROR: malloc failed.\n"); + return EXIT_FAILURE; + } + + printf("\nTest Scenario: len is bigger than the size of the converted " + "string. Expected the null-terminator at the end of the converted " + "string.\n"); + + /* Initialize dst with a known value (0xaa) */ + + memset(dst, 0xaa, dst_size); + + /* Convert wide characters to multibyte characters */ + + ret = wcstombs(dst, src, dst_size); + + /* Check if the conversion was successful */ + + if (ret == (size_t)-1) + { + printf("ERROR: wcstombs failed.\n"); + free(dst); + return EXIT_FAILURE; + } + + /* Print the return code */ + + printf("Return code: %zu\n", ret); + + /* Print the dst buffer as an array of uint8_t elements */ + + printf("dst buffer (as uint8_t array): "); + for (i = 0; i < dst_size; i++) /* Include the null terminator in the output */ + { + printf("%02x ", (uint8_t)dst[i]); + } + + printf("\n"); + + /* Check if the dst value just after the return value is as expected */ + + if (dst[ret] == '\0') + { + printf("The character just after the return value is the null " + "terminating character.\n"); + } + else + { + printf("The character just after the return value is not the expected " + "null-terminator (value: %02x). This is a bug!\n", dst[ret]); + free(dst); + return EXIT_FAILURE; + } + + printf("\nTest Scenario: len is exactly the size of the converted string. " + "Do not expected the null-terminator at the end of the converted " + "string.\n"); + + /* Initialize dst with a known value (0xaa) */ + + memset(dst, 0xaa, dst_size); + + /* Convert wide characters to multibyte characters */ + + ret = wcstombs(dst, src, dst_size - 1); + + /* Check if the conversion was successful */ + + if (ret == (size_t)-1) + { + printf("ERROR: wcstombs failed.\n"); + free(dst); + return EXIT_FAILURE; + } + + /* Print the return code */ + + printf("Return code: %zu\n", ret); + + /* Print the dst buffer as an array of uint8_t elements */ + + printf("dst buffer (as uint8_t array): "); + for (i = 0; i < dst_size; i++) /* Include the null terminator in the output */ + { + printf("%02x ", (uint8_t)dst[i]); + } + + printf("\n"); + + /* Check if the dst value just after the return value is as expected */ + + if ((uint8_t)dst[ret] == 0xaa) + { + printf("The character just after the return value is the expected " + "0xaa value. No null-terminator.\n"); + } + else + { + printf("The character just after the return value is not the expected " + "0xaa (value: %02x). This is a bug!\n", dst[ret]); + free(dst); + return EXIT_FAILURE; + } + + printf("\nTest Scenario: len is smaller than the size of the converted " + " string. Do not expected the null-terminator at the end of the " + "converted string.\n"); + + /* Initialize dst with a known value (0xaa) */ + + memset(dst, 0xaa, dst_size); + + /* Convert wide characters to multibyte characters */ + + ret = wcstombs(dst, src, dst_size - 2); + + /* Check if the conversion was successful */ + + if (ret == (size_t)-1) + { + printf("ERROR: wcstombs failed.\n"); + free(dst); + return EXIT_FAILURE; + } + + /* Print the return code */ + + printf("Return code: %zu\n", ret); + + /* Print the dst buffer as an array of uint8_t elements */ + + printf("dst buffer (as uint8_t array): "); + for (i = 0; i < dst_size; i++) /* Include the null terminator in the output */ + { + printf("%02x ", (uint8_t)dst[i]); + } + + printf("\n"); + + /* Check if the dst value just after the return value is as expected */ + + if ((uint8_t)dst[ret] == 0xaa) + { + printf("The character just after the return value is the expected " + "0xaa value. No null-terminator.\n"); + } + else + { + printf("The character just after the return value is not the expected " + "0xaa (value: %02x). This is a bug!\n", dst[ret]); + free(dst); + return EXIT_FAILURE; + } + + /* Free the allocated memory */ + + free(dst); + + return EXIT_SUCCESS; +}