Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- testing = true
- require("compensate")
- dbg = false
- -- Initialize the tests table
- local tests = {}
- edgeID, faceID, bodyID = 1, 1, 1
- -- Define the test functions, adding them to the 'tests' table
- tests.nothing = {
- nothing = function()
- assert(true, "Tested nothing")
- end
- }
- tests.formatGCode = {
- G0 = {
- setup = function()
- command = "G0"
- err = "Incorrect formatting: expected: %s but it was: %s"
- end,
- X = function()
- local params = {X = 5}
- local expected = "G0 X5.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- Y = function()
- local params = {Y = -5}
- local expected = "G0 Y-5.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- Z = function()
- local params = {Z = 15.5}
- local expected = "G0 Z15.500"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XY = function()
- local params = {X = 3, Y = 4}
- local expected = "G0 X3.000 Y4.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XZ = function()
- local params = {X = 6, Z = -4.5}
- local expected = "G0 X6.000 Z-4.500"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- YZ = function()
- local params = {Y = -1.11111, Z = -6.666666}
- local expected = "G0 Y-1.111 Z-6.667"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYZ = function()
- local params = {X = 1, Y = 2, Z = 3}
- local expected = "G0 X1.000 Y2.000 Z3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end
- },
- G1 = {
- setup = function()
- command = "G1"
- err = "Incorrect formatting: expected: %s but it was: %s"
- end,
- X = function()
- local params = {X = 1}
- local expected = "G1 X1.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- Y = function()
- local params = {Y = -1}
- local expected = "G1 Y-1.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- Z = function()
- local params = {Z = 2}
- local expected = "G1 Z2.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XY = function()
- local params = {Y = -5}
- local expected = "G1 Y-5.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XZ = function()
- local params = {X = 1, Z = 2}
- local expected = "G1 X1.000 Z2.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- YZ = function()
- local params = {Y = 5, Z = 2}
- local expected = "G1 Y5.000 Z2.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYZ = function()
- local params = {X = 6, Y = 7, Z = 8}
- local expected = "G1 X6.000 Y7.000 Z8.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYF = function()
- local params = {X = 6, Y = 7, F = 60}
- local expected = "G1 X6.000 Y7.000 F60.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end
- },
- G2 = {
- setup = function()
- command = "G2"
- err = "Incorrect formatting: expected: %s but it was: %s"
- end,
- XYR = function()
- local params = {X = 3, Y = 4, R = 1}
- local expected = "G2 X3.000 Y4.000 R1.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYZR = function()
- local params = {X = 4, Y = 3, Z = -5, R = 3}
- local expected = "G2 X4.000 Y3.000 Z-5.000 R3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYI = function()
- local params = {X = 4, Y = 3, I = -4}
- local expected = "G2 X4.000 Y3.000 I-4.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYJ = function()
- local params = {X = 4, Y = 3, J = -3}
- local expected = "G2 X4.000 Y3.000 J-3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYIJ = function()
- local params = {X = 4, Y = 3, I = -4, J = -3}
- local expected = "G2 X4.000 Y3.000 I-4.000 J-3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYZIJ = function()
- local params = {X = 4, Y = 3, Z = -5, I = -4, J = -3}
- local expected = "G2 X4.000 Y3.000 Z-5.000 I-4.000 J-3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end
- },
- G3 = {
- setup = function()
- command = "G3"
- err = "Incorrect formatting: expected: %s but it was: %s"
- end,
- XYR = function()
- local params = {X = 3, Y = 4, R = 1}
- local expected = "G3 X3.000 Y4.000 R1.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYZR = function()
- local params = {X = 4, Y = 3, Z = -5, R = 3}
- local expected = "G3 X4.000 Y3.000 Z-5.000 R3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYI = function()
- local params = {X = 4, Y = 3, I = -4}
- local expected = "G3 X4.000 Y3.000 I-4.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYJ = function()
- local params = {X = 4, Y = 3, J = -3}
- local expected = "G3 X4.000 Y3.000 J-3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYIJ = function()
- local params = {X = 4, Y = 3, I = -4, J = -3}
- local expected = "G3 X4.000 Y3.000 I-4.000 J-3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- XYZIJ = function()
- local params = {X = 4, Y = 3, Z = -5, I = -4, J = -3}
- local expected = "G3 X4.000 Y3.000 Z-5.000 I-4.000 J-3.000"
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end
- },
- no_command = {
- setup = function()
- params = {}
- err = "Incorrect formatting: expected: %s but it was: %s"
- end,
- gibberish = function()
- local command = "Cats are so nice and good"
- local expected = command
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- feed = function()
- local command = "F600"
- local expected = command
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- comment = function()
- local command = "(Test of a gcode comment)"
- local expected = command
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end,
- blankLine = function()
- local command = ""
- local expected = command
- local result = formatGCode(command, params)
- assert(result == expected, string.format(err, expected, result))
- end
- }
- }
- tests.findArcCenter = {
- setup = function()
- err = "Incorrect arc center: expected: %s, %s but it was at: %s, %s"
- end,
- both_I_J = function()
- local params = {I = 3, J = 4}
- local nominalXPos, nominalYPos = 1, 2
- local clockwise = true
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = 4, 6
- assert(cx == expected_cx and cy == expected_cy, string.format(err, expected_cx, expected_cy, cx, cy))
- end,
- only_I = function()
- local params = {I = 3}
- local nominalXPos, nominalYPos = 1, 2
- local clockwise = true
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = 4, 2
- assert(cx == expected_cx and cy == expected_cy, string.format(err, expected_cx, expected_cy, cx, cy))
- end,
- only_J = function()
- local params = {J = 4}
- local nominalXPos, nominalYPos = 1, 2
- local clockwise = true
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = 1, 6
- assert(cx == expected_cx and cy == expected_cy, string.format(err, expected_cx, expected_cy, cx, cy))
- end,
- R_clockwise = function()
- local params = {X = 2, Y = 2, R = 2}
- local nominalXPos, nominalYPos = 0, 0
- local clockwise = true
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = 2, 0
- 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))
- end,
- R_counterclockwise_1 = function()
- local params = {X = 2, Y = 2, R = 2}
- local nominalXPos, nominalYPos = 0, 0
- local clockwise = false
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = 0, 2
- 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))
- end,
- R_counterclockwise_2 = function()
- local params = {X = 0, Y = 0, R = 2.5}
- local nominalXPos, nominalYPos = 0, 5
- local clockwise = false
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = 0, 2.5
- 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))
- end,
- test_from_cad = function()
- local params = {X = -46.41, Y = 13.86, R = 27.2996}
- local nominalXPos, nominalYPos = -31.39, 36.2987
- local clockwise = false
- local cx, cy = findArcCenter(params, nominalXPos, nominalYPos, clockwise)
- local expected_cx, expected_cy = -19.182247346859, 11.88069567287
- 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))
- end,
- R_invalid = function()
- local params = {X = 10, Y = 0, R = 3}
- local nominalXPos, nominalYPos = 0, 0
- local clockwise = true
- local status, err = pcall(function() findArcCenter(params, nominalXPos, nominalYPos, clockwise) end)
- assert(not status, "Expected error but none was thrown")
- end,
- missing_parameters = function()
- local params = {}
- local nominalXPos, nominalYPos = 0, 0
- local clockwise = true
- local status, err = pcall(function() findArcCenter(params, nominalXPos, nominalYPos, clockwise) end)
- assert(not status, "Expected error but none was thrown")
- end,
- }
- tests.normalizeAngles = {
- setup = function()
- err = "Incorrect normalized angles: expected: %s - %s but it was: %s - %s"
- end,
- fullCircle_startAtZero_clockwise = function()
- local startAngle, endAngle, clockwise = 0, 0, true
- local expectedStart, expectedEnd = math.pi*2, 0
- local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- fullCircle_startAtZero_counterclockwise = function()
- local startAngle, endAngle, clockwise = 0, 0, false
- local expectedStart, expectedEnd = 0, math.pi*2
- local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- fullCircle_startAtOne_clockwise = function()
- local startAngle, endAngle, clockwise = 1, 1, true
- local expectedStart, expectedEnd = 1 + math.pi*2, 1
- local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- fullCircle_startAtOne_counterclockwise = function()
- local startAngle, endAngle, clockwise = 1, 1, false
- local expectedStart, expectedEnd = 1, 1 + math.pi*2
- local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- fullCircle_startJustBefore3 = function()
- local startAngle, endAngle, clockwise = -0.01, -0.01, true
- local expectedStart, expectedEnd = math.pi * 4 - 0.01, math.pi * 2 - 0.01
- local startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- both_positive = function()
- local startAngle, endAngle, clockwise = 3, 4, false
- local expectedStart, expectedEnd = 3, 4
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- start_negative = function()
- local startAngle, endAngle, clockwise = -1, 2, false
- local expectedStart, expectedEnd = math.pi*2 - 1, math.pi*2 + 2
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- end_negative = function()
- local startAngle, endAngle, clockwise = 3, -3, false
- local expectedStart, expectedEnd = 3, math.pi*2 - 3
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- both_negative = function()
- local startAngle, endAngle, clockwise = -2, -1, false
- local expectedStart, expectedEnd = math.pi*2 - 2, math.pi*2 - 1
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- both_positive_outOfRange = function()
- local startAngle, endAngle, clockwise = 7, 8, false
- local expectedStart, expectedEnd = 7 - math.pi*2, 8 - math.pi*2
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- start_positive_outOfRange = function()
- local startAngle, endAngle, clockwise = 7, 6, true
- local expectedStart, expectedEnd = 7, 6
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end,
- end_positive_outOfRange = function()
- local startAngle, endAngle, clockwise = 6, 7, false
- local expectedStart, expectedEnd = 6, 7
- startAngle, endAngle = normalizeAngles(startAngle, endAngle, clockwise)
- assert(startAngle == expectedStart and endAngle == expectedEnd, string.format(err, expectedStart, expectedEnd, startAngle, endAngle))
- end
- }
- tests.checkCrossings = {
- clockwise = {
- setup = function()
- err = "crosses3oclock %s (should be %s) and crosses9oclock %s (should be %s)"
- clockwise = true
- end,
- fullCircle_startingRight = function()
- local startAngle, endAngle = math.pi*2, 0
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- fullCircle_startingLeft = function()
- local startAngle, endAngle = math.pi*3, math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- fullCircle_startingJustBefore3 = function()
- local startAngle, endAngle = math.pi * 4 - 0.01, math.pi * 2 - 0.01
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- fullCircle_startingJustAfter3 = function()
- local startAngle, endAngle = math.pi*2 + 0.01, 0.01
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- fullCircle_startingJustBefore9 = function()
- local startAngle, endAngle = math.pi*3 - 0.01, math.pi - 0.01
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- fullCircle_startingJustAfter9 = function()
- local startAngle, endAngle = math.pi*3 + 0.01, math.pi + 0.01
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- almostFullCircle_startingRight = function()
- local startAngle, endAngle = math.pi*2, 0.1
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- almostFullCircle_startingLeft = function()
- local startAngle, endAngle = math.pi*3, math.pi + 0.1
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- almostFullCircle_startingJustBefore3 = function()
- local startAngle, endAngle = math.pi*2 - 0.01, 0.09
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- almostFullCircle_startingJustAfter3 = function()
- local startAngle, endAngle = math.pi*2 + 0.01, 0.11
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- almostFullCircle_startingJustBefore9 = function()
- local startAngle, endAngle = math.pi*3 - 0.01, math.pi + 0.09
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- almostFullCircle_startingJustAfter9 = function()
- local startAngle, endAngle = math.pi*3 + 0.01, math.pi + 0.11
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothTopRight = function()
- local startAngle, endAngle = 1.5, 1
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothTopLeft = function()
- local startAngle, endAngle = 3, 2
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothBottomLeft = function()
- local startAngle, endAngle = 4, 3.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothBottomRight = function()
- local startAngle, endAngle = 6, 5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopRight_endBottomRight = function()
- local startAngle, endAngle = 2*math.pi + 1.5, 6
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopRight_endBottomLeft = function()
- local startAngle, endAngle = 2*math.pi + 0.5, 4.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomRight_endBottomLeft = function()
- local startAngle, endAngle = 6, 4
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomRight_endTopLeft = function()
- local startAngle, endAngle = 5, 3
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomLeft_endTopLeft = function()
- local startAngle, endAngle = 4, 3
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomLeft_endTopRight = function()
- local startAngle, endAngle = 4, 1.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopLeft_endTopRight = function()
- local startAngle, endAngle = 2, 1
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopleft_endBottomRight = function()
- local startAngle, endAngle = 2*math.pi + 2, 6
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn3 = function()
- local startAngle, endAngle = 2*math.pi, 6
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn9 = function()
- local startAngle, endAngle = math.pi, 3
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- endOn3 = function()
- local startAngle, endAngle = 1, 0
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- endOn9 = function()
- local startAngle, endAngle = 4, math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn3_endOn9 = function()
- local startAngle, endAngle = 2*math.pi, math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn9_endOn3 = function()
- local startAngle, endAngle = math.pi, 0
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end
- },
- counterclockwise = {
- setup = function()
- err = "crosses3oclock %s (should be %s) and crosses9oclock %s (should be %s)"
- clockwise = false
- end,
- bothTopRight = function()
- local startAngle, endAngle = 0.5, 1
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothTopLeft = function()
- local startAngle, endAngle = 2, 2.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothBottomLeft = function()
- local startAngle, endAngle = 4, 4.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- bothBottomRight = function()
- local startAngle, endAngle = 5, 6
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopRight_endTopLeft = function()
- local startAngle, endAngle = 1.5, 2
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopRight_endBottomLeft = function()
- local startAngle, endAngle = 1.5, 3.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopLeft_endBottomLeft = function()
- local startAngle, endAngle = 3, 3.5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startTopLeft_endBottomRight = function()
- local startAngle, endAngle = 3, 5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomLeft_endBottomRight = function()
- local startAngle, endAngle = 4.5, 5
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomLeft_endTopRight = function()
- local startAngle, endAngle = 4.5, 0.5 + 2*math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomRight_endTopRight = function()
- local startAngle, endAngle = 6, 0.5 + 2*math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startBottomRight_endTopLeft = function()
- local startAngle, endAngle = 6, 2 + 2*math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn3 = function()
- local startAngle, endAngle = 0, 1
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn9 = function()
- local startAngle, endAngle = math.pi, 4
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- endOn3 = function()
- local startAngle, endAngle = 6, 2*math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- endOn9 = function()
- local startAngle, endAngle = 3, math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn3_endOn9 = function()
- local startAngle, endAngle = 0, math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- startOn9_endOn3 = function()
- local startAngle, endAngle = math.pi, 2*math.pi
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = false, false
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- specialFailureCase = function()
- local startAngle, endAngle = 5.8893166964432, 11.389442898301
- local result3, result9 = checkCrossings(startAngle, endAngle, clockwise)
- local expected3, expected9 = true, true
- assert(result3 == expected3 and result9 == expected9, string.format(err, tostring(result3), tostring(expected3), tostring(result9), tostring(expected9)))
- end,
- }
- }
- tests.splitArc = {
- g2 = {
- setup = function()
- command = "G2"
- end,
- same_Z_pass_through_3 = function()
- local nominalPos = {X = 0, Y = 10, Z = 0}
- local command, params = "G2", {X = 0, Y = 0, R = 5}
- local expected = {
- {command = "G2", params = {X = 5.000, Y = 5.000, R = 5.000}},
- {command = "G2", params = {X = 0.000, Y = 0.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_pass_through_9 = function()
- local nominalPos = {X = 0, Y = 0, Z = 0}
- local command, params = "G2", {X = 0, Y = 10, R = 5}
- local expected = {
- {command = "G2", params = {X = -5.000, Y = 5.000, R = 5.000}},
- {command = "G2", params = {X = 0.000, Y = 10.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_pass_through_neither = function()
- local nominalPos = {X = -1, Y = 0, Z = 0}
- local command, params = "G2", {X = 1, Y = 0, R = 5}
- local expected = {
- {command = "G2", params = {X = 1.000, Y = 0.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_almostFullCircle_startBefore3 = function()
- local nominalPos = {X = 5.231, Y = 11.846, Z = 0}
- local command, params = "G2", {X = 5.096, Y = 11.981, I = -0.231, J = -0.096}
- local expected = {
- {command = "G2", params = {X = 5.250, Y = 11.750, R = 0.250}},
- {command = "G2", params = {X = 4.750, Y = 11.750, R = 0.250}},
- {command = "G2", params = {X = 5.096, Y = 11.981, R = 0.250}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- different_Z_pass_through_3 = function()
- local nominalPos = {X = 0, Y = 10, Z = 0}
- local command, params = "G2", {X = 0, Y = 0, Z = -1, R = 5}
- local expected = {
- {command = "G2", params = {X = 5.000, Y = 5.000, Z = -0.500, R = 5.000}},
- {command = "G2", params = {X = 0.000, Y = 0.000, Z = -1.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- different_Z_pass_through_9 = function()
- local nominalPos = {X = 0, Y = 0, Z = 0}
- local command, params = "G2", {X = 0, Y = 10, Z = -4, R = 5}
- local expected = {
- {command = "G2", params = {X = -5.000, Y = 5.000, Z = -2.000, R = 5.000}},
- {command = "G2", params = {X = 0.000, Y = 10.000, Z = -4.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- different_Z_pass_through_neither = function()
- local nominalPos = {X = -1, Y = 0, Z = 0}
- local command, params = "G2", {X = 1, Y = 0, Z = -1, R = 5}
- local expected = {
- {command = "G2", params = {X = 1.000, Y = 0.000, Z = -1.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end
- },
- g3 = {
- same_Z_pass_through_3 = function()
- local nominalPos = {X = 0, Y = 0, Z = 0}
- local command, params = "G3", {X = 0, Y = 10, R = 5}
- local expected = {
- {command = "G3", params = {X = 5.000, Y = 5.000, R = 5.000}},
- {command = "G3", params = {X = 0.000, Y = 10.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_pass_through_9 = function()
- local nominalPos = {X = 0, Y = 10, Z = 0}
- local command, params = "G3", {X = 0, Y = 0, R = 5}
- local expected = {
- {command = "G3", params = {X = -5.000, Y = 5.000, R = 5.000}},
- {command = "G3", params = {X = 0.000, Y = 0.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_pass_through_neither = function()
- local nominalPos = {X = -1, Y = 0, Z = 0}
- local command, params = "G3", {X = 1, Y = 0, R = 5}
- local expected = {
- {command = "G3", params = {X = 1.000, Y = 0.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_fullCircle_startingAt3 = function()
- local nominalPos = {X = 0, Y = 0, Z = 0}
- local command, params = "G3", {X = 0, Y = 0, I = -1}
- local expected = {
- {command = "G3", params = {X = -2.000, Y = 0.000, R = 1.000}},
- {command = "G3", params = {X = 0.000, Y = 0.000, R = 1.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_fullCircle_startBefore3 = function()
- local nominalPos = {X = 3, Y = 4, Z = 0}
- local command, params = "G3", {X = 3, Y = 4, I = -3, J = -4}
- local expected = {
- {command = "G3", params = {X = -5.000, Y = 0.000, R = 5.000}},
- {command = "G3", params = {X = 5.000, Y = 0.000, R = 5.000}},
- {command = "G3", params = {X = 3.000, Y = 4.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- same_Z_almostFullCircle_startAfter3 = function()
- local nominalPos = {X = 5.231, Y = -11.846, Z = 0}
- local command, params = "G3", {X = 5.096, Y = -11.981, I = -0.231, J = 0.096}
- local expected = {
- {command = "G3", params = {X = 5.250, Y = -11.75, R = 0.250}},
- {command = "G3", params = {X = 4.750, Y = -11.75, R = 0.250}},
- {command = "G3", params = {X = 5.096, Y = -11.981, R = 0.250}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- different_Z_pass_through_3 = function()
- local nominalPos = {X = 0, Y = 0, Z = 0}
- local command, params = "G3", {X = 0, Y = 10, Z = -5, R = 5}
- local expected = {
- {command = "G3", params = {X = 5.000, Y = 5.000, Z = -2.500, R = 5.000}},
- {command = "G3", params = {X = 0.000, Y = 10.000, Z = -5.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- different_Z_pass_through_9 = function()
- local nominalPos = {X = 0, Y = 10, Z = 0}
- local command, params = "G3", {X = 0, Y = 0, Z = -3, R = 5}
- local expected = {
- {command = "G3", params = {X = -5.000, Y = 5.000, Z = -1.5000, R = 5.000}},
- {command = "G3", params = {X = 0.000, Y = 0.000, Z = -3.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end,
- different_Z_pass_through_neither = function()
- local nominalPos = {X = -1, Y = 0, Z = 0}
- local command, params = "G3", {X = 1, Y = 0, Z = -2, R = 5}
- local expected = {
- {command = "G3", params = {X = 1.000, Y = 0.000, Z = -2.000, R = 5.000}}
- }
- local expectedString = commandTableToString(expected)
- local result = splitArc(command, params, nominalPos)
- local resultString = commandTableToString(result)
- assert(
- #result == #expected and resultString == expectedString,
- string.format("\nExpected:\n%s\n\nActual:\n%s", expectedString, resultString)
- )
- end
- }
- }
- -- Helper function to convert a command table to a formatted string
- function commandTableToString(commandTable)
- local commandString = ""
- for _, cmd in ipairs(commandTable) do
- commandString = commandString .. formatGCode(cmd.command, cmd.params) .. "\n"
- end
- return commandString:sub(1, -2) -- Remove the last newline character
- end
- local function runTestsRecursively(tests, prefix)
- local testResults = {}
- local testCount = 0
- local passCount = 0
- for testName, testContent in pairs(tests) do
- if type(testContent) == "function" and testName ~= "setup" then
- -- Reset the bodies table before each test function
- bodies = {}
- if tests.setup then
- tests.setup() -- Call the setup function if present
- end
- testCount = testCount + 1
- dbg = false
- local status, err = pcall(testContent)
- if status then
- passCount = passCount + 1
- else
- table.insert(testResults, {testName = prefix .. "." .. testName, err = err})
- end
- elseif type(testContent) == "table" then
- local subResults, subCount, subPass = runTestsRecursively(testContent, prefix .. "." .. testName)
- testCount = testCount + subCount
- passCount = passCount + subPass
- for _, result in ipairs(subResults) do
- table.insert(testResults, result)
- end
- end
- end
- return testResults, testCount, passCount
- end
- function runAllTests()
- local testResults, testCount, passCount = runTestsRecursively(tests, "tests")
- print(string.format("%d tests run, %d tests passed, %d tests failed", testCount, passCount, testCount - passCount))
- for _, result in ipairs(testResults) do
- print(string.format("%s failed: %s", result.testName, result.err))
- end
- end
- runAllTests()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement