Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
27.istream.get.cpp
Go to the documentation of this file.
1
27#include <fstream>
28#include <sstream>
29#include <cstdio>
30#include <cstring>
31
32#include <rw_driver.h>
33
34
35template <class CharT, class FileStream>
36void test_get (FileStream &f, const char *type,
37 const CharT array [], std::size_t)
38{
39 rw_assert (!!f, 0, __LINE__, "basic_ifstream<%s, Traits>", type);
40
41 const CharT delim = f.widen ('\xff');
42
43 CharT buf [5] = { 0 }; // 4 chars + terminating null
44 const std::size_t bufsize = sizeof buf / sizeof *buf;
45
46 std::size_t n = 0;
47
48 // read chars into character buffer, at most 4 at a time
49 while (f.get (buf, bufsize, delim)) {
50
51 rw_assert (f.good () && array [n] == *buf, 0, __LINE__,
52 "%d. basic_ifstream<%s>::get (%p, %d, '\\x%x')",
53 n, type, buf, bufsize, delim);
54
55 ++n;
56
57 CharT ch;
58 f.get (ch);
59
60 rw_assert (f.good () && ch == array [n], 0, __LINE__,
61 "%d. basic_ifstream<%s>::get ('\\x%x'); expected '\\x%x'",
62 n, type, ch, array [n]);
63 ++n;
64 }
65
66 // reached the end of stream
67 rw_assert ((std::ios::eofbit | std::ios::failbit) == f.rdstate (),
68 0, __LINE__, "basic_ifstream<%s>::get(...)", type);
69}
70
71
72template <class CharT>
73void run_test (CharT, const char *tname)
74{
75 typedef CharT char_type;
76 typedef std::char_traits<char_type> traits_type;
77
78 CharT array [256]; // array of chars
79
80 const std::size_t nelems = sizeof array / sizeof *array;
81
82 // use tmpname(buffer) instead of tmpnam(0) because HP-UX returns 0
83 // for tmpnam (0) on MT environments
84 char fname [L_tmpnam];
85 std::tmpnam (fname);
86
87 rw_fatal (0 != fname, 0, __LINE__, "tmpnam(%#p) != 0", fname);
88
89 if (1) {
90 std::basic_ofstream<char_type, traits_type> f (fname);
91
92 rw_assert (!!f, 0, __LINE__,
93 "basic_ofstream <%s>::basic_ofstream (#%s)",
94 tname, fname);
95
96 // populate array with some chars with '\xff' interspersed
97
98 CharT *p = array;
99
100 for (std::size_t i = 0; std::size_t (i) < nelems / 2; ++i) {
101 *p++ = char_type (i + 1);
102
103 // separate each character with a `\xff'
104 *p++ = f.widen ('\xff');
105 }
106
107 // write array to stream in one shot
108 f.write (array, nelems);
109
110 rw_assert (f.good (), 0, __LINE__,
111 "basic_ofstream<%s>::write (%p, %d)",
112 tname, array, nelems);
113
114 f.close ();
115
116 rw_assert (f.good (), 0, __LINE__,
117 "basic_ofstream<%s>::close()", tname);
118 }
119
120 if (1) {
121 std::basic_ifstream<char_type, traits_type> f (fname);
122
123 rw_assert (!!f, 0, __LINE__,
124 "basic_ifstream<%s>::basic_ifstream (%#s)",
125 tname, fname);
126
127 // see if we can read the whole thing in correctly
128 test_get (f, tname, array, nelems);
129
130 // should have reached EOF above
131 rw_assert ((std::ios::eofbit | std::ios::failbit) == f.rdstate (),
132 0, __LINE__,
133 "basic_ifstream<%s>::get(...)", tname);
134
135 f.close ();
136
137 // successful close should have no effect on rdstate
138 rw_assert ((std::ios::eofbit | std::ios::failbit) == f.rdstate (),
139 0, __LINE__,
140 "basic_ifstream<%s>::get(...)", tname);
141 }
142
143 std::remove (fname);
144}
145
146
147static int run_test (int, char**)
148{
149 run_test (char (), "char");
150
151#ifndef _RWSTD_NO_WCHAR_T
152
153 run_test (wchar_t (), "wchar_t");
154
155#endif // _RWSTD_NO_WCHAR_T
156
157 return 0;
158}
159
160
162int main (int argc, char *argv[])
163{
164 return rw_test (argc, argv, __FILE__,
165 "istream.get",
166 0 /* no comment */,
167 run_test,
168 "", 0);
169}
#define i
Definition 17.names.cpp:126
#define f
Definition 17.names.cpp:119
#define array
Definition 17.names.cpp:447
#define CharT
Definition 17.names.cpp:457
#define buf
Definition 17.names.cpp:452
#define n
Definition 17.names.cpp:130
#define p
Definition 17.names.cpp:132
void run_test(CharT, const char *tname)
void test_get(FileStream &f, const char *type, const CharT array[], std::size_t)
#define main
#define L_tmpnam
A TOML array.
Definition array.hpp:258
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_fatal(int expr, const char *filename, int line, const char *fmtspec,...)
Definition driver.cpp:1596
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630