diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-18 00:11:35 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-18 00:11:35 +0000 |
commit | 7b19923e2d7a09de1830756e4a64dd862d570fef (patch) | |
tree | b77a9c225daa6fc43660bf5b7f6623b97165bdbd /mojo | |
parent | 49e8757b120907d4df5e72ea0283d238eb62eb57 (diff) | |
download | chromium_src-7b19923e2d7a09de1830756e4a64dd862d570fef.zip chromium_src-7b19923e2d7a09de1830756e4a64dd862d570fef.tar.gz chromium_src-7b19923e2d7a09de1830756e4a64dd862d570fef.tar.bz2 |
Mojo: Actually test some APIs in C (not C++).
Also fix some problems in our C declarations/defines.
R=sky@chromium.org
Review URL: https://codereview.chromium.org/136793027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245668 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
-rw-r--r-- | mojo/mojo_public.gypi | 1 | ||||
-rw-r--r-- | mojo/public/system/async_waiter.h | 4 | ||||
-rw-r--r-- | mojo/public/system/core.h | 2 | ||||
-rw-r--r-- | mojo/public/system/macros.h | 2 | ||||
-rw-r--r-- | mojo/public/tests/system/core_unittest.cc | 9 | ||||
-rw-r--r-- | mojo/public/tests/system/core_unittest_pure_c.c | 85 |
6 files changed, 100 insertions, 3 deletions
diff --git a/mojo/mojo_public.gypi b/mojo/mojo_public.gypi index 79af450..3c029c0 100644 --- a/mojo/mojo_public.gypi +++ b/mojo/mojo_public.gypi @@ -94,6 +94,7 @@ 'sources': [ 'public/tests/system/core_cpp_unittest.cc', 'public/tests/system/core_unittest.cc', + 'public/tests/system/core_unittest_pure_c.c', ], }, { diff --git a/mojo/public/system/async_waiter.h b/mojo/public/system/async_waiter.h index fc7c207..6726b46 100644 --- a/mojo/public/system/async_waiter.h +++ b/mojo/public/system/async_waiter.h @@ -20,7 +20,7 @@ struct MojoAsyncWaiter { // of MojoWait to the given MojoAsyncWaitCallback on the current thread. // Returns a non-zero MojoAsyncWaitID that can be used with CancelWait to // stop waiting. This identifier becomes invalid once the callback runs. - MojoAsyncWaitID (*AsyncWait)(MojoAsyncWaiter* waiter, + MojoAsyncWaitID (*AsyncWait)(struct MojoAsyncWaiter* waiter, MojoHandle handle, MojoWaitFlags flags, MojoDeadline deadline, @@ -29,7 +29,7 @@ struct MojoAsyncWaiter { // Cancel an existing call to AsyncWait with the given MojoAsyncWaitID. The // corresponding MojoAsyncWaitCallback will not be called in this case. - void (*CancelWait)(MojoAsyncWaiter* waiter, + void (*CancelWait)(struct MojoAsyncWaiter* waiter, MojoAsyncWaitID wait_id); }; diff --git a/mojo/public/system/core.h b/mojo/public/system/core.h index bb36748..3d01d77 100644 --- a/mojo/public/system/core.h +++ b/mojo/public/system/core.h @@ -139,7 +139,7 @@ typedef uint64_t MojoDeadline; #ifdef __cplusplus const MojoDeadline MOJO_DEADLINE_INDEFINITE = static_cast<MojoDeadline>(-1); #else -#define MOJO_DEADLINE_INDEFINITE = ((MojoDeadline) -1); +#define MOJO_DEADLINE_INDEFINITE ((MojoDeadline) -1) #endif // |MojoWaitFlags|: Used to specify the state of a handle to wait on (e.g., the diff --git a/mojo/public/system/macros.h b/mojo/public/system/macros.h index f3a6034..4bd5e77 100644 --- a/mojo/public/system/macros.h +++ b/mojo/public/system/macros.h @@ -25,6 +25,8 @@ #define MOJO_WARN_UNUSED_RESULT #endif +// C++-only macros ------------------------------------------------------------- + #ifdef __cplusplus // Annotate a virtual method indicating it must be overriding a virtual method diff --git a/mojo/public/tests/system/core_unittest.cc b/mojo/public/tests/system/core_unittest.cc index 32174c3..2c31bef 100644 --- a/mojo/public/tests/system/core_unittest.cc +++ b/mojo/public/tests/system/core_unittest.cc @@ -104,6 +104,15 @@ TEST(CoreTest, Basic) { EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); } +// Defined in core_unittest_pure_c.c. +extern "C" const char* MinimalCTest(void); + +// This checks that things actually work in C (not C++). +TEST(CoreTest, MinimalCTest) { + const char* failure = MinimalCTest(); + EXPECT_TRUE(failure == NULL) << failure; +} + // TODO(vtl): Add multi-threaded tests. } // namespace diff --git a/mojo/public/tests/system/core_unittest_pure_c.c b/mojo/public/tests/system/core_unittest_pure_c.c new file mode 100644 index 0000000..82bef17 --- /dev/null +++ b/mojo/public/tests/system/core_unittest_pure_c.c @@ -0,0 +1,85 @@ +// Copyright 2014 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. + +#ifdef __cplusplus +#error "This file should be compiled as C, not C++." +#endif + +#include <stddef.h> +#include <string.h> + +// Include all the header files that are meant to be compilable as C. Start with +// core.h, since it's the most important one. +#include "mojo/public/system/core.h" +#include "mojo/public/system/async_waiter.h" +#include "mojo/public/system/macros.h" + +// The joys of the C preprocessor.... +#define STRINGIFY(x) #x +#define STRINGIFY2(x) STRINGIFY(x) +#define FAILURE(message) \ + __FILE__ "(" STRINGIFY2(__LINE__) "): Failure: " message + +// Poor man's gtest. +#define EXPECT_EQ(a, b) \ + do { \ + if ((a) != (b)) \ + return FAILURE(STRINGIFY(a) " != " STRINGIFY(b) " (expected ==)"); \ + } while (0) +#define EXPECT_NE(a, b) \ + do { \ + if ((a) == (b)) \ + return FAILURE(STRINGIFY(a) " == " STRINGIFY(b) " (expected !=)"); \ + } while (0) + +// This function exists mainly to be compiled and linked. We do some cursory +// checks and call it from a unit test, to make sure that link problems aren't +// missed due to deadstripping. Returns null on success and a string on failure +// (describing the failure). +const char* MinimalCTest(void) { +// TODO(vtl): stdint.h seems to be broken in MSVS before 2013 (at least in our +// build). Figure out what's going on. +#if !defined(_MSC_VER) || (_MSC_VER >= 1800) + MojoTimeTicks ticks = MojoGetTimeTicksNow(); + EXPECT_NE(ticks, 0); + + MojoHandle handle0 = MOJO_HANDLE_INVALID; + EXPECT_NE(MOJO_RESULT_OK, MojoClose(handle0)); + + EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, + MojoWait(handle0, MOJO_WAIT_FLAG_EVERYTHING, + MOJO_DEADLINE_INDEFINITE)); + + MojoHandle handle1 = MOJO_HANDLE_INVALID; + EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(&handle0, &handle1)); + + MojoWaitFlags wait_flags = MOJO_WAIT_FLAG_READABLE; + EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, + MojoWaitMany(&handle0, &wait_flags, 1, 1)); + + const char kHello[] = "hello"; + EXPECT_EQ(MOJO_RESULT_OK, + MojoWriteMessage(handle0, kHello, (uint32_t) sizeof(kHello), NULL, + 0u, MOJO_WRITE_DATA_FLAG_NONE)); + + EXPECT_EQ(MOJO_RESULT_OK, + MojoWait(handle1, MOJO_WAIT_FLAG_READABLE, + MOJO_DEADLINE_INDEFINITE)); + + char buffer[200] = { 0 }; + uint32_t num_bytes = (uint32_t) sizeof(buffer); + EXPECT_EQ(MOJO_RESULT_OK, + MojoReadMessage(handle1, buffer, &num_bytes, NULL, NULL, + MOJO_READ_MESSAGE_FLAG_NONE)); + EXPECT_EQ((uint32_t) sizeof(kHello), num_bytes); + EXPECT_EQ(0, memcmp(buffer, kHello, sizeof(kHello))); + + EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0)); + EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1)); + + // TODO(vtl): data pipe +#endif // !defined(_MSC_VER) || (_MSC_VER >= 1800) + + return NULL; +} |