Adding tests for SafeBits component.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@918 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2009-01-20 07:25:36 +00:00
parent 592aa240b6
commit 394e080a72
3 changed files with 502 additions and 0 deletions

View file

@ -0,0 +1,287 @@
////////////////////////////////////////////////////////////////////////////////
//
// This is a test program for Safe-Bit-Fields in the Loki Library.
// Copyright (c) 2009 by Fedor Pikus & Rich Sposato
// The copyright on this file is protected under the terms of the MIT license.
//
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
// The author makes no claims about the suitability of this software for any
// purpose. It is provided "as is" without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
// $Id$
#include <loki/SafeBits.h>
#include <iostream>
using namespace std;
using namespace Loki;
LOKI_BIT_FIELD( unsigned int ) Cat_state;
LOKI_BIT_CONST( Cat_state, CAT_NO_STATE, 0 ); // 0x0000 - no bit is set
LOKI_BIT_CONST( Cat_state, CAT_SLEEPING, 1 ); // 0x0001 - 1st bit is set
LOKI_BIT_CONST( Cat_state, CAT_PURRING, 2 ); // 0x0002 - 2nd bit is set
LOKI_BIT_CONST( Cat_state, CAT_PLAYING, 3 ); // 0x0004 - 3rd bit is set
LOKI_BIT_FIELD( unsigned int ) Dog_state;
LOKI_BIT_CONST( Dog_state, DOG_BARKING, 1 );
LOKI_BIT_CONST( Dog_state, DOG_CHEWING, 2 );
LOKI_BIT_CONST( Dog_state, DOG_DROOLING, 3 );
int main( void )
{
cout << "Running tests on Loki safe bit fields." << endl;
Cat_state cat_state = CAT_SLEEPING;
assert( cat_state );
Dog_state dog_state = DOG_DROOLING;
assert( dog_state );
bool happy = cat_state & ( CAT_SLEEPING | CAT_PURRING ); // OK
assert( happy );
assert( CAT_SLEEPING < CAT_PURRING );
assert( CAT_SLEEPING <= CAT_SLEEPING );
assert( CAT_SLEEPING <= CAT_PURRING );
assert( CAT_SLEEPING != CAT_PURRING );
assert( CAT_SLEEPING == CAT_SLEEPING );
assert( CAT_PURRING >= CAT_SLEEPING );
assert( CAT_PURRING >= CAT_PURRING );
#ifdef ERROR1
assert( DOG_DROOLING != CAT_SLEEPING ); // Can't compare different types.
#endif
#ifdef ERROR2
if ( cat_state & DOG_BARKING ) {} // Wrong bit type
#endif
#ifdef ERROR3
if ( cat_state & CAT_SLEEPING == 0 ) {} // Operator precedence
#endif
#ifdef ERROR4
if ( dog_state && DOG_BARKING ) {} // Logical &&
#endif
if ( dog_state & DOG_BARKING ) {} // OK
#ifdef ERROR5
Cat_state state0 = 0; // Conversion from non-safe bits
#endif
Cat_state state = CAT_NO_STATE; // OK
assert( !state );
state = ~state;
assert( state );
assert( state != CAT_NO_STATE );
assert( state & CAT_SLEEPING );
assert( state & CAT_PURRING );
assert( state & CAT_PLAYING );
state = CAT_SLEEPING;
assert( state == cat_state );
assert( state.size() == 8 * sizeof( unsigned int ) );
assert( sizeof( Cat_state ) == sizeof( unsigned int ) );
dog_state = DOG_BARKING;
#ifdef ERROR6
if ( dog_state == cat_state ) {} // Don't allow comparison of different types.
#endif
/// @note All These assertions are inside #ifdef sections because they
/// compare either Safe_bit_field or Safe_bit_const to literal integers.
/// If you compile any of these assertions they should generate errors.
/// These #ifdef sections exhaustively demonstrate that all possible
/// operations and comparisons with literal values are forbidden.
#ifdef ERROR7
assert( dog_state == 1 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR8
assert( dog_state != 2 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR9
assert( dog_state < 2 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR10
assert( dog_state > 0 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR11
assert( dog_state <= 1 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR12
assert( dog_state >= 1 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR13
assert( DOG_BARKING == 1 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR14
assert( DOG_BARKING != 2 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR15
assert( DOG_BARKING < 2 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR16
assert( DOG_BARKING > 0 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR17
assert( DOG_BARKING <= 1 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR18
assert( DOG_BARKING >= 1 ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR19
assert( ( dog_state | 1 ) != 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR20
assert( ( dog_state & 2 ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR21
assert( ( dog_state ^ 1 ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR22
assert( ( dog_state |= 2 ) == 3 ); // Don't allow operations with integers.
#endif
#ifdef ERROR23
assert( ( dog_state &= 3 ) == 2 ); // Don't allow operations with integers.
#endif
#ifdef ERROR24
assert( ( dog_state ^= 1 ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR25
assert( ( DOG_BARKING | 1 ) != 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR26
assert( ( DOG_BARKING & 2 ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR27
assert( ( DOG_BARKING ^ 1 ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR28
assert( ( DOG_BARKING |= 2 ) == 3 ); // Don't allow operations with integers.
#endif
#ifdef ERROR29
assert( ( DOG_BARKING &= 3 ) == 2 ); // Don't allow operations with integers.
#endif
#ifdef ERROR30
assert( ( DOG_BARKING ^= 1 ) == 0 ); // Don't allow operations with integers.
#endif
/// @note All These assertions are inside #ifdef sections because they
/// compare either Safe_bit_field or Safe_bit_const to an int variable.
/// If you compile any of these assertions they should generate errors.
/// These #ifdef sections exhaustively demonstrate that all possible
/// operations and comparisons with integers are forbidden.
int value = 1;
(void)value;
#ifdef ERROR31
assert( dog_state == value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR32
value = 2;
assert( dog_state != value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR33
value = 2;
assert( dog_state < value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR34
value = 0;
assert( dog_state > value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR35
value = 1;
assert( dog_state <= value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR36
value = 1;
assert( dog_state >= value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR37
value = 1;
assert( DOG_BARKING == value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR38
value = 2;
assert( DOG_BARKING != value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR39
value = 2;
assert( DOG_BARKING < value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR40
value = 0;
assert( DOG_BARKING > value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR41
value = 1;
assert( DOG_BARKING <= value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR42
value = 1;
assert( DOG_BARKING >= value ); // Don't allow comparisons to integers.
#endif
#ifdef ERROR43
value = 1;
assert( ( dog_state | value ) != 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR44
value = 2;
assert( ( dog_state & value ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR45
value = 1;
assert( ( dog_state ^ value ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR46
value = 2;
assert( ( dog_state |= value ) == 3 ); // Don't allow operations with integers.
#endif
#ifdef ERROR47
value = 3;
assert( ( dog_state &= value ) == 2 ); // Don't allow operations with integers.
#endif
#ifdef ERROR48
value = 1;
assert( ( dog_state ^= value ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR49
value = 1;
assert( ( DOG_BARKING | value ) != 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR50
value = 2;
assert( ( DOG_BARKING & value ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR51
value = 1;
assert( ( DOG_BARKING ^ value ) == 0 ); // Don't allow operations with integers.
#endif
#ifdef ERROR52
value = 2;
assert( ( DOG_BARKING |= value ) == 3 ); // Don't allow operations with integers.
#endif
#ifdef ERROR53
value = 3;
assert( ( DOG_BARKING &= value ) == 2 ); // Don't allow operations with integers.
#endif
#ifdef ERROR54
value = 1;
assert( ( DOG_BARKING ^= value ) == 0 ); // Don't allow operations with integers.
#endif
dog_state |= DOG_CHEWING;
assert( dog_state & ( DOG_CHEWING | DOG_BARKING ) );
dog_state &= DOG_CHEWING;
assert( dog_state == DOG_CHEWING );
dog_state = ~dog_state;
assert( dog_state != DOG_CHEWING );
dog_state = ~dog_state;
assert( dog_state == DOG_CHEWING );
cout << "If nothing asserted, then all tests passed!" << endl;
return 0;
}

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="SafeBits" />
<Option pch_mode="2" />
<Option compiler="cygwin" />
<Build>
<Target title="Debug">
<Option output="obj\Debug\SafeBits" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Debug\" />
<Option type="1" />
<Option compiler="cygwin" />
<Compiler>
<Add option="-Wall" />
<Add option="-g" />
<Add directory="..\..\include" />
</Compiler>
</Target>
<Target title="Release">
<Option output="obj\Release\SafeBits" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Release\" />
<Option type="1" />
<Option compiler="cygwin" />
<Compiler>
<Add option="-Os" />
<Add option="-Wall" />
<Add directory="..\..\include" />
</Compiler>
</Target>
</Build>
<Unit filename="SafeBitTest.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

View file

@ -0,0 +1,176 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="SafeBits"
ProjectGUID="{ECD7ED50-B99D-44BE-BA38-E17D6110C3E5}"
RootNamespace="SafeBits"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\..\include"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
WarningLevel="4"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="$(ProjectDir)\$(IntDir)\$(ProjectName).exe"
GenerateDebugInformation="true"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
AdditionalIncludeDirectories="..\..\include"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
WarningLevel="4"
DebugInformationFormat="0"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="$(ProjectDir)\$(IntDir)\$(ProjectName).exe"
GenerateDebugInformation="true"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\SafeBitTest.cpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>