From c9264eecac6746dd0f4589508b0ebf648ee87478 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 30 Dec 2015 06:31:02 -0800 Subject: [PATCH] add debug-only code to assert that the hash on the move stack remains unchanged across a push and pop. Was used to verify memstream.c change just committed. --- xwords4/common/movestak.c | 45 ++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/xwords4/common/movestak.c b/xwords4/common/movestak.c index 09dcda766..b3b40619a 100644 --- a/xwords4/common/movestak.c +++ b/xwords4/common/movestak.c @@ -147,18 +147,22 @@ stack_getHashOld( StackCtxt* stack ) XP_U32 stack_getHash( const StackCtxt* stack ) { - XP_U32 hash; - XP_U16 len = 0; - stream_copyBits( stack->data, 0, stack->top, NULL, &len ); - XP_U8 buf[len]; - stream_copyBits( stack->data, 0, stack->top, buf, &len ); + XP_U32 hash = 0; + if ( !!stack->data ) { + XP_U16 len = 0; + stream_copyBits( stack->data, 0, stack->top, NULL, &len ); + if ( 0 < len ) { + XP_U8 buf[len]; + stream_copyBits( stack->data, 0, stack->top, buf, &len ); #ifdef DEBUG_HASHING - LOG_HEX( buf, len, __func__ ); + LOG_HEX( buf, len, __func__ ); #endif - hash = finishHash( augmentHash( 0L, buf, len ) ); + hash = finishHash( augmentHash( 0L, buf, len ) ); #ifdef DEBUG_HASHING - LOG_RETURNF( "%.8X", (unsigned int)hash ); + LOG_RETURNF( "%.8X", (unsigned int)hash ); #endif + } + } return hash; } /* stack_getHash */ #endif @@ -260,7 +264,7 @@ stack_copy( const StackCtxt* stack ) } static void -pushEntry( StackCtxt* stack, const StackEntry* entry ) +pushEntryImpl( StackCtxt* stack, const StackEntry* entry ) { XP_U16 ii, bitsPerTile; XWStreamPos oldLoc; @@ -322,7 +326,28 @@ pushEntry( StackCtxt* stack, const StackEntry* entry ) XP_LOGSTREAM( stack->data ); #endif SET_DIRTY( stack ); -} /* pushEntry */ +} /* pushEntryImpl */ + +static void +pushEntry( StackCtxt* stack, const StackEntry* entry ) +{ +#ifdef DEBUG_HASHING + XP_U32 origHash = stack_getHash( stack ); +#endif + + pushEntryImpl( stack, entry ); + +#ifdef DEBUG_HASHING + XP_U32 newHash = stack_getHash( stack ); + StackEntry lastEntry; + if ( stack_popEntry( stack, &lastEntry ) ) { + XP_ASSERT( origHash == stack_getHash( stack ) ); + pushEntryImpl( stack, &lastEntry ); + XP_ASSERT( newHash == stack_getHash( stack ) ); + XP_LOGF( "%s: all ok", __func__ ); + } +#endif +} static void readEntry( const StackCtxt* stack, StackEntry* entry )