- add third_party src.
[platform/framework/web/crosswalk.git] / src / native_client / tests / toolchain / abi_types.cc
1 // Copyright (c) 2011 The Native Client 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 test confirms the actual data types of types defined by various
6 // C/C++ standards. We must ensure that the actual types match on all
7 // platforms.
8
9 #include <setjmp.h>
10 #include <stddef.h>
11 #include <stdarg.h>
12 #include <sys/types.h>
13
14 #include <typeinfo>
15
16 #include "native_client/tests/toolchain/utils.h"
17
18 // Checks size and alignment.
19 template<typename T>
20 void CheckSizeAndAlignOf(const char* test_type, int size, unsigned int align) {
21   char error[256];
22   snprintf(error, sizeof(error), "sizeof(%s)=%d", test_type, sizeof(T));
23   ASSERT(sizeof(T) == size, error);
24   snprintf(error, sizeof(error), "alignof(%s)=%u", test_type, __alignof__(T));
25   ASSERT(__alignof__(T) == align, error);
26 }
27
28 // Checks type equivalency.
29 template<typename T, typename A>
30 void CheckType(const char* test_type) {
31   char error[256];
32   snprintf(error, sizeof(error), "typeid(%s)=%s", test_type, typeid(T).name());
33   ASSERT(typeid(T) == typeid(A), error);
34 }
35
36 // Checks type equivalency and size and alignment.
37 template<typename T, typename A>
38 void CheckTypeAndSizeAndAlignOf(const char* test_type,
39                                 int size,
40                                 unsigned int align) {
41   CheckType<T, A>(test_type);
42   CheckSizeAndAlignOf<T>(test_type, size, align);
43 }
44
45 int main() {
46   // setjmp.h
47   // TODO(eaeltsin): jmp_buf must be the same on all platforms.
48   // CheckSizeAndAlignOf<jmp_buf>("jmp_buf", 48, 8);  // x86-64
49   // CheckSizeAndAlignOf<jmp_buf>("jmp_buf", 36, 4);  // x86-32
50
51   // stddef.h
52   CheckTypeAndSizeAndAlignOf<size_t, unsigned int>("size_t", 4, 4);
53   CheckTypeAndSizeAndAlignOf<ptrdiff_t, int>("ptrdiff_t", 4, 4);
54   CheckSizeAndAlignOf<wchar_t>("wchar_t", 4, 4);
55
56   // stdarg.h
57   CheckSizeAndAlignOf<va_list>("va_list", 16, 4);
58
59   // sys/types.h
60   CheckTypeAndSizeAndAlignOf<ssize_t, int>("ssize_t", 4, 4);
61   // TODO(eaeltsin): add more types
62
63   return 0;
64 }