Alex Vinokur | 14 Feb 12:56

[Source] Simple C/C++ Perfometer: Copying Files (Versions 3.x)

// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
#define PROGRAM_NAME     "Simple C/C++ Perfometer : Copying files"
#define PROGRAM_VERSION  "Version CF-3.0"
// -------------------------------------
// Copyright (C) 2002-2005 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(), ofstream::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    : istream::getline, std::ostringstream, ostream::operator<<
//    CPP-11    : istream::get(char) and ostream::put
//    CPP-12    : istream::get(char*, streamsize) , ostream::operator<< - with const size buffer
//    CPP-13    : istream::get(streambuf&)  and std::streambuf, ostream::operator<<
//    CPP-14    : std::istream_iterator, std::ostream_iterator and std::copy
//    CPP-15    : std::istreambuf_iterator, std::ostreambuf_iterator and std::copy
//    CPP-16    : std::istreambuf_iterator, std::ostreambuf_iterator and std::transform
//    CPP-17    : std::vector and std::copy
//
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



// --------------------------------------
#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))


// ========================================
// ====== 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_purified_content;
static string infile_content_bin;

static uint   foo_setw = 0;

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

#define OPEN_TXT_FP_OUT open_txt_fp_out (__LINE__)
#define OPEN_BIN_FP_OUT open_bin_fp_out (__LINE__)
#define OPEN_TXT_FD_OUT open_txt_fd_out (__LINE__)
#define OPEN_BIN_FD_OUT open_bin_fd_out (__LINE__)
#define OPEN_TXT_FS_OUT open_txt_fs_out (__LINE__)
#define OPEN_BIN_FS_OUT open_bin_fs_out (__LINE__)


#define CLOSE_TXT_FP_OUT close_txt_fp_out (__LINE__)
#define CLOSE_BIN_FP_OUT close_bin_fp_out (__LINE__)
#define CLOSE_TXT_FD_OUT close_txt_fd_out (__LINE__)
#define CLOSE_BIN_FD_OUT close_bin_fd_out (__LINE__)
#define CLOSE_TXT_FS_OUT close_txt_fs_out (__LINE__)
#define CLOSE_BIN_FS_OUT close_bin_fs_out (__LINE__)


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


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

#define ROWS_IN_INPUT_FILE 7

static FILE* fp_txt_in;
static FILE* fp_txt_out;
static FILE* fp_bin_in;
static FILE* fp_bin_out;

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

static ifstream fs_txt_in;
static ofstream fs_txt_out;
static ifstream fs_bin_in;
static ofstream fs_bin_out;

#define BUFFER_SIZE  4096

static char* mbuffer = NULL;




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

// -----------------------------
// Function 3.01
void aux_check_assert_out (
 const char * const filename_i,
 const bool         is_txt_mode_i,
 const int          lineno_i
 )
// -----------------------------
{
const string outfile_content (file_to_string(filename_i, is_txt_mode_i));

const string infile_content (is_txt_mode_i ? infile_content_txt : infile_content_bin);

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

  SMART_ASSERT ((infile_content.size() == outfile_content.size()));
  SMART_ASSERT ((infile_content == outfile_content));
}

// -------------- Level-1 -----------------
// ----------------------------------------

// -----------------------------
// Function 3.02
void aux_open_fp_out (
 const char * const filename_i,
 FILE*&       fp_o,
 const char * const mode_i,
 const int    lineno_i
 )
// -----------------------------
{
  TRACE("\n" << __FUNCTION__ << " : Start  -> " << filename_i);

const int rc = remove (filename_i);
  SMART_ASSERT (!(rc == 0));

  fp_o = fopen(filename_i, mode_i);
  SMART_ASSERT(fp_o);

  TRACE(__FUNCTION__ << " : Finish -> " << filename_i);
}

// --------------------------------
// Function 3.03
void aux_close_fp_out (
 const char * const filename_i,
 FILE*&             fp_o,
 const bool         is_txt_mode_i,
 const int          lineno_i
 )
