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 <stdint.h> 11 #include <string.h> 12 13 // Include all the header files that are meant to be compilable as C. Start with 14 // core.h, since it's the most important one. 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 // Makeshift 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 46 ticks = MojoGetTimeTicksNow(); 47 EXPECT_NE(ticks, 0); 48 49 handle0 = MOJO_HANDLE_INVALID; 50 EXPECT_NE(MOJO_RESULT_OK, MojoClose(handle0)); 51 52 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 53 MojoQueryHandleSignalsState(handle0, NULL)); 54 55 handle1 = MOJO_HANDLE_INVALID; 56 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(NULL, &handle0, &handle1)); 57 58 MojoMessageHandle message; 59 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessage(NULL, &message)); 60 EXPECT_EQ(MOJO_RESULT_OK, 61 MojoSetMessageContext(message, 42, NULL, NULL, NULL)); 62 EXPECT_EQ(MOJO_RESULT_OK, MojoWriteMessage(handle0, message, NULL)); 63 EXPECT_EQ(MOJO_RESULT_OK, MojoReadMessage(handle1, NULL, &message)); 64 65 uintptr_t context; 66 EXPECT_EQ(MOJO_RESULT_OK, MojoGetMessageContext(message, NULL, &context)); 67 EXPECT_EQ(MOJO_RESULT_OK, 68 MojoSetMessageContext(message, 0, NULL, NULL, NULL)); 69 EXPECT_EQ(42, context); 70 EXPECT_EQ(MOJO_RESULT_OK, MojoDestroyMessage(message)); 71 72 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0)); 73 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1)); 74 75 return NULL; 76 } 77