Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / c / system / tests / core_unittest_pure_c.c
1 // Copyright 2014 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 #ifdef __cplusplus
6 #error "This file should be compiled as C, not C++."
7 #endif
8
9 #include <stddef.h>
10 #include <string.h>
11
12 // Include all the header files that are meant to be compilable as C. Start with
13 // core.h, since it's the most important one.
14 #include "mojo/public/c/environment/async_waiter.h"
15 #include "mojo/public/c/system/core.h"
16 #include "mojo/public/c/system/macros.h"
17
18 // The joys of the C preprocessor....
19 #define STRINGIFY(x) #x
20 #define STRINGIFY2(x) STRINGIFY(x)
21 #define FAILURE(message) \
22   __FILE__ "(" STRINGIFY2(__LINE__) "): Failure: " message
23
24 // Poor man's gtest.
25 #define EXPECT_EQ(a, b) \
26   do { \
27     if ((a) != (b)) \
28       return FAILURE(STRINGIFY(a) " != " STRINGIFY(b) " (expected ==)"); \
29   } while (0)
30 #define EXPECT_NE(a, b) \
31   do { \
32     if ((a) == (b)) \
33       return FAILURE(STRINGIFY(a) " == " STRINGIFY(b) " (expected !=)"); \
34   } while (0)
35
36 // This function exists mainly to be compiled and linked. We do some cursory
37 // checks and call it from a unit test, to make sure that link problems aren't
38 // missed due to deadstripping. Returns null on success and a string on failure
39 // (describing the failure).
40 const char* MinimalCTest(void) {
41   // MSVS before 2013 *really* only supports C90: All variables must be declared
42   // at the top. (MSVS 2013 is more reasonable.)
43   MojoTimeTicks ticks;
44   MojoHandle handle0, handle1;
45   MojoWaitFlags wait_flags;
46   const char kHello[] = "hello";
47   char buffer[200] = { 0 };
48   uint32_t num_bytes;
49
50   ticks = MojoGetTimeTicksNow();
51   EXPECT_NE(ticks, 0);
52
53   handle0 = MOJO_HANDLE_INVALID;
54   EXPECT_NE(MOJO_RESULT_OK, MojoClose(handle0));
55
56   EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
57             MojoWait(handle0, MOJO_WAIT_FLAG_EVERYTHING,
58                      MOJO_DEADLINE_INDEFINITE));
59
60   handle1 = MOJO_HANDLE_INVALID;
61   EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(&handle0, &handle1));
62
63   wait_flags = MOJO_WAIT_FLAG_READABLE;
64   EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
65             MojoWaitMany(&handle0, &wait_flags, 1, 1));
66
67   EXPECT_EQ(MOJO_RESULT_OK,
68             MojoWriteMessage(handle0, kHello, (uint32_t) sizeof(kHello), NULL,
69                              0u, MOJO_WRITE_DATA_FLAG_NONE));
70
71   EXPECT_EQ(MOJO_RESULT_OK,
72             MojoWait(handle1, MOJO_WAIT_FLAG_READABLE,
73                      MOJO_DEADLINE_INDEFINITE));
74
75   num_bytes = (uint32_t) sizeof(buffer);
76   EXPECT_EQ(MOJO_RESULT_OK,
77             MojoReadMessage(handle1, buffer, &num_bytes, NULL, NULL,
78                             MOJO_READ_MESSAGE_FLAG_NONE));
79   EXPECT_EQ((uint32_t) sizeof(kHello), num_bytes);
80   EXPECT_EQ(0, memcmp(buffer, kHello, sizeof(kHello)));
81
82   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0));
83   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1));
84
85   // TODO(vtl): data pipe
86
87   return NULL;
88 }