Alex Vinokur | 23 Mar 06:39
Picon

<Source> Simple C/C++ Perfometer: Reading file to string (Versions 1.x)


#####################
### Program files ###
#####################

=========================================
* Performance
* Comparative Performance Measurement
-----------------------------------------
* Tool     : Simple C/C++ Perfometer
* Algorithm: Reading file to string
* Language : C++
* Content  : Program files
* Version  : F2S-1.0
-----------------------------------------
* Attachments
  - file2str-1-0.cpp
=========================================

  Alex Vinokur
     email: alex DOT vinokur AT gmail DOT com
     http://mathforum.org/library/view/10978.html
     http://sourceforge.net/users/alexvn


// ===========================================================================
//
//  C/C++ Program Performance Measurement
    #define PROGRAM_NAME     "Simple C/C++ Perfometer : Reading file to string"
    #define PROGRAM_VERSION  "Version F2S-1.0"
//
//  -------------------------------------
//
//  Copyright (C) 2002-2005 Alex Vinokur
//
//  --------------------------------------------------------------------------
//
//  This software is provided 'as-is', without any express or implied
//  warranty.  In no event will the author be held liable for any damages
//  arising from the use of this software.
//
//  Permission is granted to anyone to use this software for any purpose,
//  including commercial applications, and to alter it and redistribute it
//  freely, subject to the following restrictions:
//
//  1. The origin of this software must not be misrepresented; you must not
//     claim that you wrote the original software. If you use this software
//     in a product, an acknowledgment in the product documentation would be
//     appreciated but is not required.
//  2. Altered source versions must be plainly marked as such, and must not be
//     misrepresented as being the original software.
//  3. This notice may not be removed or altered from any source distribution.
//
//     Alex Vinokur
//     
//  --------------------------------------------------------------------------
// 
//  email:alex DOT vinokur AT gmail DOT com
//  http://up.to/alexv
//
// ===========================================================================



// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
//    Testsuites
//    ----------
//    C-01      : C-Functions getc() and putc()
//    C-02      : C-Functions fgetc() and fputc()
//    C-03      : C-Functions fread() and fwrite() - with const size buffer
//    C-04      : C-Functions fread() and fwrite() - with max size buffer
//    unix_C_05 : UNIX system call mmap
//    CPP-01    : istream::operator>> and ostream::operator<<
//    CPP-02    : streambuf::sbumpc() and streambuf::sputc()
//    CPP-03    : streambuf::sbumpc() and ostream::operator<<
//    CPP-04    : ifstream::rdbuf() and ostream::operator<<
//    CPP-05    : istream::read() and ostream::write() - with const size buffer
//    CPP-06    : istream::read() and ostream::write(), std::ostringstream, ostream::operator<< - with const buffer
//    CPP-07    : istream::readsome() and ostream::write() - with const size buffer
//    CPP-08    : istream::read() and ostream::write() - with max size buffer
//    CPP-09    : std::getline, std::ostringstream, ostream::operator<<
//    CPP-10    : std::getline, std::string, ostream::operator<<
//    CPP-11    : istream::getline, std::ostringstream, ostream::operator<<
//    CPP-12    : istream::get(char) and ostream::put
//    CPP-13    : istream::get(char)
//    CPP-14    : istream::get(char*, streamsize) , ostream::operator<< - with const size buffer
//    CPP-15    : istream::get(streambuf&)  and std::streambuf, ostream::operator<<
//    CPP-16    : std::istream_iterator, std::ostream_iterator and std::copy
//    CPP-17    : std::istream_iterator, std::string
//    CPP-18    : std::istreambuf_iterator, std::ostreambuf_iterator and std::copy
//    CPP-19    : std::istreambuf_iterator, std::ostreambuf_iterator and std::transform
//    CPP-20    : std::istreambuf_iterator and std::string
//    CPP-21    : std::vector and std::copy
//    CPP-22    : std::vector and push_back()
//    CPP-23    : std::vector and istream::read()
//    CPP-24    : std::string and istream::read()
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



// --------------------------------------
#if ((defined unix) || (defined __unix) || (defined unix__) || (defined __unix__))
#if (!((defined DJGPP) || (defined __DJGPP) || (defined DJGPP__) || (defined __DJGPP__)))
#define UNIX_ENV
#endif
#endif

// ==============
#include <ctime>
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <iterator>
#include <algorithm>
#ifdef UNIX_ENV
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#endif
using namespace std;

// --------------------------------------
typedef unsigned long ulong;
typedef unsigned int  uint;

// --------------------------------------
#define  MAX_VALUE(x,y)  ((x) > (y) ? (x) : (y))
#define  ASSERT(x)       if (!(x)) \
			 { \
			 assert(x); \
			 cerr << "[" \
			      << __FILE__ \
			      << ", " \
			      << __LINE__  \
			      << "] assert() not working" \
			      << endl;  \
			      abort(); \
			 }


// ========================================
// ====== Part-1: Common Auxilary Functions
// ========================================

// ------------------------------
// Function 1.01
static string get_compiler_info()
// ------------------------------
{
ostringstream oss;
ostringstream tss;
string str;

// ------ GNU gcc ------
#ifdef __GNUC__
  oss.str("");
  tss.str("");
  str.erase();

  oss << "GNU gcc " << __GNUC__;
#ifdef __GNUC_MINOR__
  oss << "." << __GNUC_MINOR__;
#ifdef __GNUC_PATCHLEVEL__
#if (__GNUC_PATCHLEVEL__)
  oss << "." << __GNUC_PATCHLEVEL__;
#endif
#endif
#endif

#if (__CYGWIN32__ || __CYGWIN__)
  oss << " (CYGWIN)";
#endif

#if (__MINGW32__ || __MINGW__ )
  oss << " (MINGW)";
#endif

#if (__DJGPP__)
  oss << " (DGGPP " << __DJGPP__;
#ifdef __DJGPP_MINOR__
  oss << "." << __DJGPP_MINOR__;
#endif
  oss << ")";
#endif

#endif

// ------ Microsoft C++ ------
#ifdef _MSC_VER
  oss.str("");
  tss.str("");
  str.erase();

  oss << "Microsoft C++ ";
  tss << _MSC_VER;
  str = tss.str();
  ASSERT (str.size() == 4);
  oss << str[0] << str[1] << "." << str[2] << str[3];

#ifdef _MANAGED

#if (_MANAGED)
  oss << " (Managed)";
#else
  ASSERT (0);
  oss << " (Unmanaged)";
#endif
#else

  oss << " (Unmanaged)";
#endif

#endif

// ------ Intel C++ ------
#ifdef __INTEL_COMPILER
  oss.str("");
  tss.str("");
  str.erase();

  oss << "Intel C++ ";
  tss << __INTEL_COMPILER;
  str = tss.str();
  ASSERT (str.size() == 3);
  oss << str[0] << "." << str[1];
#endif

// ------ Borland C++ ------
#ifdef __BCPLUSPLUS__
  oss.str("");
  tss.str("");
  str.erase();

  oss << "Borland C++ ";
  tss << hex << __BCPLUSPLUS__;
  str = tss.str();
  ASSERT (str.size() == 3);
  oss << str[0] << "." << str[1] << "." << str[2];
#endif

// ------ Digital Mars C++ ------
#ifdef __DMC__
  oss.str("");
  tss.str("");
  str.erase();

#ifndef __DMC_VERSION_STRING__
#error __DMC_VERSION_STRING__ Not Defined
#endif
  oss << __DMC_VERSION_STRING__;
#endif

  return oss.str();

} // get_compiler_info


