/******************************************************************************
   BDBBTreeRecordNumberTEST
   BDB
 
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA


  -------------------------------------------------------------------------
  Modification history

  16.01.2005 ola     initial version
  23.08.2006 ola     license changed
  -------------------------------------------------------------------------
******************************************************************************/

#include "BDBBTreeRecordNumberTEST.h"

@implementation BDBBTreeRecordNumberTEST

- setup {
  [BDBAbstractBTreeTEST deleteDatabase];
  return [ super setup ];
}


- testSpecificConfigurables: (BDBDatabaseConfig *) config {
  [super testSpecificConfigurables: config];
  [config setBTreeRecordNumbering: YES];
  
  return self;
}


- testRecordNumbering {
  BDBDatabaseEntry *key, *data;
  BDBDatabaseRecordNumber *recNo;
  int i;
  NSLog( @"BDBCursorTest::testRecordNumbering - BEGIN..." );
  
  NSLog( @"Adding testdata first..." );
  key = [[[BDBDatabaseEntry alloc] initWithObject: @"key1" ] autorelease];
  data = [[[BDBDatabaseEntry alloc] initWithObject: @"value1"] autorelease];
  [database putEntryWithTransaction:nil key: key value: data ];

  key = [[[BDBDatabaseEntry alloc] initWithObject: @"key2" ] autorelease];
  data = [[[BDBDatabaseEntry alloc] initWithObject: @"value2"] autorelease];
  [database putEntryWithTransaction:nil key: key value: data ];
    
  key = [[[BDBDatabaseEntry alloc] initWithObject: @"key3" ] autorelease];
  data = [[[BDBDatabaseEntry alloc] initWithObject: @"value3"] autorelease];
  [database putEntryWithTransaction:nil key: key value: data ];
  
  NSLog( @"Now fetching records by number..." );
  
  for( i = 1; i <= 3; i++ ) {
    recNo = [[[BDBDatabaseRecordNumber alloc] initWithEntryNumber: i ] 
	  autorelease];
    data = [[[BDBDatabaseEntry alloc] init] autorelease];
    NSAssert( BDB_STATUS_SUCCESS == [database getEntryWithTransaction:nil 
      recordNumber:recNo data: data], @"Fetching record number=1 FAILED!" );
  
    NSLog( @"Content of record numbered %d: %@", i, [data object] );
  }
  
  NSLog( @"Accessing non-existent record 4..." );
  recNo = [[[BDBDatabaseRecordNumber alloc] initWithEntryNumber: 4 ] 
    autorelease];
  data = [[[BDBDatabaseEntry alloc] init] autorelease];
    NSAssert( BDB_STATUS_NOTFOUND == [database getEntryWithTransaction:nil 
      recordNumber:recNo data: data], @"Fetching record number=4 SUCCEEDED!" );
  
  NSLog( @"Deleting first record and accessing all other records..." );
  key = [[[BDBDatabaseEntry alloc] initWithObject: @"key1" ] autorelease];
  [ database deleteEntryWithTransaction: nil key: key ];
  for( i = 1; i <= 2; i++ ) {
    recNo = [[[BDBDatabaseRecordNumber alloc] initWithEntryNumber: i ] 
	  autorelease];
    data = [[[BDBDatabaseEntry alloc] init] autorelease];
    NSAssert( BDB_STATUS_SUCCESS == [database getEntryWithTransaction:nil 
      recordNumber:recNo data: data], @"Fetching record number=1 FAILED!" );
  
    NSLog( @"Content of record numbered %d: %@", i, [data object] );
  }
	
  NSLog( @"BDBCursorTest::testRecordNumbering - FINISHED SUCCESSFULLY" );
  
  return self;
}
@end