// --------------------------------
{
  TRACE(__FUNCTION__ << " : Start  -> " << filename_i);

  aux_check_assert_out (filename_i, is_txt_mode_i, lineno_i);

int rc;

  clearerr(fp_o);
  rc = fclose(fp_o);
  SMART_ASSERT (rc == 0);

  rc = remove (filename_i);
  SMART_ASSERT (rc == 0);

  TRACE(__FUNCTION__ << " : Finish -> " << filename_i);

}


#ifdef UNIX_ENV
// --------------------------------
// Function 3.04
void aux_open_fd_out (
 const char * const filename_i,
 int&               fd_out_o,
 const int          fd_in_i,
 const bool         is_txt_mode_i,
 const int          lineno_i
 )
// --------------------------------
{
  TRACE("\n" << __FUNCTION__ << " : Start  -> " << filename_i << ", fd = " << fd_out_o << ", mode = " << is_txt_mode_i);

const int rc = remove (filename_i);
  SMART_ASSERT (!(rc == 0));

  fd_out_o = open(filename_i, is_txt_mode_i ? O_CREAT | O_WRONLY : O_CREAT | O_WRONLY | O_BINARY);

  SMART_ASSERT (fd_out_o != -1);
  SMART_ASSERT (fd_in_i != fd_out_o);

  TRACE(__FUNCTION__ << " : Finish -> " << filename_i << ", fd = " << fd_out_o);
}


// --------------------------------
// Function 3.05
void aux_close_fd_out (
 const char * const filename_i,
 int&               fd_o,
 const bool         is_txt_mode_i,
 const int          lineno_i
 )
// --------------------------------
{
  TRACE(__FUNCTION__ << " : Start  -> " << filename_i << ", fd = " << fd_o << ", mode = " << is_txt_mode_i);

  aux_check_assert_out (filename_i, is_txt_mode_i, lineno_i);

int rc;

  rc = close(fd_o);
  SMART_ASSERT(rc == 0);

  rc = remove (filename_i);
  SMART_ASSERT (rc == 0);

  TRACE(__FUNCTION__ << " : Finish -> " << filename_i << ", fd = " << fd_o);
}
#endif


// --------------------------------
// Function 3.06
void aux_open_fs_out (
 const char * const filename_i,
 ofstream&    fs_o,
 const bool   is_txt_mode_i,
 const int    lineno_i
 )
// --------------------------------
{
  TRACE("\n" << __FUNCTION__ << " : Start  -> " << filename_i);

const int rc = remove (filename_i);
  SMART_ASSERT (!(rc == 0));

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

  SMART_ASSERT (fs_o);
  SMART_ASSERT (fs_o.is_open());

  TRACE(__FUNCTION__ << " : Finish -> " << filename_i);
}



// --------------------------------
// Function 3.07
void aux_close_fs_out (
 const char * const filename_i,
 ofstream&          fs_o,
 const bool         is_txt_mode_i,
 const int          lineno_i
 )
// --------------------------------
{
  TRACE(__FUNCTION__ << " : Start  -> " << filename_i);

  fs_o.clear();
  fs_o.seekp (0, ios::beg);

  aux_check_assert_out (filename_i, is_txt_mode_i, lineno_i);

  fs_o.clear();
  fs_o.close();
  SMART_ASSERT (!fs_o.is_open());

const int rc = remove (filename_i);
  SMART_ASSERT (rc == 0);

  TRACE(__FUNCTION__ << " : Finish -> " << filename_i);
}


// -------------- Level-2 -----------------
// ----------------------------------------

// --------------------------------------
// Function 3.08
void open_txt_fp_out (const int lineno_i)
// --------------------------------------
{
  aux_open_fp_out (OUTPUT_TXT_FILE_NAME, fp_txt_out, "w", lineno_i);
}

// --------------------------------------
// Function 3.09
void open_bin_fp_out (const int lineno_i)
// --------------------------------------
{
  aux_open_fp_out (OUTPUT_BIN_FILE_NAME, fp_bin_out, "wb", lineno_i);
}


// ---------------------------------------
// Function 3.10
void close_txt_fp_out (const int lineno_i)
// ---------------------------------------
{
  aux_close_fp_out (OUTPUT_TXT_FILE_NAME, fp_txt_out, true, lineno_i);
}

