// AStringAssignment //------------------------------------------------------------------------------ void TestAString::AStringAssignment() const { AString str; str = "test"; TEST_ASSERT( str.GetLength() == 4 ); TEST_ASSERT( str.GetReserved() >= 4 ); TEST_ASSERT( str.IsEmpty() == false ); TEST_ASSERT( str.MemoryMustBeFreed() == true ); AString str2; str2 = str; TEST_ASSERT( str2.GetLength() == 4 ); TEST_ASSERT( str2.GetReserved() >= 4 ); TEST_ASSERT( str2.IsEmpty() == false ); TEST_ASSERT( str2.MemoryMustBeFreed() == true ); const char * testData = "hellozzzzzzzzz"; AString str3; str3.Assign( testData, testData + 5 ); TEST_ASSERT( str3.GetLength() == 5 ); TEST_ASSERT( str3.GetReserved() >= 5 ); TEST_ASSERT( str3.IsEmpty() == false ); TEST_ASSERT( str3.MemoryMustBeFreed() == true ); // assign empty { AString dst; dst.Assign( AString::GetEmpty() ); } { AString dst; dst.Assign( AString::GetEmpty().Get(), AString::GetEmpty().Get() ); } }
REGISTER_TESTS_END // AStringConstructors //------------------------------------------------------------------------------ void TestAString::AStringConstructors() const { { // AString with no arguments AString empty; TEST_ASSERT( empty.GetLength() == 0 ); TEST_ASSERT( empty.GetReserved() == 0 ); TEST_ASSERT( empty.IsEmpty() == true ); TEST_ASSERT( empty.MemoryMustBeFreed() == false ); } { // AString with reserve capacity argument AString empty( 16 ); TEST_ASSERT( empty.GetLength() == 0 ); TEST_ASSERT( empty.GetReserved() == 16 ); TEST_ASSERT( empty.IsEmpty() == true ); TEST_ASSERT( empty.MemoryMustBeFreed() == true ); } { // AString from char * AString fromCharStar( "hello" ); TEST_ASSERT( fromCharStar.GetLength() == 5 ); TEST_ASSERT( fromCharStar.GetReserved() >= 5 ); TEST_ASSERT( fromCharStar.IsEmpty() == false ); TEST_ASSERT( fromCharStar.MemoryMustBeFreed() == true ); // AString from AString AString fromAString( fromCharStar ); TEST_ASSERT( fromAString.GetLength() == 5 ); TEST_ASSERT( fromAString.GetReserved() >= 5 ); TEST_ASSERT( fromAString.IsEmpty() == false ); TEST_ASSERT( fromAString.MemoryMustBeFreed() == true ); } { const char * hello = "hellohellohello"; AString fromCharStarPair( hello, hello + 5 ); TEST_ASSERT( fromCharStarPair.GetLength() == 5 ); TEST_ASSERT( fromCharStarPair.GetReserved() >= 5 ); TEST_ASSERT( fromCharStarPair.IsEmpty() == false ); TEST_ASSERT( fromCharStarPair.MemoryMustBeFreed() == true ); } }
// GetEnvVariable //------------------------------------------------------------------------------ /*static*/ bool Env::GetEnvVariable( const char * envVarName, AString & envVarValue ) { #if defined( __WINDOWS__ ) // try to get the env variable into whatever storage we have available uint32_t maxSize = envVarValue.GetReserved(); DWORD ret = ::GetEnvironmentVariable( envVarName, envVarValue.Get(), maxSize ); // variable does not exist if ( ret == 0 ) { return false; } // variable exists - was there enough space? if ( ret > maxSize ) { // not enough space, allocate enough envVarValue.SetReserved( ret ); maxSize = envVarValue.GetReserved(); ret = ::GetEnvironmentVariable( envVarName, envVarValue.Get(), maxSize ); ASSERT( ret <= maxSize ); // should have fit } // make string aware of valid buffer length as populated by ::GetEnvironmentVariable envVarValue.SetLength( ret ); return true; #elif defined( __LINUX__ ) || defined( __APPLE__ ) const char * envVar = ::getenv( envVarName ); if ( envVar ) { envVarValue = envVar; return true; } return false; #else #error Unknown platform #endif }