microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
minestarks/dev-container

Branches

Tags

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

Clone

HTTPS

Download ZIP

library/src/tests/arrays.rs

1016lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::test_expression;
5use num_bigint::BigInt;
6use qsc::interpret::Value;
7
8// Tests for Microsoft.Quantum.Arrays namespace
9
10#[test]
11fn check_all() {
12 test_expression(
13 "Microsoft.Quantum.Arrays.All(x -> x != 0, [1, 2, 3, 4, 5])",
14 &Value::Bool(true),
15 );
16 test_expression(
17 "Microsoft.Quantum.Arrays.All(x -> x != 0, [1, 2, 0, 4, 5])",
18 &Value::Bool(false),
19 );
20 test_expression(
21 "Microsoft.Quantum.Arrays.All(x -> x == One, [One, One, One])",
22 &Value::Bool(true),
23 );
24 test_expression(
25 "Microsoft.Quantum.Arrays.All(x -> x == One, [One, One, Zero])",
26 &Value::Bool(false),
27 );
28}
29
30#[test]
31fn check_any() {
32 test_expression(
33 "Microsoft.Quantum.Arrays.Any(x -> x % 2 == 0, [1, 3, 6, 7, 9])",
34 &Value::Bool(true),
35 );
36 test_expression(
37 "Microsoft.Quantum.Arrays.Any(x -> x % 2 == 0, [1, 3, 5, 7, 9])",
38 &Value::Bool(false),
39 );
40}
41
42#[test]
43fn check_chunks() {
44 test_expression(
45 "Microsoft.Quantum.Arrays.Chunks(1, [10, 11, 12, 13, 14, 15])",
46 &Value::Array(
47 vec![
48 Value::Array(vec![Value::Int(10)].into()),
49 Value::Array(vec![Value::Int(11)].into()),
50 Value::Array(vec![Value::Int(12)].into()),
51 Value::Array(vec![Value::Int(13)].into()),
52 Value::Array(vec![Value::Int(14)].into()),
53 Value::Array(vec![Value::Int(15)].into()),
54 ]
55 .into(),
56 ),
57 );
58 test_expression(
59 "{
60 let empty: Int[] = [];
61 Microsoft.Quantum.Arrays.Chunks(2, empty)
62 }",
63 &Value::Array(vec![].into()),
64 );
65 test_expression(
66 "Microsoft.Quantum.Arrays.Chunks(2, [10])",
67 &Value::Array(vec![Value::Array(vec![Value::Int(10)].into())].into()),
68 );
69 test_expression(
70 "Microsoft.Quantum.Arrays.Chunks(2, [10, 11, 12, 13, 14, 15])",
71 &Value::Array(
72 vec![
73 Value::Array(vec![Value::Int(10), Value::Int(11)].into()),
74 Value::Array(vec![Value::Int(12), Value::Int(13)].into()),
75 Value::Array(vec![Value::Int(14), Value::Int(15)].into()),
76 ]
77 .into(),
78 ),
79 );
80 test_expression(
81 "Microsoft.Quantum.Arrays.Chunks(3, [10, 11, 12, 13, 14, 15])",
82 &Value::Array(
83 vec![
84 Value::Array(vec![Value::Int(10), Value::Int(11), Value::Int(12)].into()),
85 Value::Array(vec![Value::Int(13), Value::Int(14), Value::Int(15)].into()),
86 ]
87 .into(),
88 ),
89 );
90 test_expression(
91 "Microsoft.Quantum.Arrays.Chunks(4, [10, 11, 12, 13, 14, 15])",
92 &Value::Array(
93 vec![
94 Value::Array(
95 vec![
96 Value::Int(10),
97 Value::Int(11),
98 Value::Int(12),
99 Value::Int(13),
100 ]
101 .into(),
102 ),
103 Value::Array(vec![Value::Int(14), Value::Int(15)].into()),
104 ]
105 .into(),
106 ),
107 );
108}
109
110#[test]
111fn check_circularly_shifted() {
112 test_expression(
113 "Microsoft.Quantum.Arrays.CircularlyShifted(0, [10, 11, 12])",
114 &Value::Array(vec![Value::Int(10), Value::Int(11), Value::Int(12)].into()),
115 );
116 test_expression(
117 "Microsoft.Quantum.Arrays.CircularlyShifted(1, [10, 11, 12])",
118 &Value::Array(vec![Value::Int(12), Value::Int(10), Value::Int(11)].into()),
119 );
120 test_expression(
121 "Microsoft.Quantum.Arrays.CircularlyShifted(-1, [10, 11, 12])",
122 &Value::Array(vec![Value::Int(11), Value::Int(12), Value::Int(10)].into()),
123 );
124 test_expression(
125 "Microsoft.Quantum.Arrays.CircularlyShifted(500, [10, 11, 12])",
126 &Value::Array(vec![Value::Int(11), Value::Int(12), Value::Int(10)].into()),
127 );
128 test_expression(
129 "Microsoft.Quantum.Arrays.CircularlyShifted(-500, [10, 11, 12])",
130 &Value::Array(vec![Value::Int(12), Value::Int(10), Value::Int(11)].into()),
131 );
132}
133
134#[test]
135fn check_column_at() {
136 test_expression(
137 "Microsoft.Quantum.Arrays.ColumnAt(0, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])",
138 &Value::Array(vec![Value::Int(1), Value::Int(4), Value::Int(7)].into()),
139 );
140 test_expression(
141 "Microsoft.Quantum.Arrays.ColumnAt(2, [[true, true, true], [false, false, false]])",
142 &Value::Array(vec![Value::Bool(true), Value::Bool(false)].into()),
143 );
144 test_expression(
145 "Microsoft.Quantum.Arrays.ColumnAt(1, [[One, One], [Zero, Zero], [Zero, One]])",
146 &Value::Array(vec![Value::RESULT_ONE, Value::RESULT_ZERO, Value::RESULT_ONE].into()),
147 );
148}
149
150#[test]
151fn check_count() {
152 test_expression(
153 "Microsoft.Quantum.Arrays.Count(x -> x % 2 != 0, [1, 3, 6, 7, 9])",
154 &Value::Int(4),
155 );
156 test_expression(
157 "Microsoft.Quantum.Arrays.Count(x -> x % 2 == 0, [1, 3, 6, 7, 9])",
158 &Value::Int(1),
159 );
160}
161
162#[test]
163fn check_diagnonal() {
164 test_expression(
165 "{
166 let empty: Int[][] = [];
167 Microsoft.Quantum.Arrays.Diagonal(empty)
168 }",
169 &Value::Array(vec![].into()),
170 );
171 test_expression(
172 "Microsoft.Quantum.Arrays.Diagonal([[1]])",
173 &Value::Array(vec![Value::Int(1)].into()),
174 );
175 test_expression(
176 "Microsoft.Quantum.Arrays.Diagonal([[1, 2, 3], [4, 5, 6], [7, 8, 9]])",
177 &Value::Array(vec![Value::Int(1), Value::Int(5), Value::Int(9)].into()),
178 );
179 test_expression(
180 "Microsoft.Quantum.Arrays.Diagonal([[1, 2, 3], [4, 5, 6]])",
181 &Value::Array(vec![Value::Int(1), Value::Int(5)].into()),
182 );
183 test_expression(
184 "Microsoft.Quantum.Arrays.Diagonal([[1, 2], [3, 4], [5, 6]])",
185 &Value::Array(vec![Value::Int(1), Value::Int(4)].into()),
186 );
187}
188
189#[test]
190fn check_draw_many() {
191 test_expression(
192 "{
193 use qubit = Qubit();
194 let results = Microsoft.Quantum.Arrays.DrawMany(q => {X(q); M(q)}, 3, qubit);
195 Reset(qubit);
196 results
197 }",
198 &Value::Array(vec![Value::RESULT_ONE, Value::RESULT_ZERO, Value::RESULT_ONE].into()),
199 );
200}
201
202#[test]
203fn check_excluding() {
204 test_expression(
205 "{
206 let empty: Int[] = [];
207 Microsoft.Quantum.Arrays.Excluding(empty, empty)
208 }",
209 &Value::Array(vec![].into()),
210 );
211 test_expression(
212 "Microsoft.Quantum.Arrays.Excluding([], [10, 11, 12, 13, 14, 15])",
213 &Value::Array(
214 vec![
215 Value::Int(10),
216 Value::Int(11),
217 Value::Int(12),
218 Value::Int(13),
219 Value::Int(14),
220 Value::Int(15),
221 ]
222 .into(),
223 ),
224 );
225 test_expression(
226 "Microsoft.Quantum.Arrays.Excluding([1, 3, 4], [10, 11, 12, 13, 14, 15])",
227 &Value::Array(vec![Value::Int(10), Value::Int(12), Value::Int(15)].into()),
228 );
229 test_expression(
230 "Microsoft.Quantum.Arrays.Excluding([3, 1, 4, 1], [10, 11, 12, 13, 14, 15])",
231 &Value::Array(vec![Value::Int(10), Value::Int(12), Value::Int(15)].into()),
232 );
233}
234
235#[test]
236fn check_enumerated() {
237 test_expression(
238 "Microsoft.Quantum.Arrays.Enumerated([false, true, false])",
239 &Value::Array(
240 vec![
241 Value::Tuple(vec![Value::Int(0), Value::Bool(false)].into(), None),
242 Value::Tuple(vec![Value::Int(1), Value::Bool(true)].into(), None),
243 Value::Tuple(vec![Value::Int(2), Value::Bool(false)].into(), None),
244 ]
245 .into(),
246 ),
247 );
248}
249
250#[test]
251fn check_filtered() {
252 test_expression(
253 "Microsoft.Quantum.Arrays.Filtered(x -> x % 2 == 0, [0, 1, 2, 3, 4])",
254 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
255 );
256 test_expression(
257 "Microsoft.Quantum.Arrays.Filtered(x -> x % 2 != 0, [1, 2, 3, 4, 5])",
258 &Value::Array(vec![Value::Int(1), Value::Int(3), Value::Int(5)].into()),
259 );
260}
261
262#[test]
263fn check_flat_mapped() {
264 test_expression(
265 "Microsoft.Quantum.Arrays.FlatMapped(x -> Repeated(x, 2), [1, 2, 3])",
266 &Value::Array(
267 vec![
268 Value::Int(1),
269 Value::Int(1),
270 Value::Int(2),
271 Value::Int(2),
272 Value::Int(3),
273 Value::Int(3),
274 ]
275 .into(),
276 ),
277 );
278}
279
280#[test]
281fn check_flattened() {
282 test_expression(
283 "Microsoft.Quantum.Arrays.Flattened([[1, 2], [3], [4, 5, 6]])",
284 &Value::Array(
285 vec![
286 Value::Int(1),
287 Value::Int(2),
288 Value::Int(3),
289 Value::Int(4),
290 Value::Int(5),
291 Value::Int(6),
292 ]
293 .into(),
294 ),
295 );
296}
297
298#[test]
299fn check_fold() {
300 test_expression(
301 "Microsoft.Quantum.Arrays.Fold((x, y) -> x + y, 0, [1, 2, 3, 4, 5])",
302 &Value::Int(15),
303 );
304 test_expression(
305 "Microsoft.Quantum.Arrays.Fold((x, y) -> x or y, false, [true, false, true])",
306 &Value::Bool(true),
307 );
308 test_expression(
309 "Microsoft.Quantum.Arrays.Fold((x, y) -> x and y, true, [true, false, true])",
310 &Value::Bool(false),
311 );
312}
313
314#[test]
315fn check_for_each() {
316 test_expression(
317 "{
318 use register = Qubit[3];
319 Microsoft.Quantum.Arrays.ForEach
320 (q => {X(q); Microsoft.Quantum.Measurement.MResetZ(q)},
321 register)
322 }",
323 &Value::Array(vec![Value::RESULT_ONE, Value::RESULT_ONE, Value::RESULT_ONE].into()),
324 );
325}
326
327#[test]
328fn check_head() {
329 test_expression("Microsoft.Quantum.Arrays.Head([5,6,7,8])", &Value::Int(5));
330}
331
332#[test]
333fn check_head_and_rest() {
334 test_expression(
335 "Microsoft.Quantum.Arrays.HeadAndRest([5,6,7,8])",
336 &Value::Tuple(
337 vec![
338 Value::Int(5),
339 Value::Array(vec![Value::Int(6), Value::Int(7), Value::Int(8)].into()),
340 ]
341 .into(),
342 None,
343 ),
344 );
345}
346
347#[test]
348fn check_index_of() {
349 test_expression(
350 "Microsoft.Quantum.Arrays.IndexOf(x -> x % 2 != 0, [10, 8, 6, 5, 4])",
351 &Value::Int(3),
352 );
353 test_expression(
354 "Microsoft.Quantum.Arrays.IndexOf(x -> x % 2 == 0, [1, 3, 4, 5, 7])",
355 &Value::Int(2),
356 );
357 test_expression(
358 "Microsoft.Quantum.Arrays.IndexOf(x -> x % 2 == 0, [1, 3, 5, 7, 9])",
359 &Value::Int(-1),
360 );
361}
362
363#[test]
364fn check_index_range() {
365 test_expression(
366 "Microsoft.Quantum.Arrays.IndexRange([7,6,5,4])::Start",
367 &Value::Int(0),
368 );
369 test_expression(
370 "Microsoft.Quantum.Arrays.IndexRange([7,6,5,4])::Step",
371 &Value::Int(1),
372 );
373 test_expression(
374 "Microsoft.Quantum.Arrays.IndexRange([7,6,5,4])::End",
375 &Value::Int(3),
376 );
377}
378
379#[test]
380fn check_interleaved() {
381 test_expression(
382 "Microsoft.Quantum.Arrays.Interleaved([1, 2, 3], [-1, -2, -3])",
383 &Value::Array(
384 vec![
385 Value::Int(1),
386 Value::Int(-1),
387 Value::Int(2),
388 Value::Int(-2),
389 Value::Int(3),
390 Value::Int(-3),
391 ]
392 .into(),
393 ),
394 );
395 test_expression(
396 "Microsoft.Quantum.Arrays.Interleaved([true, true], [false])",
397 &Value::Array(vec![Value::Bool(true), Value::Bool(false), Value::Bool(true)].into()),
398 );
399}
400
401#[test]
402fn check_is_empty() {
403 test_expression(
404 "{
405 let empty: Int[] = [];
406 Microsoft.Quantum.Arrays.IsEmpty(empty)
407 }",
408 &Value::Bool(true),
409 );
410 test_expression("Microsoft.Quantum.Arrays.IsEmpty([1])", &Value::Bool(false));
411 test_expression(
412 "Microsoft.Quantum.Arrays.IsEmpty([1, 2, 3, 4, 5])",
413 &Value::Bool(false),
414 );
415}
416
417#[test]
418fn check_is_rectangular_array() {
419 test_expression(
420 "{
421 let empty: Int[] = [];
422 Microsoft.Quantum.Arrays.IsRectangularArray([empty])
423 }",
424 &Value::Bool(true),
425 );
426 test_expression(
427 "Microsoft.Quantum.Arrays.IsRectangularArray([[1]])",
428 &Value::Bool(true),
429 );
430 test_expression(
431 "Microsoft.Quantum.Arrays.IsRectangularArray([[1, 2], [3, 4]])",
432 &Value::Bool(true),
433 );
434 test_expression(
435 "Microsoft.Quantum.Arrays.IsRectangularArray([[1, 2, 3], [4, 5, 6]])",
436 &Value::Bool(true),
437 );
438 test_expression(
439 "Microsoft.Quantum.Arrays.IsRectangularArray([[1, 2], [3, 4, 5]])",
440 &Value::Bool(false),
441 );
442}
443
444#[test]
445fn check_is_sorted() {
446 test_expression(
447 "{
448 let empty: Int[] = [];
449 Microsoft.Quantum.Arrays.IsSorted((x, y) -> x <= y, empty)
450 }",
451 &Value::Bool(true),
452 );
453 test_expression(
454 "Microsoft.Quantum.Arrays.IsSorted((x, y) -> x <= y, [1])",
455 &Value::Bool(true),
456 );
457 test_expression(
458 "Microsoft.Quantum.Arrays.IsSorted((x, y) -> x <= y, [1, 2, 3, 4, 5])",
459 &Value::Bool(true),
460 );
461 test_expression(
462 "Microsoft.Quantum.Arrays.IsSorted((x, y) -> x >= y, [5, 4, 3, 2, 1])",
463 &Value::Bool(true),
464 );
465 test_expression(
466 "Microsoft.Quantum.Arrays.IsSorted((x, y) -> x <= y, [1, 2, 3, 5, 4])",
467 &Value::Bool(false),
468 );
469 test_expression(
470 "Microsoft.Quantum.Arrays.IsSorted((x, y) -> x <= y, [5, 4, 3, 2, 1])",
471 &Value::Bool(false),
472 );
473}
474
475#[test]
476fn check_is_square_array() {
477 test_expression(
478 "{
479 let empty: Int[][] = [];
480 Microsoft.Quantum.Arrays.IsSquareArray(empty)
481 }",
482 &Value::Bool(true),
483 );
484 test_expression(
485 "Microsoft.Quantum.Arrays.IsSquareArray([[1]])",
486 &Value::Bool(true),
487 );
488 test_expression(
489 "Microsoft.Quantum.Arrays.IsSquareArray([[1, 2], [3, 4]])",
490 &Value::Bool(true),
491 );
492 test_expression(
493 "Microsoft.Quantum.Arrays.IsSquareArray([[1, 2, 3], [4, 5, 6]])",
494 &Value::Bool(false),
495 );
496 test_expression(
497 "Microsoft.Quantum.Arrays.IsSquareArray([[1, 2], [3, 4], [5, 6]])",
498 &Value::Bool(false),
499 );
500}
501
502#[test]
503fn check_mapped() {
504 test_expression(
505 "Microsoft.Quantum.Arrays.Mapped(i -> i * 2, [0, 1, 2])",
506 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
507 );
508}
509
510#[test]
511fn check_mapped_by_index() {
512 test_expression(
513 "Microsoft.Quantum.Arrays.MappedByIndex((index, element) -> index == element ,[0, -1, 2])",
514 &Value::Array(vec![Value::Bool(true), Value::Bool(false), Value::Bool(true)].into()),
515 );
516}
517
518#[test]
519fn check_mapped_over_range() {
520 test_expression(
521 "Microsoft.Quantum.Arrays.MappedOverRange(x -> x + 1, 0..2..10)",
522 &Value::Array(
523 vec![
524 Value::Int(1),
525 Value::Int(3),
526 Value::Int(5),
527 Value::Int(7),
528 Value::Int(9),
529 Value::Int(11),
530 ]
531 .into(),
532 ),
533 );
534 test_expression(
535 "Microsoft.Quantum.Arrays.MappedOverRange(x -> x * 2, 3..-1..1)",
536 &Value::Array(vec![Value::Int(6), Value::Int(4), Value::Int(2)].into()),
537 );
538}
539
540#[test]
541fn check_most() {
542 test_expression(
543 "Microsoft.Quantum.Arrays.Most([5, 6, 7, 8])",
544 &Value::Array(vec![Value::Int(5), Value::Int(6), Value::Int(7)].into()),
545 );
546}
547
548#[test]
549fn check_most_and_tail() {
550 test_expression(
551 "Microsoft.Quantum.Arrays.MostAndTail([5, 6, 7, 8])",
552 &Value::Tuple(
553 vec![
554 Value::Array(vec![Value::Int(5), Value::Int(6), Value::Int(7)].into()),
555 Value::Int(8),
556 ]
557 .into(),
558 None,
559 ),
560 );
561}
562
563#[test]
564fn check_padded() {
565 test_expression(
566 "Microsoft.Quantum.Arrays.Padded(-5, 2, [10, 11, 12])",
567 &Value::Array(
568 vec![
569 Value::Int(10),
570 Value::Int(11),
571 Value::Int(12),
572 Value::Int(2),
573 Value::Int(2),
574 ]
575 .into(),
576 ),
577 );
578 test_expression(
579 "Microsoft.Quantum.Arrays.Padded(5, 2, [10, 11, 12])",
580 &Value::Array(
581 vec![
582 Value::Int(2),
583 Value::Int(2),
584 Value::Int(10),
585 Value::Int(11),
586 Value::Int(12),
587 ]
588 .into(),
589 ),
590 );
591 test_expression(
592 "Microsoft.Quantum.Arrays.Padded(3, 2, [10, 11, 12])",
593 &Value::Array(vec![Value::Int(10), Value::Int(11), Value::Int(12)].into()),
594 );
595 test_expression(
596 "Microsoft.Quantum.Arrays.Padded(-3, 2, [10, 11, 12])",
597 &Value::Array(vec![Value::Int(10), Value::Int(11), Value::Int(12)].into()),
598 );
599}
600
601#[test]
602fn check_partitioned() {
603 test_expression(
604 "Microsoft.Quantum.Arrays.Partitioned([2, 1], [2, 3, 5, 7])",
605 &Value::Array(
606 vec![
607 Value::Array(vec![Value::Int(2), Value::Int(3)].into()),
608 Value::Array(vec![Value::Int(5)].into()),
609 Value::Array(vec![Value::Int(7)].into()),
610 ]
611 .into(),
612 ),
613 );
614 test_expression(
615 "Microsoft.Quantum.Arrays.Partitioned([2, 2], [2, 3, 5, 7])",
616 &Value::Array(
617 vec![
618 Value::Array(vec![Value::Int(2), Value::Int(3)].into()),
619 Value::Array(vec![Value::Int(5), Value::Int(7)].into()),
620 Value::Array(vec![].into()),
621 ]
622 .into(),
623 ),
624 );
625}
626
627#[test]
628fn check_sequence_i() {
629 test_expression(
630 "Microsoft.Quantum.Arrays.SequenceI(0, 3)",
631 &Value::Array(vec![Value::Int(0), Value::Int(1), Value::Int(2), Value::Int(3)].into()),
632 );
633 test_expression(
634 "Microsoft.Quantum.Arrays.SequenceI(-5, -2)",
635 &Value::Array(
636 vec![
637 Value::Int(-5),
638 Value::Int(-4),
639 Value::Int(-3),
640 Value::Int(-2),
641 ]
642 .into(),
643 ),
644 );
645}
646
647#[test]
648fn check_sequence_l() {
649 test_expression(
650 "Microsoft.Quantum.Arrays.SequenceL(0L, 3L)",
651 &Value::Array(
652 vec![
653 Value::BigInt(BigInt::from(0)),
654 Value::BigInt(BigInt::from(1)),
655 Value::BigInt(BigInt::from(2)),
656 Value::BigInt(BigInt::from(3)),
657 ]
658 .into(),
659 ),
660 );
661 test_expression(
662 "Microsoft.Quantum.Arrays.SequenceL(-5L, -2L)",
663 &Value::Array(
664 vec![
665 Value::BigInt(BigInt::from(-5)),
666 Value::BigInt(BigInt::from(-4)),
667 Value::BigInt(BigInt::from(-3)),
668 Value::BigInt(BigInt::from(-2)),
669 ]
670 .into(),
671 ),
672 );
673}
674
675#[test]
676fn check_sorted() {
677 test_expression(
678 "{
679 let empty: Int[] = [];
680 Microsoft.Quantum.Arrays.Sorted((x, y) -> x <= y, empty)
681 }",
682 &Value::Array(vec![].into()),
683 );
684 test_expression(
685 "Microsoft.Quantum.Arrays.Sorted((x, y) -> x <= y, [-1])",
686 &Value::Array(vec![Value::Int(-1)].into()),
687 );
688 test_expression(
689 "Microsoft.Quantum.Arrays.Sorted((x, y) -> x <= y, [1, 2, 0, 4, 3])",
690 &Value::Array(
691 vec![
692 Value::Int(0),
693 Value::Int(1),
694 Value::Int(2),
695 Value::Int(3),
696 Value::Int(4),
697 ]
698 .into(),
699 ),
700 );
701 test_expression(
702 "Microsoft.Quantum.Arrays.Sorted((x, y) -> x >= y, [1, 2, 0, 4, 3])",
703 &Value::Array(
704 vec![
705 Value::Int(4),
706 Value::Int(3),
707 Value::Int(2),
708 Value::Int(1),
709 Value::Int(0),
710 ]
711 .into(),
712 ),
713 );
714 test_expression(
715 "Microsoft.Quantum.Arrays.Sorted((x, y) -> x <= y, [-1, 2, 0, 1, -2])",
716 &Value::Array(
717 vec![
718 Value::Int(-2),
719 Value::Int(-1),
720 Value::Int(0),
721 Value::Int(1),
722 Value::Int(2),
723 ]
724 .into(),
725 ),
726 );
727}
728
729#[test]
730fn check_rest() {
731 test_expression(
732 "Microsoft.Quantum.Arrays.Rest([5,6,7,8])",
733 &Value::Array(vec![Value::Int(6), Value::Int(7), Value::Int(8)].into()),
734 );
735}
736
737#[test]
738fn check_reversed() {
739 test_expression(
740 "Microsoft.Quantum.Arrays.Reversed([5,6,7,8])",
741 &Value::Array(vec![Value::Int(8), Value::Int(7), Value::Int(6), Value::Int(5)].into()),
742 );
743}
744
745#[test]
746fn check_subarray() {
747 test_expression(
748 "Microsoft.Quantum.Arrays.Subarray([3, 0, 2, 1], [1, 2, 3, 4])",
749 &Value::Array(vec![Value::Int(4), Value::Int(1), Value::Int(3), Value::Int(2)].into()),
750 );
751 test_expression(
752 "Microsoft.Quantum.Arrays.Subarray([1, 2, 2], [1, 2, 3, 4])",
753 &Value::Array(vec![Value::Int(2), Value::Int(3), Value::Int(3)].into()),
754 );
755 test_expression(
756 "Microsoft.Quantum.Arrays.Subarray([0, 0, 0, 0, 0], [false])",
757 &Value::Array(
758 vec![
759 Value::Bool(false),
760 Value::Bool(false),
761 Value::Bool(false),
762 Value::Bool(false),
763 Value::Bool(false),
764 ]
765 .into(),
766 ),
767 );
768}
769
770#[test]
771fn check_swapped() {
772 test_expression(
773 "Microsoft.Quantum.Arrays.Swapped(1, 3, [0, 1, 2, 3, 4])",
774 &Value::Array(
775 vec![
776 Value::Int(0),
777 Value::Int(3),
778 Value::Int(2),
779 Value::Int(1),
780 Value::Int(4),
781 ]
782 .into(),
783 ),
784 );
785}
786
787#[test]
788fn check_tail() {
789 test_expression("Microsoft.Quantum.Arrays.Tail([5,6,7,8])", &Value::Int(8));
790}
791
792#[test]
793fn check_transposed() {
794 test_expression(
795 "Microsoft.Quantum.Arrays.Transposed([[1, 2, 3], [4, 5, 6]])",
796 &Value::Array(
797 vec![
798 Value::Array(vec![Value::Int(1), Value::Int(4)].into()),
799 Value::Array(vec![Value::Int(2), Value::Int(5)].into()),
800 Value::Array(vec![Value::Int(3), Value::Int(6)].into()),
801 ]
802 .into(),
803 ),
804 );
805 test_expression(
806 "Microsoft.Quantum.Arrays.Transposed([[1, 4], [2, 5], [3, 6]])",
807 &Value::Array(
808 vec![
809 Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)].into()),
810 Value::Array(vec![Value::Int(4), Value::Int(5), Value::Int(6)].into()),
811 ]
812 .into(),
813 ),
814 );
815}
816
817#[test]
818fn check_unzipped() {
819 test_expression(
820 "{
821 let empty: (Int, Int)[] = [];
822 Microsoft.Quantum.Arrays.Unzipped(empty)
823 }",
824 &Value::Tuple(
825 vec![Value::Array(vec![].into()), Value::Array(vec![].into())].into(),
826 None,
827 ),
828 );
829 test_expression(
830 "Microsoft.Quantum.Arrays.Unzipped([(5, true), (4, false), (3, true), (2, true), (1, false)])",
831 &Value::Tuple(
832 vec![
833 Value::Array(
834 vec![
835 Value::Int(5),
836 Value::Int(4),
837 Value::Int(3),
838 Value::Int(2),
839 Value::Int(1),
840 ]
841 .into(),
842 ),
843 Value::Array(
844 vec![
845 Value::Bool(true),
846 Value::Bool(false),
847 Value::Bool(true),
848 Value::Bool(true),
849 Value::Bool(false),
850 ]
851 .into(),
852 ),
853 ]
854 .into(),
855 None,
856 ),
857 );
858 test_expression(
859 "Microsoft.Quantum.Arrays.Unzipped([(true, 5), (false, 4), (true, 3), (true, 2), (false, 1)])",
860 &Value::Tuple(
861 vec![
862 Value::Array(
863 vec![
864 Value::Bool(true),
865 Value::Bool(false),
866 Value::Bool(true),
867 Value::Bool(true),
868 Value::Bool(false),
869 ]
870 .into(),
871 ),
872 Value::Array(
873 vec![
874 Value::Int(5),
875 Value::Int(4),
876 Value::Int(3),
877 Value::Int(2),
878 Value::Int(1),
879 ]
880 .into(),
881 ),
882 ]
883 .into(),
884 None,
885 ),
886 );
887}
888
889#[test]
890fn check_where() {
891 test_expression(
892 "Microsoft.Quantum.Arrays.Where(x -> x % 2 == 0, [0, 1, 2, 3, 4])",
893 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
894 );
895 test_expression(
896 "Microsoft.Quantum.Arrays.Where(x -> x % 2 != 0, [1, 2, 3, 4, 5])",
897 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
898 );
899}
900
901#[test]
902fn check_windows() {
903 test_expression(
904 "Microsoft.Quantum.Arrays.Windows(1, [1, 2, 3, 4, 5])",
905 &Value::Array(
906 vec![
907 Value::Array(vec![Value::Int(1)].into()),
908 Value::Array(vec![Value::Int(2)].into()),
909 Value::Array(vec![Value::Int(3)].into()),
910 Value::Array(vec![Value::Int(4)].into()),
911 Value::Array(vec![Value::Int(5)].into()),
912 ]
913 .into(),
914 ),
915 );
916 test_expression(
917 "Microsoft.Quantum.Arrays.Windows(3, [1, 2, 3, 4, 5])",
918 &Value::Array(
919 vec![
920 Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)].into()),
921 Value::Array(vec![Value::Int(2), Value::Int(3), Value::Int(4)].into()),
922 Value::Array(vec![Value::Int(3), Value::Int(4), Value::Int(5)].into()),
923 ]
924 .into(),
925 ),
926 );
927 test_expression(
928 "Microsoft.Quantum.Arrays.Windows(5, [1, 2, 3, 4, 5])",
929 &Value::Array(
930 vec![Value::Array(
931 vec![
932 Value::Int(1),
933 Value::Int(2),
934 Value::Int(3),
935 Value::Int(4),
936 Value::Int(5),
937 ]
938 .into(),
939 )]
940 .into(),
941 ),
942 );
943}
944
945#[test]
946fn check_zipped() {
947 test_expression(
948 "{
949 let empty: Int[] = [];
950 Microsoft.Quantum.Arrays.Zipped(empty, empty)
951 }",
952 &Value::Array(vec![].into()),
953 );
954 test_expression(
955 "{
956 let empty: Int[] = [];
957 Microsoft.Quantum.Arrays.Zipped([1], empty)
958 }",
959 &Value::Array(vec![].into()),
960 );
961 test_expression(
962 "{
963 let empty: Int[] = [];
964 Microsoft.Quantum.Arrays.Zipped(empty, [false])
965 }",
966 &Value::Array(vec![].into()),
967 );
968 test_expression(
969 "Microsoft.Quantum.Arrays.Zipped([1, 2, 3, 4, 5], [false, true, true, false, true])",
970 &Value::Array(
971 vec![
972 Value::Tuple(vec![Value::Int(1), Value::Bool(false)].into(), None),
973 Value::Tuple(vec![Value::Int(2), Value::Bool(true)].into(), None),
974 Value::Tuple(vec![Value::Int(3), Value::Bool(true)].into(), None),
975 Value::Tuple(vec![Value::Int(4), Value::Bool(false)].into(), None),
976 Value::Tuple(vec![Value::Int(5), Value::Bool(true)].into(), None),
977 ]
978 .into(),
979 ),
980 );
981 test_expression(
982 "Microsoft.Quantum.Arrays.Zipped([false, true, true, false, true], [1, 2, 3, 4, 5])",
983 &Value::Array(
984 vec![
985 Value::Tuple(vec![Value::Bool(false), Value::Int(1)].into(), None),
986 Value::Tuple(vec![Value::Bool(true), Value::Int(2)].into(), None),
987 Value::Tuple(vec![Value::Bool(true), Value::Int(3)].into(), None),
988 Value::Tuple(vec![Value::Bool(false), Value::Int(4)].into(), None),
989 Value::Tuple(vec![Value::Bool(true), Value::Int(5)].into(), None),
990 ]
991 .into(),
992 ),
993 );
994 test_expression(
995 "Microsoft.Quantum.Arrays.Zipped([1, 2, 3], [false, true, true, false, true])",
996 &Value::Array(
997 vec![
998 Value::Tuple(vec![Value::Int(1), Value::Bool(false)].into(), None),
999 Value::Tuple(vec![Value::Int(2), Value::Bool(true)].into(), None),
1000 Value::Tuple(vec![Value::Int(3), Value::Bool(true)].into(), None),
1001 ]
1002 .into(),
1003 ),
1004 );
1005 test_expression(
1006 "Microsoft.Quantum.Arrays.Zipped([1, 2, 3, 4, 5], [false, true, true])",
1007 &Value::Array(
1008 vec![
1009 Value::Tuple(vec![Value::Int(1), Value::Bool(false)].into(), None),
1010 Value::Tuple(vec![Value::Int(2), Value::Bool(true)].into(), None),
1011 Value::Tuple(vec![Value::Int(3), Value::Bool(true)].into(), None),
1012 ]
1013 .into(),
1014 ),
1015 );
1016}
1017