PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ensemble_test.rb

https://github.com/marksweiss/aleatoric
Ruby | 367 lines | 293 code | 73 blank | 1 comment | 60 complexity | db50353f9536e941948f11a9454c7c7a MD5 | raw file
  1. require_relative 'test_global'
  2. $LOAD_PATH << psub("../lib")
  3. require 'ensemble'
  4. require 'minitest/autorun'
  5. # require 'ruby-debug' ; Debugger.start
  6. module Aleatoric
  7. class Ensemble_Test < MiniTest::Unit::TestCase
  8. def test__initialize
  9. puts "test__initialize ENTERED"
  10. ensemble = Ensemble.new("Test Ensemble")
  11. assert(ensemble.class == Ensemble)
  12. puts "test__initialize COMPLETED"
  13. end
  14. def test__add_player__get_player__players
  15. puts "test__add_player__get_player__players ENTERED"
  16. ensemble = Ensemble.new("Test Ensemble")
  17. name = "Test Player"
  18. player = Player.new("Test Player")
  19. ensemble.add_player(name, player)
  20. expected = player
  21. actual = ensemble.players.last
  22. assert(expected.name == actual.name && expected.object_id == actual.object_id)
  23. actual = ensemble.get_player(name)
  24. assert(expected.name == actual.name && expected.object_id == actual.object_id)
  25. puts "test__add_player__get_player__players COMPLETED"
  26. end
  27. def test__remove_player__clear_players__players_length__players_empty
  28. puts "test__clear_players__players_length__players_empty ENTERED"
  29. ensemble = Ensemble.new("Test Ensemble")
  30. name = "Test Player"
  31. player = Player.new("Test Player")
  32. expected = 0
  33. actual = ensemble.players_length
  34. assert(expected == actual)
  35. assert(ensemble.players_empty?)
  36. ensemble.add_player(name, player)
  37. expected = 1
  38. actual = ensemble.players_length
  39. assert(expected == actual)
  40. name2 = "Test Player 2"
  41. player2 = Player.new("Test Player")
  42. ensemble.add_player(name2, player2)
  43. expected = 2
  44. actual = ensemble.players_length
  45. assert(expected == actual)
  46. ensemble.remove_player(name2)
  47. expected = 1
  48. actual = ensemble.players_length
  49. assert(expected == actual)
  50. ensemble.remove_player(name)
  51. expected = 0
  52. actual = ensemble.players_length
  53. assert(expected == actual)
  54. assert(ensemble.players_empty?)
  55. puts "test__clear_players__players_length__players_empty COMPLETED"
  56. end
  57. def test__set_state__get_state
  58. puts "test__set_state__get_state ENTERED"
  59. ensemble = Ensemble.new("Test Ensemble")
  60. expected_key = "key_1"
  61. expected_val = 100
  62. ensemble.set_state(expected_key, expected_val)
  63. actual_val = ensemble.get_state(expected_key)
  64. assert(expected_val == actual_val)
  65. puts "test__set_state__get_state COMPLETED"
  66. end
  67. def test__clear_state__state_keys
  68. puts "test__clear_state__state_keys ENTERED"
  69. ensemble = Ensemble.new("Test Ensemble")
  70. expected_key = "key_1"
  71. ensemble.clear_state
  72. ensemble.set_state(expected_key, nil)
  73. actual = ensemble.state_keys
  74. assert([expected_key] == actual)
  75. puts "test__clear_state__state_keys COMPLETED"
  76. end
  77. def test__add_preplay_hook__get_preplay_hook
  78. puts "test__add_preplay_hook__get_preplay_hook ENTERED"
  79. ensemble = Ensemble.new("Test Ensemble")
  80. note_1_name = "note 1"
  81. note_1 = Note.new note_1_name
  82. note_1.start(0.0)
  83. hook_1_name = "hook 1"
  84. expected = lambda {|note| note.start(note.start + 1.0)}
  85. ensemble.add_preplay_hook(hook_1_name, &expected)
  86. actual = ensemble.get_preplay_hook(hook_1_name)
  87. assert(expected.call(note_1) == actual.call(note_1))
  88. puts "test__add_preplay_hook__get_preplay_hook COMPLETED"
  89. end
  90. def test__remove_preplay_hook
  91. puts "test__remove_preplay_hook ENTERED"
  92. ensemble = Ensemble.new("Test Ensemble")
  93. note_1_name = "note 1"
  94. note_1 = Note.new note_1_name
  95. note_1.start(0.0)
  96. hook_1_name = "hook 1"
  97. expected = lambda {|note| note.start(note.start + 1.0)}
  98. ensemble.add_preplay_hook(hook_1_name) {|note| note.start(note.start + 1.0)}
  99. actual = ensemble.get_preplay_hook(hook_1_name)
  100. assert(expected.call(note_1) == actual.call(note_1))
  101. ensemble.remove_preplay_hook(hook_1_name)
  102. actual = ensemble.get_preplay_hook(hook_1_name)
  103. assert(actual == nil)
  104. puts "test__remove_preplay_hook COMPLETED"
  105. end
  106. def test__add_postplay_hook__get_postplay_hook
  107. puts "test__add_postplay_hook__get_postplay_hook ENTERED"
  108. ensemble = Ensemble.new("Test Ensemble")
  109. note_1_name = "note 1"
  110. note_1 = Note.new note_1_name
  111. note_1.start(0.0)
  112. hook_1_name = "hook 1"
  113. expected = lambda {|note| note.start(note.start + 1.0)}
  114. ensemble.add_postplay_hook(hook_1_name) {|note| note.start(note.start + 1.0)}
  115. actual = ensemble.get_postplay_hook(hook_1_name)
  116. assert(expected.call(note_1) == actual.call(note_1))
  117. puts "test__add_postplay_hook__get_postplay_hook COMPLETED"
  118. end
  119. def test__remove_postplay_hook
  120. puts "test__remove_postplay_hook ENTERED"
  121. ensemble = Ensemble.new("Test Ensemble")
  122. note_1_name = "note 1"
  123. note_1 = Note.new note_1_name
  124. note_1.start(0.0)
  125. hook_1_name = "hook 1"
  126. expected = lambda {|note| note.start(note.start + 1.0)}
  127. ensemble.add_postplay_hook(hook_1_name) {|note| note.start(note.start + 1.0)}
  128. actual = ensemble.get_postplay_hook(hook_1_name)
  129. assert(expected.call(note_1) == actual.call(note_1))
  130. ensemble.remove_postplay_hook(hook_1_name)
  131. actual = ensemble.get_postplay_hook(hook_1_name)
  132. assert(actual == nil)
  133. puts "test__remove_postplay_hook COMPLETED"
  134. end
  135. def test__play
  136. puts "test__play ENTERED"
  137. ensemble = Ensemble.new("Test Ensemble")
  138. player_1_name = "Test Player 1"
  139. player_1 = Player.new(player_1_name)
  140. note_1_name = "note 1"
  141. note_2_name = "note 2"
  142. note_1 = Note.new(note_1_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  143. note_2 = Note.new(note_2_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  144. expected_1 = [note_1, note_2]
  145. score_1_name = "score 1"
  146. score_1 = Score.new(score_1_name)
  147. score_1 << expected_1
  148. player_1.add_score(score_1_name, score_1)
  149. ensemble.add_player(player_1_name, player_1)
  150. ensemble.play player_1_name
  151. actual = player_1.output
  152. assert(actual.length == expected_1.length)
  153. assert(actual[0].name == expected_1[0].name && actual[1].name == expected_1[1].name)
  154. player_2_name = "Test Player 2"
  155. player_2 = Player.new(player_2_name)
  156. note_3_name = "note 3"
  157. note_4_name = "note 4"
  158. note_3 = Note.new(note_3_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  159. note_4 = Note.new(note_4_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  160. expected_2 = [note_3, note_4]
  161. score_2_name = "score 2"
  162. score_2 = Score.new(score_2_name)
  163. score_2 << expected_2
  164. player_2.add_score(score_2_name, score_2)
  165. ensemble.add_player(player_2_name, player_2)
  166. player_1.clear_output
  167. ensemble.play player_1_name
  168. actual = player_1.output
  169. assert(actual.length == expected_1.length)
  170. assert(actual[0].name == expected_1[0].name && actual[1].name == expected_1[1].name)
  171. ensemble.play player_2_name
  172. actual = player_2.output
  173. assert(actual.length == expected_2.length)
  174. assert(actual[0].name == expected_2[0].name && actual[1].name == expected_2[1].name)
  175. puts "test__play COMPLETED"
  176. end
  177. def test__play_all
  178. puts "test__play_all ENTERED"
  179. ensemble = Ensemble.new("Test Ensemble")
  180. player_1_name = "Test Player 1"
  181. player_1 = Player.new(player_1_name)
  182. note_1_name = "note 1"
  183. note_2_name = "note 2"
  184. note_1 = Note.new(note_1_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  185. note_2 = Note.new(note_2_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  186. score_1_name = "score 1"
  187. score_1 = Score.new(score_1_name)
  188. expected_1 = [note_1, note_2]
  189. score_1 << expected_1
  190. player_1.add_score(score_1_name, score_1)
  191. ensemble.add_player(player_1_name, player_1)
  192. player_2_name = "Test Player 2"
  193. player_2 = Player.new(player_2_name)
  194. note_3_name = "note 3"
  195. note_4_name = "note 4"
  196. note_3 = Note.new(note_3_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  197. note_4 = Note.new(note_4_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  198. score_2_name = "score 2"
  199. score_2 = Score.new(score_2_name)
  200. expected_2 = [note_3, note_4]
  201. score_2 << expected_2
  202. player_2.add_score(score_2_name, score_2)
  203. ensemble.add_player(player_2_name, player_2)
  204. ensemble.play_all
  205. actual = player_1.output
  206. assert(actual.length == expected_1.length)
  207. assert(actual[0].name == expected_1[0].name && actual[1].name == expected_1[1].name)
  208. actual = player_2.output
  209. assert(actual.length == expected_2.length)
  210. assert(actual[0].name == expected_2[0].name && actual[1].name == expected_2[1].name)
  211. puts "test__play_all COMPLETED"
  212. end
  213. def test__play_repeat
  214. puts "test__play_repeat ENTERED"
  215. ensemble = Ensemble.new("Test Ensemble")
  216. player_1_name = "Test Player 1"
  217. player_1 = Player.new(player_1_name)
  218. note_1_name = "note 1"
  219. note_2_name = "note 2"
  220. note_1 = Note.new(note_1_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  221. note_2 = Note.new(note_2_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  222. expected_1 = [note_1, note_2]
  223. score_1_name = "score 1"
  224. score_1 = Score.new(score_1_name)
  225. score_1 << expected_1
  226. player_1.add_score(score_1_name, score_1)
  227. ensemble.add_player(player_1_name, player_1)
  228. ensemble.play player_1_name
  229. actual = player_1.output
  230. assert(actual.length == expected_1.length)
  231. assert(actual[0].name == expected_1[0].name && actual[1].name == expected_1[1].name)
  232. player_2_name = "Test Player 2"
  233. player_2 = Player.new(player_2_name)
  234. note_3_name = "note 3"
  235. note_4_name = "note 4"
  236. note_3 = Note.new(note_3_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  237. note_4 = Note.new(note_4_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  238. expected_2 = [note_3, note_4]
  239. score_2_name = "score 2"
  240. score_2 = Score.new(score_2_name)
  241. score_2 << expected_2
  242. player_2.add_score(score_2_name, score_2)
  243. ensemble.add_player(player_2_name, player_2)
  244. player_1.clear_output
  245. num_times_repeat = 3
  246. ensemble.play_repeat(player_1_name, num_times_repeat)
  247. actual = player_1.output
  248. assert(actual.length == expected_1.length * num_times_repeat)
  249. assert(actual[0].name == expected_1[0].name && actual[1].name == expected_1[1].name)
  250. ensemble.play_repeat(player_2_name, num_times_repeat)
  251. actual = player_2.output
  252. assert(actual.length == expected_2.length * num_times_repeat)
  253. assert(actual[0].name == expected_2[0].name && actual[1].name == expected_2[1].name)
  254. puts "test__play_repeat COMPLETED"
  255. end
  256. def test__play_all_repeat
  257. puts "test__play_all_repeat ENTERED"
  258. ensemble = Ensemble.new("Test Ensemble")
  259. player_1_name = "Test Player 1"
  260. player_1 = Player.new(player_1_name)
  261. note_1_name = "note 1"
  262. note_2_name = "note 2"
  263. note_1 = Note.new(note_1_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  264. note_2 = Note.new(note_2_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  265. score_1_name = "score 1"
  266. score_1 = Score.new(score_1_name)
  267. expected_1 = [note_1, note_2]
  268. score_1 << expected_1
  269. player_1.add_score(score_1_name, score_1)
  270. ensemble.add_player(player_1_name, player_1)
  271. player_2_name = "Test Player 2"
  272. player_2 = Player.new(player_2_name)
  273. note_3_name = "note 3"
  274. note_4_name = "note 4"
  275. note_3 = Note.new(note_3_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  276. note_4 = Note.new(note_4_name, {:instrument=>1, :start=>0.0, :duration=>4.0, :amplitude=>1000, :pitch=>8.01000, :func_table=>1})
  277. score_2_name = "score 2"
  278. score_2 = Score.new(score_2_name)
  279. expected_2 = [note_3, note_4]
  280. score_2 << expected_2
  281. player_2.add_score(score_2_name, score_2)
  282. ensemble.add_player(player_2_name, player_2)
  283. num_times_repeat = 3
  284. ensemble.play_all_repeat(num_times_repeat)
  285. actual = player_1.output
  286. assert(actual.length == expected_1.length * num_times_repeat)
  287. assert(actual[0].name == expected_1[0].name && actual[1].name == expected_1[1].name)
  288. actual = player_2.output
  289. assert(actual.length == expected_2.length * num_times_repeat)
  290. assert(actual[0].name == expected_2[0].name && actual[1].name == expected_2[1].name)
  291. puts "test__play_all_repeat COMPLETED"
  292. end
  293. end
  294. end