STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Macros | Typedefs | Functions
xtest File Reference
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <cvt/wbuffer>
#include <cvt/wstring>

Macros

#define _CVT_XTEST_
 
#define CHECK(x)
 
#define CHECK_INT(x, y)
 
#define CHECK_MSG(msg, ok)
 
#define NCHARS   0x100
 
#define MYWCHAR   wchar_t
 
#define Mywchar   MYWCHAR
 
#define MYWCMAX   0xffff
 

Typedefs

typedef basic_istream< MYWCHARMyistream
 
typedef basic_ostream< MYWCHARMyostream
 
typedef basic_string< MYWCHARMystring
 
typedef stdext::cvt::wstring_convert< MYNAME, MYWCHARMysconvert
 
typedef basic_filebuf< char > Mybfilebuf
 

Functions

int leave_chk (const char *fname)
 
bool test_write (const char *fname, const Mystring &mystring)
 
bool test_read (const char *fname, const Mystring &mystring)
 
void test_other ()
 
int main ()
 

Macro Definition Documentation

#define _CVT_XTEST_
#define CHECK (   x)
#define CHECK_INT (   x,
 
)
#define CHECK_MSG (   msg,
  ok 
)
#define MYWCHAR   wchar_t
#define Mywchar   MYWCHAR
#define MYWCMAX   0xffff
#define NCHARS   0x100

Typedef Documentation

typedef basic_filebuf<char> Mybfilebuf

Function Documentation

