Guest User

Untitled

a guest
Dec 16th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #define PROFILE_AND_VERIFY new /datum/profile_and_verify
  2.  
  3. /datum/profile_and_verify
  4. var/list/callbacks
  5. var/sample_size
  6. var/random_input
  7. var/compare_output
  8.  
  9. /datum/profile_and_verify/New(_sample_size, _random_input, _compare_output, ...)
  10. sample_size = _sample_size
  11.  
  12. random_input = _random_input
  13.  
  14. compare_output = _compare_output
  15.  
  16. callbacks = args.Copy(4)
  17.  
  18. start()
  19.  
  20. /datum/profile_and_verify/proc/start()
  21. var/list/options = list()
  22. for(var/i in 1 to callbacks.len)
  23. options += i
  24.  
  25. var/percent_done = 0
  26. var/hundredth = CEILING(sample_size / 100, 1)
  27.  
  28. for(var/i in 1 to sample_size)
  29. var/list/current_run = options.Copy()
  30.  
  31. var/list/input
  32. if(random_input)
  33. input = call(random_input)()
  34. if(!istype(input, /list))
  35. input = list(input)
  36.  
  37. var/list/current_outputs = list()
  38.  
  39. while(current_run.len)
  40. var/choice = pick(current_run)
  41. current_run -= choice
  42. var/callback = callbacks[choice]
  43.  
  44. var/output = call(callback)(arglist(input))
  45.  
  46. if(compare_output)
  47. var/bad_output = FALSE
  48. for(var/k in 1 to length(current_outputs))
  49. bad_output = call(compare_output)(output, current_outputs[k]) || bad_output
  50. if(bad_output)
  51. var/usr_out = "A tested proc has different outputs than the others:\nproc:[callback]\noutput:[bad_output]\nargs:"
  52. for(var/k in 1 to input.len)
  53. usr_out = "[usr_out] \"[input[k]]\""
  54. usr << usr_out
  55. sleep(1)
  56.  
  57. current_outputs += list(output)
  58.  
  59. if(i % hundredth == 0)
  60. usr << "profile [++percent_done]% done"
  61. sleep(1)
  62.  
  63. //////////////////////////////////////////////////////////////////////////EXAMPLE
  64.  
  65. /obj/fake_port
  66. var/width
  67. var/dwidth
  68. var/height
  69. var/dheight
  70.  
  71. /mob/verb/profile_ordered_turfs()
  72. PROFILE_AND_VERIFY(
  73. 1000000,
  74. .proc/rand_ordered,
  75. .proc/compare_ordered,
  76. .proc/new_return_ordered_turfs,
  77. .proc/old_return_ordered_turfs,
  78. )
  79.  
  80. /proc/rand_ordered()
  81. var/obj/fake_port/fake_port = new()
  82. fake_port.width = rand(1, 100)
  83. fake_port.height = rand(1, 100)
  84. fake_port.dwidth = rand(-100, 100)
  85. fake_port.dheight = rand(-100, 100)
  86. fake_port.dir = pick(list(1, 2, 4, 8))
  87.  
  88. return list(rand(1, 100), rand(1, 100), rand(1, 10), pick(list(1, 2, 4, 8)), fake_port)
  89.  
  90. /proc/compare_ordered(var/list/output1, var/list/output2)
  91. if(length(output1) != length(output2))
  92. return "The 2 outputs have different lengths"
  93.  
  94. . = ""
  95. for(var/i in 1 to output1.len)
  96. var/key1 = output1[i]
  97. var/key2 = output2[i]
  98.  
  99. var/value1 = output1[key1]
  100. var/value2 = output2[key2]
  101. if(key1 != key2)
  102. . = "[.]\nkeys out of order: [i] ([key1]->[key2])"
  103. if(value1 != value2)
  104. . = "[.]\nvalues out of order: [i] ([value1]->[value2])"
  105.  
  106. /proc/new_return_ordered_turfs(_x, _y, _z, _dir, obj/fake_port/port)
  107. var/cos = 1
  108. var/sin = 0
  109. switch(_dir)
  110. if(WEST)
  111. cos = 0
  112. sin = 1
  113. if(SOUTH)
  114. cos = -1
  115. sin = 0
  116. if(EAST)
  117. cos = 0
  118. sin = -1
  119.  
  120. . = list()
  121.  
  122. .["base"] = "[cos],[sin]"
  123.  
  124. for(var/dx in 0 to port.width-1)
  125. var/compX = dx-port.dwidth
  126. for(var/dy in 0 to port.height-1)
  127. var/compY = dy-port.dheight
  128. // realX = _x + compX*cos - compY*sin
  129. // realY = _y + compY*cos + compX*sin
  130. // locate(realX, realY, _z)
  131. var/static/turf_num = 1
  132. .["turf#[turf_num++]"] = "[_x + compX*cos - compY*sin],[_y + compY*cos + compX*sin],[_z]"
  133.  
  134. /proc/old_return_ordered_turfs(_x, _y, _z, _dir, obj/fake_port/port)
  135. if(!_dir)
  136. _dir = port.dir
  137. if(!_x)
  138. _x = port.x
  139. if(!_y)
  140. _y = port.y
  141. if(!_z)
  142. _z = port.z
  143. var/cos = 1
  144. var/sin = 0
  145. switch(_dir)
  146. if(WEST)
  147. cos = 0
  148. sin = 1
  149. if(SOUTH)
  150. cos = -1
  151. sin = 0
  152. if(EAST)
  153. cos = 0
  154. sin = -1
  155.  
  156. . = list()
  157.  
  158. .["base"] = "[cos],[sin]"
  159.  
  160. var/xi
  161. var/yi
  162. for(var/dx=0, dx<port.width, ++dx)
  163. for(var/dy=0, dy<port.height, ++dy)
  164. xi = _x + (dx-port.dwidth)*cos - (dy-port.dheight)*sin
  165. yi = _y + (dy-port.dheight)*cos + (dx-port.dwidth)*sin
  166. var/static/turf_num = 1
  167. .["turf#[turf_num++]"] = "[xi],[yi],[_z]"
Add Comment
Please, Sign In to add comment