<commit_before>/* Copyright 2022 The ChromiumOS Authors.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test standard library functions.
 */

#include "common.h"
#include "console.h"
#include "system.h"
#include "printf.h"
#include "shared_mem.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

static int test_isalpha(void)
{
	TEST_ASSERT(isalpha('a'));
	TEST_ASSERT(isalpha('z'));
	TEST_ASSERT(isalpha('A'));
	TEST_ASSERT(isalpha('Z'));
	TEST_ASSERT(!isalpha('0'));
	TEST_ASSERT(!isalpha('~'));
	TEST_ASSERT(!isalpha(' '));
	TEST_ASSERT(!isalpha('\0'));
	TEST_ASSERT(!isalpha('\n'));
	return EC_SUCCESS;
}

static int test_isprint(void)
{
	TEST_ASSERT(isprint('a'));
	TEST_ASSERT(isprint('z'));
	TEST_ASSERT(isprint('A'));
	TEST_ASSERT(isprint('Z'));
	TEST_ASSERT(isprint('0'));
	TEST_ASSERT(isprint('~'));
	TEST_ASSERT(isprint(' '));
	TEST_ASSERT(!isprint('\0'));
	TEST_ASSERT(!isprint('\n'));
	return EC_SUCCESS;
}

static int test_strstr(void)
{
	const char s1[] = "abcde";

	TEST_ASSERT(strstr(s1, "ab") == s1);
	TEST_ASSERT(strstr(s1, "") == NULL);
	TEST_ASSERT(strstr("", "ab") == NULL);
	TEST_ASSERT(strstr("", "x") == NULL);
	TEST_ASSERT(strstr(s1, "de") == &s1[3]);
	TEST_ASSERT(strstr(s1, "def") == NULL);

	return EC_SUCCESS;
}

static int test_strtoull(void)
{
	char *e;

	TEST_ASSERT(strtoull("10", &e, 0) == 10);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("010", &e, 0) == 8);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+010", &e, 0) == 8);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-010", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0x1f z", &e, 0) == 31);
	TEST_ASSERT(e && (*e == ' '));
	TEST_ASSERT(strtoull("0X1f z", &e, 0) == 31);
	TEST_ASSERT(e && (*e == ' '));
	TEST_ASSERT(strtoull("10a", &e, 16) == 266);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("0x02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0x02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0x02C", &e, 16) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0x02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0x02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0x02C", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0X02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0X02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0X02C", &e, 16) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0X02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0X02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0X02C", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("   -12", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0x!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0X!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));

	return EC_SUCCESS;
}

