tstRTAssertCompile.cpp revision b529ce62f7f41042866f179ea4c103e913f499e0
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync/* $Id$ */
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync/** @file
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * IPRT Testcase - AssertCompile* - A Compile Time Testcase.
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync */
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync/*
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * Copyright (C) 2010 Oracle Corporation
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync *
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * available from http://www.virtualbox.org. This file is free software;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * General Public License (GPL) as published by the Free Software
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync *
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * The contents of this file may alternatively be used under the terms
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * of the Common Development and Distribution License Version 1.0
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * CDDL are applicable instead of those of the GPL.
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync *
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * You may elect to license modified versions of this file under the
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync * terms and conditions of either the GPL or the CDDL or both.
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync */
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync/*******************************************************************************
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync* Header Files *
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync*******************************************************************************/
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync#include <iprt/assert.h>
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsyncint main(int argc, char **argv)
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync{
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync /* Only positive tests here. */
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile(true);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile(1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile(2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile(99);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint8_t u8; NOREF( u8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint16_t u16; NOREF(u16);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint32_t u32; NOREF(u32);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint64_t u64; NOREF(u64);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSize( u8, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSize(u16, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSize(u32, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSize(u64, 8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment( u8, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u16, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u16, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u32, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u32, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u32, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u64, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u64, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u64, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileSizeAlignment(u64, 8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b529ce62f7f41042866f179ea4c103e913f499e0vboxsync typedef struct STRUCT12S
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync {
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint8_t u8;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint8_t au8[8];
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint64_t u64;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint8_t u8UnalignmentFiller1;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint32_t u32;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint8_t u8UnalignmentFiller2;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint16_t u16;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync const char *psz;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint32_t u32A;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync uint32_t u32B;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync } STRUCT1, STRUCT2;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSize(STRUCT1, u8, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSize(STRUCT1, u16, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSize(STRUCT1, u32, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSize(STRUCT1, u64, 8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u8, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u16, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u16, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u32, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u32, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u32, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u64, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u64, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u64, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, u64, 8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberSizeAlignment(STRUCT1, psz, sizeof(void *));
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u8, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u16, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u16, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u32, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u32, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u32, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u64, 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u64, 2);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u64, 4);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync#if defined(__GNUC__) && ARCH_BITS >= 64
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, u64, 8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync#endif
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberAlignment(STRUCT1, psz, sizeof(void *));
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberOffset(STRUCT1, u8, 0);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberOffset(STRUCT1, au8, 1);
b3b92d92835cf5987674d949b8bc55a4765d05b9vboxsync#ifndef _MSC_VER /** @todo figure out why MSC has trouble with these expressions */
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberOffset(STRUCT1, au8[0], 1);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMemberOffset(STRUCT1, au8[8], 9);
b3b92d92835cf5987674d949b8bc55a4765d05b9vboxsync#endif
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b529ce62f7f41042866f179ea4c103e913f499e0vboxsync typedef union UNION1U
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync {
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync STRUCT1 s1;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync STRUCT2 s2;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync } UNION1;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile2MemberOffsets(UNION1, s1.u8, s2.u8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile2MemberOffsets(UNION1, s1.u16, s2.u16);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile2MemberOffsets(UNION1, s1.u32, s2.u32);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile2MemberOffsets(UNION1, s1.u64, s2.u64);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompile2MemberOffsets(UNION1, s1.psz, s2.psz);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileAdjacentMembers(STRUCT1, u32A, u32B);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileAdjacentMembers(STRUCT1, u8, au8);
b3b92d92835cf5987674d949b8bc55a4765d05b9vboxsync#ifndef _MSC_VER /** @todo figure out why MSC has trouble with these expressions */
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileAdjacentMembers(STRUCT1, u8, au8[0]);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileAdjacentMembers(STRUCT1, au8[0], au8[1]);
b3b92d92835cf5987674d949b8bc55a4765d05b9vboxsync#endif
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersAtSameOffset(STRUCT1, u8, STRUCT2, u8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersAtSameOffset(STRUCT1, au8, STRUCT2, au8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersAtSameOffset(STRUCT1, u16, STRUCT2, u16);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersAtSameOffset(STRUCT1, u32, STRUCT2, u32);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersAtSameOffset(STRUCT1, u64, STRUCT2, u64);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSize(STRUCT1, u8, STRUCT2, u8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSize(STRUCT1, au8, STRUCT2, au8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSize(STRUCT1, u16, STRUCT2, u16);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSize(STRUCT1, u32, STRUCT2, u32);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSize(STRUCT1, u64, STRUCT2, u64);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSizeAndOffset(STRUCT1, u8, STRUCT2, u8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSizeAndOffset(STRUCT1, au8, STRUCT2, au8);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSizeAndOffset(STRUCT1, u16, STRUCT2, u16);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSizeAndOffset(STRUCT1, u32, STRUCT2, u32);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync AssertCompileMembersSameSizeAndOffset(STRUCT1, u64, STRUCT2, u64);
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync return 0;
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync}
b0d52aeac86d6a7d2564559be4be560f9e362c37vboxsync