/[glob2]/glob2/testing/StreamTest.cpp
ViewVC logotype

Contents of /glob2/testing/StreamTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.2.3 - (show annotations) (download)
Sat Oct 1 14:20:34 2005 UTC (18 years, 7 months ago) by andrew_sayers
Branch: map-rewrite
Changes since 1.1.2.2: +12 -1 lines
More iostream work, some bugfixes

1 /* emacs settings information: -*- mode: c++; tab-width: 4; c-file-style: "glob2"; -*- */
2 /*
3 Copyright (C) 2001-2004 Stephane Magnenat & Luc-Olivier de Charrière
4 for any question or comment contact us at nct@ysagoon.com or nuage@ysagoon.com
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*
22 This is a test suite for The Glob2 stream libraries. It is fairly complete
23 */
24
25 #include <Fstream.h>
26 #include <Sstream.h>
27 #include <iostream>
28
29 template<class T> void check(char* name, T expected, T observed)
30 {
31 if (expected != observed) std::cout << "ERROR: " << name << "=" << observed << " instead of " << expected << "\n";
32 }
33
34 template<> void check<std::string>(char* name, std::string expected, std::string observed)
35 {
36 if (expected != observed) std::cout << "ERROR: " << name << "={" << observed << "} instead of {" << expected << "}\n";
37 }
38
39 int main() {
40
41 // PART 1: BINARY I/O
42
43 char c = 'a';
44 int8_t c1 = -1;
45 uint8_t c2 = 99;
46 short sh = -1;
47 int i = -1;
48 std::string st("Test\0String \"speech marks should be escaped\" \\As should backslashes\\Ignored Data", 68);
49
50 std::cout << " Checking Binary I/O..." << std::flush;
51 obfstream binout("StreamTest.bin", std::ios::binary) ;
52 binout
53 << enterSection("TestSection")
54 << title("c") << c
55 << title("c1") << c1
56 << title("c2") << c2
57 << title("sh") << sh
58 << title("i") << i << std::flush
59 << title("st") << st
60 << leaveSection(); // TestSection
61 binout.close();
62
63 c=' ';
64 c1=0;
65 c2=0;
66 sh=0;
67 i=0;
68 st = "";
69 ibfstream input("StreamTest.bin");
70 input
71 >> enterSection("TestSection")
72 >> title("c") >> c
73 >> title("c1") >> c1
74 >> title("c2") >> c2
75 >> title("sh") >> sh
76 >> title("i") >> i
77 >> title("st") >> st
78 >> leaveSection() // TestSection
79 ;
80 check("c" , 'a', c);
81 check("c1", int8_t(-1), c1);
82 check("c2", uint8_t(99), c2);
83 check("sh", short(-1), sh);
84 check("i" , int(-1), i);
85 check("st", std::string("Test\0String \"speech marks should be escaped\" \\As should backslashes\\Ignored Data", 68), st);
86
87 std::cout << " done." << std::endl;
88
89 // PART 2: STL I/O
90
91 std::cout << " Checking STL I/O..." << std::flush;
92
93 c=' ';
94 c1=0;
95 c2=0;
96 sh=0;
97 i=0;
98 st = "";
99 std::istringstream stringin("a\nÿ\nc\n-1\n-1\nTest_string\n");
100 stringin
101 >> enterSection("TestSection")
102 >> title("c") >> c
103 >> title("c1") >> c1
104 >> title("c2") >> c2
105 >> title("sh") >> sh
106 >> title("i") >> i
107 >> title("st") >> st
108 >> leaveSection() // TestSection
109 ;
110 check("c" , 'a', c);
111 check("c1", int8_t(-1), c1);
112 check("c2", uint8_t(99), c2);
113 check("sh", short(-1), sh);
114 check("i" , int(-1), i);
115 check("st", std::string("Test_string"), st);
116
117 c=' ';
118 c1=0;
119 c2=0;
120 sh=0;
121 i=0;
122 st = "";
123 std::istringstream stringin1("a\nÿ\nc\n-1\n-1\nTest_string\n");
124 std::ostringstream stringout;
125 stringin1.tie(&stringout);
126 stringin1
127 >> enterSection("TestSection")
128 >> title("c") >> c
129 >> title("c1") >> c1
130 >> title("c2") >> c2
131 >> title("sh") >> sh
132 >> title("i") >> i
133 >> title("st") >> st
134 >> leaveSection() // TestSection
135 ;
136 check("c" , 'a', c);
137 check("c1", int8_t(-1), c1);
138 check("c2", uint8_t(99), c2);
139 check("sh", short(-1), sh);
140 check("i" , int(-1), i);
141 check("st", std::string("Test_string"), st);
142 check("output", std::string("--> TestSection\n c = c1 = c2 = sh = i = st = <--\n"), stringout.str());
143
144 std::cout << " done." << std::endl;
145
146 // PART 3: TEXT I/O
147
148 std::cout << " Checking text I/O..." << std::flush;
149
150 otstringstream txtout("StreamTest.txt");
151 txtout
152 << enterSection("TestSection")
153 << title("c") << c
154 << title("c1") << c1
155 << title("c2") << c2
156 << title("sh") << sh
157 << title("i") << i << std::flush
158 << title("st") << st
159 << leaveSection(); // TestSection
160
161 if (std::basic_string<char, Text<char> >("\n"
162 "TestSection\n"
163 "{\n"
164 " c = a\n"
165 " c1 = ÿ\n"
166 " c2 = c\n"
167 " sh = -1\n"
168 " i = -1\n"
169 " st = \"Test_string\"\n"
170 "}") != txtout.str())
171 std::cout << "ERROR: " << "StreamTest.txt" << "={" << txtout.str().c_str() << "}\n";
172
173 std::cout << " done." << std::endl;
174
175 return 0;
176 }

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26