// -----------------------------
// Function 1.02
static void show_compiler_info()
// -----------------------------
{
const string str(get_compiler_info());

  if (str.empty()) return;

  cout << string (str.size(), '-') << endl;
  cout << str << endl;
  cout << string (str.size(), '-') << endl;
}



// -----------------------------------------
// Function 1.03
static size_t get_filesize_via_fseek_ftell (
	const char * const filename_i, 
	const bool         is_txt_mode_i
	)
// -----------------------------------------
{
FILE* fp = NULL;

  fp = fopen(filename_i, is_txt_mode_i ? "r" : "rb");
  ASSERT (fp);

int rc = fseek(fp, 0, SEEK_END);
  ASSERT (rc == 0);

const size_t ret_filesize (ftell(fp));

  rc = fclose(fp);
  ASSERT (rc == 0);

  return ret_filesize;
}


#ifdef UNIX_ENV


// -----------------------------------------
// Function 1.04
static size_t get_filesize_via_lseek (
	const char * const filename_i, 
	const bool         is_txt_mode_i
	)
{
// -----------------------------------------
int fd = -1;

  fd = open(filename_i, is_txt_mode_i ? O_RDONLY : O_RDONLY | O_BINARY);
  ASSERT (fd != -1);

off_t rc;
  rc = lseek(fd, 0, SEEK_END);
  ASSERT (rc != -1);

const size_t ret_filesize (static_cast<size_t>(rc));

  rc = close(fd);
  ASSERT (rc == 0);

  return ret_filesize;
}



// -----------------------------------------
// Function 1.05
static size_t get_filesize_via_fstat (
	const char * const filename_i, 
	const bool         is_txt_mode_i
	)
// -----------------------------------------
{
int fd = -1;

  fd = open(filename_i, is_txt_mode_i ? O_RDONLY : O_RDONLY | O_BINARY);
  ASSERT (fd != -1);

struct stat buf;
int rc = fstat(fd, &buf);
  ASSERT (rc == 0);

const size_t ret_filesize (static_cast<size_t>(buf.st_size));

  rc = close(fd);
  ASSERT (rc == 0);

  return ret_filesize;
}


// -----------------------------------------
// Function 1.06
static size_t get_filesize_via_stat (
	const char * const filename_i
	)
// -----------------------------------------
{
struct stat buf;

int rc = stat (filename_i, &buf);
  ASSERT (rc == 0);

const size_t ret_filesize (static_cast<size_t>(buf.st_size));

  return ret_filesize;
}

#endif


// -----------------------------------------
// Function 1.07
static size_t get_filesize_via_seekg_tellg (
	const char * const filename_i, 
	const bool         is_txt_mode_i
	)
// -----------------------------------------
{
ifstream fs;

  if (is_txt_mode_i) fs.open (filename_i);
  else               fs.open (filename_i, ios::binary);

  ASSERT (fs);
  ASSERT (fs.is_open());


   fs.seekg(0, ios::beg);
const ios::pos_type start_pos = fs.tellg();

   fs.seekg(0, ios::end);
const ios::pos_type end_pos = fs.tellg();

const size_t ret_filesize (static_cast<size_t>(end_pos - start_pos));

  fs.close();
  ASSERT (!fs.is_open());

  return ret_filesize;
}


// -----------------------------------------
// Function 1.08
static size_t get_filesize_via_distance (
	const char * const filename_i, 
	const bool         is_txt_mode_i
	)
// -----------------------------------------
{
ifstream fs;

  if (is_txt_mode_i) fs.open (filename_i);
  else               fs.open (filename_i, ios::binary);

  ASSERT (fs);
  ASSERT (fs.is_open());

const size_t ret_filesize (static_cast<size_t>(distance(
	istreambuf_iterator<char>(fs),
	istreambuf_iterator<char>()
	)));

  fs.close();
  ASSERT (!fs.is_open());

  return ret_filesize;
}


// --------------------------------
// Function 1.09
static string file_to_string (
	ifstream& fin_io,
	size_t    filesize_i
	)
// --------------------------------
{
  ASSERT (!fin_io.bad());

  ASSERT (fin_io);
  ASSERT (fin_io.is_open());

const ios::iostate prev_state = fin_io.rdstate(); 
const ios::pos_type prev_pos  = fin_io.tellg();  


string ret_str (filesize_i, '0');

  // ---------------------
  fin_io.clear();
  fin_io.seekg(0, ios::beg);

char ch;
ulong i;
  for (i = 0; fin_io.get (ch); i++)
  {
    ASSERT (i < ret_str.size());
    ret_str[i] = ch;
  }

  ASSERT (i <= filesize_i);
  ret_str.erase (i);

  // ---------------------------
  fin_io.clear(prev_state);
  fin_io.seekg(prev_pos, ios::beg);

  ASSERT (prev_state == fin_io.rdstate());
  ASSERT (prev_pos   == fin_io.tellg());
  // ---------------------------

  return ret_str;

} // file_to_string (1)

// ====================================================
static string file_to_string (
	const char * const filename_i, 
	const bool         is_txt_mode_i
	)
// ====================================================
{
ifstream fin;

  if (is_txt_mode_i) fin.open (filename_i);
  else               fin.open (filename_i, ios::binary);

  ASSERT (fin);
  ASSERT (fin.is_open());

const size_t filesize (get_filesize_via_seekg_tellg (filename_i, is_txt_mode_i));
const string ret_str (file_to_string (fin, filesize));

  fin.close();
  ASSERT (!fin.is_open());

  return ret_str;

} // file_to_string (2)



