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
19namespace mojo {
20namespace {
21
22TEST(MacrosTest, AllowUnused) {
23  // Test that no warning/error is issued even though |x| is unused.
24  int x MOJO_ALLOW_UNUSED = 123;
25}
26
27int MustUseReturnedResult() MOJO_WARN_UNUSED_RESULT;
28int MustUseReturnedResult() {
29  return 456;
30}
31
32TEST(MacrosTest, WarnUnusedResult) {
33  if (!MustUseReturnedResult())
34    abort();
35}
36
37// First test |MOJO_COMPILE_ASSERT()| in a global scope.
38MOJO_COMPILE_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t),
39                    bad_compile_assert_failure_in_global_scope);
40
41TEST(MacrosTest, CompileAssert) {
42  // Then in a local scope.
43  MOJO_COMPILE_ASSERT(sizeof(int32_t) == 2 * sizeof(int16_t),
44                      bad_compile_assert_failure);
45}
46
47TEST(MacrosTest, Alignof) {
48  // Strictly speaking, this isn't a portable test, but I think it'll pass on
49  // all the platforms we currently support.
50  EXPECT_EQ(1u, MOJO_ALIGNOF(char));
51  EXPECT_EQ(4u, MOJO_ALIGNOF(int32_t));
52  EXPECT_EQ(8u, MOJO_ALIGNOF(int64_t));
53  EXPECT_EQ(8u, MOJO_ALIGNOF(double));
54}
55
56// These structs are used in the Alignas test. Define them globally to avoid
57// MSVS warnings/errors.
58#if defined(_MSC_VER)
59#pragma warning(push)
60// Disable the warning "structure was padded due to __declspec(align())".
61#pragma warning(disable : 4324)
62#endif
63struct MOJO_ALIGNAS(1) StructAlignas1 {
64  char x;
65};
66struct MOJO_ALIGNAS(4) StructAlignas4 {
67  char x;
68};
69struct MOJO_ALIGNAS(8) StructAlignas8 {
70  char x;
71};
72#if defined(_MSC_VER)
73#pragma warning(pop)
74#endif
75
76TEST(MacrosTest, Alignas) {
77  EXPECT_EQ(1u, MOJO_ALIGNOF(StructAlignas1));
78  EXPECT_EQ(4u, MOJO_ALIGNOF(StructAlignas4));
79  EXPECT_EQ(8u, MOJO_ALIGNOF(StructAlignas8));
80}
81
82}  // namespace
83}  // namespace mojo
84