// ---------------------------------------
// Function 3.11
void close_bin_fp_out (const int lineno_i)
// ---------------------------------------
{
  aux_close_fp_out (OUTPUT_BIN_FILE_NAME, fp_bin_out, false, lineno_i);
}


#ifdef UNIX_ENV
// --------------------------------------
// Function 3.12
void open_txt_fd_out (const int lineno_i)
// --------------------------------------
{
  aux_open_fd_out (OUTPUT_TXT_FILE_NAME, fd_txt_out, fd_txt_in, true, lineno_i);
}

// --------------------------------------
// Function 3.13
void open_bin_fd_out (const int lineno_i)
// --------------------------------------
{
  aux_open_fd_out (OUTPUT_BIN_FILE_NAME, fd_bin_out, fd_bin_in, false, lineno_i);
}

// ---------------------------------------
// Function 3.14
void close_txt_fd_out (const int lineno_i)
// ---------------------------------------
{
  aux_close_fd_out (OUTPUT_TXT_FILE_NAME, fd_txt_out, true, lineno_i);
}

// ---------------------------------------
// Function 3.15
void close_bin_fd_out (const int lineno_i)
// ---------------------------------------
{
  aux_close_fd_out (OUTPUT_BIN_FILE_NAME, fd_bin_out, false, lineno_i);
}


#endif


// --------------------------------------
// Function 3.16
void open_txt_fs_out (const int lineno_i)
// --------------------------------------
{
  aux_open_fs_out (OUTPUT_TXT_FILE_NAME, fs_txt_out, true, lineno_i);
}


// --------------------------------------
// Function 3.17
void open_bin_fs_out (const int lineno_i)
// --------------------------------------
{
  aux_open_fs_out (OUTPUT_BIN_FILE_NAME, fs_bin_out, false, lineno_i);
}


// ---------------------------------------
// Function 3.18
void close_txt_fs_out (const int lineno_i)
// ---------------------------------------
{
  aux_close_fs_out (OUTPUT_TXT_FILE_NAME, fs_txt_out, true, lineno_i);
}

// ---------------------------------------
// Function 3.19
void close_bin_fs_out (const int lineno_i)
// ---------------------------------------
{
  aux_close_fs_out (OUTPUT_BIN_FILE_NAME, fs_bin_out, false, lineno_i);
}




// ==========================================
// ====== 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
// ====================================

// --------------------------------------
void C_01_txt__functions_getc_putc ()
{
int      int_ch;

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((int_ch = getc(fp_txt_in)) != EOF)
  {
    putc(int_ch, fp_txt_out);
  }
  // ------------------

  clearerr(fp_txt_out);
  rewind (fp_txt_out);
}

// --------------------------------------
void C_01_bin__functions_getc_putc ()
{
int      int_ch;

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((int_ch = getc(fp_bin_in)) != EOF) putc(int_ch, fp_bin_out);
  // ------------------

  clearerr(fp_bin_out);
  rewind (fp_bin_out);
}

// --------------------------------------
void C_02_txt__functions_fgetc_fputc ()
{
int      int_ch;

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((int_ch = fgetc(fp_txt_in)) != EOF) fputc(int_ch, fp_txt_out);
  // ------------------

  clearerr(fp_txt_out);
  rewind (fp_txt_out);
}

// --------------------------------------
void C_02_bin__functions_fgetc_fputc ()
{
int      int_ch;

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((int_ch = fgetc(fp_bin_in)) != EOF) fputc(int_ch, fp_bin_out);
  // ------------------

  clearerr(fp_bin_out);
  rewind (fp_bin_out);
}

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

  clearerr(fp_txt_in);
  rewind (fp_txt_in);

  // ------ Body ------
  while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_txt_in)) > 0)
  {
    fwrite(cbuffer, sizeof(char), nread, fp_txt_out);
  }
  // ------------------

  clearerr(fp_txt_out);
  rewind (fp_txt_out);
}

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

  clearerr(fp_bin_in);
  rewind (fp_bin_in);

  // ------ Body ------
  while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_bin_in)) > 0)
  {
    fwrite(cbuffer, sizeof(char), nread, fp_bin_out);
  }
  // ------------------

  clearerr(fp_bin_out);
  rewind (fp_bin_out);
}



// --------------------------------------
void C_04_txt__functions_fread_fwrite__max_buf ()
{
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)
  {
    fwrite(mbuffer, sizeof(char), nread, fp_txt_out);
  }
  // ------------------

  clearerr(fp_txt_out);
  rewind (fp_txt_out);
}

// --------------------------------------
void C_04_bin__functions_fread_fwrite__max_buf ()
{
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)
  {
    fwrite(mbuffer, sizeof(char), nread, fp_bin_out);
  }
  // ------------------

  clearerr(fp_bin_out);
  rewind (fp_bin_out);
}

#ifdef UNIX_ENV
// --------------------------------------
void unix_C_05_txt__mmap ()
{
off_t rc;
  rc = lseek(fd_txt_in, 0, SEEK_SET);
  assert (rc != -1);
  rc = lseek(fd_txt_out, 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);
  write(fd_txt_out, ptr, infile_size2_txt);
  rc = munmap(ptr, infile_size2_txt);
  assert (rc == 0);
  // ------------------
}

// --------------------------------------
void unix_C_05_bin__mmap ()
{
off_t rc;
  rc = lseek(fd_bin_in, 0, SEEK_SET);
  assert (rc != -1);
  rc = lseek(fd_bin_out, 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);
  write(fd_bin_out, ptr, infile_size_bin);
  rc = munmap(ptr, infile_size_bin);
  assert (rc == 0);
  // ------------------
}

#endif

// --------------------------------------
void CPP_01_txt__operators_in_out ()
{
char ch;

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_txt_in.unsetf(ios::skipws);
  while (fs_txt_in >> ch) fs_txt_out << ch;
  // ------------------

}

// --------------------------------------
void CPP_01_bin__operators_in_out ()
{
char ch;

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_bin_in.unsetf(ios::skipws);
  while (fs_bin_in >> ch) fs_bin_out << ch;
  // ------------------

}


// --------------------------------------
void CPP_02_txt__methods_sbumpc_sputc ()
{
char ch;

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  while ((ch = fs_txt_in.rdbuf()->sbumpc()) != EOF) fs_txt_out.rdbuf()->sputc(ch);
  // ------------------

}

// --------------------------------------
void CPP_02_bin__methods_sbumpc_sputc ()
{
char ch;

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  while ((ch = fs_bin_in.rdbuf()->sbumpc()) != EOF) fs_bin_out.rdbuf()->sputc(ch);
  // ------------------

}

// --------------------------------------
void CPP_03_txt__method_sbumpc__op_out ()
{
char ch;

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

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

}

// --------------------------------------
void CPP_03_bin__method_sbumpc__op_out ()
{
char ch;

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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

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

}

// --------------------------------------
void CPP_04_txt__method_rdbuf__op_out ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

}

// --------------------------------------
void CPP_04_bin__method_rdbuf__op_out ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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

}



// --------------------------------------
void CPP_05_txt__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

}

// --------------------------------------
void CPP_05_bin__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  while (!fs_bin_in.eof())
  {
    fs_bin_in.read (cbuffer, sizeof(cbuffer));
    fs_bin_out.write (cbuffer, fs_bin_in.gcount());
  }
  // ------------------

}



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

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

}

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

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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

}


// --------------------------------------
void CPP_07_txt__methods_cpp_readsome_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];
streamsize len;

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  do
  {
    len = fs_txt_in.readsome (cbuffer, sizeof(cbuffer));
    fs_txt_out.write (cbuffer, len);
  } while (len);
  // ------------------
}

// --------------------------------------
void CPP_07_bin__methods_cpp_readsome_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];
streamsize len;

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  do
  {
    len = fs_bin_in.readsome (cbuffer, sizeof(cbuffer));
    fs_bin_out.write (cbuffer, len);
  } while (len);
  // ------------------

}


// --------------------------------------
void CPP_08_txt__methods_cpp_read_write__max_buf ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

}

// --------------------------------------
void CPP_08_bin__methods_cpp_read_write__max_buf ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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

}

// --------------------------------------
void CPP_09_txt__function_getline ()
{
string line;
ostringstream oss;

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
int ccc = 0;
  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);
  }
  fs_txt_out << str;
  // ------------------

}


