From 577ae1debc724cb0144d903a8a1bfa4daf467475 Mon Sep 17 00:00:00 2001 From: Cedric Bail Date: Wed, 16 Oct 2013 16:47:21 +0900 Subject: [PATCH] eina: let's add some straight forward test. --- src/Makefile_Eina.am | 3 +- src/tests/eina/eina_suite.c | 1 + src/tests/eina/eina_suite.h | 1 + src/tests/eina/eina_test_lock.c | 172 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 src/tests/eina/eina_test_lock.c diff --git a/src/Makefile_Eina.am b/src/Makefile_Eina.am index 09264ce..5ad1c20 100644 --- a/src/Makefile_Eina.am +++ b/src/Makefile_Eina.am @@ -268,7 +268,8 @@ tests/eina/eina_test_simple_xml_parser.c \ tests/eina/eina_test_value.c \ tests/eina/eina_test_cow.c \ tests/eina/eina_test_barrier.c \ -tests/eina/eina_test_tmpstr.c +tests/eina/eina_test_tmpstr.c \ +tests/eina/eina_test_lock.c # tests/eina/eina_test_model.c tests_eina_eina_suite_CPPFLAGS = -I$(top_builddir)/src/lib/efl \ diff --git a/src/tests/eina/eina_suite.c b/src/tests/eina/eina_suite.c index 1577b3a..1a27503 100644 --- a/src/tests/eina/eina_suite.c +++ b/src/tests/eina/eina_suite.c @@ -73,6 +73,7 @@ static const Eina_Test_Case etc[] = { // { "Model", eina_test_model }, { "Barrier", eina_test_barrier }, { "Tmp String", eina_test_tmpstr }, + { "Locking", eina_test_locking }, { NULL, NULL } }; diff --git a/src/tests/eina/eina_suite.h b/src/tests/eina/eina_suite.h index 85a32cf..e786309 100644 --- a/src/tests/eina/eina_suite.h +++ b/src/tests/eina/eina_suite.h @@ -60,5 +60,6 @@ void eina_test_model(TCase *tc); void eina_test_cow(TCase *tc); void eina_test_barrier(TCase *tc); void eina_test_tmpstr(TCase *tc); +void eina_test_locking(TCase *tc); #endif /* EINA_SUITE_H_ */ diff --git a/src/tests/eina/eina_test_lock.c b/src/tests/eina/eina_test_lock.c new file mode 100644 index 0000000..a813352 --- /dev/null +++ b/src/tests/eina/eina_test_lock.c @@ -0,0 +1,172 @@ +/* EINA - EFL data type library + * Copyright (C) 2013 Cedric Bail + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; + * if not, see . + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "eina_suite.h" +#include "Eina.h" + +static Eina_Spinlock spin; +static Eina_Thread thread; +static unsigned int counter; + +static void * +_eina_test_lock_thread(void *data, Eina_Thread t) +{ + unsigned int i; + + fail_if(!eina_thread_equal(t, thread)); + fail_if(strcmp("test", data)); + + for (i = 0; i < 10000; i++) + { + fail_if(eina_spinlock_take(&spin) != EINA_LOCK_SUCCEED); + counter++; + fail_if(eina_spinlock_release(&spin) != EINA_LOCK_SUCCEED); + } + + return data; +} + +START_TEST(eina_test_spinlock) +{ + unsigned int i; + + fail_if(!eina_init()); + + counter = 0; + fail_if(!eina_spinlock_new(&spin)); + + fail_if(!eina_thread_create(&thread, EINA_THREAD_NORMAL, 0, _eina_test_lock_thread, "test")); + + for (i = 0; i < 10000; i++) + { + fail_if(eina_spinlock_take(&spin) != EINA_LOCK_SUCCEED); + counter++; + fail_if(eina_spinlock_release(&spin) != EINA_LOCK_SUCCEED); + } + + fail_if(strcmp("test", eina_thread_join(thread))); + + fail_if(counter != 20000); + + eina_spinlock_free(&spin); + + eina_shutdown(); +} +END_TEST + +static Eina_TLS key; + +static void * +_eina_test_tls_thread(void *data EINA_UNUSED, Eina_Thread t EINA_UNUSED) +{ + unsigned int mystack = 21; + + fail_if(!eina_tls_set(key, &mystack)); + fail_if(eina_tls_get(key) != &mystack); + + return NULL; +} + +START_TEST(eina_test_tls) +{ + unsigned int ft = 42; + + fail_if(!eina_init()); + + fail_if(!eina_tls_new(&key)); + + fail_if(!eina_tls_set(key, &ft)); + + fail_if(!eina_thread_create(&thread, EINA_THREAD_NORMAL, 0, _eina_test_tls_thread, NULL)); + + eina_thread_join(thread); + + fail_if(eina_tls_get(key) != &ft); + + eina_tls_free(key); + + eina_shutdown(); +} +END_TEST + +static Eina_Lock mtcond; +static Eina_Condition cond; +static Eina_RWLock mutex; + +static void * +_eina_test_rwlock_thread(void *data EINA_UNUSED, Eina_Thread t EINA_UNUSED) +{ + fail_if(!eina_condition_broadcast(&cond)); + + fail_if(eina_rwlock_take_write(&mutex) != EINA_LOCK_SUCCEED); + counter = 7200; + fail_if(eina_rwlock_release(&mutex) != EINA_LOCK_SUCCEED); + + fail_if(!eina_condition_broadcast(&cond)); + + return NULL; +} + +START_TEST(eina_test_rwlock) +{ + fail_if(!eina_init()); + + fail_if(!eina_rwlock_new(&mutex)); + fail_if(!eina_lock_new(&mtcond)); + fail_if(!eina_condition_new(&cond, &mtcond)); + + counter = 42; + + fail_if(eina_rwlock_take_read(&mutex) != EINA_LOCK_SUCCEED); + fail_if(eina_lock_take(&mtcond) != EINA_LOCK_SUCCEED); + + fail_if(!eina_thread_create(&thread, EINA_THREAD_NORMAL, 0, _eina_test_rwlock_thread, NULL)); + + fail_if(!eina_condition_wait(&cond)); + fail_if(!eina_lock_release(&mtcond)); + + fail_if(counter != 42); + fail_if(eina_rwlock_release(&mutex) != EINA_LOCK_SUCCEED); + + fail_if(eina_lock_take(&mtcond) != EINA_LOCK_SUCCEED); + fail_if(!eina_condition_wait(&cond)); + fail_if(eina_lock_release(&mtcond) != EINA_LOCK_SUCCEED); + + fail_if(eina_rwlock_take_read(&mutex) != EINA_LOCK_SUCCEED); + fail_if(counter != 7200); + fail_if(eina_rwlock_release(&mutex) != EINA_LOCK_SUCCEED); + + eina_condition_free(&cond); + eina_lock_free(&mtcond); + eina_rwlock_free(&mutex); + + eina_shutdown(); +} +END_TEST + +void +eina_test_locking(TCase *tc) +{ + tcase_add_test(tc, eina_test_spinlock); + tcase_add_test(tc, eina_test_tls); + tcase_add_test(tc, eina_test_rwlock); +} -- 2.7.4