summaryrefslogtreecommitdiffstats
path: root/base/tools_sanity_unittest.cc
blob: 91f56b4ba6212bf4af282c15451c5013211443ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/message_loop.h"
#include "base/thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// We use caps here just to ensure that the method name doesn't interfere with
// the wildcarded suppressions.
class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
 public:
  explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
  ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
  void ThreadMain() {
    *value_ = true;
  }
 private:
  bool* value_;
};

}

// A memory leak detector should report an error in this test.
TEST(ToolsSanityTest, MemoryLeak) {
  int *leak = new int[256];  // Leak some memory intentionally.
  leak[4] = 1;  // Make sure the allocated memory is used.
}

// A data race detector should report an error in this test.
TEST(ToolsSanityTest, DataRace) {
  bool shared = false;
  PlatformThreadHandle a;
  PlatformThreadHandle b;
  PlatformThread::Delegate *thread1 =
      new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared);
  PlatformThread::Delegate *thread2 =
      new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared);

  PlatformThread::Create(0, thread1, &a);
  PlatformThread::Create(0, thread2, &b);
  PlatformThread::Join(a);
  PlatformThread::Join(b);
  EXPECT_TRUE(shared);
  delete thread1;
  delete thread2;
}