ooxml_crypt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +58 -0
  5. data/Rakefile +12 -0
  6. data/bin/console +15 -0
  7. data/bin/setup +8 -0
  8. data/ext/ooxml_crypt/extconf.rb +18 -0
  9. data/ext/ooxml_crypt/ooxml_crypt.c +27 -0
  10. data/ext/ooxml_crypt/ooxml_crypt.h +7 -0
  11. data/lib/ooxml_crypt/version.rb +5 -0
  12. data/lib/ooxml_crypt.rb +75 -0
  13. data/vendor/cybozulib/.github/workflows/main.yml +12 -0
  14. data/vendor/cybozulib/.gitignore +5 -0
  15. data/vendor/cybozulib/CMakeLists.txt +6 -0
  16. data/vendor/cybozulib/COPYRIGHT +27 -0
  17. data/vendor/cybozulib/Makefile +26 -0
  18. data/vendor/cybozulib/bin/libeay32.dll +0 -0
  19. data/vendor/cybozulib/bin/libmecab.dll +0 -0
  20. data/vendor/cybozulib/bin/ssleay32.dll +0 -0
  21. data/vendor/cybozulib/common.mk +116 -0
  22. data/vendor/cybozulib/common.props +25 -0
  23. data/vendor/cybozulib/cybozulib.sln +286 -0
  24. data/vendor/cybozulib/debug.props +14 -0
  25. data/vendor/cybozulib/include/cybozu/array.hpp +197 -0
  26. data/vendor/cybozulib/include/cybozu/atoi.hpp +238 -0
  27. data/vendor/cybozulib/include/cybozu/atomic.hpp +146 -0
  28. data/vendor/cybozulib/include/cybozu/base64.hpp +210 -0
  29. data/vendor/cybozulib/include/cybozu/benchmark.hpp +212 -0
  30. data/vendor/cybozulib/include/cybozu/bfd.hpp +105 -0
  31. data/vendor/cybozulib/include/cybozu/bit_operation.hpp +139 -0
  32. data/vendor/cybozulib/include/cybozu/bitvector.hpp +358 -0
  33. data/vendor/cybozulib/include/cybozu/condition_variable.hpp +113 -0
  34. data/vendor/cybozulib/include/cybozu/condition_variable_cs.hpp +74 -0
  35. data/vendor/cybozulib/include/cybozu/config.hpp +392 -0
  36. data/vendor/cybozulib/include/cybozu/critical_section.hpp +60 -0
  37. data/vendor/cybozulib/include/cybozu/crypto.hpp +321 -0
  38. data/vendor/cybozulib/include/cybozu/csucvector.hpp +624 -0
  39. data/vendor/cybozulib/include/cybozu/csv.hpp +294 -0
  40. data/vendor/cybozulib/include/cybozu/data_type.hpp +27 -0
  41. data/vendor/cybozulib/include/cybozu/endian.hpp +224 -0
  42. data/vendor/cybozulib/include/cybozu/env.hpp +63 -0
  43. data/vendor/cybozulib/include/cybozu/event.hpp +122 -0
  44. data/vendor/cybozulib/include/cybozu/exception.hpp +253 -0
  45. data/vendor/cybozulib/include/cybozu/file.hpp +626 -0
  46. data/vendor/cybozulib/include/cybozu/fmindex.hpp +291 -0
  47. data/vendor/cybozulib/include/cybozu/format.hpp +93 -0
  48. data/vendor/cybozulib/include/cybozu/frequency.hpp +264 -0
  49. data/vendor/cybozulib/include/cybozu/hash.hpp +67 -0
  50. data/vendor/cybozulib/include/cybozu/inttype.hpp +174 -0
  51. data/vendor/cybozulib/include/cybozu/itoa.hpp +336 -0
  52. data/vendor/cybozulib/include/cybozu/json.hpp +120 -0
  53. data/vendor/cybozulib/include/cybozu/line_stream.hpp +149 -0
  54. data/vendor/cybozulib/include/cybozu/link_libeay32.hpp +21 -0
  55. data/vendor/cybozulib/include/cybozu/link_mpir.hpp +18 -0
  56. data/vendor/cybozulib/include/cybozu/link_ssleay32.hpp +19 -0
  57. data/vendor/cybozulib/include/cybozu/log.hpp +237 -0
  58. data/vendor/cybozulib/include/cybozu/minixml.hpp +452 -0
  59. data/vendor/cybozulib/include/cybozu/mmap.hpp +143 -0
  60. data/vendor/cybozulib/include/cybozu/mutex.hpp +144 -0
  61. data/vendor/cybozulib/include/cybozu/nlp/mecab.hpp +96 -0
  62. data/vendor/cybozulib/include/cybozu/nlp/plsi.hpp +315 -0
  63. data/vendor/cybozulib/include/cybozu/nlp/random.hpp +74 -0
  64. data/vendor/cybozulib/include/cybozu/nlp/sparse.hpp +529 -0
  65. data/vendor/cybozulib/include/cybozu/nlp/svd.hpp +486 -0
  66. data/vendor/cybozulib/include/cybozu/nlp/tfidf.hpp +226 -0
  67. data/vendor/cybozulib/include/cybozu/nlp/top_score.hpp +75 -0
  68. data/vendor/cybozulib/include/cybozu/option.hpp +743 -0
  69. data/vendor/cybozulib/include/cybozu/parallel.hpp +88 -0
  70. data/vendor/cybozulib/include/cybozu/pcg.hpp +72 -0
  71. data/vendor/cybozulib/include/cybozu/process.hpp +324 -0
  72. data/vendor/cybozulib/include/cybozu/quit_signal_handler.hpp +66 -0
  73. data/vendor/cybozulib/include/cybozu/random_generator.hpp +144 -0
  74. data/vendor/cybozulib/include/cybozu/regex.hpp +463 -0
  75. data/vendor/cybozulib/include/cybozu/select8.hpp +279 -0
  76. data/vendor/cybozulib/include/cybozu/serializer.hpp +363 -0
  77. data/vendor/cybozulib/include/cybozu/sha1.hpp +209 -0
  78. data/vendor/cybozulib/include/cybozu/sha2.hpp +506 -0
  79. data/vendor/cybozulib/include/cybozu/siphash.hpp +105 -0
  80. data/vendor/cybozulib/include/cybozu/socket.hpp +785 -0
  81. data/vendor/cybozulib/include/cybozu/ssl.hpp +203 -0
  82. data/vendor/cybozulib/include/cybozu/stacktrace.hpp +291 -0
  83. data/vendor/cybozulib/include/cybozu/stream.hpp +269 -0
  84. data/vendor/cybozulib/include/cybozu/string.hpp +1746 -0
  85. data/vendor/cybozulib/include/cybozu/string_operation.hpp +365 -0
  86. data/vendor/cybozulib/include/cybozu/sucvector.hpp +378 -0
  87. data/vendor/cybozulib/include/cybozu/test.hpp +373 -0
  88. data/vendor/cybozulib/include/cybozu/thread.hpp +229 -0
  89. data/vendor/cybozulib/include/cybozu/time.hpp +281 -0
  90. data/vendor/cybozulib/include/cybozu/tls.hpp +115 -0
  91. data/vendor/cybozulib/include/cybozu/unordered_map.hpp +13 -0
  92. data/vendor/cybozulib/include/cybozu/unordered_set.hpp +13 -0
  93. data/vendor/cybozulib/include/cybozu/v128.hpp +376 -0
  94. data/vendor/cybozulib/include/cybozu/wavelet_matrix.hpp +345 -0
  95. data/vendor/cybozulib/include/cybozu/xorshift.hpp +189 -0
  96. data/vendor/cybozulib/include/cybozu/zlib.hpp +325 -0
  97. data/vendor/cybozulib/include/sais.hxx +364 -0
  98. data/vendor/cybozulib/misc/make_select8tbl.cpp +26 -0
  99. data/vendor/cybozulib/mk.bat +37 -0
  100. data/vendor/cybozulib/readme.md +29 -0
  101. data/vendor/cybozulib/release.props +12 -0
  102. data/vendor/cybozulib/sample/Makefile +30 -0
  103. data/vendor/cybozulib/sample/csucvector_smpl.cpp +42 -0
  104. data/vendor/cybozulib/sample/data/svd/org/test1.S +4 -0
  105. data/vendor/cybozulib/sample/data/svd/org/test1.U +4 -0
  106. data/vendor/cybozulib/sample/data/svd/org/test1.V +6 -0
  107. data/vendor/cybozulib/sample/data/svd/test1 +4 -0
  108. data/vendor/cybozulib/sample/data/svd/test2 +4 -0
  109. data/vendor/cybozulib/sample/desymbol.cpp +127 -0
  110. data/vendor/cybozulib/sample/exception_smpl.cpp +46 -0
  111. data/vendor/cybozulib/sample/fmindex_smpl.cpp +231 -0
  112. data/vendor/cybozulib/sample/log_smpl.cpp +19 -0
  113. data/vendor/cybozulib/sample/mecab_smpl.cpp +37 -0
  114. data/vendor/cybozulib/sample/option2_smpl.cpp +68 -0
  115. data/vendor/cybozulib/sample/option_smpl.cpp +42 -0
  116. data/vendor/cybozulib/sample/plsi_smpl.cpp +207 -0
  117. data/vendor/cybozulib/sample/proj/exception_smpl.vcproj +184 -0
  118. data/vendor/cybozulib/sample/proj/mecab_smpl.vcproj +184 -0
  119. data/vendor/cybozulib/sample/proj/ssl_smpl/ssl_smpl.vcxproj +85 -0
  120. data/vendor/cybozulib/sample/proj/ssl_smpl.vcproj +347 -0
  121. data/vendor/cybozulib/sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj +85 -0
  122. data/vendor/cybozulib/sample/proj/svd_smpl.vcproj +184 -0
  123. data/vendor/cybozulib/sample/quit_signal_handler.cpp +30 -0
  124. data/vendor/cybozulib/sample/serializer_smpl.cpp +196 -0
  125. data/vendor/cybozulib/sample/socket_smpl.cpp +82 -0
  126. data/vendor/cybozulib/sample/ssl_smpl.cpp +39 -0
  127. data/vendor/cybozulib/sample/stacktrace_smpl.cpp +52 -0
  128. data/vendor/cybozulib/sample/svd_bench_smpl.cpp +143 -0
  129. data/vendor/cybozulib/sample/svd_smpl.cpp +94 -0
  130. data/vendor/cybozulib/sample/wm_bench_smpl.cpp +182 -0
  131. data/vendor/cybozulib/sample/zlib_smpl.cpp +41 -0
  132. data/vendor/cybozulib/src/Makefile +8 -0
  133. data/vendor/cybozulib/src/base/Makefile +19 -0
  134. data/vendor/cybozulib/test/Makefile +12 -0
  135. data/vendor/cybozulib/test/base/Makefile +37 -0
  136. data/vendor/cybozulib/test/base/array_test.cpp +173 -0
  137. data/vendor/cybozulib/test/base/atoi_test.cpp +774 -0
  138. data/vendor/cybozulib/test/base/atomic_test.cpp +49 -0
  139. data/vendor/cybozulib/test/base/base64_test.cpp +113 -0
  140. data/vendor/cybozulib/test/base/bit_operation_test.cpp +134 -0
  141. data/vendor/cybozulib/test/base/bitvector_test.cpp +204 -0
  142. data/vendor/cybozulib/test/base/condition_variable_cs_test.cpp +92 -0
  143. data/vendor/cybozulib/test/base/condition_variable_test.cpp +88 -0
  144. data/vendor/cybozulib/test/base/config_test.cpp +236 -0
  145. data/vendor/cybozulib/test/base/crypto_test.cpp +122 -0
  146. data/vendor/cybozulib/test/base/csucvector_test.cpp +63 -0
  147. data/vendor/cybozulib/test/base/csv_test.cpp +182 -0
  148. data/vendor/cybozulib/test/base/data/a.xml +26 -0
  149. data/vendor/cybozulib/test/base/endian_test.cpp +56 -0
  150. data/vendor/cybozulib/test/base/env_test.cpp +22 -0
  151. data/vendor/cybozulib/test/base/event_test.cpp +41 -0
  152. data/vendor/cybozulib/test/base/file_test.cpp +233 -0
  153. data/vendor/cybozulib/test/base/fmindex_test.cpp +118 -0
  154. data/vendor/cybozulib/test/base/format_test.cpp +12 -0
  155. data/vendor/cybozulib/test/base/frequency_test.cpp +104 -0
  156. data/vendor/cybozulib/test/base/itoa_test.cpp +522 -0
  157. data/vendor/cybozulib/test/base/line_stream_test.cpp +208 -0
  158. data/vendor/cybozulib/test/base/mecab_test.cpp +41 -0
  159. data/vendor/cybozulib/test/base/minixml_test.cpp +103 -0
  160. data/vendor/cybozulib/test/base/mmap_test.cpp +15 -0
  161. data/vendor/cybozulib/test/base/option_test.cpp +487 -0
  162. data/vendor/cybozulib/test/base/parallel_test.cpp +48 -0
  163. data/vendor/cybozulib/test/base/proj/array_test/array_test.vcxproj +86 -0
  164. data/vendor/cybozulib/test/base/proj/atoi_test/atoi_test.vcxproj +86 -0
  165. data/vendor/cybozulib/test/base/proj/atomic_test/atomic_test.vcxproj +86 -0
  166. data/vendor/cybozulib/test/base/proj/base64_test/base64_test.vcxproj +86 -0
  167. data/vendor/cybozulib/test/base/proj/condition_variable_cs_test/condition_variable_cs_test.vcxproj +86 -0
  168. data/vendor/cybozulib/test/base/proj/condition_variable_test/condition_variable_test.vcxproj +86 -0
  169. data/vendor/cybozulib/test/base/proj/config_test/config_test.vcxproj +86 -0
  170. data/vendor/cybozulib/test/base/proj/csv_test/csv_test.vcxproj +86 -0
  171. data/vendor/cybozulib/test/base/proj/endian_test/endian_test.vcxproj +86 -0
  172. data/vendor/cybozulib/test/base/proj/env_test/env_test.vcxproj +86 -0
  173. data/vendor/cybozulib/test/base/proj/event_test/event_test.vcxproj +86 -0
  174. data/vendor/cybozulib/test/base/proj/file_test/file_test.vcxproj +86 -0
  175. data/vendor/cybozulib/test/base/proj/itoa_test/itoa_test.vcxproj +86 -0
  176. data/vendor/cybozulib/test/base/proj/mecab_test/mecab_test.vcxproj +88 -0
  177. data/vendor/cybozulib/test/base/proj/minixml_test/minixml_test.vcxproj +86 -0
  178. data/vendor/cybozulib/test/base/proj/mmap_test/mmap_test.vcxproj +86 -0
  179. data/vendor/cybozulib/test/base/proj/serializer_test/serializer_test.vcxproj +86 -0
  180. data/vendor/cybozulib/test/base/proj/sha1_test/sha1_test.vcxproj +86 -0
  181. data/vendor/cybozulib/test/base/proj/stream_test/stream_test.vcxproj +86 -0
  182. data/vendor/cybozulib/test/base/proj/string_operation_test/string_operation_test.vcxproj +86 -0
  183. data/vendor/cybozulib/test/base/proj/string_test/string_test.vcxproj +86 -0
  184. data/vendor/cybozulib/test/base/proj/thread_test/thread_test.vcxproj +86 -0
  185. data/vendor/cybozulib/test/base/proj/time_test/time_test.vcxproj +86 -0
  186. data/vendor/cybozulib/test/base/proj/tls_test/tls_test.vcxproj +86 -0
  187. data/vendor/cybozulib/test/base/proj/zlib_test/zlib_test.vcxproj +86 -0
  188. data/vendor/cybozulib/test/base/random_generator_test.cpp +28 -0
  189. data/vendor/cybozulib/test/base/regex_test.cpp +74 -0
  190. data/vendor/cybozulib/test/base/serializer_test.cpp +483 -0
  191. data/vendor/cybozulib/test/base/sha1_test.cpp +61 -0
  192. data/vendor/cybozulib/test/base/sha2_test.cpp +191 -0
  193. data/vendor/cybozulib/test/base/siphash_test.cpp +33 -0
  194. data/vendor/cybozulib/test/base/socket_test.cpp +76 -0
  195. data/vendor/cybozulib/test/base/stream_test.cpp +101 -0
  196. data/vendor/cybozulib/test/base/string_operation_test.cpp +340 -0
  197. data/vendor/cybozulib/test/base/string_test.cpp +1705 -0
  198. data/vendor/cybozulib/test/base/sucvector_test.cpp +312 -0
  199. data/vendor/cybozulib/test/base/thread_test.cpp +62 -0
  200. data/vendor/cybozulib/test/base/time_test.cpp +164 -0
  201. data/vendor/cybozulib/test/base/tls_test.cpp +50 -0
  202. data/vendor/cybozulib/test/base/wavelet_matrix_test.cpp +145 -0
  203. data/vendor/cybozulib/test/base/zlib_test.cpp +371 -0
  204. data/vendor/cybozulib/test/nlp/Makefile +27 -0
  205. data/vendor/cybozulib/test/nlp/proj/random_test.vcproj +184 -0
  206. data/vendor/cybozulib/test/nlp/proj/sparse_test.vcproj +184 -0
  207. data/vendor/cybozulib/test/nlp/proj/svd_test.vcproj +184 -0
  208. data/vendor/cybozulib/test/nlp/random_test.cpp +62 -0
  209. data/vendor/cybozulib/test/nlp/sparse_test.cpp +347 -0
  210. data/vendor/cybozulib/test/nlp/svd_test.cpp +234 -0
  211. data/vendor/cybozulib/test/nlp/top_score_test.cpp +40 -0
  212. data/vendor/cybozulib/tool/create_vcproj.py +186 -0
  213. data/vendor/cybozulib/tool/vcproj_tmpl.py +185 -0
  214. data/vendor/msoffice/COPYRIGHT +27 -0
  215. data/vendor/msoffice/Makefile +29 -0
  216. data/vendor/msoffice/bin/64/msoc.dll +0 -0
  217. data/vendor/msoffice/bin/64/msocsample.exe +0 -0
  218. data/vendor/msoffice/bin/64/msoffice-crypt.exe +0 -0
  219. data/vendor/msoffice/bin/msoc.dll +0 -0
  220. data/vendor/msoffice/bin/msocsample.exe +0 -0
  221. data/vendor/msoffice/bin/msoffice-crypt.exe +0 -0
  222. data/vendor/msoffice/common.mk +71 -0
  223. data/vendor/msoffice/common.props +26 -0
  224. data/vendor/msoffice/debug.props +14 -0
  225. data/vendor/msoffice/include/attack.hpp +211 -0
  226. data/vendor/msoffice/include/cfb.hpp +777 -0
  227. data/vendor/msoffice/include/crypto_util.hpp +450 -0
  228. data/vendor/msoffice/include/custom_sha1.hpp +342 -0
  229. data/vendor/msoffice/include/decode.hpp +240 -0
  230. data/vendor/msoffice/include/encode.hpp +221 -0
  231. data/vendor/msoffice/include/make_dataspace.hpp +316 -0
  232. data/vendor/msoffice/include/msoc.h +129 -0
  233. data/vendor/msoffice/include/resource.hpp +7 -0
  234. data/vendor/msoffice/include/standard_encryption.hpp +145 -0
  235. data/vendor/msoffice/include/uint32vec.hpp +179 -0
  236. data/vendor/msoffice/include/util.hpp +212 -0
  237. data/vendor/msoffice/lib/.emptydir +0 -0
  238. data/vendor/msoffice/misc/decrypt-xls.vbs +46 -0
  239. data/vendor/msoffice/mk.bat +1 -0
  240. data/vendor/msoffice/mkdll.bat +3 -0
  241. data/vendor/msoffice/msoc.def +13 -0
  242. data/vendor/msoffice/msocsample.py +178 -0
  243. data/vendor/msoffice/msoffice12.sln +31 -0
  244. data/vendor/msoffice/readme.md +110 -0
  245. data/vendor/msoffice/release.props +28 -0
  246. data/vendor/msoffice/src/Makefile +19 -0
  247. data/vendor/msoffice/src/attack.cpp +124 -0
  248. data/vendor/msoffice/src/cfb_test.cpp +77 -0
  249. data/vendor/msoffice/src/minisample.c +54 -0
  250. data/vendor/msoffice/src/msocdll.cpp +276 -0
  251. data/vendor/msoffice/src/msocsample.c +136 -0
  252. data/vendor/msoffice/src/msoffice-crypt.cpp +219 -0
  253. data/vendor/msoffice/src/proj/attack/attack.vcxproj +88 -0
  254. data/vendor/msoffice/src/proj/main/msoffice-crypt.vcxproj +88 -0
  255. data/vendor/msoffice/src/sha1.cpp +234 -0
  256. data/vendor/msoffice/test/Makefile +20 -0
  257. data/vendor/msoffice/test/cfb_test.cpp +74 -0
  258. data/vendor/msoffice/test/hash_test.cpp +59 -0
  259. data/vendor/msoffice/test/proj/cfb/cfb_test.vcxproj +90 -0
  260. data/vendor/msoffice/test/proj/hash/hash_test.vcxproj +90 -0
  261. data/vendor/msoffice/test/sampl.bat +8 -0
  262. data/vendor/msoffice/test_all.py +46 -0
  263. data/vendor/update +4 -0
  264. metadata +351 -0
