clds/tests/lock_free_set_ut/lock_free_set_ut.c

1032 строки
33 KiB
C
Исходник Постоянная ссылка Обычный вид История

2019-03-19 01:10:24 +03:00
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.See LICENSE file in the project root for full license information.
2019-03-19 01:10:24 +03:00
#include <stdbool.h>
#include <stdlib.h>
#include "macro_utils/macro_utils.h"
2019-03-19 01:10:24 +03:00
#include "testrunnerswitcher.h"
2020-08-09 04:17:58 +03:00
#include "real_gballoc_ll.h"
2019-03-19 01:10:24 +03:00
void* real_malloc(size_t size)
{
2020-08-09 04:17:58 +03:00
return real_gballoc_ll_malloc(size);
2019-03-19 01:10:24 +03:00
}
void real_free(void* ptr)
{
2020-08-09 04:17:58 +03:00
real_gballoc_ll_free(ptr);
2019-03-19 01:10:24 +03:00
}
2019-04-12 08:46:36 +03:00
#include "umock_c/umock_c.h"
#include "umock_c/umocktypes_bool.h"
#include "c_pal/interlocked.h"
2019-03-19 01:10:24 +03:00
#define ENABLE_MOCKS
#include "c_pal/gballoc_hl.h"
#include "c_pal/gballoc_hl_redirect.h"
2019-03-19 01:10:24 +03:00
#undef ENABLE_MOCKS
#include "real_gballoc_hl.h"
#include "clds/lock_free_set.h"
2019-03-19 06:57:53 +03:00
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
2019-03-19 01:10:24 +03:00
static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
{
ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
2019-03-19 01:10:24 +03:00
}
MOCK_FUNCTION_WITH_CODE(, void, test_node_cleanup, void*, context, struct LOCK_FREE_SET_ITEM_TAG*, item)
MOCK_FUNCTION_END()
BEGIN_TEST_SUITE(TEST_SUITE_NAME_FROM_CMAKE)
2019-03-19 01:10:24 +03:00
TEST_SUITE_INITIALIZE(suite_init)
{
int result;
ASSERT_ARE_EQUAL(int, 0, real_gballoc_hl_init(NULL, NULL));
2019-03-19 01:10:24 +03:00
umock_c_init(on_umock_c_error);
result = umocktypes_bool_register_types();
ASSERT_ARE_EQUAL(int, 0, result);
REGISTER_GBALLOC_HL_GLOBAL_MOCK_HOOK();
2019-03-19 01:10:24 +03:00
}
TEST_SUITE_CLEANUP(suite_cleanup)
{
umock_c_deinit();
real_gballoc_hl_deinit();
2019-03-19 01:10:24 +03:00
}
TEST_FUNCTION_INITIALIZE(method_init)
{
umock_c_reset_all_calls();
}
TEST_FUNCTION_CLEANUP(method_cleanup)
{
}
/* lock_free_set_create */
/* Tests_SRS_LOCK_FREE_SET_01_001: [ lock_free_set_create shall create a lock free set. ] */
/* Tests_SRS_LOCK_FREE_SET_01_002: [ On success, lock_free_set_create shall return a non-NULL handle to the newly created set. ]*/
TEST_FUNCTION(lock_free_set_create_succeeds)
{
// arrange
LOCK_FREE_SET_HANDLE set;
STRICT_EXPECTED_CALL(malloc(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
// act
set = lock_free_set_create();
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_IS_NOT_NULL(set);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_003: [ If lock_free_set_create fails allocating memory for the set then it shall fail and return NULL. ] */
TEST_FUNCTION(when_allocating_memory_for_the_set_fails_lock_free_set_create_fails)
{
// arrange
LOCK_FREE_SET_HANDLE set;
STRICT_EXPECTED_CALL(malloc(IGNORED_ARG))
2019-03-19 01:10:24 +03:00
.SetReturn(NULL);
// act
set = lock_free_set_create();
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_IS_NULL(set);
}
/* Tests_SRS_LOCK_FREE_SET_01_001: [ lock_free_set_create shall create a lock free set. ] */
/* Tests_SRS_LOCK_FREE_SET_01_002: [ On success, lock_free_set_create shall return a non-NULL handle to the newly created set. ]*/
TEST_FUNCTION(two_lock_free_sets_can_be_created)
{
// arrange
LOCK_FREE_SET_HANDLE set1;
LOCK_FREE_SET_HANDLE set2;
STRICT_EXPECTED_CALL(malloc(IGNORED_ARG));
STRICT_EXPECTED_CALL(malloc(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
set1 = lock_free_set_create();
// act
set2 = lock_free_set_create();
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_IS_NOT_NULL(set1);
ASSERT_IS_NOT_NULL(set2);
ASSERT_ARE_NOT_EQUAL(void_ptr, set1, set2);
// cleanup
lock_free_set_destroy(set1, NULL, NULL);
lock_free_set_destroy(set2, NULL, NULL);
}
/* lock_free_set_destroy */
/* Tests_SRS_LOCK_FREE_SET_01_004: [ lock_free_set_destroy shall free all resources associated with the lock free set. ]*/
TEST_FUNCTION(lock_free_set_destroy_frees_the_memory_for_the_set)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(free(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
// act
lock_free_set_destroy(set, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_005: [ If lock_free_set is NULL, lock_free_set_destroy shall do nothing. ]*/
TEST_FUNCTION(lock_free_set_destroy_with_NULL_handle_frees_nothing)
{
// arrange
// act
lock_free_set_destroy(NULL, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_006: [ node_cleanup_callback and context shall be allowed to be NULL. ]*/
TEST_FUNCTION(lock_free_set_destroy_with_NULL_node_cleanup_callback_still_frees_set_memory)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(free(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
// act
lock_free_set_destroy(set, NULL, NULL);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_007: [ If node_cleanup_callback is non-NULL, node_cleanup_callback shall be called for each item that exists in the set at the time lock_free_set_destroy is called. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_008: [ When node_cleanup_callback is called the set item pointer and context shall be passed as arguments. ]*/
TEST_FUNCTION(lock_free_set_destroy_with_1_item_calls_the_node_free_callback)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[1];
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(test_node_cleanup((void*)0x4242, (struct LOCK_FREE_SET_ITEM_TAG*)&items[0]));
STRICT_EXPECTED_CALL(free(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
// act
lock_free_set_destroy(set, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_007: [ If node_cleanup_callback is non-NULL, node_cleanup_callback shall be called for each item that exists in the set at the time lock_free_set_destroy is called. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_008: [ When node_cleanup_callback is called the set item pointer and context shall be passed as arguments. ]*/
TEST_FUNCTION(lock_free_set_destroy_with_2_item_calls_the_node_free_callback)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(test_node_cleanup((void*)0x4242, (struct LOCK_FREE_SET_ITEM_TAG*)&items[1]));
STRICT_EXPECTED_CALL(test_node_cleanup((void*)0x4242, (struct LOCK_FREE_SET_ITEM_TAG*)&items[0]));
STRICT_EXPECTED_CALL(free(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
// act
lock_free_set_destroy(set, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_007: [ If node_cleanup_callback is non-NULL, node_cleanup_callback shall be called for each item that exists in the set at the time lock_free_set_destroy is called. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_006: [ node_cleanup_callback and context shall be allowed to be NULL. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_008: [ When node_cleanup_callback is called the set item pointer and context shall be passed as arguments. ]*/
TEST_FUNCTION(lock_free_set_destroy_with_1_item_calls_the_node_free_callback_NULL_context)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[1];
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(test_node_cleanup(NULL, (struct LOCK_FREE_SET_ITEM_TAG*)&items[0]));
STRICT_EXPECTED_CALL(free(IGNORED_ARG));
2019-03-19 01:10:24 +03:00
// act
lock_free_set_destroy(set, (NODE_CLEANUP_FUNC)test_node_cleanup, NULL);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* lock_free_set_insert */
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_inserts_the_item)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM item1;
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &item1);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_011: [ If lock_free_set or item is NULL, lock_free_set_insert shall fail and return a non-zero value. ]*/
TEST_FUNCTION(lock_free_set_insert_with_NULL_set_fails)
{
// arrange
int result;
LOCK_FREE_SET_ITEM item1;
// act
result = lock_free_set_insert(NULL, &item1);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_NOT_EQUAL(int, 0, result);
}
/* Tests_SRS_LOCK_FREE_SET_01_011: [ If lock_free_set or item is NULL, lock_free_set_insert shall fail and return a non-zero value. ]*/
TEST_FUNCTION(lock_free_set_insert_with_NULL_item_fails)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, NULL);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_NOT_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_2nd_item_left)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
int result;
(void)lock_free_set_insert(set, &items[1]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_2nd_item_right)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
int result;
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_3rd_item_left_left)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result;
(void)lock_free_set_insert(set, &items[2]);
(void)lock_free_set_insert(set, &items[1]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_3rd_item_right_right)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result;
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[2]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_3rd_item_left_right)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result;
(void)lock_free_set_insert(set, &items[2]);
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_3rd_item_right_left)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result;
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[2]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_3_items_filling_both_left_and_right_left_first)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result;
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[2]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_3_items_filling_both_left_and_right_right_first)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result;
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[2]);
umock_c_reset_all_calls();
// act
result = lock_free_set_insert(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
ASSERT_ARE_EQUAL(int, 0, result);
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_256_items_right_only)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[256];
int result;
size_t i;
umock_c_reset_all_calls();
for (i = 0; i < 256; i++)
{
// act
result = lock_free_set_insert(set, &items[i]);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
}
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_009: [ lock_free_set_insert shall insert the item item in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_010: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_insert_256_items_left_only)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[256];
int result;
size_t i;
umock_c_reset_all_calls();
for (i = 0; i < 256; i++)
{
// act
result = lock_free_set_insert(set, &items[255 - i]);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
}
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* lock_free_set_remove */
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_the_head)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[1];
int result;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
// act
result = lock_free_set_remove(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_017: [ If lock_free_set or item is NULL, lock_free_set_remove shall fail and return a non-zero value. ]*/
TEST_FUNCTION(lock_free_set_remove_with_NULL_item_fails)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_remove(set, NULL);
// assert
ASSERT_ARE_NOT_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_017: [ If lock_free_set or item is NULL, lock_free_set_remove shall fail and return a non-zero value. ]*/
TEST_FUNCTION(lock_free_set_remove_with_NULL_set_fails)
{
// arrange
LOCK_FREE_SET_ITEM items[1];
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_remove(NULL, &items[0]);
// assert
ASSERT_ARE_NOT_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_the_first_item)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
int result;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
// act
result = lock_free_set_remove(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_the_second_item)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
int result;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
// act
result = lock_free_set_remove(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_both_items_insert_0_1_remove_0_1)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
int result1;
int result2;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
// act
result1 = lock_free_set_remove(set, &items[0]);
result2 = lock_free_set_remove(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_both_items_insert_1_0_remove_1_0)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[2];
int result1;
int result2;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
// act
result1 = lock_free_set_remove(set, &items[1]);
result2 = lock_free_set_remove(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_all_3_items_insert_0_1_2_remove_0_1_2)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result1;
int result2;
int result3;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[2]);
// act
result1 = lock_free_set_remove(set, &items[0]);
result2 = lock_free_set_remove(set, &items[1]);
result3 = lock_free_set_remove(set, &items[2]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(int, 0, result3);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_all_3_items_insert_0_1_2_remove_0_2_1)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result1;
int result2;
int result3;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[2]);
// act
result1 = lock_free_set_remove(set, &items[0]);
result2 = lock_free_set_remove(set, &items[2]);
result3 = lock_free_set_remove(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(int, 0, result3);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_all_3_items_insert_0_1_2_remove_1_0_2)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result1;
int result2;
int result3;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[2]);
// act
result1 = lock_free_set_remove(set, &items[1]);
result2 = lock_free_set_remove(set, &items[0]);
result3 = lock_free_set_remove(set, &items[2]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(int, 0, result3);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_all_3_items_insert_0_1_2_remove_2_0_1)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result1;
int result2;
int result3;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[2]);
// act
result1 = lock_free_set_remove(set, &items[2]);
result2 = lock_free_set_remove(set, &items[0]);
result3 = lock_free_set_remove(set, &items[1]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(int, 0, result3);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_015: [ lock_free_set_remove shall remove the item item from the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_016: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_removes_all_3_items_insert_0_1_2_remove_2_1_0)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
LOCK_FREE_SET_ITEM items[3];
int result1;
int result2;
int result3;
umock_c_reset_all_calls();
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
(void)lock_free_set_insert(set, &items[2]);
// act
result1 = lock_free_set_remove(set, &items[2]);
result2 = lock_free_set_remove(set, &items[1]);
result3 = lock_free_set_remove(set, &items[0]);
// assert
ASSERT_ARE_EQUAL(int, 0, result1);
ASSERT_ARE_EQUAL(int, 0, result2);
ASSERT_ARE_EQUAL(int, 0, result3);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* lock_free_set_purge_not_thread_safe */
/* Tests_SRS_LOCK_FREE_SET_01_019: [ lock_free_set_purge_not_thread_safe shall remove all items in the set. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_020: [ On success it shall return 0. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_an_empty_set_succeeds)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_purge_not_thread_safe(set, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_021: [ If lock_free_set is NULL, lock_free_set_purge_not_thread_safe shall fail and return a non-zero value. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_NULL_set_fails)
{
// arrange
int result;
// act
result = lock_free_set_purge_not_thread_safe(NULL, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_NOT_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
}
/* Tests_SRS_LOCK_FREE_SET_01_022: [ node_cleanup_callback and context shall be allowed to be NULL. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_NULL_callback_and_empty_set_succeeds)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_purge_not_thread_safe(set, NULL, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_022: [ node_cleanup_callback and context shall be allowed to be NULL. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_NULL_context_and_empty_set_succeeds)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
umock_c_reset_all_calls();
// act
result = lock_free_set_purge_not_thread_safe(set, (NODE_CLEANUP_FUNC)test_node_cleanup, NULL);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_023: [ If node_cleanup_callback is non-NULL, node_cleanup_callback shall be called for each item that exists in the set at the time lock_free_set_purge_not_thread_safe is called. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_one_item_calls_the_callback)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
LOCK_FREE_SET_ITEM items[1];
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(test_node_cleanup((void*)0x4242, (struct LOCK_FREE_SET_ITEM_TAG*)&items[0]));
// act
result = lock_free_set_purge_not_thread_safe(set, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_023: [ If node_cleanup_callback is non-NULL, node_cleanup_callback shall be called for each item that exists in the set at the time lock_free_set_purge_not_thread_safe is called. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_2_items_calls_the_callback)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
LOCK_FREE_SET_ITEM items[2];
(void)lock_free_set_insert(set, &items[0]);
(void)lock_free_set_insert(set, &items[1]);
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(test_node_cleanup((void*)0x4242, (struct LOCK_FREE_SET_ITEM_TAG*)&items[1]));
STRICT_EXPECTED_CALL(test_node_cleanup((void*)0x4242, (struct LOCK_FREE_SET_ITEM_TAG*)&items[0]));
// act
result = lock_free_set_purge_not_thread_safe(set, (NODE_CLEANUP_FUNC)test_node_cleanup, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_022: [ node_cleanup_callback and context shall be allowed to be NULL. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_023: [ If node_cleanup_callback is non-NULL, node_cleanup_callback shall be called for each item that exists in the set at the time lock_free_set_purge_not_thread_safe is called. ]*/
/* Tests_SRS_LOCK_FREE_SET_01_024: [ When node_cleanup_callback is called the set item pointer and context shall be passed as arguments. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_1_item_and_NULL_context_calls_the_callback)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
LOCK_FREE_SET_ITEM items[2];
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
STRICT_EXPECTED_CALL(test_node_cleanup(NULL, (struct LOCK_FREE_SET_ITEM_TAG*)&items[0]));
// act
result = lock_free_set_purge_not_thread_safe(set, (NODE_CLEANUP_FUNC)test_node_cleanup, NULL);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
/* Tests_SRS_LOCK_FREE_SET_01_022: [ node_cleanup_callback and context shall be allowed to be NULL. ]*/
TEST_FUNCTION(lock_free_set_purge_not_thread_safe_with_1_item_and_NULL_callback_succeeds_but_does_not_call_callback)
{
// arrange
LOCK_FREE_SET_HANDLE set = lock_free_set_create();
int result;
LOCK_FREE_SET_ITEM items[2];
(void)lock_free_set_insert(set, &items[0]);
umock_c_reset_all_calls();
// act
result = lock_free_set_purge_not_thread_safe(set, NULL, (void*)0x4242);
// assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
// cleanup
lock_free_set_destroy(set, NULL, NULL);
}
END_TEST_SUITE(TEST_SUITE_NAME_FROM_CMAKE)