// ===================================
// ====== Part-2: Local Data & Defines
// ===================================

// --------------------------------------
static vector<string>           foo_names;
static vector<string>::iterator iter_names;
static vector<vector<clock_t> > used_time;

static ulong  run_no = 0;
static ulong  test_no = 0;

// static size_t filesize_via_fseek_ftell = 0;
#ifdef UNIX_ENV
// static size_t filesize_via_lseek       = 0;
// static size_t filesize_via_fstat       = 0;
// static size_t filesize_via_stat        = 0;
#endif
// static size_t filesize_via_seekg_tellg = 0;
// static size_t filesize_via_distance    = 0;


static size_t user_defined_filesize = 0;
static size_t infile_size1_txt      = 0;
static size_t infile_size2_txt      = 0;
static size_t infile_size_bin       = 0;

static string  infile_content_txt;
static string  infile_content_bin;
static string& infile_content = infile_content_txt;
static string  ret_string;

static uint   foo_setw = 0;

// ------------------------------
#define SMART_ASSERT(x) if (!(x)) { cerr << "SOURCE LINENo = " << lineno_i << endl; ASSERT(x); abort(); }


#define TRACE(x) 
// #define TRACE(x)  cerr << x << endl

#define CHECK_RETURNED_STRING(x) check_returned_string(x, __LINE__) 
#define CHECK_TXT_RETURNED_STRING CHECK_RETURNED_STRING(true) 
#define CHECK_BIN_RETURNED_STRING CHECK_RETURNED_STRING(false) 


// ------------------------------
#define INPUT_TXT_FILE_NAME   "z-txt.in"
#define INPUT_BIN_FILE_NAME   "z-bin.in"

#define ROWS_IN_INPUT_FILE 7

static FILE* fp_txt_in;
static FILE* fp_bin_in;

#ifdef UNIX_ENV
static int fd_txt_in;
static int fd_bin_in;
#endif

static ifstream fs_txt_in;
static ifstream fs_bin_in;

#define BUFFER_SIZE  4096

static char* mbuffer = NULL;




// =======================================
// ====== Part-3: Local Auxilary Functions
// =======================================

// -----------------------------
// Function 3.01
void check_returned_string (
	const bool         is_txt_mode_i, 
	const int          lineno_i
	)
// -----------------------------
{
const string infile_content (is_txt_mode_i ? infile_content_txt : infile_content_bin);

  if (!(infile_content.size() == ret_string.size()))
  {
    cerr << "FATAL ERROR: infile size = " 
         << infile_content.size() 
         << ", " 
         << " returned string size = "
         << ret_string.size()
         << "; mode = "
         << (is_txt_mode_i ? "txt" : "bin")
         << endl;
  }

  SMART_ASSERT ((infile_content.size() == ret_string.size()));
  SMART_ASSERT ((infile_content == ret_string));
  ret_string.erase();
}



// ==========================================
// ====== Part-4: Locat Preparation Functions
// ==========================================


// ---------------------------------------
// Function 4.01
static void fill_input_file (
	uint total_full_rows = ROWS_IN_INPUT_FILE
	)
// ---------------------------------------
{
  ASSERT (total_full_rows > 0);
  if (user_defined_filesize < total_full_rows)
  {
    total_full_rows = user_defined_filesize;
  }
  ASSERT (total_full_rows <= user_defined_filesize);

  remove (INPUT_TXT_FILE_NAME);
  remove (INPUT_BIN_FILE_NAME);

ofstream test_txt_infile (INPUT_TXT_FILE_NAME);
  ASSERT (test_txt_infile);
  ASSERT (test_txt_infile.is_open());

ofstream test_bin_infile (INPUT_BIN_FILE_NAME, ios::binary);
  ASSERT (test_bin_infile);
  ASSERT (test_bin_infile.is_open());

const uint row_size (user_defined_filesize/total_full_rows);


#define START_CH 0
char ch = START_CH;

ulong counter = 0;
  for (ulong row_no = 0; row_no < total_full_rows; row_no++)
  {
    for (ulong ch_no = 0; ch_no < row_size; ch_no++)
    {
      while (!isprint(ch)) ch++;
      ASSERT (counter < user_defined_filesize);

      test_txt_infile << ch;
      test_bin_infile << ch;
      ch++;
      counter++;
      if (counter == user_defined_filesize) break;

      if (ch == SCHAR_MAX) ch = START_CH;
    }

    if (counter == user_defined_filesize) break;
    ASSERT (counter < user_defined_filesize);

    test_txt_infile << '\n';
    test_bin_infile << '\n';
    counter++;
    if (counter == user_defined_filesize) break;
  }

  /*
  for (ulong i = 0; i < user_defined_filesize; i++)
  {
    while (!isprint(ch)) ch++;
    test_txt_infile << ch++;
    if (ch == SCHAR_MAX) ch = 0;
  }
  */

  test_txt_infile.close();
  ASSERT (!test_txt_infile.is_open());

  test_bin_infile.close();
  ASSERT (!test_bin_infile.is_open());


  infile_content_txt = file_to_string (INPUT_TXT_FILE_NAME, true);
  infile_content_bin = file_to_string (INPUT_BIN_FILE_NAME, false);

  ASSERT (infile_content_txt == infile_content_bin);
  
  infile_size1_txt = infile_content_txt.size(); 
  infile_size2_txt = infile_size1_txt
                   + count (infile_content_txt.begin(), infile_content_txt.end(), '\n');
  infile_size_bin  = infile_content_bin.size(); 

  ASSERT (infile_size1_txt <= user_defined_filesize);
  ASSERT (infile_size1_txt <= infile_size2_txt);
  ASSERT (infile_size1_txt == infile_size_bin);
  ASSERT (infile_size_bin <= infile_size2_txt);


  if (!(infile_size_bin <= infile_size1_txt))
  {
    cerr << "Uder defined filesize = " << user_defined_filesize << endl;
    cerr << "Txt input file size1  = " << infile_size1_txt << endl;
    cerr << "Txt input file size2  = " << infile_size2_txt << endl;
    cerr << "Bin input file size   = " << infile_size_bin << endl;
    ASSERT (infile_size_bin <= infile_size1_txt);
  }

  ASSERT (infile_size_bin <= infile_size2_txt);

  ASSERT (infile_size_bin == user_defined_filesize);

/*
  cout << endl;
  cout << endl;
  cout << "Uder defined file size = " << user_defined_filesize << endl;
  cout << "Txt input file size    = " << infile_size2_txt << endl;
  cout << "Bin input file size    = " << infile_size_bin << endl;
  cout << endl;
  cout << endl;
*/

} // fill_input_file


