PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PStreamTest.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: PStreamTest.cpp
4// Purpose: Tests the ../devices/PStream.cpp functions
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003 Shane T. Mueller <smueller@umich.edu>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28#include "../devices/PStream.h"
29#include <iostream>
30
31
32
33
34using std::cout;
35using std::endl;
36using std::flush;
37
38
39int main(int argc, char **argv)
40{
41
42 cout << "\n\n\n==========================================================\n";
43 cout << "Testing PStream Class\n";
44 cout << "==========================================================\n";
45
46
47
48 cout << "Opening file stream 'tmp.dat'\n";
49 PStream mystream("tmp.dat", sdWrite, stASCII);
50
51 cout << "Successfully opened\n";
52 cout << "---------------------------------------\n\n\n";
53
54 cout << "Writing text to file using WriteChar()\n";
55 mystream.WriteChar('a');
56 mystream.WriteChar('b');
57 mystream.WriteChar('c');
58 mystream.WriteChar('d');
59 mystream.WriteChar('e');
60 mystream.WriteChar('\n');
61
62
63
64 cout << "Writing text to file using WriteBuffer():\n";
65
66
67 mystream.WriteBuffer("-------------------\n",20);
68 mystream.WriteBuffer("1234567901234567890\n",20);
69 mystream.WriteBuffer("-------------------\n",20);
70 mystream.WriteBuffer("-------------------\n",20);
71 mystream.WriteBuffer("- -- --- ---- -----\n",20);
72 mystream.WriteBuffer("htoeas-ueoa-hoeutns\n",20);
73 mystream.WriteBuffer("htoeas-ueoa-hoeutns\n",20);
74
75
76 cout << "Text written\n";
77 cout << "---------------------------------------\n\n\n";
78
79 cout << "Closing text stream.\n";
80 mystream.Close();
81 cout << "Text stream Closed.\n";
82 cout << "---------------------------------------\n\n\n";
83
84
85 cout << "Reopening text stream with append\n\n\n";
86 mystream.Open("tmp.dat",sdAppend,stASCII);
87 cout << "Text stream ReOpened.\n";
88 cout << "---------------------------------------\n\n\n";
89
90 cout << "Writing to file with WriteBuffer\n";
91 mystream.WriteBuffer("-------------------\n",20);
92
93 cout << "Writing to buffer with WriteString\n";
94 mystream.WriteString("Another String");
95 cout << "WriteString testing complete.\n";
96 cout << "---------------------------------------\n\n\n";
97
98 cout << "Closing text stream (again).\n";
99 mystream.Close();
100 cout << "Text stream Closed (again).\n";
101 cout << "---------------------------------------\n\n\n";
102
103
104 cout << "Opening file for reading\n";
105 mystream.Open("tmp.dat",sdRead,stASCII);
106 cout << "File Opened for reading\n";
107 cout << "---------------------------------------\n\n\n";
108
109 cout << "Testing EOF\n";
110 if(mystream.Eof())
111 {
112 cout << " File At END";
113 }
114 else
115 {
116 cout << " File Not At END";
117 }
118 cout << "EOF Testing complete\n";
119 cout << "---------------------------------------\n\n\n";
120
121
122 cout << "Reading Characters in a line\n";
123 char tmpchar = mystream.ReadChar();
124 while(!mystream.Eof() && tmpchar != 10)
125 {
126 cout << "[" << tmpchar << "] " << flush;
127 tmpchar = mystream.ReadChar();
128 }
129
130 cout << "\nCharacter-reading testing complete\n";
131 cout << "---------------------------------------\n\n\n";
132
133
134
135 cout << "Reading entire line four times\n";
136 cout << "1: ["<< mystream.ReadLine() << "]\n";
137 cout << "2: ["<< mystream.ReadLine() << "]\n";
138 cout << "3: ["<< mystream.ReadLine() << "]\n";
139 cout << "4: ["<< mystream.ReadLine() << "]\n";
140
141 cout << "\nEntire line(s)-reading testing complete\n";
142 cout << "---------------------------------------\n\n\n";
143
144
145 cout << "Reading Tokens in a line\n";
146 char* tmpchar1 = mystream.ReadToken(' ');
147 while(!mystream.Eof() && *tmpchar1 != 10)
148 {
149 cout << "[" << tmpchar1 << "] " << flush;
150 delete(tmpchar1);
151 tmpchar1 = mystream.ReadToken(' ');
152 }
153
154 cout << "Final: [" << tmpchar1 << "] " << flush;
155 delete(tmpchar1);
156 cout << "residual: [" << mystream.ReadLine() << "]" << endl;
157
158 cout << "[" << mystream.ReadToken('-') << "]" << endl;
159 cout << "[" << mystream.ReadToken('-') << "]" << endl;
160 cout << "[" << mystream.ReadToken('-') << "]" << endl;
161 cout << "[" << mystream.ReadToken('-') << "]" << endl;
162 cout << "[" << mystream.ReadToken('-') << "]" << endl;
163 cout << "residual: [" << mystream.ReadLine() << "]" << endl;
164
165 cout << "[" << mystream.ReadToken('-') << "]" << endl;
166 cout << "[" << mystream.ReadToken('-') << "]" << endl;
167 cout << "[" << mystream.ReadToken('-') << "]" << endl;
168 cout << "[" << mystream.ReadToken('-') << "]" << endl;
169 cout << "[" << mystream.ReadToken('-') << "]" << endl;
170 cout << "residual: [" << mystream.ReadLine() << "]" << endl;
171
172
173 cout << "\nToken-testing complete\n";
174 cout << "---------------------------------------\n\n\n";
175 mystream.Close();
176
177 cout << "\n\n\n==========================================================\n";
178 cout << "Testing PStream class complete\n";
179 cout << "==========================================================\n";
180
181
182
183
184 return 0;
185}
186
int main(int argc, char **argv)
@ sdAppend
Definition PStream.h:40
@ sdWrite
Definition PStream.h:39
@ sdRead
Definition PStream.h:38
@ stASCII
Definition PStream.h:47
void WriteChar(const char character)
This method sends a single character to the stream.
Definition PStream.cpp:172
void WriteBuffer(const std::string &buffer, unsigned int length)
Definition PStream.cpp:190
bool Eof()
Definition PStream.cpp:388
std::string ReadToken(const char separator)
This reads up until the next separator token (or eof character)
Definition PStream.cpp:251
void WriteString(const std::string &buffer)
This method just writes the char* string to the stream.
Definition PStream.cpp:211
void Open(const std::string &filename, StreamDirection dir, StreamType type)
Definition PStream.cpp:118
bool Close()
Definition PStream.cpp:394
std::string ReadLine()
Definition PStream.cpp:290
char ReadChar()
Definition PStream.cpp:230