microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/language_service/src/rename/openqasm_tests.rs

776lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::{get_rename, prepare_rename};
5use crate::Encoding;
6use crate::test_utils::openqasm::compile_with_markers;
7
8/// Asserts that the rename locations given at the cursor position matches the expected rename locations.
9/// The cursor position is indicated by a `↘` marker in the source text.
10/// The expected rename location ranges are indicated by `◉` markers in the source text.
11fn check(source_with_markers: &str) {
12 let (compilation, cursor_position, target_spans) = compile_with_markers(source_with_markers);
13 let actual = get_rename(&compilation, "<source>", cursor_position, Encoding::Utf8)
14 .into_iter()
15 .map(|l| l.range)
16 .collect::<Vec<_>>();
17 for target in &target_spans {
18 assert!(actual.contains(target));
19 }
20 assert!(target_spans.len() == actual.len());
21}
22
23/// Asserts that the prepare rename given at the cursor position returns None.
24/// The cursor position is indicated by a `↘` marker in the source text.
25fn assert_no_rename(source_with_markers: &str) {
26 let (compilation, cursor_position, _) = compile_with_markers(source_with_markers);
27 let actual = prepare_rename(&compilation, "<source>", cursor_position, Encoding::Utf8);
28 assert!(actual.is_none());
29}
30
31#[test]
32fn callable_def() {
33 check(
34 r#"
35 def ◉Fo↘o◉(int x, int y, int z) {
36 ◉Foo◉(x, y, z);
37 }
38
39 def Bar(int x, int y, int z) {
40 ◉Foo◉(x, y, z);
41 }
42 "#,
43 );
44}
45
46#[test]
47fn callable_ref() {
48 check(
49 r#"
50 def ◉Foo◉(int x, int y, int z) {
51 ◉Foo◉(x, y, z);
52 }
53
54 def Bar(int x, int y, int z) {
55 ◉Fo↘o◉(x, y, z);
56 }
57 "#,
58 );
59}
60
61#[test]
62fn gate_def() {
63 check(
64 r#"
65 gate ◉Fo↘o◉(x, y, z) q { }
66
67 gate Bar(x, y, z) q {
68 ◉Foo◉(x, y, z) q;
69 }
70 "#,
71 );
72}
73
74#[test]
75fn gate_ref() {
76 check(
77 r#"
78 gate ◉Foo◉(x, y, z) q { }
79
80 gate Bar(x, y, z) q {
81 ◉Fo↘o◉(x, y, z) q;
82 }
83 "#,
84 );
85}
86
87#[test]
88fn parameter_def() {
89 check(
90 r#"
91 def Foo(int ◉↘x◉, int y, int z) {
92 int temp = ◉x◉;
93 Foo(◉x◉, y, z);
94 }
95 "#,
96 );
97}
98
99#[test]
100fn parameter_ref() {
101 check(
102 r#"
103 def Foo(int ◉x◉, int y, int z) {
104 int temp = ◉x◉;
105 Foo(◉↘x◉, y, z);
106 }
107 "#,
108 );
109}
110
111#[test]
112fn local_def_in_def() {
113 check(
114 r#"
115 int temp = x;
116 def Foo(int x, int y, int z) {
117 int ◉t↘emp◉ = x;
118 Foo(◉temp◉, y, ◉temp◉);
119 }
120 Foo(temp, y, temp);
121 "#,
122 );
123}
124
125#[test]
126fn local_ref_in_def() {
127 check(
128 r#"
129 int temp = x;
130 def Foo(int x, int y, int z) {
131 int ◉temp◉ = x;
132 Foo(◉t↘emp◉, y, ◉temp◉);
133 }
134 Foo(temp, y, temp);
135 "#,
136 );
137}
138
139#[test]
140fn local_def() {
141 check(
142 r#"
143 def Foo(int x, int y, int z) {
144 int temp = x;
145 Foo(temp, y, temp);
146 }
147 int ◉t↘emp◉ = x;
148 Foo(◉temp◉, y, ◉temp◉);
149 "#,
150 );
151}
152
153#[test]
154fn local_ref() {
155 check(
156 r#"
157 def Foo(int x, int y, int z) {
158 int temp = x;
159 Foo(temp, y, temp);
160 }
161 int ◉temp◉ = x;
162 Foo(◉t↘emp◉, y, ◉temp◉);
163 "#,
164 );
165}
166
167#[test]
168fn local_ref_cursor_touches_start() {
169 check(
170 r#"
171 def Foo(int x, int y, int z) {
172 int temp = x;
173 Foo(temp, y, temp);
174 }
175 int ◉temp◉ = x;
176 Foo(◉↘temp◉, y, ◉temp◉);
177 "#,
178 );
179}
180
181#[test]
182fn local_ref_cursor_touches_end() {
183 check(
184 r#"
185 def Foo(int x, int y, int z) {
186 int temp = x;
187 Foo(temp, y, temp);
188 }
189 int ◉temp◉ = x;
190 Foo(◉temp↘◉, y, ◉temp◉);
191 "#,
192 );
193}
194
195#[test]
196fn input_def() {
197 check(
198 r#"
199 def Foo(int x, int y, int z) {
200 int temp = x;
201 Foo(temp, y, temp);
202 }
203 input int ◉t↘emp◉;
204 Foo(◉temp◉, y, ◉temp◉);
205 "#,
206 );
207}
208
209#[test]
210fn input_ref() {
211 check(
212 r#"
213 def Foo(int x, int y, int z) {
214 int temp = x;
215 Foo(temp, y, temp);
216 }
217 input int ◉temp◉;
218 Foo(◉t↘emp◉, y, ◉temp◉);
219 "#,
220 );
221}
222
223#[test]
224fn output_def() {
225 check(
226 r#"
227 def Foo(int x, int y, int z) {
228 int temp = x;
229 Foo(temp, y, temp);
230 }
231 output int ◉t↘emp◉;
232 Foo(◉temp◉, y, ◉temp◉);
233 "#,
234 );
235}
236
237#[test]
238fn output_ref() {
239 check(
240 r#"
241 def Foo(int x, int y, int z) {
242 int temp = x;
243 Foo(temp, y, temp);
244 }
245 output int ◉temp◉;
246 Foo(◉t↘emp◉, y, ◉temp◉);
247 "#,
248 );
249}
250
251#[test]
252fn no_rename_openqasm_header() {
253 assert_no_rename(
254 r#"
255 OP↘ENQASM 3.0;
256 "#,
257 );
258}
259
260#[test]
261fn no_rename_keyword() {
262 assert_no_rename(
263 r#"
264 inc↘lude "stdgates.inc";
265 "#,
266 );
267}
268
269#[test]
270fn no_rename_type() {
271 assert_no_rename(
272 r#"
273 in↘t x;
274 "#,
275 );
276}
277
278#[test]
279fn no_rename_string_literal() {
280 assert_no_rename(
281 r#"
282 include "He↘llo World!";
283 "#,
284 );
285}
286
287#[test]
288fn rename_for_loop_iter_def() {
289 check(
290 r#"
291 def Foo(int x, int y, int z) {}
292 for int ◉i↘ndex◉ in [0:10] {
293 int temp = ◉index◉;
294 Foo(◉index◉, 0, 7 * ◉index◉ + 3);
295 }
296 "#,
297 );
298}
299
300#[test]
301fn rename_for_loop_iter_ref() {
302 check(
303 r#"
304 def Foo(int x, int y, int z) {}
305 for int ◉index◉ in [0:10] {
306 int temp = ◉↘index◉;
307 Foo(◉index◉, 0, 7 * ◉index◉ + 3);
308 }
309 "#,
310 );
311}
312
313#[test]
314fn no_rename_comment() {
315 assert_no_rename(
316 r#"
317 OPENQASM 3.0;
318 // He↘llo World!
319 include "stdgates.inc";
320 "#,
321 );
322}
323
324#[test]
325fn no_rename_std_item() {
326 assert_no_rename(
327 r#"
328 OPENQASM 3.0;
329 include "stdgates.inc";
330
331 // Built-in operation identifier shouldn't be renameable
332 qubit[1] q;
333 ↘x q[0];
334 "#,
335 );
336}
337
338#[test]
339fn no_rename_intrinsic_3_item() {
340 assert_no_rename(
341 r#"
342 OPENQASM 3.0;
343 // Built-in operation identifier shouldn't be renameable
344 qubit q;
345 ↘U(0.0, 0.0, 0.0) q;
346 "#,
347 );
348}
349
350#[test]
351fn no_rename_intrinsic_2_item() {
352 assert_no_rename(
353 r#"
354 OPENQASM 2.0;
355 // Built-in operation identifier shouldn't be renameable
356 qubit q;
357 ↘U(0.0, 0.0, 0.0) q;
358 "#,
359 );
360}
361
362#[test]
363fn no_rename_intrinsic_const() {
364 assert_no_rename(
365 r#"
366 float i = ↘pi * 7. / 8.;
367 "#,
368 );
369}
370
371#[test]
372fn no_rename_non_id_character() {
373 assert_no_rename(
374 r#"
375 // Non-identifier character '='
376 int x ↘= 0;
377 "#,
378 );
379}
380
381#[test]
382fn ty_param_def() {
383 check(
384 r#"
385 // Use a parameter identifier to model rename
386 def Foo(int ◉↘t◉) -> int { return ◉t◉; }
387 "#,
388 );
389}
390
391#[test]
392fn ty_param_ref() {
393 check(
394 r#"
395 def Foo(int ◉t◉) -> int { return ◉↘t◉; }
396 "#,
397 );
398}
399
400#[test]
401#[ignore = "index sets aren't yet supported"]
402fn alias_index_set_ref() {
403 check(
404 r#"
405 // classical decl
406 const int ◉s↘kip◉ = 2;
407
408 qubit[5] qreg0;
409 qubit[5] qreg1;
410 let my_reg = qreg0[{0 * ◉skip◉, ◉skip◉, ◉skip◉ * 2}] ++ qreg1[{◉skip◉ - 1, ◉skip◉ + ◉skip◉ / 2}];
411 "#,
412 );
413}
414
415#[test]
416fn alias_range_ref() {
417 check(
418 r#"
419 // classical decl
420 const int ◉s↘kip◉ = 2;
421
422 qubit[5] qreg0;
423 qubit[5] qreg1;
424 let my_reg = qreg0[◉skip◉-2:◉skip◉:2 * ◉skip◉ + 1] ++ qreg1[◉skip◉ - 1:◉skip◉:5];
425 "#,
426 );
427}
428
429#[test]
430fn box_designator_expr_ref() {
431 check(
432 r#"
433 // classical decl
434 const int ◉s↘ize◉ = 5;
435
436 const duration ad = 2ns;
437 box [◉size◉ * ad] {}
438 "#,
439 );
440}
441
442#[test]
443fn delay_designator_expr_ref() {
444 check(
445 r#"
446 // classical decl
447 const int ◉s↘ize◉ = 5;
448
449 const duration ad = 2ns;
450 delay [◉size◉ * ad] $0;
451 "#,
452 );
453}
454
455#[test]
456fn box_and_delay_designator_expr_ref() {
457 check(
458 r#"
459 // classical decl
460 const int ◉s↘ize◉ = 5;
461
462 const duration ad = 2ns;
463 box [◉size◉ * ad] {
464 delay [◉size◉ * ad] $0;
465 }
466 "#,
467 );
468}
469
470#[test]
471fn gphase_and_gate_call_designator_expr_ref() {
472 check(
473 r#"
474 include "stdgates.inc";
475
476 // classical decl
477 const int ◉s↘ize◉ = 5;
478
479 U(0.0, 0.0, 0.0) [◉size◉ * 2ns] $0;
480 gphase [◉size◉ * 2ns] $0;
481 "#,
482 );
483}
484
485#[test]
486fn sized_for_loop_iter_ty_param_decl_def() {
487 check(
488 r#"
489 // classical decl
490 const int ◉size◉ = 5;
491
492 // for stmt initializer var width
493 // redefine size so the inner scope should be a different var
494 for int[◉s↘ize◉] size in [◉size◉ - 0:◉size◉ * 2] {
495 for int[size] i in [size - 0:size * 2] {
496 // Do something with i
497 }
498 }
499 "#,
500 );
501}
502
503#[test]
504fn sized_quantum_register_def_length_param_def() {
505 check(
506 r#"
507 // classical decl
508 const int ◉s↘ize◉ = 5;
509
510 // quantum decl bitarray length
511 qubit[◉size◉] cdecl_qal;
512 "#,
513 );
514}
515
516#[test]
517fn sized_bit_register_def_length_param_def() {
518 check(
519 r#"
520 // classical decl
521 const int ◉s↘ize◉ = 5;
522
523 // classical decl bitarray length
524 bit[◉size◉] cdecl_bal = 3;
525 "#,
526 );
527}
528
529#[test]
530fn sized_classical_def_ty_param_def() {
531 check(
532 r#"
533 // classical decl
534 const int ◉s↘ize◉ = 5;
535
536 // classical decl int width
537 int[◉size◉] cdecl_iw = 3;
538
539 // classical decl uint width
540 uint[◉size◉] cdecl_uiw = 3;
541
542 // classical decl float width
543 float[◉size◉] cdecl_fw = 3.14;
544
545 // classical decl angle width
546 angle[◉size◉] cdecl_aw = pi;
547
548 // complex type width
549 complex[float[◉size◉]] w = 1.0 + 2.0im;
550
551 // const decl width
552 const float[◉size◉] ccdecl_cfw = 1.0 * ◉size◉;
553
554 // const complex type width
555 const complex[float[◉size◉]] ccw = 1.0 + 2.0im;
556 "#,
557 );
558}
559
560#[test]
561fn sized_io_scalar_ty_param_ref() {
562 check(
563 r#"
564 // classical decl
565 const int ◉s↘ize◉ = 5;
566
567 // input decl width
568 input float[◉size◉] ifw;
569 "#,
570 );
571}
572
573#[test]
574fn sized_old_style_length_param_ref() {
575 check(
576 r#"
577 // classical decl
578 const int ◉s↘ize◉ = 5;
579
580 // old style classical length
581 creg old_creg[◉size◉];
582
583 // old style quantum length
584 qreg old_qreg[◉size◉];
585 "#,
586 );
587}
588
589#[test]
590fn sized_cast_ty_param_ref() {
591 check(
592 r#"
593 // classical decl
594 const int ◉s↘ize◉ = 5;
595
596 // cast width
597 float[◉size◉] cast = float[◉size◉](4);
598 "#,
599 );
600}
601
602#[test]
603fn array_decls_ty_param_ref() {
604 check(
605 r#"
606 // classical decl
607 const int ◉s↘ize◉ = 5;
608
609 // classical array decl type width
610 array[int[◉size◉], 5] cadecl_itw;
611 "#,
612 );
613}
614
615#[test]
616fn array_decls_dims_param_ref() {
617 check(
618 r#"
619 // classical decl
620 const int ◉s↘ize◉ = 5;
621
622 // classical array dims
623 array[int, ◉size◉, 2 * ◉size◉] cadecl_itw_dims_sizes;
624 "#,
625 );
626}
627
628#[test]
629fn array_decls_ty_size_and_dims_param_ref() {
630 check(
631 r#"
632 // classical decl
633 const int ◉s↘ize◉ = 5;
634
635 // classical array dims
636 array[int[◉size◉], ◉size◉, 2 * ◉size◉] cadecl_itw_dims_sizes;
637 "#,
638 );
639}
640
641#[test]
642fn complex_array_decls_ty_param_and_dims_ref() {
643 check(
644 r#"
645 // classical decl
646 const int ◉s↘ize◉ = 5;
647
648 // complex array size and dims
649 array[complex[float[◉size◉ - 3]], ◉size◉, 2 * ◉size◉] cadecl_ctw_dims_sizes;
650 "#,
651 );
652}
653
654#[test]
655fn io_array_decls_ty_param_ref() {
656 check(
657 r#"
658 // classical decl
659 const int ◉s↘ize◉ = 5;
660
661 // classical array size and dims
662 input array[int[◉size◉], ◉size◉, 2 * ◉size◉] cadecl_iitw_dims_sizes;
663
664 // classical array size and dims
665 output array[int[◉size◉], ◉size◉, 2 * ◉size◉] cadecl_oitw_dims_sizes;
666
667 // complex array size and dims
668 input array[complex[float[◉size◉ - 3]], ◉size◉, 2 * ◉size◉] cadecl_ictw_dims_sizes;
669
670 // complex array size and dims
671 output array[complex[float[◉size◉ - 3]], ◉size◉, 2 * ◉size◉] cadecl_octw_dims_sizes;
672 "#,
673 );
674}
675
676#[test]
677fn def_ty_params_and_returns() {
678 check(
679 r#"
680 // classical decl
681 const int ◉s↘ize◉ = 5;
682
683 // return ty width
684 def sample_def_return(int t) -> int[◉size◉] { return t; }
685
686 // def param ty width
687 def sample_def_param(int[◉size◉] t) -> int { return t; }
688
689 // return ty width
690 def sample_def_complex_return(int c) -> complex[float[◉size◉]] { return c; }
691
692 // def param ty width
693 def sample_def_complex_param(complex[float[◉size◉]] c) -> complex { return c; }
694
695 // def param array ty width
696 def sample_def_array_param(readonly array[int[◉size◉], ◉size◉, 2 * ◉size◉] c) -> int { return 0; }
697
698 // def param array ty width
699 def sample_def_mut_array_param(mutable array[int[◉size◉], ◉size◉, 2 * ◉size◉] c) -> int { return 0; }
700 "#,
701 );
702}
703
704#[test]
705fn def_dyn_array_ty_params() {
706 check(
707 r#"
708 // classical decl
709 const int ◉s↘ize◉ = 5;
710
711 // def param dyn array ty width and dims
712 def sample_def_dyn_array_param(readonly array[int[◉size◉], dim = 1 * ◉size◉] c) -> int { return 0; }
713 "#,
714 );
715}
716
717#[test]
718fn extern_ty_params_and_returns() {
719 check(
720 r#"
721 // classical decl
722 const int ◉s↘ize◉ = 5;
723
724 // return ty width
725 extern sample_def_return(int) -> int[◉size◉];
726
727 // def param ty width
728 extern sample_def_param(int[◉size◉]) -> int;
729
730 // return complex ty width
731 extern sample_def_complex_return(int) -> complex[float[◉size◉]];
732
733 // def param complex ty width
734 extern sample_def_complex_param(complex[float[◉size◉]]) -> complex;
735
736 // extern def param array ty width
737 extern sample_extern_def_array_param(readonly array[int[◉size◉], ◉size◉, 2 * ◉size◉]) -> int;
738
739 // extern def param mut array ty width
740 extern sample_extern_def_mut_array_param(mutable array[int[◉size◉], ◉size◉, 2 * ◉size◉]) -> int;
741
742 // extern def param creg ty width
743 extern sample_extern_def_creg_param(creg[2 * ◉size◉]) -> int;
744 "#,
745 );
746}
747
748#[test]
749fn extern_dyn_array_ty_params_and_returns() {
750 check(
751 r#"
752 // classical decl
753 const int ◉s↘ize◉ = 5;
754
755 // extern def param mut array ty width
756 extern sample_extern_def_mut_dyn_array_param(readonly array[int[◉size◉], dim = 1 * ◉size◉]) -> int;
757 "#,
758 );
759}
760
761#[test]
762fn def_captures_ref_original_symbol_def() {
763 check(
764 r#"
765 // classical decl
766 const int ◉n↘Qubits◉ = 5;
767
768 def PrepareUniform(qubit[◉nQubits◉] q) -> bit[◉nQubits◉] {
769 bit[◉nQubits◉] results;
770 int ivar = ◉nQubits◉;
771 for int i in [0:◉nQubits◉-1] {
772 }
773 }
774 "#,
775 );
776}
777