// ====================================
// ====== Part-5: Function To Be Tested
// ====================================

// --------------------------------------
string C_01_txt__functions_getc_putc ()
{
string ret_str;
int    int_ch;

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((int_ch = getc(fp_txt_in)) != EOF) 
  {
    ret_str += char(int_ch);  
  }
  // ------------------

  return ret_str;
}

// --------------------------------------
string C_01_bin__functions_getc_putc ()
{
string ret_str;
int    int_ch;

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((int_ch = getc(fp_bin_in)) != EOF)
  {
    ret_str += char(int_ch);  
  }
  // ------------------

  return ret_str;
}

// --------------------------------------
string C_02_txt__functions_fgetc_fputc ()
{
string ret_str;
int    int_ch;

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((int_ch = fgetc(fp_txt_in)) != EOF)
  {
    ret_str += char(int_ch);  
  }
  // ------------------

  return ret_str;

}

// --------------------------------------
string C_02_bin__functions_fgetc_fputc ()
{
string ret_str;
int    int_ch;

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((int_ch = fgetc(fp_bin_in)) != EOF)
  {
    ret_str += char(int_ch);  
  }
  // ------------------

  return ret_str;

}

// --------------------------------------
string C_03_txt__functions_fread_fwrite__const_buf ()
{
string ret_str;
char   cbuffer[BUFFER_SIZE];
size_t nread;

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer) - 1, fp_txt_in)) > 0)
  {
    cbuffer[nread] = 0;
    ret_str += cbuffer;  
  }
  // ------------------

  return ret_str;

}

// --------------------------------------
string C_03_bin__functions_fread_fwrite__const_buf ()
{
string ret_str;
char   cbuffer[BUFFER_SIZE];
size_t nread;

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer) - 1, fp_bin_in)) > 0)
  {
    cbuffer[nread] = 0;
    ret_str += cbuffer;  
  }
  // ------------------
  return ret_str;

}



// --------------------------------------
string C_04_txt__functions_fread_fwrite__max_buf ()
{
string ret_str;
size_t nread;

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((nread = fread(mbuffer, sizeof(char), infile_size2_txt, fp_txt_in)) > 0)
  {
    mbuffer[nread] = 0;
    ret_str += mbuffer;  
  }
  // ------------------
  return ret_str;

}

// --------------------------------------
string C_04_bin__functions_fread_fwrite__max_buf ()
{
string ret_str;
size_t   nread;

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((nread = fread(mbuffer, sizeof(char), infile_size_bin, fp_bin_in)) > 0)
  {
    mbuffer[nread] = 0;
    ret_str += mbuffer;  
  }
  // ------------------
  return ret_str;

}

#ifdef UNIX_ENV
// --------------------------------------
string unix_C_05_txt__mmap ()
{
string ret_str;
off_t  rc;
  rc = lseek(fd_txt_in, 0, SEEK_SET);
  ASSERT (rc != -1);

  // ------ Body ------
char* ptr = (char*)mmap(0, infile_size2_txt, PROT_READ, MAP_SHARED, fd_txt_in, 0);
  ASSERT (ptr != MAP_FAILED);

  ret_str = string(ptr, ptr + infile_size2_txt);
  rc = munmap(ptr, infile_size2_txt);
  ASSERT (rc == 0);
  // ------------------
  ret_str.erase(remove(ret_str.begin(), ret_str.end(), '\r'), ret_str.end()); 
  return ret_str;
}

// --------------------------------------
string unix_C_05_bin__mmap ()
{
string ret_str;
off_t rc;
  rc = lseek(fd_bin_in, 0, SEEK_SET);
  ASSERT (rc != -1);

  // ------ Body ------
char* ptr = (char*)mmap(0, infile_size_bin, PROT_READ, MAP_SHARED, fd_bin_in, 0);
  ASSERT (ptr != MAP_FAILED);

  ret_str = string(ptr, ptr + infile_size_bin);
  rc = munmap(ptr, infile_size_bin);
  ASSERT (rc == 0);
  // ------------------

  return ret_str;
}

#endif

// --------------------------------------
string CPP_01_txt__operators_in_out ()
{
string ret_str;
char ch;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in.unsetf(ios::skipws);
  while (fs_txt_in >> ch) 
  {
    ret_str += ch;  
  }
  // ------------------

  return ret_str;

}

// --------------------------------------
string CPP_01_bin__operators_in_out ()
{
string ret_str;
char   ch;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in.unsetf(ios::skipws);
  while (fs_bin_in >> ch) 
  {
    ret_str += ch;  
  }
  // ------------------

  return ret_str;

}


// --------------------------------------
string CPP_02_txt__methods_sbumpc_sputc ()
{
string ret_str;
char ch;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while ((ch = fs_txt_in.rdbuf()->sbumpc()) != EOF) 
  {
    ret_str += ch;  
  }
  // ------------------
  return ret_str;

}

// --------------------------------------
string CPP_02_bin__methods_sbumpc_sputc ()
{
string ret_str;
char ch;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while ((ch = fs_bin_in.rdbuf()->sbumpc()) != EOF) 
  {
    ret_str += ch;  
  }
  // ------------------
  return ret_str;

}

// --------------------------------------
string CPP_03_txt__method_sbumpc__op_out ()
{
ostringstream oss;
char ch;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  ch = fs_txt_in.rdbuf()->sbumpc();
  oss << ch;  

  while (ch != EOF)
  {
    oss << fs_txt_in.rdbuf();
    ch = fs_txt_in.rdbuf()->sbumpc();
  }
  return oss.str();
}

// --------------------------------------
string CPP_03_bin__method_sbumpc__op_out ()
{
ostringstream oss;
char ch;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  ch = fs_bin_in.rdbuf()->sbumpc();
  oss << ch;

  while (ch != EOF)
  {
    oss << fs_bin_in.rdbuf();
    ch = fs_bin_in.rdbuf()->sbumpc();
  }
  // ------------------

  return oss.str();
}

// --------------------------------------
string CPP_04_txt__method_rdbuf__op_out ()
{
ostringstream oss;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  oss << fs_txt_in.rdbuf();
  // ------------------

  return oss.str();
}

// --------------------------------------
string CPP_04_bin__method_rdbuf__op_out ()
{
ostringstream oss;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  oss << fs_bin_in.rdbuf();
  // ------------------

  return oss.str();

}



