microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
sccarda/BlochLearning

Branches

Tags

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

Clone

HTTPS

Download ZIP

library/src/tests/arrays.rs

998lines · 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 Std.Arrays namespace
9
10#[test]
11fn check_all() {
12 test_expression(
13 "Std.Arrays.All(x -> x != 0, [1, 2, 3, 4, 5])",
14 &Value::Bool(true),
15 );
16 test_expression(
17 "Std.Arrays.All(x -> x != 0, [1, 2, 0, 4, 5])",
18 &Value::Bool(false),
19 );
20 test_expression(
21 "Std.Arrays.All(x -> x == One, [One, One, One])",
22 &Value::Bool(true),
23 );
24 test_expression(
25 "Std.Arrays.All(x -> x == One, [One, One, Zero])",
26 &Value::Bool(false),
27 );
28}
29
30#[test]
31fn check_any() {
32 test_expression(
33 "Std.Arrays.Any(x -> x % 2 == 0, [1, 3, 6, 7, 9])",
34 &Value::Bool(true),
35 );
36 test_expression(
37 "Std.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 "Std.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 Std.Arrays.Chunks(2, empty)
62 }",
63 &Value::Array(vec![].into()),
64 );
65 test_expression(
66 "Std.Arrays.Chunks(2, [10])",
67 &Value::Array(vec![Value::Array(vec![Value::Int(10)].into())].into()),
68 );
69 test_expression(
70 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.Arrays.Count(x -> x % 2 != 0, [1, 3, 6, 7, 9])",
154 &Value::Int(4),
155 );
156 test_expression(
157 "Std.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 Std.Arrays.Diagonal(empty)
168 }",
169 &Value::Array(vec![].into()),
170 );
171 test_expression(
172 "Std.Arrays.Diagonal([[1]])",
173 &Value::Array(vec![Value::Int(1)].into()),
174 );
175 test_expression(
176 "Std.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 "Std.Arrays.Diagonal([[1, 2, 3], [4, 5, 6]])",
181 &Value::Array(vec![Value::Int(1), Value::Int(5)].into()),
182 );
183 test_expression(
184 "Std.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 = Std.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 Std.Arrays.Excluding(empty, empty)
208 }",
209 &Value::Array(vec![].into()),
210 );
211 test_expression(
212 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.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 "Std.Arrays.Fold((x, y) -> x + y, 0, [1, 2, 3, 4, 5])",
302 &Value::Int(15),
303 );
304 test_expression(
305 "Std.Arrays.Fold((x, y) -> x or y, false, [true, false, true])",
306 &Value::Bool(true),
307 );
308 test_expression(
309 "Std.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 Std.Arrays.ForEach
320 (q => {X(q); Std.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("Std.Arrays.Head([5,6,7,8])", &Value::Int(5));
330}
331
332#[test]
333fn check_head_and_rest() {
334 test_expression(
335 "Std.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 "Std.Arrays.IndexOf(x -> x % 2 != 0, [10, 8, 6, 5, 4])",
351 &Value::Int(3),
352 );
353 test_expression(
354 "Std.Arrays.IndexOf(x -> x % 2 == 0, [1, 3, 4, 5, 7])",
355 &Value::Int(2),
356 );
357 test_expression(
358 "Std.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("Std.Arrays.IndexRange([7,6,5,4])::Start", &Value::Int(0));
366 test_expression("Std.Arrays.IndexRange([7,6,5,4])::Step", &Value::Int(1));
367 test_expression("Std.Arrays.IndexRange([7,6,5,4])::End", &Value::Int(3));
368}
369
370#[test]
371fn check_interleaved() {
372 test_expression(
373 "Std.Arrays.Interleaved([1, 2, 3], [-1, -2, -3])",
374 &Value::Array(
375 vec![
376 Value::Int(1),
377 Value::Int(-1),
378 Value::Int(2),
379 Value::Int(-2),
380 Value::Int(3),
381 Value::Int(-3),
382 ]
383 .into(),
384 ),
385 );
386 test_expression(
387 "Std.Arrays.Interleaved([true, true], [false])",
388 &Value::Array(vec![Value::Bool(true), Value::Bool(false), Value::Bool(true)].into()),
389 );
390}
391
392#[test]
393fn check_is_empty() {
394 test_expression(
395 "{
396 let empty: Int[] = [];
397 Std.Arrays.IsEmpty(empty)
398 }",
399 &Value::Bool(true),
400 );
401 test_expression("Std.Arrays.IsEmpty([1])", &Value::Bool(false));
402 test_expression("Std.Arrays.IsEmpty([1, 2, 3, 4, 5])", &Value::Bool(false));
403}
404
405#[test]
406fn check_is_rectangular_array() {
407 test_expression(
408 "{
409 let empty: Int[] = [];
410 Std.Arrays.IsRectangularArray([empty])
411 }",
412 &Value::Bool(true),
413 );
414 test_expression("Std.Arrays.IsRectangularArray([[1]])", &Value::Bool(true));
415 test_expression(
416 "Std.Arrays.IsRectangularArray([[1, 2], [3, 4]])",
417 &Value::Bool(true),
418 );
419 test_expression(
420 "Std.Arrays.IsRectangularArray([[1, 2, 3], [4, 5, 6]])",
421 &Value::Bool(true),
422 );
423 test_expression(
424 "Std.Arrays.IsRectangularArray([[1, 2], [3, 4, 5]])",
425 &Value::Bool(false),
426 );
427}
428
429#[test]
430fn check_is_sorted() {
431 test_expression(
432 "{
433 let empty: Int[] = [];
434 Std.Arrays.IsSorted((x, y) -> x <= y, empty)
435 }",
436 &Value::Bool(true),
437 );
438 test_expression(
439 "Std.Arrays.IsSorted((x, y) -> x <= y, [1])",
440 &Value::Bool(true),
441 );
442 test_expression(
443 "Std.Arrays.IsSorted((x, y) -> x <= y, [1, 2, 3, 4, 5])",
444 &Value::Bool(true),
445 );
446 test_expression(
447 "Std.Arrays.IsSorted((x, y) -> x >= y, [5, 4, 3, 2, 1])",
448 &Value::Bool(true),
449 );
450 test_expression(
451 "Std.Arrays.IsSorted((x, y) -> x <= y, [1, 2, 3, 5, 4])",
452 &Value::Bool(false),
453 );
454 test_expression(
455 "Std.Arrays.IsSorted((x, y) -> x <= y, [5, 4, 3, 2, 1])",
456 &Value::Bool(false),
457 );
458}
459
460#[test]
461fn check_is_square_array() {
462 test_expression(
463 "{
464 let empty: Int[][] = [];
465 Std.Arrays.IsSquareArray(empty)
466 }",
467 &Value::Bool(true),
468 );
469 test_expression("Std.Arrays.IsSquareArray([[1]])", &Value::Bool(true));
470 test_expression(
471 "Std.Arrays.IsSquareArray([[1, 2], [3, 4]])",
472 &Value::Bool(true),
473 );
474 test_expression(
475 "Std.Arrays.IsSquareArray([[1, 2, 3], [4, 5, 6]])",
476 &Value::Bool(false),
477 );
478 test_expression(
479 "Std.Arrays.IsSquareArray([[1, 2], [3, 4], [5, 6]])",
480 &Value::Bool(false),
481 );
482}
483
484#[test]
485fn check_mapped() {
486 test_expression(
487 "Std.Arrays.Mapped(i -> i * 2, [0, 1, 2])",
488 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
489 );
490}
491
492#[test]
493fn check_mapped_by_index() {
494 test_expression(
495 "Std.Arrays.MappedByIndex((index, element) -> index == element ,[0, -1, 2])",
496 &Value::Array(vec![Value::Bool(true), Value::Bool(false), Value::Bool(true)].into()),
497 );
498}
499
500#[test]
501fn check_mapped_over_range() {
502 test_expression(
503 "Std.Arrays.MappedOverRange(x -> x + 1, 0..2..10)",
504 &Value::Array(
505 vec![
506 Value::Int(1),
507 Value::Int(3),
508 Value::Int(5),
509 Value::Int(7),
510 Value::Int(9),
511 Value::Int(11),
512 ]
513 .into(),
514 ),
515 );
516 test_expression(
517 "Std.Arrays.MappedOverRange(x -> x * 2, 3..-1..1)",
518 &Value::Array(vec![Value::Int(6), Value::Int(4), Value::Int(2)].into()),
519 );
520}
521
522#[test]
523fn check_most() {
524 test_expression(
525 "Std.Arrays.Most([5, 6, 7, 8])",
526 &Value::Array(vec![Value::Int(5), Value::Int(6), Value::Int(7)].into()),
527 );
528}
529
530#[test]
531fn check_most_and_tail() {
532 test_expression(
533 "Std.Arrays.MostAndTail([5, 6, 7, 8])",
534 &Value::Tuple(
535 vec![
536 Value::Array(vec![Value::Int(5), Value::Int(6), Value::Int(7)].into()),
537 Value::Int(8),
538 ]
539 .into(),
540 None,
541 ),
542 );
543}
544
545#[test]
546fn check_padded() {
547 test_expression(
548 "Std.Arrays.Padded(-5, 2, [10, 11, 12])",
549 &Value::Array(
550 vec![
551 Value::Int(10),
552 Value::Int(11),
553 Value::Int(12),
554 Value::Int(2),
555 Value::Int(2),
556 ]
557 .into(),
558 ),
559 );
560 test_expression(
561 "Std.Arrays.Padded(5, 2, [10, 11, 12])",
562 &Value::Array(
563 vec![
564 Value::Int(2),
565 Value::Int(2),
566 Value::Int(10),
567 Value::Int(11),
568 Value::Int(12),
569 ]
570 .into(),
571 ),
572 );
573 test_expression(
574 "Std.Arrays.Padded(3, 2, [10, 11, 12])",
575 &Value::Array(vec![Value::Int(10), Value::Int(11), Value::Int(12)].into()),
576 );
577 test_expression(
578 "Std.Arrays.Padded(-3, 2, [10, 11, 12])",
579 &Value::Array(vec![Value::Int(10), Value::Int(11), Value::Int(12)].into()),
580 );
581}
582
583#[test]
584fn check_partitioned() {
585 test_expression(
586 "Std.Arrays.Partitioned([2, 1], [2, 3, 5, 7])",
587 &Value::Array(
588 vec![
589 Value::Array(vec![Value::Int(2), Value::Int(3)].into()),
590 Value::Array(vec![Value::Int(5)].into()),
591 Value::Array(vec![Value::Int(7)].into()),
592 ]
593 .into(),
594 ),
595 );
596 test_expression(
597 "Std.Arrays.Partitioned([2, 2], [2, 3, 5, 7])",
598 &Value::Array(
599 vec![
600 Value::Array(vec![Value::Int(2), Value::Int(3)].into()),
601 Value::Array(vec![Value::Int(5), Value::Int(7)].into()),
602 Value::Array(vec![].into()),
603 ]
604 .into(),
605 ),
606 );
607}
608
609#[test]
610fn check_sequence_i() {
611 test_expression(
612 "Std.Arrays.SequenceI(0, 3)",
613 &Value::Array(vec![Value::Int(0), Value::Int(1), Value::Int(2), Value::Int(3)].into()),
614 );
615 test_expression(
616 "Std.Arrays.SequenceI(-5, -2)",
617 &Value::Array(
618 vec![
619 Value::Int(-5),
620 Value::Int(-4),
621 Value::Int(-3),
622 Value::Int(-2),
623 ]
624 .into(),
625 ),
626 );
627}
628
629#[test]
630fn check_sequence_l() {
631 test_expression(
632 "Std.Arrays.SequenceL(0L, 3L)",
633 &Value::Array(
634 vec![
635 Value::BigInt(BigInt::from(0)),
636 Value::BigInt(BigInt::from(1)),
637 Value::BigInt(BigInt::from(2)),
638 Value::BigInt(BigInt::from(3)),
639 ]
640 .into(),
641 ),
642 );
643 test_expression(
644 "Std.Arrays.SequenceL(-5L, -2L)",
645 &Value::Array(
646 vec![
647 Value::BigInt(BigInt::from(-5)),
648 Value::BigInt(BigInt::from(-4)),
649 Value::BigInt(BigInt::from(-3)),
650 Value::BigInt(BigInt::from(-2)),
651 ]
652 .into(),
653 ),
654 );
655}
656
657#[test]
658fn check_sorted() {
659 test_expression(
660 "{
661 let empty: Int[] = [];
662 Std.Arrays.Sorted((x, y) -> x <= y, empty)
663 }",
664 &Value::Array(vec![].into()),
665 );
666 test_expression(
667 "Std.Arrays.Sorted((x, y) -> x <= y, [-1])",
668 &Value::Array(vec![Value::Int(-1)].into()),
669 );
670 test_expression(
671 "Std.Arrays.Sorted((x, y) -> x <= y, [1, 2, 0, 4, 3])",
672 &Value::Array(
673 vec![
674 Value::Int(0),
675 Value::Int(1),
676 Value::Int(2),
677 Value::Int(3),
678 Value::Int(4),
679 ]
680 .into(),
681 ),
682 );
683 test_expression(
684 "Std.Arrays.Sorted((x, y) -> x >= y, [1, 2, 0, 4, 3])",
685 &Value::Array(
686 vec![
687 Value::Int(4),
688 Value::Int(3),
689 Value::Int(2),
690 Value::Int(1),
691 Value::Int(0),
692 ]
693 .into(),
694 ),
695 );
696 test_expression(
697 "Std.Arrays.Sorted((x, y) -> x <= y, [-1, 2, 0, 1, -2])",
698 &Value::Array(
699 vec![
700 Value::Int(-2),
701 Value::Int(-1),
702 Value::Int(0),
703 Value::Int(1),
704 Value::Int(2),
705 ]
706 .into(),
707 ),
708 );
709}
710
711#[test]
712fn check_rest() {
713 test_expression(
714 "Std.Arrays.Rest([5,6,7,8])",
715 &Value::Array(vec![Value::Int(6), Value::Int(7), Value::Int(8)].into()),
716 );
717}
718
719#[test]
720fn check_reversed() {
721 test_expression(
722 "Std.Arrays.Reversed([5,6,7,8])",
723 &Value::Array(vec![Value::Int(8), Value::Int(7), Value::Int(6), Value::Int(5)].into()),
724 );
725}
726
727#[test]
728fn check_subarray() {
729 test_expression(
730 "Std.Arrays.Subarray([3, 0, 2, 1], [1, 2, 3, 4])",
731 &Value::Array(vec![Value::Int(4), Value::Int(1), Value::Int(3), Value::Int(2)].into()),
732 );
733 test_expression(
734 "Std.Arrays.Subarray([1, 2, 2], [1, 2, 3, 4])",
735 &Value::Array(vec![Value::Int(2), Value::Int(3), Value::Int(3)].into()),
736 );
737 test_expression(
738 "Std.Arrays.Subarray([0, 0, 0, 0, 0], [false])",
739 &Value::Array(
740 vec![
741 Value::Bool(false),
742 Value::Bool(false),
743 Value::Bool(false),
744 Value::Bool(false),
745 Value::Bool(false),
746 ]
747 .into(),
748 ),
749 );
750}
751
752#[test]
753fn check_swapped() {
754 test_expression(
755 "Std.Arrays.Swapped(1, 3, [0, 1, 2, 3, 4])",
756 &Value::Array(
757 vec![
758 Value::Int(0),
759 Value::Int(3),
760 Value::Int(2),
761 Value::Int(1),
762 Value::Int(4),
763 ]
764 .into(),
765 ),
766 );
767}
768
769#[test]
770fn check_tail() {
771 test_expression("Std.Arrays.Tail([5,6,7,8])", &Value::Int(8));
772}
773
774#[test]
775fn check_transposed() {
776 test_expression(
777 "Std.Arrays.Transposed([[1, 2, 3], [4, 5, 6]])",
778 &Value::Array(
779 vec![
780 Value::Array(vec![Value::Int(1), Value::Int(4)].into()),
781 Value::Array(vec![Value::Int(2), Value::Int(5)].into()),
782 Value::Array(vec![Value::Int(3), Value::Int(6)].into()),
783 ]
784 .into(),
785 ),
786 );
787 test_expression(
788 "Std.Arrays.Transposed([[1, 4], [2, 5], [3, 6]])",
789 &Value::Array(
790 vec![
791 Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)].into()),
792 Value::Array(vec![Value::Int(4), Value::Int(5), Value::Int(6)].into()),
793 ]
794 .into(),
795 ),
796 );
797}
798
799#[test]
800fn check_unzipped() {
801 test_expression(
802 "{
803 let empty: (Int, Int)[] = [];
804 Std.Arrays.Unzipped(empty)
805 }",
806 &Value::Tuple(
807 vec![Value::Array(vec![].into()), Value::Array(vec![].into())].into(),
808 None,
809 ),
810 );
811 test_expression(
812 "Std.Arrays.Unzipped([(5, true), (4, false), (3, true), (2, true), (1, false)])",
813 &Value::Tuple(
814 vec![
815 Value::Array(
816 vec![
817 Value::Int(5),
818 Value::Int(4),
819 Value::Int(3),
820 Value::Int(2),
821 Value::Int(1),
822 ]
823 .into(),
824 ),
825 Value::Array(
826 vec![
827 Value::Bool(true),
828 Value::Bool(false),
829 Value::Bool(true),
830 Value::Bool(true),
831 Value::Bool(false),
832 ]
833 .into(),
834 ),
835 ]
836 .into(),
837 None,
838 ),
839 );
840 test_expression(
841 "Std.Arrays.Unzipped([(true, 5), (false, 4), (true, 3), (true, 2), (false, 1)])",
842 &Value::Tuple(
843 vec![
844 Value::Array(
845 vec![
846 Value::Bool(true),
847 Value::Bool(false),
848 Value::Bool(true),
849 Value::Bool(true),
850 Value::Bool(false),
851 ]
852 .into(),
853 ),
854 Value::Array(
855 vec![
856 Value::Int(5),
857 Value::Int(4),
858 Value::Int(3),
859 Value::Int(2),
860 Value::Int(1),
861 ]
862 .into(),
863 ),
864 ]
865 .into(),
866 None,
867 ),
868 );
869}
870
871#[test]
872fn check_where() {
873 test_expression(
874 "Std.Arrays.Where(x -> x % 2 == 0, [0, 1, 2, 3, 4])",
875 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
876 );
877 test_expression(
878 "Std.Arrays.Where(x -> x % 2 != 0, [1, 2, 3, 4, 5])",
879 &Value::Array(vec![Value::Int(0), Value::Int(2), Value::Int(4)].into()),
880 );
881}
882
883#[test]
884fn check_windows() {
885 test_expression(
886 "Std.Arrays.Windows(1, [1, 2, 3, 4, 5])",
887 &Value::Array(
888 vec![
889 Value::Array(vec![Value::Int(1)].into()),
890 Value::Array(vec![Value::Int(2)].into()),
891 Value::Array(vec![Value::Int(3)].into()),
892 Value::Array(vec![Value::Int(4)].into()),
893 Value::Array(vec![Value::Int(5)].into()),
894 ]
895 .into(),
896 ),
897 );
898 test_expression(
899 "Std.Arrays.Windows(3, [1, 2, 3, 4, 5])",
900 &Value::Array(
901 vec![
902 Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)].into()),
903 Value::Array(vec![Value::Int(2), Value::Int(3), Value::Int(4)].into()),
904 Value::Array(vec![Value::Int(3), Value::Int(4), Value::Int(5)].into()),
905 ]
906 .into(),
907 ),
908 );
909 test_expression(
910 "Std.Arrays.Windows(5, [1, 2, 3, 4, 5])",
911 &Value::Array(
912 vec![Value::Array(
913 vec![
914 Value::Int(1),
915 Value::Int(2),
916 Value::Int(3),
917 Value::Int(4),
918 Value::Int(5),
919 ]
920 .into(),
921 )]
922 .into(),
923 ),
924 );
925}
926
927#[test]
928fn check_zipped() {
929 test_expression(
930 "{
931 let empty: Int[] = [];
932 Std.Arrays.Zipped(empty, empty)
933 }",
934 &Value::Array(vec![].into()),
935 );
936 test_expression(
937 "{
938 let empty: Int[] = [];
939 Std.Arrays.Zipped([1], empty)
940 }",
941 &Value::Array(vec![].into()),
942 );
943 test_expression(
944 "{
945 let empty: Int[] = [];
946 Std.Arrays.Zipped(empty, [false])
947 }",
948 &Value::Array(vec![].into()),
949 );
950 test_expression(
951 "Std.Arrays.Zipped([1, 2, 3, 4, 5], [false, true, true, false, true])",
952 &Value::Array(
953 vec![
954 Value::Tuple(vec![Value::Int(1), Value::Bool(false)].into(), None),
955 Value::Tuple(vec![Value::Int(2), Value::Bool(true)].into(), None),
956 Value::Tuple(vec![Value::Int(3), Value::Bool(true)].into(), None),
957 Value::Tuple(vec![Value::Int(4), Value::Bool(false)].into(), None),
958 Value::Tuple(vec![Value::Int(5), Value::Bool(true)].into(), None),
959 ]
960 .into(),
961 ),
962 );
963 test_expression(
964 "Std.Arrays.Zipped([false, true, true, false, true], [1, 2, 3, 4, 5])",
965 &Value::Array(
966 vec![
967 Value::Tuple(vec![Value::Bool(false), Value::Int(1)].into(), None),
968 Value::Tuple(vec![Value::Bool(true), Value::Int(2)].into(), None),
969 Value::Tuple(vec![Value::Bool(true), Value::Int(3)].into(), None),
970 Value::Tuple(vec![Value::Bool(false), Value::Int(4)].into(), None),
971 Value::Tuple(vec![Value::Bool(true), Value::Int(5)].into(), None),
972 ]
973 .into(),
974 ),
975 );
976 test_expression(
977 "Std.Arrays.Zipped([1, 2, 3], [false, true, true, false, true])",
978 &Value::Array(
979 vec![
980 Value::Tuple(vec![Value::Int(1), Value::Bool(false)].into(), None),
981 Value::Tuple(vec![Value::Int(2), Value::Bool(true)].into(), None),
982 Value::Tuple(vec![Value::Int(3), Value::Bool(true)].into(), None),
983 ]
984 .into(),
985 ),
986 );
987 test_expression(
988 "Std.Arrays.Zipped([1, 2, 3, 4, 5], [false, true, true])",
989 &Value::Array(
990 vec![
991 Value::Tuple(vec![Value::Int(1), Value::Bool(false)].into(), None),
992 Value::Tuple(vec![Value::Int(2), Value::Bool(true)].into(), None),
993 Value::Tuple(vec![Value::Int(3), Value::Bool(true)].into(), None),
994 ]
995 .into(),
996 ),
997 );
998}
999