File: test_interp_basics.cc

package info (click to toggle)
linuxcnc 1%3A2.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 285,604 kB
  • sloc: python: 202,568; ansic: 109,036; cpp: 99,239; tcl: 16,054; xml: 10,631; sh: 10,303; makefile: 1,255; javascript: 138; sql: 72; asm: 15
file content (581 lines) | stat: -rw-r--r-- 23,093 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "catch.hpp"

#include <interp_testing_util.hh> // For core interp stuff and extra REQUIRE macros/ setup
#include <rs274ngc_interp.hh>
#include <interp_inspection.hh>
#include <interp_return.hh>
#include <saicanon.hh>
#include <interp_parameter_def.hh>
using namespace interp_param_global;

TEST_CASE("Interp Basics")
{
  DECL_INIT_TEST_INTERP();
  SECTION("Interp Init")
  {
    REQUIRE(currentX(settings) == 0.0);
    REQUIRE(currentY(settings) == 0.0);
    REQUIRE(currentZ(settings) == 0.0);
  }

  SECTION("Change Units")
  {
    REQUIRE_INTERP_OK(test_interp.convert_length_units(G_21, settings));
    REQUIRE(settings->length_units == CANON_UNITS_MM);
    // Magically jump 1 inch
    currentX(settings) = 25.4;
    REQUIRE_INTERP_OK(test_interp.convert_length_units(G_20, settings));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    CHECK_FUZZ(currentX(settings), 1.0);
    REQUIRE_INTERP_OK(test_interp.convert_length_units(G_21, settings));
  }

  SECTION("G52 without rotation")
  {
    // Assume all offsets and such start at zero
    REQUIRE(settings->length_units == CANON_UNITS_MM);
    REQUIRE(settings->parameters[G92_X] == 0.0);
    REQUIRE(settings->parameters[G92_Y] == 0.0);
    REQUIRE_INTERP_OK(test_interp.execute("G52 X25.4 Y0"));
    CHECK_FUZZ(settings->parameters[G92_X], 1.0);
    CHECK_FUZZ(settings->parameters[G92_Y], 0.0);
    REQUIRE_INTERP_OK(test_interp.execute("G52 Y25.4"));
    CHECK_FUZZ(settings->parameters[G92_X], 1.0);
    CHECK_FUZZ(settings->parameters[G92_Y], 1.0);
  }

  SECTION("G92 X and G5x Rotation")
  {
    // Assume all offsets and such start at zero
    REQUIRE_INTERP_OK(test_interp.execute("G20"));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    REQUIRE_INTERP_OK(test_interp.execute("G92.1"));
    REQUIRE(settings->parameters[G92_X] == 0.0);
    REQUIRE_INTERP_OK(test_interp.execute("G52 X1 Y0"));
    REQUIRE_INTERP_OK(test_interp.execute("G10 L2 P0 X1 Y0 Z0 R0"));
    CHECK_FUZZ(currentX(settings), -2.0);
    // FIXME this is the wrong behavior but what the interpreter currently expects
    REQUIRE_INTERP_OK(test_interp.execute("G10 L2 P0 X1 Y0 Z0 R90"));
    CHECK_FUZZ(currentX(settings), -0.0);
    CHECK_FUZZ(currentY(settings), 2.0);
  }

  SECTION("G92 off-axis behavior with rotation")
  {
    // Assume all offsets and such start at zero
    REQUIRE_INTERP_OK(test_interp.execute("G20"));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    REQUIRE_INTERP_OK(test_interp.execute("G92.1"));
    REQUIRE_INTERP_OK(test_interp.execute("G52 X1 Y0"));
    REQUIRE_INTERP_OK(test_interp.execute("G10 L2 P0 X1 Y2 Z3 A4 B5 C6 R90"));
    // FIXME this is the wrong behavior but what the interpreter currently expects
    CHECK_FUZZ(currentX(settings), -2.0);
    CHECK_FUZZ(currentY(settings), 2.0);
    // Should not be affected by rotation
    CHECK_FUZZ(currentZ(settings), -3.0);
    CHECK_FUZZ(currentA(settings), -4.0);
    CHECK_FUZZ(currentB(settings), -5.0);
    CHECK_FUZZ(currentC(settings), -6.0);
  }

  SECTION("G55 without rotation")
  {
    REQUIRE_INTERP_OK(test_interp.execute("G20"));
    CHECK_FUZZ(currentX(settings), 0.0);
    CHECK_FUZZ(currentY(settings), 0.0);
    CHECK_FUZZ(currentZ(settings), 0.0);

    currentX(settings) = 1.0;
    currentY(settings) = 1.0;
    currentZ(settings) = 1.0;
    // KLUDGE hack in parameters directly to avoid depending on other functions
    test_interp._setup.parameters[G55_X] = 2;
    test_interp._setup.parameters[G55_Y] = 3;
    test_interp._setup.parameters[G55_Z] = 1;
    REQUIRE_INTERP_OK(test_interp.convert_coordinate_system(G_55, settings));
    CHECK_FUZZ(currentX(settings), -1.0);
    CHECK_FUZZ(currentY(settings), -2.0);
    CHECK_FUZZ(currentZ(settings), 0.0);
  }

  SECTION("G55 with rotation")
  {
    REQUIRE_INTERP_OK(test_interp.execute("G20"));
    currentX(settings) = 0.0;
    // KLUDGE hack in parameters directly to avoid depending on other functions
    test_interp._setup.parameters[G55_X] = 2;
    test_interp._setup.parameters[G55_Y] = 3;
    test_interp._setup.parameters[G55_Z] = 1;
    test_interp._setup.parameters[G55_R] = 90;
    REQUIRE_INTERP_OK(test_interp.convert_coordinate_system(G_55, settings));
    CHECK_FUZZ(currentX(settings), -3.0);
    CHECK_FUZZ(currentY(settings), 2.0);
    CHECK_FUZZ(currentZ(settings), -1.0);
  }
}