// --------------------------------------
string CPP_05_txt__methods_cpp_read_write__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (!fs_txt_in.eof())
  {
    fs_txt_in.read (cbuffer, sizeof(cbuffer) - 1);
    oss.write (cbuffer, fs_txt_in.gcount());
  }
  // ------------------

  return oss.str();

}

// --------------------------------------
string CPP_05_bin__methods_cpp_read_write__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (!fs_bin_in.eof())
  {
    fs_bin_in.read (cbuffer, sizeof(cbuffer) - 1);
    oss.write (cbuffer, fs_bin_in.gcount());
  }
  // ------------------
  return oss.str();
}



// --------------------------------------
string CPP_06_txt__methods_cpp_read_write_oss__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (!fs_txt_in.eof())
  {
    fs_txt_in.read (cbuffer, sizeof(cbuffer) - 1);
    oss.write (cbuffer, fs_txt_in.gcount());
  }
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_06_bin__methods_cpp_read_write_oss__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (!fs_bin_in.eof())
  {
    fs_bin_in.read (cbuffer, sizeof(cbuffer) - 1);
    oss.write (cbuffer, fs_bin_in.gcount());
  }
  // ------------------
  return oss.str();

}


// --------------------------------------
string CPP_07_txt__methods_cpp_readsome_write__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];
streamsize len;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  do
  { 
    len = fs_txt_in.readsome (cbuffer, sizeof(cbuffer) - 1); 
    oss.write (cbuffer, len);
  } while (len); 
  // ------------------
  return oss.str();
}

// --------------------------------------
string CPP_07_bin__methods_cpp_readsome_write__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];
streamsize len;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  do
  { 
    len = fs_bin_in.readsome (cbuffer, sizeof(cbuffer) - 1); 
    oss.write (cbuffer, len);
  } while (len); 
  // ------------------
  return oss.str();

}


// --------------------------------------
string CPP_08_txt__methods_cpp_read_write__max_buf ()
{
ostringstream oss;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (!fs_txt_in.eof())
  {
    fs_txt_in.read (mbuffer, infile_size2_txt);
    oss.write (mbuffer, fs_txt_in.gcount());
  }
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_08_bin__methods_cpp_read_write__max_buf ()
{
ostringstream oss;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (!fs_bin_in.eof())
  {
    fs_bin_in.read (mbuffer, infile_size_bin);
    oss.write (mbuffer, fs_bin_in.gcount());
  }
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_09_txt__function_getline__ostringstream ()
{
ostringstream oss;
string line;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (getline (fs_txt_in, line)) 
  {
    oss << line << '\n';
  }

string str(oss.str());
  if (!str.empty())
  {
    fs_txt_in.rdbuf()->sungetc ();
    if (fs_txt_in.rdbuf()->sgetc() != '\n') str.erase(str.size() - 1);
  }
  // ------------------
  return str;

}


// --------------------------------------
string CPP_09_bin__function_getline__ostringstream ()
{
ostringstream oss;
string line;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (getline (fs_bin_in, line)) oss << line << '\n';

string str(oss.str());
  if (!str.empty())
  {
    fs_bin_in.rdbuf()->sungetc ();
    if (fs_bin_in.rdbuf()->sgetc() != '\n') str.erase(str.size() - 1);
  }
  // ------------------
  return str;

}


// --------------------------------------
string CPP_10_txt__function_getline__string ()
{
string ret_str;
string line;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (getline (fs_txt_in, line)) 
  {
    ret_str +=line;
    ret_str +='\n';
  }

  if (!ret_str.empty())
  {
    fs_txt_in.rdbuf()->sungetc ();
    if (fs_txt_in.rdbuf()->sgetc() != '\n') ret_str.erase(ret_str.size() - 1);
  }
  // ------------------
  return ret_str;

}


// --------------------------------------
string CPP_10_bin__function_getline__string ()
{
string ret_str;
string line;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (getline (fs_bin_in, line))
  {
    ret_str +=line;
    ret_str +='\n';
  }

  if (!ret_str.empty())
  {
    fs_bin_in.rdbuf()->sungetc ();
    if (fs_bin_in.rdbuf()->sgetc() != '\n') ret_str.erase(ret_str.size() - 1);
  }
  // ------------------
  return ret_str;

}


// --------------------------------------
string CPP_11_txt__method_ifstream_getline ()
{
ostringstream oss;
char buffer[BUFFER_SIZE];
  ASSERT (sizeof(buffer) > 1);

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (fs_txt_in.getline (buffer, sizeof(buffer)).gcount())
  {
    oss << buffer;

    if (fs_txt_in.fail()) fs_txt_in.clear (~(ios_base::failbit | ~fs_txt_in.rdstate ())); 
    else                  oss << '\n';
  }
  ASSERT (fs_txt_in.eof());

string str(oss.str());
  if (!str.empty())
  {
    fs_txt_in.rdbuf()->sungetc ();
    if (fs_txt_in.rdbuf()->sgetc() != '\n') str.erase(str.size() - 1);
  }
  // ------------------
  return str;
}

// --------------------------------------
string CPP_11_bin__method_ifstream_getline ()
{
ostringstream oss;
char buffer[BUFFER_SIZE];
  ASSERT (sizeof(buffer) > 1);

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (fs_bin_in.getline (buffer, sizeof(buffer)).gcount())
  {
    oss << buffer;

    if (fs_bin_in.fail()) fs_bin_in.clear (~(ios_base::failbit | ~fs_bin_in.rdstate ())); 
    else                  oss << '\n';
  }
  ASSERT (fs_bin_in.eof());

string str(oss.str());
  if (!str.empty())
  {
    fs_bin_in.rdbuf()->sungetc ();
    if (fs_bin_in.rdbuf()->sgetc() != '\n') str.erase(str.size() - 1);
  }
  // ------------------
  return str;
}


// --------------------------------------
string CPP_12_txt__methods_ifstream_get_put ()
{
ostringstream oss;
char ch;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  while (fs_txt_in.get(ch)) oss.put(ch);
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_12_bin__methods_ifstream_get_put ()
{
ostringstream oss;
char ch;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  while (fs_bin_in.get(ch)) oss.put(ch);
  // ------------------
  return oss.str();
}



// --------------------------------------
string CPP_13_txt__method_ifstream_get ()
{
string ret_str (infile_size1_txt + 1, '0');
char ch;
ulong i;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  for (i = 0; fs_txt_in.get (ch); i++)
  {
    ret_str[i] = ch;
  }
  ret_str.erase (i);
  // ------------------
  return ret_str;
}

// --------------------------------------
string CPP_13_bin__method_ifstream_get ()
{
string ret_str (infile_size_bin + 1, '0');
char ch;
ulong i;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  for (i = 0; fs_bin_in.get (ch); i++)
  {
    ret_str[i] = ch;
  }
  ret_str.erase (i);
  // ------------------
  return ret_str;
}


// --------------------------------------
string CPP_14_txt__method_ifstream_get__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];

  ASSERT (sizeof(cbuffer) > 1);

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
const int newline_int_symbol (int ('\n'));
  while (fs_txt_in.get (cbuffer, sizeof(cbuffer) - 1))
  {
    oss << cbuffer;
    if (fs_txt_in.peek() == newline_int_symbol)
    {
      oss << char(fs_txt_in.get());
    }
  }
  // ------------------
  return oss.str();
}

// --------------------------------------
string CPP_14_bin__method_ifstream_get__const_buf ()
{
ostringstream oss;
char cbuffer[BUFFER_SIZE];

  ASSERT (sizeof(cbuffer) > 1);

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
const int newline_int_symbol (int ('\n'));
  while (fs_bin_in.get (cbuffer, sizeof(cbuffer) - 1))
  {
    oss << cbuffer;
    if (fs_bin_in.peek() == newline_int_symbol)
    {
      oss << char(fs_bin_in.get());
    }
  }
  // ------------------
  return oss.str();
}


// --------------------------------------
string CPP_15_txt__method_ifstream_get__streambuf ()
{
ostringstream oss;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
const int newline_int_symbol (int ('\n'));
  while (fs_txt_in.get (*oss.rdbuf()))
  {
    if (fs_txt_in.peek() == newline_int_symbol)
    {
      oss << char(fs_txt_in.get());
    }
  }
  // ------------------
  return oss.str();
}

// --------------------------------------
string CPP_15_bin__method_ifstream_get__streambuf ()
{
ostringstream oss;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
const int newline_int_symbol (int ('\n'));
  while (fs_bin_in.get (*oss.rdbuf()))
  {
    if (fs_bin_in.peek() == newline_int_symbol)
    {
      oss << char(fs_bin_in.get());
    }
  }
  // ------------------
  return oss.str();
}


// --------------------------------------
string CPP_16_txt__iostream_iterators__copy ()
{
ostringstream oss;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
istream_iterator<char> in(fs_txt_in), eos;
ostream_iterator<char> out(oss);
  copy (in, eos, out);
  // ------------------
  return oss.str();
}

// --------------------------------------
string CPP_16_bin__iostream_iterators__copy ()
{
ostringstream oss;
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
istream_iterator<char> in(fs_bin_in), eos;
ostream_iterator<char> out(oss);
  copy (in, eos, out);
  // ------------------
  return oss.str();
}



// --------------------------------------
string CPP_17_txt__iostream_iterators__string ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
  istream_iterator<char> in(fs_txt_in), eos;
  // ------------------
  return string(in, eos);
}

