<commit_before>/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 *
 * Licensed 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 "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "mbed.h"
#include <LittleFileSystem.h>
#include "HeapBlockDevice.h"

using utest::v1::Case;

/** Test error count and reset functionality
 */
void test_error_count_and_reset()
{
    int count = 7;
    
    //Log multiple errors and get the error count and make sure its 15
    for(int i=0; i<count; i++) {
        MBED_WARNING1(MBED_ERROR_OUT_OF_MEMORY, "Out of memory", i);
    }
    
    TEST_ASSERT_EQUAL_INT(count, mbed_get_error_count());
    
    //clear the errors and error count to 0
    mbed_clear_all_errors();
    
    //Now the error count should be 0
    TEST_ASSERT_EQUAL_INT(0, mbed_get_error_count());
    
}

/** Test error type encoding and test capturing of system, custom, posix errors
 *  and ensure the status/error code/type/error value is correct
 */
void test_error_capturing()
{
    uint32_t error_value = 0xAA11BB22;
    mbed_error_ctx error_ctx = {0};
    
    //first clear all errors and start afresh
    
    MBED_WARNING1(MBED_ERROR_OUT_OF_RESOURCES, "System type error", 0x1100 );
    mbed_error_status_t lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_SYSTEM, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_UNKNOWN, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_OUT_OF_RESOURCES, MBED_GET_ERROR_CODE(lastError));
    
    mbed_error_status_t error = MBED_MAKE_ERROR(MBED_MODULE_DRIVER_SERIAL, MBED_ERROR_CODE_OUT_OF_RESOURCES);
    MBED_WARNING1(error, "Error Serial", 0xAA );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(error, lastError);
    
    error = MBED_MAKE_CUSTOM_ERROR(MBED_MODULE_APPLICATION, MBED_ERROR_CODE_UNKNOWN);
    MBED_WARNING1(error, "Custom Error Unknown", 0x1234 );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(error, lastError);
    
    MBED_WARNING1(MBED_ERROR_EPERM, "Posix Error Eperm", 0x1234 );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_EPERM, lastError);
    
    error = MBED_MAKE_CUSTOM_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_CREATE_FAILED);
    MBED_WARNING1(error, "Custom Error Type", error_value);
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_CUSTOM, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_PLATFORM, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_CREATE_FAILED, MBED_GET_ERROR_CODE(lastError));
    mbed_error_status_t status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    
    error_value = 0xAABBCC;
    MBED_WARNING1(MBED_ERROR_EACCES, "Posix Error", error_value );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_POSIX, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_UNKNOWN, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_EACCES, MBED_GET_ERROR_CODE(lastError));
    status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    
    error_value = 0;
    error = MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNKNOWN);
    MBED_WARNING1(error, "HAL Entity error", error_value );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_SYSTEM, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_HAL, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_UNKNOWN, MBED_GET_ERROR_CODE(lastError));
    status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    
    MBED_WARNING1(MBED_ERROR_MUTEX_LOCK_FAILED, "Mutex lock failed", 0x4455 );
    error = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_MUTEX_LOCK_FAILED, error);
    
    error = mbed_get_first_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_OUT_OF_RESOURCES, error);
    
}

/** Test error context capture
 */
void test_error_context_capture()
{
    uint32_t error_value = 0xABCD;
    mbed_error_ctx error_ctx = {0};
    
    MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "System type error", error_value );
    mbed_error_status_t status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    TEST_ASSERT_EQUAL_UINT(osThreadGetId(), error_ctx.thread_id);
    
    //Capture thread info and compare
    osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
    TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->thread_addr, error_ctx.thread_entry_address);
    TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->stack_size, error_ctx.thread_stack_size);
    TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->stack_mem, error_ctx.thread_stack_mem);
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
    TEST_ASSERT_EQUAL_STRING(MBED_FILENAME, error_ctx.error_filename);
#endif    
}

#ifndef MBED_CONF_ERROR_HIST_DISABLED
/** Test error logging functionality
 */
