1 #ifdef _MSC_VER
2 #include <io.h>
3 #else
4 #include <unistd.h>
5 #endif // _MSC_VER
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10
11 #include <sstream>
12
13 #include <marisa/grimoire/io.h>
14
15 #include "marisa-assert.h"
16
17 namespace {
18
TestFilename()19 void TestFilename() {
20 TEST_START();
21
22 {
23 marisa::grimoire::Writer writer;
24 writer.open("io-test.dat");
25
26 writer.write((marisa::UInt32)123);
27 writer.write((marisa::UInt32)234);
28
29 double values[] = { 3.45, 4.56 };
30 writer.write(values, 2);
31
32 EXCEPT(writer.write(values, MARISA_SIZE_MAX), MARISA_SIZE_ERROR);
33 }
34
35 {
36 marisa::grimoire::Reader reader;
37 reader.open("io-test.dat");
38
39 marisa::UInt32 value;
40 reader.read(&value);
41 ASSERT(value == 123);
42 reader.read(&value);
43 ASSERT(value == 234);
44
45 double values[2];
46 reader.read(values, 2);
47 ASSERT(values[0] == 3.45);
48 ASSERT(values[1] == 4.56);
49
50 char byte;
51 EXCEPT(reader.read(&byte), MARISA_IO_ERROR);
52 }
53
54 {
55 marisa::grimoire::Mapper mapper;
56 mapper.open("io-test.dat");
57
58 marisa::UInt32 value;
59 mapper.map(&value);
60 ASSERT(value == 123);
61 mapper.map(&value);
62 ASSERT(value == 234);
63
64 const double *values;
65 mapper.map(&values, 2);
66 ASSERT(values[0] == 3.45);
67 ASSERT(values[1] == 4.56);
68
69 char byte;
70 EXCEPT(mapper.map(&byte), MARISA_IO_ERROR);
71 }
72
73 {
74 marisa::grimoire::Writer writer;
75 writer.open("io-test.dat");
76 }
77
78 {
79 marisa::grimoire::Reader reader;
80 reader.open("io-test.dat");
81
82 char byte;
83 EXCEPT(reader.read(&byte), MARISA_IO_ERROR);
84 }
85
86 TEST_END();
87 }
88
TestFd()89 void TestFd() {
90 TEST_START();
91
92 {
93 #ifdef _MSC_VER
94 int fd = -1;
95 ASSERT(::_sopen_s(&fd, "io-test.dat",
96 _O_BINARY | _O_CREAT | _O_WRONLY | _O_TRUNC,
97 _SH_DENYRW, _S_IREAD | _S_IWRITE) == 0);
98 #else // _MSC_VER
99 int fd = ::creat("io-test.dat", 0644);
100 ASSERT(fd != -1);
101 #endif // _MSC_VER
102 marisa::grimoire::Writer writer;
103 writer.open(fd);
104
105 marisa::UInt32 value = 234;
106 writer.write(value);
107
108 double values[] = { 34.5, 67.8 };
109 writer.write(values, 2);
110
111 #ifdef _MSC_VER
112 ASSERT(::_close(fd) == 0);
113 #else // _MSC_VER
114 ASSERT(::close(fd) == 0);
115 #endif // _MSC_VER
116 }
117
118 {
119 #ifdef _MSC_VER
120 int fd = -1;
121 ASSERT(::_sopen_s(&fd, "io-test.dat", _O_BINARY | _O_RDONLY,
122 _SH_DENYRW, _S_IREAD) == 0);
123 #else // _MSC_VER
124 int fd = ::open("io-test.dat", O_RDONLY);
125 ASSERT(fd != -1);
126 #endif // _MSC_VER
127 marisa::grimoire::Reader reader;
128 reader.open(fd);
129
130 marisa::UInt32 value;
131 reader.read(&value);
132 ASSERT(value == 234);
133
134 double values[2];
135 reader.read(values, 2);
136 ASSERT(values[0] == 34.5);
137 ASSERT(values[1] == 67.8);
138
139 char byte;
140 EXCEPT(reader.read(&byte), MARISA_IO_ERROR);
141
142 #ifdef _MSC_VER
143 ASSERT(::_close(fd) == 0);
144 #else // _MSC_VER
145 ASSERT(::close(fd) == 0);
146 #endif // _MSC_VER
147 }
148
149 TEST_END();
150 }
151
TestFile()152 void TestFile() {
153 TEST_START();
154
155 {
156 #ifdef _MSC_VER
157 FILE *file = NULL;
158 ASSERT(::fopen_s(&file, "io-test.dat", "wb") == 0);
159 #else // _MSC_VER
160 FILE *file = std::fopen("io-test.dat", "wb");
161 ASSERT(file != NULL);
162 #endif // _MSC_VER
163 marisa::grimoire::Writer writer;
164 writer.open(file);
165
166 marisa::UInt32 value = 10;
167 writer.write(value);
168
169 double values[2] = { 0.1, 0.2 };
170 writer.write(values, 2);
171
172 ASSERT(std::fclose(file) == 0);
173 }
174
175 {
176 #ifdef _MSC_VER
177 FILE *file = NULL;
178 ASSERT(::fopen_s(&file, "io-test.dat", "rb") == 0);
179 #else // _MSC_VER
180 FILE *file = std::fopen("io-test.dat", "rb");
181 ASSERT(file != NULL);
182 #endif // _MSC_VER
183 marisa::grimoire::Reader reader;
184 reader.open(file);
185
186 marisa::UInt32 value;
187 reader.read(&value);
188 ASSERT(value == 10);
189
190 double values[2];
191 reader.read(values, 2);
192 ASSERT(values[0] == 0.1);
193 ASSERT(values[1] == 0.2);
194
195 char byte;
196 EXCEPT(reader.read(&byte), MARISA_IO_ERROR);
197
198 ASSERT(std::fclose(file) == 0);
199 }
200
201 TEST_END();
202 }
203
TestStream()204 void TestStream() {
205 TEST_START();
206
207 std::stringstream stream;
208
209 {
210 marisa::grimoire::Writer writer;
211 writer.open(stream);
212
213 marisa::UInt32 value = 12;
214 writer.write(value);
215
216 double values[2] = { 3.4, 5.6 };
217 writer.write(values, 2);
218 }
219
220 {
221 marisa::grimoire::Reader reader;
222 reader.open(stream);
223
224 marisa::UInt32 value;
225 reader.read(&value);
226 ASSERT(value == 12);
227
228 double values[2];
229 reader.read(values, 2);
230 ASSERT(values[0] == 3.4);
231 ASSERT(values[1] == 5.6);
232
233 char byte;
234 EXCEPT(reader.read(&byte), MARISA_IO_ERROR);
235 }
236
237 TEST_END();
238 }
239
240 } // namespace
241
main()242 int main() try {
243 TestFilename();
244 TestFd();
245 TestFile();
246 TestStream();
247
248 return 0;
249 } catch (const marisa::Exception &ex) {
250 std::cerr << ex.what() << std::endl;
251 throw;
252 }
253