int umockcallpairs_track_destroy_paired_call()

in src/umockcallpairs.c [108:187]


int umockcallpairs_track_destroy_paired_call(PAIRED_HANDLES* paired_handles, const void* handle)
{
    size_t i;
    int result;

    if ((paired_handles == NULL) ||
        (handle == NULL))
    {
        /* Codes_SRS_UMOCKCALLPAIRS_01_010: [ If any of the arguments is NULL, umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
        result = __LINE__;
        UMOCK_LOG("umock_track_create_destroy_paired_calls_free: are_equal failed");
    }
    else
    {
        unsigned char is_error = 0;

        /* Codes_SRS_UMOCKCALLPAIRS_01_008: [ umockcallpairs_track_destroy_paired_call shall remove from the paired handles array pointed by the paired_handles field the entry that is associated with the handle passed in the handle argument. ]*/
        for (i = 0; i < paired_handles->paired_handle_count; i++)
        {
            /* Codes_SRS_UMOCKCALLPAIRS_01_013: [ When looking up which entry to remove, the comparison of the handle values shall be done by calling umocktypes_are_equal. ]*/
            int are_equal_result = umocktypes_are_equal(paired_handles->paired_handles[i].handle_type, paired_handles->paired_handles[i].handle_value, handle);
            if (are_equal_result < 0)
            {
                /* Codes_SRS_UMOCKCALLPAIRS_01_014: [ If umocktypes_are_equal fails, umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
                result = __LINE__;
                is_error = 1;
                UMOCK_LOG("umock_track_create_destroy_paired_calls_free: are_equal failed");
                break;
            }
            else
            {
                if (are_equal_result == 1)
                {
                    break;
                }
            }
        }

        if (i == paired_handles->paired_handle_count)
        {
            /* Codes_SRS_UMOCKCALLPAIRS_01_015: [ If the handle is not found in the array then umockcallpairs_track_destroy_paired_call shall fail and return a non-zero value. ]*/
            result = __LINE__;
            UMOCK_LOG("umock_track_create_destroy_paired_calls_free: could not find handle");
        }
        else
        {
            if (!is_error)
            {
                /* Codes_SRS_UMOCKCALLPAIRS_01_011: [ umockcallpairs_track_destroy_paired_call shall free the memory pointed by the memory field in the PAIRED_HANDLES array entry associated with handle. ]*/
                umocktypes_free(paired_handles->paired_handles[i].handle_type, paired_handles->paired_handles[i].handle_value);
                umockalloc_free(paired_handles->paired_handles[i].handle_type);
                umockalloc_free(paired_handles->paired_handles[i].handle_value);
                if(i == paired_handles->paired_handle_count - 1)
                { 
                    /*don't move any memory if it is the last one*/
                }
                else
                {
                    (void)memmove(&paired_handles->paired_handles[i], &paired_handles->paired_handles[i + 1], sizeof(PAIRED_HANDLE) * (paired_handles->paired_handle_count - i - 1));
                }
                paired_handles->paired_handle_count--;
                if (paired_handles->paired_handle_count == 0)
                {
                    /* Codes_SRS_UMOCKCALLPAIRS_01_012: [ If the paired handles array is empty after removing the entry, the paired_handles field shall be freed and set to NULL. ]*/
                    umockalloc_free(paired_handles->paired_handles);
                    paired_handles->paired_handles = NULL;
                }

                /* Codes_SRS_UMOCKCALLPAIRS_01_009: [ On success umockcallpairs_track_destroy_paired_call shall return 0. ]*/
                result = 0;
            }
            else
            {
                result = __LINE__;
            }
        }
    }

    return result;
}