TEST_CASE("G10 init")
{
  DECL_INIT_TEST_INTERP();

  const char *test_setup[] = {
    "g20",
    "g10 l2 p1 x0 y0 z0",
    "g54",
    "g10 l1 p1 x0 y0 z0",
    "t1 m6 g43",
  };
  execute_lines(test_interp, test_setup);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], 0.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], 0.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);

  SECTION("G10 L1 direct offsets")
  {
    // 2-pass loop ensures that tool offsets persist regardless of G43 / G49 state
    for (int k = 0; k < 2; ++k) {
      REQUIRE_INTERP_OK(test_interp.execute("g10 l1 p1 x0 y0 z0 "));
      REQUIRE_INTERP_OK(test_interp.execute("g10 l1 p1 x1"));
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], 1.0);
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], 0.0);
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);

      REQUIRE_INTERP_OK(test_interp.execute("g10 l1 p1 y2"));
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], 1.0);
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], 2.0);
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);

      REQUIRE_INTERP_OK(test_interp.execute("g10 l1 p1 z3"));
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], 1.0);
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], 2.0);
      CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 3.0);

      REQUIRE_INTERP_OK(test_interp.execute("g49"));
    }
  }
}

TEST_CASE("G10 L10 ")
{
  DECL_INIT_TEST_INTERP();

  SECTION("tool offsets relative to position no rotation")
  {
  const char *test_setup[] = {
      "g20",
      "g10 l2 p1 x0 y0 z0",
      "g54",
      "g10 l1 p1 x0 y0 z0",
      "t1 m6 g43",
  };
  REQUIRE_INTERP_OK(execute_lines(test_interp, test_setup));

  REQUIRE_INTERP_OK(test_interp.execute("g0 x.1 y.2 z.3"));
  REQUIRE_INTERP_OK(test_interp.execute("g10 l10 p1 x1"));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], -0.9);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], 0.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);
  REQUIRE_INTERP_OK(test_interp.execute("g10 l10 p1 y2"));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], -0.9);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], -1.8);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);

  REQUIRE_INTERP_OK(test_interp.execute("g10 l10 p1 z3"));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], -0.9);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], -1.8);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], -2.7);
}

