Skip to content

Commit

Permalink
Unit tests...
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Mar 24, 2017
1 parent bc1f2d5 commit 064611f
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 133 deletions.
8 changes: 8 additions & 0 deletions CPPAtomic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
057E726B1E847DB4006B9999 /* Test-Helper in CopyFiles */ = {isa = PBXBuildFile; fileRef = 057E72611E847D62006B9999 /* Test-Helper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
057E726E1E847F1B006B9999 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 057E726D1E847F1B006B9999 /* CoreFoundation.framework */; };
057E726F1E8538C9006B9999 /* libCPPAtomic.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 05EC8A471E81E854007233F2 /* libCPPAtomic.a */; };
05A76A1A1E85A5040086C883 /* XS-IPC-TestBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 05A76A181E85A5040086C883 /* XS-IPC-TestBase.cpp */; };
05A76A1B1E85A5040086C883 /* XS-IPC-TestBase.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 05A76A191E85A5040086C883 /* XS-IPC-TestBase.hpp */; };
05CDB0391E81F85000DD9BB2 /* XS-IPC-Semaphore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 05CDB0381E81F85000DD9BB2 /* XS-IPC-Semaphore.cpp */; };
05D17E7D1E8294F20009AF6E /* Semaphore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 05D17E7C1E8294F20009AF6E /* Semaphore.cpp */; };
05EC8A5C1E81E945007233F2 /* libCPPAtomic.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 05EC8A471E81E854007233F2 /* libCPPAtomic.a */; };
Expand Down Expand Up @@ -169,6 +171,8 @@
057E72631E847D62006B9999 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
057E726D1E847F1B006B9999 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
05A6C6891C18AEDD002C6A44 /* Atomic.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Atomic.hpp; sourceTree = "<group>"; };
05A76A181E85A5040086C883 /* XS-IPC-TestBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "XS-IPC-TestBase.cpp"; sourceTree = "<group>"; };
05A76A191E85A5040086C883 /* XS-IPC-TestBase.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = "XS-IPC-TestBase.hpp"; sourceTree = "<group>"; };
05CDB0381E81F85000DD9BB2 /* XS-IPC-Semaphore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "XS-IPC-Semaphore.cpp"; sourceTree = "<group>"; };
05D17E7C1E8294F20009AF6E /* Semaphore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Semaphore.cpp; sourceTree = "<group>"; };
05D17E7F1E8295000009AF6E /* Semaphore.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Semaphore.hpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -225,6 +229,8 @@
children = (
053BEDC71B8EFE1E007D82A4 /* Info.plist */,
053BEDF91B90B37E007D82A4 /* Prefix.pch */,
05A76A181E85A5040086C883 /* XS-IPC-TestBase.cpp */,
05A76A191E85A5040086C883 /* XS-IPC-TestBase.hpp */,
05F58B2A1E81EDC3000D50A0 /* XS-Atomic-Functions.cpp */,
051F9C771C19C3EE005D471A /* XS-Atomic-Non-Trivial.cpp */,
05FCF6E61E83073800D98BFC /* XS-Atomic-Static.cpp */,
Expand Down Expand Up @@ -400,6 +406,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
05A76A1B1E85A5040086C883 /* XS-IPC-TestBase.hpp in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -655,6 +662,7 @@
05F58B251E81EB9D000D50A0 /* AtomicDecrement32.cpp in Sources */,
057CD63A1E81F36600A80970 /* RWLock.cpp in Sources */,
05F58B221E81EB9D000D50A0 /* AtomicCompareAndSwap32.cpp in Sources */,
05A76A1A1E85A5040086C883 /* XS-IPC-TestBase.cpp in Sources */,
05F58B281E81EB9D000D50A0 /* AtomicIncrement64.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
29 changes: 29 additions & 0 deletions Test-Helper/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <string>
#include <unistd.h>
#include <XS/IPC/Semaphore.hpp>
#include <XS/IPC/SharedMemory.hpp>

int main( int argc, const char * argv[] )
{
Expand Down Expand Up @@ -106,6 +107,34 @@ int main( int argc, const char * argv[] )
*( p ) = 0;
}
}
else if( arg == "mem-write" )
{
{
int id;
unsigned int size;
std::string str;

if( i >= argc - 3 )
{
std::cout << "Test-Helper: not enough arguments provided for " << arg << std::endl;

return -1;
}

id = std::stoi( argv[ ++i ] );
size = static_cast< unsigned int >( std::stoi( argv[ ++i ] ) );
str = argv[ ++i ];

{
XS::IPC::SharedMemory mem( id, size );

if( mem.IsValid() )
{
strcat( mem.Get< char * >(), str.c_str() );
}
}
}
}
}

std::cout << "Test-Helper: exiting" << std::endl;
Expand Down
109 changes: 3 additions & 106 deletions Unit-Tests/XS-IPC-Semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,122 +27,19 @@
* @brief ...
*/

/* Disabled warnings for GoogleMock */
#ifdef __clang__
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic push
#if __clang_major__ >= 7
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic ignored "-Wused-but-marked-unused"
#pragma clang diagnostic ignored "-Wdeprecated"
#endif

#include <GoogleMock/GoogleMock.h>

#ifdef __clang__
#pragma clang diagnostic pop
#endif

#ifdef __clang__
#pragma clang diagnostic ignored "-Wc++98-compat-local-type-template-args"
#endif

#include <XS/Atomic-Functions.hpp>
#include "XS-IPC-TestBase.hpp"
#include <XS/IPC/Semaphore.hpp>
#include <CoreFoundation/CoreFoundation.h>
#include <string>
#include <thread>
#include <atomic>
#include <unistd.h>

