summaryrefslogtreecommitdiffstats
path: root/util/test/util/test_thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/test/util/test_thread.c')
-rw-r--r--util/test/util/test_thread.c196
1 files changed, 37 insertions, 159 deletions
diff --git a/util/test/util/test_thread.c b/util/test/util/test_thread.c
index cbdead5..4bddd7f 100644
--- a/util/test/util/test_thread.c
+++ b/util/test/util/test_thread.c
@@ -24,13 +24,14 @@
#include <axutil_allocator.h>
#include <axutil_utils.h>
#include "test_thread.h"
-#include <unistd.h>
+#include <cut_defs.h>
const axutil_env_t *env = NULL;
static axutil_thread_mutex_t *thread_lock = NULL;
static axutil_thread_once_t *control = NULL;
static int x = 0;
static int value = 0;
+static int param_data;
static axutil_thread_t *t1 = NULL;
static axutil_thread_t *t2 = NULL;
@@ -55,19 +56,14 @@ test_thread_init(
allocator = env->allocator;
control = axutil_thread_once_init(allocator);
-
- if (control)
- printf("success - test_thread_init - axutil_thread_once_init \n");
- else
- printf("failure - test_thread_init - axutil_thread_once_init \n");
+ CUT_ASSERT_PTR_NOT_EQUAL(control, NULL, 0);
thread_lock =
axutil_thread_mutex_create(allocator, AXIS2_THREAD_MUTEX_DEFAULT);
+ CUT_ASSERT_PTR_NOT_EQUAL(thread_lock, NULL, 0);
+ /* To avoid warning of not using cut_str_equal */
+ CUT_ASSERT_STR_EQUAL("", "", 0);
- if (thread_lock)
- printf("success - test_thread_init - axutil_thread_mutex_create \n");
- else
- printf("failure - test_thread_init - axutil_thread_mutex_create \n");
}
void *AXIS2_CALL
@@ -75,12 +71,12 @@ test_function(
axutil_thread_t * td,
void *param)
{
- int i;
- i = *((int *) param);
+ int i = *((int *) param);
printf("thread data = %d \n", i);
-
+ param_data = i;
+
axutil_thread_once(control, init_func);
-
+ CUT_ASSERT(value==1);
axutil_thread_mutex_lock(thread_lock);
printf("x = %d \n", ++x);
axutil_thread_mutex_unlock(thread_lock);
@@ -102,38 +98,26 @@ test_axutil_thread_create(
allocator = env->allocator;
i = AXIS2_MALLOC(allocator, sizeof(int));
*i = 5;
+ param_data = -1;
t1 = axutil_thread_create(allocator, NULL, test_function, (void *) i);
+ CUT_ASSERT_PTR_NOT_EQUAL(t1, NULL, 0);
+ AXIS2_SLEEP(1);
+ CUT_ASSERT_INT_EQUAL(param_data, *i, 0);
- if (t1)
- printf("success - test_axutil_thread_create - axutil_thread_create \n");
- else
- printf("failure - test_axutil_thread_create - axutil_thread_create \n");
j = AXIS2_MALLOC(allocator, sizeof(int));
*j = 25;
-
+ param_data = -1;
t2 = axutil_thread_create(allocator, NULL, test_function, (void *) j);
-
- if (t2)
- printf("success - test_axutil_thread_create - axutil_thread_create \n");
- else
- printf("failure - test_axutil_thread_create - axutil_thread_create \n");
+ CUT_ASSERT_PTR_NOT_EQUAL(t2, NULL, 0);
+ AXIS2_SLEEP(1);
+ CUT_ASSERT_INT_EQUAL(param_data, *j, 0);
rv = axutil_thread_join(t1);
-
- if (AXIS2_SUCCESS == rv)
- printf("success - test_axutil_thread_create - axutil_thread_join \n");
- else
- printf
- ("failure - test_thread_init - test_axutil_thread_create - axutil_thread_join \n");
+ CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 0);
rv = axutil_thread_join(t2);
-
- if (AXIS2_SUCCESS == rv)
- printf("success - test_axutil_thread_create - axutil_thread_join \n");
- else
- printf
- ("failure - test_thread_init - test_axutil_thread_create - axutil_thread_join \n");
+ CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 0);
}
@@ -143,8 +127,6 @@ test_function2(
void *param)
{
printf("thread \n");
- /*axutil_thread_exit(td, env->allocator); */
-
return (void *) 1;
}
@@ -158,49 +140,18 @@ test_axutil_thread_detach(
allocator = env->allocator;
attr = axutil_threadattr_create(allocator);
- if (!attr)
- {
- printf("failure - test_axutil_thread_detach\n");
- return;
- }
+ CUT_ASSERT_PTR_NOT_EQUAL(attr, NULL, 1);
rv = axutil_threadattr_detach_set(attr, 1);
-
- if (AXIS2_SUCCESS != rv)
- {
- printf("failure - test_axutil_thread_detach\n");
- return;
- }
+ CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 1);
t3 = axutil_thread_create(allocator, attr, test_function2, NULL);
-
- if (!t3)
- {
- printf("failure - test_axutil_thread_detach\n");
- return;
- }
+ CUT_ASSERT_PTR_NOT_EQUAL(t3, NULL, 1);
/*
* thread is already detached - should return AXIS2_FAILURE
*/
rv = axutil_thread_detach(t3);
-
- if (AXIS2_FAILURE != rv)
- {
- printf("failure - test_axutil_thread_detach\n");
- return;
- }
-
- /*
- * thread is already detached - should return AXIS2_FAILURE
- * cannot join detached threads
- */
- /*rv = axutil_thread_join(t3); */
- if (AXIS2_FAILURE != rv)
- {
- printf("failure - test_axutil_thread_detach\n");
- return;
- }
- printf("success - test_axutil_thread_detach\n");
-}
+ CUT_ASSERT_INT_EQUAL(rv, AXIS2_FAILURE, 1);
+ }
void
test_axutil_thread_detach2(
@@ -212,63 +163,29 @@ test_axutil_thread_detach2(
allocator = env->allocator;
attr = axutil_threadattr_create(allocator);
- if (!attr)
- {
- printf("failure - test_axutil_thread_detach2\n");
- return;
- }
+ CUT_ASSERT_PTR_NOT_EQUAL(attr, NULL, 1);
t4 = axutil_thread_create(allocator, attr, test_function2, NULL);
-
- if (!t4)
- {
- printf("failure - test_axutil_thread_detach2\n");
- return;
- }
-
+ CUT_ASSERT_PTR_NOT_EQUAL(t4, NULL, 1);
/*
* thread is not detached yet - should return AXIS2_SUCCESS
*/
rv = axutil_thread_detach(t4);
-
- if (AXIS2_SUCCESS != rv)
- {
- printf("failure - test_axutil_thread_detach\n");
- return;
- }
-
- /*
- * thread is already detached - should return AXIS2_FAILURE
- * cannot join detached threads
- */
- /*rv = axutil_thread_join(t4); */
- if (AXIS2_FAILURE != rv)
- {
- printf("failure - test_axutil_thread_detach2\n");
- return;
- }
- printf("success - test_axutil_thread_detach2\n");
+ CUT_ASSERT_INT_EQUAL(rv, AXIS2_SUCCESS, 1);
}
void
check_locks(
)
{
- if (2 == x)
- printf("success - check_locks \n");
- else
- printf("failure - check_locks \n");
-
+ CUT_ASSERT_INT_EQUAL(x, 2, 0);
}
void
check_thread_once(
)
{
- if (1 == value)
- printf("success - check_thread_once \n");
- else
- printf("failure - check_thread_once \n");
+ CUT_ASSERT_INT_EQUAL(value, 1, 0);
}
void
@@ -291,55 +208,16 @@ run_test_thread(
axutil_thread_mutex_destroy(thread_lock);
}
-const axutil_env_t *
-create_env_with_error_log(
- )
-{
- axutil_error_t *error = NULL;
- axutil_log_t *log22 = NULL;
- const axutil_env_t *env = NULL;
- axutil_allocator_t *allocator = axutil_allocator_init(NULL);
- if (!allocator)
- {
- printf("allocator is NULL\n");
- return NULL;
- }
- error = axutil_error_create(allocator);
- if (!error)
- {
- printf("cannot create error\n");
- return NULL;
- }
-
- log22 = axutil_log_create(allocator, NULL, "test123.log");
- if (!log22)
- {
- printf("cannot create log\n");
- return NULL;
- }
- /*
- * allow all types of logs
- */
- log22->level = AXIS2_LOG_LEVEL_DEBUG;
- /* log22->enabled = 0; */
- env = axutil_env_create_with_error_log(allocator, error, log22);
- if (!env)
- {
- printf("cannot create env with error and log\n");
- return NULL;
- }
- return env;
-}
-
int
main(
void)
{
- env = create_env_with_error_log();
-
- if (!env)
- return -1;
- run_test_thread(env);
-
+ axutil_env_t *env = cut_setup_env("util thread");
+ CUT_ASSERT(env != NULL);
+ if (env) {
+ run_test_thread(env);
+ axutil_env_free(env);
+ }
+ CUT_RETURN_ON_FAILURE(-1);
return 0;
}