SECTION("G10 L10 tool offsets relative to position + 45 deg rotation")
{

  const char *test_setup[] = {
      "g20",
      "g10 l2 p1 x0 y0 z0 r45",
      "g54",
      "g10 l1 p1 x0 y0 z0",
      "t1 m6 g43",
  };
  REQUIRE(execute_lines(test_interp, test_setup));

  REQUIRE_INTERP_OK(test_interp.execute("g0 x0 y0 z0"));
  REQUIRE_INTERP_OK(test_interp.execute("g10 l10 p1 x1"));
  REQUIRE_INTERP_OK(test_interp.execute("g43"));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], -sqrt(2.0)/2.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], -sqrt(2.0)/2.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);
  CHECK_FUZZ(currentX(settings), 1.0);
  CHECK_FUZZ(currentY(settings), 0.0);

  REQUIRE_INTERP_OK(test_interp.execute("g10 l10 p1 y1"));
  REQUIRE_INTERP_OK(test_interp.execute("g43"));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], 0.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], -sqrt(2));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);
  CHECK_FUZZ(currentX(settings), 1.0);
  CHECK_FUZZ(currentY(settings), 1.0);
  REQUIRE_INTERP_OK(test_interp.execute("g49"));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_X], 0.0);
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Y], -sqrt(2));
  CHECK_FUZZ(settings->parameters[TOOL_OFFSET_Z], 0.0);
  CHECK_FUZZ(currentX(settings), 0.0);
  CHECK_FUZZ(currentY(settings), 0.0);
}
}

