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,49 @@
1
+ #include <cybozu/atomic.hpp>
2
+ #include <cybozu/test.hpp>
3
+
4
+ CYBOZU_TEST_AUTO(atomic_not_in_thread)
5
+ {
6
+ int a, b, c;
7
+ int64_t ia, ib;
8
+
9
+ a = 2, b = 5;
10
+ cybozu::AtomicAdd(&a, b);
11
+ CYBOZU_TEST_EQUAL(a, 7);
12
+
13
+ #if (CYBOZU_OS_BIT == 64)
14
+ ia = 4, ib = 9;
15
+ cybozu::AtomicAdd(&ia, ib);
16
+ CYBOZU_TEST_EQUAL(ia, 13);
17
+ #endif
18
+
19
+ a = 4;
20
+ c = cybozu::AtomicCompareExchange(&a, 9, 4);
21
+ CYBOZU_TEST_EQUAL(c, 4);
22
+ CYBOZU_TEST_EQUAL(a, 9);
23
+
24
+ a = 4;
25
+ c = cybozu::AtomicCompareExchange(&a, 9, 5);
26
+ CYBOZU_TEST_EQUAL(c, 4);
27
+ CYBOZU_TEST_EQUAL(a, 4);
28
+
29
+ ia = 10;
30
+ ib = cybozu::AtomicCompareExchange<int64_t>(&ia, 0x123456789012LL, 10);
31
+ CYBOZU_TEST_EQUAL(ib, 10);
32
+ CYBOZU_TEST_EQUAL(ia, 0x123456789012LL);
33
+
34
+ ia = 10;
35
+ ib = cybozu::AtomicCompareExchange<int64_t>(&ia, 0x123456789012LL, 11);
36
+ CYBOZU_TEST_EQUAL(ib, 10);
37
+ CYBOZU_TEST_EQUAL(ia, 10);
38
+
39
+ a = 3;
40
+ b = cybozu::AtomicExchange(&a, 4);
41
+ CYBOZU_TEST_EQUAL(b, 3);
42
+ CYBOZU_TEST_EQUAL(a, 4);
43
+
44
+ size_t ua, ub;
45
+ ua = 5;
46
+ ub = cybozu::AtomicExchange<size_t>(&ua, 4);
47
+ CYBOZU_TEST_EQUAL(ua, 4ULL);
48
+ CYBOZU_TEST_EQUAL(ub, 5ULL);
49
+ }
@@ -0,0 +1,113 @@
1
+ #include <cybozu/test.hpp>
2
+ #include <cybozu/base64.hpp>
3
+ #include <string>
4
+
5
+ CYBOZU_TEST_AUTO(base64)
6
+ {
7
+ const struct {
8
+ std::string in;
9
+ std::string out;
10
+ size_t maxLineSize;
11
+ } tbl[] = {
12
+ { "", "", 76 },
13
+ { "1", "MQ==\r\n", 8 },
14
+ { "12", "MTI=\r\n", 8 },
15
+ { "123", "MTIz\r\n", 8 },
16
+ { "1234", "MTIzNA==\r\n", 8 },
17
+ { "12345", "MTIzNDU=\r\n", 8 },
18
+ { "123456", "MTIzNDU2\r\n", 8 },
19
+ { "1234567", "MTIzNDU2\r\nNw==\r\n", 8 },
20
+ { "12345678", "MTIzNDU2\r\nNzg=\r\n", 8 },
21
+ { "123456789", "MTIzNDU2\r\nNzg5\r\n", 8 },
22
+ { "123456789a", "MTIzNDU2\r\nNzg5YQ==\r\n", 8 },
23
+ { "123456789ab", "MTIzNDU2\r\nNzg5YWI=\r\n", 8 },
24
+ { "123456789abc", "MTIzNDU2\r\nNzg5YWJj\r\n", 8 },
25
+ { "123456789abcd", "MTIzNDU2\r\nNzg5YWJj\r\nZA==\r\n", 8 },
26
+
27
+ { "1", "MQ==", 0 },
28
+ { "12", "MTI=", 0 },
29
+ { "123", "MTIz", 0 },
30
+ { "1234", "MTIzNA==", 0 },
31
+ { "12345", "MTIzNDU=", 0 },
32
+ { "123456", "MTIzNDU2", 0 },
33
+ { "1234567", "MTIzNDU2Nw==", 0 },
34
+ { "12345678", "MTIzNDU2Nzg=", 0 },
35
+ { "123456789", "MTIzNDU2Nzg5", 0 },
36
+ { "123456789a", "MTIzNDU2Nzg5YQ==", 0 },
37
+ { "123456789ab", "MTIzNDU2Nzg5YWI=", 0 },
38
+ { "123456789abc", "MTIzNDU2Nzg5YWJj", 0 },
39
+ { "123456789abcd", "MTIzNDU2Nzg5YWJjZA==", 0 },
40
+
41
+ { "123456789012345678901234567890123456789012345678901234567",
42
+ "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3\r\n", 76 },
43
+ { "123456789012345678901234567890123456789012345678901234567"
44
+ "890123456789012345678901234567890123456789012345678901234"
45
+ "567890123456789012345678901234567890123456789012345678901"
46
+ "23456789012345678901234567890",
47
+ "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3\r\n"
48
+ "ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0\r\n"
49
+ "NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAx\r\n"
50
+ "MjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTA=\r\n", 76},
51
+ };
52
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
53
+ cybozu::StringInputStream is(tbl[i].in);
54
+ std::string str;
55
+ cybozu::StringOutputStream os(str);
56
+ cybozu::EncodeToBase64(os, is, tbl[i].maxLineSize);
57
+ CYBOZU_TEST_EQUAL(str.size(), tbl[i].out.size());
58
+ CYBOZU_TEST_EQUAL(str, tbl[i].out);
59
+ }
60
+
61
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
62
+ cybozu::StringInputStream is(tbl[i].out);
63
+ std::string str;
64
+ cybozu::StringOutputStream os(str);
65
+ cybozu::DecodeFromBase64(os, is);
66
+ CYBOZU_TEST_EQUAL(str.size(), tbl[i].in.size());
67
+ CYBOZU_TEST_EQUAL(str, tbl[i].in);
68
+ }
69
+ }
70
+
71
+ CYBOZU_TEST_AUTO(decodeSkip)
72
+ {
73
+ const struct {
74
+ std::string in;
75
+ std::string out;
76
+ } tbl[] = {
77
+ { "123456789a", "MTIzNDU2\r\n$$Nz%%~~!!g5YQ==\r\n" },
78
+ { "123456789ab", "MTIzNDU>>2<<\r\nNzg5YWI=!!\r\n" },
79
+
80
+ { "123", "MTIz" },
81
+ { "1234", "MT I z NA==" },
82
+ { "12345", "MTIzNDU=" },
83
+ { "123456", "MTIzNDU2" },
84
+ };
85
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
86
+ cybozu::StringInputStream is(tbl[i].out);
87
+ std::string str;
88
+ cybozu::StringOutputStream os(str);
89
+ cybozu::DecodeFromBase64(os, is);
90
+ CYBOZU_TEST_EQUAL(str.size(), tbl[i].in.size());
91
+ CYBOZU_TEST_EQUAL(str, tbl[i].in);
92
+ }
93
+ }
94
+
95
+ CYBOZU_TEST_AUTO(ignoreAllEqual)
96
+ {
97
+ /* we verify the number of '=' */
98
+ const struct {
99
+ std::string in;
100
+ std::string out;
101
+ } tbl[] = {
102
+ { "123", "M=T===Iz=" },
103
+ { "123", "M====TIz==" },
104
+ { "123", "MT===Iz===" },
105
+ };
106
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
107
+ cybozu::StringInputStream is(tbl[i].out);
108
+ std::string str;
109
+ cybozu::StringOutputStream os(str);
110
+ cybozu::DecodeFromBase64(os, is);
111
+ CYBOZU_TEST_EQUAL(str, tbl[i].in);
112
+ }
113
+ }
@@ -0,0 +1,134 @@
1
+ #include <cybozu/test.hpp>
2
+ #include <cybozu/bit_operation.hpp>
3
+
4
+ CYBOZU_TEST_AUTO(bsf)
5
+ {
6
+ const struct {
7
+ uint32_t x;
8
+ int val;
9
+ } tbl[] = {
10
+ { 1, 0 },
11
+ { 2, 1 },
12
+ { 3, 0 },
13
+ { 4, 2 },
14
+ { 0xffffffff, 0 },
15
+ { 0x80000000, 31 },
16
+ { 0x80000010, 4 },
17
+ };
18
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
19
+ CYBOZU_TEST_EQUAL(cybozu::bsf(tbl[i].x), tbl[i].val);
20
+ }
21
+ }
22
+
23
+ CYBOZU_TEST_AUTO(bsr)
24
+ {
25
+ const struct {
26
+ uint32_t x;
27
+ int val;
28
+ } tbl[] = {
29
+ { 1, 0 },
30
+ { 2, 1 },
31
+ { 3, 1 },
32
+ { 4, 2 },
33
+ { 0xfff, 11 },
34
+ { 0xfffff, 19 },
35
+ { 0xffffffff, 31 },
36
+ { 0x80000000, 31 },
37
+ { 0x80000010, 31 },
38
+ };
39
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
40
+ CYBOZU_TEST_EQUAL(cybozu::bsr(tbl[i].x), tbl[i].val);
41
+ }
42
+ }
43
+
44
+ CYBOZU_TEST_AUTO(bsf64)
45
+ {
46
+ const struct {
47
+ uint64_t x;
48
+ int val;
49
+ } tbl[] = {
50
+ { 1, 0 },
51
+ { 2, 1 },
52
+ { 3, 0 },
53
+ { 4, 2 },
54
+ { 0xffffffff, 0 },
55
+ { 0x80000000, 31 },
56
+ { 0x100000000ULL, 32 },
57
+ { 0xffffffffffffffffULL, 0 },
58
+ { 0x8000000000000000ULL, 63 },
59
+ { 0x8000000000000010ULL, 4 },
60
+ { 0x8000001000000000ULL, 36 },
61
+ };
62
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
63
+ CYBOZU_TEST_EQUAL(cybozu::bsf(tbl[i].x), tbl[i].val);
64
+ }
65
+ }
66
+
67
+ CYBOZU_TEST_AUTO(bsr64)
68
+ {
69
+ const struct {
70
+ uint64_t x;
71
+ int val;
72
+ } tbl[] = {
73
+ { 1, 0 },
74
+ { 2, 1 },
75
+ { 3, 1 },
76
+ { 4, 2 },
77
+ { 0xfff, 11 },
78
+ { 0xfffff, 19 },
79
+ { 0xffffffff, 31 },
80
+ { 0x80000000, 31 },
81
+ { 0x100000000ULL, 32 },
82
+ { 0x1100000000ULL, 36 },
83
+ { 0xffffffffffffffffULL, 63 },
84
+ { 0x8000000000000000ULL, 63 },
85
+ { 0x8000001000000000ULL, 63 },
86
+ };
87
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
88
+ CYBOZU_TEST_EQUAL(cybozu::bsr(tbl[i].x), tbl[i].val);
89
+ }
90
+ }
91
+
92
+ CYBOZU_TEST_AUTO(popcnt)
93
+ {
94
+ const struct {
95
+ uint32_t x;
96
+ uint32_t val;
97
+ } tbl[] = {
98
+ { 0, 0 },
99
+ { 1, 1 },
100
+ { 2, 1 },
101
+ { 3, 2 },
102
+ { 4, 1 },
103
+ { 5, 2 },
104
+ { 0xfff, 12 },
105
+ { 0xf0000000, 4 },
106
+ { 0xffffffff, 32 },
107
+ };
108
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
109
+ CYBOZU_TEST_EQUAL(cybozu::popcnt(tbl[i].x), tbl[i].val);
110
+ }
111
+ }
112
+
113
+ CYBOZU_TEST_AUTO(popcnt64)
114
+ {
115
+ const struct {
116
+ uint64_t x;
117
+ uint32_t val;
118
+ } tbl[] = {
119
+ { 0, 0 },
120
+ { 1, 1 },
121
+ { 2, 1 },
122
+ { 3, 2 },
123
+ { 4, 1 },
124
+ { 5, 2 },
125
+ { 0xfff, 12 },
126
+ { 0xffffffff, 32 },
127
+ { 0xfffffffffull, 36 },
128
+ { 0xf000000000000000ull, 4 },
129
+ { 0xffffffffffffffffull, 64 },
130
+ };
131
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
132
+ CYBOZU_TEST_EQUAL(cybozu::popcnt(tbl[i].x), tbl[i].val);
133
+ }
134
+ }
@@ -0,0 +1,204 @@
1
+ #include <cybozu/test.hpp>
2
+ #include <cybozu/bitvector.hpp>
3
+ #include <cybozu/xorshift.hpp>
4
+
5
+ CYBOZU_TEST_AUTO(set)
6
+ {
7
+ cybozu::BitVector cv;
8
+ std::vector<bool> sv;
9
+ cybozu::XorShift rg;
10
+ const size_t size = 100;
11
+ cv.resize(size);
12
+ sv.resize(size);
13
+ for (int j = 0; j < 2; j++) {
14
+ for (size_t i = 0; i < size; i++) {
15
+ bool b = (rg.get32() & 1) != 0;
16
+ cv.set(i, b);
17
+ sv[i] = b;
18
+ }
19
+ for (size_t i = 0; i < size; i++) {
20
+ CYBOZU_TEST_EQUAL(cv.get(i), sv[i]);
21
+ }
22
+ }
23
+ }
24
+
25
+ CYBOZU_TEST_AUTO(resize)
26
+ {
27
+ uint16_t x[] = { 0x1234, 0x5678, 0x9abc };
28
+ cybozu::BitVectorT<uint16_t> v;
29
+ v.append(x, 48);
30
+ CYBOZU_TEST_EQUAL(v.size(), 48);
31
+ CYBOZU_TEST_EQUAL(v.getBlock()[2], x[2]);
32
+ uint16_t val = x[2];
33
+ for (size_t i = 47; i >= 33; i--) {
34
+ v.resize(i);
35
+ CYBOZU_TEST_EQUAL(v.size(), i);
36
+ CYBOZU_TEST_EQUAL(v.getBlock()[2], val & cybozu::GetMaskBit<uint16_t>(i - 32));
37
+ }
38
+ }
39
+
40
+ struct StdVec : std::vector<bool> {
41
+ template<class T>
42
+ void append(const T*src, size_t bitLen)
43
+ {
44
+ for (size_t i = 0; i < bitLen; i++) {
45
+ bool b = cybozu::GetBlockBit(src, i);
46
+ push_back(b);
47
+ }
48
+ }
49
+ void append(size_t src, size_t bitLen)
50
+ {
51
+ for (size_t i = 0; i < bitLen; i++) {
52
+ bool b = (src & (size_t(1) << i)) != 0;
53
+ push_back(b);
54
+ }
55
+ }
56
+ };
57
+
58
+ template<class T>
59
+ void verifyVec(const T& v1, const StdVec& v2)
60
+ {
61
+ CYBOZU_TEST_EQUAL(v1.size(), v2.size());
62
+ int sum = 0;
63
+ for (size_t i = 0; i < v1.size(); i++) {
64
+ sum += v1.get(i) ^ v2[i];
65
+ }
66
+ CYBOZU_TEST_EQUAL(sum, 0);
67
+ }
68
+
69
+ CYBOZU_TEST_AUTO(shiftLeftBit)
70
+ {
71
+ const struct {
72
+ uint32_t x[4];
73
+ size_t bitLen;
74
+ size_t shift;
75
+ uint32_t z0;
76
+ uint32_t z[5];
77
+ } tbl[] = {
78
+ { { 1, 0, 0, 0 }, 1, 1, 0xfffffff, { 3, 0, 0, 0 } },
79
+ { { 0x12345678, 0, 0, 0 }, 16, 16, 0xabcd1234, { 0x56781234, 0, 0, 0 } },
80
+ { { 0x12345678, 0, 0, 0 }, 16, 17, 0xabcd1234, { 0xacf11234, 0x0, 0, 0 } },
81
+ { { 0x12345678, 0, 0, 0 }, 16, 18, 0xabcd1234, { 0x59e11234, 0x1, 0, 0 } },
82
+ { { 0x12345678, 0, 0, 0 }, 32, 31, 0xabcd1234, { 0x2bcd1234, 0x91a2b3c, 0, 0 } },
83
+ { { 0x12345678, 0x9abcdef0, 0x11112222, 0xffccaaee }, 128, 19, 0x983a4ba, { 0xb3c3a4ba, 0xf78091a2, 0x1114d5e6, 0x57708889, 0x7fe65 } },
84
+ };
85
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
86
+ const size_t bitLen = tbl[i].bitLen;
87
+ const size_t shift = tbl[i].shift;
88
+ uint32_t z[5];
89
+ z[0] = tbl[i].z0;
90
+ cybozu::bitvector_local::shiftLeftBit(z, tbl[i].x, bitLen, shift);
91
+ const size_t n = cybozu::RoundupBit<uint32_t>(bitLen + shift);
92
+ CYBOZU_TEST_EQUAL_ARRAY(z, tbl[i].z, n);
93
+ }
94
+ }
95
+
96
+ CYBOZU_TEST_AUTO(shiftRightBit)
97
+ {
98
+ const struct {
99
+ uint32_t x[5];
100
+ size_t bitLen;
101
+ size_t shift;
102
+ uint32_t z[4];
103
+ } tbl[] = {
104
+ { { 0x12345678, 0, 0, 0 }, 1, 1, { 0, 0, 0, 0 } },
105
+ { { 0x12345678, 0, 0, 0 }, 1, 3, { 1, 0, 0, 0 } },
106
+ { { 0x12345678, 0xaaaabbbb, 0, 0 }, 10, 31, { 0x376, 0, 0, 0 } },
107
+ { { 0x12345678, 0, 0, 0 }, 1, 1, { 0, 0, 0, 0 } },
108
+ { { 0x12345678, 0, 0, 0 }, 13, 5, { 0x2b3, 0, 0, 0 } },
109
+ { { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874, 1 }, 128, 1, { 0x891a2b3c, 0x55555ddd, 0x7ff75de6, 0xff5d4c3a } },
110
+ { { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874, 0 }, 128, 1, { 0x891a2b3c, 0x55555ddd, 0x7ff75de6, 0x7f5d4c3a } },
111
+ { { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874, 0 }, 128, 18, { 0xaeeec48d, 0xaef32aaa, 0xa61d3ffb, 0x3fae } },
112
+ { { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874, 0 }, 96, 18, { 0xaeeec48d, 0xaef32aaa, 0xa61d3ffb, 0 } },
113
+ };
114
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
115
+ uint32_t z[4];
116
+ cybozu::bitvector_local::shiftRightBit(z, tbl[i].x, tbl[i].bitLen, tbl[i].shift);
117
+ const size_t n = cybozu::RoundupBit<uint32_t>(tbl[i].bitLen);
118
+ CYBOZU_TEST_EQUAL_ARRAY(z, tbl[i].z, n);
119
+ }
120
+ }
121
+
122
+ CYBOZU_TEST_AUTO(append)
123
+ {
124
+ const uint16_t src1[] = { 0x3210, 0x7654, 0xba98 };
125
+ const uint16_t src2[] = { 0xabcd, 0xfebd, 0xffff };
126
+ typedef cybozu::BitVectorT<uint16_t> Vec;
127
+ {
128
+ Vec v1;
129
+ StdVec v2;
130
+ v1.append(src1, 2);
131
+ v2.append(src1, 2);
132
+ v1.append(src2, 16);
133
+ v2.append(src2, 16);
134
+ verifyVec(v1, v2);
135
+ Vec v3, v4;
136
+ v3.append(src1, 2);
137
+ v4.append(src2, 16);
138
+ v3.append(v4);
139
+ CYBOZU_TEST_ASSERT(v1 == v3);
140
+ }
141
+ for (size_t i = 0; i < 48; i++) {
142
+ for (size_t j = 0; j < 48; j++) {
143
+ Vec v1;
144
+ StdVec v2;
145
+ v1.append(src1, i);
146
+ v2.append(src1, i);
147
+ v1.append(src2, j);
148
+ v2.append(src2, j);
149
+ verifyVec(v1, v2);
150
+ Vec v3, v4;
151
+ v3.append(src1, i);
152
+ v4.append(src2, j);
153
+ v3.append(v4);
154
+ CYBOZU_TEST_ASSERT(v1 == v3);
155
+ }
156
+ }
157
+ for (size_t i = 0; i < 16; i++) {
158
+ for (size_t j = 0; j < 16; j++) {
159
+ Vec v1;
160
+ StdVec v2;
161
+ v1.append(src1[0], i);
162
+ v2.append(src1[0], i);
163
+ v1.append(src2[0], j);
164
+ v2.append(src2[0], j);
165
+ verifyVec(v1, v2);
166
+ }
167
+ }
168
+ }
169
+
170
+ CYBOZU_TEST_AUTO(extract)
171
+ {
172
+ const uint16_t src1[] = { 0x3210, 0x7654, 0xba98, 0xabcd, 0x98db };
173
+ typedef cybozu::BitVectorT<uint16_t> Vec;
174
+ Vec v;
175
+ v.append(src1, sizeof(src1) * 8);
176
+ for (size_t pos = 0; pos <= 33; pos++) {
177
+ int sum = 0;
178
+ for (size_t bitLen = 0; bitLen <= 33; bitLen++) {
179
+ uint16_t dst[3];
180
+ v.extract(dst, pos, bitLen);
181
+ for (size_t i = 0; i < bitLen; i++) {
182
+ sum += v.get(pos + i) ^ cybozu::GetBlockBit(dst, i);
183
+ }
184
+ }
185
+ CYBOZU_TEST_EQUAL(sum, 0);
186
+ sum = 0;
187
+ for (size_t bitLen = 0; bitLen <= 33; bitLen++) {
188
+ Vec v2;
189
+ v.extract(v2, pos, bitLen);
190
+ for (size_t i = 0; i < bitLen; i++) {
191
+ sum += v.get(pos + i) ^ v2.get(i);
192
+ }
193
+ }
194
+ CYBOZU_TEST_EQUAL(sum, 0);
195
+ sum = 0;
196
+ for (size_t bitLen = 0; bitLen <= 16; bitLen++) {
197
+ uint16_t r = v.extract(pos, bitLen);
198
+ for (size_t i = 0; i < bitLen; i++) {
199
+ sum += v.get(pos + i) ^ cybozu::GetBlockBit(&r, i);
200
+ }
201
+ }
202
+ CYBOZU_TEST_EQUAL(sum, 0);
203
+ }
204
+ }
@@ -0,0 +1,92 @@
1
+ #ifdef _WIN32
2
+ #undef _WIN32_WINNT
3
+ #define _WIN32_WINNT 0x600
4
+ #endif
5
+ #include <stdio.h>
6
+ #include <cybozu/thread.hpp>
7
+ #include <cybozu/condition_variable_cs.hpp>
8
+ #include <cybozu/test.hpp>
9
+
10
+ class Thread : public cybozu::ThreadBase {
11
+ cybozu::CriticalSection& cs_;
12
+ cybozu::ConditionVariableCs& cv_;
13
+ bool done_;
14
+ public:
15
+ Thread(cybozu::CriticalSection& cs, cybozu::ConditionVariableCs& cv)
16
+ : cs_(cs)
17
+ , cv_(cv)
18
+ , done_(false)
19
+ {
20
+ }
21
+
22
+ void threadEntry()
23
+ {
24
+ cs_.lock();
25
+ cv_.wait(cs_);
26
+ cs_.unlock();
27
+ done_ = true;
28
+ }
29
+ bool done() const { return done_; }
30
+ };
31
+
32
+ CYBOZU_TEST_AUTO(notifyOne)
33
+ {
34
+ cybozu::CriticalSection cs;
35
+ cybozu::ConditionVariableCs cv;
36
+
37
+ Thread t1(cs, cv);
38
+ Thread t2(cs, cv);
39
+ Thread t3(cs, cv);
40
+
41
+ t1.beginThread();
42
+ t2.beginThread();
43
+ t3.beginThread();
44
+ cybozu::Sleep(100);
45
+
46
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 0);
47
+
48
+ cv.notifyOne();
49
+ cybozu::Sleep(100);
50
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 1);
51
+
52
+ cv.notifyOne();
53
+ cybozu::Sleep(100);
54
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 2);
55
+
56
+ cv.notifyOne();
57
+ cybozu::Sleep(100);
58
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 3);
59
+
60
+ t1.joinThread();
61
+ t2.joinThread();
62
+ t3.joinThread();
63
+ }
64
+
65
+ CYBOZU_TEST_AUTO(broadcast)
66
+ {
67
+ cybozu::CriticalSection cs;
68
+ cybozu::ConditionVariableCs cv;
69
+
70
+ Thread t1(cs, cv);
71
+ Thread t2(cs, cv);
72
+ Thread t3(cs, cv);
73
+
74
+ t1.beginThread();
75
+ t2.beginThread();
76
+ t3.beginThread();
77
+ cybozu::Sleep(100);
78
+
79
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 0);
80
+
81
+ cv.notifyOne();
82
+ cybozu::Sleep(100);
83
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 1);
84
+
85
+ cv.notifyAll();
86
+ cybozu::Sleep(100);
87
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 3);
88
+
89
+ t1.joinThread();
90
+ t2.joinThread();
91
+ t3.joinThread();
92
+ }
@@ -0,0 +1,88 @@
1
+ #include <stdio.h>
2
+ #include <cybozu/thread.hpp>
3
+ #include <cybozu/condition_variable.hpp>
4
+ #include <cybozu/test.hpp>
5
+
6
+ class Thread : public cybozu::ThreadBase {
7
+ cybozu::Mutex& mutex_;
8
+ cybozu::ConditionVariable& cv_;
9
+ bool done_;
10
+ public:
11
+ Thread(cybozu::Mutex& mutex, cybozu::ConditionVariable& cv)
12
+ : mutex_(mutex)
13
+ , cv_(cv)
14
+ , done_(false)
15
+ {
16
+ }
17
+
18
+ void threadEntry()
19
+ {
20
+ mutex_.lock();
21
+ cv_.wait(mutex_);
22
+ mutex_.unlock();
23
+ done_ = true;
24
+ }
25
+ bool done() const { return done_; }
26
+ };
27
+
28
+ CYBOZU_TEST_AUTO(notifyOne)
29
+ {
30
+ cybozu::Mutex mutex;
31
+ cybozu::ConditionVariable cv;
32
+
33
+ Thread t1(mutex, cv);
34
+ Thread t2(mutex, cv);
35
+ Thread t3(mutex, cv);
36
+
37
+ t1.beginThread();
38
+ t2.beginThread();
39
+ t3.beginThread();
40
+ cybozu::Sleep(100);
41
+
42
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 0);
43
+
44
+ cv.notifyOne();
45
+ cybozu::Sleep(100);
46
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 1);
47
+
48
+ cv.notifyOne();
49
+ cybozu::Sleep(100);
50
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 2);
51
+
52
+ cv.notifyOne();
53
+ cybozu::Sleep(100);
54
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 3);
55
+
56
+ t1.joinThread();
57
+ t2.joinThread();
58
+ t3.joinThread();
59
+ }
60
+
61
+ CYBOZU_TEST_AUTO(broadcast)
62
+ {
63
+ cybozu::Mutex mutex;
64
+ cybozu::ConditionVariable cv;
65
+
66
+ Thread t1(mutex, cv);
67
+ Thread t2(mutex, cv);
68
+ Thread t3(mutex, cv);
69
+
70
+ t1.beginThread();
71
+ t2.beginThread();
72
+ t3.beginThread();
73
+ cybozu::Sleep(100);
74
+
75
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 0);
76
+
77
+ cv.notifyOne();
78
+ cybozu::Sleep(100);
79
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 1);
80
+
81
+ cv.notifyAll();
82
+ cybozu::Sleep(100);
83
+ CYBOZU_TEST_EQUAL(t1.done() + t2.done() + t3.done(), 3);
84
+
85
+ t1.joinThread();
86
+ t2.joinThread();
87
+ t3.joinThread();
88
+ }