// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// C++ Source Code File Name: testprog.cpp
// Compiler Used: MSVC40, DJGPP 2.7.2.1, egcs-2.90.29, HP CPP 10.24
// Author: Doug Gaer 
// File Creation Date: 09/20/1999
// Date Last Modified: 06/21/2000
// Copyright (c) 2000 Douglas M. Gaer
// ----------------------------------------------------------- // 
// ------------- Program Description and Details ------------- // 
// ----------------------------------------------------------- // 
/*
The VBS C++ classes are copyright (c) 2000, by Douglas M. Gaer.
All those who put this code or its derivatives in a commercial
product MUST mention this copyright in their documentation for
users of the products in which this code or its derivative
classes are used. Otherwise, you have the freedom to redistribute
verbatim copies of this source code, adapt it to your specific
needs, or improve the code and release your improvements to the
public provided that the modified files carry prominent notices
stating that you changed the files and the date of any change.

THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
CORRECTION.

This is a test program for the MemoryBuffer class. The MemoryBuffer
class is used to store variable length binary data of any type.
This example demonstrates how to use the MemoryBuffer class with
null terminated strings.
*/
// ----------------------------------------------------------- // 
#include <iostream.h>
#include "membuf.h"

void pause()
{
  cout << endl;
  cout << "Press enter to continue..." << endl;
  cin.get();
}

main()
{
  cout << "Testing MemoryBuffer constructors..." << endl;
  char *test_pattern = "The quick brown fox jumps over the lazy dog";
  char *test_message = "This is a test...";

  MemoryBuffer a((char *)test_pattern, strlen(test_pattern)+1);
  a[strlen(test_pattern)+1] = 0; // Null terminate the buffer
  cout << (char *)a << endl;
  
  MemoryBuffer b;

  MemoryBuffer c((char *)test_message, strlen(test_message)+1);
  c[strlen(test_message)+1] = 0; // Null terminate the buffer
  cout << (char *)c << endl;
  
  pause();

  cout << "Testing overloaded assignment operators..." << endl;
  b = a;
  cout << (char *)b << endl;
  b = c;
  cout << (char *)b << endl;
  
  pause();

  cout << "Testing copy constructor..." << endl;
  MemoryBuffer aa(a);
  cout << (char *)aa << endl;
  MemoryBuffer bb(b);
  cout << (char *)bb << endl;

  pause();
  
  cout << "Testing overloaded += operator..." << endl;
  char *num_test = " 0123456789";
  MemoryBuffer sbuf((char *)test_pattern, strlen(test_pattern));
  MemoryBuffer buf((char *)num_test, strlen(num_test));
  sbuf += buf;
  sbuf += '\0'; // Null terminate the buffer
  cout << (char *)sbuf << endl;

  pause();
  
  cout << "Testing concatenation..." << endl;
  char *s1 = "DOG "; char *s2 = "CAT "; char *s3 = "MOUSE";
  MemoryBuffer m1;
  m1.Load(s1, strlen(s1));
  m1.Cat(s2, strlen(s2));
  m1.Cat(s3, strlen(s3));
  m1.Cat('\0'); // Null terminate the buffer
  cout << (char *)m1 << endl;

  pause();
  
  cout << "Testing find functions..." << endl;
  cout << "String = " << (char *)m1 << endl;
  if(m1.Find(s2, strlen(s2), 0) == MemoryBuffer::NoMatch)
    cout << "Pattern not found!" << endl;
  else
    cout << "Found pattern: " << s1 << endl;

  if(m1.Find(s3, strlen(s3), 0) == MemoryBuffer::NoMatch)
    cout << "Pattern not found!" << endl;
  else
    cout << "Found pattern: " << s3 << endl;

  cout << endl;

  cout << "Testing repeated pattern finding..." << endl;
  MemoryBuffer m2(c);
  cout << "String = " << (char *)m2 << endl;
  char *s4 = "is";
  unsigned Offset = 0;
  while(1) {
    Offset = m2.Find((char *)s4, strlen(s4), Offset);
    if (Offset == MemoryBuffer::NoMatch) break;
    cout << "Found pattern \"" << s4 << "\" at index: " << Offset << endl;
    Offset++;
  }

  pause();

  cout << "Testing delete function..." << endl;
  MemoryBuffer x1(a);
  cout << (char *)x1 << endl;
  char *xx = "fox";
  int Index = x1.Find((char *)xx);
  cout << "Deleting fox from string..." << endl;
  x1.DeleteAt(Index, strlen(xx));
  cout << (char *)x1 << endl;

  pause();

  cout << "Testing Growth and Null Terminate function..." << endl;
  MemoryBuffer z(c); // Default GrowBy of 16
  cout << "String = " << (char *)z << endl;
  cout << "Buffer's logical length = " << z.GetLength() << endl;
  cout << "Buffer's allocated length = " << z.GetDimLen() << endl;
  cout << "Changing allocated length..." << endl;
  z.Grow();
  cout << "Buffer's allocated length = " << z.GetDimLen() << endl;
  cout << "Freeing unused allocated space..." << endl;
  z.FreeExtra();
  cout << "Buffer's allocated length = " << z.GetDimLen() << endl;
  cout << "Disallowing growth..." << endl;
  cout << "Buffer's logical length = " << z.GetLength() << endl;
  cout << "Buffer's allocated length = " << z.GetDimLen() << endl;

  pause();

  cout << "Testing resizing function" << endl;
  MemoryBuffer rs(a);
  cout << "String = " << (char *)rs << endl;
  cout << "Buffer's logical length = " << rs.GetLength() << endl;
  cout << "Buffer's allocated length = " << rs.GetDimLen() << endl;
  cout << "Resizing the buffer to 24 bytes" << endl;
  rs.resize(24);
  cout << "Buffer's logical length = " << rs.GetLength() << endl;
  cout << "Buffer's allocated length = " << rs.GetDimLen() << endl;

  pause();
  
  cout << "Testing overload subscript operator..." << endl;
  char *Message = "This is a test message";
  MemoryBuffer ss((char *)Message, strlen(Message));
  for(unsigned i = 0; i < strlen(Message); i++)
    cout << ss[i];
  cout << endl;

  return 0;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //