• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This file tests the C Mojo system macros and consists of "positive" tests,
6 // i.e., those verifying that things work (without compile errors, or even
7 // warnings if warnings are treated as errors).
8 // TODO(vtl): Fix no-compile tests (which are all disabled; crbug.com/105388)
9 // and write some "negative" tests.
10 
11 #include "mojo/public/c/system/macros.h"
12 
13 #include <assert.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace mojo {
20 namespace {
21 
22 // First test |MOJO_STATIC_ASSERT()| in a global scope.
23 MOJO_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t),
24                    "Bad static_assert() failure in global scope");
25 
TEST(MacrosTest,CompileAssert)26 TEST(MacrosTest, CompileAssert) {
27   // Then in a local scope.
28   MOJO_STATIC_ASSERT(sizeof(int32_t) == 2 * sizeof(int16_t),
29                      "Bad static_assert() failure");
30 }
31 
TEST(MacrosTest,Alignof)32 TEST(MacrosTest, Alignof) {
33   // Strictly speaking, this isn't a portable test, but I think it'll pass on
34   // all the platforms we currently support.
35   EXPECT_EQ(1u, MOJO_ALIGNOF(char));
36   EXPECT_EQ(4u, MOJO_ALIGNOF(int32_t));
37   EXPECT_EQ(8u, MOJO_ALIGNOF(int64_t));
38   EXPECT_EQ(8u, MOJO_ALIGNOF(double));
39 }
40 
41 // These structs are used in the Alignas test. Define them globally to avoid
42 // MSVS warnings/errors.
43 #if defined(_MSC_VER)
44 #pragma warning(push)
45 // Disable the warning "structure was padded due to __declspec(align())".
46 #pragma warning(disable : 4324)
47 #endif
48 struct MOJO_ALIGNAS(1) StructAlignas1 {
49   char x;
50 };
51 struct MOJO_ALIGNAS(4) StructAlignas4 {
52   char x;
53 };
54 struct MOJO_ALIGNAS(8) StructAlignas8 {
55   char x;
56 };
57 #if defined(_MSC_VER)
58 #pragma warning(pop)
59 #endif
60 
TEST(MacrosTest,Alignas)61 TEST(MacrosTest, Alignas) {
62   EXPECT_EQ(1u, MOJO_ALIGNOF(StructAlignas1));
63   EXPECT_EQ(4u, MOJO_ALIGNOF(StructAlignas4));
64   EXPECT_EQ(8u, MOJO_ALIGNOF(StructAlignas8));
65 }
66 
67 }  // namespace
68 }  // namespace mojo
69