// --------------------------------------
void CPP_09_bin__function_getline ()
{
string line;
ostringstream oss;

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

  fs_bin_out.clear();
  fs_bin_out.seekp (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);
  }
  fs_bin_out << str;
  // ------------------

}

// --------------------------------------
void CPP_10_txt__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];
ostringstream oss;
  assert (sizeof(buffer) > 1);

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

  fs_txt_out.clear();
  fs_txt_out.seekp (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);
  }
  fs_txt_out << str;
  // ------------------
}

// --------------------------------------
void CPP_10_bin__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];
ostringstream oss;
  assert (sizeof(buffer) > 1);

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

  fs_bin_out.clear();
  fs_bin_out.seekp (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);
  }
  fs_bin_out << str;
  // ------------------
}


// --------------------------------------
void CPP_11_txt__methods_ifstream_get_put ()
{
char ch;

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  while (fs_txt_in.get(ch)) fs_txt_out.put(ch);
  // ------------------

}

// --------------------------------------
void CPP_11_bin__methods_ifstream_get_put ()
{
char ch;

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  while (fs_bin_in.get(ch)) fs_bin_out.put(ch);
  // ------------------
}



// --------------------------------------
void CPP_12_txt__method_ifstream_get__const_buf ()
{
char cbuffer[BUFFER_SIZE];

  assert (sizeof(cbuffer) > 1);

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

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

// --------------------------------------
void CPP_12_bin__method_ifstream_get__const_buf ()
{
char cbuffer[BUFFER_SIZE];

  assert (sizeof(cbuffer) > 1);

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

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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


// --------------------------------------
void CPP_13_txt__method_ifstream_get__streambuf ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

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

// --------------------------------------
void CPP_13_bin__method_ifstream_get__streambuf ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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


// --------------------------------------
void CPP_14_txt__iostream_iterators__copy ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
istream_iterator<char> in(fs_txt_in), eos;
ostream_iterator<char> out(fs_txt_out);
  copy (in, eos, out);
  // ------------------
}

// --------------------------------------
void CPP_14_bin__iostream_iterators__copy ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
istream_iterator<char> in(fs_bin_in), eos;
ostream_iterator<char> out(fs_bin_out);
  copy (in, eos, out);
  // ------------------
}

// --------------------------------------
void CPP_15_txt__iostreambuf_iterators__copy ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
istreambuf_iterator<char> in(fs_txt_in), eos;
ostreambuf_iterator<char> out(fs_txt_out);
  copy (in, eos, out);
  // ------------------

}

// --------------------------------------
void CPP_15_bin__iostreambuf_iterators__copy ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_bin_in >> noskipws;
istreambuf_iterator<char> in(fs_bin_in), eos;
ostreambuf_iterator<char> out(fs_bin_out);
  copy (in, eos, out);
  // ------------------

}

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

// --------------------------------------
void CPP_16_txt__iostreambuf_iterators__transform ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();
  fs_txt_out.seekp (0, ios::beg);

  // ------ Body ------
  fs_txt_in >> noskipws;
istreambuf_iterator<char> in(fs_txt_in), eos;
ostreambuf_iterator<char> out(fs_txt_out);
  transform(in, eos, out, char_identity());
  // ------------------

}

// --------------------------------------
void CPP_16_bin__iostreambuf_iterators__transform ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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



// --------------------------------------
void CPP_17_txt__vector_copy ()
{
  fs_txt_in.clear();
  fs_txt_in.seekg (0, ios::beg);

  fs_txt_out.clear();;
  fs_txt_out.seekp (0, ios::beg);

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

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

}

// --------------------------------------
void CPP_17_bin__vector_copy ()
{
  fs_bin_in.clear();
  fs_bin_in.seekg (0, ios::beg);

  fs_bin_out.clear();
  fs_bin_out.seekp (0, ios::beg);

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

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

}


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

