cosmos 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (912) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +48 -48
  4. data/.travis.yml +7 -7
  5. data/CONTRIBUTING.txt +50 -50
  6. data/Gemfile +6 -6
  7. data/Guardfile +27 -27
  8. data/LICENSE.txt +879 -879
  9. data/Manifest.txt +1116 -1114
  10. data/README.md +109 -107
  11. data/Rakefile +214 -214
  12. data/autohotkey/config/data/diamond.STL +57 -57
  13. data/autohotkey/config/system/system.txt +34 -34
  14. data/autohotkey/config/targets/COSMOS/cmd_tlm/cosmos_server_cmds.txt +41 -41
  15. data/autohotkey/config/targets/COSMOS/cmd_tlm/cosmos_server_tlm.txt +15 -15
  16. data/autohotkey/config/targets/COSMOS/cmd_tlm_server.txt +6 -6
  17. data/autohotkey/config/targets/COSMOS/target.txt +5 -5
  18. data/autohotkey/config/targets/INST/cmd_tlm/inst_cmds.txt +121 -121
  19. data/autohotkey/config/targets/INST/cmd_tlm/inst_tlm.txt +247 -247
  20. data/autohotkey/config/targets/INST/cmd_tlm_server.txt +5 -5
  21. data/autohotkey/config/targets/INST/lib/example_limits_response.rb +30 -30
  22. data/autohotkey/config/targets/INST/lib/sim_inst.rb +305 -294
  23. data/autohotkey/config/targets/INST/screens/adcs.txt +46 -46
  24. data/autohotkey/config/targets/INST/screens/array.txt +7 -7
  25. data/autohotkey/config/targets/INST/screens/block.txt +8 -8
  26. data/autohotkey/config/targets/INST/screens/commanding.txt +30 -30
  27. data/autohotkey/config/targets/INST/screens/graphs.txt +14 -14
  28. data/autohotkey/config/targets/INST/screens/ground.txt +25 -25
  29. data/autohotkey/config/targets/INST/screens/health_status.txt +33 -33
  30. data/autohotkey/config/targets/INST/screens/hs.txt +49 -49
  31. data/autohotkey/config/targets/INST/screens/image.txt +21 -21
  32. data/autohotkey/config/targets/INST/screens/latest.txt +23 -23
  33. data/autohotkey/config/targets/INST/screens/mech.txt +25 -25
  34. data/autohotkey/config/targets/INST/screens/other.txt +25 -25
  35. data/autohotkey/config/targets/INST/screens/params.txt +25 -25
  36. data/autohotkey/config/targets/INST/screens/tabs.txt +68 -68
  37. data/autohotkey/config/targets/INST/target.txt +26 -26
  38. data/autohotkey/config/targets/META/cmd_tlm/meta_cmd.txt +10 -10
  39. data/autohotkey/config/targets/META/cmd_tlm/meta_tlm.txt +9 -9
  40. data/autohotkey/config/targets/SYSTEM/cmd_tlm/limits_groups.txt +7 -7
  41. data/autohotkey/config/targets/SYSTEM/screens/error.txt +11 -11
  42. data/autohotkey/config/tools/cmd_tlm_server/cmd_tlm_server.txt +22 -22
  43. data/autohotkey/config/tools/data_viewer/data_viewer.txt +11 -11
  44. data/autohotkey/config/tools/handbook_creator/handbook_creator.txt +49 -49
  45. data/autohotkey/config/tools/handbook_creator/templates/command_packets.html.erb +86 -86
  46. data/autohotkey/config/tools/handbook_creator/templates/command_toc.html.erb +38 -38
  47. data/autohotkey/config/tools/handbook_creator/templates/footer.html.erb +9 -9
  48. data/autohotkey/config/tools/handbook_creator/templates/header.html.erb +25 -25
  49. data/autohotkey/config/tools/handbook_creator/templates/limits_groups.html.erb +13 -13
  50. data/autohotkey/config/tools/handbook_creator/templates/nav.html.erb +27 -27
  51. data/autohotkey/config/tools/handbook_creator/templates/overview.html.erb +1 -1
  52. data/autohotkey/config/tools/handbook_creator/templates/pdf_cover.html.erb +23 -23
  53. data/autohotkey/config/tools/handbook_creator/templates/pdf_footer.html.erb +33 -33
  54. data/autohotkey/config/tools/handbook_creator/templates/pdf_header.html.erb +41 -41
  55. data/autohotkey/config/tools/handbook_creator/templates/telemetry_packets.html.erb +80 -80
  56. data/autohotkey/config/tools/handbook_creator/templates/telemetry_toc.html.erb +38 -38
  57. data/autohotkey/config/tools/handbook_creator/templates/title.html.erb +1 -1
  58. data/autohotkey/config/tools/launcher/launcher.txt +38 -38
  59. data/autohotkey/config/tools/script_runner/script_runner.txt +3 -3
  60. data/autohotkey/config/tools/table_manager/ConfigTables_def.txt +8 -8
  61. data/autohotkey/config/tools/table_manager/OneDimensionalTable_def.txt +19 -19
  62. data/autohotkey/config/tools/table_manager/TwoDimensionalTable_def.txt +248 -248
  63. data/autohotkey/config/tools/test_runner/test_runner.txt +8 -8
  64. data/autohotkey/config/tools/test_runner/test_runner2.txt +11 -11
  65. data/autohotkey/config/tools/test_runner/test_runner3.txt +6 -6
  66. data/autohotkey/config/tools/test_runner/test_runner4.txt +1 -1
  67. data/autohotkey/config/tools/tlm_extractor/tlm_extractor.txt +13 -13
  68. data/autohotkey/config/tools/tlm_extractor/tlm_extractor2.txt +9 -9
  69. data/autohotkey/config/tools/tlm_grapher/bad.txt +50 -50
  70. data/autohotkey/config/tools/tlm_grapher/temp1-4.txt +51 -51
  71. data/autohotkey/config/tools/tlm_grapher/test2.txt +111 -111
  72. data/autohotkey/config/tools/tlm_viewer/tlm_viewer.txt +24 -24
  73. data/autohotkey/config/tools/tlm_viewer/tlm_viewer2.txt +4 -4
  74. data/autohotkey/config/tools/tlm_viewer/tlm_viewer3.txt +3 -3
  75. data/autohotkey/lib/example_background_task.rb +42 -42
  76. data/autohotkey/lib/user_version.rb +3 -3
  77. data/autohotkey/procedures/clear_util.rb +7 -7
  78. data/autohotkey/procedures/collect.rb +18 -18
  79. data/autohotkey/procedures/collect_util.rb +14 -14
  80. data/autohotkey/procedures/example_test.rb +67 -67
  81. data/autohotkey/procedures/example_test2.rb +74 -74
  82. data/autohotkey/procedures/script_test.rb +17 -17
  83. data/autohotkey/procedures/syntax_error.rb +18 -18
  84. data/autohotkey/tools/CmdExtractorAHK +16 -16
  85. data/autohotkey/tools/CmdSender +14 -14
  86. data/autohotkey/tools/CmdSenderAHK +18 -18
  87. data/autohotkey/tools/CmdTlmServer +14 -14
  88. data/autohotkey/tools/CmdTlmServerAHK +28 -28
  89. data/autohotkey/tools/CmdTlmServerAHK2 +17 -17
  90. data/autohotkey/tools/DataViewer +14 -14
  91. data/autohotkey/tools/DataViewerAHK +17 -17
  92. data/autohotkey/tools/HandbookCreatorAHK +20 -20
  93. data/autohotkey/tools/LauncherAHK +17 -17
  94. data/autohotkey/tools/LimitsMonitorAHK +20 -20
  95. data/autohotkey/tools/OpenGLBuilderAHK +20 -20
  96. data/autohotkey/tools/PacketViewer +14 -14
  97. data/autohotkey/tools/PacketViewerAHK +18 -18
  98. data/autohotkey/tools/PacketViewerAHK2 +17 -17
  99. data/autohotkey/tools/Replay +14 -14
  100. data/autohotkey/tools/Replay.bat +59 -59
  101. data/autohotkey/tools/ReplayAHK +17 -17
  102. data/autohotkey/tools/ScriptRunner +14 -14
  103. data/autohotkey/tools/ScriptRunnerAHK +20 -20
  104. data/autohotkey/tools/ScriptRunnerAHK2 +17 -17
  105. data/autohotkey/tools/TableManager +14 -14
  106. data/autohotkey/tools/TableManagerAHK +30 -30
  107. data/autohotkey/tools/TestRunner +15 -15
  108. data/autohotkey/tools/TestRunnerAHK +17 -17
  109. data/autohotkey/tools/TestRunnerAHK2 +17 -17
  110. data/autohotkey/tools/TestRunnerAHK3 +17 -17
  111. data/autohotkey/tools/TestRunnerAHK4 +17 -17
  112. data/autohotkey/tools/TlmExtractor +15 -15
  113. data/autohotkey/tools/TlmExtractorAHK +19 -19
  114. data/autohotkey/tools/TlmExtractorAHK2 +16 -16
  115. data/autohotkey/tools/TlmExtractorAHK3 +16 -16
  116. data/autohotkey/tools/TlmGrapher +14 -14
  117. data/autohotkey/tools/TlmGrapherAHK +19 -19
  118. data/autohotkey/tools/TlmGrapherAHK2 +23 -23
  119. data/autohotkey/tools/TlmGrapherAHK3 +17 -17
  120. data/autohotkey/tools/TlmGrapherAHK4 +17 -17
  121. data/autohotkey/tools/TlmViewer +14 -14
  122. data/autohotkey/tools/TlmViewerAHK +28 -28
  123. data/autohotkey/tools/TlmViewerAHK2 +18 -18
  124. data/autohotkey/tools/TlmViewerAHK3 +18 -18
  125. data/autohotkey/tools/TlmViewerAHK4 +18 -18
  126. data/autohotkey/tools/TlmViewerAHK5 +18 -18
  127. data/autohotkey/tools/autohotkey.rb +37 -37
  128. data/autohotkey/tools/cmd_extractor.ahk +27 -27
  129. data/autohotkey/tools/cmd_sender.ahk +182 -162
  130. data/autohotkey/tools/cmd_tlm_server.ahk +89 -89
  131. data/autohotkey/tools/cmd_tlm_server2.ahk +45 -45
  132. data/autohotkey/tools/data_viewer.ahk +135 -135
  133. data/autohotkey/tools/handbook_creator.ahk +23 -23
  134. data/autohotkey/tools/launcher.ahk +41 -41
  135. data/autohotkey/tools/limits_monitor.ahk +70 -70
  136. data/autohotkey/tools/open_gl_builder.ahk +134 -134
  137. data/autohotkey/tools/packet_viewer.ahk +143 -143
  138. data/autohotkey/tools/packet_viewer2.ahk +9 -9
  139. data/autohotkey/tools/replay.ahk +98 -98
  140. data/autohotkey/tools/script_runner.ahk +589 -589
  141. data/autohotkey/tools/script_runner2.ahk +34 -31
  142. data/autohotkey/tools/table_manager.ahk +220 -220
  143. data/autohotkey/tools/test_runner.ahk +262 -259
  144. data/autohotkey/tools/test_runner2.ahk +52 -52
  145. data/autohotkey/tools/test_runner3.ahk +13 -13
  146. data/autohotkey/tools/tlm_extractor.ahk +272 -272
  147. data/autohotkey/tools/tlm_grapher.ahk +642 -642
  148. data/autohotkey/tools/tlm_grapher2.ahk +115 -115
  149. data/autohotkey/tools/tlm_grapher3.ahk +24 -24
  150. data/autohotkey/tools/tlm_viewer.ahk +133 -133
  151. data/autohotkey/tools/tlm_viewer2.ahk +50 -49
  152. data/autohotkey/tools/tlm_viewer4.ahk +4 -4
  153. data/autohotkey/tools/tlm_viewer5.ahk +20 -20
  154. data/bin/cosmos +96 -96
  155. data/bin/cstol_converter +1166 -1166
  156. data/bin/rubysloc +85 -85
  157. data/cosmos.gemspec +98 -97
  158. data/data/about.txt +4 -4
  159. data/data/crc.txt +306 -305
  160. data/data/diamond.STL +57 -57
  161. data/data/legal.txt +9 -9
  162. data/demo/Gemfile +6 -6
  163. data/demo/Launcher +15 -15
  164. data/demo/Launcher.bat +59 -59
  165. data/demo/Rakefile +61 -61
  166. data/demo/config/data/crc.txt +222 -206
  167. data/demo/config/data/diamond.STL +57 -57
  168. data/demo/config/data/meta_init.txt +4 -4
  169. data/demo/config/system/system.txt +34 -34
  170. data/demo/config/system/system2.txt +33 -33
  171. data/demo/config/targets/COSMOS/cmd_tlm/cosmos_server_cmds.txt +41 -41
  172. data/demo/config/targets/COSMOS/cmd_tlm/cosmos_server_tlm.txt +15 -15
  173. data/demo/config/targets/COSMOS/cmd_tlm_server.txt +6 -6
  174. data/demo/config/targets/COSMOS/screens/limits_change.txt +20 -20
  175. data/demo/config/targets/COSMOS/screens/version.txt +19 -19
  176. data/demo/config/targets/COSMOS/target.txt +11 -11
  177. data/demo/config/targets/EXAMPLE/cmd_tlm/example_cmds.txt +2 -2
  178. data/demo/config/targets/EXAMPLE/cmd_tlm/example_tlm.txt +3 -3
  179. data/demo/config/targets/EXAMPLE/cmd_tlm_server.txt +6 -6
  180. data/demo/config/targets/EXAMPLE/lib/example_interface.rb +22 -22
  181. data/demo/config/targets/EXAMPLE/target.txt +6 -6
  182. data/demo/config/targets/INST/cmd_tlm/inst_cmds.txt +121 -121
  183. data/demo/config/targets/INST/cmd_tlm/inst_tlm.txt +247 -247
  184. data/demo/config/targets/INST/cmd_tlm_server.txt +5 -5
  185. data/demo/config/targets/INST/lib/example_limits_response.rb +30 -30
  186. data/demo/config/targets/INST/lib/sim_inst.rb +305 -294
  187. data/demo/config/targets/INST/screens/adcs.txt +46 -46
  188. data/demo/config/targets/INST/screens/array.txt +15 -15
  189. data/demo/config/targets/INST/screens/block.txt +8 -8
  190. data/demo/config/targets/INST/screens/commanding.txt +30 -30
  191. data/demo/config/targets/INST/screens/graphs.txt +14 -14
  192. data/demo/config/targets/INST/screens/ground.txt +25 -25
  193. data/demo/config/targets/INST/screens/hs.txt +44 -44
  194. data/demo/config/targets/INST/screens/latest.txt +23 -23
  195. data/demo/config/targets/INST/screens/other.txt +29 -29
  196. data/demo/config/targets/INST/screens/tabs.txt +70 -70
  197. data/demo/config/targets/INST/target.txt +33 -33
  198. data/demo/config/targets/META/cmd_tlm/meta_cmd.txt +10 -10
  199. data/demo/config/targets/META/cmd_tlm/meta_tlm.txt +13 -13
  200. data/demo/config/targets/SYSTEM/cmd_tlm/limits_groups.txt +7 -7
  201. data/demo/config/targets/SYSTEM/cmd_tlm/override.txt +29 -29
  202. data/demo/config/targets/SYSTEM/screens/status.txt +12 -12
  203. data/demo/config/targets/TEMPLATED/cmd_tlm/templated_cmds.txt +13 -12
  204. data/demo/config/targets/TEMPLATED/cmd_tlm/templated_tlm.txt +3 -3
  205. data/demo/config/targets/TEMPLATED/cmd_tlm_server.txt +6 -6
  206. data/demo/config/targets/TEMPLATED/lib/templated_interface.rb +54 -48
  207. data/demo/config/targets/TEMPLATED/target.txt +6 -6
  208. data/demo/config/tools/cmd_tlm_server/cmd_tlm_server.txt +33 -33
  209. data/demo/config/tools/cmd_tlm_server/cmd_tlm_server2.txt +29 -29
  210. data/demo/config/tools/data_viewer/data_viewer.txt +11 -11
  211. data/demo/config/tools/handbook_creator/handbook_creator.txt +66 -66
  212. data/demo/config/tools/handbook_creator/templates/command_packets.html.erb +86 -86
  213. data/demo/config/tools/handbook_creator/templates/command_toc.html.erb +38 -38
  214. data/demo/config/tools/handbook_creator/templates/footer.html.erb +9 -9
  215. data/demo/config/tools/handbook_creator/templates/header.html.erb +25 -25
  216. data/demo/config/tools/handbook_creator/templates/limits_groups.html.erb +13 -13
  217. data/demo/config/tools/handbook_creator/templates/nav.html.erb +27 -27
  218. data/demo/config/tools/handbook_creator/templates/overview.html.erb +1 -1
  219. data/demo/config/tools/handbook_creator/templates/pdf_cover.html.erb +23 -23
  220. data/demo/config/tools/handbook_creator/templates/pdf_footer.html.erb +33 -33
  221. data/demo/config/tools/handbook_creator/templates/pdf_header.html.erb +41 -41
  222. data/demo/config/tools/handbook_creator/templates/telemetry_packets.html.erb +80 -80
  223. data/demo/config/tools/handbook_creator/templates/telemetry_toc.html.erb +38 -38
  224. data/demo/config/tools/handbook_creator/templates/title.html.erb +1 -1
  225. data/demo/config/tools/launcher/launcher.txt +45 -45
  226. data/demo/config/tools/launcher/launcher2.txt +45 -45
  227. data/demo/config/tools/script_runner/script_runner.txt +3 -3
  228. data/demo/config/tools/table_manager/ConfigTables_def.txt +8 -8
  229. data/demo/config/tools/table_manager/ExampleTableDefinition.txt +24 -24
  230. data/demo/config/tools/table_manager/MCConfigurationTable_fsw1_def.txt +25 -25
  231. data/demo/config/tools/table_manager/MCConfigurationTable_fsw2_def.txt +25 -25
  232. data/demo/config/tools/table_manager/PPSSelectionTable_def.txt +8 -8
  233. data/demo/config/tools/table_manager/TLMMonitoringTable_def.txt +248 -248
  234. data/demo/config/tools/test_runner/test_runner.txt +17 -17
  235. data/demo/config/tools/tlm_extractor/tlm_extractor.txt +13 -13
  236. data/demo/config/tools/tlm_extractor/tlm_extractor2.txt +2 -2
  237. data/demo/config/tools/tlm_extractor/tlm_extractor3.txt +2 -2
  238. data/demo/config/tools/tlm_extractor/tlm_extractor4.txt +2 -2
  239. data/demo/config/tools/tlm_viewer/tlm_viewer.txt +41 -41
  240. data/demo/lib/example_background_task.rb +57 -52
  241. data/demo/lib/example_target.rb +113 -108
  242. data/demo/lib/scpi_target.rb +74 -74
  243. data/demo/lib/user_version.rb +3 -3
  244. data/demo/procedures/checks.rb +11 -11
  245. data/demo/procedures/clear_util.rb +7 -7
  246. data/demo/procedures/collect.rb +18 -18
  247. data/demo/procedures/collect_util.rb +14 -14
  248. data/demo/procedures/cosmos_api_test.rb +293 -293
  249. data/demo/procedures/disconnect.rb +29 -29
  250. data/demo/procedures/example_test.rb +182 -182
  251. data/demo/procedures/plot_test.rb +8 -8
  252. data/demo/procedures/run_example_test.rb +3 -3
  253. data/demo/procedures/test.rb +51 -51
  254. data/demo/tools/CmdExtractor +15 -15
  255. data/demo/tools/CmdExtractor.bat +59 -59
  256. data/demo/tools/CmdSender +15 -15
  257. data/demo/tools/CmdSender.bat +59 -59
  258. data/demo/tools/CmdTlmServer +15 -15
  259. data/demo/tools/CmdTlmServer.bat +59 -59
  260. data/demo/tools/DataViewer +15 -15
  261. data/demo/tools/DataViewer.bat +59 -59
  262. data/demo/tools/ExampleTarget +15 -15
  263. data/demo/tools/ExampleTarget.bat +59 -59
  264. data/demo/tools/HandbookCreator +15 -15
  265. data/demo/tools/HandbookCreator.bat +61 -61
  266. data/demo/tools/Launcher +15 -15
  267. data/demo/tools/Launcher.bat +59 -59
  268. data/demo/tools/LimitsMonitor +15 -15
  269. data/demo/tools/LimitsMonitor.bat +59 -59
  270. data/demo/tools/OpenGLBuilder +15 -15
  271. data/demo/tools/OpenGLBuilder.bat +59 -59
  272. data/demo/tools/PacketViewer +15 -15
  273. data/demo/tools/PacketViewer.bat +59 -59
  274. data/demo/tools/Replay +15 -15
  275. data/demo/tools/Replay.bat +59 -59
  276. data/demo/tools/ScpiTarget +15 -15
  277. data/demo/tools/ScpiTarget.bat +59 -59
  278. data/demo/tools/ScriptRunner +15 -15
  279. data/demo/tools/ScriptRunner.bat +59 -59
  280. data/demo/tools/TableManager +15 -15
  281. data/demo/tools/TableManager.bat +59 -59
  282. data/demo/tools/TestRunner +15 -15
  283. data/demo/tools/TestRunner.bat +59 -59
  284. data/demo/tools/TlmExtractor +15 -15
  285. data/demo/tools/TlmExtractor.bat +59 -59
  286. data/demo/tools/TlmGrapher +15 -15
  287. data/demo/tools/TlmGrapher.bat +59 -59
  288. data/demo/tools/TlmViewer +15 -15
  289. data/demo/tools/TlmViewer.bat +59 -59
  290. data/demo/tools/mac/CmdExtractor.app/Contents/Info.plist +38 -38
  291. data/demo/tools/mac/CmdExtractor.app/Contents/MacOS/CmdExtractor.rb +15 -15
  292. data/demo/tools/mac/CmdExtractor.app/Contents/MacOS/main.sh +6 -6
  293. data/demo/tools/mac/CmdSender.app/Contents/Info.plist +38 -38
  294. data/demo/tools/mac/CmdSender.app/Contents/MacOS/CmdSender.rb +15 -15
  295. data/demo/tools/mac/CmdSender.app/Contents/MacOS/main.sh +6 -6
  296. data/demo/tools/mac/CmdTlmServer.app/Contents/Info.plist +38 -38
  297. data/demo/tools/mac/CmdTlmServer.app/Contents/MacOS/CmdTlmServer.rb +15 -15
  298. data/demo/tools/mac/CmdTlmServer.app/Contents/MacOS/main.sh +6 -6
  299. data/demo/tools/mac/DataViewer.app/Contents/Info.plist +38 -38
  300. data/demo/tools/mac/DataViewer.app/Contents/MacOS/DataViewer.rb +15 -15
  301. data/demo/tools/mac/DataViewer.app/Contents/MacOS/main.sh +6 -6
  302. data/demo/tools/mac/HandbookCreator.app/Contents/Info.plist +38 -38
  303. data/demo/tools/mac/HandbookCreator.app/Contents/MacOS/HandbookCreator.rb +15 -15
  304. data/demo/tools/mac/HandbookCreator.app/Contents/MacOS/main.sh +6 -6
  305. data/demo/tools/mac/Launcher.app/Contents/Info.plist +38 -38
  306. data/demo/tools/mac/Launcher.app/Contents/MacOS/Launcher.rb +15 -15
  307. data/demo/tools/mac/Launcher.app/Contents/MacOS/main.sh +6 -6
  308. data/demo/tools/mac/LimitsMonitor.app/Contents/Info.plist +38 -38
  309. data/demo/tools/mac/LimitsMonitor.app/Contents/MacOS/LimitsMonitor.rb +15 -15
  310. data/demo/tools/mac/LimitsMonitor.app/Contents/MacOS/main.sh +6 -6
  311. data/demo/tools/mac/OpenGLBuilder.app/Contents/Info.plist +38 -38
  312. data/demo/tools/mac/OpenGLBuilder.app/Contents/MacOS/OpenGLBuilder.rb +15 -15
  313. data/demo/tools/mac/OpenGLBuilder.app/Contents/MacOS/main.sh +6 -6
  314. data/demo/tools/mac/PacketViewer.app/Contents/Info.plist +38 -38
  315. data/demo/tools/mac/PacketViewer.app/Contents/MacOS/PacketViewer.rb +15 -15
  316. data/demo/tools/mac/PacketViewer.app/Contents/MacOS/main.sh +6 -6
  317. data/demo/tools/mac/Replay.app/Contents/Info.plist +38 -38
  318. data/demo/tools/mac/Replay.app/Contents/MacOS/Replay.rb +15 -15
  319. data/demo/tools/mac/Replay.app/Contents/MacOS/main.sh +6 -6
  320. data/demo/tools/mac/ScriptRunner.app/Contents/Info.plist +38 -38
  321. data/demo/tools/mac/ScriptRunner.app/Contents/MacOS/ScriptRunner.rb +15 -15
  322. data/demo/tools/mac/ScriptRunner.app/Contents/MacOS/main.sh +6 -6
  323. data/demo/tools/mac/TableManager.app/Contents/Info.plist +38 -38
  324. data/demo/tools/mac/TableManager.app/Contents/MacOS/TableManager.rb +15 -15
  325. data/demo/tools/mac/TableManager.app/Contents/MacOS/main.sh +6 -6
  326. data/demo/tools/mac/TestRunner.app/Contents/Info.plist +38 -38
  327. data/demo/tools/mac/TestRunner.app/Contents/MacOS/TestRunner.rb +15 -15
  328. data/demo/tools/mac/TestRunner.app/Contents/MacOS/main.sh +6 -6
  329. data/demo/tools/mac/TlmExtractor.app/Contents/Info.plist +38 -38
  330. data/demo/tools/mac/TlmExtractor.app/Contents/MacOS/TlmExtractor.rb +15 -15
  331. data/demo/tools/mac/TlmExtractor.app/Contents/MacOS/main.sh +6 -6
  332. data/demo/tools/mac/TlmGrapher.app/Contents/Info.plist +38 -38
  333. data/demo/tools/mac/TlmGrapher.app/Contents/MacOS/TlmGrapher.rb +15 -15
  334. data/demo/tools/mac/TlmGrapher.app/Contents/MacOS/main.sh +6 -6
  335. data/demo/tools/mac/TlmViewer.app/Contents/Info.plist +38 -38
  336. data/demo/tools/mac/TlmViewer.app/Contents/MacOS/TlmViewer.rb +15 -15
  337. data/demo/tools/mac/TlmViewer.app/Contents/MacOS/main.sh +6 -6
  338. data/ext/cosmos/ext/array/array.c +111 -111
  339. data/ext/cosmos/ext/array/extconf.rb +13 -13
  340. data/ext/cosmos/ext/buffered_file/buffered_file.c +167 -167
  341. data/ext/cosmos/ext/buffered_file/extconf.rb +13 -13
  342. data/ext/cosmos/ext/config_parser/config_parser.c +237 -237
  343. data/ext/cosmos/ext/config_parser/extconf.rb +13 -13
  344. data/ext/cosmos/ext/cosmos_io/cosmos_io.c +117 -117
  345. data/ext/cosmos/ext/cosmos_io/extconf.rb +13 -13
  346. data/ext/cosmos/ext/crc/crc.c +341 -341
  347. data/ext/cosmos/ext/crc/extconf.rb +12 -12
  348. data/ext/cosmos/ext/line_graph/extconf.rb +13 -13
  349. data/ext/cosmos/ext/line_graph/line_graph.c +501 -501
  350. data/ext/cosmos/ext/low_fragmentation_array/extconf.rb +12 -12
  351. data/ext/cosmos/ext/low_fragmentation_array/low_fragmentation_array.c +261 -261
  352. data/ext/cosmos/ext/packet/extconf.rb +13 -13
  353. data/ext/cosmos/ext/packet/packet.c +339 -339
  354. data/ext/cosmos/ext/platform/extconf.rb +13 -13
  355. data/ext/cosmos/ext/platform/platform.c +81 -81
  356. data/ext/cosmos/ext/polynomial_conversion/extconf.rb +13 -13
  357. data/ext/cosmos/ext/polynomial_conversion/polynomial_conversion.c +73 -73
  358. data/ext/cosmos/ext/string/extconf.rb +13 -13
  359. data/ext/cosmos/ext/string/string.c +49 -49
  360. data/ext/cosmos/ext/structure/structure.c +894 -894
  361. data/ext/cosmos/ext/tabbed_plots_config/extconf.rb +13 -13
  362. data/ext/cosmos/ext/tabbed_plots_config/tabbed_plots_config.c +51 -51
  363. data/ext/cosmos/ext/telemetry/extconf.rb +13 -13
  364. data/ext/cosmos/ext/telemetry/telemetry.c +306 -306
  365. data/ext/mkrf_conf.rb +40 -40
  366. data/install/Gemfile +6 -6
  367. data/install/Launcher +14 -14
  368. data/install/Launcher.bat +59 -59
  369. data/install/Rakefile +61 -61
  370. data/install/config/data/crc.txt +134 -133
  371. data/install/config/system/system.txt +29 -29
  372. data/install/config/targets/COSMOS/cmd_tlm/cosmos_server_cmds.txt +41 -41
  373. data/install/config/targets/COSMOS/cmd_tlm/cosmos_server_tlm.txt +15 -15
  374. data/install/config/targets/COSMOS/cmd_tlm_server.txt +6 -6
  375. data/install/config/targets/COSMOS/screens/limits_change.txt +20 -20
  376. data/install/config/targets/COSMOS/screens/version.txt +19 -19
  377. data/install/config/targets/COSMOS/target.txt +8 -8
  378. data/install/config/tools/cmd_tlm_server/cmd_tlm_server.txt +2 -2
  379. data/install/config/tools/data_viewer/data_viewer.txt +3 -3
  380. data/install/config/tools/handbook_creator/handbook_creator.txt +49 -49
  381. data/install/config/tools/handbook_creator/templates/command_packets.html.erb +86 -86
  382. data/install/config/tools/handbook_creator/templates/command_toc.html.erb +38 -38
  383. data/install/config/tools/handbook_creator/templates/footer.html.erb +9 -9
  384. data/install/config/tools/handbook_creator/templates/header.html.erb +25 -25
  385. data/install/config/tools/handbook_creator/templates/limits_groups.html.erb +13 -13
  386. data/install/config/tools/handbook_creator/templates/nav.html.erb +27 -27
  387. data/install/config/tools/handbook_creator/templates/overview.html.erb +1 -1
  388. data/install/config/tools/handbook_creator/templates/pdf_cover.html.erb +23 -23
  389. data/install/config/tools/handbook_creator/templates/pdf_footer.html.erb +33 -33
  390. data/install/config/tools/handbook_creator/templates/pdf_header.html.erb +41 -41
  391. data/install/config/tools/handbook_creator/templates/telemetry_packets.html.erb +80 -80
  392. data/install/config/tools/handbook_creator/templates/telemetry_toc.html.erb +38 -38
  393. data/install/config/tools/handbook_creator/templates/title.html.erb +1 -1
  394. data/install/config/tools/launcher/launcher.txt +39 -39
  395. data/install/config/tools/script_runner/script_runner.txt +3 -3
  396. data/install/config/tools/test_runner/test_runner.txt +8 -8
  397. data/install/config/tools/tlm_viewer/tlm_viewer.txt +5 -5
  398. data/install/lib/user_version.rb +3 -3
  399. data/install/tools/CmdExtractor +15 -15
  400. data/install/tools/CmdExtractor.bat +59 -59
  401. data/install/tools/CmdSender +15 -15
  402. data/install/tools/CmdSender.bat +59 -59
  403. data/install/tools/CmdTlmServer +15 -15
  404. data/install/tools/CmdTlmServer.bat +59 -59
  405. data/install/tools/DataViewer +15 -15
  406. data/install/tools/DataViewer.bat +59 -59
  407. data/install/tools/HandbookCreator +15 -15
  408. data/install/tools/HandbookCreator.bat +61 -61
  409. data/install/tools/Launcher +15 -15
  410. data/install/tools/Launcher.bat +59 -59
  411. data/install/tools/LimitsMonitor +15 -15
  412. data/install/tools/LimitsMonitor.bat +59 -59
  413. data/install/tools/OpenGLBuilder +15 -15
  414. data/install/tools/OpenGLBuilder.bat +59 -59
  415. data/install/tools/PacketViewer +15 -15
  416. data/install/tools/PacketViewer.bat +59 -59
  417. data/install/tools/Replay +15 -15
  418. data/install/tools/Replay.bat +59 -59
  419. data/install/tools/ScriptRunner +15 -15
  420. data/install/tools/ScriptRunner.bat +59 -59
  421. data/install/tools/TableManager +15 -15
  422. data/install/tools/TableManager.bat +59 -59
  423. data/install/tools/TestRunner +15 -15
  424. data/install/tools/TestRunner.bat +59 -59
  425. data/install/tools/TlmExtractor +15 -15
  426. data/install/tools/TlmExtractor.bat +59 -59
  427. data/install/tools/TlmGrapher +15 -15
  428. data/install/tools/TlmGrapher.bat +59 -59
  429. data/install/tools/TlmViewer +15 -15
  430. data/install/tools/TlmViewer.bat +59 -59
  431. data/install/tools/mac/CmdExtractor.app/Contents/Info.plist +38 -38
  432. data/install/tools/mac/CmdExtractor.app/Contents/MacOS/CmdExtractor.rb +15 -15
  433. data/install/tools/mac/CmdExtractor.app/Contents/MacOS/main.sh +6 -6
  434. data/install/tools/mac/CmdSender.app/Contents/Info.plist +38 -38
  435. data/install/tools/mac/CmdSender.app/Contents/MacOS/CmdSender.rb +15 -15
  436. data/install/tools/mac/CmdSender.app/Contents/MacOS/main.sh +6 -6
  437. data/install/tools/mac/CmdTlmServer.app/Contents/Info.plist +38 -38
  438. data/install/tools/mac/CmdTlmServer.app/Contents/MacOS/CmdTlmServer.rb +15 -15
  439. data/install/tools/mac/CmdTlmServer.app/Contents/MacOS/main.sh +6 -6
  440. data/install/tools/mac/DataViewer.app/Contents/Info.plist +38 -38
  441. data/install/tools/mac/DataViewer.app/Contents/MacOS/DataViewer.rb +15 -15
  442. data/install/tools/mac/DataViewer.app/Contents/MacOS/main.sh +6 -6
  443. data/install/tools/mac/HandbookCreator.app/Contents/Info.plist +38 -38
  444. data/install/tools/mac/HandbookCreator.app/Contents/MacOS/HandbookCreator.rb +15 -15
  445. data/install/tools/mac/HandbookCreator.app/Contents/MacOS/main.sh +6 -6
  446. data/install/tools/mac/Launcher.app/Contents/Info.plist +38 -38
  447. data/install/tools/mac/Launcher.app/Contents/MacOS/Launcher.rb +15 -15
  448. data/install/tools/mac/Launcher.app/Contents/MacOS/main.sh +6 -6
  449. data/install/tools/mac/LimitsMonitor.app/Contents/Info.plist +38 -38
  450. data/install/tools/mac/LimitsMonitor.app/Contents/MacOS/LimitsMonitor.rb +15 -15
  451. data/install/tools/mac/LimitsMonitor.app/Contents/MacOS/main.sh +6 -6
  452. data/install/tools/mac/OpenGLBuilder.app/Contents/Info.plist +38 -38
  453. data/install/tools/mac/OpenGLBuilder.app/Contents/MacOS/OpenGLBuilder.rb +15 -15
  454. data/install/tools/mac/OpenGLBuilder.app/Contents/MacOS/main.sh +6 -6
  455. data/install/tools/mac/PacketViewer.app/Contents/Info.plist +38 -38
  456. data/install/tools/mac/PacketViewer.app/Contents/MacOS/PacketViewer.rb +15 -15
  457. data/install/tools/mac/PacketViewer.app/Contents/MacOS/main.sh +6 -6
  458. data/install/tools/mac/Replay.app/Contents/Info.plist +38 -38
  459. data/install/tools/mac/Replay.app/Contents/MacOS/Replay.rb +15 -15
  460. data/install/tools/mac/Replay.app/Contents/MacOS/main.sh +6 -6
  461. data/install/tools/mac/ScriptRunner.app/Contents/Info.plist +38 -38
  462. data/install/tools/mac/ScriptRunner.app/Contents/MacOS/ScriptRunner.rb +15 -15
  463. data/install/tools/mac/ScriptRunner.app/Contents/MacOS/main.sh +6 -6
  464. data/install/tools/mac/TableManager.app/Contents/Info.plist +38 -38
  465. data/install/tools/mac/TableManager.app/Contents/MacOS/TableManager.rb +15 -15
  466. data/install/tools/mac/TableManager.app/Contents/MacOS/main.sh +6 -6
  467. data/install/tools/mac/TestRunner.app/Contents/Info.plist +38 -38
  468. data/install/tools/mac/TestRunner.app/Contents/MacOS/TestRunner.rb +15 -15
  469. data/install/tools/mac/TestRunner.app/Contents/MacOS/main.sh +6 -6
  470. data/install/tools/mac/TlmExtractor.app/Contents/Info.plist +38 -38
  471. data/install/tools/mac/TlmExtractor.app/Contents/MacOS/TlmExtractor.rb +15 -15
  472. data/install/tools/mac/TlmExtractor.app/Contents/MacOS/main.sh +6 -6
  473. data/install/tools/mac/TlmGrapher.app/Contents/Info.plist +38 -38
  474. data/install/tools/mac/TlmGrapher.app/Contents/MacOS/TlmGrapher.rb +15 -15
  475. data/install/tools/mac/TlmGrapher.app/Contents/MacOS/main.sh +6 -6
  476. data/install/tools/mac/TlmViewer.app/Contents/Info.plist +38 -38
  477. data/install/tools/mac/TlmViewer.app/Contents/MacOS/TlmViewer.rb +15 -15
  478. data/install/tools/mac/TlmViewer.app/Contents/MacOS/main.sh +6 -6
  479. data/lib/cosmos.rb +63 -63
  480. data/lib/cosmos/ccsds/ccsds_packet.rb +63 -63
  481. data/lib/cosmos/ccsds/ccsds_parser.rb +143 -143
  482. data/lib/cosmos/config/config_parser.rb +324 -324
  483. data/lib/cosmos/conversions.rb +13 -13
  484. data/lib/cosmos/conversions/conversion.rb +47 -47
  485. data/lib/cosmos/conversions/generic_conversion.rb +55 -55
  486. data/lib/cosmos/conversions/new_packet_log_conversion.rb +45 -45
  487. data/lib/cosmos/conversions/polynomial_conversion.rb +57 -57
  488. data/lib/cosmos/conversions/processor_conversion.rb +46 -46
  489. data/lib/cosmos/conversions/received_count_conversion.rb +33 -33
  490. data/lib/cosmos/conversions/received_time_formatted_conversion.rb +37 -37
  491. data/lib/cosmos/conversions/received_time_seconds_conversion.rb +37 -37
  492. data/lib/cosmos/conversions/segmented_polynomial_conversion.rb +128 -128
  493. data/lib/cosmos/conversions/unix_time_conversion.rb +50 -50
  494. data/lib/cosmos/conversions/unix_time_formatted_conversion.rb +44 -44
  495. data/lib/cosmos/conversions/unix_time_seconds_conversion.rb +44 -44
  496. data/lib/cosmos/core_ext.rb +18 -18
  497. data/lib/cosmos/core_ext/array.rb +354 -354
  498. data/lib/cosmos/core_ext/class.rb +51 -51
  499. data/lib/cosmos/core_ext/cosmos_io.rb +29 -29
  500. data/lib/cosmos/core_ext/exception.rb +52 -52
  501. data/lib/cosmos/core_ext/file.rb +75 -75
  502. data/lib/cosmos/core_ext/hash.rb +28 -28
  503. data/lib/cosmos/core_ext/io.rb +75 -75
  504. data/lib/cosmos/core_ext/kernel.rb +38 -38
  505. data/lib/cosmos/core_ext/math.rb +119 -119
  506. data/lib/cosmos/core_ext/matrix.rb +146 -146
  507. data/lib/cosmos/core_ext/objectspace.rb +29 -29
  508. data/lib/cosmos/core_ext/range.rb +22 -22
  509. data/lib/cosmos/core_ext/socket.rb +32 -32
  510. data/lib/cosmos/core_ext/string.rb +310 -310
  511. data/lib/cosmos/core_ext/stringio.rb +24 -24
  512. data/lib/cosmos/core_ext/time.rb +446 -446
  513. data/lib/cosmos/gui/choosers/combobox_chooser.rb +130 -130
  514. data/lib/cosmos/gui/choosers/file_chooser.rb +68 -68
  515. data/lib/cosmos/gui/choosers/float_chooser.rb +82 -82
  516. data/lib/cosmos/gui/choosers/integer_chooser.rb +80 -80
  517. data/lib/cosmos/gui/choosers/string_chooser.rb +53 -53
  518. data/lib/cosmos/gui/choosers/telemetry_chooser.rb +317 -317
  519. data/lib/cosmos/gui/dialogs/about_dialog.rb +128 -128
  520. data/lib/cosmos/gui/dialogs/calendar_dialog.rb +136 -136
  521. data/lib/cosmos/gui/dialogs/cmd_details_dialog.rb +52 -52
  522. data/lib/cosmos/gui/dialogs/cmd_tlm_raw_dialog.rb +149 -149
  523. data/lib/cosmos/gui/dialogs/details_dialog.rb +174 -174
  524. data/lib/cosmos/gui/dialogs/exception_dialog.rb +97 -97
  525. data/lib/cosmos/gui/dialogs/exception_list_dialog.rb +59 -59
  526. data/lib/cosmos/gui/dialogs/find_replace_dialog.rb +196 -196
  527. data/lib/cosmos/gui/dialogs/legal_dialog.rb +169 -168
  528. data/lib/cosmos/gui/dialogs/packet_log_dialog.rb +118 -118
  529. data/lib/cosmos/gui/dialogs/progress_dialog.rb +270 -262
  530. data/lib/cosmos/gui/dialogs/pry_dialog.rb +165 -161
  531. data/lib/cosmos/gui/dialogs/scroll_text_dialog.rb +37 -37
  532. data/lib/cosmos/gui/dialogs/select_dialog.rb +54 -54
  533. data/lib/cosmos/gui/dialogs/set_tlm_dialog.rb +131 -131
  534. data/lib/cosmos/gui/dialogs/splash.rb +113 -113
  535. data/lib/cosmos/gui/dialogs/tlm_details_dialog.rb +206 -206
  536. data/lib/cosmos/gui/dialogs/tlm_edit_dialog.rb +81 -81
  537. data/lib/cosmos/gui/line_graph/line_graph.rb +456 -456
  538. data/lib/cosmos/gui/line_graph/line_graph_dialog.rb +34 -34
  539. data/lib/cosmos/gui/line_graph/line_graph_drawing.rb +494 -494
  540. data/lib/cosmos/gui/line_graph/line_graph_popups.rb +116 -116
  541. data/lib/cosmos/gui/line_graph/line_graph_scaling.rb +460 -460
  542. data/lib/cosmos/gui/line_graph/line_graph_script.rb +26 -26
  543. data/lib/cosmos/gui/line_graph/lines.rb +290 -290
  544. data/lib/cosmos/gui/line_graph/overview_graph.rb +459 -459
  545. data/lib/cosmos/gui/opengl/earth_model.rb +22 -22
  546. data/lib/cosmos/gui/opengl/gl_bounds.rb +67 -67
  547. data/lib/cosmos/gui/opengl/gl_light.rb +39 -39
  548. data/lib/cosmos/gui/opengl/gl_material.rb +29 -29
  549. data/lib/cosmos/gui/opengl/gl_scene.rb +72 -72
  550. data/lib/cosmos/gui/opengl/gl_shape.rb +146 -146
  551. data/lib/cosmos/gui/opengl/gl_viewer.rb +724 -712
  552. data/lib/cosmos/gui/opengl/gl_viewport.rb +35 -35
  553. data/lib/cosmos/gui/opengl/moon_model.rb +22 -22
  554. data/lib/cosmos/gui/opengl/opengl.rb +8 -8
  555. data/lib/cosmos/gui/opengl/stl_reader.rb +211 -211
  556. data/lib/cosmos/gui/opengl/stl_shape.rb +124 -124
  557. data/lib/cosmos/gui/opengl/texture_mapped_sphere.rb +202 -202
  558. data/lib/cosmos/gui/qt.rb +813 -786
  559. data/lib/cosmos/gui/qt_tool.rb +378 -373
  560. data/lib/cosmos/gui/text/completion.rb +381 -381
  561. data/lib/cosmos/gui/text/completion_line_edit.rb +30 -30
  562. data/lib/cosmos/gui/text/completion_text_edit.rb +179 -179
  563. data/lib/cosmos/gui/text/ruby_editor.rb +395 -395
  564. data/lib/cosmos/gui/utilities/screenshot.rb +25 -25
  565. data/lib/cosmos/gui/utilities/script_module_gui.rb +203 -203
  566. data/lib/cosmos/gui/widgets/full_text_search_line_edit.rb +161 -161
  567. data/lib/cosmos/gui/widgets/packet_log_frame.rb +305 -305
  568. data/lib/cosmos/gui/widgets/realtime_button_bar.rb +98 -98
  569. data/lib/cosmos/interfaces.rb +11 -11
  570. data/lib/cosmos/interfaces/cmd_tlm_server_interface.rb +153 -149
  571. data/lib/cosmos/interfaces/interface.rb +213 -213
  572. data/lib/cosmos/interfaces/linc_interface.rb +360 -360
  573. data/lib/cosmos/interfaces/serial_interface.rb +76 -76
  574. data/lib/cosmos/interfaces/simulated_target_interface.rb +129 -128
  575. data/lib/cosmos/interfaces/stream_interface.rb +156 -156
  576. data/lib/cosmos/interfaces/tcpip_client_interface.rb +60 -60
  577. data/lib/cosmos/interfaces/tcpip_server_interface.rb +154 -154
  578. data/lib/cosmos/interfaces/udp_interface.rb +173 -173
  579. data/lib/cosmos/io/buffered_file.rb +11 -11
  580. data/lib/cosmos/io/cosmos_snmp.rb +50 -50
  581. data/lib/cosmos/io/io_multiplexer.rb +89 -89
  582. data/lib/cosmos/io/json_drb.rb +344 -320
  583. data/lib/cosmos/io/json_drb_object.rb +137 -137
  584. data/lib/cosmos/io/json_rpc.rb +365 -365
  585. data/lib/cosmos/io/posix_serial_driver.rb +145 -145
  586. data/lib/cosmos/io/raw_logger.rb +174 -174
  587. data/lib/cosmos/io/raw_logger_pair.rb +71 -71
  588. data/lib/cosmos/io/serial_driver.rb +85 -85
  589. data/lib/cosmos/io/stderr.rb +36 -36
  590. data/lib/cosmos/io/stdout.rb +36 -36
  591. data/lib/cosmos/io/tcpip_server.rb +583 -532
  592. data/lib/cosmos/io/udp_sockets.rb +152 -152
  593. data/lib/cosmos/io/win32_serial_driver.rb +147 -147
  594. data/lib/cosmos/packet_logs.rb +6 -6
  595. data/lib/cosmos/packet_logs/meta_packet_log_writer.rb +107 -107
  596. data/lib/cosmos/packet_logs/packet_log_reader.rb +441 -439
  597. data/lib/cosmos/packet_logs/packet_log_writer.rb +321 -309
  598. data/lib/cosmos/packet_logs/packet_log_writer_pair.rb +30 -30
  599. data/lib/cosmos/packets/binary_accessor.rb +921 -921
  600. data/lib/cosmos/packets/commands.rb +291 -291
  601. data/lib/cosmos/packets/limits.rb +263 -263
  602. data/lib/cosmos/packets/limits_response.rb +38 -38
  603. data/lib/cosmos/packets/packet.rb +714 -699
  604. data/lib/cosmos/packets/packet_config.rb +1034 -1034
  605. data/lib/cosmos/packets/packet_item.rb +317 -317
  606. data/lib/cosmos/packets/packet_item_limits.rb +128 -128
  607. data/lib/cosmos/packets/structure.rb +421 -386
  608. data/lib/cosmos/packets/structure_item.rb +233 -233
  609. data/lib/cosmos/packets/telemetry.rb +317 -317
  610. data/lib/cosmos/processors.rb +6 -6
  611. data/lib/cosmos/processors/new_packet_log_processor.rb +34 -34
  612. data/lib/cosmos/processors/processor.rb +71 -71
  613. data/lib/cosmos/processors/statistics_processor.rb +65 -65
  614. data/lib/cosmos/processors/watermark_processor.rb +44 -44
  615. data/lib/cosmos/script.rb +9 -9
  616. data/lib/cosmos/script/extract.rb +115 -115
  617. data/lib/cosmos/script/script.rb +1513 -1493
  618. data/lib/cosmos/streams/burst_stream_protocol.rb +25 -25
  619. data/lib/cosmos/streams/fixed_stream_protocol.rb +111 -111
  620. data/lib/cosmos/streams/length_stream_protocol.rb +140 -140
  621. data/lib/cosmos/streams/preidentified_stream_protocol.rb +118 -118
  622. data/lib/cosmos/streams/serial_stream.rb +152 -143
  623. data/lib/cosmos/streams/stream.rb +57 -57
  624. data/lib/cosmos/streams/stream_protocol.rb +369 -369
  625. data/lib/cosmos/streams/tcpip_client_stream.rb +77 -77
  626. data/lib/cosmos/streams/tcpip_socket_stream.rb +139 -139
  627. data/lib/cosmos/streams/template_stream_protocol.rb +140 -140
  628. data/lib/cosmos/streams/terminated_stream_protocol.rb +81 -81
  629. data/lib/cosmos/system.rb +4 -4
  630. data/lib/cosmos/system/system.rb +558 -558
  631. data/lib/cosmos/system/target.rb +178 -178
  632. data/lib/cosmos/tools/cmd_extractor/cmd_extractor.rb +254 -253
  633. data/lib/cosmos/tools/cmd_sender/cmd_sender.rb +716 -716
  634. data/lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb +77 -77
  635. data/lib/cosmos/tools/cmd_sender/cmd_sender_text_edit.rb +70 -70
  636. data/lib/cosmos/tools/cmd_tlm_server/api.rb +936 -940
  637. data/lib/cosmos/tools/cmd_tlm_server/background_task.rb +46 -46
  638. data/lib/cosmos/tools/cmd_tlm_server/background_tasks.rb +67 -63
  639. data/lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server.rb +511 -497
  640. data/lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server_config.rb +241 -241
  641. data/lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server_gui.rb +1033 -1008
  642. data/lib/cosmos/tools/cmd_tlm_server/commanding.rb +112 -112
  643. data/lib/cosmos/tools/cmd_tlm_server/connections.rb +176 -176
  644. data/lib/cosmos/tools/cmd_tlm_server/interface_thread.rb +241 -221
  645. data/lib/cosmos/tools/cmd_tlm_server/interfaces.rb +127 -127
  646. data/lib/cosmos/tools/cmd_tlm_server/packet_logging.rb +132 -132
  647. data/lib/cosmos/tools/cmd_tlm_server/router_thread.rb +66 -66
  648. data/lib/cosmos/tools/cmd_tlm_server/routers.rb +97 -97
  649. data/lib/cosmos/tools/data_viewer/data_viewer.rb +628 -600
  650. data/lib/cosmos/tools/data_viewer/data_viewer_component.rb +167 -167
  651. data/lib/cosmos/tools/data_viewer/dump_component.rb +40 -40
  652. data/lib/cosmos/tools/handbook_creator/handbook_creator.rb +149 -149
  653. data/lib/cosmos/tools/handbook_creator/handbook_creator_config.rb +360 -360
  654. data/lib/cosmos/tools/launcher/launcher.rb +184 -184
  655. data/lib/cosmos/tools/launcher/launcher_config.rb +175 -167
  656. data/lib/cosmos/tools/launcher/launcher_multitool.rb +40 -41
  657. data/lib/cosmos/tools/launcher/launcher_tool.rb +104 -104
  658. data/lib/cosmos/tools/limits_monitor/limits_monitor.rb +796 -768
  659. data/lib/cosmos/tools/opengl_builder/opengl_builder.rb +416 -416
  660. data/lib/cosmos/tools/opengl_builder/scene_config.rb +118 -118
  661. data/lib/cosmos/tools/packet_viewer/packet_viewer.rb +525 -525
  662. data/lib/cosmos/tools/replay/replay.rb +495 -488
  663. data/lib/cosmos/tools/replay/replay_server.rb +91 -91
  664. data/lib/cosmos/tools/script_runner/script_audit.rb +147 -139
  665. data/lib/cosmos/tools/script_runner/script_runner.rb +914 -914
  666. data/lib/cosmos/tools/script_runner/script_runner_config.rb +40 -40
  667. data/lib/cosmos/tools/script_runner/script_runner_frame.rb +1892 -1859
  668. data/lib/cosmos/tools/table_manager/table.rb +70 -70
  669. data/lib/cosmos/tools/table_manager/table_config.rb +764 -764
  670. data/lib/cosmos/tools/table_manager/table_item.rb +74 -74
  671. data/lib/cosmos/tools/table_manager/table_manager.rb +1065 -1065
  672. data/lib/cosmos/tools/table_manager/table_manager_core.rb +539 -539
  673. data/lib/cosmos/tools/test_runner/results_writer.rb +283 -283
  674. data/lib/cosmos/tools/test_runner/test.rb +480 -480
  675. data/lib/cosmos/tools/test_runner/test_runner.rb +1157 -1157
  676. data/lib/cosmos/tools/test_runner/test_runner_chooser.rb +338 -338
  677. data/lib/cosmos/tools/tlm_extractor/text_item_chooser.rb +60 -60
  678. data/lib/cosmos/tools/tlm_extractor/tlm_extractor.rb +1008 -1008
  679. data/lib/cosmos/tools/tlm_extractor/tlm_extractor_config.rb +371 -371
  680. data/lib/cosmos/tools/tlm_extractor/tlm_extractor_processor.rb +60 -60
  681. data/lib/cosmos/tools/tlm_grapher/data_object_adders/housekeeping_data_object_adder.rb +75 -75
  682. data/lib/cosmos/tools/tlm_grapher/data_object_adders/singlexy_data_object_adder.rb +44 -44
  683. data/lib/cosmos/tools/tlm_grapher/data_object_adders/xy_data_object_adder.rb +94 -94
  684. data/lib/cosmos/tools/tlm_grapher/data_object_editors/data_object_editor.rb +61 -61
  685. data/lib/cosmos/tools/tlm_grapher/data_object_editors/housekeeping_data_object_editor.rb +180 -180
  686. data/lib/cosmos/tools/tlm_grapher/data_object_editors/linegraph_data_object_editor.rb +141 -141
  687. data/lib/cosmos/tools/tlm_grapher/data_object_editors/singlexy_data_object_editor.rb +30 -30
  688. data/lib/cosmos/tools/tlm_grapher/data_object_editors/xy_data_object_editor.rb +173 -173
  689. data/lib/cosmos/tools/tlm_grapher/data_objects/data_object.rb +177 -177
  690. data/lib/cosmos/tools/tlm_grapher/data_objects/housekeeping_data_object.rb +412 -409
  691. data/lib/cosmos/tools/tlm_grapher/data_objects/linegraph_data_object.rb +176 -176
  692. data/lib/cosmos/tools/tlm_grapher/data_objects/singlexy_data_object.rb +25 -25
  693. data/lib/cosmos/tools/tlm_grapher/data_objects/xy_data_object.rb +323 -320
  694. data/lib/cosmos/tools/tlm_grapher/plot_editors/linegraph_plot_editor.rb +181 -181
  695. data/lib/cosmos/tools/tlm_grapher/plot_editors/plot_editor.rb +28 -28
  696. data/lib/cosmos/tools/tlm_grapher/plot_editors/singlexy_plot_editor.rb +30 -30
  697. data/lib/cosmos/tools/tlm_grapher/plot_editors/xy_plot_editor.rb +59 -59
  698. data/lib/cosmos/tools/tlm_grapher/plot_gui_objects/linegraph_plot_gui_object.rb +172 -172
  699. data/lib/cosmos/tools/tlm_grapher/plot_gui_objects/singlexy_plot_gui_object.rb +27 -27
  700. data/lib/cosmos/tools/tlm_grapher/plot_gui_objects/xy_plot_gui_object.rb +74 -74
  701. data/lib/cosmos/tools/tlm_grapher/plots/linegraph_plot.rb +201 -201
  702. data/lib/cosmos/tools/tlm_grapher/plots/plot.rb +69 -69
  703. data/lib/cosmos/tools/tlm_grapher/plots/singlexy_plot.rb +20 -20
  704. data/lib/cosmos/tools/tlm_grapher/plots/xy_plot.rb +61 -61
  705. data/lib/cosmos/tools/tlm_grapher/tabbed_plots/overview_tabbed_plots.rb +1278 -1278
  706. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_config.rb +430 -430
  707. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_data_object_editor.rb +107 -107
  708. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_logfile_thread.rb +111 -95
  709. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_plot_editor.rb +101 -101
  710. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_realtime_thread.rb +72 -66
  711. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_tab.rb +57 -57
  712. data/lib/cosmos/tools/tlm_grapher/tabbed_plots_tool/tabbed_plots_tool.rb +1004 -1004
  713. data/lib/cosmos/tools/tlm_grapher/tlm_grapher.rb +87 -87
  714. data/lib/cosmos/tools/tlm_viewer/screen.rb +486 -458
  715. data/lib/cosmos/tools/tlm_viewer/tlm_viewer.rb +563 -544
  716. data/lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb +287 -287
  717. data/lib/cosmos/tools/tlm_viewer/widgets.rb +53 -53
  718. data/lib/cosmos/tools/tlm_viewer/widgets/aging_widget.rb +110 -110
  719. data/lib/cosmos/tools/tlm_viewer/widgets/array_widget.rb +70 -70
  720. data/lib/cosmos/tools/tlm_viewer/widgets/block_widget.rb +61 -61
  721. data/lib/cosmos/tools/tlm_viewer/widgets/button_widget.rb +39 -39
  722. data/lib/cosmos/tools/tlm_viewer/widgets/canvas_widget.rb +62 -62
  723. data/lib/cosmos/tools/tlm_viewer/widgets/canvasimage_widget.rb +41 -41
  724. data/lib/cosmos/tools/tlm_viewer/widgets/canvasimagevalue_widget.rb +57 -57
  725. data/lib/cosmos/tools/tlm_viewer/widgets/canvaslabel_widget.rb +37 -37
  726. data/lib/cosmos/tools/tlm_viewer/widgets/canvaslabelvalue_widget.rb +56 -56
  727. data/lib/cosmos/tools/tlm_viewer/widgets/canvasline_widget.rb +55 -55
  728. data/lib/cosmos/tools/tlm_viewer/widgets/canvaslinevalue_widget.rb +66 -66
  729. data/lib/cosmos/tools/tlm_viewer/widgets/canvasvalue_widget.rb +124 -124
  730. data/lib/cosmos/tools/tlm_viewer/widgets/checkbutton_widget.rb +31 -31
  731. data/lib/cosmos/tools/tlm_viewer/widgets/combobox_widget.rb +30 -30
  732. data/lib/cosmos/tools/tlm_viewer/widgets/formatfontvalue_widget.rb +36 -36
  733. data/lib/cosmos/tools/tlm_viewer/widgets/formatvalue_widget.rb +35 -35
  734. data/lib/cosmos/tools/tlm_viewer/widgets/horizontal_widget.rb +27 -27
  735. data/lib/cosmos/tools/tlm_viewer/widgets/horizontalbox_widget.rb +31 -31
  736. data/lib/cosmos/tools/tlm_viewer/widgets/horizontalline_widget.rb +26 -26
  737. data/lib/cosmos/tools/tlm_viewer/widgets/label_widget.rb +29 -29
  738. data/lib/cosmos/tools/tlm_viewer/widgets/labelformatvalue_widget.rb +39 -39
  739. data/lib/cosmos/tools/tlm_viewer/widgets/labelprogressbar_widget.rb +38 -38
  740. data/lib/cosmos/tools/tlm_viewer/widgets/labeltrendlimitsbar_widget.rb +38 -38
  741. data/lib/cosmos/tools/tlm_viewer/widgets/labelvalue_widget.rb +39 -39
  742. data/lib/cosmos/tools/tlm_viewer/widgets/labelvaluedesc_widget.rb +42 -42
  743. data/lib/cosmos/tools/tlm_viewer/widgets/labelvaluelimitsbar_widget.rb +37 -37
  744. data/lib/cosmos/tools/tlm_viewer/widgets/labelvaluerangebar_widget.rb +37 -37
  745. data/lib/cosmos/tools/tlm_viewer/widgets/layout_widget.rb +34 -34
  746. data/lib/cosmos/tools/tlm_viewer/widgets/limitsbar_widget.rb +178 -178
  747. data/lib/cosmos/tools/tlm_viewer/widgets/linegraph_widget.rb +54 -54
  748. data/lib/cosmos/tools/tlm_viewer/widgets/matrixbycolumns_widget.rb +47 -47
  749. data/lib/cosmos/tools/tlm_viewer/widgets/multi_widget.rb +116 -116
  750. data/lib/cosmos/tools/tlm_viewer/widgets/progressbar_widget.rb +34 -34
  751. data/lib/cosmos/tools/tlm_viewer/widgets/radiobutton_widget.rb +30 -30
  752. data/lib/cosmos/tools/tlm_viewer/widgets/rangebar_widget.rb +57 -57
  753. data/lib/cosmos/tools/tlm_viewer/widgets/screenshotbutton_widget.rb +34 -34
  754. data/lib/cosmos/tools/tlm_viewer/widgets/scrollwindow_widget.rb +35 -35
  755. data/lib/cosmos/tools/tlm_viewer/widgets/sectionheader_widget.rb +33 -33
  756. data/lib/cosmos/tools/tlm_viewer/widgets/tabbook_widget.rb +26 -26
  757. data/lib/cosmos/tools/tlm_viewer/widgets/tabitem_widget.rb +28 -28
  758. data/lib/cosmos/tools/tlm_viewer/widgets/textbox_widget.rb +47 -47
  759. data/lib/cosmos/tools/tlm_viewer/widgets/textfield_widget.rb +26 -26
  760. data/lib/cosmos/tools/tlm_viewer/widgets/timegraph_widget.rb +88 -88
  761. data/lib/cosmos/tools/tlm_viewer/widgets/title_widget.rb +27 -27
  762. data/lib/cosmos/tools/tlm_viewer/widgets/trendbar_widget.rb +130 -130
  763. data/lib/cosmos/tools/tlm_viewer/widgets/trendlimitsbar_widget.rb +46 -46
  764. data/lib/cosmos/tools/tlm_viewer/widgets/value_widget.rb +43 -43
  765. data/lib/cosmos/tools/tlm_viewer/widgets/valuelimitsbar_widget.rb +37 -37
  766. data/lib/cosmos/tools/tlm_viewer/widgets/valuerangebar_widget.rb +37 -37
  767. data/lib/cosmos/tools/tlm_viewer/widgets/vertical_widget.rb +35 -35
  768. data/lib/cosmos/tools/tlm_viewer/widgets/verticalbox_widget.rb +37 -37
  769. data/lib/cosmos/tools/tlm_viewer/widgets/widget.rb +257 -257
  770. data/lib/cosmos/top_level.rb +647 -596
  771. data/lib/cosmos/utilities.rb +11 -10
  772. data/lib/cosmos/utilities/crc.rb +166 -166
  773. data/lib/cosmos/utilities/csv.rb +83 -83
  774. data/lib/cosmos/utilities/logger.rb +137 -137
  775. data/lib/cosmos/utilities/low_fragmentation_array.rb +11 -11
  776. data/lib/cosmos/utilities/message_log.rb +74 -74
  777. data/lib/cosmos/utilities/quaternion.rb +258 -258
  778. data/lib/cosmos/utilities/ruby_lex_utils.rb +313 -313
  779. data/lib/cosmos/utilities/simulated_target.rb +99 -99
  780. data/lib/cosmos/utilities/sleeper.rb +44 -0
  781. data/lib/cosmos/version.rb +12 -12
  782. data/lib/cosmos/win32/excel.rb +66 -66
  783. data/lib/cosmos/win32/win32.rb +387 -387
  784. data/lib/cosmos/win32/win32_main.rb +311 -311
  785. data/roodi.yml +24 -24
  786. data/run_gui_tests.bat +32 -32
  787. data/spec/ccsds/ccsds_packet_spec.rb +67 -67
  788. data/spec/ccsds/ccsds_parser_spec.rb +148 -148
  789. data/spec/config/config_parser_spec.rb +322 -322
  790. data/spec/conversions/conversion_spec.rb +31 -31
  791. data/spec/conversions/generic_conversion_spec.rb +45 -45
  792. data/spec/conversions/new_packet_log_conversion_spec.rb +39 -39
  793. data/spec/conversions/polynomial_conversion_spec.rb +40 -40
  794. data/spec/conversions/processor_conversion_spec.rb +45 -45
  795. data/spec/conversions/received_count_conversion_spec.rb +43 -43
  796. data/spec/conversions/received_time_formatted_conversion_spec.rb +49 -49
  797. data/spec/conversions/received_time_seconds_conversion_spec.rb +50 -50
  798. data/spec/conversions/segmented_polynomial_conversion_spec.rb +51 -51
  799. data/spec/conversions/unix_time_formatted_conversion_spec.rb +74 -74
  800. data/spec/conversions/unix_time_seconds_conversion_spec.rb +76 -76
  801. data/spec/core_ext/array_spec.rb +186 -186
  802. data/spec/core_ext/class_spec.rb +36 -36
  803. data/spec/core_ext/cosmos_io_spec.rb +77 -77
  804. data/spec/core_ext/exception_spec.rb +91 -91
  805. data/spec/core_ext/file_spec.rb +72 -72
  806. data/spec/core_ext/hash_spec.rb +24 -24
  807. data/spec/core_ext/io_spec.rb +46 -46
  808. data/spec/core_ext/kernel_spec.rb +54 -54
  809. data/spec/core_ext/math_spec.rb +116 -116
  810. data/spec/core_ext/matrix_spec.rb +66 -66
  811. data/spec/core_ext/objectspace_spec.rb +29 -29
  812. data/spec/core_ext/range_spec.rb +21 -21
  813. data/spec/core_ext/socket_spec.rb +32 -32
  814. data/spec/core_ext/string_spec.rb +223 -223
  815. data/spec/core_ext/stringio_spec.rb +21 -21
  816. data/spec/core_ext/time_spec.rb +151 -151
  817. data/spec/gui/line_graph/line_clip_spec.rb +322 -322
  818. data/spec/install/config/system/system.txt +33 -33
  819. data/spec/install/config/targets/COSMOS/cmd_tlm/cosmos_server_cmds.txt +41 -41
  820. data/spec/install/config/targets/COSMOS/cmd_tlm/cosmos_server_tlm.txt +15 -15
  821. data/spec/install/config/targets/COSMOS/cmd_tlm_server.txt +6 -6
  822. data/spec/install/config/targets/COSMOS/screens/limits_change.txt +20 -20
  823. data/spec/install/config/targets/COSMOS/screens/version.txt +19 -19
  824. data/spec/install/config/targets/COSMOS/target.txt +5 -5
  825. data/spec/install/config/targets/INST/cmd_tlm/inst_cmd_linc.txt +30 -30
  826. data/spec/install/config/targets/INST/cmd_tlm/inst_cmds.txt +111 -111
  827. data/spec/install/config/targets/INST/cmd_tlm/inst_tlm.txt +236 -236
  828. data/spec/install/config/targets/INST/cmd_tlm/inst_tlm_linc.txt +25 -25
  829. data/spec/install/config/targets/INST/cmd_tlm_server.txt +5 -5
  830. data/spec/install/config/targets/INST/lib/sim_inst.rb +305 -294
  831. data/spec/install/config/targets/INST/target.txt +10 -10
  832. data/spec/install/config/targets/META/cmd_tlm/meta_cmd.txt +4 -4
  833. data/spec/install/config/targets/META/cmd_tlm/meta_tlm.txt +4 -4
  834. data/spec/install/config/targets/SYSTEM/cmd_tlm/limits_groups.txt +7 -7
  835. data/spec/interfaces/cmd_tlm_server_interface_spec.rb +150 -150
  836. data/spec/interfaces/interface_spec.rb +130 -131
  837. data/spec/interfaces/linc_interface_spec.rb +199 -199
  838. data/spec/interfaces/serial_interface_spec.rb +56 -56
  839. data/spec/interfaces/simulated_target_interface_spec.rb +128 -128
  840. data/spec/interfaces/stream_interface_spec.rb +157 -157
  841. data/spec/interfaces/tcpip_client_interface_spec.rb +54 -54
  842. data/spec/interfaces/tcpip_server_interface_spec.rb +151 -151
  843. data/spec/interfaces/udp_interface_spec.rb +175 -177
  844. data/spec/io/buffered_file_spec.rb +113 -113
  845. data/spec/io/io_multiplexer_spec.rb +94 -94
  846. data/spec/io/json_drb_object_spec.rb +99 -99
  847. data/spec/io/json_drb_spec.rb +311 -311
  848. data/spec/io/json_rpc_spec.rb +264 -264
  849. data/spec/io/raw_logger_pair_spec.rb +76 -76
  850. data/spec/io/raw_logger_spec.rb +133 -133
  851. data/spec/io/serial_driver_spec.rb +61 -61
  852. data/spec/io/stderr_spec.rb +32 -32
  853. data/spec/io/stdout_spec.rb +32 -32
  854. data/spec/io/tcpip_server_spec.rb +338 -338
  855. data/spec/io/udp_sockets_spec.rb +94 -94
  856. data/spec/io/win32_serial_driver_spec.rb +88 -88
  857. data/spec/packet_logs/meta_packet_log_writer_spec.rb +170 -170
  858. data/spec/packet_logs/packet_log_reader_spec.rb +408 -408
  859. data/spec/packet_logs/packet_log_writer_pair_spec.rb +30 -30
  860. data/spec/packet_logs/packet_log_writer_spec.rb +223 -223
  861. data/spec/packets/binary_accessor_spec.rb +2073 -2073
  862. data/spec/packets/commands_spec.rb +369 -369
  863. data/spec/packets/limits_response_spec.rb +25 -25
  864. data/spec/packets/limits_spec.rb +326 -326
  865. data/spec/packets/packet_config_spec.rb +1620 -1620
  866. data/spec/packets/packet_item_limits_spec.rb +161 -161
  867. data/spec/packets/packet_item_spec.rb +386 -386
  868. data/spec/packets/packet_spec.rb +1057 -949
  869. data/spec/packets/structure_item_spec.rb +195 -195
  870. data/spec/packets/structure_spec.rb +419 -419
  871. data/spec/packets/telemetry_spec.rb +535 -535
  872. data/spec/processors/new_packet_log_processor_spec.rb +39 -39
  873. data/spec/processors/processor_spec.rb +55 -55
  874. data/spec/processors/statistics_processor_spec.rb +60 -60
  875. data/spec/processors/watermark_processor_spec.rb +51 -51
  876. data/spec/script/script_spec.rb +654 -654
  877. data/spec/spec_helper.rb +154 -148
  878. data/spec/streams/burst_stream_protocol_spec.rb +32 -32
  879. data/spec/streams/fixed_stream_protocol_spec.rb +110 -110
  880. data/spec/streams/length_stream_protocol_spec.rb +297 -297
  881. data/spec/streams/preidentified_stream_protocol_spec.rb +118 -118
  882. data/spec/streams/serial_stream_spec.rb +105 -105
  883. data/spec/streams/stream_protocol_spec.rb +332 -332
  884. data/spec/streams/stream_spec.rb +29 -29
  885. data/spec/streams/tcpip_client_stream_spec.rb +54 -54
  886. data/spec/streams/tcpip_socket_stream_spec.rb +146 -146
  887. data/spec/streams/template_stream_protocol_spec.rb +151 -151
  888. data/spec/streams/terminated_stream_protocol_spec.rb +123 -123
  889. data/spec/system/system_spec.rb +645 -645
  890. data/spec/system/target_spec.rb +248 -248
  891. data/spec/tools/cmd_tlm_server/api_spec.rb +1087 -1113
  892. data/spec/tools/cmd_tlm_server/background_task_spec.rb +32 -32
  893. data/spec/tools/cmd_tlm_server/background_tasks_spec.rb +81 -81
  894. data/spec/tools/cmd_tlm_server/cmd_tlm_server_config_spec.rb +411 -411
  895. data/spec/tools/cmd_tlm_server/cmd_tlm_server_spec.rb +415 -415
  896. data/spec/tools/cmd_tlm_server/commanding_spec.rb +123 -123
  897. data/spec/tools/cmd_tlm_server/connections_spec.rb +147 -147
  898. data/spec/tools/cmd_tlm_server/interface_thread_spec.rb +306 -306
  899. data/spec/tools/cmd_tlm_server/interfaces_spec.rb +252 -238
  900. data/spec/tools/cmd_tlm_server/packet_logging_spec.rb +143 -143
  901. data/spec/tools/cmd_tlm_server/router_thread_spec.rb +98 -101
  902. data/spec/tools/cmd_tlm_server/routers_spec.rb +223 -208
  903. data/spec/top_level/top_level_spec.rb +334 -321
  904. data/spec/utilities/crc_spec.rb +45 -45
  905. data/spec/utilities/csv_spec.rb +97 -97
  906. data/spec/utilities/logger_spec.rb +102 -102
  907. data/spec/utilities/message_log_spec.rb +89 -89
  908. data/spec/utilities/quaternion_spec.rb +107 -107
  909. data/spec/utilities/ruby_lex_utils_spec.rb +86 -86
  910. data/tasks/manifest.rake +22 -22
  911. data/tasks/spec.rake +23 -23
  912. metadata +18 -2
@@ -1,2073 +1,2073 @@
1
- # encoding: ascii-8bit
2
-
3
- # Copyright 2014 Ball Aerospace & Technologies Corp.
4
- # All Rights Reserved.
5
- #
6
- # This program is free software; you can modify and/or redistribute it
7
- # under the terms of the GNU General Public License
8
- # as published by the Free Software Foundation; version 3 with
9
- # attribution addendums as found in the LICENSE.txt
10
-
11
- require 'spec_helper'
12
- require 'cosmos'
13
- require 'cosmos/packets/binary_accessor'
14
-
15
- module Cosmos
16
-
17
- describe BinaryAccessor do
18
-
19
- describe "read" do
20
-
21
- before(:each) do
22
- @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
23
- end
24
-
25
- it "should complain about unrecognized data types" do
26
- lambda { BinaryAccessor.read(0, 32, :BLOB, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "data_type BLOB is not recognized")
27
- end
28
-
29
- it "should complain about bit_offsets before the beginning of the buffer" do
30
- lambda { BinaryAccessor.read(-((@data.length * 8) + 8), 32, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to read STRING at bit_offset #{-((@data.length * 8) + 8)} with bit_size 32")
31
- end
32
-
33
- it "should complain about a negative bit_offset and zero bit_size" do
34
- lambda { BinaryAccessor.read(-8, 0, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "negative or zero bit_sizes (0) cannot be given with negative bit_offsets (-8)")
35
- end
36
-
37
- it "should complain about a negative bit_offset and negative bit_size" do
38
- lambda { BinaryAccessor.read(-8, -8, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "negative or zero bit_sizes (-8) cannot be given with negative bit_offsets (-8)")
39
- end
40
-
41
- it "should complain about negative bit_sizes larger than the size of the buffer" do
42
- lambda { BinaryAccessor.read(0, -((@data.length * 8) + 8), :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to read STRING at bit_offset 0 with bit_size #{-((@data.length * 8) + 8)}")
43
- end
44
-
45
- it "should complain about negative or zero bit_sizes with data_types other than STRING and BLOCK" do
46
- lambda { BinaryAccessor.read(0, -8, :INT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
47
- lambda { BinaryAccessor.read(0, -8, :UINT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
48
- lambda { BinaryAccessor.read(0, -8, :FLOAT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
49
- end
50
-
51
- it "should read aligned strings" do
52
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
53
- if (bit_offset / 8) <= 7
54
- BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN).should eql(@data[(bit_offset / 8)..7])
55
- elsif (bit_offset / 8) == 8
56
- BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN).should eql('')
57
- else
58
- BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN).should eql(@data[(bit_offset / 8)..-1])
59
- end
60
- end
61
- end
62
-
63
- it "should read variable length strings with a zero and negative bit_size" do
64
- 0.step(-(@data.length * 8), -8) do |bit_size|
65
- if (bit_size / 8) >= -8
66
- BinaryAccessor.read(0, bit_size, :STRING, @data, :BIG_ENDIAN).should eql(@data[0..7])
67
- else
68
- BinaryAccessor.read(0, bit_size, :STRING, @data, :BIG_ENDIAN).should eql(@data[0..((bit_size / 8) - 1)])
69
- end
70
- end
71
- end
72
-
73
- it "should read strings with negative bit_offsets" do
74
- BinaryAccessor.read(-16, 16, :STRING, @data, :BIG_ENDIAN).should eql(@data[-2..-1])
75
- end
76
-
77
- it "should complain about unaligned strings" do
78
- lambda { BinaryAccessor.read(1, 32, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
79
- end
80
-
81
- it "should read aligned blocks" do
82
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
83
- BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :BLOCK, @data, :BIG_ENDIAN).should eql(@data[(bit_offset / 8)..-1])
84
- end
85
- end
86
-
87
- it "should read variable length blocks with a zero and negative bit_size" do
88
- 0.step(-(@data.length * 8), -8) do |bit_size|
89
- BinaryAccessor.read(0, bit_size, :BLOCK, @data, :BIG_ENDIAN).should eql(@data[0..((bit_size / 8) - 1)])
90
- end
91
- end
92
-
93
- it "should read blocks with negative bit_offsets" do
94
- BinaryAccessor.read(-16, 16, :BLOCK, @data, :BIG_ENDIAN).should eql(@data[-2..-1])
95
- end
96
-
97
- it "should complain about unaligned blocks" do
98
- lambda { BinaryAccessor.read(7, 16, :BLOCK, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 7 is not byte aligned for data_type BLOCK")
99
- end
100
-
101
- it "should complain if read exceeds the size of the buffer" do
102
- lambda { BinaryAccessor.read(8, 800, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "16 byte buffer insufficient to read STRING at bit_offset 8 with bit_size 800")
103
- end
104
-
105
- it "should read aligned 8-bit unsigned integers" do
106
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
107
- BinaryAccessor.read(bit_offset, 8, :UINT, @data, :BIG_ENDIAN).should eql(@data.getbyte(bit_offset / 8))
108
- end
109
- end
110
-
111
- it "should read aligned 8-bit signed integers" do
112
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
113
- expected = @data.getbyte(bit_offset / 8)
114
- expected = expected - 256 if expected >= 128
115
- BinaryAccessor.read(bit_offset, 8, :INT, @data, :BIG_ENDIAN).should eql(expected)
116
- end
117
- end
118
-
119
- describe "given big endian data" do
120
-
121
- it "should read 1-bit unsigned integers" do
122
- expected = [0x1, 0x0]
123
- bit_size = 1
124
- BinaryAccessor.read(8, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
125
- BinaryAccessor.read(9, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
126
- end
127
-
128
- it "should read 1-bit signed integers" do
129
- expected = [0x1, 0x0]
130
- bit_size = 1
131
- BinaryAccessor.read(8, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
132
- BinaryAccessor.read(9, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
133
- end
134
-
135
- it "should read 7-bit unsigned integers" do
136
- expected = [0x40, 0x02]
137
- bit_size = 7
138
- BinaryAccessor.read(8, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
139
- BinaryAccessor.read(3, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
140
- end
141
-
142
- it "should read 7-bit signed integers" do
143
- expected = [0x40, 0x02]
144
- bit_size = 7
145
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
146
- BinaryAccessor.read(8, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
147
- BinaryAccessor.read(3, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
148
- end
149
-
150
- it "should read 13-bit unsigned integers" do
151
- expected = [0x1C24, 0x20]
152
- bit_size = 13
153
- BinaryAccessor.read(30, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
154
- BinaryAccessor.read(1, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
155
- end
156
-
157
- it "should read 13-bit signed integers" do
158
- expected = [0x1C24, 0x20]
159
- bit_size = 13
160
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
161
- BinaryAccessor.read(30, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
162
- BinaryAccessor.read(1, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
163
- end
164
-
165
- it "should read aligned 16-bit unsigned integers" do
166
- expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
167
- index = 0
168
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
169
- BinaryAccessor.read(bit_offset, 16, :UINT, @data, :BIG_ENDIAN).should eql(expected_array[index])
170
- index += 1
171
- end
172
- end
173
-
174
- it "should read aligned 16-bit signed integers" do
175
- expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
176
- index = 0
177
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
178
- expected = expected_array[index]
179
- expected = expected - 2**16 if expected >= 2**15
180
- BinaryAccessor.read(bit_offset, 16, :INT, @data, :BIG_ENDIAN).should eql(expected)
181
- index += 1
182
- end
183
- end
184
-
185
- it "should read aligned 32-bit unsigned integers" do
186
- expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
187
- index = 0
188
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
189
- BinaryAccessor.read(bit_offset, 32, :UINT, @data, :BIG_ENDIAN).should eql(expected_array[index])
190
- index += 1
191
- end
192
- end
193
-
194
- it "should read aligned 32-bit signed integers" do
195
- expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
196
- index = 0
197
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
198
- expected = expected_array[index]
199
- expected = expected - 2**32 if expected >= 2**31
200
- BinaryAccessor.read(bit_offset, 32, :INT, @data, :BIG_ENDIAN).should eql(expected)
201
- index += 1
202
- end
203
- end
204
-
205
- it "should read aligned 32-bit floats" do
206
- expected_array = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
207
- BinaryAccessor.read(0, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-038).of(expected_array[0])
208
- BinaryAccessor.read(32, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-036).of(expected_array[1])
209
- BinaryAccessor.read(64, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-040).of(expected_array[2])
210
- BinaryAccessor.read(96, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-031).of(expected_array[3])
211
- end
212
-
213
- it "should read 37-bit unsigned integers" do
214
- expected = [0x8182838485 >> 3, 0x00090A0B0C]
215
- bit_size = 37
216
- BinaryAccessor.read(8, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
217
- BinaryAccessor.read(67, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
218
- end
219
-
220
- it "should read 37-bit signed integers" do
221
- expected = [0x8182838485 >> 3, 0x00090A0B0C]
222
- bit_size = 37
223
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
224
- BinaryAccessor.read(8, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
225
- BinaryAccessor.read(67, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
226
- end
227
-
228
- it "should read 63-bit unsigned integers" do
229
- expected = [0x8081828384858687 >> 1, 0x00090A0B0C0D0E0F]
230
- bit_size = 63
231
- BinaryAccessor.read(0, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
232
- BinaryAccessor.read(65, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
233
- end
234
-
235
- it "should read 63-bit signed integers" do
236
- expected = [0x8081828384858687 >> 1, 0x00090A0B0C0D0E0F]
237
- bit_size = 63
238
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
239
- BinaryAccessor.read(0, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
240
- BinaryAccessor.read(65, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
241
- end
242
-
243
- it "should read aligned 64-bit unsigned integers" do
244
- expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
245
- index = 0
246
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
247
- BinaryAccessor.read(bit_offset, 64, :UINT, @data, :BIG_ENDIAN).should eql(expected_array[index])
248
- index += 1
249
- end
250
- end
251
-
252
- it "should read aligned 64-bit signed integers" do
253
- expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
254
- index = 0
255
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
256
- expected = expected_array[index]
257
- expected = expected - 2**64 if expected >= 2**63
258
- BinaryAccessor.read(bit_offset, 64, :INT, @data, :BIG_ENDIAN).should eql(expected)
259
- index += 1
260
- end
261
- end
262
-
263
- it "should read aligned 64-bit floats" do
264
- expected_array = [-3.116851e-306, 1.257060e-308]
265
- BinaryAccessor.read(0, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-306).of(expected_array[0])
266
- BinaryAccessor.read(64, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-308).of(expected_array[1])
267
- end
268
-
269
- it "should complain about unaligned floats" do
270
- lambda { BinaryAccessor.read(17, 32, :FLOAT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 17 is not byte aligned for data_type FLOAT")
271
- end
272
-
273
- it "should complain about mis-sized floats" do
274
- lambda { BinaryAccessor.read(0, 33, :FLOAT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size is 33 but must be 32 or 64 for data_type FLOAT")
275
- end
276
-
277
- end # given big endian data
278
-
279
- describe "given little endian data" do
280
-
281
- it "should complain about ill-defined little endian bitfields" do
282
- lambda { BinaryAccessor.read(3, 7, :UINT, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "LITTLE_ENDIAN bitfield with bit_offset 3 and bit_size 7 is invalid")
283
- end
284
-
285
- it "should read 1-bit unsigned integers" do
286
- expected = [0x1, 0x0]
287
- bit_size = 1
288
- BinaryAccessor.read(8, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
289
- BinaryAccessor.read(9, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
290
- end
291
-
292
- it "should read 1-bit signed integers" do
293
- expected = [0x1, 0x0]
294
- bit_size = 1
295
- BinaryAccessor.read(8, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
296
- BinaryAccessor.read(9, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
297
- end
298
-
299
- it "should read 7-bit unsigned integers" do
300
- expected = [0x40, 0x60]
301
- bit_size = 7
302
- BinaryAccessor.read(8, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
303
- BinaryAccessor.read(15, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
304
- end
305
-
306
- it "should read 7-bit signed integers" do
307
- expected = [0x40, 0x60]
308
- bit_size = 7
309
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
310
- BinaryAccessor.read(8, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
311
- BinaryAccessor.read(15, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
312
- end
313
-
314
- it "should read 13-bit unsigned integers" do
315
- expected = [0x038281 >> 5, 0x0180 >> 2]
316
- bit_size = 13
317
- BinaryAccessor.read(30, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
318
- BinaryAccessor.read(9, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
319
- end
320
-
321
- it "should read 13-bit signed integers" do
322
- expected = [0x038281 >> 5, 0x0180 >> 2]
323
- bit_size = 13
324
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
325
- BinaryAccessor.read(30, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
326
- BinaryAccessor.read(9, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
327
- end
328
-
329
- it "should read aligned 16-bit unsigned integers" do
330
- expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
331
- index = 0
332
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
333
- BinaryAccessor.read(bit_offset, 16, :UINT, @data, :LITTLE_ENDIAN).should eql(expected_array[index])
334
- index += 1
335
- end
336
- end
337
-
338
- it "should read aligned 16-bit signed integers" do
339
- expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
340
- index = 0
341
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
342
- expected = expected_array[index]
343
- expected = expected - 2**16 if expected >= 2**15
344
- BinaryAccessor.read(bit_offset, 16, :INT, @data, :LITTLE_ENDIAN).should eql(expected)
345
- index += 1
346
- end
347
- end
348
-
349
- it "should read aligned 32-bit unsigned integers" do
350
- expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
351
- index = 0
352
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
353
- BinaryAccessor.read(bit_offset, 32, :UINT, @data, :LITTLE_ENDIAN).should eql(expected_array[index])
354
- index += 1
355
- end
356
- end
357
-
358
- it "should read aligned 32-bit signed integers" do
359
- expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
360
- index = 0
361
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
362
- expected = expected_array[index]
363
- expected = expected - 2**32 if expected >= 2**31
364
- BinaryAccessor.read(bit_offset, 32, :INT, @data, :LITTLE_ENDIAN).should eql(expected)
365
- index += 1
366
- end
367
- end
368
-
369
- it "should read aligned 32-bit floats" do
370
- expected_array = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
371
- BinaryAccessor.read(0, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-037).of(expected_array[0])
372
- BinaryAccessor.read(32, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-034).of(expected_array[1])
373
- BinaryAccessor.read(64, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-032).of(expected_array[2])
374
- BinaryAccessor.read(96, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-030).of(expected_array[3])
375
- end
376
-
377
- it "should read 37-bit unsigned integers" do
378
- expected = [0x8584838281 >> 3, 0x0F0E0D0C0B]
379
- bit_size = 37
380
- BinaryAccessor.read(40, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
381
- BinaryAccessor.read(123, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
382
- end
383
-
384
- it "should read 37-bit signed integers" do
385
- expected = [0x8584838281 >> 3, 0x0F0E0D0C0B]
386
- bit_size = 37
387
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
388
- BinaryAccessor.read(40, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
389
- BinaryAccessor.read(123, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
390
- end
391
-
392
- it "should read 63-bit unsigned integers" do
393
- expected = [0x0F0E0D0C0B0A0900 >> 1, 0x0786858483828180]
394
- bit_size = 63
395
- BinaryAccessor.read(120, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
396
- BinaryAccessor.read(57, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
397
- end
398
-
399
- it "should read 63-bit signed integers" do
400
- expected = [0x0F0E0D0C0B0A0900 >> 1, 0x0786858483828180]
401
- bit_size = 63
402
- expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
403
- BinaryAccessor.read(120, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
404
- BinaryAccessor.read(57, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
405
- end
406
-
407
- it "should read aligned 64-bit unsigned integers" do
408
- expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
409
- index = 0
410
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
411
- BinaryAccessor.read(bit_offset, 64, :UINT, @data, :LITTLE_ENDIAN).should eql(expected_array[index])
412
- index += 1
413
- end
414
- end
415
-
416
- it "should read aligned 64-bit signed integers" do
417
- expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
418
- index = 0
419
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
420
- expected = expected_array[index]
421
- expected = expected - 2**64 if expected >= 2**63
422
- BinaryAccessor.read(bit_offset, 64, :INT, @data, :LITTLE_ENDIAN).should eql(expected)
423
- index += 1
424
- end
425
- end
426
-
427
- it "should read aligned 64-bit floats" do
428
- expected_array = [-2.081577e-272, 3.691916e-236]
429
- BinaryAccessor.read(0, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-272).of(expected_array[0])
430
- BinaryAccessor.read(64, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-236).of(expected_array[1])
431
- end
432
-
433
- it "should complain about unaligned floats" do
434
- lambda { BinaryAccessor.read(1, 32, :FLOAT, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
435
- end
436
-
437
- it "should complain about mis-sized floats" do
438
- lambda { BinaryAccessor.read(0, 65, :FLOAT, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
439
- end
440
-
441
- end # little endian
442
- end # describe 'read'
443
-
444
- describe "read_array" do
445
-
446
- before(:each) do
447
- @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
448
- end
449
-
450
- it "should complain with unknown data_type" do
451
- expect { BinaryAccessor.read_array(0, 8, :BLAH, 0, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "data_type BLAH is not recognized")
452
- end
453
-
454
- describe "given little endian data" do
455
-
456
- it "should complain about negative bit sizes" do
457
- expect { BinaryAccessor.read_array(0, -8, :UINT, @data.length*8, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "bit_size -8 must be positive for arrays")
458
- end
459
-
460
- context "when positive or zero bit_offset" do
461
- it "should read the given array_size amount of items" do
462
- data = @data.unpack('C*')
463
- BinaryAccessor.read_array(0, 8, :UINT, 32, @data, :LITTLE_ENDIAN).should eql(data[0..3])
464
- end
465
-
466
- it "should read the given array_size amount of items" do
467
- data = @data.unpack('C*')
468
- BinaryAccessor.read_array(32, 8, :UINT, 32, @data, :LITTLE_ENDIAN).should eql(data[4..7])
469
- end
470
-
471
- it "should read the total buffer given array_size = buffer size" do
472
- data = @data.unpack('c*')
473
-
474
- BinaryAccessor.read_array(0, 8, :INT, @data.length*8, @data, :LITTLE_ENDIAN).should eql(data)
475
- end
476
-
477
- it "should complain with an array_size not a multiple of bit_size" do
478
- data = @data.unpack('C*')
479
- expect { BinaryAccessor.read_array(0, 8, :UINT, 10, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "array_size 10 not a multiple of bit_size 8")
480
- end
481
-
482
- it "should read as many items as possible with a zero array_size" do
483
- data = @data.unpack('C*')
484
- BinaryAccessor.read_array(0, 8, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
485
- end
486
-
487
- it "should exclude the remaining bits if array_size is negative" do
488
- data = @data.unpack('C*')
489
- BinaryAccessor.read_array(0, 8, :UINT, -32, @data, :LITTLE_ENDIAN).should eql(data[0..-5])
490
- end
491
-
492
- it "should return an empty array if the offset equals the negative array size" do
493
- data = @data.unpack('C*')
494
- BinaryAccessor.read_array(@data.length*8-32, 8, :UINT, -32, @data, :LITTLE_ENDIAN).should eql([])
495
- end
496
-
497
- it "should complain if the offset is greater than the negative array size" do
498
- data = @data.unpack('C*')
499
- offset = @data.length * 8 - 16
500
- expect { BinaryAccessor.read_array(offset, 8, :UINT, -32, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "16 byte buffer insufficient to read UINT at bit_offset #{offset} with bit_size 8")
501
- end
502
- end
503
-
504
- context "when negative bit_offset" do
505
- it "should read an array of aligned 8 bit unsigned integers" do
506
- data = @data.unpack('C*')
507
- BinaryAccessor.read_array(-32, 8, :UINT, 32, @data, :LITTLE_ENDIAN).should eql(data[-4..-1])
508
- end
509
-
510
- it "should read an array if the negative offset is the size of the array" do
511
- data = @data.unpack('C*')
512
- BinaryAccessor.read_array(-(@data.length*8), 8, :UINT, @data.length*8, @data, :LITTLE_ENDIAN).should eql(data)
513
- end
514
-
515
- it "should complain if the offset is larger than the buffer" do
516
- expect { BinaryAccessor.read_array(-(@data.length*8+1), 8, :UINT, @data.length*8, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to read #{:UINT} at bit_offset -#{@data.length*8+1} with bit_size 8")
517
- end
518
-
519
- it "should complain with zero array_size" do
520
- expect { BinaryAccessor.read_array(-32, 8, :UINT, 0, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "negative or zero array_size (0) cannot be given with negative bit_offset (-32)")
521
- end
522
-
523
- it "should complain with negative array_size" do
524
- expect { BinaryAccessor.read_array(-32, 8, :UINT, -8, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "negative or zero array_size (-8) cannot be given with negative bit_offset (-32)")
525
- end
526
- end
527
-
528
- it "should complain about accessing data from a buffer which is too small" do
529
- expect { BinaryAccessor.read_array(0, 256, :STRING, 256, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "16 byte buffer insufficient to read STRING at bit_offset 0 with bit_size 256")
530
- end
531
-
532
- it "should return an empty array when passed a zero length buffer" do
533
- BinaryAccessor.read_array(0, 8, :UINT, 32, "", :LITTLE_ENDIAN).should eql([])
534
- end
535
-
536
- it "should complain about unaligned strings" do
537
- lambda { BinaryAccessor.read_array(1, 32, :STRING, 32, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
538
- end
539
-
540
- it "should read a single STRING item" do
541
- BinaryAccessor.read_array(0, 128, :STRING, 0, @data, :LITTLE_ENDIAN).should eql([@data[0..7]])
542
- end
543
-
544
- it "should read a single BLOCK item" do
545
- BinaryAccessor.read_array(0, 128, :BLOCK, 0, @data, :LITTLE_ENDIAN).should eql([@data])
546
- end
547
-
548
- it "should read BLOCK items" do
549
- data = BinaryAccessor.read_array(0, 8, :BLOCK, 0, @data, :LITTLE_ENDIAN)
550
- data.each_with_index {|val, i| val.should eql(@data[i]) }
551
- end
552
-
553
- it "should read 1-bit integers" do
554
- expected = [0x1, 0x0, 0x0, 0x0]
555
- BinaryAccessor.read_array(0, 1, :INT, 4, @data, :LITTLE_ENDIAN).should eql(expected)
556
- BinaryAccessor.read_array(0, 1, :INT, 2, @data, :LITTLE_ENDIAN).should eql(expected[0..1])
557
- end
558
-
559
- it "should complain about little endian bit-fields greater than 1-bit" do
560
- expect { BinaryAccessor.read_array(8, 7, :UINT, 21, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "read_array does not support little endian bit fields with bit_size greater than 1-bit")
561
- end
562
-
563
- it "should read 16 bit UINT items" do
564
- data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
565
- BinaryAccessor.read_array(0, 16, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
566
- end
567
-
568
- it "should read 16 bit INT items" do
569
- data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
570
- data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
571
- BinaryAccessor.read_array(0, 16, :INT, 0, @data, :LITTLE_ENDIAN).should eql(data)
572
- end
573
-
574
- it "should read 32 bit UINT items" do
575
- data = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
576
- BinaryAccessor.read_array(0, 32, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
577
- end
578
-
579
- it "should read 32 bit INT items" do
580
- data = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
581
- data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
582
- BinaryAccessor.read_array(0, 32, :INT, 0, @data, :LITTLE_ENDIAN).should eql(data)
583
- end
584
-
585
- it "should read 64 bit UINT items" do
586
- data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
587
- BinaryAccessor.read_array(0, 64, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
588
- end
589
-
590
- it "should read 64 bit INT items" do
591
- data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
592
- data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
593
- BinaryAccessor.read_array(0, 64, :INT, 0, @data, :LITTLE_ENDIAN).should eql(data)
594
- end
595
-
596
- it "should read aligned 32-bit floats" do
597
- expected_array = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
598
- actual = BinaryAccessor.read_array(0, 32, :FLOAT, 0, @data, :LITTLE_ENDIAN)
599
- actual.each_with_index do |val, index|
600
- val.should be_within(1.0e-030).of(expected_array[index])
601
- end
602
- end
603
-
604
- it "should read aligned 64-bit floats" do
605
- expected_array = [-2.081577e-272, 3.691916e-236]
606
- actual = BinaryAccessor.read_array(0, 64, :FLOAT, 0, @data, :LITTLE_ENDIAN)
607
- actual.each_with_index do |val, index|
608
- val.should be_within(1.0e-236).of(expected_array[index])
609
- end
610
- end
611
-
612
- it "should complain about unaligned floats" do
613
- lambda { BinaryAccessor.read_array(1, 32, :FLOAT, 32, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
614
- end
615
-
616
- it "should complain about mis-sized floats" do
617
- lambda { BinaryAccessor.read_array(0, 65, :FLOAT, 65, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
618
- end
619
- end # given little endian data
620
-
621
- describe "given big endian data" do
622
-
623
- it "should read 7-bit unsigned integers" do
624
- expected = [0x40, 0x60, 0x50]
625
- bit_size = 7
626
- BinaryAccessor.read_array(8, bit_size, :UINT, 21, @data, :BIG_ENDIAN).should eql(expected)
627
- end
628
-
629
- it "should read 16 bit UINT items" do
630
- data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
631
- BinaryAccessor.read_array(0, 16, :UINT, 0, @data, :BIG_ENDIAN).should eql(data)
632
- end
633
-
634
- it "should read 16 bit INT items" do
635
- data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
636
- data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
637
- BinaryAccessor.read_array(0, 16, :INT, 0, @data, :BIG_ENDIAN).should eql(data)
638
- end
639
-
640
- it "should read 32 bit UINT items" do
641
- data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
642
- BinaryAccessor.read_array(0, 32, :UINT, 0, @data, :BIG_ENDIAN).should eql(data)
643
- end
644
-
645
- it "should read 32 bit INT items" do
646
- data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
647
- data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
648
- BinaryAccessor.read_array(0, 32, :INT, 0, @data, :BIG_ENDIAN).should eql(data)
649
- end
650
-
651
- it "should read 64 bit UINT items" do
652
- data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
653
- BinaryAccessor.read_array(0, 64, :UINT, 0, @data, :BIG_ENDIAN).should eql(data)
654
- end
655
-
656
- it "should read 64 bit INT items" do
657
- data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
658
- data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
659
- BinaryAccessor.read_array(0, 64, :INT, 0, @data, :BIG_ENDIAN).should eql(data)
660
- end
661
-
662
- it "should read aligned 32-bit floats" do
663
- expected_array = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
664
- actual = BinaryAccessor.read_array(0, 32, :FLOAT, 0, @data, :BIG_ENDIAN)
665
- actual.each_with_index do |val, index|
666
- val.should be_within(1.0e-030).of(expected_array[index])
667
- end
668
- end
669
-
670
- it "should read aligned 64-bit floats" do
671
- expected_array = [-3.116851e-306, 1.257060e-308]
672
- actual = BinaryAccessor.read_array(0, 64, :FLOAT, 0, @data, :BIG_ENDIAN)
673
- actual.each_with_index do |val, index|
674
- val.should be_within(1.0e-236).of(expected_array[index])
675
- end
676
- end
677
- end # given big endian data
678
- end # describe "read_array"
679
-
680
- describe "write" do
681
-
682
- before(:each) do
683
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
684
- @baseline_data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
685
- end
686
-
687
- it "should complain about unrecognized data types" do
688
- lambda { BinaryAccessor.write(0, 0, 32, :BLOB, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "data_type BLOB is not recognized")
689
- end
690
-
691
- it "should complain about bit_offsets before the beginning of the buffer" do
692
- lambda { BinaryAccessor.write('', -((@data.length * 8) + 8), 32, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to write STRING at bit_offset #{-((@data.length * 8) + 8)} with bit_size 32")
693
- end
694
-
695
- it "should complain about a negative bit_offset and zero bit_size" do
696
- lambda { BinaryAccessor.write('', -8, 0, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "negative or zero bit_sizes (0) cannot be given with negative bit_offsets (-8)")
697
- end
698
-
699
- it "should complain about a negative bit_offset and negative bit_size" do
700
- lambda { BinaryAccessor.write('', -8, -8, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "negative or zero bit_sizes (-8) cannot be given with negative bit_offsets (-8)")
701
- end
702
-
703
- it "should complain about negative or zero bit_sizes with data_types other than STRING and BLOCK" do
704
- lambda { BinaryAccessor.write(0, 0, -8, :INT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
705
- lambda { BinaryAccessor.write(0, 0, -8, :UINT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
706
- lambda { BinaryAccessor.write(0, 0, -8, :FLOAT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
707
- end
708
-
709
- it "should write aligned strings" do
710
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
711
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
712
- @expected_data = @baseline_data.clone
713
- first_byte_index = bit_offset / 8
714
- if first_byte_index > 0
715
- @expected_data[0..(first_byte_index - 1)] = "\x00" * first_byte_index
716
- end
717
- BinaryAccessor.write(@baseline_data[first_byte_index..-1], bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN, :ERROR)
718
- @data.should eql(@expected_data)
719
- end
720
- end
721
-
722
- it "should write variable length strings with a zero and negative bit_size" do
723
- 0.step(-(@baseline_data.length * 8), -8) do |bit_size|
724
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
725
- @expected_data = @baseline_data.clone + ("\x00" * -(bit_size / 8))
726
- BinaryAccessor.write(@baseline_data, 0, bit_size, :STRING, @data, :BIG_ENDIAN, :ERROR)
727
- @data.should eql(@expected_data)
728
- end
729
- end
730
-
731
- it "should write strings with negative bit_offsets" do
732
- BinaryAccessor.write(@baseline_data[14..15], -16, 16, :STRING, @data, :BIG_ENDIAN, :ERROR)
733
- @data.should eql(("\x00" * 14) + @baseline_data[14..15])
734
- end
735
-
736
- it "should complain about unaligned strings" do
737
- lambda { BinaryAccessor.write('', 1, 32, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
738
- end
739
-
740
- it "should write aligned blocks" do
741
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
742
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
743
- @expected_data = @baseline_data.clone
744
- first_byte_index = bit_offset / 8
745
- if first_byte_index > 0
746
- @expected_data[0..(first_byte_index - 1)] = "\x00" * first_byte_index
747
- end
748
- BinaryAccessor.write(@baseline_data[first_byte_index..-1], bit_offset, (@data.length * 8) - bit_offset, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
749
- @data.should eql(@expected_data)
750
- end
751
- end
752
-
753
- it "should write variable length blocks with a zero and negative bit_size" do
754
- 0.step(-(@data.length * 8), -8) do |bit_size|
755
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
756
- @expected_data = @baseline_data.clone + ("\x00" * -(bit_size / 8))
757
- BinaryAccessor.write(@baseline_data, 0, bit_size, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
758
- @data.should eql(@expected_data)
759
- end
760
- end
761
-
762
- it "should write blocks with negative bit_offsets" do
763
- BinaryAccessor.write(@baseline_data[0..1], -16, 16, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
764
- @data[-2..-1].should eql(@baseline_data[0..1])
765
- end
766
-
767
- it "should write a blank string with zero bit size" do
768
- BinaryAccessor.write('', 0, 0, :STRING, @data, :BIG_ENDIAN, :ERROR)
769
- @data.should eql('')
770
- end
771
-
772
- it "should write a blank block with zero bit size" do
773
- BinaryAccessor.write('', 0, 0, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
774
- @data.should eql('')
775
- end
776
-
777
- it "should write a shorter string with zero bit size" do
778
- BinaryAccessor.write("\x00\x00\x00\x00\x00\x00\x00\x00", 0, 0, :STRING, @data, :BIG_ENDIAN, :ERROR)
779
- @data.should eql("\x00\x00\x00\x00\x00\x00\x00\x00")
780
- end
781
-
782
- it "should write a shorter block with zero bit size" do
783
- BinaryAccessor.write("\x00\x00\x00\x00\x00\x00\x00\x00", 0, 0, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
784
- @data.should eql("\x00\x00\x00\x00\x00\x00\x00\x00")
785
- end
786
-
787
- it "should write a shorter string and zero fill to the given bit size" do
788
- @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
789
- BinaryAccessor.write("\x01\x02\x03\x04\x05\x06\x07\x08", 0, 128, :STRING, @data, :BIG_ENDIAN, :ERROR)
790
- @data.should eql("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00\x00\x00\x00\x00\x00\x00")
791
- end
792
-
793
- it "should write a shorter block and zero fill to the given bit size" do
794
- @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
795
- BinaryAccessor.write("\x01\x02\x03\x04\x05\x06\x07\x08", 0, 128, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
796
- @data.should eql("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00\x00\x00\x00\x00\x00\x00")
797
- end
798
-
799
- it "should complain about unaligned blocks" do
800
- lambda { BinaryAccessor.write(@baseline_data, 7, 16, :BLOCK, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 7 is not byte aligned for data_type BLOCK")
801
- end
802
-
803
- it "should complain if write exceeds the size of the buffer" do
804
- lambda { BinaryAccessor.write(@baseline_data, 8, 800, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "16 byte buffer insufficient to write STRING at bit_offset 8 with bit_size 800")
805
- end
806
-
807
- it "should write aligned 8-bit unsigned integers" do
808
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
809
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
810
- byte_index = bit_offset / 8
811
- BinaryAccessor.write(@baseline_data.getbyte(byte_index), bit_offset, 8, :UINT, @data, :BIG_ENDIAN, :ERROR)
812
- @data[byte_index..byte_index].should eq(@baseline_data[byte_index..byte_index])
813
- end
814
- end
815
-
816
- it "should write aligned 8-bit signed integers" do
817
- 0.step((@data.length - 1) * 8, 8) do |bit_offset|
818
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
819
- byte_index = bit_offset / 8
820
- value = @baseline_data.getbyte(byte_index)
821
- value = value - 256 if value >= 128
822
- BinaryAccessor.write(value, bit_offset, 8, :INT, @data, :BIG_ENDIAN, :ERROR)
823
- @data[byte_index..byte_index].should eql(@baseline_data[byte_index..byte_index])
824
- end
825
- end
826
-
827
- describe "given big endian data" do
828
-
829
- it "should write 1-bit unsigned integers" do
830
- BinaryAccessor.write(0x1, 8, 1, :UINT, @data, :BIG_ENDIAN, :ERROR)
831
- BinaryAccessor.write(0x0, 9, 1, :UINT, @data, :BIG_ENDIAN, :ERROR)
832
- BinaryAccessor.write(0x1, 10, 1, :UINT, @data, :BIG_ENDIAN, :ERROR)
833
- @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
834
- end
835
-
836
- it "should write 1-bit signed integers" do
837
- BinaryAccessor.write(0x1, 8, 1, :INT, @data, :BIG_ENDIAN, :ERROR)
838
- BinaryAccessor.write(0x0, 9, 1, :INT, @data, :BIG_ENDIAN, :ERROR)
839
- BinaryAccessor.write(0x1, 10, 1, :INT, @data, :BIG_ENDIAN, :ERROR)
840
- @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
841
- end
842
-
843
- it "should write 7-bit unsigned integers" do
844
- BinaryAccessor.write(0x40, 8, 7, :UINT, @data, :BIG_ENDIAN, :ERROR)
845
- @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
846
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
847
- BinaryAccessor.write(0x20, 3, 7, :UINT, @data, :BIG_ENDIAN, :ERROR)
848
- @data.should eql("\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
849
- end
850
-
851
- it "should write 7-bit signed integers" do
852
- BinaryAccessor.write(-64, 8, 7, :INT, @data, :BIG_ENDIAN, :ERROR)
853
- @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
854
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
855
- BinaryAccessor.write(32, 3, 7, :INT, @data, :BIG_ENDIAN, :ERROR)
856
- @data.should eql("\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
857
- end
858
-
859
- it "should write 13-bit unsigned integers" do
860
- BinaryAccessor.write(0x1C24, 30, 13, :UINT, @data, :BIG_ENDIAN, :ERROR)
861
- @data.should eql("\x00\x00\x00\x03\x84\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
862
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
863
- BinaryAccessor.write(0x0020, 1, 13, :UINT, @data, :BIG_ENDIAN, :ERROR)
864
- @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
865
- end
866
-
867
- it "should write 13-bit signed integers" do
868
- BinaryAccessor.write(-988, 30, 13, :INT, @data, :BIG_ENDIAN, :ERROR)
869
- @data.should eql("\x00\x00\x00\x03\x84\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
870
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
871
- BinaryAccessor.write(32, 1, 13, :INT, @data, :BIG_ENDIAN, :ERROR)
872
- @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
873
- end
874
-
875
- it "should write aligned 16-bit unsigned integers" do
876
- expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
877
- index = 0
878
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
879
- BinaryAccessor.write(expected_array[index], bit_offset, 16, :UINT, @data, :BIG_ENDIAN, :ERROR)
880
- index += 1
881
- end
882
- @data.should eql(@baseline_data)
883
- end
884
-
885
- it "should write aligned 16-bit signed integers" do
886
- expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
887
- index = 0
888
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
889
- expected = expected_array[index]
890
- expected = expected - 2**16 if expected >= 2**15
891
- BinaryAccessor.write(expected, bit_offset, 16, :INT, @data, :BIG_ENDIAN, :ERROR)
892
- index += 1
893
- end
894
- @data.should eql(@baseline_data)
895
- end
896
-
897
- it "should write aligned 32-bit unsigned integers" do
898
- expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
899
- index = 0
900
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
901
- BinaryAccessor.write(expected_array[index], bit_offset, 32, :UINT, @data, :BIG_ENDIAN, :ERROR)
902
- index += 1
903
- end
904
- @data.should eql(@baseline_data)
905
- end
906
-
907
- it "should write aligned 32-bit signed integers" do
908
- expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
909
- index = 0
910
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
911
- expected = expected_array[index]
912
- expected = expected - 2**32 if expected >= 2**31
913
- BinaryAccessor.write(expected_array[index], bit_offset, 32, :INT, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
914
- index += 1
915
- end
916
- @data.should eql(@baseline_data)
917
- end
918
-
919
- it "should write aligned 32-bit floats" do
920
- expected_array = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
921
- BinaryAccessor.write(expected_array[0], 0, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
922
- BinaryAccessor.write(expected_array[1], 32, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
923
- BinaryAccessor.write(expected_array[2], 64, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
924
- BinaryAccessor.write(expected_array[3], 96, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
925
- BinaryAccessor.read(0, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-038).of(expected_array[0])
926
- BinaryAccessor.read(32, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-036).of(expected_array[1])
927
- BinaryAccessor.read(64, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-040).of(expected_array[2])
928
- BinaryAccessor.read(96, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-031).of(expected_array[3])
929
- end
930
-
931
- it "should write 37-bit unsigned integers" do
932
- BinaryAccessor.write(0x8182838485 >> 3, 8, 37, :UINT, @data, :BIG_ENDIAN, :ERROR)
933
- BinaryAccessor.write(0x00090A0B0C, 67, 37, :UINT, @data, :BIG_ENDIAN, :ERROR)
934
- @data.should eql("\x00\x81\x82\x83\x84\x80\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
935
- end
936
-
937
- it "should write 37-bit signed integers" do
938
- BinaryAccessor.write((0x8182838485 >> 3) - 2**37, 8, 37, :INT, @data, :BIG_ENDIAN, :ERROR)
939
- BinaryAccessor.write(0x00090A0B0C, 67, 37, :INT, @data, :BIG_ENDIAN, :ERROR)
940
- @data.should eql("\x00\x81\x82\x83\x84\x80\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
941
- end
942
-
943
- it "should write 63-bit unsigned integers" do
944
- BinaryAccessor.write(0x8081828384858687 >> 1, 0, 63, :UINT, @data, :BIG_ENDIAN, :ERROR)
945
- BinaryAccessor.write(0x00090A0B0C0D0E0F, 65, 63, :UINT, @data, :BIG_ENDIAN, :ERROR)
946
- @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x86\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
947
- end
948
-
949
- it "should write 63-bit signed integers" do
950
- BinaryAccessor.write((0x8081828384858687 >> 1) - 2**63, 0, 63, :INT, @data, :BIG_ENDIAN, :ERROR)
951
- BinaryAccessor.write(0x00090A0B0C0D0E0F, 65, 63, :INT, @data, :BIG_ENDIAN, :ERROR)
952
- @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x86\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
953
- end
954
-
955
- it "should write aligned 64-bit unsigned integers" do
956
- expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
957
- index = 0
958
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
959
- BinaryAccessor.write(expected_array[index], bit_offset, 64, :UINT, @data, :BIG_ENDIAN, :ERROR)
960
- index += 1
961
- end
962
- @data.should eql(@baseline_data)
963
- end
964
-
965
- it "should write aligned 64-bit signed integers" do
966
- expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
967
- index = 0
968
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
969
- expected = expected_array[index]
970
- expected = expected - 2**64 if expected >= 2**63
971
- BinaryAccessor.write(expected_array[index], bit_offset, 64, :INT, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
972
- index += 1
973
- end
974
- @data.should eql(@baseline_data)
975
- end
976
-
977
- it "should write aligned 64-bit floats" do
978
- expected_array = [-3.116851e-306, 1.257060e-308]
979
- BinaryAccessor.write(expected_array[0], 0, 64, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
980
- BinaryAccessor.write(expected_array[1], 64, 64, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
981
- BinaryAccessor.read(0, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-306).of(expected_array[0])
982
- BinaryAccessor.read(64, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-308).of(expected_array[1])
983
- end
984
-
985
- it "should complain about unaligned floats" do
986
- lambda { BinaryAccessor.write(0.0, 17, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 17 is not byte aligned for data_type FLOAT")
987
- end
988
-
989
- it "should complain about mis-sized floats" do
990
- lambda { BinaryAccessor.write(0.0, 0, 33, :FLOAT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 33 but must be 32 or 64 for data_type FLOAT")
991
- end
992
-
993
- end # given big endian data
994
-
995
- describe "given little endian data" do
996
-
997
- it "should complain about ill-defined little endian bitfields" do
998
- lambda { BinaryAccessor.write(0x1, 3, 7, :UINT, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "LITTLE_ENDIAN bitfield with bit_offset 3 and bit_size 7 is invalid")
999
- end
1000
-
1001
- it "should write 1-bit unsigned integers" do
1002
- BinaryAccessor.write(0x1, 8, 1, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1003
- BinaryAccessor.write(0x0, 9, 1, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1004
- BinaryAccessor.write(0x1, 10, 1, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1005
- @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1006
- end
1007
-
1008
- it "should write 1-bit signed integers" do
1009
- BinaryAccessor.write(0x1, 8, 1, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1010
- BinaryAccessor.write(0x0, 9, 1, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1011
- BinaryAccessor.write(0x1, 10, 1, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1012
- @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1013
- end
1014
-
1015
- it "should write 7-bit unsigned integers" do
1016
- BinaryAccessor.write(0x40, 8, 7, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1017
- @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1018
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1019
- BinaryAccessor.write(0x7F, 11, 7, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1020
- @data.should eql("\xC0\x1F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1021
- end
1022
-
1023
- it "should write 7-bit signed integers" do
1024
- BinaryAccessor.write(-64, 8, 7, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1025
- @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1026
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1027
- BinaryAccessor.write(32, 11, 7, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1028
- @data.should eql("\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1029
- end
1030
-
1031
- it "should write 13-bit unsigned integers" do
1032
- BinaryAccessor.write(0x1C24, 30, 13, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1033
- @data.should eql("\x00\x80\x84\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1034
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1035
- BinaryAccessor.write(0x0020, 9, 13, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1036
- @data.should eql("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1037
- end
1038
-
1039
- it "should write 13-bit signed integers" do
1040
- BinaryAccessor.write(-988, 30, 13, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1041
- @data.should eql("\x00\x80\x84\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1042
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1043
- BinaryAccessor.write(32, 9, 13, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1044
- @data.should eql("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1045
- end
1046
-
1047
- it "should write aligned 16-bit unsigned integers" do
1048
- expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1049
- index = 0
1050
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
1051
- BinaryAccessor.write(expected_array[index], bit_offset, 16, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1052
- index += 1
1053
- end
1054
- @data.should eql(@baseline_data)
1055
- end
1056
-
1057
- it "should write aligned 16-bit signed integers" do
1058
- expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1059
- index = 0
1060
- 0.step((@data.length - 1) * 8, 16) do |bit_offset|
1061
- expected = expected_array[index]
1062
- expected = expected - 2**16 if expected >= 2**15
1063
- BinaryAccessor.write(expected, bit_offset, 16, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1064
- index += 1
1065
- end
1066
- @data.should eql(@baseline_data)
1067
- end
1068
-
1069
- it "should write aligned 32-bit unsigned integers" do
1070
- expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1071
- index = 0
1072
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
1073
- BinaryAccessor.write(expected_array[index], bit_offset, 32, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1074
- index += 1
1075
- end
1076
- @data.should eql(@baseline_data)
1077
- end
1078
-
1079
- it "should write aligned 32-bit signed integers" do
1080
- expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1081
- index = 0
1082
- 0.step((@data.length - 1) * 8, 32) do |bit_offset|
1083
- expected = expected_array[index]
1084
- expected = expected - 2**32 if expected >= 2**31
1085
- BinaryAccessor.write(expected_array[index], bit_offset, 32, :INT, @data, :LITTLE_ENDIAN, :ERROR_ALLOW_HEX)
1086
- index += 1
1087
- end
1088
- @data.should eql(@baseline_data)
1089
- end
1090
-
1091
- it "should write aligned 32-bit floats" do
1092
- expected_array = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
1093
- BinaryAccessor.write(expected_array[0], 0, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1094
- BinaryAccessor.write(expected_array[1], 32, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1095
- BinaryAccessor.write(expected_array[2], 64, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1096
- BinaryAccessor.write(expected_array[3], 96, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1097
- BinaryAccessor.read(0, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-037).of(expected_array[0])
1098
- BinaryAccessor.read(32, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-034).of(expected_array[1])
1099
- BinaryAccessor.read(64, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-032).of(expected_array[2])
1100
- BinaryAccessor.read(96, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-030).of(expected_array[3])
1101
- end
1102
-
1103
- it "should write 37-bit unsigned integers" do
1104
- BinaryAccessor.write(0x1584838281, 43, 37, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1105
- BinaryAccessor.write(0x0C0B0A0900 >> 3, 96, 37, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1106
- @data.should eql("\x00\x81\x82\x83\x84\x15\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
1107
- end
1108
-
1109
- it "should write 37-bit signed integers" do
1110
- BinaryAccessor.write(0x1584838281 - 2**37, 43, 37, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1111
- BinaryAccessor.write(0x0C0B0A0900 >> 3, 96, 37, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1112
- @data.should eql("\x00\x81\x82\x83\x84\x15\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
1113
- end
1114
-
1115
- it "should write 63-bit unsigned integers" do
1116
- BinaryAccessor.write(0x4786858483828180, 57, 63, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1117
- BinaryAccessor.write(0x0F0E0D0C0B0A0900 >> 1, 120, 63, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1118
- @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x47\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
1119
- end
1120
-
1121
- it "should write 63-bit signed integers" do
1122
- BinaryAccessor.write(0x4786858483828180 - 2**63, 57, 63, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1123
- BinaryAccessor.write(0x0F0E0D0C0B0A0900 >> 1, 120, 63, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1124
- @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x47\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
1125
- end
1126
-
1127
- it "should write aligned 64-bit unsigned integers" do
1128
- expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1129
- index = 0
1130
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
1131
- BinaryAccessor.write(expected_array[index], bit_offset, 64, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1132
- index += 1
1133
- end
1134
- @data.should eql(@baseline_data)
1135
- end
1136
-
1137
- it "should write aligned 64-bit signed integers" do
1138
- expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1139
- index = 0
1140
- 0.step((@data.length - 1) * 8, 64) do |bit_offset|
1141
- expected = expected_array[index]
1142
- expected = expected - 2**64 if expected >= 2**63
1143
- BinaryAccessor.write(expected_array[index], bit_offset, 64, :INT, @data, :LITTLE_ENDIAN, :ERROR_ALLOW_HEX)
1144
- index += 1
1145
- end
1146
- @data.should eql(@baseline_data)
1147
- end
1148
-
1149
- it "should write aligned 64-bit floats" do
1150
- expected_array = [-2.081577e-272, 3.691916e-236]
1151
- BinaryAccessor.write(expected_array[0], 0, 64, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1152
- BinaryAccessor.write(expected_array[1], 64, 64, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1153
- BinaryAccessor.read(0, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-272).of(expected_array[0])
1154
- BinaryAccessor.read(64, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-236).of(expected_array[1])
1155
- end
1156
-
1157
- it "should complain about unaligned floats" do
1158
- lambda { BinaryAccessor.write(0.0, 1, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
1159
- end
1160
-
1161
- it "should complain about mis-sized floats" do
1162
- lambda { BinaryAccessor.write(0.0, 0, 65, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
1163
- end
1164
-
1165
- end # given little endian data
1166
-
1167
- describe "should support overflow types" do
1168
- before(:each) do
1169
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1170
- end
1171
-
1172
- it "should prevent overflow of STRING" do
1173
- lambda { BinaryAccessor.write("abcde", 0, 32, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type STRING")
1174
- end
1175
-
1176
- it "should prevent overflow of BLOCK" do
1177
- lambda { BinaryAccessor.write("abcde", 0, 32, :BLOCK, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type BLOCK")
1178
- end
1179
-
1180
- it "should prevent overflow of 8-bit INT" do
1181
- bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1)
1182
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1183
- value = -(value + 1)
1184
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1185
- end
1186
-
1187
- it "should prevent overflow of 16-bit INT" do
1188
- bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1)
1189
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1190
- value = -(value + 1)
1191
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1192
- end
1193
-
1194
- it "should prevent overflow of 32-bit INT" do
1195
- bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1)
1196
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1197
- value = -(value + 1)
1198
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1199
- end
1200
-
1201
- it "should prevent overflow of 64-bit INT" do
1202
- bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1)
1203
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1204
- value = -(value + 1)
1205
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1206
- end
1207
-
1208
- it "should prevent overflow of 3-bit INT" do
1209
- bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1)
1210
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1211
- value = -(value + 1)
1212
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1213
- end
1214
-
1215
- it "should prevent overflow of 8-bit UINT" do
1216
- bit_size = 8; data_type = :UINT; value = 2 ** bit_size
1217
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1218
- value = -1
1219
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1220
- end
1221
-
1222
- it "should prevent overflow of 16-bit UINT" do
1223
- bit_size = 16; data_type = :UINT; value = 2 ** bit_size
1224
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1225
- value = -1
1226
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1227
- end
1228
-
1229
- it "should prevent overflow of 32-bit UINT" do
1230
- bit_size = 32; data_type = :UINT; value = 2 ** bit_size
1231
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1232
- value = -1
1233
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1234
- end
1235
-
1236
- it "should prevent overflow of 64-bit UINT" do
1237
- bit_size = 64; data_type = :UINT; value = 2 ** bit_size
1238
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1239
- value = -1
1240
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1241
- end
1242
-
1243
- it "should prevent overflow of 3-bit UINT" do
1244
- bit_size = 3; data_type = :UINT; value = 2 ** bit_size
1245
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1246
- value = -1
1247
- lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1248
- end
1249
-
1250
- it "should truncate STRING" do
1251
- BinaryAccessor.write("abcde", 0, 32, :STRING, @data, :BIG_ENDIAN, :TRUNCATE)
1252
- @data[0..4].should eql "abcd\x00"
1253
- end
1254
-
1255
- it "should truncate BLOCK" do
1256
- BinaryAccessor.write("abcde", 0, 32, :BLOCK, @data, :BIG_ENDIAN, :TRUNCATE)
1257
- @data[0..4].should eql "abcd\x00"
1258
- end
1259
-
1260
- it "should truncate 8-bit INT" do
1261
- bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1262
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1263
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1264
- end
1265
-
1266
- it "should truncate 16-bit INT" do
1267
- bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1268
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1269
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1270
- end
1271
-
1272
- it "should truncate 32-bit INT" do
1273
- bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1274
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1275
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1276
- end
1277
-
1278
- it "should truncate 64-bit INT" do
1279
- bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1280
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1281
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1282
- end
1283
-
1284
- it "should truncate 3-bit INT" do
1285
- bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1286
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1287
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1288
- end
1289
-
1290
- it "should truncate 8-bit UINT" do
1291
- bit_size = 8; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1292
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1293
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1294
- end
1295
-
1296
- it "should truncate 16-bit UINT" do
1297
- bit_size = 16; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1298
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1299
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1300
- end
1301
-
1302
- it "should truncate 32-bit UINT" do
1303
- bit_size = 32; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1304
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1305
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1306
- end
1307
-
1308
- it "should truncate 64-bit UINT" do
1309
- bit_size = 64; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1310
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1311
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1312
- end
1313
-
1314
- it "should truncate 3-bit UINT" do
1315
- bit_size = 3; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1316
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1317
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1318
- end
1319
-
1320
- it "should saturate 8-bit INT" do
1321
- bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1322
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1323
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1324
- value = -(value + 1); saturated_value = value + 1
1325
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1326
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1327
- end
1328
-
1329
- it "should saturate 16-bit INT" do
1330
- bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1331
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1332
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1333
- value = -(value + 1); saturated_value = value + 1
1334
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1335
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1336
- end
1337
-
1338
- it "should saturate 32-bit INT" do
1339
- bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1340
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1341
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1342
- value = -(value + 1); saturated_value = value + 1
1343
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1344
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1345
- end
1346
-
1347
- it "should saturate 64-bit INT" do
1348
- bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1349
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1350
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1351
- value = -(value + 1); saturated_value = value + 1
1352
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1353
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1354
- end
1355
-
1356
- it "should saturate 3-bit INT" do
1357
- bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1358
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1359
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1360
- value = -(value + 1); saturated_value = value + 1
1361
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1362
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1363
- end
1364
-
1365
- it "should saturate 8-bit UINT" do
1366
- bit_size = 8; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1367
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1368
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1369
- value = -1; saturated_value = 0
1370
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1371
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1372
- end
1373
-
1374
- it "should saturate 16-bit UINT" do
1375
- bit_size = 16; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1376
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1377
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1378
- value = -1; saturated_value = 0
1379
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1380
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1381
- end
1382
-
1383
- it "should saturate 32-bit UINT" do
1384
- bit_size = 32; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1385
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1386
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1387
- value = -1; saturated_value = 0
1388
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1389
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1390
- end
1391
-
1392
- it "should saturate 64-bit UINT" do
1393
- bit_size = 64; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1394
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1395
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1396
- value = -1; saturated_value = 0
1397
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1398
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1399
- end
1400
-
1401
- it "should saturate 3-bit UINT" do
1402
- bit_size = 3; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1403
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1404
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1405
- value = -1; saturated_value = 0
1406
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1407
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1408
- end
1409
-
1410
- it "should allow hex value entry of 8-bit INT" do
1411
- bit_size = 8; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1412
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1413
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1414
- end
1415
-
1416
- it "should allow hex value entry of 16-bit INT" do
1417
- bit_size = 16; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1418
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1419
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1420
- end
1421
-
1422
- it "should allow hex value entry of 32-bit INT" do
1423
- bit_size = 32; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1424
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1425
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1426
- end
1427
-
1428
- it "should allow hex value entry of 64-bit INT" do
1429
- bit_size = 64; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1430
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1431
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1432
- end
1433
-
1434
- it "should allow hex value entry of 3-bit INT" do
1435
- bit_size = 3; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1436
- BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1437
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1438
- end
1439
-
1440
- end
1441
-
1442
- end # describe "write"
1443
-
1444
- describe "write_array" do
1445
- before(:each) do
1446
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1447
- @data_array = []
1448
- @data.length.times {|i| @data_array << @data[i] }
1449
- @baseline_data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
1450
- @baseline_data_array = []
1451
- @baseline_data.length.times {|i| @baseline_data_array << @baseline_data[i] }
1452
- end
1453
-
1454
- it "should complain about value other than Array" do
1455
- lambda { BinaryAccessor.write_array("", 0, 32, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "values must be an Array type class is String")
1456
- end
1457
-
1458
- it "should complain about unrecognized data types" do
1459
- lambda { BinaryAccessor.write_array([0], 0, 32, :BLOB, 0, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "data_type BLOB is not recognized")
1460
- end
1461
-
1462
- it "should complain about bit_offsets before the beginning of the buffer" do
1463
- expect { BinaryAccessor.write_array([''], -((@data.length * 8) + 8), 32, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to write STRING at bit_offset #{-((@data.length * 8) + 8)} with bit_size 32")
1464
- end
1465
-
1466
- it "should write if a negative bit_offset is equal to length of buffer" do
1467
- BinaryAccessor.write_array(@baseline_data_array, -(@data.length * 8), 8, :BLOCK, @baseline_data_array.length*8, @data, :BIG_ENDIAN, :ERROR)
1468
- @data.should eql @baseline_data
1469
- end
1470
-
1471
- it "should complain about a negative or zero bit_size" do
1472
- expect { BinaryAccessor.write_array([''], 0, 0, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_size 0 must be positive for arrays")
1473
- expect { BinaryAccessor.write_array([''], 0, -8, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_size -8 must be positive for arrays")
1474
- end
1475
-
1476
- it "should write aligned strings with fixed array_size" do
1477
- data = @data.clone
1478
- BinaryAccessor.write_array(@baseline_data_array, 0, 8, :STRING, @baseline_data_array.length*8, data, :BIG_ENDIAN, :ERROR)
1479
- data.should eql(@baseline_data)
1480
- end
1481
-
1482
- it "should write aligned strings with zero array_size" do
1483
- BinaryAccessor.write_array(@baseline_data_array, 0, 8, :STRING, 0, @data, :BIG_ENDIAN, :ERROR)
1484
- @data.should eql(@baseline_data)
1485
- end
1486
-
1487
- it "should write strings with negative bit_offsets" do
1488
- BinaryAccessor.write_array(@baseline_data_array[14..15], -16, 8, :STRING, 16, @data, :BIG_ENDIAN, :ERROR)
1489
- @data.should eql(("\x00" * 14) + @baseline_data[14..15])
1490
- end
1491
-
1492
- it "should complain about unaligned strings" do
1493
- expect { BinaryAccessor.write_array([], 1, 32, :STRING, 32, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
1494
- end
1495
-
1496
- it "should complain if pass more values than the given array_size can hold" do
1497
- expect { BinaryAccessor.write_array(@baseline_data_array, 0, 8, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "too many values #{@baseline_data_array.length} for given array_size 32 and bit_size 8")
1498
- end
1499
-
1500
- it "should write blocks with fixed array_size" do
1501
- BinaryAccessor.write_array(@baseline_data_array, 0, 8, :BLOCK, @baseline_data_array.length*8, @data, :BIG_ENDIAN, :ERROR)
1502
- @data.should eql(@baseline_data)
1503
- end
1504
-
1505
- it "should zero fill if array_size > number of values passed" do
1506
- data = @baseline_data.clone
1507
- BinaryAccessor.write_array(["\x01","\x02","\x03","\x04"], 0, 8, :BLOCK, 64, @baseline_data, :BIG_ENDIAN, :ERROR)
1508
- @baseline_data.should eql("\x01\x02\x03\x04" + "\x00" * 4 + data[8..-1])
1509
- end
1510
-
1511
- it "should write blocks with fixed array_size at non zero offset" do
1512
- BinaryAccessor.write_array(@baseline_data_array[0..-5], 32, 8, :BLOCK, @baseline_data_array.length*8-32, @data, :BIG_ENDIAN, :ERROR)
1513
- @data.should eql(("\x00" * 4) + @baseline_data[0..-5])
1514
- end
1515
-
1516
- it "should write blocks with zero array_size" do
1517
- BinaryAccessor.write_array(@baseline_data_array, 0, 8, :BLOCK, 0, @data, :BIG_ENDIAN, :ERROR)
1518
- @data.should eql(@baseline_data)
1519
- end
1520
-
1521
- it "should write blocks with negative bit_offsets" do
1522
- BinaryAccessor.write_array(["\x80\x81","\x82\x83"], -32, 16, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR)
1523
- @data.should eql(("\x00" * 12) + @baseline_data[0..3])
1524
- end
1525
-
1526
- it "should complain with an array_size not a multiple of bit_size" do
1527
- data = @data.unpack('C*')
1528
- expect { BinaryAccessor.write_array([1,2], 0, 8, :UINT, 10, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "array_size 10 not a multiple of bit_size 8")
1529
- end
1530
-
1531
- it "should complain with an array_size not a multiple of bit_size" do
1532
- data = @data.unpack('C*')
1533
- expect { BinaryAccessor.write_array([1,2], 0, 8, :UINT, -10, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "array_size -10 not a multiple of bit_size 8")
1534
- end
1535
-
1536
- it "should exclude the remaining bits if array_size is negative" do
1537
- data = @data.clone
1538
- BinaryAccessor.write_array(@baseline_data_array[0..-5], 0, 8, :BLOCK, -32, @data, :BIG_ENDIAN, :ERROR)
1539
- @data.should eql(@baseline_data[0..-5] + data[-4..-1])
1540
- end
1541
-
1542
- it "should not write if the offset equals the negative array size" do
1543
- data = @data.clone
1544
- BinaryAccessor.write_array([], @data.length*8-32, 8, :BLOCK, -32, @data, :LITTLE_ENDIAN, :ERROR)
1545
- @data.should eql(data)
1546
- end
1547
-
1548
- it "should expand the buffer to handle negative array size" do
1549
- @data = "\x00\x01\x02\x00\x03"
1550
- BinaryAccessor.write_array([1,2,3,4], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1551
- @data.should eql("\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x03")
1552
- end
1553
-
1554
- it "should shrink the buffer when handling negative array size" do
1555
- # Start with one array item
1556
- @data = "\x00\x01\x02\x00\x03"
1557
- # Goto 4 array items array item
1558
- BinaryAccessor.write_array([1,2,3,4], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1559
- @data.should eql("\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x03")
1560
- # Goto 2 array items
1561
- BinaryAccessor.write_array([1,2], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1562
- @data.should eql("\x00\x00\x00\x01\x00\x00\x00\x02\x03")
1563
- # Goto 0 array items
1564
- BinaryAccessor.write_array([], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1565
- @data.should eql("\x03")
1566
- # Go back to 1 array items
1567
- BinaryAccessor.write_array([1], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1568
- @data.should eql("\x00\x00\x00\x01\x03")
1569
- end
1570
-
1571
- it "complain when passed a zero length buffer" do
1572
- expect { BinaryAccessor.write_array([1,2,3], 0, 8, :UINT, 32, "", :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError)
1573
- end
1574
-
1575
- it "should expand the buffer if the offset is greater than the negative array size" do
1576
- offset = @data.length * 8 - 16
1577
- data = @data.clone
1578
- BinaryAccessor.write_array([1,2], offset, 8, :UINT, -32, @data, :LITTLE_ENDIAN, :ERROR)
1579
- @data.should eql(data[0..-3] + "\x01\x02" + data[-4..-1])
1580
- end
1581
-
1582
- it "should complain with negative bit_offset and zero array_size" do
1583
- expect { BinaryAccessor.write_array([1,2], -32, 8, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "negative or zero array_size (0) cannot be given with negative bit_offset (-32)")
1584
- end
1585
-
1586
- it "should complain with negative array_size" do
1587
- expect { BinaryAccessor.write_array([1,2], -32, 8, :UINT, -8, @data, :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "negative or zero array_size (-8) cannot be given with negative bit_offset (-32)")
1588
- end
1589
-
1590
- it "should write a shorter string and zero fill to the given bit size" do
1591
- @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
1592
- BinaryAccessor.write_array(["\x01\x02","\x01\x02","\x01\x02","\x01\x02"], 0, 32, :STRING, 128, @data, :BIG_ENDIAN, :ERROR)
1593
- @data.should eql("\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00")
1594
- end
1595
-
1596
- it "should write a shorter string and zero fill to the given bit size" do
1597
- @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
1598
- BinaryAccessor.write_array(["\x01\x02","\x01\x02","\x01\x02","\x01\x02"], 0, 32, :BLOCK, 128, @data, :BIG_ENDIAN, :ERROR)
1599
- @data.should eql("\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00")
1600
- end
1601
-
1602
- it "should complain about unaligned blocks" do
1603
- lambda { BinaryAccessor.write_array(@baseline_data_array[0..1], 7, 16, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 7 is not byte aligned for data_type BLOCK")
1604
- end
1605
-
1606
- it "should complain if write exceeds the size of the buffer" do
1607
- lambda { BinaryAccessor.write_array([], 8, 800, :STRING, 800, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "16 byte buffer insufficient to write STRING at bit_offset 8 with bit_size 800")
1608
- end
1609
-
1610
- it "should write aligned 8-bit unsigned integers" do
1611
- data = @data.clone
1612
- BinaryAccessor.write_array([0,1,2,3,4,5,255,255], 0, 8, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1613
- @data.should eql("\x00\x01\x02\x03\x04\x05\xFF\xFF")
1614
- end
1615
-
1616
- it "should write aligned 8-bit signed integers" do
1617
- data = @data.clone
1618
- BinaryAccessor.write_array([0,1,2,3,4,5,-1,127], 0, 8, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1619
- @data.should eql("\x00\x01\x02\x03\x04\x05\xFF\x7F")
1620
- end
1621
-
1622
- it "should complain about unaligned strings" do
1623
- expect { BinaryAccessor.write_array(['X'], 1, 32, :STRING, 32, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
1624
- end
1625
-
1626
- it "should write STRING items" do
1627
- data = @baseline_data.clone
1628
- BinaryAccessor.write_array(['a'], 0, 64, :STRING, 0, @baseline_data, :BIG_ENDIAN, :ERROR)
1629
- @baseline_data.should eql("a\x00\x00\x00\x00\x00\x00\x00")
1630
- end
1631
-
1632
- it "should write BLOCK items" do
1633
- BinaryAccessor.write_array(["\x01","\x02","\x03","\x04"], 0, 32, :BLOCK, 0, @baseline_data, :BIG_ENDIAN, :ERROR)
1634
- @baseline_data.should eql("\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00")
1635
- end
1636
-
1637
- it "should write variable length arrays with a zero and negative array_size" do
1638
- baseline_data_array_uint8 = []
1639
- @baseline_data.length.times {|i| baseline_data_array_uint8 << @baseline_data[i].ord }
1640
- 0.step(-(@baseline_data.length * 8), -8) do |array_size|
1641
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1642
- @expected_data = @baseline_data.clone + ("\x00" * -(array_size / 8))
1643
- BinaryAccessor.write_array(baseline_data_array_uint8, 0, 8, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1644
- @data.should eql(@expected_data)
1645
- end
1646
- end
1647
-
1648
- it "should write variable length arrays or 32-bit UINTS with a zero and negative array_size" do
1649
- baseline_data = "\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04"
1650
- data_array_uint32 = [0x01010101, 0x02020202, 0x03030303, 0x04040404]
1651
- 0.step(-(baseline_data.length * 8), -8) do |array_size|
1652
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1653
- @expected_data = baseline_data.clone + ("\x00" * -(array_size / 8))
1654
- BinaryAccessor.write_array(data_array_uint32, 0, 32, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1655
- @data.should eql(@expected_data)
1656
- end
1657
- end
1658
-
1659
- it "should write variable length arrays of 32-bit UINTS with a zero and negative array_size and non-zero bit offset" do
1660
- baseline_data = "\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04"
1661
- data_array_uint32 = [0x01010101, 0x02020202, 0x03030303, 0x04040404]
1662
- 0.step(-(baseline_data.length * 8), -8) do |array_size|
1663
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1664
- @expected_data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + baseline_data.clone + ("\x00" * -(array_size / 8))
1665
- BinaryAccessor.write_array(data_array_uint32, 128, 32, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1666
- @data.should eql(@expected_data)
1667
- end
1668
- end
1669
-
1670
- it "should write variable length arrays of 32-bit UINTS with a zero and negative array_size and non-zero bit offset and grow the buffer" do
1671
- baseline_data = "\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04"
1672
- data_array_uint32 = [0x01010101, 0x02020202, 0x03030303, 0x04040404]
1673
- 0.step(-(baseline_data.length * 8), -8) do |array_size|
1674
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1675
- @expected_data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + baseline_data.clone + ("\x00" * -(array_size / 8))
1676
- BinaryAccessor.write_array(data_array_uint32, 128, 32, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1677
- @data.should eql(@expected_data)
1678
- end
1679
- end
1680
-
1681
- describe "given big endian data" do
1682
-
1683
- it "should write 1-bit unsigned integers" do
1684
- BinaryAccessor.write_array([1,0,1], 8, 1, :UINT, 3, @data, :BIG_ENDIAN, :ERROR)
1685
- @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1686
- end
1687
-
1688
- it "should write 1-bit signed integers" do
1689
- BinaryAccessor.write_array([1,0,1], 8, 1, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1690
- @data.should eql("\x00\xA0")
1691
- end
1692
-
1693
- it "should write 7-bit unsigned integers" do
1694
- BinaryAccessor.write_array([0x40,0x60,0x50], 8, 7, :UINT, 21, @data, :BIG_ENDIAN, :ERROR)
1695
- @data.should eql("\x00\x81\x82\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1696
- end
1697
-
1698
- it "should write aligned 16-bit unsigned integers" do
1699
- data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
1700
- BinaryAccessor.write_array(data, 0, 16, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1701
- @data.should eql(@baseline_data)
1702
- end
1703
-
1704
- it "should write aligned 16-bit signed integers" do
1705
- data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
1706
- data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
1707
- BinaryAccessor.write_array(data, 0, 16, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1708
- @data.should eql(@baseline_data)
1709
- end
1710
-
1711
- it "should write aligned 32-bit unsigned integers" do
1712
- data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
1713
- BinaryAccessor.write_array(data, 0, 32, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1714
- @data.should eql(@baseline_data)
1715
- end
1716
-
1717
- it "should write aligned 32-bit signed integers" do
1718
- data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
1719
- data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
1720
- BinaryAccessor.write_array(data, 0, 32, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1721
- @data.should eql(@baseline_data)
1722
- end
1723
-
1724
- it "should write aligned 32-bit floats" do
1725
- data = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
1726
- BinaryAccessor.write_array(data, 0, 32, :FLOAT, 0, @data, :BIG_ENDIAN, :ERROR)
1727
- BinaryAccessor.read(0, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-038).of(data[0])
1728
- BinaryAccessor.read(32, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-036).of(data[1])
1729
- BinaryAccessor.read(64, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-040).of(data[2])
1730
- BinaryAccessor.read(96, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-031).of(data[3])
1731
- end
1732
-
1733
- it "should write aligned 64-bit unsigned integers" do
1734
- data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
1735
- BinaryAccessor.write_array(data, 0, 64, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1736
- @data.should eql(@baseline_data)
1737
- end
1738
-
1739
- it "should write aligned 64-bit signed integers" do
1740
- data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
1741
- data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
1742
- BinaryAccessor.write_array(data, 0, 64, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1743
- @data.should eql(@baseline_data)
1744
- end
1745
-
1746
- it "should write aligned 64-bit floats" do
1747
- data = [-3.116851e-306, 1.257060e-308]
1748
- BinaryAccessor.write_array(data, 0, 64, :FLOAT, 0, @data, :BIG_ENDIAN, :ERROR)
1749
- BinaryAccessor.read(0, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-306).of(data[0])
1750
- BinaryAccessor.read(64, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-308).of(data[1])
1751
- end
1752
-
1753
- it "should complain about unaligned floats" do
1754
- lambda { BinaryAccessor.write_array([0.0], 17, 32, :FLOAT, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 17 is not byte aligned for data_type FLOAT")
1755
- end
1756
-
1757
- it "should complain about mis-sized floats" do
1758
- lambda { BinaryAccessor.write_array([0.0], 0, 33, :FLOAT, 33, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 33 but must be 32 or 64 for data_type FLOAT")
1759
- end
1760
-
1761
- end # given big endian data
1762
-
1763
- describe "given little endian data" do
1764
-
1765
- it "should write 1-bit unsigned integers" do
1766
- BinaryAccessor.write_array([1,0,1], 8, 1, :UINT, 3, @data, :LITTLE_ENDIAN, :ERROR)
1767
- @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1768
- end
1769
-
1770
- it "should write 1-bit signed integers" do
1771
- BinaryAccessor.write_array([1,0,1], 8, 1, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1772
- @data.should eql("\x00\xA0")
1773
- end
1774
-
1775
- it "should complain about little endian bit-fields greater than 1-bit" do
1776
- expect { BinaryAccessor.write_array([0x40,0x60,0x50], 8, 7, :UINT, 21, @data, :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "write_array does not support little endian bit fields with bit_size greater than 1-bit")
1777
- end
1778
-
1779
- it "should write aligned 16-bit unsigned integers" do
1780
- data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1781
- BinaryAccessor.write_array(data, 0, 16, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1782
- @data.should eql(@baseline_data)
1783
- end
1784
-
1785
- it "should write aligned 16-bit signed integers" do
1786
- data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1787
- data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
1788
- BinaryAccessor.write_array(data, 0, 16, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1789
- @data.should eql(@baseline_data)
1790
- end
1791
-
1792
- it "should write aligned 32-bit unsigned integers" do
1793
- data = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1794
- BinaryAccessor.write_array(data, 0, 32, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1795
- @data.should eql(@baseline_data)
1796
- end
1797
-
1798
- it "should write aligned 32-bit signed integers" do
1799
- data= [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1800
- data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
1801
- BinaryAccessor.write_array(data, 0, 32, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1802
- @data.should eql(@baseline_data)
1803
- end
1804
-
1805
- it "should write aligned 32-bit floats" do
1806
- data = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
1807
- BinaryAccessor.write_array(data, 0, 32, :FLOAT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1808
- BinaryAccessor.read(0, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-037).of(data[0])
1809
- BinaryAccessor.read(32, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-034).of(data[1])
1810
- BinaryAccessor.read(64, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-032).of(data[2])
1811
- BinaryAccessor.read(96, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-030).of(data[3])
1812
- end
1813
-
1814
- it "should write aligned 64-bit unsigned integers" do
1815
- data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1816
- BinaryAccessor.write_array(data, 0, 64, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1817
- @data.should eql(@baseline_data)
1818
- end
1819
-
1820
- it "should write aligned 64-bit signed integers" do
1821
- data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1822
- data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
1823
- BinaryAccessor.write_array(data, 0, 64, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1824
- @data.should eql(@baseline_data)
1825
- end
1826
-
1827
- it "should write aligned 64-bit floats" do
1828
- data = [-2.081577e-272, 3.691916e-236]
1829
- BinaryAccessor.write_array(data, 0, 64, :FLOAT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1830
- BinaryAccessor.read(0, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-272).of(data[0])
1831
- BinaryAccessor.read(64, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-236).of(data[1])
1832
- end
1833
-
1834
- it "should complain about unaligned floats" do
1835
- lambda { BinaryAccessor.write_array([0.0], 1, 32, :FLOAT, 32, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
1836
- end
1837
-
1838
- it "should complain about mis-sized floats" do
1839
- lambda { BinaryAccessor.write_array([0.0], 0, 65, :FLOAT, 65, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
1840
- end
1841
-
1842
- end # given little endian data
1843
-
1844
- describe "should support overflow types" do
1845
- before(:each) do
1846
- @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1847
- end
1848
-
1849
- it "should prevent overflow of STRING" do
1850
- lambda { BinaryAccessor.write_array(["abcde"], 0, 32, :STRING, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type STRING")
1851
- end
1852
-
1853
- it "should prevent overflow of BLOCK" do
1854
- lambda { BinaryAccessor.write_array(["abcde"], 0, 32, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type BLOCK")
1855
- end
1856
-
1857
- it "should prevent overflow of 8-bit INT" do
1858
- bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1)
1859
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1860
- end
1861
-
1862
- it "should prevent overflow of 16-bit INT" do
1863
- bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1)
1864
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1865
- end
1866
-
1867
- it "should prevent overflow of 32-bit INT" do
1868
- bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1)
1869
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1870
- end
1871
-
1872
- it "should prevent overflow of 64-bit INT" do
1873
- bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1)
1874
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1875
- end
1876
-
1877
- it "should prevent overflow of 3-bit INT" do
1878
- bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1)
1879
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1880
- end
1881
-
1882
- it "should prevent overflow of 8-bit UINT" do
1883
- bit_size = 8; data_type = :UINT; value = 2 ** bit_size
1884
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1885
- end
1886
-
1887
- it "should prevent overflow of 16-bit UINT" do
1888
- bit_size = 16; data_type = :UINT; value = 2 ** bit_size
1889
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1890
- end
1891
-
1892
- it "should prevent overflow of 32-bit UINT" do
1893
- bit_size = 32; data_type = :UINT; value = 2 ** bit_size
1894
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1895
- end
1896
-
1897
- it "should prevent overflow of 64-bit UINT" do
1898
- bit_size = 64; data_type = :UINT; value = 2 ** bit_size
1899
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1900
- end
1901
-
1902
- it "should prevent overflow of 3-bit UINT" do
1903
- bit_size = 3; data_type = :UINT; value = 2 ** bit_size
1904
- lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1905
- end
1906
-
1907
- it "should truncate STRING" do
1908
- BinaryAccessor.write_array(["abcde"], 0, 32, :STRING, 32, @data, :BIG_ENDIAN, :TRUNCATE)
1909
- @data[0..4].should eql "abcd\x00"
1910
- end
1911
-
1912
- it "should truncate BLOCK" do
1913
- BinaryAccessor.write_array(["abcde"], 0, 32, :BLOCK, 32, @data, :BIG_ENDIAN, :TRUNCATE)
1914
- @data[0..4].should eql "abcd\x00"
1915
- end
1916
-
1917
- it "should truncate 8-bit INT" do
1918
- bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1919
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1920
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1921
- end
1922
-
1923
- it "should truncate 16-bit INT" do
1924
- bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1925
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1926
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1927
- end
1928
-
1929
- it "should truncate 32-bit INT" do
1930
- bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1931
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1932
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1933
- end
1934
-
1935
- it "should truncate 64-bit INT" do
1936
- bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1937
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1938
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1939
- end
1940
-
1941
- it "should truncate 3-bit INT" do
1942
- bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1943
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1944
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1945
- end
1946
-
1947
- it "should truncate 8-bit UINT" do
1948
- bit_size = 8; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1949
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1950
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1951
- end
1952
-
1953
- it "should truncate 16-bit UINT" do
1954
- bit_size = 16; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1955
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1956
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1957
- end
1958
-
1959
- it "should truncate 32-bit UINT" do
1960
- bit_size = 32; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1961
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1962
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1963
- end
1964
-
1965
- it "should truncate 64-bit UINT" do
1966
- bit_size = 64; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1967
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1968
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1969
- end
1970
-
1971
- it "should truncate 3-bit UINT" do
1972
- bit_size = 3; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1973
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1974
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1975
- end
1976
-
1977
- it "should saturate 8-bit INT" do
1978
- bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1979
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1980
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1981
- end
1982
-
1983
- it "should saturate 16-bit INT" do
1984
- bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1985
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1986
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1987
- end
1988
-
1989
- it "should saturate 32-bit INT" do
1990
- bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1991
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1992
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1993
- end
1994
-
1995
- it "should saturate 64-bit INT" do
1996
- bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1997
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1998
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1999
- end
2000
-
2001
- it "should saturate 3-bit INT" do
2002
- bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
2003
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2004
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2005
- end
2006
-
2007
- it "should saturate 8-bit UINT" do
2008
- bit_size = 8; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2009
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2010
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2011
- end
2012
-
2013
- it "should saturate 16-bit UINT" do
2014
- bit_size = 16; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2015
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2016
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2017
- end
2018
-
2019
- it "should saturate 32-bit UINT" do
2020
- bit_size = 32; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2021
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2022
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2023
- end
2024
-
2025
- it "should saturate 64-bit UINT" do
2026
- bit_size = 64; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2027
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2028
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2029
- end
2030
-
2031
- it "should saturate 3-bit UINT" do
2032
- bit_size = 3; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2033
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2034
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2035
- end
2036
-
2037
- it "should allow hex value entry of 8-bit INT" do
2038
- bit_size = 8; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2039
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2040
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2041
- end
2042
-
2043
- it "should allow hex value entry of 16-bit INT" do
2044
- bit_size = 16; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2045
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2046
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2047
- end
2048
-
2049
- it "should allow hex value entry of 32-bit INT" do
2050
- bit_size = 32; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2051
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2052
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2053
- end
2054
-
2055
- it "should allow hex value entry of 64-bit INT" do
2056
- bit_size = 64; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2057
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2058
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2059
- end
2060
-
2061
- it "should allow hex value entry of 3-bit INT" do
2062
- bit_size = 3; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2063
- BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2064
- BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2065
- end
2066
-
2067
- end
2068
-
2069
- end # describe "write_array"
2070
-
2071
- end # describe BinaryAccessor
2072
-
2073
- end # module Cosmos
1
+ # encoding: ascii-8bit
2
+
3
+ # Copyright 2014 Ball Aerospace & Technologies Corp.
4
+ # All Rights Reserved.
5
+ #
6
+ # This program is free software; you can modify and/or redistribute it
7
+ # under the terms of the GNU General Public License
8
+ # as published by the Free Software Foundation; version 3 with
9
+ # attribution addendums as found in the LICENSE.txt
10
+
11
+ require 'spec_helper'
12
+ require 'cosmos'
13
+ require 'cosmos/packets/binary_accessor'
14
+
15
+ module Cosmos
16
+
17
+ describe BinaryAccessor do
18
+
19
+ describe "read" do
20
+
21
+ before(:each) do
22
+ @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
23
+ end
24
+
25
+ it "should complain about unrecognized data types" do
26
+ lambda { BinaryAccessor.read(0, 32, :BLOB, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "data_type BLOB is not recognized")
27
+ end
28
+
29
+ it "should complain about bit_offsets before the beginning of the buffer" do
30
+ lambda { BinaryAccessor.read(-((@data.length * 8) + 8), 32, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to read STRING at bit_offset #{-((@data.length * 8) + 8)} with bit_size 32")
31
+ end
32
+
33
+ it "should complain about a negative bit_offset and zero bit_size" do
34
+ lambda { BinaryAccessor.read(-8, 0, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "negative or zero bit_sizes (0) cannot be given with negative bit_offsets (-8)")
35
+ end
36
+
37
+ it "should complain about a negative bit_offset and negative bit_size" do
38
+ lambda { BinaryAccessor.read(-8, -8, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "negative or zero bit_sizes (-8) cannot be given with negative bit_offsets (-8)")
39
+ end
40
+
41
+ it "should complain about negative bit_sizes larger than the size of the buffer" do
42
+ lambda { BinaryAccessor.read(0, -((@data.length * 8) + 8), :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to read STRING at bit_offset 0 with bit_size #{-((@data.length * 8) + 8)}")
43
+ end
44
+
45
+ it "should complain about negative or zero bit_sizes with data_types other than STRING and BLOCK" do
46
+ lambda { BinaryAccessor.read(0, -8, :INT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
47
+ lambda { BinaryAccessor.read(0, -8, :UINT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
48
+ lambda { BinaryAccessor.read(0, -8, :FLOAT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
49
+ end
50
+
51
+ it "should read aligned strings" do
52
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
53
+ if (bit_offset / 8) <= 7
54
+ BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN).should eql(@data[(bit_offset / 8)..7])
55
+ elsif (bit_offset / 8) == 8
56
+ BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN).should eql('')
57
+ else
58
+ BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN).should eql(@data[(bit_offset / 8)..-1])
59
+ end
60
+ end
61
+ end
62
+
63
+ it "should read variable length strings with a zero and negative bit_size" do
64
+ 0.step(-(@data.length * 8), -8) do |bit_size|
65
+ if (bit_size / 8) >= -8
66
+ BinaryAccessor.read(0, bit_size, :STRING, @data, :BIG_ENDIAN).should eql(@data[0..7])
67
+ else
68
+ BinaryAccessor.read(0, bit_size, :STRING, @data, :BIG_ENDIAN).should eql(@data[0..((bit_size / 8) - 1)])
69
+ end
70
+ end
71
+ end
72
+
73
+ it "should read strings with negative bit_offsets" do
74
+ BinaryAccessor.read(-16, 16, :STRING, @data, :BIG_ENDIAN).should eql(@data[-2..-1])
75
+ end
76
+
77
+ it "should complain about unaligned strings" do
78
+ lambda { BinaryAccessor.read(1, 32, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
79
+ end
80
+
81
+ it "should read aligned blocks" do
82
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
83
+ BinaryAccessor.read(bit_offset, (@data.length * 8) - bit_offset, :BLOCK, @data, :BIG_ENDIAN).should eql(@data[(bit_offset / 8)..-1])
84
+ end
85
+ end
86
+
87
+ it "should read variable length blocks with a zero and negative bit_size" do
88
+ 0.step(-(@data.length * 8), -8) do |bit_size|
89
+ BinaryAccessor.read(0, bit_size, :BLOCK, @data, :BIG_ENDIAN).should eql(@data[0..((bit_size / 8) - 1)])
90
+ end
91
+ end
92
+
93
+ it "should read blocks with negative bit_offsets" do
94
+ BinaryAccessor.read(-16, 16, :BLOCK, @data, :BIG_ENDIAN).should eql(@data[-2..-1])
95
+ end
96
+
97
+ it "should complain about unaligned blocks" do
98
+ lambda { BinaryAccessor.read(7, 16, :BLOCK, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 7 is not byte aligned for data_type BLOCK")
99
+ end
100
+
101
+ it "should complain if read exceeds the size of the buffer" do
102
+ lambda { BinaryAccessor.read(8, 800, :STRING, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "16 byte buffer insufficient to read STRING at bit_offset 8 with bit_size 800")
103
+ end
104
+
105
+ it "should read aligned 8-bit unsigned integers" do
106
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
107
+ BinaryAccessor.read(bit_offset, 8, :UINT, @data, :BIG_ENDIAN).should eql(@data.getbyte(bit_offset / 8))
108
+ end
109
+ end
110
+
111
+ it "should read aligned 8-bit signed integers" do
112
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
113
+ expected = @data.getbyte(bit_offset / 8)
114
+ expected = expected - 256 if expected >= 128
115
+ BinaryAccessor.read(bit_offset, 8, :INT, @data, :BIG_ENDIAN).should eql(expected)
116
+ end
117
+ end
118
+
119
+ describe "given big endian data" do
120
+
121
+ it "should read 1-bit unsigned integers" do
122
+ expected = [0x1, 0x0]
123
+ bit_size = 1
124
+ BinaryAccessor.read(8, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
125
+ BinaryAccessor.read(9, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
126
+ end
127
+
128
+ it "should read 1-bit signed integers" do
129
+ expected = [0x1, 0x0]
130
+ bit_size = 1
131
+ BinaryAccessor.read(8, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
132
+ BinaryAccessor.read(9, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
133
+ end
134
+
135
+ it "should read 7-bit unsigned integers" do
136
+ expected = [0x40, 0x02]
137
+ bit_size = 7
138
+ BinaryAccessor.read(8, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
139
+ BinaryAccessor.read(3, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
140
+ end
141
+
142
+ it "should read 7-bit signed integers" do
143
+ expected = [0x40, 0x02]
144
+ bit_size = 7
145
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
146
+ BinaryAccessor.read(8, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
147
+ BinaryAccessor.read(3, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
148
+ end
149
+
150
+ it "should read 13-bit unsigned integers" do
151
+ expected = [0x1C24, 0x20]
152
+ bit_size = 13
153
+ BinaryAccessor.read(30, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
154
+ BinaryAccessor.read(1, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
155
+ end
156
+
157
+ it "should read 13-bit signed integers" do
158
+ expected = [0x1C24, 0x20]
159
+ bit_size = 13
160
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
161
+ BinaryAccessor.read(30, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
162
+ BinaryAccessor.read(1, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
163
+ end
164
+
165
+ it "should read aligned 16-bit unsigned integers" do
166
+ expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
167
+ index = 0
168
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
169
+ BinaryAccessor.read(bit_offset, 16, :UINT, @data, :BIG_ENDIAN).should eql(expected_array[index])
170
+ index += 1
171
+ end
172
+ end
173
+
174
+ it "should read aligned 16-bit signed integers" do
175
+ expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
176
+ index = 0
177
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
178
+ expected = expected_array[index]
179
+ expected = expected - 2**16 if expected >= 2**15
180
+ BinaryAccessor.read(bit_offset, 16, :INT, @data, :BIG_ENDIAN).should eql(expected)
181
+ index += 1
182
+ end
183
+ end
184
+
185
+ it "should read aligned 32-bit unsigned integers" do
186
+ expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
187
+ index = 0
188
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
189
+ BinaryAccessor.read(bit_offset, 32, :UINT, @data, :BIG_ENDIAN).should eql(expected_array[index])
190
+ index += 1
191
+ end
192
+ end
193
+
194
+ it "should read aligned 32-bit signed integers" do
195
+ expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
196
+ index = 0
197
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
198
+ expected = expected_array[index]
199
+ expected = expected - 2**32 if expected >= 2**31
200
+ BinaryAccessor.read(bit_offset, 32, :INT, @data, :BIG_ENDIAN).should eql(expected)
201
+ index += 1
202
+ end
203
+ end
204
+
205
+ it "should read aligned 32-bit floats" do
206
+ expected_array = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
207
+ BinaryAccessor.read(0, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-038).of(expected_array[0])
208
+ BinaryAccessor.read(32, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-036).of(expected_array[1])
209
+ BinaryAccessor.read(64, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-040).of(expected_array[2])
210
+ BinaryAccessor.read(96, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-031).of(expected_array[3])
211
+ end
212
+
213
+ it "should read 37-bit unsigned integers" do
214
+ expected = [0x8182838485 >> 3, 0x00090A0B0C]
215
+ bit_size = 37
216
+ BinaryAccessor.read(8, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
217
+ BinaryAccessor.read(67, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
218
+ end
219
+
220
+ it "should read 37-bit signed integers" do
221
+ expected = [0x8182838485 >> 3, 0x00090A0B0C]
222
+ bit_size = 37
223
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
224
+ BinaryAccessor.read(8, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
225
+ BinaryAccessor.read(67, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
226
+ end
227
+
228
+ it "should read 63-bit unsigned integers" do
229
+ expected = [0x8081828384858687 >> 1, 0x00090A0B0C0D0E0F]
230
+ bit_size = 63
231
+ BinaryAccessor.read(0, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[0])
232
+ BinaryAccessor.read(65, bit_size, :UINT, @data, :BIG_ENDIAN).should eql(expected[1])
233
+ end
234
+
235
+ it "should read 63-bit signed integers" do
236
+ expected = [0x8081828384858687 >> 1, 0x00090A0B0C0D0E0F]
237
+ bit_size = 63
238
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
239
+ BinaryAccessor.read(0, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[0])
240
+ BinaryAccessor.read(65, bit_size, :INT, @data, :BIG_ENDIAN).should eql(expected[1])
241
+ end
242
+
243
+ it "should read aligned 64-bit unsigned integers" do
244
+ expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
245
+ index = 0
246
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
247
+ BinaryAccessor.read(bit_offset, 64, :UINT, @data, :BIG_ENDIAN).should eql(expected_array[index])
248
+ index += 1
249
+ end
250
+ end
251
+
252
+ it "should read aligned 64-bit signed integers" do
253
+ expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
254
+ index = 0
255
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
256
+ expected = expected_array[index]
257
+ expected = expected - 2**64 if expected >= 2**63
258
+ BinaryAccessor.read(bit_offset, 64, :INT, @data, :BIG_ENDIAN).should eql(expected)
259
+ index += 1
260
+ end
261
+ end
262
+
263
+ it "should read aligned 64-bit floats" do
264
+ expected_array = [-3.116851e-306, 1.257060e-308]
265
+ BinaryAccessor.read(0, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-306).of(expected_array[0])
266
+ BinaryAccessor.read(64, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-308).of(expected_array[1])
267
+ end
268
+
269
+ it "should complain about unaligned floats" do
270
+ lambda { BinaryAccessor.read(17, 32, :FLOAT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 17 is not byte aligned for data_type FLOAT")
271
+ end
272
+
273
+ it "should complain about mis-sized floats" do
274
+ lambda { BinaryAccessor.read(0, 33, :FLOAT, @data, :BIG_ENDIAN) }.should raise_error(ArgumentError, "bit_size is 33 but must be 32 or 64 for data_type FLOAT")
275
+ end
276
+
277
+ end # given big endian data
278
+
279
+ describe "given little endian data" do
280
+
281
+ it "should complain about ill-defined little endian bitfields" do
282
+ lambda { BinaryAccessor.read(3, 7, :UINT, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "LITTLE_ENDIAN bitfield with bit_offset 3 and bit_size 7 is invalid")
283
+ end
284
+
285
+ it "should read 1-bit unsigned integers" do
286
+ expected = [0x1, 0x0]
287
+ bit_size = 1
288
+ BinaryAccessor.read(8, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
289
+ BinaryAccessor.read(9, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
290
+ end
291
+
292
+ it "should read 1-bit signed integers" do
293
+ expected = [0x1, 0x0]
294
+ bit_size = 1
295
+ BinaryAccessor.read(8, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
296
+ BinaryAccessor.read(9, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
297
+ end
298
+
299
+ it "should read 7-bit unsigned integers" do
300
+ expected = [0x40, 0x60]
301
+ bit_size = 7
302
+ BinaryAccessor.read(8, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
303
+ BinaryAccessor.read(15, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
304
+ end
305
+
306
+ it "should read 7-bit signed integers" do
307
+ expected = [0x40, 0x60]
308
+ bit_size = 7
309
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
310
+ BinaryAccessor.read(8, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
311
+ BinaryAccessor.read(15, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
312
+ end
313
+
314
+ it "should read 13-bit unsigned integers" do
315
+ expected = [0x038281 >> 5, 0x0180 >> 2]
316
+ bit_size = 13
317
+ BinaryAccessor.read(30, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
318
+ BinaryAccessor.read(9, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
319
+ end
320
+
321
+ it "should read 13-bit signed integers" do
322
+ expected = [0x038281 >> 5, 0x0180 >> 2]
323
+ bit_size = 13
324
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
325
+ BinaryAccessor.read(30, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
326
+ BinaryAccessor.read(9, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
327
+ end
328
+
329
+ it "should read aligned 16-bit unsigned integers" do
330
+ expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
331
+ index = 0
332
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
333
+ BinaryAccessor.read(bit_offset, 16, :UINT, @data, :LITTLE_ENDIAN).should eql(expected_array[index])
334
+ index += 1
335
+ end
336
+ end
337
+
338
+ it "should read aligned 16-bit signed integers" do
339
+ expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
340
+ index = 0
341
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
342
+ expected = expected_array[index]
343
+ expected = expected - 2**16 if expected >= 2**15
344
+ BinaryAccessor.read(bit_offset, 16, :INT, @data, :LITTLE_ENDIAN).should eql(expected)
345
+ index += 1
346
+ end
347
+ end
348
+
349
+ it "should read aligned 32-bit unsigned integers" do
350
+ expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
351
+ index = 0
352
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
353
+ BinaryAccessor.read(bit_offset, 32, :UINT, @data, :LITTLE_ENDIAN).should eql(expected_array[index])
354
+ index += 1
355
+ end
356
+ end
357
+
358
+ it "should read aligned 32-bit signed integers" do
359
+ expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
360
+ index = 0
361
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
362
+ expected = expected_array[index]
363
+ expected = expected - 2**32 if expected >= 2**31
364
+ BinaryAccessor.read(bit_offset, 32, :INT, @data, :LITTLE_ENDIAN).should eql(expected)
365
+ index += 1
366
+ end
367
+ end
368
+
369
+ it "should read aligned 32-bit floats" do
370
+ expected_array = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
371
+ BinaryAccessor.read(0, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-037).of(expected_array[0])
372
+ BinaryAccessor.read(32, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-034).of(expected_array[1])
373
+ BinaryAccessor.read(64, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-032).of(expected_array[2])
374
+ BinaryAccessor.read(96, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-030).of(expected_array[3])
375
+ end
376
+
377
+ it "should read 37-bit unsigned integers" do
378
+ expected = [0x8584838281 >> 3, 0x0F0E0D0C0B]
379
+ bit_size = 37
380
+ BinaryAccessor.read(40, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
381
+ BinaryAccessor.read(123, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
382
+ end
383
+
384
+ it "should read 37-bit signed integers" do
385
+ expected = [0x8584838281 >> 3, 0x0F0E0D0C0B]
386
+ bit_size = 37
387
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
388
+ BinaryAccessor.read(40, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
389
+ BinaryAccessor.read(123, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
390
+ end
391
+
392
+ it "should read 63-bit unsigned integers" do
393
+ expected = [0x0F0E0D0C0B0A0900 >> 1, 0x0786858483828180]
394
+ bit_size = 63
395
+ BinaryAccessor.read(120, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[0])
396
+ BinaryAccessor.read(57, bit_size, :UINT, @data, :LITTLE_ENDIAN).should eql(expected[1])
397
+ end
398
+
399
+ it "should read 63-bit signed integers" do
400
+ expected = [0x0F0E0D0C0B0A0900 >> 1, 0x0786858483828180]
401
+ bit_size = 63
402
+ expected.each_with_index { |value, index| expected[index] = value - 2**bit_size if value >= 2**(bit_size - 1) }
403
+ BinaryAccessor.read(120, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[0])
404
+ BinaryAccessor.read(57, bit_size, :INT, @data, :LITTLE_ENDIAN).should eql(expected[1])
405
+ end
406
+
407
+ it "should read aligned 64-bit unsigned integers" do
408
+ expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
409
+ index = 0
410
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
411
+ BinaryAccessor.read(bit_offset, 64, :UINT, @data, :LITTLE_ENDIAN).should eql(expected_array[index])
412
+ index += 1
413
+ end
414
+ end
415
+
416
+ it "should read aligned 64-bit signed integers" do
417
+ expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
418
+ index = 0
419
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
420
+ expected = expected_array[index]
421
+ expected = expected - 2**64 if expected >= 2**63
422
+ BinaryAccessor.read(bit_offset, 64, :INT, @data, :LITTLE_ENDIAN).should eql(expected)
423
+ index += 1
424
+ end
425
+ end
426
+
427
+ it "should read aligned 64-bit floats" do
428
+ expected_array = [-2.081577e-272, 3.691916e-236]
429
+ BinaryAccessor.read(0, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-272).of(expected_array[0])
430
+ BinaryAccessor.read(64, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-236).of(expected_array[1])
431
+ end
432
+
433
+ it "should complain about unaligned floats" do
434
+ lambda { BinaryAccessor.read(1, 32, :FLOAT, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
435
+ end
436
+
437
+ it "should complain about mis-sized floats" do
438
+ lambda { BinaryAccessor.read(0, 65, :FLOAT, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
439
+ end
440
+
441
+ end # little endian
442
+ end # describe 'read'
443
+
444
+ describe "read_array" do
445
+
446
+ before(:each) do
447
+ @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
448
+ end
449
+
450
+ it "should complain with unknown data_type" do
451
+ expect { BinaryAccessor.read_array(0, 8, :BLAH, 0, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "data_type BLAH is not recognized")
452
+ end
453
+
454
+ describe "given little endian data" do
455
+
456
+ it "should complain about negative bit sizes" do
457
+ expect { BinaryAccessor.read_array(0, -8, :UINT, @data.length*8, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "bit_size -8 must be positive for arrays")
458
+ end
459
+
460
+ context "when positive or zero bit_offset" do
461
+ it "should read the given array_size amount of items" do
462
+ data = @data.unpack('C*')
463
+ BinaryAccessor.read_array(0, 8, :UINT, 32, @data, :LITTLE_ENDIAN).should eql(data[0..3])
464
+ end
465
+
466
+ it "should read the given array_size amount of items" do
467
+ data = @data.unpack('C*')
468
+ BinaryAccessor.read_array(32, 8, :UINT, 32, @data, :LITTLE_ENDIAN).should eql(data[4..7])
469
+ end
470
+
471
+ it "should read the total buffer given array_size = buffer size" do
472
+ data = @data.unpack('c*')
473
+
474
+ BinaryAccessor.read_array(0, 8, :INT, @data.length*8, @data, :LITTLE_ENDIAN).should eql(data)
475
+ end
476
+
477
+ it "should complain with an array_size not a multiple of bit_size" do
478
+ data = @data.unpack('C*')
479
+ expect { BinaryAccessor.read_array(0, 8, :UINT, 10, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "array_size 10 not a multiple of bit_size 8")
480
+ end
481
+
482
+ it "should read as many items as possible with a zero array_size" do
483
+ data = @data.unpack('C*')
484
+ BinaryAccessor.read_array(0, 8, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
485
+ end
486
+
487
+ it "should exclude the remaining bits if array_size is negative" do
488
+ data = @data.unpack('C*')
489
+ BinaryAccessor.read_array(0, 8, :UINT, -32, @data, :LITTLE_ENDIAN).should eql(data[0..-5])
490
+ end
491
+
492
+ it "should return an empty array if the offset equals the negative array size" do
493
+ data = @data.unpack('C*')
494
+ BinaryAccessor.read_array(@data.length*8-32, 8, :UINT, -32, @data, :LITTLE_ENDIAN).should eql([])
495
+ end
496
+
497
+ it "should complain if the offset is greater than the negative array size" do
498
+ data = @data.unpack('C*')
499
+ offset = @data.length * 8 - 16
500
+ expect { BinaryAccessor.read_array(offset, 8, :UINT, -32, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "16 byte buffer insufficient to read UINT at bit_offset #{offset} with bit_size 8")
501
+ end
502
+ end
503
+
504
+ context "when negative bit_offset" do
505
+ it "should read an array of aligned 8 bit unsigned integers" do
506
+ data = @data.unpack('C*')
507
+ BinaryAccessor.read_array(-32, 8, :UINT, 32, @data, :LITTLE_ENDIAN).should eql(data[-4..-1])
508
+ end
509
+
510
+ it "should read an array if the negative offset is the size of the array" do
511
+ data = @data.unpack('C*')
512
+ BinaryAccessor.read_array(-(@data.length*8), 8, :UINT, @data.length*8, @data, :LITTLE_ENDIAN).should eql(data)
513
+ end
514
+
515
+ it "should complain if the offset is larger than the buffer" do
516
+ expect { BinaryAccessor.read_array(-(@data.length*8+1), 8, :UINT, @data.length*8, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to read #{:UINT} at bit_offset -#{@data.length*8+1} with bit_size 8")
517
+ end
518
+
519
+ it "should complain with zero array_size" do
520
+ expect { BinaryAccessor.read_array(-32, 8, :UINT, 0, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "negative or zero array_size (0) cannot be given with negative bit_offset (-32)")
521
+ end
522
+
523
+ it "should complain with negative array_size" do
524
+ expect { BinaryAccessor.read_array(-32, 8, :UINT, -8, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "negative or zero array_size (-8) cannot be given with negative bit_offset (-32)")
525
+ end
526
+ end
527
+
528
+ it "should complain about accessing data from a buffer which is too small" do
529
+ expect { BinaryAccessor.read_array(0, 256, :STRING, 256, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "16 byte buffer insufficient to read STRING at bit_offset 0 with bit_size 256")
530
+ end
531
+
532
+ it "should return an empty array when passed a zero length buffer" do
533
+ BinaryAccessor.read_array(0, 8, :UINT, 32, "", :LITTLE_ENDIAN).should eql([])
534
+ end
535
+
536
+ it "should complain about unaligned strings" do
537
+ lambda { BinaryAccessor.read_array(1, 32, :STRING, 32, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
538
+ end
539
+
540
+ it "should read a single STRING item" do
541
+ BinaryAccessor.read_array(0, 128, :STRING, 0, @data, :LITTLE_ENDIAN).should eql([@data[0..7]])
542
+ end
543
+
544
+ it "should read a single BLOCK item" do
545
+ BinaryAccessor.read_array(0, 128, :BLOCK, 0, @data, :LITTLE_ENDIAN).should eql([@data])
546
+ end
547
+
548
+ it "should read BLOCK items" do
549
+ data = BinaryAccessor.read_array(0, 8, :BLOCK, 0, @data, :LITTLE_ENDIAN)
550
+ data.each_with_index {|val, i| val.should eql(@data[i]) }
551
+ end
552
+
553
+ it "should read 1-bit integers" do
554
+ expected = [0x1, 0x0, 0x0, 0x0]
555
+ BinaryAccessor.read_array(0, 1, :INT, 4, @data, :LITTLE_ENDIAN).should eql(expected)
556
+ BinaryAccessor.read_array(0, 1, :INT, 2, @data, :LITTLE_ENDIAN).should eql(expected[0..1])
557
+ end
558
+
559
+ it "should complain about little endian bit-fields greater than 1-bit" do
560
+ expect { BinaryAccessor.read_array(8, 7, :UINT, 21, @data, :LITTLE_ENDIAN) }.to raise_error(ArgumentError, "read_array does not support little endian bit fields with bit_size greater than 1-bit")
561
+ end
562
+
563
+ it "should read 16 bit UINT items" do
564
+ data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
565
+ BinaryAccessor.read_array(0, 16, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
566
+ end
567
+
568
+ it "should read 16 bit INT items" do
569
+ data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
570
+ data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
571
+ BinaryAccessor.read_array(0, 16, :INT, 0, @data, :LITTLE_ENDIAN).should eql(data)
572
+ end
573
+
574
+ it "should read 32 bit UINT items" do
575
+ data = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
576
+ BinaryAccessor.read_array(0, 32, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
577
+ end
578
+
579
+ it "should read 32 bit INT items" do
580
+ data = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
581
+ data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
582
+ BinaryAccessor.read_array(0, 32, :INT, 0, @data, :LITTLE_ENDIAN).should eql(data)
583
+ end
584
+
585
+ it "should read 64 bit UINT items" do
586
+ data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
587
+ BinaryAccessor.read_array(0, 64, :UINT, 0, @data, :LITTLE_ENDIAN).should eql(data)
588
+ end
589
+
590
+ it "should read 64 bit INT items" do
591
+ data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
592
+ data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
593
+ BinaryAccessor.read_array(0, 64, :INT, 0, @data, :LITTLE_ENDIAN).should eql(data)
594
+ end
595
+
596
+ it "should read aligned 32-bit floats" do
597
+ expected_array = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
598
+ actual = BinaryAccessor.read_array(0, 32, :FLOAT, 0, @data, :LITTLE_ENDIAN)
599
+ actual.each_with_index do |val, index|
600
+ val.should be_within(1.0e-030).of(expected_array[index])
601
+ end
602
+ end
603
+
604
+ it "should read aligned 64-bit floats" do
605
+ expected_array = [-2.081577e-272, 3.691916e-236]
606
+ actual = BinaryAccessor.read_array(0, 64, :FLOAT, 0, @data, :LITTLE_ENDIAN)
607
+ actual.each_with_index do |val, index|
608
+ val.should be_within(1.0e-236).of(expected_array[index])
609
+ end
610
+ end
611
+
612
+ it "should complain about unaligned floats" do
613
+ lambda { BinaryAccessor.read_array(1, 32, :FLOAT, 32, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
614
+ end
615
+
616
+ it "should complain about mis-sized floats" do
617
+ lambda { BinaryAccessor.read_array(0, 65, :FLOAT, 65, @data, :LITTLE_ENDIAN) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
618
+ end
619
+ end # given little endian data
620
+
621
+ describe "given big endian data" do
622
+
623
+ it "should read 7-bit unsigned integers" do
624
+ expected = [0x40, 0x60, 0x50]
625
+ bit_size = 7
626
+ BinaryAccessor.read_array(8, bit_size, :UINT, 21, @data, :BIG_ENDIAN).should eql(expected)
627
+ end
628
+
629
+ it "should read 16 bit UINT items" do
630
+ data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
631
+ BinaryAccessor.read_array(0, 16, :UINT, 0, @data, :BIG_ENDIAN).should eql(data)
632
+ end
633
+
634
+ it "should read 16 bit INT items" do
635
+ data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
636
+ data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
637
+ BinaryAccessor.read_array(0, 16, :INT, 0, @data, :BIG_ENDIAN).should eql(data)
638
+ end
639
+
640
+ it "should read 32 bit UINT items" do
641
+ data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
642
+ BinaryAccessor.read_array(0, 32, :UINT, 0, @data, :BIG_ENDIAN).should eql(data)
643
+ end
644
+
645
+ it "should read 32 bit INT items" do
646
+ data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
647
+ data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
648
+ BinaryAccessor.read_array(0, 32, :INT, 0, @data, :BIG_ENDIAN).should eql(data)
649
+ end
650
+
651
+ it "should read 64 bit UINT items" do
652
+ data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
653
+ BinaryAccessor.read_array(0, 64, :UINT, 0, @data, :BIG_ENDIAN).should eql(data)
654
+ end
655
+
656
+ it "should read 64 bit INT items" do
657
+ data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
658
+ data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
659
+ BinaryAccessor.read_array(0, 64, :INT, 0, @data, :BIG_ENDIAN).should eql(data)
660
+ end
661
+
662
+ it "should read aligned 32-bit floats" do
663
+ expected_array = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
664
+ actual = BinaryAccessor.read_array(0, 32, :FLOAT, 0, @data, :BIG_ENDIAN)
665
+ actual.each_with_index do |val, index|
666
+ val.should be_within(1.0e-030).of(expected_array[index])
667
+ end
668
+ end
669
+
670
+ it "should read aligned 64-bit floats" do
671
+ expected_array = [-3.116851e-306, 1.257060e-308]
672
+ actual = BinaryAccessor.read_array(0, 64, :FLOAT, 0, @data, :BIG_ENDIAN)
673
+ actual.each_with_index do |val, index|
674
+ val.should be_within(1.0e-236).of(expected_array[index])
675
+ end
676
+ end
677
+ end # given big endian data
678
+ end # describe "read_array"
679
+
680
+ describe "write" do
681
+
682
+ before(:each) do
683
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
684
+ @baseline_data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
685
+ end
686
+
687
+ it "should complain about unrecognized data types" do
688
+ lambda { BinaryAccessor.write(0, 0, 32, :BLOB, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "data_type BLOB is not recognized")
689
+ end
690
+
691
+ it "should complain about bit_offsets before the beginning of the buffer" do
692
+ lambda { BinaryAccessor.write('', -((@data.length * 8) + 8), 32, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to write STRING at bit_offset #{-((@data.length * 8) + 8)} with bit_size 32")
693
+ end
694
+
695
+ it "should complain about a negative bit_offset and zero bit_size" do
696
+ lambda { BinaryAccessor.write('', -8, 0, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "negative or zero bit_sizes (0) cannot be given with negative bit_offsets (-8)")
697
+ end
698
+
699
+ it "should complain about a negative bit_offset and negative bit_size" do
700
+ lambda { BinaryAccessor.write('', -8, -8, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "negative or zero bit_sizes (-8) cannot be given with negative bit_offsets (-8)")
701
+ end
702
+
703
+ it "should complain about negative or zero bit_sizes with data_types other than STRING and BLOCK" do
704
+ lambda { BinaryAccessor.write(0, 0, -8, :INT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
705
+ lambda { BinaryAccessor.write(0, 0, -8, :UINT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
706
+ lambda { BinaryAccessor.write(0, 0, -8, :FLOAT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size -8 must be positive for data types other than :STRING and :BLOCK")
707
+ end
708
+
709
+ it "should write aligned strings" do
710
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
711
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
712
+ @expected_data = @baseline_data.clone
713
+ first_byte_index = bit_offset / 8
714
+ if first_byte_index > 0
715
+ @expected_data[0..(first_byte_index - 1)] = "\x00" * first_byte_index
716
+ end
717
+ BinaryAccessor.write(@baseline_data[first_byte_index..-1], bit_offset, (@data.length * 8) - bit_offset, :STRING, @data, :BIG_ENDIAN, :ERROR)
718
+ @data.should eql(@expected_data)
719
+ end
720
+ end
721
+
722
+ it "should write variable length strings with a zero and negative bit_size" do
723
+ 0.step(-(@baseline_data.length * 8), -8) do |bit_size|
724
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
725
+ @expected_data = @baseline_data.clone + ("\x00" * -(bit_size / 8))
726
+ BinaryAccessor.write(@baseline_data, 0, bit_size, :STRING, @data, :BIG_ENDIAN, :ERROR)
727
+ @data.should eql(@expected_data)
728
+ end
729
+ end
730
+
731
+ it "should write strings with negative bit_offsets" do
732
+ BinaryAccessor.write(@baseline_data[14..15], -16, 16, :STRING, @data, :BIG_ENDIAN, :ERROR)
733
+ @data.should eql(("\x00" * 14) + @baseline_data[14..15])
734
+ end
735
+
736
+ it "should complain about unaligned strings" do
737
+ lambda { BinaryAccessor.write('', 1, 32, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
738
+ end
739
+
740
+ it "should write aligned blocks" do
741
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
742
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
743
+ @expected_data = @baseline_data.clone
744
+ first_byte_index = bit_offset / 8
745
+ if first_byte_index > 0
746
+ @expected_data[0..(first_byte_index - 1)] = "\x00" * first_byte_index
747
+ end
748
+ BinaryAccessor.write(@baseline_data[first_byte_index..-1], bit_offset, (@data.length * 8) - bit_offset, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
749
+ @data.should eql(@expected_data)
750
+ end
751
+ end
752
+
753
+ it "should write variable length blocks with a zero and negative bit_size" do
754
+ 0.step(-(@data.length * 8), -8) do |bit_size|
755
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
756
+ @expected_data = @baseline_data.clone + ("\x00" * -(bit_size / 8))
757
+ BinaryAccessor.write(@baseline_data, 0, bit_size, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
758
+ @data.should eql(@expected_data)
759
+ end
760
+ end
761
+
762
+ it "should write blocks with negative bit_offsets" do
763
+ BinaryAccessor.write(@baseline_data[0..1], -16, 16, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
764
+ @data[-2..-1].should eql(@baseline_data[0..1])
765
+ end
766
+
767
+ it "should write a blank string with zero bit size" do
768
+ BinaryAccessor.write('', 0, 0, :STRING, @data, :BIG_ENDIAN, :ERROR)
769
+ @data.should eql('')
770
+ end
771
+
772
+ it "should write a blank block with zero bit size" do
773
+ BinaryAccessor.write('', 0, 0, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
774
+ @data.should eql('')
775
+ end
776
+
777
+ it "should write a shorter string with zero bit size" do
778
+ BinaryAccessor.write("\x00\x00\x00\x00\x00\x00\x00\x00", 0, 0, :STRING, @data, :BIG_ENDIAN, :ERROR)
779
+ @data.should eql("\x00\x00\x00\x00\x00\x00\x00\x00")
780
+ end
781
+
782
+ it "should write a shorter block with zero bit size" do
783
+ BinaryAccessor.write("\x00\x00\x00\x00\x00\x00\x00\x00", 0, 0, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
784
+ @data.should eql("\x00\x00\x00\x00\x00\x00\x00\x00")
785
+ end
786
+
787
+ it "should write a shorter string and zero fill to the given bit size" do
788
+ @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
789
+ BinaryAccessor.write("\x01\x02\x03\x04\x05\x06\x07\x08", 0, 128, :STRING, @data, :BIG_ENDIAN, :ERROR)
790
+ @data.should eql("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00\x00\x00\x00\x00\x00\x00")
791
+ end
792
+
793
+ it "should write a shorter block and zero fill to the given bit size" do
794
+ @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
795
+ BinaryAccessor.write("\x01\x02\x03\x04\x05\x06\x07\x08", 0, 128, :BLOCK, @data, :BIG_ENDIAN, :ERROR)
796
+ @data.should eql("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00\x00\x00\x00\x00\x00\x00")
797
+ end
798
+
799
+ it "should complain about unaligned blocks" do
800
+ lambda { BinaryAccessor.write(@baseline_data, 7, 16, :BLOCK, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 7 is not byte aligned for data_type BLOCK")
801
+ end
802
+
803
+ it "should complain if write exceeds the size of the buffer" do
804
+ lambda { BinaryAccessor.write(@baseline_data, 8, 800, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "16 byte buffer insufficient to write STRING at bit_offset 8 with bit_size 800")
805
+ end
806
+
807
+ it "should write aligned 8-bit unsigned integers" do
808
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
809
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
810
+ byte_index = bit_offset / 8
811
+ BinaryAccessor.write(@baseline_data.getbyte(byte_index), bit_offset, 8, :UINT, @data, :BIG_ENDIAN, :ERROR)
812
+ @data[byte_index..byte_index].should eq(@baseline_data[byte_index..byte_index])
813
+ end
814
+ end
815
+
816
+ it "should write aligned 8-bit signed integers" do
817
+ 0.step((@data.length - 1) * 8, 8) do |bit_offset|
818
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
819
+ byte_index = bit_offset / 8
820
+ value = @baseline_data.getbyte(byte_index)
821
+ value = value - 256 if value >= 128
822
+ BinaryAccessor.write(value, bit_offset, 8, :INT, @data, :BIG_ENDIAN, :ERROR)
823
+ @data[byte_index..byte_index].should eql(@baseline_data[byte_index..byte_index])
824
+ end
825
+ end
826
+
827
+ describe "given big endian data" do
828
+
829
+ it "should write 1-bit unsigned integers" do
830
+ BinaryAccessor.write(0x1, 8, 1, :UINT, @data, :BIG_ENDIAN, :ERROR)
831
+ BinaryAccessor.write(0x0, 9, 1, :UINT, @data, :BIG_ENDIAN, :ERROR)
832
+ BinaryAccessor.write(0x1, 10, 1, :UINT, @data, :BIG_ENDIAN, :ERROR)
833
+ @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
834
+ end
835
+
836
+ it "should write 1-bit signed integers" do
837
+ BinaryAccessor.write(0x1, 8, 1, :INT, @data, :BIG_ENDIAN, :ERROR)
838
+ BinaryAccessor.write(0x0, 9, 1, :INT, @data, :BIG_ENDIAN, :ERROR)
839
+ BinaryAccessor.write(0x1, 10, 1, :INT, @data, :BIG_ENDIAN, :ERROR)
840
+ @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
841
+ end
842
+
843
+ it "should write 7-bit unsigned integers" do
844
+ BinaryAccessor.write(0x40, 8, 7, :UINT, @data, :BIG_ENDIAN, :ERROR)
845
+ @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
846
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
847
+ BinaryAccessor.write(0x20, 3, 7, :UINT, @data, :BIG_ENDIAN, :ERROR)
848
+ @data.should eql("\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
849
+ end
850
+
851
+ it "should write 7-bit signed integers" do
852
+ BinaryAccessor.write(-64, 8, 7, :INT, @data, :BIG_ENDIAN, :ERROR)
853
+ @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
854
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
855
+ BinaryAccessor.write(32, 3, 7, :INT, @data, :BIG_ENDIAN, :ERROR)
856
+ @data.should eql("\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
857
+ end
858
+
859
+ it "should write 13-bit unsigned integers" do
860
+ BinaryAccessor.write(0x1C24, 30, 13, :UINT, @data, :BIG_ENDIAN, :ERROR)
861
+ @data.should eql("\x00\x00\x00\x03\x84\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
862
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
863
+ BinaryAccessor.write(0x0020, 1, 13, :UINT, @data, :BIG_ENDIAN, :ERROR)
864
+ @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
865
+ end
866
+
867
+ it "should write 13-bit signed integers" do
868
+ BinaryAccessor.write(-988, 30, 13, :INT, @data, :BIG_ENDIAN, :ERROR)
869
+ @data.should eql("\x00\x00\x00\x03\x84\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
870
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
871
+ BinaryAccessor.write(32, 1, 13, :INT, @data, :BIG_ENDIAN, :ERROR)
872
+ @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
873
+ end
874
+
875
+ it "should write aligned 16-bit unsigned integers" do
876
+ expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
877
+ index = 0
878
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
879
+ BinaryAccessor.write(expected_array[index], bit_offset, 16, :UINT, @data, :BIG_ENDIAN, :ERROR)
880
+ index += 1
881
+ end
882
+ @data.should eql(@baseline_data)
883
+ end
884
+
885
+ it "should write aligned 16-bit signed integers" do
886
+ expected_array = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
887
+ index = 0
888
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
889
+ expected = expected_array[index]
890
+ expected = expected - 2**16 if expected >= 2**15
891
+ BinaryAccessor.write(expected, bit_offset, 16, :INT, @data, :BIG_ENDIAN, :ERROR)
892
+ index += 1
893
+ end
894
+ @data.should eql(@baseline_data)
895
+ end
896
+
897
+ it "should write aligned 32-bit unsigned integers" do
898
+ expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
899
+ index = 0
900
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
901
+ BinaryAccessor.write(expected_array[index], bit_offset, 32, :UINT, @data, :BIG_ENDIAN, :ERROR)
902
+ index += 1
903
+ end
904
+ @data.should eql(@baseline_data)
905
+ end
906
+
907
+ it "should write aligned 32-bit signed integers" do
908
+ expected_array = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
909
+ index = 0
910
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
911
+ expected = expected_array[index]
912
+ expected = expected - 2**32 if expected >= 2**31
913
+ BinaryAccessor.write(expected_array[index], bit_offset, 32, :INT, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
914
+ index += 1
915
+ end
916
+ @data.should eql(@baseline_data)
917
+ end
918
+
919
+ it "should write aligned 32-bit floats" do
920
+ expected_array = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
921
+ BinaryAccessor.write(expected_array[0], 0, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
922
+ BinaryAccessor.write(expected_array[1], 32, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
923
+ BinaryAccessor.write(expected_array[2], 64, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
924
+ BinaryAccessor.write(expected_array[3], 96, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
925
+ BinaryAccessor.read(0, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-038).of(expected_array[0])
926
+ BinaryAccessor.read(32, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-036).of(expected_array[1])
927
+ BinaryAccessor.read(64, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-040).of(expected_array[2])
928
+ BinaryAccessor.read(96, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-031).of(expected_array[3])
929
+ end
930
+
931
+ it "should write 37-bit unsigned integers" do
932
+ BinaryAccessor.write(0x8182838485 >> 3, 8, 37, :UINT, @data, :BIG_ENDIAN, :ERROR)
933
+ BinaryAccessor.write(0x00090A0B0C, 67, 37, :UINT, @data, :BIG_ENDIAN, :ERROR)
934
+ @data.should eql("\x00\x81\x82\x83\x84\x80\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
935
+ end
936
+
937
+ it "should write 37-bit signed integers" do
938
+ BinaryAccessor.write((0x8182838485 >> 3) - 2**37, 8, 37, :INT, @data, :BIG_ENDIAN, :ERROR)
939
+ BinaryAccessor.write(0x00090A0B0C, 67, 37, :INT, @data, :BIG_ENDIAN, :ERROR)
940
+ @data.should eql("\x00\x81\x82\x83\x84\x80\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
941
+ end
942
+
943
+ it "should write 63-bit unsigned integers" do
944
+ BinaryAccessor.write(0x8081828384858687 >> 1, 0, 63, :UINT, @data, :BIG_ENDIAN, :ERROR)
945
+ BinaryAccessor.write(0x00090A0B0C0D0E0F, 65, 63, :UINT, @data, :BIG_ENDIAN, :ERROR)
946
+ @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x86\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
947
+ end
948
+
949
+ it "should write 63-bit signed integers" do
950
+ BinaryAccessor.write((0x8081828384858687 >> 1) - 2**63, 0, 63, :INT, @data, :BIG_ENDIAN, :ERROR)
951
+ BinaryAccessor.write(0x00090A0B0C0D0E0F, 65, 63, :INT, @data, :BIG_ENDIAN, :ERROR)
952
+ @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x86\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
953
+ end
954
+
955
+ it "should write aligned 64-bit unsigned integers" do
956
+ expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
957
+ index = 0
958
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
959
+ BinaryAccessor.write(expected_array[index], bit_offset, 64, :UINT, @data, :BIG_ENDIAN, :ERROR)
960
+ index += 1
961
+ end
962
+ @data.should eql(@baseline_data)
963
+ end
964
+
965
+ it "should write aligned 64-bit signed integers" do
966
+ expected_array = [0x8081828384858687, 0x00090A0B0C0D0E0F]
967
+ index = 0
968
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
969
+ expected = expected_array[index]
970
+ expected = expected - 2**64 if expected >= 2**63
971
+ BinaryAccessor.write(expected_array[index], bit_offset, 64, :INT, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
972
+ index += 1
973
+ end
974
+ @data.should eql(@baseline_data)
975
+ end
976
+
977
+ it "should write aligned 64-bit floats" do
978
+ expected_array = [-3.116851e-306, 1.257060e-308]
979
+ BinaryAccessor.write(expected_array[0], 0, 64, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
980
+ BinaryAccessor.write(expected_array[1], 64, 64, :FLOAT, @data, :BIG_ENDIAN, :ERROR)
981
+ BinaryAccessor.read(0, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-306).of(expected_array[0])
982
+ BinaryAccessor.read(64, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-308).of(expected_array[1])
983
+ end
984
+
985
+ it "should complain about unaligned floats" do
986
+ lambda { BinaryAccessor.write(0.0, 17, 32, :FLOAT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 17 is not byte aligned for data_type FLOAT")
987
+ end
988
+
989
+ it "should complain about mis-sized floats" do
990
+ lambda { BinaryAccessor.write(0.0, 0, 33, :FLOAT, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 33 but must be 32 or 64 for data_type FLOAT")
991
+ end
992
+
993
+ end # given big endian data
994
+
995
+ describe "given little endian data" do
996
+
997
+ it "should complain about ill-defined little endian bitfields" do
998
+ lambda { BinaryAccessor.write(0x1, 3, 7, :UINT, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "LITTLE_ENDIAN bitfield with bit_offset 3 and bit_size 7 is invalid")
999
+ end
1000
+
1001
+ it "should write 1-bit unsigned integers" do
1002
+ BinaryAccessor.write(0x1, 8, 1, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1003
+ BinaryAccessor.write(0x0, 9, 1, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1004
+ BinaryAccessor.write(0x1, 10, 1, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1005
+ @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1006
+ end
1007
+
1008
+ it "should write 1-bit signed integers" do
1009
+ BinaryAccessor.write(0x1, 8, 1, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1010
+ BinaryAccessor.write(0x0, 9, 1, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1011
+ BinaryAccessor.write(0x1, 10, 1, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1012
+ @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1013
+ end
1014
+
1015
+ it "should write 7-bit unsigned integers" do
1016
+ BinaryAccessor.write(0x40, 8, 7, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1017
+ @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1018
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1019
+ BinaryAccessor.write(0x7F, 11, 7, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1020
+ @data.should eql("\xC0\x1F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1021
+ end
1022
+
1023
+ it "should write 7-bit signed integers" do
1024
+ BinaryAccessor.write(-64, 8, 7, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1025
+ @data.should eql("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1026
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1027
+ BinaryAccessor.write(32, 11, 7, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1028
+ @data.should eql("\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1029
+ end
1030
+
1031
+ it "should write 13-bit unsigned integers" do
1032
+ BinaryAccessor.write(0x1C24, 30, 13, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1033
+ @data.should eql("\x00\x80\x84\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1034
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1035
+ BinaryAccessor.write(0x0020, 9, 13, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1036
+ @data.should eql("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1037
+ end
1038
+
1039
+ it "should write 13-bit signed integers" do
1040
+ BinaryAccessor.write(-988, 30, 13, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1041
+ @data.should eql("\x00\x80\x84\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1042
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1043
+ BinaryAccessor.write(32, 9, 13, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1044
+ @data.should eql("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1045
+ end
1046
+
1047
+ it "should write aligned 16-bit unsigned integers" do
1048
+ expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1049
+ index = 0
1050
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
1051
+ BinaryAccessor.write(expected_array[index], bit_offset, 16, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1052
+ index += 1
1053
+ end
1054
+ @data.should eql(@baseline_data)
1055
+ end
1056
+
1057
+ it "should write aligned 16-bit signed integers" do
1058
+ expected_array = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1059
+ index = 0
1060
+ 0.step((@data.length - 1) * 8, 16) do |bit_offset|
1061
+ expected = expected_array[index]
1062
+ expected = expected - 2**16 if expected >= 2**15
1063
+ BinaryAccessor.write(expected, bit_offset, 16, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1064
+ index += 1
1065
+ end
1066
+ @data.should eql(@baseline_data)
1067
+ end
1068
+
1069
+ it "should write aligned 32-bit unsigned integers" do
1070
+ expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1071
+ index = 0
1072
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
1073
+ BinaryAccessor.write(expected_array[index], bit_offset, 32, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1074
+ index += 1
1075
+ end
1076
+ @data.should eql(@baseline_data)
1077
+ end
1078
+
1079
+ it "should write aligned 32-bit signed integers" do
1080
+ expected_array = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1081
+ index = 0
1082
+ 0.step((@data.length - 1) * 8, 32) do |bit_offset|
1083
+ expected = expected_array[index]
1084
+ expected = expected - 2**32 if expected >= 2**31
1085
+ BinaryAccessor.write(expected_array[index], bit_offset, 32, :INT, @data, :LITTLE_ENDIAN, :ERROR_ALLOW_HEX)
1086
+ index += 1
1087
+ end
1088
+ @data.should eql(@baseline_data)
1089
+ end
1090
+
1091
+ it "should write aligned 32-bit floats" do
1092
+ expected_array = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
1093
+ BinaryAccessor.write(expected_array[0], 0, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1094
+ BinaryAccessor.write(expected_array[1], 32, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1095
+ BinaryAccessor.write(expected_array[2], 64, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1096
+ BinaryAccessor.write(expected_array[3], 96, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1097
+ BinaryAccessor.read(0, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-037).of(expected_array[0])
1098
+ BinaryAccessor.read(32, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-034).of(expected_array[1])
1099
+ BinaryAccessor.read(64, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-032).of(expected_array[2])
1100
+ BinaryAccessor.read(96, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-030).of(expected_array[3])
1101
+ end
1102
+
1103
+ it "should write 37-bit unsigned integers" do
1104
+ BinaryAccessor.write(0x1584838281, 43, 37, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1105
+ BinaryAccessor.write(0x0C0B0A0900 >> 3, 96, 37, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1106
+ @data.should eql("\x00\x81\x82\x83\x84\x15\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
1107
+ end
1108
+
1109
+ it "should write 37-bit signed integers" do
1110
+ BinaryAccessor.write(0x1584838281 - 2**37, 43, 37, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1111
+ BinaryAccessor.write(0x0C0B0A0900 >> 3, 96, 37, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1112
+ @data.should eql("\x00\x81\x82\x83\x84\x15\x00\x00\x00\x09\x0A\x0B\x0C\x00\x00\x00")
1113
+ end
1114
+
1115
+ it "should write 63-bit unsigned integers" do
1116
+ BinaryAccessor.write(0x4786858483828180, 57, 63, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1117
+ BinaryAccessor.write(0x0F0E0D0C0B0A0900 >> 1, 120, 63, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1118
+ @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x47\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
1119
+ end
1120
+
1121
+ it "should write 63-bit signed integers" do
1122
+ BinaryAccessor.write(0x4786858483828180 - 2**63, 57, 63, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1123
+ BinaryAccessor.write(0x0F0E0D0C0B0A0900 >> 1, 120, 63, :INT, @data, :LITTLE_ENDIAN, :ERROR)
1124
+ @data.should eql("\x80\x81\x82\x83\x84\x85\x86\x47\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F")
1125
+ end
1126
+
1127
+ it "should write aligned 64-bit unsigned integers" do
1128
+ expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1129
+ index = 0
1130
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
1131
+ BinaryAccessor.write(expected_array[index], bit_offset, 64, :UINT, @data, :LITTLE_ENDIAN, :ERROR)
1132
+ index += 1
1133
+ end
1134
+ @data.should eql(@baseline_data)
1135
+ end
1136
+
1137
+ it "should write aligned 64-bit signed integers" do
1138
+ expected_array = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1139
+ index = 0
1140
+ 0.step((@data.length - 1) * 8, 64) do |bit_offset|
1141
+ expected = expected_array[index]
1142
+ expected = expected - 2**64 if expected >= 2**63
1143
+ BinaryAccessor.write(expected_array[index], bit_offset, 64, :INT, @data, :LITTLE_ENDIAN, :ERROR_ALLOW_HEX)
1144
+ index += 1
1145
+ end
1146
+ @data.should eql(@baseline_data)
1147
+ end
1148
+
1149
+ it "should write aligned 64-bit floats" do
1150
+ expected_array = [-2.081577e-272, 3.691916e-236]
1151
+ BinaryAccessor.write(expected_array[0], 0, 64, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1152
+ BinaryAccessor.write(expected_array[1], 64, 64, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR)
1153
+ BinaryAccessor.read(0, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-272).of(expected_array[0])
1154
+ BinaryAccessor.read(64, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-236).of(expected_array[1])
1155
+ end
1156
+
1157
+ it "should complain about unaligned floats" do
1158
+ lambda { BinaryAccessor.write(0.0, 1, 32, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
1159
+ end
1160
+
1161
+ it "should complain about mis-sized floats" do
1162
+ lambda { BinaryAccessor.write(0.0, 0, 65, :FLOAT, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
1163
+ end
1164
+
1165
+ end # given little endian data
1166
+
1167
+ describe "should support overflow types" do
1168
+ before(:each) do
1169
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1170
+ end
1171
+
1172
+ it "should prevent overflow of STRING" do
1173
+ lambda { BinaryAccessor.write("abcde", 0, 32, :STRING, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type STRING")
1174
+ end
1175
+
1176
+ it "should prevent overflow of BLOCK" do
1177
+ lambda { BinaryAccessor.write("abcde", 0, 32, :BLOCK, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type BLOCK")
1178
+ end
1179
+
1180
+ it "should prevent overflow of 8-bit INT" do
1181
+ bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1)
1182
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1183
+ value = -(value + 1)
1184
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1185
+ end
1186
+
1187
+ it "should prevent overflow of 16-bit INT" do
1188
+ bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1)
1189
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1190
+ value = -(value + 1)
1191
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1192
+ end
1193
+
1194
+ it "should prevent overflow of 32-bit INT" do
1195
+ bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1)
1196
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1197
+ value = -(value + 1)
1198
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1199
+ end
1200
+
1201
+ it "should prevent overflow of 64-bit INT" do
1202
+ bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1)
1203
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1204
+ value = -(value + 1)
1205
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1206
+ end
1207
+
1208
+ it "should prevent overflow of 3-bit INT" do
1209
+ bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1)
1210
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1211
+ value = -(value + 1)
1212
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1213
+ end
1214
+
1215
+ it "should prevent overflow of 8-bit UINT" do
1216
+ bit_size = 8; data_type = :UINT; value = 2 ** bit_size
1217
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1218
+ value = -1
1219
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1220
+ end
1221
+
1222
+ it "should prevent overflow of 16-bit UINT" do
1223
+ bit_size = 16; data_type = :UINT; value = 2 ** bit_size
1224
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1225
+ value = -1
1226
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1227
+ end
1228
+
1229
+ it "should prevent overflow of 32-bit UINT" do
1230
+ bit_size = 32; data_type = :UINT; value = 2 ** bit_size
1231
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1232
+ value = -1
1233
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1234
+ end
1235
+
1236
+ it "should prevent overflow of 64-bit UINT" do
1237
+ bit_size = 64; data_type = :UINT; value = 2 ** bit_size
1238
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1239
+ value = -1
1240
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1241
+ end
1242
+
1243
+ it "should prevent overflow of 3-bit UINT" do
1244
+ bit_size = 3; data_type = :UINT; value = 2 ** bit_size
1245
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1246
+ value = -1
1247
+ lambda { BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1248
+ end
1249
+
1250
+ it "should truncate STRING" do
1251
+ BinaryAccessor.write("abcde", 0, 32, :STRING, @data, :BIG_ENDIAN, :TRUNCATE)
1252
+ @data[0..4].should eql "abcd\x00"
1253
+ end
1254
+
1255
+ it "should truncate BLOCK" do
1256
+ BinaryAccessor.write("abcde", 0, 32, :BLOCK, @data, :BIG_ENDIAN, :TRUNCATE)
1257
+ @data[0..4].should eql "abcd\x00"
1258
+ end
1259
+
1260
+ it "should truncate 8-bit INT" do
1261
+ bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1262
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1263
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1264
+ end
1265
+
1266
+ it "should truncate 16-bit INT" do
1267
+ bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1268
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1269
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1270
+ end
1271
+
1272
+ it "should truncate 32-bit INT" do
1273
+ bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1274
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1275
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1276
+ end
1277
+
1278
+ it "should truncate 64-bit INT" do
1279
+ bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1280
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1281
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1282
+ end
1283
+
1284
+ it "should truncate 3-bit INT" do
1285
+ bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1286
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1287
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1288
+ end
1289
+
1290
+ it "should truncate 8-bit UINT" do
1291
+ bit_size = 8; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1292
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1293
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1294
+ end
1295
+
1296
+ it "should truncate 16-bit UINT" do
1297
+ bit_size = 16; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1298
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1299
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1300
+ end
1301
+
1302
+ it "should truncate 32-bit UINT" do
1303
+ bit_size = 32; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1304
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1305
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1306
+ end
1307
+
1308
+ it "should truncate 64-bit UINT" do
1309
+ bit_size = 64; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1310
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1311
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1312
+ end
1313
+
1314
+ it "should truncate 3-bit UINT" do
1315
+ bit_size = 3; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1316
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :TRUNCATE)
1317
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1318
+ end
1319
+
1320
+ it "should saturate 8-bit INT" do
1321
+ bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1322
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1323
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1324
+ value = -(value + 1); saturated_value = value + 1
1325
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1326
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1327
+ end
1328
+
1329
+ it "should saturate 16-bit INT" do
1330
+ bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1331
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1332
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1333
+ value = -(value + 1); saturated_value = value + 1
1334
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1335
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1336
+ end
1337
+
1338
+ it "should saturate 32-bit INT" do
1339
+ bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1340
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1341
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1342
+ value = -(value + 1); saturated_value = value + 1
1343
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1344
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1345
+ end
1346
+
1347
+ it "should saturate 64-bit INT" do
1348
+ bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1349
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1350
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1351
+ value = -(value + 1); saturated_value = value + 1
1352
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1353
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1354
+ end
1355
+
1356
+ it "should saturate 3-bit INT" do
1357
+ bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1358
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1359
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1360
+ value = -(value + 1); saturated_value = value + 1
1361
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1362
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1363
+ end
1364
+
1365
+ it "should saturate 8-bit UINT" do
1366
+ bit_size = 8; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1367
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1368
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1369
+ value = -1; saturated_value = 0
1370
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1371
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1372
+ end
1373
+
1374
+ it "should saturate 16-bit UINT" do
1375
+ bit_size = 16; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1376
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1377
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1378
+ value = -1; saturated_value = 0
1379
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1380
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1381
+ end
1382
+
1383
+ it "should saturate 32-bit UINT" do
1384
+ bit_size = 32; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1385
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1386
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1387
+ value = -1; saturated_value = 0
1388
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1389
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1390
+ end
1391
+
1392
+ it "should saturate 64-bit UINT" do
1393
+ bit_size = 64; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1394
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1395
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1396
+ value = -1; saturated_value = 0
1397
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1398
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1399
+ end
1400
+
1401
+ it "should saturate 3-bit UINT" do
1402
+ bit_size = 3; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
1403
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1404
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1405
+ value = -1; saturated_value = 0
1406
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :SATURATE)
1407
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1408
+ end
1409
+
1410
+ it "should allow hex value entry of 8-bit INT" do
1411
+ bit_size = 8; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1412
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1413
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1414
+ end
1415
+
1416
+ it "should allow hex value entry of 16-bit INT" do
1417
+ bit_size = 16; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1418
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1419
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1420
+ end
1421
+
1422
+ it "should allow hex value entry of 32-bit INT" do
1423
+ bit_size = 32; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1424
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1425
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1426
+ end
1427
+
1428
+ it "should allow hex value entry of 64-bit INT" do
1429
+ bit_size = 64; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1430
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1431
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1432
+ end
1433
+
1434
+ it "should allow hex value entry of 3-bit INT" do
1435
+ bit_size = 3; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
1436
+ BinaryAccessor.write(value, 0, bit_size, data_type, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
1437
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
1438
+ end
1439
+
1440
+ end
1441
+
1442
+ end # describe "write"
1443
+
1444
+ describe "write_array" do
1445
+ before(:each) do
1446
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1447
+ @data_array = []
1448
+ @data.length.times {|i| @data_array << @data[i] }
1449
+ @baseline_data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
1450
+ @baseline_data_array = []
1451
+ @baseline_data.length.times {|i| @baseline_data_array << @baseline_data[i] }
1452
+ end
1453
+
1454
+ it "should complain about value other than Array" do
1455
+ lambda { BinaryAccessor.write_array("", 0, 32, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "values must be an Array type class is String")
1456
+ end
1457
+
1458
+ it "should complain about unrecognized data types" do
1459
+ lambda { BinaryAccessor.write_array([0], 0, 32, :BLOB, 0, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "data_type BLOB is not recognized")
1460
+ end
1461
+
1462
+ it "should complain about bit_offsets before the beginning of the buffer" do
1463
+ expect { BinaryAccessor.write_array([''], -((@data.length * 8) + 8), 32, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "#{@data.length} byte buffer insufficient to write STRING at bit_offset #{-((@data.length * 8) + 8)} with bit_size 32")
1464
+ end
1465
+
1466
+ it "should write if a negative bit_offset is equal to length of buffer" do
1467
+ BinaryAccessor.write_array(@baseline_data_array, -(@data.length * 8), 8, :BLOCK, @baseline_data_array.length*8, @data, :BIG_ENDIAN, :ERROR)
1468
+ @data.should eql @baseline_data
1469
+ end
1470
+
1471
+ it "should complain about a negative or zero bit_size" do
1472
+ expect { BinaryAccessor.write_array([''], 0, 0, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_size 0 must be positive for arrays")
1473
+ expect { BinaryAccessor.write_array([''], 0, -8, :STRING, 0, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_size -8 must be positive for arrays")
1474
+ end
1475
+
1476
+ it "should write aligned strings with fixed array_size" do
1477
+ data = @data.clone
1478
+ BinaryAccessor.write_array(@baseline_data_array, 0, 8, :STRING, @baseline_data_array.length*8, data, :BIG_ENDIAN, :ERROR)
1479
+ data.should eql(@baseline_data)
1480
+ end
1481
+
1482
+ it "should write aligned strings with zero array_size" do
1483
+ BinaryAccessor.write_array(@baseline_data_array, 0, 8, :STRING, 0, @data, :BIG_ENDIAN, :ERROR)
1484
+ @data.should eql(@baseline_data)
1485
+ end
1486
+
1487
+ it "should write strings with negative bit_offsets" do
1488
+ BinaryAccessor.write_array(@baseline_data_array[14..15], -16, 8, :STRING, 16, @data, :BIG_ENDIAN, :ERROR)
1489
+ @data.should eql(("\x00" * 14) + @baseline_data[14..15])
1490
+ end
1491
+
1492
+ it "should complain about unaligned strings" do
1493
+ expect { BinaryAccessor.write_array([], 1, 32, :STRING, 32, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
1494
+ end
1495
+
1496
+ it "should complain if pass more values than the given array_size can hold" do
1497
+ expect { BinaryAccessor.write_array(@baseline_data_array, 0, 8, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "too many values #{@baseline_data_array.length} for given array_size 32 and bit_size 8")
1498
+ end
1499
+
1500
+ it "should write blocks with fixed array_size" do
1501
+ BinaryAccessor.write_array(@baseline_data_array, 0, 8, :BLOCK, @baseline_data_array.length*8, @data, :BIG_ENDIAN, :ERROR)
1502
+ @data.should eql(@baseline_data)
1503
+ end
1504
+
1505
+ it "should zero fill if array_size > number of values passed" do
1506
+ data = @baseline_data.clone
1507
+ BinaryAccessor.write_array(["\x01","\x02","\x03","\x04"], 0, 8, :BLOCK, 64, @baseline_data, :BIG_ENDIAN, :ERROR)
1508
+ @baseline_data.should eql("\x01\x02\x03\x04" + "\x00" * 4 + data[8..-1])
1509
+ end
1510
+
1511
+ it "should write blocks with fixed array_size at non zero offset" do
1512
+ BinaryAccessor.write_array(@baseline_data_array[0..-5], 32, 8, :BLOCK, @baseline_data_array.length*8-32, @data, :BIG_ENDIAN, :ERROR)
1513
+ @data.should eql(("\x00" * 4) + @baseline_data[0..-5])
1514
+ end
1515
+
1516
+ it "should write blocks with zero array_size" do
1517
+ BinaryAccessor.write_array(@baseline_data_array, 0, 8, :BLOCK, 0, @data, :BIG_ENDIAN, :ERROR)
1518
+ @data.should eql(@baseline_data)
1519
+ end
1520
+
1521
+ it "should write blocks with negative bit_offsets" do
1522
+ BinaryAccessor.write_array(["\x80\x81","\x82\x83"], -32, 16, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR)
1523
+ @data.should eql(("\x00" * 12) + @baseline_data[0..3])
1524
+ end
1525
+
1526
+ it "should complain with an array_size not a multiple of bit_size" do
1527
+ data = @data.unpack('C*')
1528
+ expect { BinaryAccessor.write_array([1,2], 0, 8, :UINT, 10, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "array_size 10 not a multiple of bit_size 8")
1529
+ end
1530
+
1531
+ it "should complain with an array_size not a multiple of bit_size" do
1532
+ data = @data.unpack('C*')
1533
+ expect { BinaryAccessor.write_array([1,2], 0, 8, :UINT, -10, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "array_size -10 not a multiple of bit_size 8")
1534
+ end
1535
+
1536
+ it "should exclude the remaining bits if array_size is negative" do
1537
+ data = @data.clone
1538
+ BinaryAccessor.write_array(@baseline_data_array[0..-5], 0, 8, :BLOCK, -32, @data, :BIG_ENDIAN, :ERROR)
1539
+ @data.should eql(@baseline_data[0..-5] + data[-4..-1])
1540
+ end
1541
+
1542
+ it "should not write if the offset equals the negative array size" do
1543
+ data = @data.clone
1544
+ BinaryAccessor.write_array([], @data.length*8-32, 8, :BLOCK, -32, @data, :LITTLE_ENDIAN, :ERROR)
1545
+ @data.should eql(data)
1546
+ end
1547
+
1548
+ it "should expand the buffer to handle negative array size" do
1549
+ @data = "\x00\x01\x02\x00\x03"
1550
+ BinaryAccessor.write_array([1,2,3,4], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1551
+ @data.should eql("\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x03")
1552
+ end
1553
+
1554
+ it "should shrink the buffer when handling negative array size" do
1555
+ # Start with one array item
1556
+ @data = "\x00\x01\x02\x00\x03"
1557
+ # Goto 4 array items array item
1558
+ BinaryAccessor.write_array([1,2,3,4], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1559
+ @data.should eql("\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x03")
1560
+ # Goto 2 array items
1561
+ BinaryAccessor.write_array([1,2], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1562
+ @data.should eql("\x00\x00\x00\x01\x00\x00\x00\x02\x03")
1563
+ # Goto 0 array items
1564
+ BinaryAccessor.write_array([], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1565
+ @data.should eql("\x03")
1566
+ # Go back to 1 array items
1567
+ BinaryAccessor.write_array([1], 0, 32, :UINT, -8, @data, :BIG_ENDIAN, :ERROR)
1568
+ @data.should eql("\x00\x00\x00\x01\x03")
1569
+ end
1570
+
1571
+ it "complain when passed a zero length buffer" do
1572
+ expect { BinaryAccessor.write_array([1,2,3], 0, 8, :UINT, 32, "", :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError)
1573
+ end
1574
+
1575
+ it "should expand the buffer if the offset is greater than the negative array size" do
1576
+ offset = @data.length * 8 - 16
1577
+ data = @data.clone
1578
+ BinaryAccessor.write_array([1,2], offset, 8, :UINT, -32, @data, :LITTLE_ENDIAN, :ERROR)
1579
+ @data.should eql(data[0..-3] + "\x01\x02" + data[-4..-1])
1580
+ end
1581
+
1582
+ it "should complain with negative bit_offset and zero array_size" do
1583
+ expect { BinaryAccessor.write_array([1,2], -32, 8, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "negative or zero array_size (0) cannot be given with negative bit_offset (-32)")
1584
+ end
1585
+
1586
+ it "should complain with negative array_size" do
1587
+ expect { BinaryAccessor.write_array([1,2], -32, 8, :UINT, -8, @data, :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "negative or zero array_size (-8) cannot be given with negative bit_offset (-32)")
1588
+ end
1589
+
1590
+ it "should write a shorter string and zero fill to the given bit size" do
1591
+ @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
1592
+ BinaryAccessor.write_array(["\x01\x02","\x01\x02","\x01\x02","\x01\x02"], 0, 32, :STRING, 128, @data, :BIG_ENDIAN, :ERROR)
1593
+ @data.should eql("\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00")
1594
+ end
1595
+
1596
+ it "should write a shorter string and zero fill to the given bit size" do
1597
+ @data = "\x80\x81\x82\x83\x84\x85\x86\x87\x00\x09\x0A\x0B\x0C\x0D\x0E\x0F"
1598
+ BinaryAccessor.write_array(["\x01\x02","\x01\x02","\x01\x02","\x01\x02"], 0, 32, :BLOCK, 128, @data, :BIG_ENDIAN, :ERROR)
1599
+ @data.should eql("\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00\x01\x02\x00\x00")
1600
+ end
1601
+
1602
+ it "should complain about unaligned blocks" do
1603
+ lambda { BinaryAccessor.write_array(@baseline_data_array[0..1], 7, 16, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 7 is not byte aligned for data_type BLOCK")
1604
+ end
1605
+
1606
+ it "should complain if write exceeds the size of the buffer" do
1607
+ lambda { BinaryAccessor.write_array([], 8, 800, :STRING, 800, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "16 byte buffer insufficient to write STRING at bit_offset 8 with bit_size 800")
1608
+ end
1609
+
1610
+ it "should write aligned 8-bit unsigned integers" do
1611
+ data = @data.clone
1612
+ BinaryAccessor.write_array([0,1,2,3,4,5,255,255], 0, 8, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1613
+ @data.should eql("\x00\x01\x02\x03\x04\x05\xFF\xFF")
1614
+ end
1615
+
1616
+ it "should write aligned 8-bit signed integers" do
1617
+ data = @data.clone
1618
+ BinaryAccessor.write_array([0,1,2,3,4,5,-1,127], 0, 8, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1619
+ @data.should eql("\x00\x01\x02\x03\x04\x05\xFF\x7F")
1620
+ end
1621
+
1622
+ it "should complain about unaligned strings" do
1623
+ expect { BinaryAccessor.write_array(['X'], 1, 32, :STRING, 32, @data, :BIG_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type STRING")
1624
+ end
1625
+
1626
+ it "should write STRING items" do
1627
+ data = @baseline_data.clone
1628
+ BinaryAccessor.write_array(['a'], 0, 64, :STRING, 0, @baseline_data, :BIG_ENDIAN, :ERROR)
1629
+ @baseline_data.should eql("a\x00\x00\x00\x00\x00\x00\x00")
1630
+ end
1631
+
1632
+ it "should write BLOCK items" do
1633
+ BinaryAccessor.write_array(["\x01","\x02","\x03","\x04"], 0, 32, :BLOCK, 0, @baseline_data, :BIG_ENDIAN, :ERROR)
1634
+ @baseline_data.should eql("\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00")
1635
+ end
1636
+
1637
+ it "should write variable length arrays with a zero and negative array_size" do
1638
+ baseline_data_array_uint8 = []
1639
+ @baseline_data.length.times {|i| baseline_data_array_uint8 << @baseline_data[i].ord }
1640
+ 0.step(-(@baseline_data.length * 8), -8) do |array_size|
1641
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1642
+ @expected_data = @baseline_data.clone + ("\x00" * -(array_size / 8))
1643
+ BinaryAccessor.write_array(baseline_data_array_uint8, 0, 8, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1644
+ @data.should eql(@expected_data)
1645
+ end
1646
+ end
1647
+
1648
+ it "should write variable length arrays or 32-bit UINTS with a zero and negative array_size" do
1649
+ baseline_data = "\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04"
1650
+ data_array_uint32 = [0x01010101, 0x02020202, 0x03030303, 0x04040404]
1651
+ 0.step(-(baseline_data.length * 8), -8) do |array_size|
1652
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1653
+ @expected_data = baseline_data.clone + ("\x00" * -(array_size / 8))
1654
+ BinaryAccessor.write_array(data_array_uint32, 0, 32, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1655
+ @data.should eql(@expected_data)
1656
+ end
1657
+ end
1658
+
1659
+ it "should write variable length arrays of 32-bit UINTS with a zero and negative array_size and non-zero bit offset" do
1660
+ baseline_data = "\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04"
1661
+ data_array_uint32 = [0x01010101, 0x02020202, 0x03030303, 0x04040404]
1662
+ 0.step(-(baseline_data.length * 8), -8) do |array_size|
1663
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1664
+ @expected_data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + baseline_data.clone + ("\x00" * -(array_size / 8))
1665
+ BinaryAccessor.write_array(data_array_uint32, 128, 32, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1666
+ @data.should eql(@expected_data)
1667
+ end
1668
+ end
1669
+
1670
+ it "should write variable length arrays of 32-bit UINTS with a zero and negative array_size and non-zero bit offset and grow the buffer" do
1671
+ baseline_data = "\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04"
1672
+ data_array_uint32 = [0x01010101, 0x02020202, 0x03030303, 0x04040404]
1673
+ 0.step(-(baseline_data.length * 8), -8) do |array_size|
1674
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1675
+ @expected_data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + baseline_data.clone + ("\x00" * -(array_size / 8))
1676
+ BinaryAccessor.write_array(data_array_uint32, 128, 32, :UINT, array_size, @data, :BIG_ENDIAN, :ERROR)
1677
+ @data.should eql(@expected_data)
1678
+ end
1679
+ end
1680
+
1681
+ describe "given big endian data" do
1682
+
1683
+ it "should write 1-bit unsigned integers" do
1684
+ BinaryAccessor.write_array([1,0,1], 8, 1, :UINT, 3, @data, :BIG_ENDIAN, :ERROR)
1685
+ @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1686
+ end
1687
+
1688
+ it "should write 1-bit signed integers" do
1689
+ BinaryAccessor.write_array([1,0,1], 8, 1, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1690
+ @data.should eql("\x00\xA0")
1691
+ end
1692
+
1693
+ it "should write 7-bit unsigned integers" do
1694
+ BinaryAccessor.write_array([0x40,0x60,0x50], 8, 7, :UINT, 21, @data, :BIG_ENDIAN, :ERROR)
1695
+ @data.should eql("\x00\x81\x82\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1696
+ end
1697
+
1698
+ it "should write aligned 16-bit unsigned integers" do
1699
+ data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
1700
+ BinaryAccessor.write_array(data, 0, 16, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1701
+ @data.should eql(@baseline_data)
1702
+ end
1703
+
1704
+ it "should write aligned 16-bit signed integers" do
1705
+ data = [0x8081, 0x8283, 0x8485, 0x8687, 0x0009, 0x0A0B, 0x0C0D, 0x0E0F]
1706
+ data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
1707
+ BinaryAccessor.write_array(data, 0, 16, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1708
+ @data.should eql(@baseline_data)
1709
+ end
1710
+
1711
+ it "should write aligned 32-bit unsigned integers" do
1712
+ data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
1713
+ BinaryAccessor.write_array(data, 0, 32, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1714
+ @data.should eql(@baseline_data)
1715
+ end
1716
+
1717
+ it "should write aligned 32-bit signed integers" do
1718
+ data = [0x80818283, 0x84858687, 0x00090A0B, 0x0C0D0E0F]
1719
+ data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
1720
+ BinaryAccessor.write_array(data, 0, 32, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1721
+ @data.should eql(@baseline_data)
1722
+ end
1723
+
1724
+ it "should write aligned 32-bit floats" do
1725
+ data = [-1.189360e-038, -3.139169e-036, 8.301067e-040, 1.086646e-031]
1726
+ BinaryAccessor.write_array(data, 0, 32, :FLOAT, 0, @data, :BIG_ENDIAN, :ERROR)
1727
+ BinaryAccessor.read(0, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-038).of(data[0])
1728
+ BinaryAccessor.read(32, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-036).of(data[1])
1729
+ BinaryAccessor.read(64, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-040).of(data[2])
1730
+ BinaryAccessor.read(96, 32, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-031).of(data[3])
1731
+ end
1732
+
1733
+ it "should write aligned 64-bit unsigned integers" do
1734
+ data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
1735
+ BinaryAccessor.write_array(data, 0, 64, :UINT, 0, @data, :BIG_ENDIAN, :ERROR)
1736
+ @data.should eql(@baseline_data)
1737
+ end
1738
+
1739
+ it "should write aligned 64-bit signed integers" do
1740
+ data = [0x8081828384858687, 0x00090A0B0C0D0E0F]
1741
+ data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
1742
+ BinaryAccessor.write_array(data, 0, 64, :INT, 0, @data, :BIG_ENDIAN, :ERROR)
1743
+ @data.should eql(@baseline_data)
1744
+ end
1745
+
1746
+ it "should write aligned 64-bit floats" do
1747
+ data = [-3.116851e-306, 1.257060e-308]
1748
+ BinaryAccessor.write_array(data, 0, 64, :FLOAT, 0, @data, :BIG_ENDIAN, :ERROR)
1749
+ BinaryAccessor.read(0, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-306).of(data[0])
1750
+ BinaryAccessor.read(64, 64, :FLOAT, @data, :BIG_ENDIAN).should be_within(1.0e-308).of(data[1])
1751
+ end
1752
+
1753
+ it "should complain about unaligned floats" do
1754
+ lambda { BinaryAccessor.write_array([0.0], 17, 32, :FLOAT, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 17 is not byte aligned for data_type FLOAT")
1755
+ end
1756
+
1757
+ it "should complain about mis-sized floats" do
1758
+ lambda { BinaryAccessor.write_array([0.0], 0, 33, :FLOAT, 33, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 33 but must be 32 or 64 for data_type FLOAT")
1759
+ end
1760
+
1761
+ end # given big endian data
1762
+
1763
+ describe "given little endian data" do
1764
+
1765
+ it "should write 1-bit unsigned integers" do
1766
+ BinaryAccessor.write_array([1,0,1], 8, 1, :UINT, 3, @data, :LITTLE_ENDIAN, :ERROR)
1767
+ @data.should eql("\x00\xA0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
1768
+ end
1769
+
1770
+ it "should write 1-bit signed integers" do
1771
+ BinaryAccessor.write_array([1,0,1], 8, 1, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1772
+ @data.should eql("\x00\xA0")
1773
+ end
1774
+
1775
+ it "should complain about little endian bit-fields greater than 1-bit" do
1776
+ expect { BinaryAccessor.write_array([0x40,0x60,0x50], 8, 7, :UINT, 21, @data, :LITTLE_ENDIAN, :ERROR) }.to raise_error(ArgumentError, "write_array does not support little endian bit fields with bit_size greater than 1-bit")
1777
+ end
1778
+
1779
+ it "should write aligned 16-bit unsigned integers" do
1780
+ data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1781
+ BinaryAccessor.write_array(data, 0, 16, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1782
+ @data.should eql(@baseline_data)
1783
+ end
1784
+
1785
+ it "should write aligned 16-bit signed integers" do
1786
+ data = [0x8180, 0x8382, 0x8584, 0x8786, 0x0900, 0x0B0A, 0x0D0C, 0x0F0E]
1787
+ data.map! {|x| (x & ~(1 << 15)) - (x & (1 << 15)) } # convert to negative
1788
+ BinaryAccessor.write_array(data, 0, 16, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1789
+ @data.should eql(@baseline_data)
1790
+ end
1791
+
1792
+ it "should write aligned 32-bit unsigned integers" do
1793
+ data = [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1794
+ BinaryAccessor.write_array(data, 0, 32, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1795
+ @data.should eql(@baseline_data)
1796
+ end
1797
+
1798
+ it "should write aligned 32-bit signed integers" do
1799
+ data= [0x83828180, 0x87868584, 0x0B0A0900, 0x0F0E0D0C]
1800
+ data.map! {|x| (x & ~(1 << 31)) - (x & (1 << 31)) } # convert to negative
1801
+ BinaryAccessor.write_array(data, 0, 32, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1802
+ @data.should eql(@baseline_data)
1803
+ end
1804
+
1805
+ it "should write aligned 32-bit floats" do
1806
+ data = [-7.670445e-037, -2.024055e-034, 2.658460e-032, 7.003653e-030]
1807
+ BinaryAccessor.write_array(data, 0, 32, :FLOAT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1808
+ BinaryAccessor.read(0, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-037).of(data[0])
1809
+ BinaryAccessor.read(32, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-034).of(data[1])
1810
+ BinaryAccessor.read(64, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-032).of(data[2])
1811
+ BinaryAccessor.read(96, 32, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-030).of(data[3])
1812
+ end
1813
+
1814
+ it "should write aligned 64-bit unsigned integers" do
1815
+ data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1816
+ BinaryAccessor.write_array(data, 0, 64, :UINT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1817
+ @data.should eql(@baseline_data)
1818
+ end
1819
+
1820
+ it "should write aligned 64-bit signed integers" do
1821
+ data = [0x8786858483828180, 0x0F0E0D0C0B0A0900]
1822
+ data.map! {|x| (x & ~(1 << 63)) - (x & (1 << 63)) } # convert to negative
1823
+ BinaryAccessor.write_array(data, 0, 64, :INT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1824
+ @data.should eql(@baseline_data)
1825
+ end
1826
+
1827
+ it "should write aligned 64-bit floats" do
1828
+ data = [-2.081577e-272, 3.691916e-236]
1829
+ BinaryAccessor.write_array(data, 0, 64, :FLOAT, 0, @data, :LITTLE_ENDIAN, :ERROR)
1830
+ BinaryAccessor.read(0, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-272).of(data[0])
1831
+ BinaryAccessor.read(64, 64, :FLOAT, @data, :LITTLE_ENDIAN).should be_within(1.0e-236).of(data[1])
1832
+ end
1833
+
1834
+ it "should complain about unaligned floats" do
1835
+ lambda { BinaryAccessor.write_array([0.0], 1, 32, :FLOAT, 32, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_offset 1 is not byte aligned for data_type FLOAT")
1836
+ end
1837
+
1838
+ it "should complain about mis-sized floats" do
1839
+ lambda { BinaryAccessor.write_array([0.0], 0, 65, :FLOAT, 65, @data, :LITTLE_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "bit_size is 65 but must be 32 or 64 for data_type FLOAT")
1840
+ end
1841
+
1842
+ end # given little endian data
1843
+
1844
+ describe "should support overflow types" do
1845
+ before(:each) do
1846
+ @data = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1847
+ end
1848
+
1849
+ it "should prevent overflow of STRING" do
1850
+ lambda { BinaryAccessor.write_array(["abcde"], 0, 32, :STRING, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type STRING")
1851
+ end
1852
+
1853
+ it "should prevent overflow of BLOCK" do
1854
+ lambda { BinaryAccessor.write_array(["abcde"], 0, 32, :BLOCK, 32, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of 5 bytes does not fit into 4 bytes for data_type BLOCK")
1855
+ end
1856
+
1857
+ it "should prevent overflow of 8-bit INT" do
1858
+ bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1)
1859
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1860
+ end
1861
+
1862
+ it "should prevent overflow of 16-bit INT" do
1863
+ bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1)
1864
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1865
+ end
1866
+
1867
+ it "should prevent overflow of 32-bit INT" do
1868
+ bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1)
1869
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1870
+ end
1871
+
1872
+ it "should prevent overflow of 64-bit INT" do
1873
+ bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1)
1874
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1875
+ end
1876
+
1877
+ it "should prevent overflow of 3-bit INT" do
1878
+ bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1)
1879
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1880
+ end
1881
+
1882
+ it "should prevent overflow of 8-bit UINT" do
1883
+ bit_size = 8; data_type = :UINT; value = 2 ** bit_size
1884
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1885
+ end
1886
+
1887
+ it "should prevent overflow of 16-bit UINT" do
1888
+ bit_size = 16; data_type = :UINT; value = 2 ** bit_size
1889
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1890
+ end
1891
+
1892
+ it "should prevent overflow of 32-bit UINT" do
1893
+ bit_size = 32; data_type = :UINT; value = 2 ** bit_size
1894
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1895
+ end
1896
+
1897
+ it "should prevent overflow of 64-bit UINT" do
1898
+ bit_size = 64; data_type = :UINT; value = 2 ** bit_size
1899
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1900
+ end
1901
+
1902
+ it "should prevent overflow of 3-bit UINT" do
1903
+ bit_size = 3; data_type = :UINT; value = 2 ** bit_size
1904
+ lambda { BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR) }.should raise_error(ArgumentError, "value of #{value} invalid for #{bit_size}-bit #{data_type}")
1905
+ end
1906
+
1907
+ it "should truncate STRING" do
1908
+ BinaryAccessor.write_array(["abcde"], 0, 32, :STRING, 32, @data, :BIG_ENDIAN, :TRUNCATE)
1909
+ @data[0..4].should eql "abcd\x00"
1910
+ end
1911
+
1912
+ it "should truncate BLOCK" do
1913
+ BinaryAccessor.write_array(["abcde"], 0, 32, :BLOCK, 32, @data, :BIG_ENDIAN, :TRUNCATE)
1914
+ @data[0..4].should eql "abcd\x00"
1915
+ end
1916
+
1917
+ it "should truncate 8-bit INT" do
1918
+ bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1919
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1920
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1921
+ end
1922
+
1923
+ it "should truncate 16-bit INT" do
1924
+ bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1925
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1926
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1927
+ end
1928
+
1929
+ it "should truncate 32-bit INT" do
1930
+ bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1931
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1932
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1933
+ end
1934
+
1935
+ it "should truncate 64-bit INT" do
1936
+ bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1937
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1938
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1939
+ end
1940
+
1941
+ it "should truncate 3-bit INT" do
1942
+ bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); truncated_value = -value
1943
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1944
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1945
+ end
1946
+
1947
+ it "should truncate 8-bit UINT" do
1948
+ bit_size = 8; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1949
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1950
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1951
+ end
1952
+
1953
+ it "should truncate 16-bit UINT" do
1954
+ bit_size = 16; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1955
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1956
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1957
+ end
1958
+
1959
+ it "should truncate 32-bit UINT" do
1960
+ bit_size = 32; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1961
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1962
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1963
+ end
1964
+
1965
+ it "should truncate 64-bit UINT" do
1966
+ bit_size = 64; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1967
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1968
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1969
+ end
1970
+
1971
+ it "should truncate 3-bit UINT" do
1972
+ bit_size = 3; data_type = :UINT; value = 2 ** bit_size + 1; truncated_value = 1
1973
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :TRUNCATE)
1974
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql truncated_value
1975
+ end
1976
+
1977
+ it "should saturate 8-bit INT" do
1978
+ bit_size = 8; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1979
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1980
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1981
+ end
1982
+
1983
+ it "should saturate 16-bit INT" do
1984
+ bit_size = 16; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1985
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1986
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1987
+ end
1988
+
1989
+ it "should saturate 32-bit INT" do
1990
+ bit_size = 32; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1991
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1992
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1993
+ end
1994
+
1995
+ it "should saturate 64-bit INT" do
1996
+ bit_size = 64; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
1997
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
1998
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
1999
+ end
2000
+
2001
+ it "should saturate 3-bit INT" do
2002
+ bit_size = 3; data_type = :INT; value = 2 ** (bit_size - 1); saturated_value = value - 1
2003
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2004
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2005
+ end
2006
+
2007
+ it "should saturate 8-bit UINT" do
2008
+ bit_size = 8; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2009
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2010
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2011
+ end
2012
+
2013
+ it "should saturate 16-bit UINT" do
2014
+ bit_size = 16; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2015
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2016
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2017
+ end
2018
+
2019
+ it "should saturate 32-bit UINT" do
2020
+ bit_size = 32; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2021
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2022
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2023
+ end
2024
+
2025
+ it "should saturate 64-bit UINT" do
2026
+ bit_size = 64; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2027
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2028
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2029
+ end
2030
+
2031
+ it "should saturate 3-bit UINT" do
2032
+ bit_size = 3; data_type = :UINT; value = 2 ** bit_size; saturated_value = value - 1
2033
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :SATURATE)
2034
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql saturated_value
2035
+ end
2036
+
2037
+ it "should allow hex value entry of 8-bit INT" do
2038
+ bit_size = 8; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2039
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2040
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2041
+ end
2042
+
2043
+ it "should allow hex value entry of 16-bit INT" do
2044
+ bit_size = 16; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2045
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2046
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2047
+ end
2048
+
2049
+ it "should allow hex value entry of 32-bit INT" do
2050
+ bit_size = 32; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2051
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2052
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2053
+ end
2054
+
2055
+ it "should allow hex value entry of 64-bit INT" do
2056
+ bit_size = 64; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2057
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2058
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2059
+ end
2060
+
2061
+ it "should allow hex value entry of 3-bit INT" do
2062
+ bit_size = 3; data_type = :INT; value = 2 ** bit_size - 1; allowed_value = -1
2063
+ BinaryAccessor.write_array([value], 0, bit_size, data_type, bit_size, @data, :BIG_ENDIAN, :ERROR_ALLOW_HEX)
2064
+ BinaryAccessor.read(0, bit_size, data_type, @data, :BIG_ENDIAN).should eql allowed_value
2065
+ end
2066
+
2067
+ end
2068
+
2069
+ end # describe "write_array"
2070
+
2071
+ end # describe BinaryAccessor
2072
+
2073
+ end # module Cosmos