melisa 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/README.md +11 -0
  2. data/ext/marisa/bindings/marisa-swig.cxx +253 -0
  3. data/ext/marisa/bindings/marisa-swig.h +183 -0
  4. data/ext/marisa/bindings/perl/marisa-swig.cxx +253 -0
  5. data/ext/marisa/bindings/perl/marisa-swig.h +183 -0
  6. data/ext/marisa/bindings/perl/marisa-swig_wrap.cxx +5160 -0
  7. data/ext/marisa/bindings/python/marisa-swig.cxx +253 -0
  8. data/ext/marisa/bindings/python/marisa-swig.h +183 -0
  9. data/ext/marisa/bindings/python/marisa-swig_wrap.cxx +6090 -0
  10. data/ext/marisa/bindings/ruby/extconf.rb +5 -0
  11. data/ext/marisa/bindings/ruby/marisa-swig.cxx +253 -0
  12. data/ext/marisa/bindings/ruby/marisa-swig.h +183 -0
  13. data/ext/marisa/bindings/ruby/marisa-swig_wrap.cxx +4708 -0
  14. data/ext/marisa/lib/marisa.h +14 -0
  15. data/ext/marisa/lib/marisa/agent.cc +51 -0
  16. data/ext/marisa/lib/marisa/agent.h +73 -0
  17. data/ext/marisa/lib/marisa/base.h +193 -0
  18. data/ext/marisa/lib/marisa/exception.h +82 -0
  19. data/ext/marisa/lib/marisa/grimoire/algorithm.h +26 -0
  20. data/ext/marisa/lib/marisa/grimoire/algorithm/sort.h +196 -0
  21. data/ext/marisa/lib/marisa/grimoire/intrin.h +115 -0
  22. data/ext/marisa/lib/marisa/grimoire/io.h +18 -0
  23. data/ext/marisa/lib/marisa/grimoire/io/mapper.cc +163 -0
  24. data/ext/marisa/lib/marisa/grimoire/io/mapper.h +67 -0
  25. data/ext/marisa/lib/marisa/grimoire/io/reader.cc +147 -0
  26. data/ext/marisa/lib/marisa/grimoire/io/reader.h +66 -0
  27. data/ext/marisa/lib/marisa/grimoire/io/writer.cc +148 -0
  28. data/ext/marisa/lib/marisa/grimoire/io/writer.h +65 -0
  29. data/ext/marisa/lib/marisa/grimoire/trie.h +16 -0
  30. data/ext/marisa/lib/marisa/grimoire/trie/cache.h +81 -0
  31. data/ext/marisa/lib/marisa/grimoire/trie/config.h +155 -0
  32. data/ext/marisa/lib/marisa/grimoire/trie/entry.h +82 -0
  33. data/ext/marisa/lib/marisa/grimoire/trie/header.h +61 -0
  34. data/ext/marisa/lib/marisa/grimoire/trie/history.h +65 -0
  35. data/ext/marisa/lib/marisa/grimoire/trie/key.h +228 -0
  36. data/ext/marisa/lib/marisa/grimoire/trie/louds-trie.cc +876 -0
  37. data/ext/marisa/lib/marisa/grimoire/trie/louds-trie.h +134 -0
  38. data/ext/marisa/lib/marisa/grimoire/trie/range.h +115 -0
  39. data/ext/marisa/lib/marisa/grimoire/trie/state.h +117 -0
  40. data/ext/marisa/lib/marisa/grimoire/trie/tail.cc +218 -0
  41. data/ext/marisa/lib/marisa/grimoire/trie/tail.h +72 -0
  42. data/ext/marisa/lib/marisa/grimoire/vector.h +18 -0
  43. data/ext/marisa/lib/marisa/grimoire/vector/bit-vector.cc +826 -0
  44. data/ext/marisa/lib/marisa/grimoire/vector/bit-vector.h +179 -0
  45. data/ext/marisa/lib/marisa/grimoire/vector/flat-vector.h +205 -0
  46. data/ext/marisa/lib/marisa/grimoire/vector/pop-count.h +110 -0
  47. data/ext/marisa/lib/marisa/grimoire/vector/rank-index.h +82 -0
  48. data/ext/marisa/lib/marisa/grimoire/vector/vector.h +256 -0
  49. data/ext/marisa/lib/marisa/iostream.h +18 -0
  50. data/ext/marisa/lib/marisa/key.h +85 -0
  51. data/ext/marisa/lib/marisa/keyset.cc +181 -0
  52. data/ext/marisa/lib/marisa/keyset.h +80 -0
  53. data/ext/marisa/lib/marisa/query.h +71 -0
  54. data/ext/marisa/lib/marisa/scoped-array.h +48 -0
  55. data/ext/marisa/lib/marisa/scoped-ptr.h +52 -0
  56. data/ext/marisa/lib/marisa/stdio.h +15 -0
  57. data/ext/marisa/lib/marisa/trie.cc +249 -0
  58. data/ext/marisa/lib/marisa/trie.h +64 -0
  59. data/ext/marisa/tests/base-test.cc +309 -0
  60. data/ext/marisa/tests/io-test.cc +252 -0
  61. data/ext/marisa/tests/marisa-assert.h +26 -0
  62. data/ext/marisa/tests/marisa-test.cc +388 -0
  63. data/ext/marisa/tests/trie-test.cc +507 -0
  64. data/ext/marisa/tests/vector-test.cc +466 -0
  65. data/ext/marisa/tools/cmdopt.cc +298 -0
  66. data/ext/marisa/tools/cmdopt.h +58 -0
  67. data/ext/marisa/tools/marisa-benchmark.cc +418 -0
  68. data/ext/marisa/tools/marisa-build.cc +206 -0
  69. data/ext/marisa/tools/marisa-common-prefix-search.cc +143 -0
  70. data/ext/marisa/tools/marisa-dump.cc +151 -0
  71. data/ext/marisa/tools/marisa-lookup.cc +110 -0
  72. data/ext/marisa/tools/marisa-predictive-search.cc +143 -0
  73. data/ext/marisa/tools/marisa-reverse-lookup.cc +110 -0
  74. data/lib/melisa.rb +7 -0
  75. data/lib/melisa/base_config_flags.rb +76 -0
  76. data/lib/melisa/bytes_trie.rb +55 -0
  77. data/lib/melisa/int_trie.rb +14 -0
  78. data/lib/melisa/search.rb +55 -0
  79. data/lib/melisa/trie.rb +96 -0
  80. data/lib/melisa/version.rb +3 -0
  81. data/melisa.gemspec +36 -0
  82. data/spec/base_config_flags_spec.rb +73 -0
  83. data/spec/bytes_trie_spec.rb +16 -0
  84. data/spec/int_trie_spec.rb +16 -0
  85. data/spec/search_spec.rb +29 -0
  86. data/spec/spec_helper.rb +1 -0
  87. data/spec/trie_spec.rb +30 -0
  88. metadata +207 -0