// --------------------------------------
string CPP_17_bin__iostream_iterators__string ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
  istream_iterator<char> in(fs_bin_in), eos;
  // ------------------
  return string(in, eos);
}


// --------------------------------------
string CPP_18_txt__iostreambuf_iterators__copy ()
{
ostringstream oss;
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
istreambuf_iterator<char> in(fs_txt_in), eos;
ostreambuf_iterator<char> out(oss);
  copy (in, eos, out);
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_18_bin__iostreambuf_iterators__copy ()
{
ostringstream oss;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
istreambuf_iterator<char> in(fs_bin_in), eos;
ostreambuf_iterator<char> out(oss);
  copy (in, eos, out);
  // ------------------
  return oss.str();

}

// --------------------------------------
struct char_identity
{
  char operator()(char ch) const { return ch; }
};

// --------------------------------------
string CPP_19_txt__iostreambuf_iterators__transform ()
{
ostringstream oss;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
istreambuf_iterator<char> in(fs_txt_in), eos;
ostreambuf_iterator<char> out(oss);
  transform(in, eos, out, char_identity());
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_19_bin__iostreambuf_iterators__transform ()
{
ostringstream oss;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
istreambuf_iterator<char> in(fs_bin_in), eos;
ostreambuf_iterator<char> out(oss);
  transform(in, eos, out, char_identity());
  // ------------------
  return oss.str();
}


// --------------------------------------
string CPP_20_txt__iostreambuf_iterators__string ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
  istreambuf_iterator<char> in(fs_txt_in), eos;
  // ------------------
  return string(in, eos);
}

// --------------------------------------
string CPP_20_bin__iostreambuf_iterators__string ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
  istreambuf_iterator<char> in(fs_bin_in), eos;
  // ------------------
  return string(in, eos);
}


// --------------------------------------
string CPP_21_txt__vector__copy ()
{
ostringstream oss;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
vector<char> v (infile_size1_txt);
  fs_txt_in.read(&v[0], infile_size1_txt);

ostream_iterator<char> out(oss);
  copy (&v[0], &v[v.size()], out);
  // ------------------
  return oss.str();

}

// --------------------------------------
string CPP_21_bin__vector__copy ()
{
ostringstream oss;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
vector<char> v (infile_size_bin);
  fs_bin_in.read(&v[0], infile_size_bin);

ostream_iterator<char> out(oss);
  copy (&v[0], &v[v.size()], out);
  // ------------------
  return oss.str();

}



// --------------------------------------
string CPP_22_txt__vector_push_back ()
{
string ret_str;
vector<char> v;
char ch;

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  v.reserve(infile_size1_txt);
  // ------ Body ------
  while (fs_txt_in.get(ch)) v.push_back(ch);
  // ------------------
  ret_str = v.empty() ? string() : string (v.begin(), v.end());

  return ret_str;

}

// --------------------------------------
string CPP_22_bin__vector_push_back ()
{
string ret_str;
vector<char> v;
char ch;

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  v.reserve(infile_size_bin);
  // ------ Body ------
  while (fs_bin_in.get(ch)) v.push_back(ch);
  // ------------------
  ret_str = v.empty() ? string() : string (v.begin(), v.end());

  return ret_str;

}


// --------------------------------------
string CPP_23_txt__vector__cpp_read ()
{
string ret_str;
vector<char> v (infile_size1_txt);

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in.read(&v[0], infile_size1_txt);
  // ------------------
  ret_str = v.empty() ? string() : string (v.begin(), v.end());

  return ret_str;

}

// --------------------------------------
string CPP_23_bin__vector__cpp_read ()
{
string ret_str;
vector<char> v (infile_size_bin);

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in.read(&v[0], infile_size_bin);
  // ------------------
  ret_str = v.empty() ? string() : string (v.begin(), v.end());

  return ret_str;

}