void test_error_logging()
{
    mbed_error_ctx error_ctx = {0};<commit_msg>Corrected casting issue with an assert within the error_handling test<commit_after>/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 *
 * Licensed 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 "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "mbed.h"
#include <LittleFileSystem.h>
#include "HeapBlockDevice.h"

using utest::v1::Case;

/** Test error count and reset functionality
 */
void test_error_count_and_reset()
{
    int count = 7;
    
    //Log multiple errors and get the error count and make sure its 15
    for(int i=0; i<count; i++) {
        MBED_WARNING1(MBED_ERROR_OUT_OF_MEMORY, "Out of memory", i);
    }
    
    TEST_ASSERT_EQUAL_INT(count, mbed_get_error_count());
    
    //clear the errors and error count to 0
    mbed_clear_all_errors();
    
    //Now the error count should be 0
    TEST_ASSERT_EQUAL_INT(0, mbed_get_error_count());
    
}

/** Test error type encoding and test capturing of system, custom, posix errors
 *  and ensure the status/error code/type/error value is correct
 */
void test_error_capturing()
{
    uint32_t error_value = 0xAA11BB22;
    mbed_error_ctx error_ctx = {0};
    
    //first clear all errors and start afresh
    
    MBED_WARNING1(MBED_ERROR_OUT_OF_RESOURCES, "System type error", 0x1100 );
    mbed_error_status_t lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_SYSTEM, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_UNKNOWN, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_OUT_OF_RESOURCES, MBED_GET_ERROR_CODE(lastError));
    
    mbed_error_status_t error = MBED_MAKE_ERROR(MBED_MODULE_DRIVER_SERIAL, MBED_ERROR_CODE_OUT_OF_RESOURCES);
    MBED_WARNING1(error, "Error Serial", 0xAA );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(error, lastError);
    
    error = MBED_MAKE_CUSTOM_ERROR(MBED_MODULE_APPLICATION, MBED_ERROR_CODE_UNKNOWN);
    MBED_WARNING1(error, "Custom Error Unknown", 0x1234 );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(error, lastError);
    
    MBED_WARNING1(MBED_ERROR_EPERM, "Posix Error Eperm", 0x1234 );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_EPERM, lastError);
    
    error = MBED_MAKE_CUSTOM_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_CREATE_FAILED);
    MBED_WARNING1(error, "Custom Error Type", error_value);
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_CUSTOM, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_PLATFORM, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_CREATE_FAILED, MBED_GET_ERROR_CODE(lastError));
    mbed_error_status_t status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    
    error_value = 0xAABBCC;
    MBED_WARNING1(MBED_ERROR_EACCES, "Posix Error", error_value );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_POSIX, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_UNKNOWN, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_EACCES, MBED_GET_ERROR_CODE(lastError));
    status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    
    error_value = 0;
    error = MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNKNOWN);
    MBED_WARNING1(error, "HAL Entity error", error_value );
    lastError = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_SYSTEM, MBED_GET_ERROR_TYPE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_MODULE_HAL, MBED_GET_ERROR_MODULE(lastError));
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_UNKNOWN, MBED_GET_ERROR_CODE(lastError));
    status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    
    MBED_WARNING1(MBED_ERROR_MUTEX_LOCK_FAILED, "Mutex lock failed", 0x4455 );
    error = mbed_get_last_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_MUTEX_LOCK_FAILED, error);
    
    error = mbed_get_first_error();
    TEST_ASSERT_EQUAL_UINT(MBED_ERROR_OUT_OF_RESOURCES, error);
    
}

/** Test error context capture
 */
void test_error_context_capture()
{
    uint32_t error_value = 0xABCD;
    mbed_error_ctx error_ctx = {0};
    
    MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "System type error", error_value );
    mbed_error_status_t status = mbed_get_last_error_info( &error_ctx );
    TEST_ASSERT(status == MBED_SUCCESS);
    TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
    TEST_ASSERT_EQUAL_UINT((uint32_t)osThreadGetId(), error_ctx.thread_id);
    
    //Capture thread info and compare
    osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
    TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->thread_addr, error_ctx.thread_entry_address);
    TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->stack_size, error_ctx.thread_stack_size);
    TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->stack_mem, error_ctx.thread_stack_mem);
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
    TEST_ASSERT_EQUAL_STRING(MBED_FILENAME, error_ctx.error_filename);
#endif    
}

#ifndef MBED_CONF_ERROR_HIST_DISABLED
/** Test error logging functionality
 */
void test_error_logging()
{
    mbed_error_ctx error_ctx = {0};
{'fraction_non_alphanumeric': 0.08414023372287145, 'fraction_numerical': 0.011519198664440735, 'mean_word_length': 5.339153439153439, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3620055', 'n_tokens_mistral': 4255, 'n_tokens_neox': 4157, 'n_words': 760}