SCENARIO("Call G10 with G92 active (based on runtest g10-with-g92)")
{
  GIVEN("No G92 offsets applied")
  {
    DECL_INIT_TEST_INTERP();
    REQUIRE_INTERP_OK(test_interp.execute("g20"));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    WHEN("No offsets are applied") {
      THEN("Both current position and current offset should be zero") {
        CHECK_FUZZ(currentX(settings), 0.0);
        CHECK_FUZZ(currentY(settings), 0.0);
        CHECK_FUZZ(currentZ(settings), 0.0);
        CHECK_FUZZ(currentWorkOffsetX(settings), 0);
        CHECK_FUZZ(currentWorkOffsetY(settings), 0);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 0);
        CHECK_FUZZ(currentWorkOffsetA(settings), 0);
        CHECK_FUZZ(currentWorkOffsetB(settings), 0);
        CHECK_FUZZ(currentWorkOffsetC(settings), 0);
      }
    }
    WHEN("Work offsets are specified via G10 L2 for the active coordinate system")
    {
      REQUIRE_INTERP_OK(test_interp.execute("g10 l2 p0 x7 y8 z9 a10 b11 c12"));
      THEN ("New offset values are stored and immediately applied")
      {
        CHECK_FUZZ(settings->origin_index, 1); // Confirm it's G54
        CHECK_FUZZ(settings->parameters[G54_X], 7);
        CHECK_FUZZ(settings->parameters[G54_Y], 8);
        CHECK_FUZZ(settings->parameters[G54_Z], 9);
        CHECK_FUZZ(settings->parameters[G54_A], 10);
        CHECK_FUZZ(settings->parameters[G54_B], 11);
        CHECK_FUZZ(settings->parameters[G54_C], 12);
        CHECK_FUZZ(currentWorkOffsetX(settings), 7);
        CHECK_FUZZ(currentWorkOffsetY(settings), 8);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 9);
        CHECK_FUZZ(currentWorkOffsetA(settings), 10);
        CHECK_FUZZ(currentWorkOffsetB(settings), 11);
        CHECK_FUZZ(currentWorkOffsetC(settings), 12);

        CHECK_FUZZ(currentX(settings), -7);
        CHECK_FUZZ(currentY(settings), -8);
        CHECK_FUZZ(currentZ(settings), -9);
        CHECK_FUZZ(currentA(settings), -10);
        CHECK_FUZZ(currentB(settings), -11);
        CHECK_FUZZ(currentC(settings), -12);
      }
    }
    WHEN("Work offsets are specified via G10 L2 for inactive coordinate systems")
    {
      REQUIRE_INTERP_OK(test_interp.execute("G54"));
      REQUIRE_INTERP_OK(test_interp.execute("g10 l2 p2 x7 y8 z9 a10 b11 c12"));
      CHECK_FUZZ(settings->origin_index, 1); // Confirm it's G54
      THEN("Work offset parameters are updated but the current offset is not (nor is the current position)") {
        CHECK_FUZZ(settings->parameters[G55_X], 7);
        CHECK_FUZZ(settings->parameters[G55_Y], 8);
        CHECK_FUZZ(settings->parameters[G55_Z], 9);
        CHECK_FUZZ(settings->parameters[G55_A], 10);
        CHECK_FUZZ(settings->parameters[G55_B], 11);
        CHECK_FUZZ(settings->parameters[G55_C], 12);

        CHECK_FUZZ(currentWorkOffsetX(settings), 0);
        CHECK_FUZZ(currentWorkOffsetY(settings), 0);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 0);
        CHECK_FUZZ(currentWorkOffsetA(settings), 0);
        CHECK_FUZZ(currentWorkOffsetB(settings), 0);
        CHECK_FUZZ(currentWorkOffsetC(settings), 0);

        CHECK_FUZZ(currentX(settings), 0);
        CHECK_FUZZ(currentY(settings), 0);
        CHECK_FUZZ(currentZ(settings), 0);
        CHECK_FUZZ(currentA(settings), 0);
        CHECK_FUZZ(currentB(settings), 0);
        CHECK_FUZZ(currentC(settings), 0);
      }
    }
    WHEN("Work offsets are specified via G10 L2 for the active coordinate system")
    {
      REQUIRE_INTERP_OK(test_interp.execute("g10 l2 p0 x7 y8 z9 a10 b11 c12"));
      THEN ("New offset values are stored and immediately applied")
      {
        CHECK_FUZZ(settings->origin_index, 1); // Confirm it's G54
        CHECK_FUZZ(settings->parameters[G54_X], 7);
        CHECK_FUZZ(settings->parameters[G54_Y], 8);
        CHECK_FUZZ(settings->parameters[G54_Z], 9);
        CHECK_FUZZ(settings->parameters[G54_A], 10);
        CHECK_FUZZ(settings->parameters[G54_B], 11);
        CHECK_FUZZ(settings->parameters[G54_C], 12);

        CHECK_FUZZ(currentWorkOffsetX(settings), 7);
        CHECK_FUZZ(currentWorkOffsetY(settings), 8);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 9);
        CHECK_FUZZ(currentWorkOffsetA(settings), 10);
        CHECK_FUZZ(currentWorkOffsetB(settings), 11);
        CHECK_FUZZ(currentWorkOffsetC(settings), 12);

        CHECK_FUZZ(currentX(settings), -7);
        CHECK_FUZZ(currentY(settings), -8);
        CHECK_FUZZ(currentZ(settings), -9);
        CHECK_FUZZ(currentA(settings), -10);
        CHECK_FUZZ(currentB(settings), -11);
        CHECK_FUZZ(currentC(settings), -12);
      }
    }
  }
  GIVEN("G92 offsets active")
  {
    DECL_INIT_TEST_INTERP();
    REQUIRE_INTERP_OK(test_interp.execute("g20"));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    REQUIRE_INTERP_OK(test_interp.execute("g92 x3 y4 z5"));
    constexpr double axis_offset_x = -3;
    constexpr double axis_offset_y = -4;

    WHEN("No work offsets are applied") {
      THEN("Both current position and current offset should only be due to G92") {
        CHECK_FUZZ(currentX(settings), -axis_offset_x);
        CHECK_FUZZ(currentY(settings), -axis_offset_y);
        CHECK_FUZZ(currentZ(settings), 5);

        CHECK_FUZZ(currentWorkOffsetX(settings), 0);
        CHECK_FUZZ(currentWorkOffsetY(settings), 0);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 0);
        CHECK_FUZZ(currentWorkOffsetA(settings), 0);
        CHECK_FUZZ(currentWorkOffsetB(settings), 0);
        CHECK_FUZZ(currentWorkOffsetC(settings), 0);
      }
    }
    WHEN("Work offsets are specified via G10 L2 for the active coordinate system with rotation")
    {
      constexpr double work_offset_x = 7;
      constexpr double work_offset_y = 8;

      REQUIRE_INTERP_OK(test_interp.execute("g10 l2 p0 x7 y8 z9 a10 b11 c12 R45"));
      THEN ("Current position will be rotated in X/Y")
      {
        CHECK_FUZZ(settings->origin_index, 1); // Confirm it's G54
        CHECK_FUZZ(settings->parameters[G54_X], work_offset_x);
        CHECK_FUZZ(settings->parameters[G54_Y], work_offset_y);
        CHECK_FUZZ(settings->parameters[G54_Z], 9);
        CHECK_FUZZ(settings->parameters[G54_A], 10);
        CHECK_FUZZ(settings->parameters[G54_B], 11);
        CHECK_FUZZ(settings->parameters[G54_C], 12);

        CHECK_FUZZ(currentWorkOffsetX(settings), work_offset_x);
        CHECK_FUZZ(currentWorkOffsetY(settings), work_offset_y);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 9);
        CHECK_FUZZ(currentWorkOffsetA(settings), 10);
        CHECK_FUZZ(currentWorkOffsetB(settings), 11);
        CHECK_FUZZ(currentWorkOffsetC(settings), 12);

        constexpr double xy_rotation = M_PI / 4;
        // FIXME this math is wrong but what the current interpreter expects
        const double total_offset_x = axis_offset_x + work_offset_x;
        const double total_offset_y = axis_offset_y + work_offset_y;
        const double expect_x = (cos(-xy_rotation) * -total_offset_x + -sin(-xy_rotation) * -total_offset_y);
        const double expect_y = (sin(-xy_rotation) * -total_offset_x + cos(-xy_rotation) * -total_offset_y);
        CHECK_FUZZ(currentX(settings), expect_x);
        CHECK_FUZZ(currentY(settings), expect_y);
        CHECK_FUZZ(currentZ(settings), -4);
        CHECK_FUZZ(currentA(settings), -10);
        CHECK_FUZZ(currentB(settings), -11);
        CHECK_FUZZ(currentC(settings), -12);
      }
    }
  }
}