// -----------------------------------
// -----------------------------------
// -----------------------------------
#define MEASURE_IT(x, y) \
  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++) { x; } } \
  end_time = clock(); \
  assert (end_time != clock_t (-1)); \
  if (!(end_time > start_time)) { cout << "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 + 1];

  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();

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

  remove (OUTPUT_TXT_FILE_NAME);
  remove (OUTPUT_BIN_FILE_NAME);


  OPEN_TXT_FP_OUT;
  MEASURE_WITH_NO_ARG (C_01_txt__functions_getc_putc);
  CLOSE_TXT_FP_OUT;

  OPEN_BIN_FP_OUT;
  MEASURE_WITH_NO_ARG (C_01_bin__functions_getc_putc);
  CLOSE_BIN_FP_OUT;


  OPEN_TXT_FP_OUT;
  MEASURE_WITH_NO_ARG (C_02_txt__functions_fgetc_fputc);
  CLOSE_TXT_FP_OUT;

  OPEN_BIN_FP_OUT;
  MEASURE_WITH_NO_ARG (C_02_bin__functions_fgetc_fputc);
  CLOSE_BIN_FP_OUT;


  OPEN_TXT_FP_OUT;
  MEASURE_WITH_NO_ARG (C_03_txt__functions_fread_fwrite__const_buf);
  CLOSE_TXT_FP_OUT;

  OPEN_BIN_FP_OUT;
  MEASURE_WITH_NO_ARG (C_03_bin__functions_fread_fwrite__const_buf);
  CLOSE_BIN_FP_OUT;


  OPEN_TXT_FP_OUT;
  MEASURE_WITH_NO_ARG (C_04_txt__functions_fread_fwrite__max_buf);
  CLOSE_TXT_FP_OUT;

  OPEN_BIN_FP_OUT;
  MEASURE_WITH_NO_ARG (C_04_bin__functions_fread_fwrite__max_buf);
  CLOSE_BIN_FP_OUT;


#ifdef UNIX_ENV

  OPEN_TXT_FD_OUT;
  MEASURE_WITH_NO_ARG (unix_C_05_txt__mmap);
  CLOSE_TXT_FD_OUT;

  OPEN_BIN_FD_OUT;
  MEASURE_WITH_NO_ARG (unix_C_05_bin__mmap);
  CLOSE_BIN_FD_OUT;

#endif

  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_01_txt__operators_in_out);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_01_bin__operators_in_out);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_02_txt__methods_sbumpc_sputc);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_02_bin__methods_sbumpc_sputc);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_03_txt__method_sbumpc__op_out);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_03_bin__method_sbumpc__op_out);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_04_txt__method_rdbuf__op_out);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_04_bin__method_rdbuf__op_out);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_05_txt__methods_cpp_read_write__const_buf);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_05_bin__methods_cpp_read_write__const_buf);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_06_txt__methods_cpp_read_write_oss__const_buf);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_06_bin__methods_cpp_read_write_oss__const_buf);
  CLOSE_BIN_FS_OUT;

  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_07_txt__methods_cpp_readsome_write__const_buf);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_07_bin__methods_cpp_readsome_write__const_buf);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_08_txt__methods_cpp_read_write__max_buf);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_08_bin__methods_cpp_read_write__max_buf);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_09_txt__function_getline);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_09_bin__function_getline);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_10_txt__method_ifstream_getline);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_10_bin__method_ifstream_getline);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_11_txt__methods_ifstream_get_put);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_11_bin__methods_ifstream_get_put);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_12_txt__method_ifstream_get__const_buf);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_12_bin__method_ifstream_get__const_buf);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_13_txt__method_ifstream_get__streambuf);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_13_bin__method_ifstream_get__streambuf);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_14_txt__iostream_iterators__copy);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_14_bin__iostream_iterators__copy);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_15_txt__iostreambuf_iterators__copy);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_15_bin__iostreambuf_iterators__copy);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_16_txt__iostreambuf_iterators__transform);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_16_bin__iostreambuf_iterators__transform);
  CLOSE_BIN_FS_OUT;


  OPEN_TXT_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_17_txt__vector_copy);
  CLOSE_TXT_FS_OUT;

  OPEN_BIN_FS_OUT;
  MEASURE_WITH_NO_ARG (CPP_17_bin__vector_copy);
  CLOSE_BIN_FS_OUT;

  // -------------------------------
  // 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_txt_out.clear();
  fs_txt_out.close();
  assert (!fs_txt_out.is_open());

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

  fs_bin_out.clear();
  fs_bin_out.close();
  assert (!fs_bin_out.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;

}


--

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






-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

Gmane