// --------------------------------------
string CPP_24_txt__string__cpp_read ()
{
string ret_str (infile_size1_txt, '0');

  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_txt_in.read(&ret_str[0], infile_size1_txt);
  // ------------------

  return ret_str;

}

// --------------------------------------
string CPP_24_bin__string__cpp_read ()
{
string ret_str (infile_size_bin, '0');

  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  // ------ Body ------
  fs_bin_in.read(&ret_str[0], infile_size_bin);
  // ------------------

  return ret_str;

}




// =====================================
// ====== Part-6: Computation Management
// =====================================

// -----------------------------------
// -----------------------------------
// -----------------------------------
#define MEASURE_IT(x, y) \
  ASSERT (ret_string.empty()); \
  foo_setw = MAX_VALUE (foo_setw, string (#y).size()); \
  cerr << "[Run-" << run_no << "] Test-" << test_no << ": " << #y << endl; \
  start_time = clock(); \
  ASSERT (start_time != clock_t (-1)); \
  { for (ulong k = 0; k < no_of_repetitions; k++) { ret_string = x; } } \
  end_time = clock(); \
  ASSERT (end_time != clock_t (-1)); \
  ASSERT (ret_string.size() == infile_content.size()); \
  ASSERT (ret_string == infile_content); \
  if (!(end_time > start_time)) { cerr << "Number of repetitions too small: " << #y << endl; }\
  ASSERT (end_time >= start_time); \
  if (find (foo_names.begin(), foo_names.end(), #y) == foo_names.end()) \
  { \
    foo_names.push_back (#y); \
    used_time.push_back (vector<clock_t>()); \
  } \
  ASSERT (foo_names.size() == used_time.size()); \
  iter_names = find (foo_names.begin(), foo_names.end(), #y); \
  ASSERT (iter_names != foo_names.end()); \
  used_time[distance (foo_names.begin(), iter_names)].push_back ((end_time - start_time))

#define MEASURE_WITH_ARG(foo, argument) MEASURE_IT (foo(argument), foo)
#define MEASURE_WITH_NO_ARG(foo) MEASURE_IT (foo(), foo)

// ------------
void measure (ulong no_of_repetitions)
{
clock_t start_time;
clock_t end_time;
vector<clock_t> elapsed_time_vect;

  // -------------------------------
  fill_input_file ();

  ASSERT (infile_size2_txt);
  ASSERT (infile_size_bin);

const ulong infile_max_txt_bin_size (MAX_VALUE (infile_size2_txt, infile_size_bin));
  ASSERT (user_defined_filesize <= infile_max_txt_bin_size);

  mbuffer = new (nothrow) char [infile_max_txt_bin_size + 2];

  ASSERT (mbuffer != NULL);


  // -------------------------------
  fp_txt_in  = fopen(INPUT_TXT_FILE_NAME, "r");
  ASSERT(fp_txt_in);

  fp_bin_in  = fopen(INPUT_BIN_FILE_NAME, "rb");
  ASSERT(fp_bin_in);

  if (!(infile_size2_txt == get_filesize_via_fseek_ftell (INPUT_TXT_FILE_NAME, true)))
  {
    cerr << endl;
    cerr << "User defined file size    = " << user_defined_filesize << endl;
    cerr << "Txt input file size       = " << infile_size2_txt << endl;
    cerr << "Via fseek&ftell file size = " << get_filesize_via_fseek_ftell (INPUT_TXT_FILE_NAME, true) << endl;
    ASSERT (infile_size2_txt == get_filesize_via_fseek_ftell (INPUT_TXT_FILE_NAME, true));
  }
  ASSERT (infile_size2_txt == get_filesize_via_fseek_ftell (INPUT_TXT_FILE_NAME, true));

  ASSERT (infile_size_bin == get_filesize_via_fseek_ftell (INPUT_BIN_FILE_NAME, false));


#ifdef UNIX_ENV
  fd_txt_in = open(INPUT_TXT_FILE_NAME, O_CREAT | O_RDONLY);
  ASSERT (fd_txt_in != -1);

  fd_bin_in = open(INPUT_BIN_FILE_NAME, O_CREAT | O_RDONLY | O_BINARY);
  ASSERT (fd_bin_in != -1);

  ASSERT (infile_size2_txt == get_filesize_via_lseek (INPUT_TXT_FILE_NAME, true));
  ASSERT (infile_size_bin == get_filesize_via_lseek (INPUT_BIN_FILE_NAME, false));

#endif

  fs_txt_in.open (INPUT_TXT_FILE_NAME);
  ASSERT (fs_txt_in);
  ASSERT (fs_txt_in.is_open());
  
  fs_bin_in.open (INPUT_BIN_FILE_NAME, ios::binary);
  ASSERT (fs_bin_in);
  ASSERT (fs_bin_in.is_open());

  ASSERT (infile_size2_txt == get_filesize_via_seekg_tellg (INPUT_TXT_FILE_NAME, true));
  ASSERT (infile_size_bin == get_filesize_via_seekg_tellg (INPUT_BIN_FILE_NAME, false));


  // -------------------------------
  // cout << "\t---> Test-" << test_no << " started" << endl;
  cout << ".";
  cout.flush();

  // -------------------------------


  MEASURE_WITH_NO_ARG (C_01_txt__functions_getc_putc);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (C_01_bin__functions_getc_putc);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (C_02_txt__functions_fgetc_fputc);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (C_02_bin__functions_fgetc_fputc);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (C_03_txt__functions_fread_fwrite__const_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (C_03_bin__functions_fread_fwrite__const_buf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (C_04_txt__functions_fread_fwrite__max_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (C_04_bin__functions_fread_fwrite__max_buf);
  CHECK_BIN_RETURNED_STRING;


#ifdef UNIX_ENV

  MEASURE_WITH_NO_ARG (unix_C_05_txt__mmap);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (unix_C_05_bin__mmap);
  CHECK_BIN_RETURNED_STRING;

#endif

  MEASURE_WITH_NO_ARG (CPP_01_txt__operators_in_out);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_01_bin__operators_in_out);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_02_txt__methods_sbumpc_sputc);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_02_bin__methods_sbumpc_sputc);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_03_txt__method_sbumpc__op_out);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_03_bin__method_sbumpc__op_out);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_04_txt__method_rdbuf__op_out);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_04_bin__method_rdbuf__op_out);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_05_txt__methods_cpp_read_write__const_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_05_bin__methods_cpp_read_write__const_buf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_06_txt__methods_cpp_read_write_oss__const_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_06_bin__methods_cpp_read_write_oss__const_buf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_07_txt__methods_cpp_readsome_write__const_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_07_bin__methods_cpp_readsome_write__const_buf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_08_txt__methods_cpp_read_write__max_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_08_bin__methods_cpp_read_write__max_buf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_09_txt__function_getline__ostringstream);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_09_bin__function_getline__ostringstream);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_10_txt__function_getline__string);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_10_bin__function_getline__string);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_11_txt__method_ifstream_getline);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_11_bin__method_ifstream_getline);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_12_txt__methods_ifstream_get_put);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_12_bin__methods_ifstream_get_put);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_13_txt__method_ifstream_get);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_13_bin__method_ifstream_get);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_14_txt__method_ifstream_get__const_buf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_14_bin__method_ifstream_get__const_buf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_15_txt__method_ifstream_get__streambuf);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_15_bin__method_ifstream_get__streambuf);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_16_txt__iostream_iterators__copy);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_16_bin__iostream_iterators__copy);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_17_txt__iostream_iterators__string);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_17_bin__iostream_iterators__string);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_18_txt__iostreambuf_iterators__copy);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_18_bin__iostreambuf_iterators__copy);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_19_txt__iostreambuf_iterators__transform);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_19_bin__iostreambuf_iterators__transform);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_20_txt__iostreambuf_iterators__string);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_20_bin__iostreambuf_iterators__string);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_21_txt__vector__copy);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_21_bin__vector__copy);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_22_txt__vector_push_back);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_22_bin__vector_push_back);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_23_txt__vector__cpp_read);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_23_bin__vector__cpp_read);
  CHECK_BIN_RETURNED_STRING;

  MEASURE_WITH_NO_ARG (CPP_24_txt__string__cpp_read);
  CHECK_TXT_RETURNED_STRING; 
  MEASURE_WITH_NO_ARG (CPP_24_bin__string__cpp_read);
  CHECK_BIN_RETURNED_STRING;


  // -------------------------------
  // cerr << "\t     Test-" << test_no << " finished" << endl;

  // -------------------------------

  ASSERT (mbuffer != NULL);
  delete[] mbuffer;
  mbuffer = NULL;

  fs_txt_in.clear();
  fs_txt_in.close();
  ASSERT (!fs_txt_in.is_open());

  fs_bin_in.clear();
  fs_bin_in.close();
  ASSERT (!fs_bin_in.is_open());


