• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fstream>
12 #include <sstream>
13 
14 #include <marisa_alpha/io.h>
15 
16 #include "assert.h"
17 
18 namespace {
19 
TestFilename()20 void TestFilename() {
21   TEST_START();
22 
23   {
24     marisa_alpha::Writer writer;
25     writer.open("io-test.dat");
26     marisa_alpha::UInt32 value = 123;
27     writer.write(value);
28     writer.write(value);
29     double values[] = { 345, 456 };
30     writer.write(values, 2);
31     EXCEPT(writer.write(values, 1U << 30), MARISA_ALPHA_SIZE_ERROR);
32   }
33 
34   {
35     marisa_alpha::Writer writer;
36     writer.open("io-test.dat", false, 4, SEEK_SET);
37     marisa_alpha::UInt32 value = 234;
38     writer.write(value);
39   }
40 
41   {
42     marisa_alpha::Writer writer;
43     writer.open("io-test.dat", false, 0, SEEK_END);
44     double value = 567;
45     writer.write(value);
46   }
47 
48   {
49     marisa_alpha::Reader reader;
50     reader.open("io-test.dat");
51     marisa_alpha::UInt32 value;
52     reader.read(&value);
53     ASSERT(value == 123);
54     reader.read(&value);
55     ASSERT(value == 234);
56     double values[3];
57     reader.read(values, 3);
58     ASSERT(values[0] == 345);
59     ASSERT(values[1] == 456);
60     ASSERT(values[2] == 567);
61     char byte;
62     EXCEPT(reader.read(&byte), MARISA_ALPHA_IO_ERROR);
63   }
64 
65   {
66     marisa_alpha::Mapper mapper;
67     mapper.open("io-test.dat");
68     marisa_alpha::UInt32 value;
69     mapper.map(&value);
70     ASSERT(value == 123);
71     mapper.map(&value);
72     ASSERT(value == 234);
73     const double *values;
74     mapper.map(&values, 3);
75     ASSERT(values[0] == 345);
76     ASSERT(values[1] == 456);
77     ASSERT(values[2] == 567);
78     char byte;
79     EXCEPT(mapper.map(&byte), MARISA_ALPHA_IO_ERROR);
80   }
81 
82   {
83     marisa_alpha::Writer writer;
84     writer.open("io-test.dat");
85   }
86 
87   {
88     marisa_alpha::Reader reader;
89     reader.open("io-test.dat");
90     char byte;
91     EXCEPT(reader.read(&byte), MARISA_ALPHA_IO_ERROR);
92   }
93 
94   TEST_END();
95 }
96 
TestFd()97 void TestFd() {
98   TEST_START();
99 
100   {
101 #ifdef _MSC_VER
102     int fd = -1;
103     ASSERT(::_sopen_s(&fd, "io-test.dat",
104         _O_BINARY | _O_CREAT | _O_WRONLY | _O_TRUNC,
105         _SH_DENYRW, _S_IREAD | _S_IWRITE) == 0);
106 #else  // _MSC_VER
107     int fd = ::creat("io-test.dat", 0644);
108     ASSERT(fd != -1);
109 #endif  // _MSC_VER
110     marisa_alpha::Writer writer(fd);
111     marisa_alpha::UInt32 value = 345;
112     writer.write(value);
113     double values[] = { 456, 567, 678 };
114     writer.write(values, 3);
115 #ifdef _MSC_VER
116     ASSERT(::_close(fd) == 0);
117 #else  // _MSC_VER
118     ASSERT(::close(fd) == 0);
119 #endif  // _MSC_VER
120   }
121 
122   {
123 #ifdef _MSC_VER
124     int fd = -1;
125     ASSERT(::_sopen_s(&fd, "io-test.dat", _O_BINARY | _O_RDONLY,
126         _SH_DENYRW, _S_IREAD) == 0);
127 #else  // _MSC_VER
128     int fd = ::open("io-test.dat", O_RDONLY);
129     ASSERT(fd != -1);
130 #endif  // _MSC_VER
131     marisa_alpha::Reader reader(fd);
132     marisa_alpha::UInt32 value;
133     reader.read(&value);
134     ASSERT(value == 345);
135     double values[3];
136     reader.read(values, 3);
137     ASSERT(values[0] == 456);
138     ASSERT(values[1] == 567);
139     ASSERT(values[2] == 678);
140     char byte;
141     EXCEPT(reader.read(&byte), MARISA_ALPHA_IO_ERROR);
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_alpha::Writer writer(file);
164     marisa_alpha::UInt32 value = 345;
165     writer.write(value);
166     double values[3] = { 456, 567, 678 };
167     writer.write(values, 3);
168     ASSERT(std::fclose(file) == 0);
169   }
170 
171   {
172 #ifdef _MSC_VER
173     FILE *file = NULL;
174     ASSERT(::fopen_s(&file, "io-test.dat", "rb") == 0);
175 #else  // _MSC_VER
176     FILE *file = std::fopen("io-test.dat", "rb");
177     ASSERT(file != NULL);
178 #endif  // _MSC_VER
179     marisa_alpha::Reader reader(file);
180     marisa_alpha::UInt32 value;
181     reader.read(&value);
182     ASSERT(value == 345);
183     double values[3];
184     reader.read(values, 3);
185     ASSERT(values[0] == 456);
186     ASSERT(values[1] == 567);
187     ASSERT(values[2] == 678);
188     char byte;
189     EXCEPT(reader.read(&byte), MARISA_ALPHA_IO_ERROR);
190     ASSERT(std::fclose(file) == 0);
191   }
192 
193   TEST_END();
194 }
195 
TestStream()196 void TestStream() {
197   TEST_START();
198 
199   {
200     std::ofstream file("io-test.dat", std::ios::binary);
201     ASSERT(file.is_open());
202     marisa_alpha::Writer writer(&file);
203     marisa_alpha::UInt32 value = 345;
204     writer.write(value);
205     double values[3] = { 456, 567, 678 };
206     writer.write(values, 3);
207   }
208 
209   {
210     std::ifstream file("io-test.dat", std::ios::binary);
211     ASSERT(file.is_open());
212     marisa_alpha::Reader reader(&file);
213     marisa_alpha::UInt32 value;
214     reader.read(&value);
215     ASSERT(value == 345);
216     double values[3];
217     reader.read(values, 3);
218     ASSERT(values[0] == 456);
219     ASSERT(values[1] == 567);
220     ASSERT(values[2] == 678);
221     char byte;
222     EXCEPT(reader.read(&byte), MARISA_ALPHA_IO_ERROR);
223   }
224 
225   TEST_END();
226 }
227 
228 }  // namespace
229 
main()230 int main() {
231   TestFilename();
232   TestFd();
233   TestFile();
234   TestStream();
235 
236   return 0;
237 }
238