microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc_passes/src/baseprofck/tests.rs
175lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #![allow(clippy::needless_raw_string_hashes)] |
| 5 | |
| 6 | use expect_test::{expect, Expect}; |
| 7 | use indoc::indoc; |
| 8 | use qsc_frontend::compile::{self, compile, PackageStore, RuntimeCapabilityFlags, SourceMap}; |
| 9 | |
| 10 | use crate::baseprofck::check_base_profile_compliance; |
| 11 | |
| 12 | fn check(expr: &str, expect: &Expect) { |
| 13 | let mut store = PackageStore::new(compile::core()); |
| 14 | let std = store.insert(compile::std(&store, RuntimeCapabilityFlags::all())); |
| 15 | let sources = SourceMap::new([("test".into(), "".into())], Some(expr.into())); |
| 16 | let unit = compile(&store, &[std], sources, RuntimeCapabilityFlags::all()); |
| 17 | assert!(unit.errors.is_empty(), "{:?}", unit.errors); |
| 18 | |
| 19 | let errors = check_base_profile_compliance(&unit.package); |
| 20 | expect.assert_debug_eq(&errors); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn simple_program_is_valid() { |
| 25 | check( |
| 26 | indoc! {"{ |
| 27 | use q = Qubit(); |
| 28 | H(q); |
| 29 | M(q) |
| 30 | }"}, |
| 31 | &expect![[r#" |
| 32 | [] |
| 33 | "#]], |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn intrinsic_calls_with_supported_returns_are_valid() { |
| 39 | check( |
| 40 | indoc! {"{ |
| 41 | operation Foo() : Unit { |
| 42 | body intrinsic; |
| 43 | } |
| 44 | operation Bar() : Result { |
| 45 | body intrinsic; |
| 46 | } |
| 47 | Foo(); |
| 48 | Bar() |
| 49 | }"}, |
| 50 | &expect![[r#" |
| 51 | [] |
| 52 | "#]], |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn result_comparison_error() { |
| 58 | check( |
| 59 | indoc! {"{ |
| 60 | use q = Qubit(); |
| 61 | H(q); |
| 62 | if (M(q) == M(q)) { |
| 63 | X(q); |
| 64 | } |
| 65 | }"}, |
| 66 | &expect![[r#" |
| 67 | [ |
| 68 | ReturnNonResult( |
| 69 | Span { |
| 70 | lo: 0, |
| 71 | hi: 78, |
| 72 | }, |
| 73 | ), |
| 74 | ResultComparison( |
| 75 | Span { |
| 76 | lo: 41, |
| 77 | hi: 53, |
| 78 | }, |
| 79 | ), |
| 80 | ] |
| 81 | "#]], |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn result_literal_error() { |
| 87 | check( |
| 88 | indoc! {"(One, Zero)"}, |
| 89 | &expect![[r#" |
| 90 | [ |
| 91 | ResultLiteral( |
| 92 | Span { |
| 93 | lo: 1, |
| 94 | hi: 4, |
| 95 | }, |
| 96 | ), |
| 97 | ResultLiteral( |
| 98 | Span { |
| 99 | lo: 6, |
| 100 | hi: 10, |
| 101 | }, |
| 102 | ), |
| 103 | ] |
| 104 | "#]], |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn non_result_return_error() { |
| 110 | check( |
| 111 | indoc! {"{ |
| 112 | use q = Qubit(); |
| 113 | H(q); |
| 114 | M(q); |
| 115 | 3 + 1 |
| 116 | }"}, |
| 117 | &expect![[r#" |
| 118 | [ |
| 119 | ReturnNonResult( |
| 120 | Span { |
| 121 | lo: 0, |
| 122 | hi: 54, |
| 123 | }, |
| 124 | ), |
| 125 | ] |
| 126 | "#]], |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn unit_return_error() { |
| 132 | check( |
| 133 | indoc! {"{ |
| 134 | operation Foo() : Unit {} |
| 135 | Foo() |
| 136 | }"}, |
| 137 | &expect![[r#" |
| 138 | [ |
| 139 | ReturnNonResult( |
| 140 | Span { |
| 141 | lo: 0, |
| 142 | hi: 43, |
| 143 | }, |
| 144 | ), |
| 145 | ] |
| 146 | "#]], |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn unsupported_intrsinsic_error() { |
| 152 | check( |
| 153 | indoc! {"{ |
| 154 | operation Rand() : Int { |
| 155 | body intrinsic; |
| 156 | } |
| 157 | }"}, |
| 158 | &expect![[r#" |
| 159 | [ |
| 160 | ReturnNonResult( |
| 161 | Span { |
| 162 | lo: 0, |
| 163 | hi: 62, |
| 164 | }, |
| 165 | ), |
| 166 | UnsupportedIntrinsic( |
| 167 | Span { |
| 168 | lo: 16, |
| 169 | hi: 20, |
| 170 | }, |
| 171 | ), |
| 172 | ] |
| 173 | "#]], |
| 174 | ); |
| 175 | } |
| 176 | |