int rc;

#ifdef UNIX_ENV

  rc = close (fd_txt_in);
  ASSERT (rc == 0);

  rc = close (fd_bin_in);
  ASSERT (rc == 0);

#endif

  clearerr(fp_txt_in);
  rc = fclose(fp_txt_in);
  ASSERT (rc == 0);

  clearerr(fp_bin_in);
  rc = fclose(fp_bin_in);
  ASSERT (rc == 0);



}

// ------------
void show (ulong no_of_tests)
{
clock_t units;
clock_t sum;
#define THRESHOLD  0.2
const ulong   threshold = ulong(no_of_tests * THRESHOLD);

  ASSERT ((threshold * 2) <= no_of_tests);

  cout << endl;
  ASSERT (foo_names.size() == used_time.size());

  for (ulong i = 0; i < foo_names.size(); i++)
  {
    sum = 0;
    ASSERT (no_of_tests == used_time[i].size());
    for (ulong k = threshold; k < (used_time[i].size() - threshold); k++)
    {
      sum += used_time[i][k];
    }
    units = sum/(used_time[i].size() - (threshold * 2));

    cout << setw(foo_setw)
         << std::left
         << foo_names[i]
         << " : "
         << setw(6)
         << std::right
         << units
         << " units"
         << " (";
    cout.setf(ios::fixed, ios::floatfield);
    cout << setprecision (3)
         << (float(units)/float(CLOCKS_PER_SEC))
         << " secs)"
         << endl;
  }

}

// ------------
void run (ulong no_of_runs, ulong no_of_tests, ulong no_of_repetitions)
{
  for (ulong i = 0; i < no_of_runs; i++)
  {
    run_no = i + 1;
    foo_names.clear();
    used_time.clear();

    // ----------------------
    cout << endl << endl << "   Run-" << (i + 1) << " of " << no_of_runs << " : Started ";
    cout.flush();
    for (ulong k = 0; k < no_of_tests; k++) 
    {
      test_no = k + 1;
      measure (no_of_repetitions);
    }
    show (no_of_tests);
    cout << "   Run-" << (i + 1) << " of " << no_of_runs << " : Finished"<< endl << endl;
  }
}


// ------------
int main(int argc, char** argv)
{

  cout << endl;
  cout << string (string (PROGRAM_NAME).size(), '=') << endl;
  cout << PROGRAM_NAME << endl;
  cout << PROGRAM_VERSION << endl;
  cout << string (string (PROGRAM_NAME).size(), '=') << endl;
  // --------------------------

  cout << endl;
  cout << endl;
  show_compiler_info();

  cout << endl;
  cout << "\tYOUR COMMAND LINE : ";
string exe_name (argv[0]);
  cout << exe_name.substr (exe_name.find_last_of ("/\\") + 1) << " ";
  for (long i = 1; i < argc; i++) cout << argv[i] << " ";
  cout << endl;
  cout << endl;

  if (!(argc >= 4))
  {
    cout << "\tUSAGE : "
         << argv[0]
         << " "
         << "<File size> <No. of tests> <No. of repetitions> [<No. of runs>]"
         << endl;
    return 1;
  }
  ASSERT (argc >= 4);

  user_defined_filesize = atoi (argv[1]);
  ASSERT (user_defined_filesize > 0);

const ulong no_of_tests (atoi (argv[2]));
  ASSERT (no_of_tests > 0);

const ulong no_of_repetitions (atoi (argv[3]));
  ASSERT (no_of_repetitions > 0);

const ulong no_of_runs (((argc > 4) ? atoi (argv[4]) : 1));
  ASSERT (no_of_runs > 0);

  cout << "\t### File size             : " << user_defined_filesize << endl;
  cout << "\t### Number of runs        : " << no_of_runs << endl;
  cout << "\t### Number of tests       : " << no_of_tests << endl;
  cout << "\t### Number of repetitions : " << no_of_repetitions << endl;
  cout << "\t### CLOCKS_PER_SEC        : " << CLOCKS_PER_SEC << endl;
  cout << endl;

  // -----------------------------
  run (no_of_runs, no_of_tests, no_of_repetitions);

  return 0;

}

Gmane