static int test_strncpy(void)
{
	char dest[10];

	strncpy(dest, "test", 10);
	TEST_ASSERT_ARRAY_EQ("test", dest, 5);
	strncpy(dest, "12345", 6);
	TEST_ASSERT_ARRAY_EQ("12345", dest, 6);
	/*
	 * gcc complains:
	 * error: ‘__builtin_strncpy’ output truncated copying 10 bytes from a
	 * string of length 12 [-Werror=stringop-truncation]
	 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
	strncpy(dest, "testtesttest", 10);
#pragma GCC diagnostic pop
	TEST_ASSERT_ARRAY_EQ("testtestte", dest, 10);

	return EC_SUCCESS;
}

static int test_strncmp(void)
{
	TEST_ASSERT(strncmp("123", "123", 8) == 0);
	TEST_ASSERT(strncmp("789", "456", 8) > 0);
	TEST_ASSERT(strncmp("abc", "abd", 4) < 0);
	TEST_ASSERT(strncmp("abc", "abd", 2) == 0);
	return EC_SUCCESS;
}

static int test_strlen(void)
{
	TEST_ASSERT(strlen("this is a string") == 16);
	return EC_SUCCESS;
}

static int test_strnlen(void)<commit_msg>ec: Fix test if build with clang<commit_after>/* Copyright 2022 The ChromiumOS Authors.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test standard library functions.
 */

#include "common.h"
#include "console.h"
#include "system.h"
#include "printf.h"
#include "shared_mem.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

static int test_isalpha(void)
{
	TEST_ASSERT(isalpha('a'));
	TEST_ASSERT(isalpha('z'));
	TEST_ASSERT(isalpha('A'));
	TEST_ASSERT(isalpha('Z'));
	TEST_ASSERT(!isalpha('0'));
	TEST_ASSERT(!isalpha('~'));
	TEST_ASSERT(!isalpha(' '));
	TEST_ASSERT(!isalpha('\0'));
	TEST_ASSERT(!isalpha('\n'));
	return EC_SUCCESS;
}

static int test_isprint(void)
{
	TEST_ASSERT(isprint('a'));
	TEST_ASSERT(isprint('z'));
	TEST_ASSERT(isprint('A'));
	TEST_ASSERT(isprint('Z'));
	TEST_ASSERT(isprint('0'));
	TEST_ASSERT(isprint('~'));
	TEST_ASSERT(isprint(' '));
	TEST_ASSERT(!isprint('\0'));
	TEST_ASSERT(!isprint('\n'));
	return EC_SUCCESS;
}

static int test_strstr(void)
{
	const char s1[] = "abcde";

	TEST_ASSERT(strstr(s1, "ab") == s1);
	TEST_ASSERT(strstr(s1, "") == NULL);
	TEST_ASSERT(strstr("", "ab") == NULL);
	TEST_ASSERT(strstr("", "x") == NULL);
	TEST_ASSERT(strstr(s1, "de") == &s1[3]);
	TEST_ASSERT(strstr(s1, "def") == NULL);

	return EC_SUCCESS;
}

static int test_strtoull(void)
{
	char *e;

	TEST_ASSERT(strtoull("10", &e, 0) == 10);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("010", &e, 0) == 8);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+010", &e, 0) == 8);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-010", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0x1f z", &e, 0) == 31);
	TEST_ASSERT(e && (*e == ' '));
	TEST_ASSERT(strtoull("0X1f z", &e, 0) == 31);
	TEST_ASSERT(e && (*e == ' '));
	TEST_ASSERT(strtoull("10a", &e, 16) == 266);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("0x02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0x02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0x02C", &e, 16) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0x02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0x02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0x02C", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0X02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0X02C", &e, 16) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0X02C", &e, 16) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("0X02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("+0X02C", &e, 0) == 44);
	TEST_ASSERT(e && (*e == '\0'));
	TEST_ASSERT(strtoull("-0X02C", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("   -12", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '-'));
	TEST_ASSERT(strtoull("!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0x!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));
	TEST_ASSERT(strtoull("+0X!", &e, 0) == 0);
	TEST_ASSERT(e && (*e == '!'));

	return EC_SUCCESS;
}

static int test_strncpy(void)
{
	char dest[10];

	strncpy(dest, "test", 10);
	TEST_ASSERT_ARRAY_EQ("test", dest, 5);
	strncpy(dest, "12345", 6);
	TEST_ASSERT_ARRAY_EQ("12345", dest, 6);
	/*
	 * gcc complains:
	 * error: ‘__builtin_strncpy’ output truncated copying 10 bytes from a
	 * string of length 12 [-Werror=stringop-truncation]
	 */
#pragma GCC diagnostic push
#if defined(__GNUC__) && __GNUC__ >= 8
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
	strncpy(dest, "testtesttest", 10);
#pragma GCC diagnostic pop
	TEST_ASSERT_ARRAY_EQ("testtestte", dest, 10);

	return EC_SUCCESS;
}

static int test_strncmp(void)
{
	TEST_ASSERT(strncmp("123", "123", 8) == 0);
	TEST_ASSERT(strncmp("789", "456", 8) > 0);
	TEST_ASSERT(strncmp("abc", "abd", 4) < 0);
	TEST_ASSERT(strncmp("abc", "abd", 2) == 0);
	return EC_SUCCESS;
}

static int test_strlen(void)
{
	TEST_ASSERT(strlen("this is a string") == 16);
	return EC_SUCCESS;
}

static int test_strnlen(void)
{'fraction_non_alphanumeric': 0.24944761018723108, 'fraction_numerical': 0.04733108500988487, 'mean_word_length': 5.437125748502994, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2522556', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3677, 'n_words': 728}