Advertisement
Guest User

Compensate - tests.lua

a guest
Aug 14th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.66 KB | None | 0 0
  1. testing = true
  2. require("compensate")
  3. dbg = false
  4.  
  5.  
  6. -- Initialize the tests table
  7. local tests = {}
  8. edgeID, faceID, bodyID = 1, 1, 1
  9.  
  10.  
  11. -- Define the test functions, adding them to the 'tests' table
  12. tests.nothing = {
  13. nothing = function()
  14. assert(true, "Tested nothing")
  15. end
  16. }
  17.  
  18. tests.formatGCode = {
  19. G0 = {
  20. setup = function()
  21. command = "G0"
  22. err = "Incorrect formatting: expected: %s but it was: %s"
  23. end,
  24. X = function()
  25. local params = {X = 5}
  26. local expected = "G0 X5.000"
  27. local result = formatGCode(command, params)
  28. assert(result == expected, string.format(err, expected, result))
  29. end,
  30. Y = function()
  31. local params = {Y = -5}
  32. local expected = "G0 Y-5.000"
  33. local result = formatGCode(command, params)
  34. assert(result == expected, string.format(err, expected, result))
  35. end,
  36. Z = function()
  37. local params = {Z = 15.5}
  38. local expected = "G0 Z15.500"
  39. local result = formatGCode(command, params)
  40. assert(result == expected, string.format(err, expected, result))
  41. end,
  42. XY = function()
  43. local params = {X = 3, Y = 4}
  44. local expected = "G0 X3.000 Y4.000"
  45. local result = formatGCode(command, params)
  46. assert(result == expected, string.format(err, expected, result))
  47. end,
  48. XZ = function()
  49. local params = {X = 6, Z = -4.5}
  50. local expected = "G0 X6.000 Z-4.500"
  51. local result = formatGCode(command, params)
  52. assert(result == expected, string.format(err, expected, result))
  53. end,
  54. YZ = function()
  55. local params = {Y = -1.11111, Z = -6.666666}
  56. local expected = "G0 Y-1.111 Z-6.667"
  57. local result = formatGCode(command, params)
  58. assert(result == expected, string.format(err, expected, result))
  59. end,
  60. XYZ = function()
  61. local params = {X = 1, Y = 2, Z = 3}
  62. local expected = "G0 X1.000 Y2.000 Z3.000"
  63. local result = formatGCode(command, params)
  64. assert(result == expected, string.format(err, expected, result))
  65. end
  66. },
  67. G1 = {
  68. setup = function()
  69. command = "G1"
  70. err = "Incorrect formatting: expected: %s but it was: %s"
  71. end,
  72. X = function()
  73. local params = {X = 1}
  74. local expected = "G1 X1.000"
  75. local result = formatGCode(command, params)
  76. assert(result == expected, string.format(err, expected, result))
  77. end,
  78. Y = function()
  79. local params = {Y = -1}
  80. local expected = "G1 Y-1.000"
  81. local result = formatGCode(command, params)
  82. assert(result == expected, string.format(err, expected, result))
  83. end,
  84. Z = function()
  85. local params = {Z = 2}
  86. local expected = "G1 Z2.000"
  87. local result = formatGCode(command, params)
  88. assert(result == expected, string.format(err, expected, result))
  89. end,
  90.  
  91. XY = function()
  92. local params = {Y = -5}
  93. local expected = "G1 Y-5.000"
  94. local result = formatGCode(command, params)
  95. assert(result == expected, string.format(err, expected, result))
  96. end,
  97. XZ = function()
  98. local params = {X = 1, Z = 2}
  99. local expected = "G1 X1.000 Z2.000"
  100. local result = formatGCode(command, params)
  101. assert(result == expected, string.format(err, expected, result))
  102. end,
  103. YZ = function()
  104. local params = {Y = 5, Z = 2}
  105. local expected = "G1 Y5.000 Z2.000"
  106. local result = formatGCode(command, params)
  107. assert(result == expected, string.format(err, expected, result))
  108. end,
  109. XYZ = function()
  110. local params = {X = 6, Y = 7, Z = 8}
  111. local expected = "G1 X6.000 Y7.000 Z8.000"
  112. local result = formatGCode(command, params)
  113. assert(result == expected, string.format(err, expected, result))
  114. end,
  115. XYF = function()
  116. local params = {X = 6, Y = 7, F = 60}
  117. local expected = "G1 X6.000 Y7.000 F60.000"
  118. local result = formatGCode(command, params)
  119. assert(result == expected, string.format(err, expected, result))
  120. end
  121. },
  122. G2 = {
  123. setup = function()
  124. command = "G2"
  125. err = "Incorrect formatting: expected: %s but it was: %s"
  126. end,
  127. XYR = function()
  128. local params = {X = 3, Y = 4, R = 1}
  129. local expected = "G2 X3.000 Y4.000 R1.000"
  130. local result = formatGCode(command, params)
  131. assert(result == expected, string.format(err, expected, result))
  132. end,
  133. XYZR = function()
  134. local params = {X = 4, Y = 3, Z = -5, R = 3}
  135. local expected = "G2 X4.000 Y3.000 Z-5.000 R3.000"
  136. local result = formatGCode(command, params)
  137. assert(result == expected, string.format(err, expected, result))
  138. end,
  139. XYI = function()
  140. local params = {X = 4, Y = 3, I = -4}
  141. local expected = "G2 X4.000 Y3.000 I-4.000"
  142. local result = formatGCode(command, params)
  143. assert(result == expected, string.format(err, expected, result))
  144. end,
  145. XYJ = function()
  146. local params = {X = 4, Y = 3, J = -3}
  147. local expected = "G2 X4.000 Y3.000 J-3.000"
  148. local result = formatGCode(command, params)
  149. assert(result == expected, string.format(err, expected, result))
  150. end,
  151. XYIJ = function()
  152. local params = {X = 4, Y = 3, I = -4, J = -3}
  153. local expected = "G2 X4.000 Y3.000 I-4.000 J-3.000"
  154. local result = formatGCode(command, params)
  155. assert(result == expected, string.format(err, expected, result))
  156. end,
  157. XYZIJ = function()
  158. local params = {X = 4, Y = 3, Z = -5, I = -4, J = -3}
  159. local expected = "G2 X4.000 Y3.000 Z-5.000 I-4.000 J-3.000"
  160. local result = formatGCode(command, params)
  161. assert(result == expected, string.format(err, expected, result))
  162. end
  163. },
  164. G3 = {
  165. setup = function()
  166. command = "G3"
  167. err = "Incorrect formatting: expected: %s but it was: %s"
  168. end,
  169. XYR = function()
  170. local params = {X = 3, Y = 4, R = 1}
  171. local expected = "G3 X3.000 Y4.000 R1.000"
  172. local result = formatGCode(command, params)
  173. assert(result == expected, string.format(err, expected, result))
  174. end,
  175. XYZR = function()
  176. local params = {X = 4, Y = 3, Z = -5, R = 3}
  177. local expected = "G3 X4.000 Y3.000 Z-5.000 R3.000"
  178. local result = formatGCode(command, params)
  179. assert(result == expected, string.format(err, expected, result))
  180. end,
  181. XYI = function()
  182. local params = {X = 4, Y = 3, I = -4}
  183. local expected = "G3 X4.000 Y3.000 I-4.000"
  184. local result = formatGCode(command, params)
  185. assert(result == expected, string.format(err, expected, result))
  186. end,
  187. XYJ = function()
  188. local params = {X = 4, Y = 3, J = -3}
  189. local expected = "G3 X4.000 Y3.000 J-3.000"
  190. local result = formatGCode(command, params)
  191. assert(result == expected, string.format(err, expected, result))
  192. end,
  193. XYIJ = function()
  194. local params = {X = 4, Y = 3, I = -4, J = -3}
  195. local expected = "G3 X4.000 Y3.000 I-4.000 J-3.000"
  196. local result = formatGCode(command, params)
  197. assert(result == expected, string.format(err, expected, result))
  198. end,
  199. XYZIJ = function()
  200. local params = {X = 4, Y = 3, Z = -5, I = -4, J = -3}
  201. local expected = "G3 X4.000 Y3.000 Z-5.000 I-4.000 J-3.000"
  202. local result = formatGCode(command, params)
  203. assert(result == expected, string.format(err, expected, result))
  204. end
  205. },
  206. no_command = {
  207. setup = function()
  208. params = {}
  209. err = "Incorrect formatting: expected: %s but it was: %s"
  210. end,
  211. gibberish = function()
  212. local command = "Cats are so nice and good"
  213. local expected = command
  214. local result = formatGCode(command, params)
  215. assert(result == expected, string.format(err, expected, result))
  216. end,
  217. feed = function()
  218. local command = "F600"
  219. local expected = command
  220. local result = formatGCode(command, params)
  221. assert(result == expected, string.format(err, expected, result))
  222. end,
  223. comment = function()
  224. local command = "(Test of a gcode comment)"
  225. local expected = command
  226. local result = formatGCode(command, params)
  227. assert(result == expected, string.format(err, expected, result))
  228. end,
  229. blankLine = function()
  230. local command = ""
  231. local expected = command
  232. local result = formatGCode(command, params)
  233. assert(result == expected, string.format(err, expected, result))
  234. end
  235. }
  236. }
  237.  
  238. tests.findArcCenter = {
  239. setup = function()
  240. err = "Incorrect arc center: expected: %s, %s but it was at: %s, %s"
  241. end,
  242. both_I_J = function()
  243. local params = {I = 3, J = 4}
  244. local nominalXPos, nominalYPos = 1, 2
  245. local clockwise = true
  246. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  247. local expected_cx, expected_cy = 4, 6
  248. assert(cx == expected_cx and cy == expected_cy, string.format(err, expected_cx, expected_cy, cx, cy))
  249. end,
  250. only_I = function()
  251. local params = {I = 3}
  252. local nominalXPos, nominalYPos = 1, 2
  253. local clockwise = true
  254. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  255. local expected_cx, expected_cy = 4, 2
  256. assert(cx == expected_cx and cy == expected_cy, string.format(err, expected_cx, expected_cy, cx, cy))
  257. end,
  258. only_J = function()
  259. local params = {J = 4}
  260. local nominalXPos, nominalYPos = 1, 2
  261. local clockwise = true
  262. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  263. local expected_cx, expected_cy = 1, 6
  264. assert(cx == expected_cx and cy == expected_cy, string.format(err, expected_cx, expected_cy, cx, cy))
  265. end,
  266. R_clockwise = function()
  267. local params = {X = 2, Y = 2, R = 2}
  268. local nominalXPos, nominalYPos = 0, 0
  269. local clockwise = true
  270. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  271. local expected_cx, expected_cy = 2, 0
  272. assert(math.abs(cx - expected_cx) < 1e-6 and math.abs(cy - expected_cy) < 1e-6, string.format(err, expected_cx, expected_cy, cx, cy))
  273. end,
  274. R_counterclockwise_1 = function()
  275. local params = {X = 2, Y = 2, R = 2}
  276. local nominalXPos, nominalYPos = 0, 0
  277. local clockwise = false
  278. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  279. local expected_cx, expected_cy = 0, 2
  280. assert(math.abs(cx - expected_cx) < 1e-6 and math.abs(cy - expected_cy) < 1e-6, string.format(err, expected_cx, expected_cy, cx, cy))
  281. end,
  282. R_counterclockwise_2 = function()
  283. local params = {X = 0, Y = 0, R = 2.5}
  284. local nominalXPos, nominalYPos = 0, 5
  285. local clockwise = false
  286. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  287. local expected_cx, expected_cy = 0, 2.5
  288. assert(math.abs(cx - expected_cx) < 1e-6 and math.abs(cy - expected_cy) < 1e-6, string.format(err, expected_cx, expected_cy, cx, cy))
  289. end,
  290. test_from_cad = function()
  291. local params = {X = -46.41, Y = 13.86, R = 27.2996}
  292. local nominalXPos, nominalYPos = -31.39, 36.2987
  293. local clockwise = false
  294. local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
  295. local expected_cx, expected_cy = -19.182247346859, 11.88069567287
  296. assert(math.abs(cx - expected_cx) < 1e-6 and math.abs(cy - expected_cy) < 1e-6, string.format(err, expected_cx, expected_cy, cx, cy))
  297. end,
  298. R_invalid = function()
  299. local params = {X = 10, Y = 0, R = 3}
  300. local nominalXPos, nominalYPos = 0, 0
  301. local clockwise = true
  302. local status, err = pcall(function() findArcCenter(params, nominalXPos, nominalYPos, clockwise) end)
  303. assert(not status, "Expected error but none was thrown")
  304. end,
  305. missing_parameters = function()
  306. local params = {}
  307. local nominalXPos, nominalYPos = 0, 0
  308. local clockwise = true
  309. local status, err = pcall(function() findArcCenter(params, nominalXPos, nominalYPos, clockwise) end)
  310. assert(not status, "Expected error but none was thrown")
  311. end,
  312. }
  313.  
  314. tests.normalizeAngles = {
  315. setup = function()
  316. err = "Incorrect normalized angles: expected: %s - %s but it was: %s - %s"
  317. end,
  318. fullCircle_startAtZero_clockwise = function()
  319. local startAngle, endAngle, clockwise = 0, 0, true
  320. local expectedStart, expectedEnd = math.pi*2, 0
  321. local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  322. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  323. end,
  324. fullCircle_startAtZero_counterclockwise = function()
  325. local startAngle, endAngle, clockwise = 0, 0, false
  326. local expectedStart, expectedEnd = 0, math.pi*2
  327. local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  328. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  329. end,
  330. fullCircle_startAtOne_clockwise = function()
  331. local startAngle, endAngle, clockwise = 1, 1, true
  332. local expectedStart, expectedEnd = 1 + math.pi*2, 1
  333. local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  334. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  335. end,
  336. fullCircle_startAtOne_counterclockwise = function()
  337. local startAngle, endAngle, clockwise = 1, 1, false
  338. local expectedStart, expectedEnd = 1, 1 + math.pi*2
  339. local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  340. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  341. end,
  342. fullCircle_startJustBefore3 = function()
  343. local startAngle, endAngle, clockwise = -0.01, -0.01, true
  344. local expectedStart, expectedEnd = math.pi * 4 - 0.01, math.pi * 2 - 0.01
  345. local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  346. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  347. end,
  348. both_positive = function()
  349. local startAngle, endAngle, clockwise = 3, 4, false
  350. local expectedStart, expectedEnd = 3, 4
  351. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  352. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  353. end,
  354. start_negative = function()
  355. local startAngle, endAngle, clockwise = -1, 2, false
  356. local expectedStart, expectedEnd = math.pi*2 - 1, math.pi*2 + 2
  357. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  358. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  359. end,
  360. end_negative = function()
  361. local startAngle, endAngle, clockwise = 3, -3, false
  362. local expectedStart, expectedEnd = 3, math.pi*2 - 3
  363. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  364. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  365. end,
  366. both_negative = function()
  367. local startAngle, endAngle, clockwise = -2, -1, false
  368. local expectedStart, expectedEnd = math.pi*2 - 2, math.pi*2 - 1
  369. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  370. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  371. end,
  372. both_positive_outOfRange = function()
  373. local startAngle, endAngle, clockwise = 7, 8, false
  374. local expectedStart, expectedEnd = 7 - math.pi*2, 8 - math.pi*2
  375. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  376. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  377. end,
  378. start_positive_outOfRange = function()
  379. local startAngle, endAngle, clockwise = 7, 6, true
  380. local expectedStart, expectedEnd = 7, 6
  381. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  382. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  383. end,
  384. end_positive_outOfRange = function()
  385. local startAngle, endAngle, clockwise = 6, 7, false
  386. local expectedStart, expectedEnd = 6, 7
  387. startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
  388. assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
  389. end
  390. }
  391.  
  392. tests.checkCrossings = {
  393. clockwise = {
  394. setup = function()
  395. err = "crosses3oclock %s (should be %s) and crosses9oclock %s (should be %s)"
  396. clockwise = true
  397. end,
  398. fullCircle_startingRight = function()
  399. local startAngle, endAngle = math.pi*2, 0
  400. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  401. local expected3, expected9 = false, true
  402. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  403. end,
  404. fullCircle_startingLeft = function()
  405. local startAngle, endAngle = math.pi*3, math.pi
  406. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  407. local expected3, expected9 = true, false
  408. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  409. end,
  410. fullCircle_startingJustBefore3 = function()
  411. local startAngle, endAngle = math.pi * 4 - 0.01, math.pi * 2 - 0.01
  412. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  413. local expected3, expected9 = true, true
  414. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  415. end,
  416. fullCircle_startingJustAfter3 = function()
  417. local startAngle, endAngle = math.pi*2 + 0.01, 0.01
  418. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  419. local expected3, expected9 = true, true
  420. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  421. end,
  422. fullCircle_startingJustBefore9 = function()
  423. local startAngle, endAngle = math.pi*3 - 0.01, math.pi - 0.01
  424. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  425. local expected3, expected9 = true, true
  426. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  427. end,
  428. fullCircle_startingJustAfter9 = function()
  429. local startAngle, endAngle = math.pi*3 + 0.01, math.pi + 0.01
  430. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  431. local expected3, expected9 = true, true
  432. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  433. end,
  434. almostFullCircle_startingRight = function()
  435. local startAngle, endAngle = math.pi*2, 0.1
  436. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  437. local expected3, expected9 = false, true
  438. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  439. end,
  440. almostFullCircle_startingLeft = function()
  441. local startAngle, endAngle = math.pi*3, math.pi + 0.1
  442. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  443. local expected3, expected9 = true, false
  444. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  445. end,
  446. almostFullCircle_startingJustBefore3 = function()
  447. local startAngle, endAngle = math.pi*2 - 0.01, 0.09
  448. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  449. local expected3, expected9 = false, true
  450. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  451. end,
  452. almostFullCircle_startingJustAfter3 = function()
  453. local startAngle, endAngle = math.pi*2 + 0.01, 0.11
  454. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  455. local expected3, expected9 = true, true
  456. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  457. end,
  458. almostFullCircle_startingJustBefore9 = function()
  459. local startAngle, endAngle = math.pi*3 - 0.01, math.pi + 0.09
  460. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  461. local expected3, expected9 = true, false
  462. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  463. end,
  464. almostFullCircle_startingJustAfter9 = function()
  465. local startAngle, endAngle = math.pi*3 + 0.01, math.pi + 0.11
  466. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  467. local expected3, expected9 = true, true
  468. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  469. end,
  470.  
  471. bothTopRight = function()
  472. local startAngle, endAngle = 1.5, 1
  473. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  474. local expected3, expected9 = false, false
  475. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  476. end,
  477. bothTopLeft = function()
  478. local startAngle, endAngle = 3, 2
  479. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  480. local expected3, expected9 = false, false
  481. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  482. end,
  483. bothBottomLeft = function()
  484. local startAngle, endAngle = 4, 3.5
  485. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  486. local expected3, expected9 = false, false
  487. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  488. end,
  489. bothBottomRight = function()
  490. local startAngle, endAngle = 6, 5
  491. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  492. local expected3, expected9 = false, false
  493. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  494. end,
  495.  
  496. startTopRight_endBottomRight = function()
  497. local startAngle, endAngle = 2*math.pi + 1.5, 6
  498. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  499. local expected3, expected9 = true, false
  500. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  501. end,
  502. startTopRight_endBottomLeft = function()
  503. local startAngle, endAngle = 2*math.pi + 0.5, 4.5
  504. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  505. local expected3, expected9 = true, false
  506. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  507. end,
  508.  
  509.  
  510. startBottomRight_endBottomLeft = function()
  511. local startAngle, endAngle = 6, 4
  512. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  513. local expected3, expected9 = false, false
  514. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  515. end,
  516. startBottomRight_endTopLeft = function()
  517. local startAngle, endAngle = 5, 3
  518. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  519. local expected3, expected9 = false, true
  520. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  521. end,
  522.  
  523. startBottomLeft_endTopLeft = function()
  524. local startAngle, endAngle = 4, 3
  525. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  526. local expected3, expected9 = false, true
  527. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  528. end,
  529. startBottomLeft_endTopRight = function()
  530. local startAngle, endAngle = 4, 1.5
  531. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  532. local expected3, expected9 = false, true
  533. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  534. end,
  535.  
  536. startTopLeft_endTopRight = function()
  537. local startAngle, endAngle = 2, 1
  538. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  539. local expected3, expected9 = false, false
  540. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  541. end,
  542. startTopleft_endBottomRight = function()
  543. local startAngle, endAngle = 2*math.pi + 2, 6
  544. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  545. local expected3, expected9 = true, false
  546. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  547. end,
  548. startOn3 = function()
  549. local startAngle, endAngle = 2*math.pi, 6
  550. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  551. local expected3, expected9 = false, false
  552. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  553. end,
  554. startOn9 = function()
  555. local startAngle, endAngle = math.pi, 3
  556. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  557. local expected3, expected9 = false, false
  558. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  559. end,
  560. endOn3 = function()
  561. local startAngle, endAngle = 1, 0
  562. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  563. local expected3, expected9 = false, false
  564. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  565. end,
  566. endOn9 = function()
  567. local startAngle, endAngle = 4, math.pi
  568. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  569. local expected3, expected9 = false, false
  570. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  571. end,
  572. startOn3_endOn9 = function()
  573. local startAngle, endAngle = 2*math.pi, math.pi
  574. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  575. local expected3, expected9 = false, false
  576. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  577. end,
  578. startOn9_endOn3 = function()
  579. local startAngle, endAngle = math.pi, 0
  580. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  581. local expected3, expected9 = false, false
  582. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  583. end
  584. },
  585. counterclockwise = {
  586. setup = function()
  587. err = "crosses3oclock %s (should be %s) and crosses9oclock %s (should be %s)"
  588. clockwise = false
  589. end,
  590.  
  591. bothTopRight = function()
  592. local startAngle, endAngle = 0.5, 1
  593. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  594. local expected3, expected9 = false, false
  595. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  596. end,
  597. bothTopLeft = function()
  598. local startAngle, endAngle = 2, 2.5
  599. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  600. local expected3, expected9 = false, false
  601. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  602. end,
  603. bothBottomLeft = function()
  604. local startAngle, endAngle = 4, 4.5
  605. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  606. local expected3, expected9 = false, false
  607. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  608. end,
  609. bothBottomRight = function()
  610. local startAngle, endAngle = 5, 6
  611. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  612. local expected3, expected9 = false, false
  613. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  614. end,
  615.  
  616. startTopRight_endTopLeft = function()
  617. local startAngle, endAngle = 1.5, 2
  618. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  619. local expected3, expected9 = false, false
  620. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  621. end,
  622. startTopRight_endBottomLeft = function()
  623. local startAngle, endAngle = 1.5, 3.5
  624. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  625. local expected3, expected9 = false, true
  626. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  627. end,
  628.  
  629. startTopLeft_endBottomLeft = function()
  630. local startAngle, endAngle = 3, 3.5
  631. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  632. local expected3, expected9 = false, true
  633. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  634. end,
  635. startTopLeft_endBottomRight = function()
  636. local startAngle, endAngle = 3, 5
  637. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  638. local expected3, expected9 = false, true
  639. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  640. end,
  641.  
  642. startBottomLeft_endBottomRight = function()
  643. local startAngle, endAngle = 4.5, 5
  644. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  645. local expected3, expected9 = false, false
  646. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  647. end,
  648. startBottomLeft_endTopRight = function()
  649. local startAngle, endAngle = 4.5, 0.5 + 2*math.pi
  650. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  651. local expected3, expected9 = true, false
  652. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  653. end,
  654.  
  655. startBottomRight_endTopRight = function()
  656. local startAngle, endAngle = 6, 0.5 + 2*math.pi
  657. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  658. local expected3, expected9 = true, false
  659. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  660. end,
  661. startBottomRight_endTopLeft = function()
  662. local startAngle, endAngle = 6, 2 + 2*math.pi
  663. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  664. local expected3, expected9 = true, false
  665. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  666. end,
  667.  
  668. startOn3 = function()
  669. local startAngle, endAngle = 0, 1
  670. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  671. local expected3, expected9 = false, false
  672. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  673. end,
  674. startOn9 = function()
  675. local startAngle, endAngle = math.pi, 4
  676. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  677. local expected3, expected9 = false, false
  678. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  679. end,
  680. endOn3 = function()
  681. local startAngle, endAngle = 6, 2*math.pi
  682. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  683. local expected3, expected9 = false, false
  684. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  685. end,
  686. endOn9 = function()
  687. local startAngle, endAngle = 3, math.pi
  688. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  689. local expected3, expected9 = false, false
  690. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  691. end,
  692. startOn3_endOn9 = function()
  693. local startAngle, endAngle = 0, math.pi
  694. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  695. local expected3, expected9 = false, false
  696. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  697. end,
  698. startOn9_endOn3 = function()
  699. local startAngle, endAngle = math.pi, 2*math.pi
  700. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  701. local expected3, expected9 = false, false
  702. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  703. end,
  704. specialFailureCase = function()
  705. local startAngle, endAngle = 5.8893166964432, 11.389442898301
  706. local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
  707. local expected3, expected9 = true, true
  708. assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
  709. end,
  710. }
  711. }
  712.  
  713. tests.splitArc = {
  714. g2 = {
  715. setup = function()
  716. command = "G2"
  717. end,
  718. same_Z_pass_through_3 = function()
  719. local nominalPos = {X = 0, Y = 10, Z = 0}
  720. local command, params = "G2", {X = 0, Y = 0, R = 5}
  721. local expected = {
  722. {command = "G2", params = {X = 5.000, Y = 5.000, R = 5.000}},
  723. {command = "G2", params = {X = 0.000, Y = 0.000, R = 5.000}}
  724. }
  725. local expectedString = commandTableToString(expected)
  726. local result = splitArc(command, params, nominalPos)
  727. local resultString = commandTableToString(result)
  728. assert(
  729. #result == #expected and resultString == expectedString,
  730. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  731. )
  732. end,
  733. same_Z_pass_through_9 = function()
  734. local nominalPos = {X = 0, Y = 0, Z = 0}
  735. local command, params = "G2", {X = 0, Y = 10, R = 5}
  736. local expected = {
  737. {command = "G2", params = {X = -5.000, Y = 5.000, R = 5.000}},
  738. {command = "G2", params = {X = 0.000, Y = 10.000, R = 5.000}}
  739. }
  740. local expectedString = commandTableToString(expected)
  741. local result = splitArc(command, params, nominalPos)
  742. local resultString = commandTableToString(result)
  743. assert(
  744. #result == #expected and resultString == expectedString,
  745. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  746. )
  747. end,
  748. same_Z_pass_through_neither = function()
  749. local nominalPos = {X = -1, Y = 0, Z = 0}
  750. local command, params = "G2", {X = 1, Y = 0, R = 5}
  751. local expected = {
  752. {command = "G2", params = {X = 1.000, Y = 0.000, R = 5.000}}
  753. }
  754. local expectedString = commandTableToString(expected)
  755. local result = splitArc(command, params, nominalPos)
  756. local resultString = commandTableToString(result)
  757. assert(
  758. #result == #expected and resultString == expectedString,
  759. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  760. )
  761. end,
  762. same_Z_almostFullCircle_startBefore3 = function()
  763. local nominalPos = {X = 5.231, Y = 11.846, Z = 0}
  764. local command, params = "G2", {X = 5.096, Y = 11.981, I = -0.231, J = -0.096}
  765. local expected = {
  766. {command = "G2", params = {X = 5.250, Y = 11.750, R = 0.250}},
  767. {command = "G2", params = {X = 4.750, Y = 11.750, R = 0.250}},
  768. {command = "G2", params = {X = 5.096, Y = 11.981, R = 0.250}}
  769. }
  770. local expectedString = commandTableToString(expected)
  771. local result = splitArc(command, params, nominalPos)
  772. local resultString = commandTableToString(result)
  773. assert(
  774. #result == #expected and resultString == expectedString,
  775. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  776. )
  777. end,
  778. different_Z_pass_through_3 = function()
  779. local nominalPos = {X = 0, Y = 10, Z = 0}
  780. local command, params = "G2", {X = 0, Y = 0, Z = -1, R = 5}
  781. local expected = {
  782. {command = "G2", params = {X = 5.000, Y = 5.000, Z = -0.500, R = 5.000}},
  783. {command = "G2", params = {X = 0.000, Y = 0.000, Z = -1.000, R = 5.000}}
  784. }
  785. local expectedString = commandTableToString(expected)
  786. local result = splitArc(command, params, nominalPos)
  787. local resultString = commandTableToString(result)
  788. assert(
  789. #result == #expected and resultString == expectedString,
  790. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  791. )
  792. end,
  793. different_Z_pass_through_9 = function()
  794. local nominalPos = {X = 0, Y = 0, Z = 0}
  795. local command, params = "G2", {X = 0, Y = 10, Z = -4, R = 5}
  796. local expected = {
  797. {command = "G2", params = {X = -5.000, Y = 5.000, Z = -2.000, R = 5.000}},
  798. {command = "G2", params = {X = 0.000, Y = 10.000, Z = -4.000, R = 5.000}}
  799. }
  800. local expectedString = commandTableToString(expected)
  801. local result = splitArc(command, params, nominalPos)
  802. local resultString = commandTableToString(result)
  803.  
  804. assert(
  805. #result == #expected and resultString == expectedString,
  806. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  807. )
  808. end,
  809. different_Z_pass_through_neither = function()
  810. local nominalPos = {X = -1, Y = 0, Z = 0}
  811. local command, params = "G2", {X = 1, Y = 0, Z = -1, R = 5}
  812. local expected = {
  813. {command = "G2", params = {X = 1.000, Y = 0.000, Z = -1.000, R = 5.000}}
  814. }
  815. local expectedString = commandTableToString(expected)
  816. local result = splitArc(command, params, nominalPos)
  817. local resultString = commandTableToString(result)
  818. assert(
  819. #result == #expected and resultString == expectedString,
  820. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  821. )
  822. end
  823. },
  824. g3 = {
  825. same_Z_pass_through_3 = function()
  826. local nominalPos = {X = 0, Y = 0, Z = 0}
  827. local command, params = "G3", {X = 0, Y = 10, R = 5}
  828. local expected = {
  829. {command = "G3", params = {X = 5.000, Y = 5.000, R = 5.000}},
  830. {command = "G3", params = {X = 0.000, Y = 10.000, R = 5.000}}
  831. }
  832. local expectedString = commandTableToString(expected)
  833. local result = splitArc(command, params, nominalPos)
  834. local resultString = commandTableToString(result)
  835. assert(
  836. #result == #expected and resultString == expectedString,
  837. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  838. )
  839. end,
  840. same_Z_pass_through_9 = function()
  841. local nominalPos = {X = 0, Y = 10, Z = 0}
  842. local command, params = "G3", {X = 0, Y = 0, R = 5}
  843. local expected = {
  844. {command = "G3", params = {X = -5.000, Y = 5.000, R = 5.000}},
  845. {command = "G3", params = {X = 0.000, Y = 0.000, R = 5.000}}
  846. }
  847. local expectedString = commandTableToString(expected)
  848. local result = splitArc(command, params, nominalPos)
  849. local resultString = commandTableToString(result)
  850. assert(
  851. #result == #expected and resultString == expectedString,
  852. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  853. )
  854. end,
  855. same_Z_pass_through_neither = function()
  856. local nominalPos = {X = -1, Y = 0, Z = 0}
  857. local command, params = "G3", {X = 1, Y = 0, R = 5}
  858. local expected = {
  859. {command = "G3", params = {X = 1.000, Y = 0.000, R = 5.000}}
  860. }
  861. local expectedString = commandTableToString(expected)
  862. local result = splitArc(command, params, nominalPos)
  863. local resultString = commandTableToString(result)
  864. assert(
  865. #result == #expected and resultString == expectedString,
  866. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  867. )
  868. end,
  869. same_Z_fullCircle_startingAt3 = function()
  870. local nominalPos = {X = 0, Y = 0, Z = 0}
  871. local command, params = "G3", {X = 0, Y = 0, I = -1}
  872. local expected = {
  873. {command = "G3", params = {X = -2.000, Y = 0.000, R = 1.000}},
  874. {command = "G3", params = {X = 0.000, Y = 0.000, R = 1.000}}
  875. }
  876. local expectedString = commandTableToString(expected)
  877. local result = splitArc(command, params, nominalPos)
  878. local resultString = commandTableToString(result)
  879. assert(
  880. #result == #expected and resultString == expectedString,
  881. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  882. )
  883. end,
  884. same_Z_fullCircle_startBefore3 = function()
  885. local nominalPos = {X = 3, Y = 4, Z = 0}
  886. local command, params = "G3", {X = 3, Y = 4, I = -3, J = -4}
  887. local expected = {
  888. {command = "G3", params = {X = -5.000, Y = 0.000, R = 5.000}},
  889. {command = "G3", params = {X = 5.000, Y = 0.000, R = 5.000}},
  890. {command = "G3", params = {X = 3.000, Y = 4.000, R = 5.000}}
  891. }
  892. local expectedString = commandTableToString(expected)
  893. local result = splitArc(command, params, nominalPos)
  894. local resultString = commandTableToString(result)
  895. assert(
  896. #result == #expected and resultString == expectedString,
  897. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  898. )
  899. end,
  900. same_Z_almostFullCircle_startAfter3 = function()
  901. local nominalPos = {X = 5.231, Y = -11.846, Z = 0}
  902. local command, params = "G3", {X = 5.096, Y = -11.981, I = -0.231, J = 0.096}
  903. local expected = {
  904. {command = "G3", params = {X = 5.250, Y = -11.75, R = 0.250}},
  905. {command = "G3", params = {X = 4.750, Y = -11.75, R = 0.250}},
  906. {command = "G3", params = {X = 5.096, Y = -11.981, R = 0.250}}
  907. }
  908. local expectedString = commandTableToString(expected)
  909. local result = splitArc(command, params, nominalPos)
  910. local resultString = commandTableToString(result)
  911. assert(
  912. #result == #expected and resultString == expectedString,
  913. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  914. )
  915. end,
  916. different_Z_pass_through_3 = function()
  917. local nominalPos = {X = 0, Y = 0, Z = 0}
  918. local command, params = "G3", {X = 0, Y = 10, Z = -5, R = 5}
  919. local expected = {
  920. {command = "G3", params = {X = 5.000, Y = 5.000, Z = -2.500, R = 5.000}},
  921. {command = "G3", params = {X = 0.000, Y = 10.000, Z = -5.000, R = 5.000}}
  922. }
  923. local expectedString = commandTableToString(expected)
  924. local result = splitArc(command, params, nominalPos)
  925. local resultString = commandTableToString(result)
  926. assert(
  927. #result == #expected and resultString == expectedString,
  928. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  929. )
  930. end,
  931. different_Z_pass_through_9 = function()
  932. local nominalPos = {X = 0, Y = 10, Z = 0}
  933. local command, params = "G3", {X = 0, Y = 0, Z = -3, R = 5}
  934. local expected = {
  935. {command = "G3", params = {X = -5.000, Y = 5.000, Z = -1.5000, R = 5.000}},
  936. {command = "G3", params = {X = 0.000, Y = 0.000, Z = -3.000, R = 5.000}}
  937. }
  938. local expectedString = commandTableToString(expected)
  939. local result = splitArc(command, params, nominalPos)
  940. local resultString = commandTableToString(result)
  941. assert(
  942. #result == #expected and resultString == expectedString,
  943. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  944. )
  945. end,
  946. different_Z_pass_through_neither = function()
  947. local nominalPos = {X = -1, Y = 0, Z = 0}
  948. local command, params = "G3", {X = 1, Y = 0, Z = -2, R = 5}
  949. local expected = {
  950. {command = "G3", params = {X = 1.000, Y = 0.000, Z = -2.000, R = 5.000}}
  951. }
  952. local expectedString = commandTableToString(expected)
  953. local result = splitArc(command, params, nominalPos)
  954. local resultString = commandTableToString(result)
  955. assert(
  956. #result == #expected and resultString == expectedString,
  957. string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
  958. )
  959. end
  960. }
  961. }
  962.  
  963.  
  964. -- Helper function to convert a command table to a formatted string
  965. function commandTableToString(commandTable)
  966. local commandString = ""
  967. for _, cmd in ipairs(commandTable) do
  968. commandString = commandString .. formatGCode(cmd.command, cmd.params) .. "\n"
  969. end
  970. return commandString:sub(1, -2) -- Remove the last newline character
  971. end
  972.  
  973.  
  974. local function runTestsRecursively(tests, prefix)
  975. local testResults = {}
  976. local testCount = 0
  977. local passCount = 0
  978.  
  979. for testName, testContent in pairs(tests) do
  980. if type(testContent) == "function" and testName ~= "setup" then
  981. -- Reset the bodies table before each test function
  982. bodies = {}
  983. if tests.setup then
  984. tests.setup() -- Call the setup function if present
  985. end
  986.  
  987. testCount = testCount + 1
  988. dbg = false
  989. local status, err = pcall(testContent)
  990. if status then
  991. passCount = passCount + 1
  992. else
  993. table.insert(testResults, {testName = prefix .. "." .. testName, err = err})
  994. end
  995. elseif type(testContent) == "table" then
  996. local subResults, subCount, subPass = runTestsRecursively(testContent, prefix .. "." .. testName)
  997. testCount = testCount + subCount
  998. passCount = passCount + subPass
  999. for _, result in ipairs(subResults) do
  1000. table.insert(testResults, result)
  1001. end
  1002. end
  1003. end
  1004.  
  1005. return testResults, testCount, passCount
  1006. end
  1007.  
  1008. function runAllTests()
  1009. local testResults, testCount, passCount = runTestsRecursively(tests, "tests")
  1010.  
  1011. print(string.format("%d tests run, %d tests passed, %d tests failed", testCount, passCount, testCount - passCount))
  1012. for _, result in ipairs(testResults) do
  1013. print(string.format("%s failed: %s", result.testName, result.err))
  1014. end
  1015. end
  1016.  
  1017.  
  1018. runAllTests()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement