//
//  WriteTest.mm
//  bson
//
//  Created by Rakesh Vidyadharan on 09/03/2011.
//  Copyright 2011 Sans Pareil Technologies, Inc. All rights reserved.
//

#import "WriteTest.h"
#import <bson/bson.h>
#include <fstream>

using namespace mongo;
using namespace std;


@implementation WriteTest


- (void) testConstructor
{
  BSONObj orig = BSON( "name" << "Joe" << "age" << 33 );
  BSONObj clone = BSONObj( orig.objdata() );
  
  string json = clone.toString();
  STAssertTrue( json.length() > 0, @"JSON representation is empty" );
  STAssertTrue( orig == clone, @"Cloned instance not same" );
}


- (void) testReadWrite
{
  BSONObj orig = BSON( "name" << "Joe" << "age" << 33 );
  ofstream fout( "/tmp/test.bson", ios::binary );
  fout.write( orig.objdata(), orig.objsize() );
  fout << flush;
  fout.close();
  
  ifstream fin( "/tmp/test.bson", ios::binary );
  filebuf *pbuf = fin.rdbuf();

  long size = pbuf->pubseekoff( 0, ios::end, ios::in );
  pbuf->pubseekpos( 0, ios::in );
  STAssertTrue( size > 0, @"Input bson file is empty" );

  char* buffer = new char[size];
  pbuf->sgetn( buffer, size );
  
  BSONObj clone = BSONObj( buffer );
  STAssertTrue( orig == clone, @"De-serialised instance not same" );
  delete buffer;
  
  fin.close();
}


@end
