//
//  BDBAddSimpleDataTest.m
//  BDB
//
//  Created by Oliver on 19.12.04.
//  Copyright 2004 Oliver Langer. All rights reserved.
//

#include "BDBAddSimpleDataTest.h"
#include <BDB/BDBOperationStatus.h>
#include <BDB/BDBDatabaseEntry.h>

@implementation BDBAddSimpleDataTest


#define __NR_TEST_ENTRIES 1000

- testSimpleAdditions {
  NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
  unsigned i;
  NSMutableArray *keys, *values;
  
  NSLog( @"BDBAddSimpleDataTest::testSimpleAdditions - BEGIN..." );
  NSLog( @"Writing %u entries...", __NR_TEST_ENTRIES );
  
  keys = [[[ NSMutableArray alloc ] init ] autorelease ];
  values = [[[ NSMutableArray alloc ] init ] autorelease ];
  
  for( i=0; i< __NR_TEST_ENTRIES; i++ ) {
    NSString *key, *value; 
	BDBDatabaseEntry *keyEntry, *dataEntry;

	key = [ [ NSString alloc ] initWithFormat: @"Key-%u", i ] ;
	value = [ [ NSString alloc ] initWithFormat: @"Value for %u", i ];
	
	keyEntry = [ [[BDBDatabaseEntry alloc] initWithObject: key] retain ];
	dataEntry = [ [[BDBDatabaseEntry alloc] initWithObject: value ] retain ];
	
    //NSLog( @"Adding key=%@", key );
	
	[ keys addObject: key ];
	[ values addObject: value ];

	[ database putEntryWithTransaction: nil key: keyEntry value: dataEntry ];
  }
    
  NSLog( @"Reading %u entries...",__NR_TEST_ENTRIES );
  for( i = 0; i < __NR_TEST_ENTRIES; i++ ) {
    BDBDatabaseEntry *valueEntry 
	  = [[[BDBDatabaseEntry alloc]init] autorelease ];
    BDBDatabaseEntry *keyEntry;

    BDBOperationStatus operationStatus;
	
	NSLog( @"Reading key=%@", [ keys objectAtIndex: i ] );	
	
	 
	keyEntry  = [[[BDBDatabaseEntry alloc]initWithObject: 
	  [keys objectAtIndex: i ] ] autorelease];
	  
	operationStatus = [ database getEntryWithTransaction: nil
	  key: keyEntry data: valueEntry ];
	
	ECAssertTrue( BDB_STATUS_SUCCESS == operationStatus,
	  @"Value for a key missing!" );
  }
    
  /**
   * Try to read an entry which does not exist
   */  
  {
    BDBOperationStatus status;
	BDBDatabaseEntry *keyEntry;
	BDBDatabaseEntry *dataEntry;
	keyEntry = [[[BDBDatabaseEntry alloc]
	  initWithObject: @"bla...5...42" ] autorelease];
	dataEntry = [[[BDBDatabaseEntry alloc]init]autorelease];
	
	status = [ database getEntryWithTransaction: nil 
	  key: keyEntry data: dataEntry ];
	  
	ECAssertTrue( status == BDB_STATUS_NOTFOUND, @"Found non-existent key?!?" );
  }

  NSLog( @"Tests SUCCESSFULLY DONE..." );
  
  [ pool release ];
  
  NSLog( @"BDBAddSimpleDataTest::testSimpleAdditions - FINISHED SUCCESSFULLY..." );
  return self;
}
@end