@@ -0,0 +1,64 @@
1
+ #ifndef MARISA_TRIE_H_
2
+ #define MARISA_TRIE_H_
3
+
4
+ #include "marisa/keyset.h"
5
+ #include "marisa/agent.h"
6
+
7
+ namespace marisa {
8
+ namespace grimoire {
9
+ namespace trie {
10
+
11
+ class LoudsTrie;
12
+
13
+ } // namespace trie
14
+ } // namespace grimoire
15
+
16
+ class Trie {
17
+ friend class TrieIO;
18
+
19
+ public:
20
+ Trie();
21
+ ~Trie();
22
+
23
+ void build(Keyset &keyset, int config_flags = 0);
24
+
25
+ void mmap(const char *filename);
26
+ void map(const void *ptr, std::size_t size);
27
+
28
+ void load(const char *filename);
29
+ void read(int fd);
30
+
31
+ void save(const char *filename) const;
32
+ void write(int fd) const;
33
+
34
+ bool lookup(Agent &agent) const;
35
+ void reverse_lookup(Agent &agent) const;
36
+ bool common_prefix_search(Agent &agent) const;
37
+ bool predictive_search(Agent &agent) const;
38
+
39
+ std::size_t num_tries() const;
40
+ std::size_t num_keys() const;
41
+ std::size_t num_nodes() const;
42
+
43
+ TailMode tail_mode() const;
44
+ NodeOrder node_order() const;
45
+
46
+ bool empty() const;
47
+ std::size_t size() const;
48
+ std::size_t total_size() const;
49
+ std::size_t io_size() const;
50
+
51
+ void clear();
52
+ void swap(Trie &rhs);
53
+
54
+ private:
55
+ scoped_ptr<grimoire::trie::LoudsTrie> trie_;
56
+
57
+ // Disallows copy and assignment.
58
+ Trie(const Trie &);
59
+ Trie &operator=(const Trie &);
60
+ };
61
+
62
+ } // namespace marisa
63
+
64
+ #endif // MARISA_TRIE_H_
@@ -0,0 +1,309 @@
1
+ #include <cstdlib>
2
+ #include <cstring>
3
+ #include <ctime>
4
+ #include <string>
5
+ #include <vector>
6
+
7
+ #include <marisa.h>
8
+
9
+ #include "marisa-assert.h"
10
+
11
+ namespace {
12
+
13
+ void TestTypes() {
14
+ TEST_START();
15
+
16
+ ASSERT(sizeof(marisa_uint8) == 1);
17
+ ASSERT(sizeof(marisa_uint16) == 2);
18
+ ASSERT(sizeof(marisa_uint32) == 4);
19
+ ASSERT(sizeof(marisa_uint64) == 8);
20
+
21
+ ASSERT(MARISA_WORD_SIZE == (sizeof(std::size_t) * 8));
22
+
23
+ ASSERT(MARISA_UINT8_MAX == 0xFFU);
24
+ ASSERT(MARISA_UINT16_MAX == 0xFFFFU);
25
+ ASSERT(MARISA_UINT32_MAX == 0xFFFFFFFFU);
26
+ ASSERT(MARISA_UINT64_MAX == 0xFFFFFFFFFFFFFFFFULL);
27
+
28
+ ASSERT(sizeof(marisa::UInt8) == 1);
29
+ ASSERT(sizeof(marisa::UInt16) == 2);
30
+ ASSERT(sizeof(marisa::UInt32) == 4);
31
+ ASSERT(sizeof(marisa::UInt64) == 8);
32
+
33
+ TEST_END();
34
+ }
35
+
36
+ void TestSwap() {
37
+ TEST_START();
38
+
39
+ int x = 100, y = 200;
40
+ marisa::swap(x, y);
41
+ ASSERT(x == 200);
42
+ ASSERT(y == 100);
43
+
44
+ double a = 1.23, b = 2.34;
45
+ marisa::swap(a, b);
46
+ ASSERT(a == 2.34);
47
+ ASSERT(b == 1.23);
48
+
49
+ TEST_END();
50
+ }
51
+
52
+ void TestException() {
53
+ TEST_START();
54
+
55
+ try {
56
+ MARISA_THROW(MARISA_OK, "Message");
57
+ } catch (const marisa::Exception &ex) {
58
+ ASSERT(std::strcmp(ex.filename(), __FILE__) == 0);
59
+ ASSERT(ex.line() == (__LINE__ - 3));
60
+ ASSERT(ex.error_code() == MARISA_OK);
61
+ ASSERT(std::strstr(ex.error_message(), "Message") != NULL);
62
+ }
63
+
64
+ EXCEPT(MARISA_THROW(MARISA_OK, "OK"), MARISA_OK);
65
+ EXCEPT(MARISA_THROW(MARISA_NULL_ERROR, "NULL"), MARISA_NULL_ERROR);
66
+
67
+ TEST_END();
68
+ }
69
+
70
+ void TestKey() {
71
+ TEST_START();
72
+
73
+ const char * const str = "apple";
74
+
75
+ marisa::Key key;
76
+
77
+ ASSERT(key.ptr() == NULL);
78
+ ASSERT(key.length() == 0);
79
+
80
+ key.set_str(str);
81
+
82
+ ASSERT(key.ptr() == str);
83
+ ASSERT(key.length() == std::strlen(str));
84
+
85
+ key.set_str(str, 4);
86
+
87
+ ASSERT(key.ptr() == str);
88
+ ASSERT(key.length() == 4);
89
+
90
+ key.set_weight(1.0);
91
+
92
+ ASSERT(key.weight() == 1.0);
93
+
94
+ key.set_id(100);
95
+
96
+ ASSERT(key.id() == 100);
97
+
98
+ TEST_END();
99
+ }
100
+
101
+ void TestKeyset() {
102
+ TEST_START();
103
+
104
+ marisa::Keyset keyset;
105
+
106
+ ASSERT(keyset.size() == 0);
107
+ ASSERT(keyset.empty());
108
+ ASSERT(keyset.total_length() == 0);
109
+
110
+ std::vector<std::string> keys;
111
+ keys.push_back("apple");
112
+ keys.push_back("orange");
113
+ keys.push_back("banana");
114
+
115
+ std::size_t total_length = 0;
116
+ for (std::size_t i = 0; i < keys.size(); ++i) {
117
+ keyset.push_back(keys[i].c_str());
118
+ ASSERT(keyset.size() == (i + 1));
119
+ ASSERT(!keyset.empty());
120
+
121
+ total_length += keys[i].length();
122
+ ASSERT(keyset.total_length() == total_length);
123
+
124
+ ASSERT(keyset[i].length() == keys[i].length());
125
+ ASSERT(std::memcmp(keyset[i].ptr(), keys[i].c_str(),
126
+ keyset[i].length()) == 0);
127
+ ASSERT(keyset[i].weight() == 1.0);
128
+ }
129
+
130
+ keyset.clear();
131
+
132
+ marisa::Key key;
133
+
134
+ key.set_str("123");
135
+ keyset.push_back(key);
136
+ ASSERT(keyset[0].length() == 3);
137
+ ASSERT(std::memcmp(keyset[0].ptr(), "123", 3) == 0);
138
+
139
+ key.set_str("456");
140
+ keyset.push_back(key, '\0');
141
+ ASSERT(keyset[1].length() == 3);
142
+ ASSERT(std::memcmp(keyset[1].ptr(), "456", 3) == 0);
143
+ ASSERT(std::strcmp(keyset[1].ptr(), "456") == 0);
144
+
145
+ key.set_str("789");
146
+ keyset.push_back(key, '0');
147
+ ASSERT(keyset[2].length() == 3);
148
+ ASSERT(std::memcmp(keyset[2].ptr(), "789", 3) == 0);
149
+ ASSERT(std::memcmp(keyset[2].ptr(), "7890", 4) == 0);
150
+
151
+ ASSERT(keyset.size() == 3);
152
+
153
+ keyset.clear();
154
+
155
+ ASSERT(keyset.size() == 0);
156
+ ASSERT(keyset.total_length() == 0);
157
+
158
+ keys.resize(1000);
159
+ std::vector<float> weights(keys.size());
160
+
161
+ total_length = 0;
162
+ for (std::size_t i = 0; i < keys.size(); ++i) {
163
+ keys[i].resize(std::rand() % (marisa::Keyset::EXTRA_BLOCK_SIZE * 2));
164
+ for (std::size_t j = 0; j < keys[i].length(); ++j) {
165
+ keys[i][j] = (char)(std::rand() & 0xFF);
166
+ }
167
+ weights[i] = 100.0F * std::rand() / RAND_MAX;
168
+
169
+ keyset.push_back(keys[i].c_str(), keys[i].length(), weights[i]);
170
+ total_length += keys[i].length();
171
+ ASSERT(keyset.total_length() == total_length);
172
+ }
173
+
174
+ ASSERT(keyset.size() == keys.size());
175
+ for (std::size_t i = 0; i < keys.size(); ++i) {
176
+ ASSERT(keyset[i].length() == keys[i].length());
177
+ ASSERT(std::memcmp(keyset[i].ptr(), keys[i].c_str(),
178
+ keyset[i].length()) == 0);
179
+ ASSERT(keyset[i].weight() == weights[i]);
180
+ }
181
+
182
+ keyset.reset();
183
+
184
+ ASSERT(keyset.size() == 0);
185
+ ASSERT(keyset.total_length() == 0);
186
+
187
+ total_length = 0;
188
+ for (std::size_t i = 0; i < keys.size(); ++i) {
189
+ keys[i].resize(std::rand() % (marisa::Keyset::EXTRA_BLOCK_SIZE * 2));
190
+ for (std::size_t j = 0; j < keys[i].length(); ++j) {
191
+ keys[i][j] = (char)(std::rand() & 0xFF);
192
+ }
193
+ weights[i] = 100.0F * std::rand() / RAND_MAX;
194
+
195
+ keyset.push_back(keys[i].c_str(), keys[i].length(), weights[i]);
196
+ total_length += keys[i].length();
197
+ ASSERT(keyset.total_length() == total_length);
198
+ }
199
+
200
+ ASSERT(keyset.size() == keys.size());
201
+ for (std::size_t i = 0; i < keys.size(); ++i) {
202
+ ASSERT(keyset[i].length() == keys[i].length());
203
+ ASSERT(std::memcmp(keyset[i].ptr(), keys[i].c_str(),
204
+ keyset[i].length()) == 0);
205
+ ASSERT(keyset[i].weight() == weights[i]);
206
+ }
207
+
208
+ TEST_END();
209
+ }
210
+
211
+ void TestQuery() {
212
+ TEST_START();
213
+
214
+ marisa::Query query;
215
+
216
+ ASSERT(query.ptr() == NULL);
217
+ ASSERT(query.length() == 0);
218
+ ASSERT(query.id() == 0);
219
+
220
+ const char *str = "apple";
221
+ query.set_str(str);
222
+
223
+ ASSERT(query.ptr() == str);
224
+ ASSERT(query.length() == std::strlen(str));
225
+
226
+ query.set_str(str, 3);
227
+
228
+ ASSERT(query.ptr() == str);
229
+ ASSERT(query.length() == 3);
230
+
231
+ query.set_id(100);
232
+
233
+ ASSERT(query.id() == 100);
234
+
235
+ query.clear();
236
+
237
+ ASSERT(query.ptr() == NULL);
238
+ ASSERT(query.length() == 0);
239
+ ASSERT(query.id() == 0);
240
+
241
+ TEST_END();
242
+ }
243
+
244
+ void TestAgent() {
245
+ TEST_START();
246
+
247
+ marisa::Agent agent;
248
+
249
+ ASSERT(agent.query().ptr() == NULL);
250
+ ASSERT(agent.query().length() == 0);
251
+ ASSERT(agent.query().id() == 0);
252
+
253
+ ASSERT(agent.key().ptr() == NULL);
254
+ ASSERT(agent.key().length() == 0);
255
+
256
+ ASSERT(!agent.has_state());
257
+
258
+ const char *query_str = "query";
259
+ const char *key_str = "key";
260
+
261
+ agent.set_query(query_str);
262
+ agent.set_query(123);
263
+ agent.set_key(key_str);
264
+ agent.set_key(234);
265
+
266
+ ASSERT(agent.query().ptr() == query_str);
267
+ ASSERT(agent.query().length() == std::strlen(query_str));
268
+ ASSERT(agent.query().id() == 123);
269
+
270
+ ASSERT(agent.key().ptr() == key_str);
271
+ ASSERT(agent.key().length() == std::strlen(key_str));
272
+ ASSERT(agent.key().id() == 234);
273
+
274
+ agent.init_state();
275
+
276
+ ASSERT(agent.has_state());
277
+
278
+ EXCEPT(agent.init_state(), MARISA_STATE_ERROR);
279
+
280
+ agent.clear();
281
+
282
+ ASSERT(agent.query().ptr() == NULL);
283
+ ASSERT(agent.query().length() == 0);
284
+ ASSERT(agent.query().id() == 0);
285
+
286
+ ASSERT(agent.key().ptr() == NULL);
287
+ ASSERT(agent.key().length() == 0);
288
+
289
+ ASSERT(!agent.has_state());
290
+
291
+ TEST_END();
292
+ }
293
+
294
+ } // namespace
295
+
296
+ int main() try {
297
+ TestTypes();
298
+ TestSwap();
299
+ TestException();
300
+ TestKey();
301
+ TestKeyset();
302
+ TestQuery();
303
+ TestAgent();
304
+
305
+ return 0;
306
+ } catch (const marisa::Exception &ex) {
307
+ std::cerr << ex.what() << std::endl;
308
+ throw;
309
+ }
@@ -0,0 +1,252 @@
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
+
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
+
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
+
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
+
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
+
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
+ }