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