31
|
1 |
|
|
2 |
|
|
3 |
// Regression test for STL seekg
|
|
4 |
|
|
5 |
#include <cstdio> // I really like formatted output.
|
|
6 |
#include <cstdarg>
|
|
7 |
#include <fstream>
|
|
8 |
#include <vector>
|
|
9 |
#include<iostream>
|
|
10 |
|
|
11 |
#ifndef cstd
|
|
12 |
#ifdef _MSC_VER
|
|
13 |
#define cstd
|
|
14 |
#else
|
|
15 |
//#define cstd std
|
|
16 |
#define cstd
|
|
17 |
using namespace std;
|
|
18 |
#endif
|
|
19 |
#endif
|
|
20 |
|
|
21 |
#ifndef __SYMBIAN32__
|
|
22 |
#ifndef USING_STLPORT
|
|
23 |
#define vsnprintf _vsnprintf
|
|
24 |
#endif
|
|
25 |
#endif
|
|
26 |
|
|
27 |
class Error : public std::exception
|
|
28 |
{
|
|
29 |
public:
|
|
30 |
Error(const char *pszfmt, ...);
|
|
31 |
const char *what() const;
|
|
32 |
private:
|
|
33 |
char mMessage[256];
|
|
34 |
};
|
|
35 |
|
|
36 |
#define countof(x) (sizeof(x)/sizeof((x)[0]))
|
|
37 |
|
|
38 |
Error::Error(const char *pszfmt, ...)
|
|
39 |
{
|
|
40 |
cstd::va_list ap;
|
|
41 |
va_start(ap, pszfmt);
|
|
42 |
cstd::vsnprintf(mMessage, countof(mMessage), pszfmt, ap);
|
|
43 |
va_end(ap);
|
|
44 |
}
|
|
45 |
|
|
46 |
const char *Error::what() const
|
|
47 |
{
|
|
48 |
return mMessage;
|
|
49 |
}
|
|
50 |
|
|
51 |
// usage: testseek filename
|
|
52 |
#ifdef __SYMBIAN32__
|
|
53 |
int seek_test(int, char**)
|
|
54 |
#else
|
|
55 |
int main(int argc, char **argv)
|
|
56 |
#endif
|
|
57 |
{
|
|
58 |
try
|
|
59 |
{
|
|
60 |
#ifndef __SYMBIAN32__
|
|
61 |
if (argc != 2) {
|
|
62 |
throw Error ("Wrong number of arguments (Usage: testseek filename)");
|
|
63 |
}
|
|
64 |
#endif
|
|
65 |
#ifndef __SYMBIAN32__
|
|
66 |
std::ifstream ifs(argv[1]);
|
|
67 |
|
|
68 |
ifs.open(argv[1]);
|
|
69 |
#else
|
|
70 |
std::ifstream ifs;
|
|
71 |
ifs.open("c:\\testframework\\tstdcpp\\seek.txt");
|
|
72 |
#endif
|
|
73 |
|
|
74 |
if (!ifs) {
|
|
75 |
#ifdef __SYMBIAN32__
|
|
76 |
Error e1("Failed to open file \"%s\"", "c:\\testframework\\tstdcpp\\seek.txt");
|
|
77 |
throw e1;
|
|
78 |
#else
|
|
79 |
throw Error("Failed to open file \"%s\"", argv[1]);
|
|
80 |
#endif
|
|
81 |
}
|
|
82 |
|
|
83 |
ifs.exceptions(std::ios::badbit);
|
|
84 |
// First pass: read contents of file into character vector
|
|
85 |
std::vector<char> charvec;
|
|
86 |
cstd::printf("First pass:\n");
|
|
87 |
for(;;)
|
|
88 |
{
|
|
89 |
char ch;
|
|
90 |
if (!ifs.get(ch)) {
|
|
91 |
break;
|
|
92 |
}
|
|
93 |
cstd::printf("%c", ch);
|
|
94 |
charvec.push_back(ch);
|
|
95 |
}
|
|
96 |
printf("First pass done\n");
|
|
97 |
|
|
98 |
// Second pass: get positions into pos vector, checking chars
|
|
99 |
std::vector<std::ios::pos_type> posvec;
|
|
100 |
ifs.clear(); // necessary?
|
|
101 |
ifs.seekg(0);
|
|
102 |
std::vector<char>::const_iterator char_it;
|
|
103 |
printf("Second pass:\n");
|
|
104 |
for(char_it = charvec.begin(); char_it != charvec.end(); ++char_it)
|
|
105 |
{
|
|
106 |
std::ios::pos_type pos = ifs.tellg();
|
|
107 |
posvec.push_back(pos);
|
|
108 |
char ch;
|
|
109 |
if (!ifs.get(ch)) {
|
|
110 |
break;
|
|
111 |
}
|
|
112 |
cstd::printf("%c", ch);
|
|
113 |
if (ch != *char_it) {
|
|
114 |
#ifdef __SYMBIAN32__
|
|
115 |
Error e1("Character mismatch: got '%c', expected '%c'",ch, *char_it);
|
|
116 |
throw e1;
|
|
117 |
#else
|
|
118 |
throw Error("Character mismatch: got '%c', expected '%c'",
|
|
119 |
ch, *char_it);
|
|
120 |
#endif
|
|
121 |
}
|
|
122 |
}
|
|
123 |
if (char_it != charvec.end()) {
|
|
124 |
#ifdef __SYMBIAN32__
|
|
125 |
Error e1("Unexpected end of file");
|
|
126 |
throw e1;
|
|
127 |
#else
|
|
128 |
throw Error("Unexpected end of file");
|
|
129 |
#endif
|
|
130 |
}
|
|
131 |
|
|
132 |
printf("Second pass done\n");
|
|
133 |
|
|
134 |
// Third pass: seek to each saved position and read next char
|
|
135 |
ifs.clear(); // necessary, but I don't like it
|
|
136 |
std::vector<std::ios::pos_type>::const_iterator pos_it;
|
|
137 |
|
|
138 |
for(pos_it = posvec.begin();
|
|
139 |
pos_it != posvec.end(); ++pos_it)
|
|
140 |
{
|
|
141 |
printf ("%d ", (int)*pos_it);
|
|
142 |
}
|
|
143 |
printf ("\n");
|
|
144 |
|
|
145 |
printf("Third pass:\n");
|
|
146 |
for(char_it = charvec.begin(), pos_it = posvec.begin();
|
|
147 |
char_it != charvec.end(); ++char_it, ++pos_it)
|
|
148 |
{
|
|
149 |
ifs.seekg(*pos_it);
|
|
150 |
char ch;
|
|
151 |
if (!ifs.get(ch)) {
|
|
152 |
#ifdef __SYMBIAN32__
|
|
153 |
Error e1("End of file when expecting '%c'", *char_it);
|
|
154 |
throw e1;
|
|
155 |
#else
|
|
156 |
throw Error("End of file when expecting '%c'", *char_it);
|
|
157 |
#endif
|
|
158 |
}
|
|
159 |
cstd::printf("%c", ch);
|
|
160 |
if (ch != *char_it) {
|
|
161 |
#ifdef __SYMBIAN32__
|
|
162 |
Error e1("Character mismatch: got '%c', expected '%c'",ch, *char_it);
|
|
163 |
throw e1;
|
|
164 |
#else
|
|
165 |
throw Error("Character mismatch: got '%c', expected '%c'",ch, *char_it);
|
|
166 |
#endif
|
|
167 |
}
|
|
168 |
}
|
|
169 |
printf("Third pass done\n");
|
|
170 |
printf("Test passed\n");
|
|
171 |
|
|
172 |
|
|
173 |
}
|
|
174 |
catch (std::exception &e)
|
|
175 |
{
|
|
176 |
cstd::printf("\nCaught exception: %s\n", e.what());
|
|
177 |
cstd::printf("Test failed!\n");
|
|
178 |
return 1;
|
|
179 |
}
|
|
180 |
return 0;
|
|
181 |
}
|