@@ -0,0 +1,347 @@
1
+ #include <stdio.h>
2
+ #include <cybozu/test.hpp>
3
+ #include <cybozu/stream.hpp>
4
+ #include <cybozu/serializer.hpp>
5
+ #include <cybozu/nlp/sparse.hpp>
6
+ #include <map>
7
+
8
+ template<class IntVec1, class IntVec2>
9
+ void test_push_back()
10
+ {
11
+ IntVec1 v1;
12
+ IntVec2 v2;
13
+
14
+ const struct {
15
+ unsigned int pos;
16
+ int val;
17
+ } tbl[] = {
18
+ { 2, 4 }, { 5, 2 }, { 100, 4 }, { 101, 3}, { 999, 4 }, { 1000, 1 }, { 1896, 3 },
19
+ { 2793, 511 }, { 4000, 100 }, { 100000, 999 }, { 116384, 33 },
20
+ { 2397152, 123459 }, { 278435456, 9 }, { 378435456, 9 }, { 1378435456, 9 },
21
+ { 2378435456U, 1234 },
22
+ { 3378435456U, 1234 },
23
+ { 4294967295U, 56789 },
24
+ };
25
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
26
+ v1.push_back(tbl[i].pos, tbl[i].val);
27
+ v2.push_back(tbl[i].pos, tbl[i].val);
28
+ }
29
+ CYBOZU_TEST_EQUAL(v1.size(), CYBOZU_NUM_OF_ARRAY(tbl));
30
+ CYBOZU_TEST_EQUAL(v2.size(), CYBOZU_NUM_OF_ARRAY(tbl));
31
+ int j = 0;
32
+ for (typename IntVec1::const_iterator i = v1.begin(), ie = v1.end(); i != ie; ++i) {
33
+ CYBOZU_TEST_EQUAL(i->pos(), tbl[j].pos);
34
+ CYBOZU_TEST_EQUAL(i->val(), tbl[j].val);
35
+ j++;
36
+ }
37
+ j = 0;
38
+ for (typename IntVec2::const_iterator i = v2.begin(), ie = v2.end(); i != ie; ++i) {
39
+ CYBOZU_TEST_EQUAL(i->pos(), tbl[j].pos);
40
+ CYBOZU_TEST_EQUAL(i->val(), tbl[j].val);
41
+ j++;
42
+ }
43
+ {
44
+ std::stringstream ss;
45
+ cybozu::save(ss, v1);
46
+ IntVec1 out1;
47
+ cybozu::load(out1, ss);
48
+ CYBOZU_TEST_ASSERT(v1 == out1);
49
+ }
50
+ }
51
+
52
+ template<class IntVec>
53
+ void test_empty()
54
+ {
55
+ IntVec v;
56
+ CYBOZU_TEST_EQUAL((int)v.size(), 0);
57
+ CYBOZU_TEST_ASSERT(v.empty());
58
+ for (typename IntVec::const_iterator i = v.begin(); i != v.end(); ++i) {
59
+ printf("%d\n", i->val());
60
+ }
61
+ v.push_back(3, 1);
62
+ CYBOZU_TEST_EQUAL((int)v.size(), 1);
63
+ CYBOZU_TEST_ASSERT(!v.empty());
64
+ for (typename IntVec::const_iterator i = v.begin(); i != v.end(); ++i) {
65
+ CYBOZU_TEST_EQUAL(i->pos(), (size_t)3);
66
+ CYBOZU_TEST_EQUAL(i->val(), 1);
67
+ }
68
+ v.push_back(4, 2);
69
+ CYBOZU_TEST_EQUAL((int)v.size(), 2);
70
+ int j = 0;
71
+ for (typename IntVec::const_iterator i = v.begin(); i != v.end(); ++i) {
72
+ CYBOZU_TEST_EQUAL(i->pos(), size_t(j + 3));
73
+ CYBOZU_TEST_EQUAL(i->val(), j + 1);
74
+ j++;
75
+ }
76
+ v.clear();
77
+ CYBOZU_TEST_EQUAL((int)v.size(), 0);
78
+ CYBOZU_TEST_ASSERT(v.empty());
79
+ IntVec v1, v2;
80
+ for (int i = 0; i < 3; i++) {
81
+ v1.push_back(i, i);
82
+ v2.push_back(i, i);
83
+ }
84
+ CYBOZU_TEST_ASSERT(v1 == v2);
85
+ v2.push_back(10, 10);
86
+ CYBOZU_TEST_ASSERT(v1 != v2);
87
+ }
88
+
89
+ CYBOZU_TEST_AUTO(push_back1)
90
+ {
91
+ typedef cybozu::nlp::SparseVector<int> IntVec1;
92
+ typedef cybozu::nlp::SparseVector<int> IntVec2;
93
+
94
+ test_push_back<IntVec1, IntVec2>();
95
+ test_empty<IntVec1>();
96
+ }
97
+
98
+ CYBOZU_TEST_AUTO(push_back2)
99
+ {
100
+ typedef cybozu::nlp::SparseVector<int, cybozu::nlp::option::CompressedPositionTbl> IntVec1;
101
+ typedef cybozu::nlp::SparseVector<int, cybozu::nlp::option::CompressedPositionTbl> IntVec2;
102
+
103
+ test_push_back<IntVec1, IntVec2>();
104
+ test_empty<IntVec1>();
105
+ }
106
+
107
+ template<class DoubleVec, class IntVec>
108
+ void test_intersect()
109
+ {
110
+ DoubleVec dv;
111
+ IntVec iv;
112
+
113
+ const struct {
114
+ unsigned int pos;
115
+ double val;
116
+ } dTbl[] = {
117
+ { 5, 3.14 }, { 10, 2 }, { 12, 1.4 }, { 100, 2.3 }, { 1000, 4 },
118
+ };
119
+
120
+ const struct {
121
+ unsigned int pos;
122
+ int val;
123
+ } iTbl[] = {
124
+ { 2, 4 }, { 5, 2 }, { 100, 4 }, { 101, 3}, { 999, 4 }, { 1000, 1 }, { 2000, 3 },
125
+ };
126
+
127
+ const struct {
128
+ unsigned int pos;
129
+ double d;
130
+ int i;
131
+ } interTbl[] = {
132
+ { 5, 3.14, 2 }, { 100, 2.3, 4 }, { 1000, 4, 1 }
133
+ };
134
+
135
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(dTbl); i++) {
136
+ dv.push_back(dTbl[i].pos, dTbl[i].val);
137
+ }
138
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(iTbl); i++) {
139
+ iv.push_back(iTbl[i].pos, iTbl[i].val);
140
+ }
141
+
142
+ int j = 0;
143
+ for (typename DoubleVec::const_iterator i = dv.begin(), ie = dv.end(); i != ie; ++i) {
144
+ CYBOZU_TEST_EQUAL(i->pos(), dTbl[j].pos);
145
+ CYBOZU_TEST_EQUAL(i->val(), dTbl[j].val);
146
+ j++;
147
+ }
148
+
149
+ j = 0;
150
+ for (typename IntVec::const_iterator i = iv.begin(), ie = iv.end(); i != ie; ++i) {
151
+ CYBOZU_TEST_EQUAL(i->pos(), iTbl[j].pos);
152
+ CYBOZU_TEST_EQUAL(i->val(), iTbl[j].val);
153
+ j++;
154
+ }
155
+
156
+ int sum = 0;
157
+ for (typename IntVec::const_iterator i = iv.begin(), ie = iv.end(); i != ie; ++i) {
158
+ sum += i->val() * i->val();
159
+ }
160
+ CYBOZU_TEST_EQUAL(sum, 71);
161
+
162
+ typedef cybozu::nlp::Intersection<DoubleVec, IntVec> InterSection;
163
+ InterSection inter(dv, iv);
164
+
165
+ j = 0;
166
+ for (typename InterSection::const_iterator i = inter.begin(), ie = inter.end(); i != ie; ++i) {
167
+ CYBOZU_TEST_EQUAL(i->pos(), interTbl[j].pos);
168
+ CYBOZU_TEST_EQUAL(i->val1(), interTbl[j].d);
169
+ CYBOZU_TEST_EQUAL(i->val2(), interTbl[j].i);
170
+ j++;
171
+ }
172
+ }
173
+
174
+ CYBOZU_TEST_AUTO(intersect1)
175
+ {
176
+ typedef cybozu::nlp::SparseVector<double> DoubleVec;
177
+ typedef cybozu::nlp::SparseVector<int> IntVec;
178
+ test_intersect<DoubleVec, IntVec>();
179
+ }
180
+
181
+ CYBOZU_TEST_AUTO(intersect2)
182
+ {
183
+ typedef cybozu::nlp::SparseVector<double, cybozu::nlp::option::CompressedPositionTbl> DoubleVec;
184
+ typedef cybozu::nlp::SparseVector<int, cybozu::nlp::option::CompressedPositionTbl> IntVec;
185
+ test_intersect<DoubleVec, IntVec>();
186
+ }
187
+
188
+ CYBOZU_TEST_AUTO(unionTest)
189
+ {
190
+ typedef cybozu::nlp::SparseVector<int> IntVec;
191
+ typedef cybozu::nlp::Union<IntVec, IntVec> Uni;
192
+ struct V {
193
+ struct {
194
+ size_t pos;
195
+ int v1;
196
+ int v2;
197
+ } a[10];
198
+ size_t n;
199
+ };
200
+ const struct Elem {
201
+ V x;
202
+ V y;
203
+ V z;
204
+ } tbl[] = {
205
+ {
206
+ { {}, 0 },
207
+ { { { 0, 1, 0 }, { 2, 4, 0 }, { 3, 5, 0 } }, 3 },
208
+ { { { 0, 0, 1 }, { 2, 0, 4 }, { 3, 0, 5 } }, 3 },
209
+ },
210
+ {
211
+ { { { 0, 1, 0 }, { 2, 4, 0 }, { 3, 5, 0 } }, 3 },
212
+ { {}, 0 },
213
+ { { { 0, 1, 0 }, { 2, 4, 0 }, { 3, 5, 0 } }, 3 },
214
+ },
215
+ {
216
+ { { { 1, 2, 0 }, { 2, 5, 0 }, { 9, 4, 0 } }, 3 },
217
+ { { { 0, 1, 0 }, { 2, 4, 0 }, { 3, 5, 0 } }, 3 },
218
+ { { { 0, 0, 1 }, { 1, 2, 0 }, { 2, 5, 4 }, { 3, 0, 5 }, { 9, 4, 0 } }, 5 },
219
+ },
220
+ };
221
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
222
+ const Elem& e = tbl[i];
223
+ IntVec x, y;
224
+ for (size_t j = 0; j < e.x.n; j++) {
225
+ x.push_back(e.x.a[j].pos, e.x.a[j].v1);
226
+ }
227
+ for (size_t j = 0; j < e.y.n; j++) {
228
+ y.push_back(e.y.a[j].pos, e.y.a[j].v1);
229
+ }
230
+ Uni uni(x, y);
231
+ size_t n = 0;
232
+ for (Uni::const_iterator j = uni.begin(), je = uni.end(); j != je; ++j) {
233
+ CYBOZU_TEST_EQUAL(j->val1(), e.z.a[n].v1);
234
+ CYBOZU_TEST_EQUAL(j->val2(), e.z.a[n].v2);
235
+ CYBOZU_TEST_EQUAL(j->pos(), e.z.a[n].pos);
236
+ n++;
237
+ }
238
+ CYBOZU_TEST_EQUAL(n, e.z.n);
239
+ }
240
+ }
241
+
242
+ CYBOZU_TEST_AUTO(map)
243
+ {
244
+ const struct {
245
+ size_t pos;
246
+ int val;
247
+ } tbl[] = {
248
+ { 1, 100 }, { 200, 5 }, { 3, 123 }, { 9, 1 },
249
+ };
250
+ typedef std::map<size_t, int> Map;
251
+ Map m;
252
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
253
+ m[tbl[i].pos] = tbl[i].val;
254
+ }
255
+ typedef cybozu::nlp::SparseVector<int> IntVec;
256
+ IntVec v;
257
+ v.set(m);
258
+ CYBOZU_TEST_EQUAL(m.size(), v.size());
259
+ Map::const_iterator mi = m.begin(), mie = m.end();
260
+ IntVec::const_iterator vi = v.begin();
261
+ for (; mi != mie; ++mi, ++vi) {
262
+ CYBOZU_TEST_EQUAL(mi->first, vi->pos());
263
+ CYBOZU_TEST_EQUAL(mi->second, vi->val());
264
+ }
265
+ }
266
+
267
+ CYBOZU_TEST_AUTO(innerProduct)
268
+ {
269
+ const struct {
270
+ size_t pos;
271
+ int val;
272
+ } Ltbl[] = {
273
+ { 1, 100 }, { 10, 5 }, { 20, 2 }, { 30, 4 },
274
+ };
275
+
276
+ const struct {
277
+ size_t pos;
278
+ double val;
279
+ } Rtbl[] = {
280
+ { 2, 1.5 }, { 10, 0.1 }, { 11, 3 }, { 30, 2 }, { 40, 1 },
281
+ };
282
+
283
+ {
284
+ cybozu::nlp::SparseVector<int> lhs;
285
+ cybozu::nlp::SparseVector<double, cybozu::nlp::option::CompressedPositionTbl> rhs;
286
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(Ltbl); i++) {
287
+ lhs.push_back(Ltbl[i].pos, Ltbl[i].val);
288
+ }
289
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(Rtbl); i++) {
290
+ rhs.push_back(Rtbl[i].pos, Rtbl[i].val);
291
+ }
292
+ {
293
+ int a;
294
+ cybozu::nlp::InnerProduct(&a, lhs, lhs);
295
+ CYBOZU_TEST_EQUAL(a, 10000 + 25 + 4 + 16);
296
+ }
297
+ {
298
+ double b;
299
+ cybozu::nlp::InnerProduct(&b, rhs, rhs);
300
+ CYBOZU_TEST_EQUAL(b, 2.25 + 0.01 + 9 + 4 + 1);
301
+ }
302
+ {
303
+ double c;
304
+ cybozu::nlp::InnerProduct(&c, lhs, rhs);
305
+ CYBOZU_TEST_EQUAL(c, 8.5);
306
+ }
307
+ {
308
+ double d;
309
+ cybozu::nlp::InnerProduct(&d, rhs, lhs);
310
+ CYBOZU_TEST_EQUAL(d, 8.5);
311
+ }
312
+ }
313
+
314
+ {
315
+ typedef cybozu::nlp::SparseVector<int> Lhs;
316
+ typedef cybozu::nlp::SparseVector<double, cybozu::nlp::option::CompressedPositionTbl> Rhs;
317
+ Lhs lhs;
318
+ Rhs rhs;
319
+
320
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(Ltbl); i++) {
321
+ lhs.push_back(Ltbl[i].pos, Ltbl[i].val);
322
+ }
323
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(Rtbl); i++) {
324
+ rhs.push_back(Rtbl[i].pos, Rtbl[i].val);
325
+ }
326
+ {
327
+ int a;
328
+ cybozu::nlp::InnerProduct(&a, lhs, lhs);
329
+ CYBOZU_TEST_EQUAL(a, 10000 + 25 + 4 + 16);
330
+ }
331
+ {
332
+ double b;
333
+ cybozu::nlp::InnerProduct(&b, rhs, rhs);
334
+ CYBOZU_TEST_EQUAL(b, 2.25 + 0.01 + 9 + 4 + 1);
335
+ }
336
+ {
337
+ double c;
338
+ cybozu::nlp::InnerProduct(&c, lhs, rhs);
339
+ CYBOZU_TEST_EQUAL(c, 8.5);
340
+ }
341
+ {
342
+ double d;
343
+ cybozu::nlp::InnerProduct(&d, rhs, lhs);
344
+ CYBOZU_TEST_EQUAL(d, 8.5);
345
+ }
346
+ }
347
+ }
@@ -0,0 +1,234 @@
1
+ /*
2
+ A = U S t(V)
3
+ t(U) U = I
4
+ t(V) V = I
5
+ */
6
+ #include <cybozu/test.hpp>
7
+ #define CYBOZU_NLP_SVD_USE_RANDOM
8
+ #include <cybozu/nlp/svd.hpp>
9
+ #include <cybozu/file.hpp>
10
+
11
+ const double defaultEPS = 1e-5;
12
+ std::string dataPath;
13
+ std::string testName;
14
+ struct InitPath {
15
+ InitPath()
16
+ {
17
+ dataPath = cybozu::GetExePath();
18
+ size_t pos = dataPath.find("cybozulib");
19
+ if (pos == std::string::npos) {
20
+ puts("InitPath err");
21
+ exit(1);
22
+ }
23
+ dataPath = dataPath.substr(0, pos + 9) + "/bin/";
24
+ testName = dataPath + "/svd_test.txt";
25
+ }
26
+ };
27
+
28
+ CYBOZU_TEST_SETUP_FIXTURE(InitPath);
29
+
30
+ template<class Matrix>
31
+ void testSame(const Matrix& A, const Matrix& B)
32
+ {
33
+ double x = (A - B).norm();
34
+ CYBOZU_TEST_NEAR(x, 0, defaultEPS);
35
+ }
36
+
37
+ template<class Matrix, class Vector>
38
+ void testUSV(const Matrix& U, const Vector& S, const Matrix& V, const Matrix& A)
39
+ {
40
+ const Matrix B = U * S.asDiagonal() * V.transpose();
41
+ testSame(A, B);
42
+ }
43
+ /*
44
+ verity t(A) A = I
45
+ */
46
+ template<class Matrix>
47
+ void testUnitary(const Matrix& A)
48
+ {
49
+ Matrix B = A.transpose() * A;
50
+ Matrix I;
51
+ I.setIdentity(B.cols(), B.cols());
52
+ testSame(B, I);
53
+ }
54
+
55
+ template<class Matrix>
56
+ double innerProductRow(const Matrix& A, int s, int t)
57
+ {
58
+ double ret = 0;
59
+ for (int i = 0; i < A.cols(); i++) {
60
+ ret += A(s, i) * A(t, i);
61
+ }
62
+ return ret;
63
+ }
64
+
65
+ template<class Matrix, class Vector>
66
+ double innerProductUS(const Matrix& U, const Vector& S, int s, int t)
67
+ {
68
+ double ret = 0;
69
+ for (int i = 0; i < U.cols(); i++) {
70
+ double v = S(i);
71
+ ret += v * v * U(s, i) * U(t, i);
72
+ }
73
+ return ret;
74
+ }
75
+
76
+ /*
77
+ A = U S trans(V)
78
+ m = A.rows()
79
+ A = trans(a_1, ..., a_m), U = trans(u_1, ..., u_m)
80
+ product(a_s, a_t) = sum_{i=0}^{U.cols()} S(i)^2 * (u_s)_i (u_t)_i
81
+ */
82
+ template<class Matrix, class Vector>
83
+ void testProduct(const Matrix& A, const Matrix& U, const Vector& S)
84
+ {
85
+ for (int s = 0; s < U.rows(); s++) {
86
+ for (int t = s; t < U.rows(); t++) {
87
+ double x = innerProductRow(A, s, t);
88
+ double y = innerProductUS(U, S, s, t);
89
+ CYBOZU_TEST_NEAR(x, y, defaultEPS);
90
+ }
91
+ }
92
+ }
93
+
94
+ template<class Matrix>
95
+ void testSVD(const Matrix& A, int m, int n, int r)
96
+ {
97
+ Matrix U, V;
98
+ Eigen::VectorXd S;
99
+
100
+ cybozu::nlp::ComputeSVD(U, S, V, A, r);
101
+
102
+ CYBOZU_TEST_EQUAL(U.rows(), m);
103
+ CYBOZU_TEST_EQUAL(U.cols(), r);
104
+ CYBOZU_TEST_EQUAL(S.rows(), r);
105
+ CYBOZU_TEST_EQUAL(V.rows(), n);
106
+ CYBOZU_TEST_EQUAL(V.cols(), r);
107
+
108
+ testUnitary(U);
109
+ testUnitary(V);
110
+
111
+ testUSV(U, S, V, A);
112
+ testProduct(A, U, S);
113
+ }
114
+
115
+ CYBOZU_TEST_AUTO(rank2)
116
+ {
117
+ const int m = 4;
118
+ const int n = 3;
119
+ const int r = 2;
120
+ Eigen::MatrixXd A(m, n);
121
+ A << 1, 2, 3, 4,
122
+ 5, 6, 7, 8,
123
+ 9, 10, 11, 12;
124
+
125
+ testSVD(A, m, n, r);
126
+ }
127
+
128
+ CYBOZU_TEST_AUTO(rank3)
129
+ {
130
+ const int m = 4;
131
+ const int n = 3;
132
+ const int r = 3;
133
+ Eigen::MatrixXd A(m, n);
134
+ A << 1, 2, 3, 4,
135
+ 5, 6, 7, 8,
136
+ 9, 10, 11, 13;
137
+
138
+ testSVD(A, m, n, r);
139
+ }
140
+
141
+ CYBOZU_TEST_AUTO(file)
142
+ {
143
+ const int m = 3;
144
+ const int n = 4;
145
+ Eigen::MatrixXd A(m, n);
146
+ cybozu::nlp::svd::InitRandomMatrix(A);
147
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::SaveMatrix(testName, A));
148
+ Eigen::MatrixXd B;
149
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::LoadMatrix(B, testName));
150
+ testSame(A, B);
151
+ }
152
+
153
+ CYBOZU_TEST_AUTO(file2)
154
+ {
155
+ const std::string path = dataPath + "../sample/data/svd/";
156
+ Eigen::MatrixXd A;
157
+ Eigen::MatrixXd B;
158
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::LoadMatrix(A, path + "test1"));
159
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::LoadMatrix(B, path + "test2"));
160
+ testSame(A, B);
161
+ }
162
+
163
+ CYBOZU_TEST_AUTO(sparse)
164
+ {
165
+ const std::string path = dataPath + "../sample/data/svd/";
166
+ Eigen::SparseMatrix<float, Eigen::RowMajor> A;
167
+ Eigen::SparseMatrix<float, Eigen::RowMajor> B;
168
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::LoadSparseMatrix(A, path + "test2"));
169
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::SaveSparseMatrix(testName, A));
170
+ CYBOZU_TEST_ASSERT(cybozu::nlp::svd::LoadSparseMatrix(B, testName));
171
+ testSame(A, B);
172
+ }
173
+
174
+ CYBOZU_TEST_AUTO(random1)
175
+ {
176
+ const int m = 10;
177
+ const int n = 30;
178
+ const int r = 10;
179
+ Eigen::MatrixXd A(m, n);
180
+ cybozu::nlp::svd::InitRandomMatrix(A);
181
+ testSVD(A, m, n, r);
182
+ }
183
+
184
+ CYBOZU_TEST_AUTO(random2)
185
+ {
186
+ const int m = 10;
187
+ const int n = 30;
188
+ const int r = 8;
189
+ Eigen::MatrixXd A(m, n), U(m, r), V(n, r), U2, V2;
190
+ Eigen::VectorXd S(r), S2;
191
+ cybozu::nlp::svd::InitRandomMatrix(U);
192
+ cybozu::nlp::svd::OrthonormalizeMatrix(U);
193
+ cybozu::nlp::svd::InitRandomMatrix(V);
194
+ cybozu::nlp::svd::OrthonormalizeMatrix(V);
195
+ // descending order
196
+ for (int i = 0; i < r; i++) {
197
+ S(i) = r - i;
198
+ }
199
+ A = U * S.asDiagonal() * V.transpose();
200
+ cybozu::nlp::ComputeSVD(U2, S2, V2, A, r);
201
+ testUnitary(U2);
202
+ testUnitary(V2);
203
+ testSame(S, S2);
204
+ testUSV(U2, S2, V2, A);
205
+ }
206
+
207
+ CYBOZU_TEST_AUTO(multiUnitMatrix)
208
+ {
209
+ int row = 13;
210
+ int col = 15;
211
+ int r = 4;
212
+ Eigen::MatrixXd R(col, r);
213
+ cybozu::nlp::svd::InitUnitMatrix(R);
214
+ Eigen::MatrixXd A(row, col), B;
215
+ cybozu::nlp::svd::InitRandomMatrix(A);
216
+ cybozu::nlp::svd::CompressCol(B, A, r);
217
+ double diff = ((A * R) - B).norm();
218
+ CYBOZU_TEST_NEAR(diff, 0, defaultEPS);
219
+ }
220
+
221
+ CYBOZU_TEST_AUTO(multiUnitMatrix2)
222
+ {
223
+ int row = 13;
224
+ int col = 15;
225
+ int r = 5;
226
+ Eigen::MatrixXd R(col, r);
227
+ cybozu::nlp::svd::InitUnitMatrix(R);
228
+ Eigen::MatrixXd A(row, col), B;
229
+ cybozu::nlp::svd::InitRandomMatrix(A);
230
+ cybozu::nlp::svd::CompressCol(B, A, r);
231
+ double diff = ((A * R) - B).norm();
232
+ CYBOZU_TEST_NEAR(diff, 0, defaultEPS);
233
+ }
234
+
@@ -0,0 +1,40 @@
1
+ #include <cybozu/test.hpp>
2
+ #include <cybozu/nlp/top_score.hpp>
3
+ #include <cybozu/file.hpp>
4
+ #include <cybozu/nlp/random.hpp>
5
+ #include <algorithm>
6
+
7
+ struct Score {
8
+ double v_;
9
+ int pos_;
10
+ Score(double v = 0, int pos = 0)
11
+ : v_(v)
12
+ , pos_(pos)
13
+ {
14
+ }
15
+ bool operator<(const Score& rhs) const
16
+ {
17
+ return v_ > rhs.v_;
18
+ }
19
+ };
20
+
21
+ CYBOZU_TEST_AUTO(top_score)
22
+ {
23
+ cybozu::nlp::UniformRandomGenerator r;
24
+ std::vector<Score> vs;
25
+ const int N = 100;
26
+ const int max = 13;
27
+ cybozu::nlp::TopScore<int> ts(max);
28
+ for (int i = 0; i < N; i++) {
29
+ double v = r.getDouble();
30
+ vs.push_back(Score(v, i));
31
+ ts.add(v, i);
32
+ }
33
+ std::sort(vs.begin(), vs.end());
34
+ const cybozu::nlp::TopScore<int>::Table ret = ts.getTable();
35
+ for (int i = 0; i < max; i++) {
36
+ CYBOZU_TEST_EQUAL(vs[i].v_, ret[i].score);
37
+ CYBOZU_TEST_EQUAL(vs[i].pos_, ret[i].idx);
38
+ }
39
+ }
40
+