microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dmitryv/select-updated

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_passes/src/callable_limits/tests.rs

167lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![allow(clippy::needless_raw_string_hashes)]
5
6use expect_test::{expect, Expect};
7use indoc::indoc;
8use qsc_frontend::compile::{self, compile, PackageStore, RuntimeCapabilityFlags, SourceMap};
9use qsc_hir::visit::Visitor;
10
11use crate::callable_limits::CallableLimits;
12
13fn check(file: &str, expect: &Expect) {
14 let store = PackageStore::new(compile::core());
15 let sources = SourceMap::new([("test".into(), file.into())], None);
16 let unit = compile(&store, &[], sources, RuntimeCapabilityFlags::all());
17 assert!(unit.errors.is_empty(), "{:?}", unit.errors);
18
19 let mut call_limits = CallableLimits::default();
20 call_limits.visit_package(&unit.package);
21 let errors = call_limits.errors;
22 expect.assert_debug_eq(&errors);
23}
24
25#[test]
26fn funcs_cannot_use_conj() {
27 check(
28 indoc! {"
29 namespace Test {
30 function A() : Unit {
31 within {} apply {}
32 }
33 }
34 "},
35 &expect![[r#"
36 [
37 Conjugate(
38 Span {
39 lo: 51,
40 hi: 69,
41 },
42 ),
43 ]
44 "#]],
45 );
46}
47
48#[test]
49fn funcs_cannot_have_functors() {
50 check(
51 indoc! {"
52 namespace Test {
53 function A() : Unit is Adj {}
54 }
55 "},
56 &expect![[r#"
57 [
58 Functor(
59 Span {
60 lo: 30,
61 hi: 31,
62 },
63 ),
64 ]
65 "#]],
66 );
67}
68
69#[test]
70fn funcs_cannot_call_ops() {
71 check(
72 indoc! {"
73 namespace Test {
74 operation A() : Unit {}
75 function B() : Unit {
76 A();
77 }
78 }
79 "},
80 &expect![[r#"
81 [
82 OpCall(
83 Span {
84 lo: 79,
85 hi: 82,
86 },
87 ),
88 ]
89 "#]],
90 );
91}
92
93#[test]
94fn funcs_cannot_allocate_qubits() {
95 check(
96 indoc! {"
97 namespace Test {
98 function A() : Unit {
99 use q = Qubit();
100 }
101 }
102 "},
103 &expect![[r#"
104 [
105 QubitAlloc(
106 Span {
107 lo: 51,
108 hi: 67,
109 },
110 ),
111 ]
112 "#]],
113 );
114}
115
116#[test]
117fn funcs_cannot_use_repeat() {
118 check(
119 indoc! {"
120 namespace Test {
121 function A() : Unit {
122 repeat {} until true;
123 }
124 }
125 "},
126 &expect![[r#"
127 [
128 Repeat(
129 Span {
130 lo: 51,
131 hi: 71,
132 },
133 ),
134 ]
135 "#]],
136 );
137}
138
139#[test]
140fn funcs_cannot_have_specs() {
141 check(
142 indoc! {"
143 namespace Test {
144 function A() : Unit {
145 body ... {}
146 adjoint self;
147 }
148 }
149 "},
150 &expect![[r#"
151 [
152 Spec(
153 Span {
154 lo: 21,
155 hi: 90,
156 },
157 ),
158 Functor(
159 Span {
160 lo: 30,
161 hi: 31,
162 },
163 ),
164 ]
165 "#]],
166 );
167}
168