Revision: 23296
Author: [email protected]
Date: Fri Aug 22 08:52:40 2014 UTC
Log: Migrate test-semaphore to base-unittests.
TEST=base-unittests
BUG=v8:3489
LOG=n
[email protected]
Review URL: https://codereview.chromium.org/494343002
https://code.google.com/p/v8/source/detail?r=23296
Added:
/branches/bleeding_edge/test/base-unittests/platform/semaphore-unittest.cc
Deleted:
/branches/bleeding_edge/test/cctest/test-semaphore.cc
Modified:
/branches/bleeding_edge/test/base-unittests/base-unittests.gyp
/branches/bleeding_edge/test/cctest/cctest.gyp
=======================================
--- /dev/null
+++
/branches/bleeding_edge/test/base-unittests/platform/semaphore-unittest.cc
Fri Aug 22 08:52:40 2014 UTC
@@ -0,0 +1,145 @@
+// Copyright 2014 the V8 project 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 <cstring>
+
+#include "src/base/platform/platform.h"
+#include "src/base/platform/semaphore.h"
+#include "src/base/platform/time.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace v8 {
+namespace base {
+
+namespace {
+
+static const char kAlphabet[] = "XKOAD";
+static const size_t kAlphabetSize = sizeof(kAlphabet) - 1;
+static const size_t kBufferSize = 987; // GCD(buffer size, alphabet size)
= 1
+static const size_t kDataSize = kBufferSize * kAlphabetSize * 10;
+
+
+class ProducerThread V8_FINAL : public Thread {
+ public:
+ ProducerThread(char* buffer, Semaphore* free_space, Semaphore*
used_space)
+ : Thread(Options("ProducerThread")),
+ buffer_(buffer),
+ free_space_(free_space),
+ used_space_(used_space) {}
+ virtual ~ProducerThread() {}
+
+ virtual void Run() V8_OVERRIDE {
+ for (size_t n = 0; n < kDataSize; ++n) {
+ free_space_->Wait();
+ buffer_[n % kBufferSize] = kAlphabet[n % kAlphabetSize];
+ used_space_->Signal();
+ }
+ }
+
+ private:
+ char* buffer_;
+ Semaphore* const free_space_;
+ Semaphore* const used_space_;
+};
+
+
+class ConsumerThread V8_FINAL : public Thread {
+ public:
+ ConsumerThread(const char* buffer, Semaphore* free_space,
+ Semaphore* used_space)
+ : Thread(Options("ConsumerThread")),
+ buffer_(buffer),
+ free_space_(free_space),
+ used_space_(used_space) {}
+ virtual ~ConsumerThread() {}
+
+ virtual void Run() V8_OVERRIDE {
+ for (size_t n = 0; n < kDataSize; ++n) {
+ used_space_->Wait();
+ EXPECT_EQ(kAlphabet[n % kAlphabetSize], buffer_[n % kBufferSize]);
+ free_space_->Signal();
+ }
+ }
+
+ private:
+ const char* buffer_;
+ Semaphore* const free_space_;
+ Semaphore* const used_space_;
+};
+
+
+class WaitAndSignalThread V8_FINAL : public Thread {
+ public:
+ explicit WaitAndSignalThread(Semaphore* semaphore)
+ : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {}
+ virtual ~WaitAndSignalThread() {}
+
+ virtual void Run() V8_OVERRIDE {
+ for (int n = 0; n < 100; ++n) {
+ semaphore_->Wait();
+ ASSERT_FALSE(semaphore_->WaitFor(TimeDelta::FromMicroseconds(1)));
+ semaphore_->Signal();
+ }
+ }
+
+ private:
+ Semaphore* const semaphore_;
+};
+
+} // namespace
+
+
+TEST(Semaphore, ProducerConsumer) {
+ char buffer[kBufferSize];
+ std::memset(buffer, 0, sizeof(buffer));
+ Semaphore free_space(kBufferSize);
+ Semaphore used_space(0);
+ ProducerThread producer_thread(buffer, &free_space, &used_space);
+ ConsumerThread consumer_thread(buffer, &free_space, &used_space);
+ producer_thread.Start();
+ consumer_thread.Start();
+ producer_thread.Join();
+ consumer_thread.Join();
+}
+
+
+TEST(Semaphore, WaitAndSignal) {
+ Semaphore semaphore(0);
+ WaitAndSignalThread t1(&semaphore);
+ WaitAndSignalThread t2(&semaphore);
+
+ t1.Start();
+ t2.Start();
+
+ // Make something available.
+ semaphore.Signal();
+
+ t1.Join();
+ t2.Join();
+
+ semaphore.Wait();
+
+ EXPECT_FALSE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1)));
+}
+
+
+TEST(Semaphore, WaitFor) {
+ Semaphore semaphore(0);
+
+ // Semaphore not signalled - timeout.
+ ASSERT_FALSE(semaphore.WaitFor(TimeDelta::FromMicroseconds(0)));
+ ASSERT_FALSE(semaphore.WaitFor(TimeDelta::FromMicroseconds(100)));
+ ASSERT_FALSE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)));
+
+ // Semaphore signalled - no timeout.
+ semaphore.Signal();
+ ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(0)));
+ semaphore.Signal();
+ ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(100)));
+ semaphore.Signal();
+ ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)));
+}
+
+} // namespace base
+} // namespace v8
=======================================
--- /branches/bleeding_edge/test/cctest/test-semaphore.cc Tue Aug 12
13:33:35 2014 UTC
+++ /dev/null
@@ -1,156 +0,0 @@
-// Copyright 2013 the V8 project authors. All rights reserved.
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following
-// disclaimer in the documentation and/or other materials provided
-// with the distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived
-// from this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-#include <stdlib.h>
-
-#include "src/v8.h"
-
-#include "src/base/platform/platform.h"
-#include "test/cctest/cctest.h"
-
-
-using namespace ::v8::internal;
-
-
-class WaitAndSignalThread V8_FINAL : public v8::base::Thread {
- public:
- explicit WaitAndSignalThread(v8::base::Semaphore* semaphore)
- : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {}
- virtual ~WaitAndSignalThread() {}
-
- virtual void Run() V8_OVERRIDE {
- for (int n = 0; n < 1000; ++n) {
- semaphore_->Wait();
- bool result =
- semaphore_->WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
- DCHECK(!result);
- USE(result);
- semaphore_->Signal();
- }
- }
-
- private:
- v8::base::Semaphore* semaphore_;
-};
-
-
-TEST(WaitAndSignal) {
- v8::base::Semaphore semaphore(0);
- WaitAndSignalThread t1(&semaphore);
- WaitAndSignalThread t2(&semaphore);
-
- t1.Start();
- t2.Start();
-
- // Make something available.
- semaphore.Signal();
-
- t1.Join();
- t2.Join();
-
- semaphore.Wait();
-
- bool result =
semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
- DCHECK(!result);
- USE(result);
-}
-
-
-TEST(WaitFor) {
- bool ok;
- v8::base::Semaphore semaphore(0);
-
- // Semaphore not signalled - timeout.
- ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
- CHECK(!ok);
- ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
- CHECK(!ok);
- ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
- CHECK(!ok);
-
- // Semaphore signalled - no timeout.
- semaphore.Signal();
- ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
- CHECK(ok);
- semaphore.Signal();
- ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
- CHECK(ok);
- semaphore.Signal();
- ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
- CHECK(ok);
-}
-
-
-static const char alphabet[] = "XKOAD";
-static const int kAlphabetSize = sizeof(alphabet) - 1;
-static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) =
1
-static char buffer[kBufferSize];
-static const int kDataSize = kBufferSize * kAlphabetSize * 10;
-
-static v8::base::Semaphore free_space(kBufferSize);
-static v8::base::Semaphore used_space(0);
-
-
-class ProducerThread V8_FINAL : public v8::base::Thread {
- public:
- ProducerThread() : Thread(Options("ProducerThread")) {}
- virtual ~ProducerThread() {}
-
- virtual void Run() V8_OVERRIDE {
- for (int n = 0; n < kDataSize; ++n) {
- free_space.Wait();
- buffer[n % kBufferSize] = alphabet[n % kAlphabetSize];
- used_space.Signal();
- }
- }
-};
-
-
-class ConsumerThread V8_FINAL : public v8::base::Thread {
- public:
- ConsumerThread() : Thread(Options("ConsumerThread")) {}
- virtual ~ConsumerThread() {}
-
- virtual void Run() V8_OVERRIDE {
- for (int n = 0; n < kDataSize; ++n) {
- used_space.Wait();
- DCHECK_EQ(static_cast<int>(alphabet[n % kAlphabetSize]),
- static_cast<int>(buffer[n % kBufferSize]));
- free_space.Signal();
- }
- }
-};
-
-
-TEST(ProducerConsumer) {
- ProducerThread producer_thread;
- ConsumerThread consumer_thread;
- producer_thread.Start();
- consumer_thread.Start();
- producer_thread.Join();
- consumer_thread.Join();
-}
=======================================
--- /branches/bleeding_edge/test/base-unittests/base-unittests.gyp Thu Aug
14 09:07:58 2014 UTC
+++ /branches/bleeding_edge/test/base-unittests/base-unittests.gyp Fri Aug
22 08:52:40 2014 UTC
@@ -25,6 +25,7 @@
'platform/condition-variable-unittest.cc',
'platform/mutex-unittest.cc',
'platform/platform-unittest.cc',
+ 'platform/semaphore-unittest.cc',
'platform/time-unittest.cc',
'utils/random-number-generator-unittest.cc',
],
=======================================
--- /branches/bleeding_edge/test/cctest/cctest.gyp Wed Aug 20 13:05:03 2014
UTC
+++ /branches/bleeding_edge/test/cctest/cctest.gyp Fri Aug 22 08:52:40 2014
UTC
@@ -145,7 +145,6 @@
'test-regexp.cc',
'test-reloc-info.cc',
'test-representation.cc',
- 'test-semaphore.cc',
'test-serialize.cc',
'test-spaces.cc',
'test-strings.cc',
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.