Sample Code Templates

From arrizza.org wiki

Jump to navigation Jump to search
#pragma warning(disable: 4786)
#include <string>
#include <strstream>
#include <iostream>
#include <iomanip>
using namespace std;

template<typename T>
inline string getresult(const T& n1, const T& n2)   
  {
  strstream res;
  res << "expected='" << n1 << "' actual='" << n2 << "'" << ends;
  return res.str();
  }
template<>
inline string getresult(const __int64& n1, const __int64& n2)   
  {
  strstream res;
  res << "expected='" << hex << "0x" << (long) (n1 >> 32) << (long) (n1 & 0xFFFFFFFF) << "' actual='0x" << (long) (n2 >> 32) << (long) (n2 & 0xFFFFFFFF) << "'" << dec << ends;
  return res.str();
  }

template<typename T>
inline void func(const T& n1, const T& n2, const string& msg)   
  {
  if (n1 == n2) return;
  //cout << "t: " << getresult(n1, n2) << " msg='" << msg << "' " << typeid(n1).name() << "\n";
  cout << "t: " << getresult(n1, n2) << " msg='" << msg << "'\n";
  }
template<typename T>
inline void func(const T& n1, const T& n2)   
  {
  func(n1, n2, string("x"));
  }

//template<>
//inline void func(__int64 n1, __int64 n2, const string& msg)   
//  {
//  cout << "t: " << getresult(n1, n2) << setw(8) << hex << (long) (n2 >> 32)  << (long) (n1 & 0xFFFFFFFF) << dec << " " << msg << " " << typeid(n1).name() << "\n";
//  }

int main()
  {
  func((__int64)0xFFFFFFFFFFFFFFF, (__int64)5, "hi4");
  func((__int64)4, (__int64)4);
  func(1, 22, "hi");
  func(24, 24);
  func("a", "a", "hi2");
  func("a", "b");
  func(string("cde"), string("def"), "hi3");
  func(string("cde"), string("cde"));
  return 0;
  }

Personal tools