test_parser.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #! /usr/bin/env python
  2. from testutils import get_output_dir
  3. from testutils import cleandir
  4. from testutils import crange
  5. from testutils import RequireFileEquals
  6. from viz_output import RequireVizAscii, RequireVizRK, RequireVizDX
  7. from viz_output import RequireVizDreammV3
  8. from viz_output import RequireVizDreammV3MolsBin
  9. from viz_output import RequireVizDreammV3MeshBin
  10. from viz_output import RequireVizDreammV3MolsAscii
  11. from viz_output import RequireVizDreammV3MeshAscii
  12. from viz_output import RequireVizDreammV3Grouped
  13. from parser_test_types import InvalidParserTest
  14. from parser_test_types import ValidParserTest
  15. from parser_test_types import CheckpointTest
  16. from parser_test_types import KitchenSinkParserTest
  17. from reaction_output import RequireCounts
  18. import unittest
  19. ###################################################################
  20. # Test cases for valid "kitchen sink" parses, no viz output
  21. ###################################################################
  22. class TestParseValid(unittest.TestCase):
  23. def test_vanilla1(self):
  24. KitchenSinkParserTest("01-kitchen_sink").invoke(get_output_dir())
  25. def test_vanilla2(self):
  26. KitchenSinkParserTest("01-kitchen_sink_viz_NONE").invoke(get_output_dir())
  27. def test_fullyrandom(self):
  28. KitchenSinkParserTest("01-kitchen_sink_fully_random").invoke(get_output_dir())
  29. def test_microrev_surface(self):
  30. KitchenSinkParserTest("01-kitchen_sink_mr_surf").invoke(get_output_dir())
  31. def test_microrev_all(self):
  32. KitchenSinkParserTest("01-kitchen_sink_mr_true").invoke(get_output_dir())
  33. def test_microrev_volume(self):
  34. KitchenSinkParserTest("01-kitchen_sink_mr_vol").invoke(get_output_dir())
  35. def test_space_step(self):
  36. KitchenSinkParserTest("01-kitchen_sink_space_step").invoke(get_output_dir())
  37. def test_silent(self):
  38. KitchenSinkParserTest("01-kitchen_sink_silent", silent=True).invoke(get_output_dir())
  39. def test_utility_cmds(self):
  40. t = ValidParserTest("01-kitchen_sink_utility_cmds_grammar.mdl")
  41. t.add_nonempty_file("my_file.dat")
  42. t.add_nonempty_file("exp.dat")
  43. t.add_extra_check(RequireCounts("exp.dat", [(f*1e-6,10) for f in range (0,2)]))
  44. t.invoke(get_output_dir())
  45. ###################################################################
  46. # Test cases for valid "kitchen sink" parses, ASCII/RK viz modes
  47. ###################################################################
  48. class TestParseVizAscii(unittest.TestCase):
  49. def test_viz_ascii1(self):
  50. t = KitchenSinkParserTest("01-kitchen_sink_viz_ascii_iterframedata")
  51. t.add_extra_check(RequireVizAscii("viz_dat/%s/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  52. t.invoke(get_output_dir())
  53. def test_viz_ascii2(self):
  54. t = KitchenSinkParserTest("01-kitchen_sink_viz_ascii_iter")
  55. t.add_extra_check(RequireVizAscii("viz_dat/%s/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  56. t.invoke(get_output_dir())
  57. def test_viz_ascii3(self):
  58. t = KitchenSinkParserTest("01-kitchen_sink_viz_ascii_time")
  59. t.add_extra_check(RequireVizAscii("viz_dat/%s/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  60. t.invoke(get_output_dir())
  61. def test_viz_rk1(self):
  62. t = KitchenSinkParserTest("01-kitchen_sink_viz_rk1")
  63. t.add_extra_check(RequireVizAscii("viz_dat/%s/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  64. t.invoke(get_output_dir())
  65. def test_viz_rk2(self):
  66. t = KitchenSinkParserTest("01-kitchen_sink_viz_rk2")
  67. t.add_extra_check(RequireVizRK("viz_dat/%s/molecules.rk.dat" % t.basename, 10))
  68. t.invoke(get_output_dir())
  69. ###################################################################
  70. # Test cases for valid "kitchen sink" parses, old DX viz modes
  71. ###################################################################
  72. class TestParseVizDx(unittest.TestCase):
  73. def test_viz_dx1(self):
  74. t = KitchenSinkParserTest("01-kitchen_sink_viz_dx_iterframedata")
  75. t.add_extra_check(RequireVizDX("viz_dat/%s" % t.basename,
  76. molfile="molecules",
  77. objprefixes=["objects_a", "objects_b"],
  78. mpositers=[1] + crange(3, 100, 10),
  79. mstateiters=[11, 25],
  80. epositers=crange(1, 100, 10),
  81. estateiters=[1] + crange(2, 100, 10),
  82. opositers=[1, 2],
  83. ostateiters=[1, 3]))
  84. t.invoke(get_output_dir())
  85. def test_viz_dx2(self):
  86. t = KitchenSinkParserTest("01-kitchen_sink_viz_dx_iter")
  87. t.add_extra_check(RequireVizDX("viz_dat/%s" % t.basename,
  88. molfile="molecules",
  89. objprefixes=["objects_a", "objects_b"],
  90. alliters=crange(1, 100, 10)))
  91. t.invoke(get_output_dir())
  92. def test_viz_dx3(self):
  93. t = KitchenSinkParserTest("01-kitchen_sink_viz_dx_time")
  94. t.add_extra_check(RequireVizDX("viz_dat/%s" % t.basename,
  95. molfile="molecules",
  96. objprefixes=["objects_a", "objects_b"],
  97. alliters=crange(1, 100, 10)))
  98. t.invoke(get_output_dir())
  99. ###################################################################
  100. # Test cases for valid "kitchen sink" parses, DREAMM viz modes
  101. ###################################################################
  102. class TestParseVizDreamm(unittest.TestCase):
  103. ALL_MOLECULES = [
  104. "%s_%d" % (m, i)
  105. for (m, lim) in [("c_s", 0),
  106. ("c_v", 1),
  107. ("m_g", 11),
  108. ("m_v", 11),
  109. ("s_g", 11),
  110. ("s_v", 11)]
  111. for i in crange(0, lim)
  112. ]
  113. ALL_WORLDS = [ "world", "world2" ]
  114. ALL_MESHES = [ "box1", "box2", "box3", "newbox", "poly1", "poly2" ]
  115. ALL_OBJECTS = [
  116. "%s.big_object.%s" % (w, o)
  117. for w in ALL_WORLDS
  118. for o in ALL_MESHES
  119. ]
  120. ALL_OBJECTS_WITH_REGIONS = [
  121. "%s.big_object.%s" % (w, o)
  122. for w in ALL_WORLDS
  123. for o in ["box1", "box2", "box3", "poly2"]
  124. ]
  125. def test_viz_default1(self):
  126. t = KitchenSinkParserTest("01-kitchen_sink_viz_default_iter")
  127. outdir = "viz_dat/%s/world_viz_data" % t.basename
  128. t.add_extra_check(RequireVizDreammV3(outdir, "world", 101, 101))
  129. t.add_extra_check(RequireVizDreammV3MolsBin(outdir, crange(0, 100)))
  130. t.add_extra_check(RequireVizDreammV3MeshBin(outdir, crange(0, 100),
  131. positers=[1, 2, 3],
  132. regioniters=[1, 3, 4]))
  133. t.invoke(get_output_dir())
  134. def test_viz_default2(self):
  135. t = KitchenSinkParserTest("01-kitchen_sink_viz_default_time")
  136. outdir = "viz_dat/%s/world_viz_data" % t.basename
  137. t.add_extra_check(RequireVizDreammV3(outdir, "world", 101, 101))
  138. t.add_extra_check(RequireVizDreammV3MolsAscii(outdir, crange(0, 100), TestParseVizDreamm.ALL_MOLECULES))
  139. t.add_extra_check(RequireVizDreammV3MeshBin(outdir, crange(0, 100),
  140. positers=[1, 2, 3],
  141. regioniters=[1, 3, 4]))
  142. t.invoke(get_output_dir())
  143. def test_viz_dreamm1(self):
  144. t = KitchenSinkParserTest("01-kitchen_sink_viz_dreamm_time")
  145. outdir = "viz_dat/%s/world_viz_data" % t.basename
  146. t.add_extra_check(RequireVizDreammV3(outdir, "world", 101, 101))
  147. t.add_extra_check(RequireVizDreammV3MolsBin(outdir, crange(0, 100)))
  148. t.add_extra_check(RequireVizDreammV3MeshAscii(outdir, crange(0, 100), TestParseVizDreamm.ALL_OBJECTS,
  149. objswithregions=TestParseVizDreamm.ALL_OBJECTS_WITH_REGIONS,
  150. positers=[1, 3, 4],
  151. regioniters=[1, 2, 4]))
  152. t.invoke(get_output_dir())
  153. def test_viz_dreammgrouped1(self):
  154. t = KitchenSinkParserTest("01-kitchen_sink_viz_dreammgrouped_time")
  155. outdir = "viz_dat/%s" % t.basename
  156. t.add_extra_check(RequireVizDreammV3Grouped(outdir, "world", n_iters=101))
  157. t.invoke(get_output_dir())
  158. def test_viz_dreammgrouped2(self):
  159. t = KitchenSinkParserTest("01-kitchen_sink_viz_dreammgrouped_wildcard")
  160. outdir = "viz_dat/%s" % t.basename
  161. t.add_extra_check(RequireVizDreammV3Grouped(outdir, "world", n_iters=101, molstate=True, molsnonempty=False))
  162. t.add_extra_check(RequireVizDreammV3Grouped(outdir, "world2", n_iters=101))
  163. t.invoke(get_output_dir())
  164. def test_viz_everything(self):
  165. t = KitchenSinkParserTest("01-kitchen_sink_viz_everything")
  166. t.add_extra_check(RequireVizAscii("viz_dat/%s/01 - ASCII old/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  167. t.add_extra_check(RequireVizAscii("viz_dat/%s/02 - ASCII old/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  168. t.add_extra_check(RequireVizAscii("viz_dat/%s/03 - ASCII old/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  169. t.add_extra_check(RequireVizAscii("viz_dat/%s/04 - ASCII new/world" % t.basename, [0, 2, 3] + crange(10, 100, 10), [2147483647], [2147483647]))
  170. t.add_extra_check(RequireVizAscii("viz_dat/%s/05 - ASCII new/world" % t.basename, [0, 2, 3] + crange(10, 100, 10), [2147483647], [2147483647]))
  171. t.add_extra_check(RequireVizAscii("viz_dat/%s/11 - RK old ASCII/molecules" % t.basename, crange(1, 100, 10), [4], [3]))
  172. t.add_extra_check(RequireVizRK("viz_dat/%s/12 - RK old/molecules.rk.dat" % t.basename, 10))
  173. t.add_extra_check(RequireVizAscii("viz_dat/%s/13 - RK new ASCII/world" % t.basename, [0, 2, 3] + crange(10, 100, 10), [2147483647], [2147483647]))
  174. t.add_extra_check(RequireVizRK("viz_dat/%s/14 - RK new/world.rk.dat" % t.basename, 11))
  175. t.add_extra_check(RequireVizDX("viz_dat/%s/21 - DX old" % t.basename,
  176. molfile="molecules",
  177. objprefixes=["objects_a", "objects_b", "objects_c"],
  178. mpositers=[1] + crange(3, 100, 10),
  179. mstateiters=[11, 25],
  180. epositers=crange(1, 100, 10),
  181. estateiters=[1] + crange(2, 100, 10),
  182. opositers=[1, 2],
  183. ostateiters=[1, 3]))
  184. t.add_extra_check(RequireVizDX("viz_dat/%s/22 - DX old" % t.basename,
  185. molfile="molecules",
  186. objprefixes=["objects_a", "objects_b"],
  187. alliters=crange(1, 100, 10)))
  188. t.add_extra_check(RequireVizDX("viz_dat/%s/23 - DX old" % t.basename,
  189. molfile="molecules",
  190. objprefixes=["objects_a", "objects_b"],
  191. alliters=crange(1, 100, 10)))
  192. t.add_extra_check(RequireVizDX("viz_dat/%s/24 - DX new" % t.basename,
  193. molfile="world",
  194. objprefixes=["objects_a", "objects_b"],
  195. mpositers=[0, 2, 3] + crange(10, 100, 10),
  196. mstateiters=crange(0, 100, 10),
  197. epositers=[0, 2, 3] + crange(10, 100, 10),
  198. estateiters=crange(0, 100, 10),
  199. opositers=[1, 2, 3],
  200. ostateiters=[1]))
  201. t.add_extra_check(RequireVizDX("viz_dat/%s/25 - DX new" % t.basename,
  202. molfile="world",
  203. objprefixes=["objects_a", "objects_b"],
  204. mpositers=[0, 2, 3] + crange(10, 100, 10),
  205. mstateiters=crange(0, 100, 10),
  206. epositers=[0, 2, 3] + crange(10, 100, 10),
  207. estateiters=crange(0, 100, 10),
  208. opositers=[1, 2, 3],
  209. ostateiters=[1]))
  210. outsubdir = "viz_dat/%s/34 - DREAMM new/world_viz_data" % t.basename
  211. t.add_extra_check(RequireVizDreammV3(outsubdir, "world", 14, 15))
  212. t.add_extra_check(RequireVizDreammV3MolsBin(outsubdir, [0, 2, 3, 4] + crange(10, 100, 10),
  213. surfpositers=[0, 2, 3] + crange(10, 100, 10),
  214. volpositers=[0, 2, 3] + crange(10, 100, 10),
  215. surforientiters=[2, 3, 4],
  216. volorientiters=[2, 3, 4]))
  217. t.add_extra_check(RequireVizDreammV3MeshBin(outsubdir, crange(1, 4),
  218. positers=[1, 2, 3],
  219. regioniters=[1, 3, 4]))
  220. outsubdir = "viz_dat/%s/35 - DREAMM new/world_viz_data" % t.basename
  221. t.add_extra_check(RequireVizDreammV3(outsubdir, "world", 14, 15))
  222. t.add_extra_check(RequireVizDreammV3MolsAscii(outsubdir, [0, 2, 3, 4] + crange(10, 100, 10),
  223. ["c_s_0", "c_v_0", "c_v_1"] +
  224. ["%c_%c_%d" % (k, j, i) for i in crange(0, 11) for j in ['v', 'g'] for k in ['m', 's']],
  225. positers=[0, 2, 3] + crange(10, 100, 10),
  226. orientiters=[2, 3, 4]))
  227. t.add_extra_check(RequireVizDreammV3MeshBin(outsubdir, crange(1, 4),
  228. positers=[1, 3, 4],
  229. regioniters=[1, 2, 4]))
  230. outsubdir = "viz_dat/%s/36 - DREAMM new/world_viz_data" % t.basename
  231. t.add_extra_check(RequireVizDreammV3(outsubdir, "world", 14, 15))
  232. t.add_extra_check(RequireVizDreammV3MolsBin(outsubdir, [0, 2, 3, 4] + crange(10, 100, 10),
  233. surfpositers=[0, 2, 3] + crange(10, 100, 10),
  234. volpositers=[0, 2, 3] + crange(10, 100, 10),
  235. surforientiters=[2, 3, 4],
  236. volorientiters=[2, 3, 4]))
  237. t.add_extra_check(RequireVizDreammV3MeshAscii(outsubdir, crange(1, 4),
  238. ["%s.big_object.%s" % (j, i) for i in ["box1", "box2", "box3", "newbox", "poly1", "poly2"] for j in ["world", "world2"]],
  239. objswithregions=["%s.big_object.%s" % (j, i) for i in ["box1", "box2", "box3", "poly2"] for j in ["world", "world2"]],
  240. positers=[1, 2, 3],
  241. regioniters=[1, 3, 4]))
  242. outsubdir = "viz_dat/%s/37 - DREAMM new/world_viz_data" % t.basename
  243. t.add_extra_check(RequireVizDreammV3(outsubdir, "world", 14, 15))
  244. t.add_extra_check(RequireVizDreammV3MolsBin(outsubdir, [0, 2, 3, 4] + crange(10, 100, 10),
  245. surfpositers=[0, 2, 3] + crange(10, 100, 10),
  246. volpositers=[0, 2, 3] + crange(10, 100, 10),
  247. surforientiters=[2, 3, 4],
  248. volorientiters=[2, 3, 4]))
  249. t.add_extra_check(RequireVizDreammV3MeshBin(outsubdir, crange(1, 4),
  250. positers=[1, 2, 3],
  251. regioniters=[1, 3, 4]))
  252. t.add_extra_check(RequireVizDreammV3Grouped("viz_dat/%s/44 - DREAMM grouped new" % t.basename, "world", n_iters=14, n_times=15))
  253. t.add_extra_check(RequireVizDreammV3Grouped("viz_dat/%s/45 - DREAMM grouped new" % t.basename, "world2", n_iters=14, n_times=15))
  254. t.add_extra_check(RequireVizDreammV3Grouped("viz_dat/%s/46 - DREAMM grouped new" % t.basename, "world", n_iters=14, n_times=15, molstate=True, molsnonempty=False))
  255. t.invoke(get_output_dir())
  256. ###################################################################
  257. # Test cases for RT checkpoint parsing
  258. ###################################################################
  259. class TestParseRtCheckpoint(unittest.TestCase):
  260. def test01(self):
  261. CheckpointTest("02-rtcheckpoint_seconds").invoke(get_output_dir())
  262. def test02(self):
  263. CheckpointTest("02-rtcheckpoint_minutes").invoke(get_output_dir())
  264. def test03(self):
  265. CheckpointTest("02-rtcheckpoint_minutes2").invoke(get_output_dir())
  266. def test04(self):
  267. CheckpointTest("02-rtcheckpoint_hours").invoke(get_output_dir())
  268. def test05(self):
  269. CheckpointTest("02-rtcheckpoint_hours2").invoke(get_output_dir())
  270. def test06(self):
  271. CheckpointTest("02-rtcheckpoint_days").invoke(get_output_dir())
  272. def test07(self):
  273. CheckpointTest("02-rtcheckpoint_days2").invoke(get_output_dir())
  274. def test08(self):
  275. CheckpointTest("02-rtcheckpoint_noexitspec").invoke(get_output_dir())
  276. ###################################################################
  277. # Test cases for parser error handling
  278. ###################################################################
  279. class TestParseInvalid(unittest.TestCase):
  280. def test025(self):
  281. InvalidParserTest("invalid-025.mdl").add_empty_file("invalid-025.tmp").invoke(get_output_dir())
  282. def make_invalid_test(i):
  283. methname = "test%03d" % i
  284. filename = "invalid-%03d.mdl" % i
  285. func = lambda self: InvalidParserTest(filename).invoke(get_output_dir())
  286. setattr(TestParseInvalid, methname, func)
  287. ## Bulk generate invalid test cases 1...23, 26...27, 29...86
  288. ## 25 is specially specified, and 24 and 28 do not presently exist.
  289. for i in crange(1, 23) + crange(26, 86):
  290. make_invalid_test(i)
  291. ###################################################################
  292. # Generate a test suite for non-DREAMM viz output modes
  293. ###################################################################
  294. def oldvizsuite():
  295. suite = unittest.TestSuite()
  296. suite.addTest(unittest.makeSuite(TestParseVizAscii, "test"))
  297. suite.addTest(unittest.makeSuite(TestParseVizDx, "test"))
  298. return suite
  299. ###################################################################
  300. # Generate a test suite for DREAMM viz output modes
  301. ###################################################################
  302. def vizsuite():
  303. return unittest.makeSuite(TestParseVizDreamm, "test")
  304. ###################################################################
  305. # Generate a test suite for all invalid mdl files
  306. ###################################################################
  307. def errorsuite():
  308. return unittest.makeSuite(TestParseInvalid, "test")
  309. ###################################################################
  310. # Generate a test suite for all valid "kitchen sink" (non-viz) files
  311. ###################################################################
  312. def kitchensinksuite():
  313. return unittest.makeSuite(TestParseValid, "test")
  314. ###################################################################
  315. # Generate a test suite for all realtime checkpoint files
  316. ###################################################################
  317. def rtcheckpointsuite():
  318. return unittest.makeSuite(TestParseRtCheckpoint, "test")
  319. ###################################################################
  320. # Generate a test suite for all fast-running tests except for the error tests)
  321. ###################################################################
  322. def quicksuite():
  323. suite = unittest.TestSuite()
  324. suite.addTest(TestParseVizDreamm("test_viz_dreamm1"))
  325. suite.addTest(TestParseRtCheckpoint("test01"))
  326. return suite
  327. ###################################################################
  328. # Generate a test suite with all tests included.
  329. ###################################################################
  330. def fullsuite():
  331. suite = unittest.TestSuite()
  332. suite.addTest(allvizsuite())
  333. suite.addTest(errorsuite())
  334. suite.addTest(kitchensinksuite())
  335. suite.addTest(rtcheckpointsuite())
  336. return suite
  337. ###################################################################
  338. # Default use of this file will invoke all tests
  339. ###################################################################
  340. if __name__ == "__main__":
  341. cleandir(get_output_dir())
  342. unittest.main()