tools_sanity_unittest.cc revision c7f5f8508d98d5952d42ed7648c2a8f30a4da156
1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/message_loop.h"
6#include "base/thread.h"
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace {
10
11// We use caps here just to ensure that the method name doesn't interfere with
12// the wildcarded suppressions.
13class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
14 public:
15  explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
16  ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
17  void ThreadMain() {
18    *value_ = true;
19  }
20 private:
21  bool* value_;
22};
23
24}
25
26// A memory leak detector should report an error in this test.
27TEST(ToolsSanityTest, MemoryLeak) {
28  int *leak = new int[256];  // Leak some memory intentionally.
29  leak[4] = 1;  // Make sure the allocated memory is used.
30}
31
32// A data race detector should report an error in this test.
33TEST(ToolsSanityTest, DataRace) {
34  bool shared = false;
35  PlatformThreadHandle a;
36  PlatformThreadHandle b;
37  PlatformThread::Delegate *thread1 =
38      new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared);
39  PlatformThread::Delegate *thread2 =
40      new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared);
41
42  PlatformThread::Create(0, thread1, &a);
43  PlatformThread::Create(0, thread2, &b);
44  PlatformThread::Join(a);
45  PlatformThread::Join(b);
46  EXPECT_TRUE(shared);
47  delete thread1;
48  delete thread2;
49}
50
51