diff options
author | Hidehiko Abe <hidehiko@google.com> | 2018-04-24 01:37:19 +0900 |
---|---|---|
committer | Hidehiko Abe <hidehiko@google.com> | 2018-04-24 03:09:01 +0900 |
commit | b268b43ac6fdbc4f3a2ed1429b99ace424906090 (patch) | |
tree | bd2d04362f66c36d4279f7a9735ba21ea3a2a021 /mojo/public/cpp/test_support/lib/test_utils.cc | |
parent | 4a54b98aa445f521c6945e4f4a1e0ea788fa7da8 (diff) | |
download | libchrome-b268b43ac6fdbc4f3a2ed1429b99ace424906090.tar.gz |
Migrate libmojo repository into libchrome, part 2.
This CL moves following files.
- .gitignore
- Android.bp is merged into libchrome's Android.bp.
- base/android/*
- build/* except build_config.h which is exactly same with libchrome's.
- ipc/*
- mojo/* except mojo/public/tools/bindings/generators/__init__.py
which is unused and not in chrome repository.
- soong/* into libchrome_tools/
- third_party/{catapult,jinja2,markupsafe,ply}/*
- ui/gfx/{geometry,range}/mojo/*
Then, update several paths/build rules to be adapted.
Bug: 73606903
Test: Built locally. Ran on DUT.
Change-Id: I2a532a42aa68dcb215dbd71d8673192311509726
Diffstat (limited to 'mojo/public/cpp/test_support/lib/test_utils.cc')
-rw-r--r-- | mojo/public/cpp/test_support/lib/test_utils.cc | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/mojo/public/cpp/test_support/lib/test_utils.cc b/mojo/public/cpp/test_support/lib/test_utils.cc new file mode 100644 index 0000000000..7fe6f02788 --- /dev/null +++ b/mojo/public/cpp/test_support/lib/test_utils.cc @@ -0,0 +1,100 @@ +// Copyright 2013 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 "mojo/public/cpp/test_support/test_utils.h" + +#include <stddef.h> +#include <stdint.h> + +#include "mojo/public/cpp/system/core.h" +#include "mojo/public/cpp/system/wait.h" +#include "mojo/public/cpp/test_support/test_support.h" + +namespace mojo { +namespace test { + +bool WriteTextMessage(const MessagePipeHandle& handle, + const std::string& text) { + MojoResult rv = WriteMessageRaw(handle, + text.data(), + static_cast<uint32_t>(text.size()), + nullptr, + 0, + MOJO_WRITE_MESSAGE_FLAG_NONE); + return rv == MOJO_RESULT_OK; +} + +bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) { + MojoResult rv; + bool did_wait = false; + + uint32_t num_bytes = 0, num_handles = 0; + for (;;) { + rv = ReadMessageRaw(handle, + nullptr, + &num_bytes, + nullptr, + &num_handles, + MOJO_READ_MESSAGE_FLAG_NONE); + if (rv == MOJO_RESULT_SHOULD_WAIT) { + if (did_wait) { + assert(false); // Looping endlessly!? + return false; + } + rv = Wait(handle, MOJO_HANDLE_SIGNAL_READABLE); + if (rv != MOJO_RESULT_OK) + return false; + did_wait = true; + } else { + assert(!num_handles); + break; + } + } + + text->resize(num_bytes); + rv = ReadMessageRaw(handle, + &text->at(0), + &num_bytes, + nullptr, + &num_handles, + MOJO_READ_MESSAGE_FLAG_NONE); + return rv == MOJO_RESULT_OK; +} + +bool DiscardMessage(const MessagePipeHandle& handle) { + MojoResult rv = ReadMessageRaw(handle, + nullptr, + nullptr, + nullptr, + nullptr, + MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); + return rv == MOJO_RESULT_OK; +} + +void IterateAndReportPerf(const char* test_name, + const char* sub_test_name, + PerfTestSingleIteration single_iteration, + void* closure) { + // TODO(vtl): These should be specifiable using command-line flags. + static const size_t kGranularity = 100; + static const MojoTimeTicks kPerftestTimeMicroseconds = 3 * 1000000; + + const MojoTimeTicks start_time = GetTimeTicksNow(); + MojoTimeTicks end_time; + size_t iterations = 0; + do { + for (size_t i = 0; i < kGranularity; i++) + (*single_iteration)(closure); + iterations += kGranularity; + + end_time = GetTimeTicksNow(); + } while (end_time - start_time < kPerftestTimeMicroseconds); + + MojoTestSupportLogPerfResult(test_name, sub_test_name, + 1000000.0 * iterations / (end_time - start_time), + "iterations/second"); +} + +} // namespace test +} // namespace mojo |