@ -132,3 +132,127 @@ TEST(UtilTest, UncheckedCalloc) {
TEST_AND_FREE ( UncheckedCalloc ( 0 ) ) ;
TEST_AND_FREE ( UncheckedCalloc ( 1 ) ) ;
}
template < typename T >
static void MaybeStackBufferBasic ( ) {
using node : : MaybeStackBuffer ;
MaybeStackBuffer < T > buf ;
size_t old_length ;
size_t old_capacity ;
/* Default constructor */
EXPECT_EQ ( 0U , buf . length ( ) ) ;
EXPECT_FALSE ( buf . IsAllocated ( ) ) ;
EXPECT_GT ( buf . capacity ( ) , buf . length ( ) ) ;
/* SetLength() expansion */
buf . SetLength ( buf . capacity ( ) ) ;
EXPECT_EQ ( buf . capacity ( ) , buf . length ( ) ) ;
EXPECT_FALSE ( buf . IsAllocated ( ) ) ;
/* Means of accessing raw buffer */
EXPECT_EQ ( buf . out ( ) , * buf ) ;
EXPECT_EQ ( & buf [ 0 ] , * buf ) ;
/* Basic I/O */
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
buf [ i ] = static_cast < T > ( i ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
/* SetLengthAndZeroTerminate() */
buf . SetLengthAndZeroTerminate ( buf . capacity ( ) - 1 ) ;
EXPECT_EQ ( buf . capacity ( ) - 1 , buf . length ( ) ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
buf . SetLength ( buf . capacity ( ) ) ;
EXPECT_EQ ( 0 , buf [ buf . length ( ) - 1 ] ) ;
/* Initial Realloc */
old_length = buf . length ( ) - 1 ;
old_capacity = buf . capacity ( ) ;
buf . AllocateSufficientStorage ( buf . capacity ( ) * 2 ) ;
EXPECT_EQ ( buf . capacity ( ) , buf . length ( ) ) ;
EXPECT_TRUE ( buf . IsAllocated ( ) ) ;
for ( size_t i = 0 ; i < old_length ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
EXPECT_EQ ( 0 , buf [ old_length ] ) ;
/* SetLength() reduction and expansion */
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
buf [ i ] = static_cast < T > ( i ) ;
buf . SetLength ( 10 ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
buf . SetLength ( buf . capacity ( ) ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
/* Subsequent Realloc */
old_length = buf . length ( ) ;
old_capacity = buf . capacity ( ) ;
buf . AllocateSufficientStorage ( old_capacity * 1.5 ) ;
EXPECT_EQ ( buf . capacity ( ) , buf . length ( ) ) ;
EXPECT_EQ ( static_cast < size_t > ( old_capacity * 1.5 ) , buf . length ( ) ) ;
EXPECT_TRUE ( buf . IsAllocated ( ) ) ;
for ( size_t i = 0 ; i < old_length ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
/* Basic I/O on Realloc'd buffer */
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
buf [ i ] = static_cast < T > ( i ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < T > ( i ) , buf [ i ] ) ;
/* Release() */
T * rawbuf = buf . out ( ) ;
buf . Release ( ) ;
EXPECT_EQ ( 0U , buf . length ( ) ) ;
EXPECT_FALSE ( buf . IsAllocated ( ) ) ;
EXPECT_GT ( buf . capacity ( ) , buf . length ( ) ) ;
free ( rawbuf ) ;
}
TEST ( UtilTest , MaybeStackBuffer ) {
using node : : MaybeStackBuffer ;
MaybeStackBufferBasic < uint8_t > ( ) ;
MaybeStackBufferBasic < uint16_t > ( ) ;
// Constructor with size parameter
{
MaybeStackBuffer < unsigned char > buf ( 100 ) ;
EXPECT_EQ ( 100U , buf . length ( ) ) ;
EXPECT_FALSE ( buf . IsAllocated ( ) ) ;
EXPECT_GT ( buf . capacity ( ) , buf . length ( ) ) ;
buf . SetLength ( buf . capacity ( ) ) ;
EXPECT_EQ ( buf . capacity ( ) , buf . length ( ) ) ;
EXPECT_FALSE ( buf . IsAllocated ( ) ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
buf [ i ] = static_cast < unsigned char > ( i ) ;
for ( size_t i = 0 ; i < buf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < unsigned char > ( i ) , buf [ i ] ) ;
MaybeStackBuffer < unsigned char > bigbuf ( 10000 ) ;
EXPECT_EQ ( 10000U , bigbuf . length ( ) ) ;
EXPECT_TRUE ( bigbuf . IsAllocated ( ) ) ;
EXPECT_EQ ( bigbuf . length ( ) , bigbuf . capacity ( ) ) ;
for ( size_t i = 0 ; i < bigbuf . length ( ) ; i + + )
bigbuf [ i ] = static_cast < unsigned char > ( i ) ;
for ( size_t i = 0 ; i < bigbuf . length ( ) ; i + + )
EXPECT_EQ ( static_cast < unsigned char > ( i ) , bigbuf [ i ] ) ;
}
// Invalidated buffer
{
MaybeStackBuffer < char > buf ;
buf . Invalidate ( ) ;
EXPECT_TRUE ( buf . IsInvalidated ( ) ) ;
EXPECT_FALSE ( buf . IsAllocated ( ) ) ;
EXPECT_EQ ( 0U , buf . length ( ) ) ;
EXPECT_EQ ( 0U , buf . capacity ( ) ) ;
buf . Invalidate ( ) ;
EXPECT_TRUE ( buf . IsInvalidated ( ) ) ;
}
}