using namespace testing;

class XS_IPC_Semaphore: public Test
class XS_IPC_Semaphore: public XS_IPC_TestBase
{
private:

std::string _helperPath;

public:

virtual ~XS_IPC_Semaphore( void ) = default;

virtual void SetUp( void )
{
CFBundleRef bundle;
CFURLRef url;
CFStringRef str;
const char * s;
std::string path;

bundle = CFBundleGetBundleWithIdentifier( CFSTR( "com.xs-labs.CPPAtomic-Tests" ) );

if( bundle == nullptr )
{
throw std::runtime_error( "Error getting Test-Helper path in bundle" );
}

url = CFBundleCopyBundleURL( bundle );

if( url == nullptr )
{
throw std::runtime_error( "Error getting Test-Helper path in bundle" );
}

str = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle );

if( str == nullptr )
{
throw std::runtime_error( "Error getting Test-Helper path in bundle" );
}

s = CFStringGetCStringPtr( str, kCFStringEncodingUTF8 );

if( s == nullptr )
{
throw std::runtime_error( "Error getting Test-Helper path in bundle" );
}

path = s;
path += "/Contents/MacOS/Test-Helper";

CFRelease( str );
CFRelease( url );

this->_helperPath = path;
}

virtual void TearDown( void )
{}

void RunHelper( const std::string & command, const std::vector< std::string > & args ) const
{
this->RunHelper( { { command, args } } );
}

void RunHelper( const std::map< std::string, std::vector< std::string > > & commands ) const
{
std::string invoke;

invoke = this->_helperPath;

for( const auto & p: commands )
{
invoke += " ";
invoke += p.first;

for( const auto & a: p.second )
{
invoke += " ";
invoke += a;
}
}

system( invoke.c_str() );
}
};

TEST_F( XS_IPC_Semaphore, UnnamedBinaryTryWait )
Expand Down
53 changes: 26 additions & 27 deletions Unit-Tests/XS-IPC-SharedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,19 @@
* @brief ...
*/

/* Disabled warnings for GoogleMock */
#ifdef __clang__
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic push
#if __clang_major__ >= 7
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic ignored "-Wused-but-marked-unused"
#pragma clang diagnostic ignored "-Wdeprecated"
#endif

#include <GoogleMock/GoogleMock.h>

#ifdef __clang__
#pragma clang diagnostic pop
#endif

#include "XS-IPC-TestBase.hpp"
#include <XS/IPC/SharedMemory.hpp>

using namespace testing;

TEST( XS_IPC_SharedMemory, CTOR )
class XS_IPC_SharedMemory: public XS_IPC_TestBase
{
public:

virtual ~XS_IPC_SharedMemory( void ) = default;
};

TEST_F( XS_IPC_SharedMemory, CTOR )
{
XS::IPC::SharedMemory mem1;

Expand All @@ -60,7 +48,7 @@ TEST( XS_IPC_SharedMemory, CTOR )
ASSERT_EQ( mem1.Get(), nullptr );
}

TEST( XS_IPC_SharedMemory, CCTOR )
TEST_F( XS_IPC_SharedMemory, CCTOR )
{
XS::IPC::SharedMemory mem1( 666, 1024 );
XS::IPC::SharedMemory mem2( mem1 );
Expand All @@ -71,7 +59,7 @@ TEST( XS_IPC_SharedMemory, CCTOR )
ASSERT_NE( mem1.Get(), mem2.Get() );
}

TEST( XS_IPC_SharedMemory, OperatorEqual )
TEST_F( XS_IPC_SharedMemory, OperatorEqual )
{
XS::IPC::SharedMemory mem1;
XS::IPC::SharedMemory mem2;
Expand All @@ -87,7 +75,7 @@ TEST( XS_IPC_SharedMemory, OperatorEqual )
ASSERT_FALSE( mem4 == mem6 );
}

TEST( XS_IPC_SharedMemory, OperatorNotEqual )
TEST_F( XS_IPC_SharedMemory, OperatorNotEqual )
{
XS::IPC::SharedMemory mem1;
XS::IPC::SharedMemory mem2;
Expand All @@ -99,7 +87,7 @@ TEST( XS_IPC_SharedMemory, OperatorNotEqual )
ASSERT_FALSE( mem3 != mem4 );
}

TEST( XS_IPC_SharedMemory, Get )
TEST_F( XS_IPC_SharedMemory, Get )
{
void * p;
XS::IPC::SharedMemory mem( 666, 1024 );
Expand All @@ -110,16 +98,27 @@ TEST( XS_IPC_SharedMemory, Get )
ASSERT_EQ( p, mem.Get< char * >() );
}

TEST( XS_IPC_SharedMemory, GetSize )
TEST_F( XS_IPC_SharedMemory, GetSize )
{
XS::IPC::SharedMemory mem( 666, 1024 );

ASSERT_TRUE( mem.GetSize() == 1024 );
}

TEST( XS_IPC_SharedMemory, IsValid )
TEST_F( XS_IPC_SharedMemory, IsValid )
{
XS::IPC::SharedMemory mem( 666, 1024 );

ASSERT_TRUE( mem.IsValid() );
}

TEST_F( XS_IPC_SharedMemory, CrossProcess )
{
XS::IPC::SharedMemory mem( 666, 1024 );

ASSERT_TRUE( mem.IsValid() );

memset( mem.Get(), 0, mem.GetSize() );

ASSERT_TRUE( strlen( mem.Get< char * >() ) == 0 );
}

0 comments on commit 064611f

Please sign in to comment.