int leave_chk ( const char *  fname)
32  { // display message and quit
33  cout << "#PASSED: " << fname << endl;
34  return (0);
35  }
basic_ostream< _Elem, _Traits > &__CLRCALL_OR_CDECL endl(basic_ostream< _Elem, _Traits > &_Ostr)
Definition: ostream:997
__PURE_APPDOMAIN_GLOBAL _CRTDATA2 ostream cout
int main ( )
155  { // write a file and read it back
156  const char *fname = "testfile.txt";
157  Mystring mystring;
158 
159  #if defined(MYMAKE)
160  mystring = MYMAKE();
161 
162  #else /* defined(MYMAKE) */
163  Mysconvert myconv(""); // don't throw on bad codes
164 
165  for (unsigned long ch = 0;
166  ch <= MYWC_MAX && mystring.size() < NCHARS; ++ch)
167  { // add a wide character if valid
168  string buf = myconv.to_bytes((MYWCHAR)ch);
169  if (0 < buf.size() && ch != (unsigned long)WEOF)
170  mystring.insert(mystring.end(), (MYWCHAR)ch);
171  }
172  #endif /* defined(MYMAKE) */
173 
174  if (test_write(fname, mystring))
175  test_read(fname, mystring);
176  remove(fname);
177 
178  test_other();
179 
180  cout << "FINISHED testing \"cvt/"
181  << MYFILE << "\"" << endl;
182  return (leave_chk(MYFILE ".cpp"));
183  }
basic_string< MYWCHAR > Mystring
Definition: xtest:53
basic_ostream< _Elem, _Traits > &__CLRCALL_OR_CDECL endl(basic_ostream< _Elem, _Traits > &_Ostr)
Definition: ostream:997
#define NCHARS
Definition: xtest:39
bool test_read(const char *fname, const Mystring &mystring)
Definition: xtest:93
void test_other()
Definition: xtest:137
#define WEOF
Definition: ctype.h:26
Definition: wstring:22
__PURE_APPDOMAIN_GLOBAL _CRTDATA2 ostream cout
#define MYWCHAR
Definition: xtest:43
bool test_write(const char *fname, const Mystring &mystring)
Definition: xtest:58
int leave_chk(const char *fname)
Definition: xtest:31
void test_other ( )
138  { // test other codecvt member functions
139  MYNAME *p = new MYNAME;
140 
141  CHECK(!p->always_noconv());
142  CHECK(0 < p->max_length());
143  CHECK(-1 <= p->encoding() && p->encoding() <= p->max_length());
144 
145  const char arr[] = "ab";
146  static mbstate_t state0;
147  mbstate_t state = state0;
148  int len = p->length(state, &arr[0], &arr[2], 2 * p->max_length());
149  CHECK(0 <= len && len <= 2 * p->max_length());
150 
151  delete p;
152  }
int mbstate_t
Definition: wchar.h:1219
#define CHECK(x)
Definition: xtest:27
bool test_read ( const char *  fname,
const Mystring mystring 
)
94  { // read sequences of wide chars from file
95  Mybfilebuf mybbuf;
96 
97  mybbuf.open(fname, ios_base::binary | ios_base::in);
98  if (!mybbuf.is_open())
99  { // open failed, give up
100  #ifndef TERSE
101  cout << "can't read from " << fname << endl;
102  #endif /* TERSE */
103 
104  CHECK_MSG("can't open file for reading", 0);
105  return (false);
106  }
107 
109  Myistream mystr(&mybuf);
110 
111  MYWCHAR ch;
112  for (size_t idx = 0; idx < mystring.size(); ++idx)
113  { // read a wide char and test for expected value
114  ch = (MYWCHAR)(-1);
115  if (mystr.read(&ch, 1) && ch == mystring[idx])
116  CHECK(1);
117  else
118  { // read failed, quit
119  #ifndef TERSE
120  cout << hex
121  << "read failed for " << (unsigned long)mystring[idx]
122  << ", got " << (unsigned long)ch << endl;
123  #endif /* TERSE */
124 
125  CHECK_INT((int)idx, -1);
126  return (false);
127  }
128  }
129  if (mystr.read(&ch, 1))
130  { // read too much, complain
131  CHECK_MSG("end of file not reached", 0);
132  return (false);
133  }
134  return (true);
135  }
ios_base &__CLRCALL_OR_CDECL hex(ios_base &_Iosbase)
Definition: ios:233
#define CHECK_MSG(msg, ok)
Definition: xtest:29
basic_istream< MYWCHAR > Myistream
Definition: xtest:51
static const _Openmode binary
Definition: xiosbase:104
basic_ostream< _Elem, _Traits > &__CLRCALL_OR_CDECL endl(basic_ostream< _Elem, _Traits > &_Ostr)
Definition: ostream:997
#define CHECK_INT(x, y)
Definition: xtest:28
_Myt * open(const char *_Filename, ios_base::openmode _Mode, int _Prot=(int) ios_base::_Openprot)
Definition: fstream:262
#define CHECK(x)
Definition: xtest:27
__PURE_APPDOMAIN_GLOBAL _CRTDATA2 ostream cout
bool is_open() const
Definition: fstream:257
#define MYWCHAR
Definition: xtest:43
Definition: fstream:135
static const _Openmode in
Definition: xiosbase:97
Definition: wbuffer:30
bool test_write ( const char *  fname,
const Mystring mystring 
)
59  { // write sequences of wide chars to file
60  Mybfilebuf mybbuf;
61 
62  mybbuf.open(fname, ios_base::binary | ios_base::out);
63  if (!mybbuf.is_open())
64  { // open failed, give up
65  #ifndef TERSE
66  cout << "can't write to " << fname << endl;
67  #endif /* TERSE */
68 
69  CHECK_MSG("can't open file for writing", 0);
70  return (false);
71  }
72 
74  Myostream mystr(&mybuf);
75 
76  for (size_t idx = 0; idx < mystring.size(); ++idx)
77  if (mystr.write(&mystring[idx], 1))
78  CHECK(1);
79  else
80  { // write failed, quit
81  #ifndef TERSE
82  cout << hex
83  << "write failed for " << (unsigned long)mystring[idx]
84  << endl;
85  #endif /* TERSE */
86 
87  CHECK_INT((int)idx, -1);
88  return (false);
89  }
90  return (true);
91  }
static const _Openmode out
Definition: xiosbase:98
ios_base &__CLRCALL_OR_CDECL hex(ios_base &_Iosbase)
Definition: ios:233
basic_ostream< MYWCHAR > Myostream
Definition: xtest:52
#define CHECK_MSG(msg, ok)
Definition: xtest:29
static const _Openmode binary
Definition: xiosbase:104
basic_ostream< _Elem, _Traits > &__CLRCALL_OR_CDECL endl(basic_ostream< _Elem, _Traits > &_Ostr)
Definition: ostream:997
#define CHECK_INT(x, y)
Definition: xtest:28
_Myt * open(const char *_Filename, ios_base::openmode _Mode, int _Prot=(int) ios_base::_Openprot)
Definition: fstream:262
#define CHECK(x)
Definition: xtest:27
__PURE_APPDOMAIN_GLOBAL _CRTDATA2 ostream cout
bool is_open() const
Definition: fstream:257
Definition: fstream:135
Definition: wbuffer:30