michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "storage_test_harness.h" michael@0: michael@0: #include "SQLiteMutex.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::storage; michael@0: michael@0: /** michael@0: * This file test our sqlite3_mutex wrapper in SQLiteMutex.h. michael@0: */ michael@0: michael@0: void michael@0: test_AutoLock() michael@0: { michael@0: int lockTypes[] = { michael@0: SQLITE_MUTEX_FAST, michael@0: SQLITE_MUTEX_RECURSIVE, michael@0: }; michael@0: for (size_t i = 0; i < ArrayLength(lockTypes); i++) { michael@0: // Get our test mutex (we have to allocate a SQLite mutex of the right type michael@0: // too!). michael@0: SQLiteMutex mutex("TestMutex"); michael@0: sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]); michael@0: do_check_true(inner); michael@0: mutex.initWithMutex(inner); michael@0: michael@0: // And test that our automatic locking wrapper works as expected. michael@0: mutex.assertNotCurrentThreadOwns(); michael@0: { michael@0: SQLiteMutexAutoLock lockedScope(mutex); michael@0: mutex.assertCurrentThreadOwns(); michael@0: } michael@0: mutex.assertNotCurrentThreadOwns(); michael@0: michael@0: // Free the wrapped mutex - we don't need it anymore. michael@0: sqlite3_mutex_free(inner); michael@0: } michael@0: } michael@0: michael@0: void michael@0: test_AutoUnlock() michael@0: { michael@0: int lockTypes[] = { michael@0: SQLITE_MUTEX_FAST, michael@0: SQLITE_MUTEX_RECURSIVE, michael@0: }; michael@0: for (size_t i = 0; i < ArrayLength(lockTypes); i++) { michael@0: // Get our test mutex (we have to allocate a SQLite mutex of the right type michael@0: // too!). michael@0: SQLiteMutex mutex("TestMutex"); michael@0: sqlite3_mutex *inner = sqlite3_mutex_alloc(lockTypes[i]); michael@0: do_check_true(inner); michael@0: mutex.initWithMutex(inner); michael@0: michael@0: // And test that our automatic unlocking wrapper works as expected. michael@0: { michael@0: SQLiteMutexAutoLock lockedScope(mutex); michael@0: michael@0: { michael@0: SQLiteMutexAutoUnlock unlockedScope(mutex); michael@0: mutex.assertNotCurrentThreadOwns(); michael@0: } michael@0: mutex.assertCurrentThreadOwns(); michael@0: } michael@0: michael@0: // Free the wrapped mutex - we don't need it anymore. michael@0: sqlite3_mutex_free(inner); michael@0: } michael@0: } michael@0: michael@0: void (*gTests[])(void) = { michael@0: test_AutoLock, michael@0: test_AutoUnlock, michael@0: }; michael@0: michael@0: const char *file = __FILE__; michael@0: #define TEST_NAME "SQLiteMutex" michael@0: #define TEST_FILE file michael@0: #include "storage_test_harness_tail.h"