TestDataMutex.cpp (1121B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 #include "mozilla/DataMutex.h" 9 #include "nsTArray.h" 10 11 using mozilla::DataMutex; 12 13 struct A { 14 void Set(int a) { mValue = a; } 15 int mValue; 16 }; 17 18 TEST(DataMutex, Basic) 19 { 20 { 21 DataMutex<uint32_t> i(1, "1"); 22 i.Mutex().AssertNotCurrentThreadOwns(); 23 { 24 auto x = i.Lock(); 25 i.Mutex().AssertCurrentThreadOwns(); 26 *x = 4; 27 ASSERT_EQ(*x, 4u); 28 } 29 i.Mutex().AssertNotCurrentThreadOwns(); 30 } 31 { 32 DataMutex<A> a({4}, "StructA"); 33 auto x = a.Lock(); 34 ASSERT_EQ(x->mValue, 4); 35 x->Set(8); 36 ASSERT_EQ(x->mValue, 8); 37 } 38 { 39 DataMutex<nsTArray<uint32_t>> _a("array"); 40 auto a = _a.Lock(); 41 auto& x = a.ref(); 42 ASSERT_EQ(x.Length(), 0u); 43 x.AppendElement(1u); 44 ASSERT_EQ(x.Length(), 1u); 45 ASSERT_EQ(x[0], 1u); 46 } 47 }