tests: kernel: import libs test to unified kernel

This tests access to standard libraries. Adapted
to use ZTEST.

Jira: ZEP-932

Change-Id: I3564bfa61221b2456323c1018402237b6129b5ca
Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Jithu Joseph 2017-01-31 17:38:26 -08:00 committed by Anas Nashif
commit 10e36609b6
6 changed files with 342 additions and 0 deletions

View file

@ -0,0 +1,4 @@
BOARD ?= qemu_x86
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.test

View file

@ -0,0 +1,55 @@
Title: Kernel Access to Standard Libraries
Description:
This test verifies kernel access to the standard C libraries.
It is intended to catch issues in which a library is completely absent
or non-functional, and is NOT intended to be a comprehensive test suite
of all functionality provided by the libraries.
--------------------------------------------------------------------------------
Building and Running Project:
This project outputs to the console. It can be built and executed
on QEMU as follows:
make run
--------------------------------------------------------------------------------
Troubleshooting:
Problems caused by out-dated project information can be addressed by
issuing one of the following commands then rebuilding the project:
make clean # discard results of previous builds
# but keep existing configuration info
or
make pristine # discard results of previous builds
# and restore pre-defined configuration info
--------------------------------------------------------------------------------
Sample Output:
***** BOOTING ZEPHYR OS vxxxx - BUILD: xxxx *****
Running test suite test_libs
tc_start() - limits_test
===================================================================
PASS - limits_test.
tc_start() - stdbool_test
===================================================================
PASS - stdbool_test.
tc_start() - stddef_test
===================================================================
PASS - stddef_test.
tc_start() - stdint_test
===================================================================
PASS - stdint_test.
tc_start() - string_test
===================================================================
PASS - string_test.
===================================================================
PROJECT EXECUTION SUCCESSFUL

View file

@ -0,0 +1 @@
CONFIG_ZTEST=y

View file

@ -0,0 +1,3 @@
include $(ZEPHYR_BASE)/tests/Makefile.test
obj-y = libraries.o

View file

@ -0,0 +1,277 @@
/*
* Copyright (c) 2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* @file test access to the minimal C libraries
*
* This module verifies that the various minimal C libraries can be used.
*
* IMPORTANT: The module only ensures that each supported library is present,
* and that a bare minimum of its functionality is operating correctly. It does
* NOT guarantee that ALL standards-defined functionality is present, nor does
* it guarantee that ALL functionality provided is working correctly.
*/
#include <zephyr.h>
#include <misc/__assert.h>
#include <ztest.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/*
* variables used during limits library testing; must be marked as "volatile"
* to prevent compiler from computing results at compile time
*/
volatile long long_max = LONG_MAX;
volatile long long_one = 1L;
/**
*
* @brief Test implementation-defined constants library
*
*/
void limits_test(void)
{
assert_true((long_max + long_one == LONG_MIN), NULL);
}
/**
*
* @brief Test boolean types and values library
*
*/
void stdbool_test(void)
{
assert_true((true == 1), "true value");
assert_true((false == 0), "false value");
}
/*
* variables used during stddef library testing; must be marked as "volatile"
* to prevent compiler from computing results at compile time
*/
volatile long long_variable;
volatile size_t size_of_long_variable = sizeof(long_variable);
/**
*
* @brief Test standard type definitions library
*
*/
void stddef_test(void)
{
assert_true((size_of_long_variable == 4), "sizeof");
}
/*
* variables used during stdint library testing; must be marked as "volatile"
* to prevent compiler from computing results at compile time
*/
volatile uint8_t unsigned_byte = 0xff;
volatile uint32_t unsigned_int = 0xffffff00;
/**
*
* @brief Test integer types library
*
*/
void stdint_test(void)
{
assert_true((unsigned_int + unsigned_byte + 1u == 0), NULL);
}
/*
* variables used during string library testing
*/
#define BUFSIZE 10
char buffer[BUFSIZE];
/**
*
* @brief Test string memset
*
*/
void memset_test(void)
{
memset(buffer, 'a', BUFSIZE);
assert_true((buffer[0] == 'a'), "memset");
assert_true((buffer[BUFSIZE - 1] == 'a'), "memset");
}
/**
*
* @brief Test string length function
*
*/
void strlen_test(void)
{
memset(buffer, '\0', BUFSIZE);
memset(buffer, 'b', 5); /* 5 is BUFSIZE / 2 */
assert_equal(strlen(buffer), 5, "strlen");
}
/**
*
* @brief Test string compare function
*
*/
void strcmp_test(void)
{
strcpy(buffer, "eeeee");
assert_true((strcmp(buffer, "fffff") < 0), "strcmp less ...");
assert_true((strcmp(buffer, "eeeee") == 0), "strcmp equal ...");
assert_true((strcmp(buffer, "ddddd") > 0), "strcmp greater ...");
}
/**
*
* @brief Test string N compare function
*
*/
void strncmp_test(void)
{
const char pattern[] = "eeeeeeeeeeee";
/* Note we don't want to count the final \0 that sizeof will */
__ASSERT_NO_MSG(sizeof(pattern) - 1 > BUFSIZE);
memcpy(buffer, pattern, BUFSIZE);
assert_true((strncmp(buffer, "fffff", 0) == 0), "strncmp 0");
assert_true((strncmp(buffer, "eeeff", 3) == 0), "strncmp 3");
assert_true((strncmp(buffer, "eeeeeeeeeeeff", BUFSIZE) == 0),
"strncmp 10");
}
/**
*
* @brief Test string copy function
*
*/
void strcpy_test(void)
{
memset(buffer, '\0', BUFSIZE);
strcpy(buffer, "10 chars!\0");
assert_true((strcmp(buffer, "10 chars!\0") == 0), "strcpy");
}
/**
*
* @brief Test string N copy function
*
*/
void strncpy_test(void)
{
int ret;
memset(buffer, '\0', BUFSIZE);
strncpy(buffer, "This is over 10 characters", BUFSIZE);
/* Purposely different values */
ret = strncmp(buffer, "This is over 20 characters", BUFSIZE);
assert_true((ret == 0), "strncpy");
}
/**
*
* @brief Test string scanning function
*
*/
void strchr_test(void)
{
char *rs = NULL;
int ret;
memset(buffer, '\0', BUFSIZE);
strncpy(buffer, "Copy 10", BUFSIZE);
rs = strchr(buffer, '1');
assert_not_null(rs, "strchr");
ret = strncmp(rs, "10", 2);
assert_true((ret == 0), "strchr");
}
/**
*
* @brief Test memory comparison function
*
*/
void memcmp_test(void)
{
int ret;
unsigned char m1[5] = { 1, 2, 3, 4, 5 };
unsigned char m2[5] = { 1, 2, 3, 4, 6 };
ret = memcmp(m1, m2, 4);
assert_true((ret == 0), "memcmp 4");
ret = memcmp(m1, m2, 5);
assert_true((ret != 0), "memcmp 5");
}
/**
*
* @brief Test string operations library
*/
void string_test(void)
{
memset_test();
strlen_test();
strcmp_test();
strcpy_test();
strncpy_test();
strncmp_test();
strchr_test();
memcmp_test();
}
void test_main(void)
{
ztest_test_suite(test_libs,
ztest_unit_test(limits_test),
ztest_unit_test(stdbool_test),
ztest_unit_test(stddef_test),
ztest_unit_test(stdint_test),
ztest_unit_test(string_test));
ztest_run_test_suite(test_libs);
}

View file

@ -0,0 +1,2 @@
[test]
tags = bat_commit core