SCENARIO("Save / restore of G92 parameters")
{
  GIVEN("Active G92 Offsets")
  {
    DECL_INIT_TEST_INTERP();
    REQUIRE_INTERP_OK(test_interp.execute("g20"));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    REQUIRE_INTERP_OK(test_interp.execute("g92 x3 y4 z5"));
    constexpr double axis_offset_x = -3;
    constexpr double axis_offset_y = -4;
    constexpr double axis_offset_z = -5;
    CHECK_FUZZ(settings->parameters[G92_X], axis_offset_x);
    CHECK_FUZZ(settings->parameters[G92_Y], axis_offset_y);
    CHECK_FUZZ(settings->parameters[G92_Z], axis_offset_z);

    WHEN("Offsets are disabled with G92.2") {
      REQUIRE_INTERP_OK(test_interp.execute("g92.2"));
      THEN("Current position changes but internal offset parameters are unaffected") {
        CHECK_FUZZ(currentX(settings), 0);
        CHECK_FUZZ(currentY(settings), 0);
        CHECK_FUZZ(currentZ(settings), 0);

        CHECK_FUZZ(settings->parameters[G92_X], axis_offset_x);
        CHECK_FUZZ(settings->parameters[G92_Y], axis_offset_y);
        CHECK_FUZZ(settings->parameters[G92_Z], axis_offset_z);

        WHEN("Offsets are re-enabled with G92.3") {
          REQUIRE_INTERP_OK(test_interp.execute("g92.3"));
          THEN("Original position and offsets are restored") {
            CHECK_FUZZ(currentX(settings), -axis_offset_x);
            CHECK_FUZZ(currentY(settings), -axis_offset_y);
            CHECK_FUZZ(currentZ(settings), -axis_offset_z);

            CHECK_FUZZ(settings->parameters[G92_X], axis_offset_x);
            CHECK_FUZZ(settings->parameters[G92_Y], axis_offset_y);
            CHECK_FUZZ(settings->parameters[G92_Z], axis_offset_z);
          }
        }
      }
    }
  }
}


SCENARIO("Convert G20 / G21 ")
{
  GIVEN("Starting in G20")
  {
    DECL_INIT_TEST_INTERP();
    REQUIRE_INTERP_OK(test_interp.execute("g20"));
    REQUIRE_INTERP_OK(test_interp.execute("g0 x1 y2 z3 a4 b5 c6"));

    CHECK_FUZZ(currentX(settings), 1);
    CHECK_FUZZ(currentY(settings), 2);
    CHECK_FUZZ(currentZ(settings), 3);
    CHECK_FUZZ(currentA(settings), 4);
    CHECK_FUZZ(currentB(settings), 5);
    CHECK_FUZZ(currentC(settings), 6);
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    WHEN("Convert current position to mm") {
      REQUIRE_INTERP_OK(test_interp.execute("g21"));
      REQUIRE(settings->length_units == CANON_UNITS_MM);
      THEN("urrent position should be in new program units") {
        CHECK_FUZZ(currentX(settings), 1 * 25.4);
        CHECK_FUZZ(currentY(settings), 2 * 25.4);
        CHECK_FUZZ(currentZ(settings), 3 * 25.4);
        CHECK_FUZZ(currentA(settings), 4);
        CHECK_FUZZ(currentB(settings), 5);
        CHECK_FUZZ(currentC(settings), 6);
      }
      WHEN("Convert to back to inches") {
        REQUIRE_INTERP_OK(test_interp.execute("g20"));
        REQUIRE(settings->length_units == CANON_UNITS_INCHES);
        THEN("current position returns to inch value") {
          CHECK_FUZZ(currentX(settings), 1);
          CHECK_FUZZ(currentY(settings), 2);
          CHECK_FUZZ(currentZ(settings), 3);
          CHECK_FUZZ(currentA(settings), 4);
          CHECK_FUZZ(currentB(settings), 5);
          CHECK_FUZZ(currentC(settings), 6);
        }
      }
    }

    WHEN("Switch to G21") {
      REQUIRE_INTERP_OK(test_interp.execute("g10 L2 P1 X1 Y2 Z3 A4 B5 C6"));
      REQUIRE_INTERP_OK(test_interp.execute("g54"));
      REQUIRE_INTERP_OK(test_interp.execute("g0 x0.5 y0.5"));
      REQUIRE_INTERP_OK(test_interp.execute("g21"));
      REQUIRE(settings->length_units == CANON_UNITS_MM);
      THEN("Current work offset in mm") {
        CHECK_FUZZ(currentWorkOffsetX(settings), 1 * 25.4);
        CHECK_FUZZ(currentWorkOffsetY(settings), 2 * 25.4);
        CHECK_FUZZ(currentWorkOffsetZ(settings), 3 * 25.4);
        CHECK_FUZZ(currentWorkOffsetA(settings), 4);
        CHECK_FUZZ(currentWorkOffsetB(settings), 5);
        CHECK_FUZZ(currentWorkOffsetC(settings), 6);
      }
      WHEN("Switch to back to G20") {
        REQUIRE_INTERP_OK(test_interp.execute("g20"));
        REQUIRE(settings->length_units == CANON_UNITS_INCHES);
        THEN("current work offset returns to inches") {
          CHECK_FUZZ(currentWorkOffsetX(settings), 1);
          CHECK_FUZZ(currentWorkOffsetY(settings), 2);
          CHECK_FUZZ(currentWorkOffsetZ(settings), 3);
          CHECK_FUZZ(currentWorkOffsetA(settings), 4);
          CHECK_FUZZ(currentWorkOffsetB(settings), 5);
          CHECK_FUZZ(currentWorkOffsetC(settings), 6);
        }
      }
    }
  }

  GIVEN("Starting in with an axis offset")
  {
    DECL_INIT_TEST_INTERP();
    REQUIRE_INTERP_OK(test_interp.execute("g20"));
    REQUIRE_INTERP_OK(test_interp.execute("g92 x1 y2 z3 a4 b5 c6"));

    THEN("Initial axis offsets should be in inches") {
      CHECK_FUZZ(currentAxisOffsetX(settings), -1);
      CHECK_FUZZ(currentAxisOffsetY(settings), -2);
      CHECK_FUZZ(currentAxisOffsetZ(settings), -3);
      CHECK_FUZZ(currentAxisOffsetA(settings), -4);
      CHECK_FUZZ(currentAxisOffsetB(settings), -5);
      CHECK_FUZZ(currentAxisOffsetC(settings), -6);
    }
    WHEN("Switch to G21") {
      REQUIRE_INTERP_OK(test_interp.execute("g21"));
      REQUIRE(settings->length_units == CANON_UNITS_MM);
      THEN("Axis offsets in mm") {
        CHECK_FUZZ(currentAxisOffsetX(settings), -1 * 25.4);
        CHECK_FUZZ(currentAxisOffsetY(settings), -2 * 25.4);
        CHECK_FUZZ(currentAxisOffsetZ(settings), -3 * 25.4);
        CHECK_FUZZ(currentAxisOffsetA(settings), -4);
        CHECK_FUZZ(currentAxisOffsetB(settings), -5);
        CHECK_FUZZ(currentAxisOffsetC(settings), -6);
      }
      WHEN("Switch to back to G20") {
        REQUIRE_INTERP_OK(test_interp.execute("g20"));
        REQUIRE(settings->length_units == CANON_UNITS_INCHES);
        THEN("Axis offsets in inches again") {
          CHECK_FUZZ(currentAxisOffsetX(settings), -1);
          CHECK_FUZZ(currentAxisOffsetY(settings), -2);
          CHECK_FUZZ(currentAxisOffsetZ(settings), -3);
          CHECK_FUZZ(currentAxisOffsetA(settings), -4);
          CHECK_FUZZ(currentAxisOffsetB(settings), -5);
          CHECK_FUZZ(currentAxisOffsetC(settings), -6);
        }
      }
    }
  }
}

SCENARIO("Applying a work offset while active")
{
  GIVEN("A 45 deg rotated coordinate system with no offsets")
  {
    DECL_INIT_TEST_INTERP();
    REQUIRE_INTERP_OK(test_interp.execute("g20"));
    REQUIRE(settings->length_units == CANON_UNITS_INCHES);
    REQUIRE_INTERP_OK(test_interp.execute("g54"));
    REQUIRE_INTERP_OK(test_interp.execute("g10 l2 p1 x0 y0 z0 r45"));
    REQUIRE_INTERP_OK(test_interp.execute("g0 g53 x0 y0 z0"));
    REQUIRE(currentX(settings) == 0.0);
    REQUIRE(currentY(settings) == 0.0);
    REQUIRE(currentZ(settings) == 0.0);

    WHEN("Request L20 for an X offset") {
      REQUIRE_INTERP_OK(test_interp.execute("g10 l20 p1 x-1"));
      THEN("Actual work offsets are rotated") {
          CHECK_FUZZ(currentWorkOffsetX(settings), sqrt(2)/2);
          CHECK_FUZZ(currentWorkOffsetY(settings), sqrt(2)/2);
          CHECK_FUZZ(currentWorkOffsetZ(settings), 0);
      }

      WHEN("Move to X0 and request a Y offset") {
        REQUIRE_INTERP_OK(test_interp.execute("g0 x0"));
        REQUIRE_INTERP_OK(test_interp.execute("g10 l20 p1 y-1"));
        THEN("Actual work offsets are rotated") {
          CHECK_FUZZ(currentWorkOffsetX(settings), 0.0);
          CHECK_FUZZ(currentWorkOffsetY(settings), sqrt(2));
          CHECK_FUZZ(currentWorkOffsetZ(settings), 0);
        }
      }
    }
  }
}