Uh oh!
There was an error while loading. Please reload this page.
forked from microsoft/Quantum
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayTests.qs
More file actions
Latest commit
87 lines (70 loc) · 3.5 KB
/
Copy pathArrayTests.qs
File metadata and controls
87 lines (70 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespaceMicrosoft.Quantum.Tests {
openMicrosoft.Quantum.Canon;
openMicrosoft.Quantum.Primitive;
functionZipTest() : () {
letleft= [1; 2; 101];
letright= [PauliY; PauliI];
letzipped=Zip(left, right);
let (leftActual1, rightActual1) =zipped[0];
if (leftActual1!=1||rightActual1!=PauliY) {
fail$"Expected (1, PauliY), got ({leftActual1}, {rightActual1}).";
}
let (leftActual2, rightActual2) =zipped[1];
if (leftActual2!=2||rightActual2!=PauliI) {
fail$"Expected (2, PauliI), got ({leftActual2}, {rightActual2}).";
}
}
functionLookupTest() : () {
letarray= [1; 12; 71; 103];
letfn=LookupFunction(array);
AssertIntEqual(fn(0), 1, "fn(0) did not return array[0]");
// Make sure we can call in random order!
AssertIntEqual(fn(3), 103, "fn(3) did not return array[3]");
AssertIntEqual(fn(2), 71, "fn(2) did not return array[2]");
AssertIntEqual(fn(1), 12, "fn(1) did not return array[1]");
}
functionConstantArrayTestHelper(x : Int) : Int {
returnx*x;
}
functionConstantArrayTest() : () {
letdblArray=ConstantArray(71, 2.17);
AssertIntEqual(Length(dblArray), 71, "ConstantArray(Int, Double) had the wrong length.");
letignore=Map(AssertAlmostEqual(_, 2.17), dblArray);
// Stress test by making an array of Int -> Int.
letfnArray=ConstantArray(7, ConstantArrayTestHelper);
AssertIntEqual(Length(fnArray), 7, "ConstantArray(Int, Int -> Int) had the wrong length.");
AssertIntEqual((fnArray[3])(7), 49, "ConstantArray(Int, Int -> Int) had the wrong value.");
}
functionSubarrayTest() : () {
letarray0= [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
letsubarrayOdd=Subarray([1; 3; 5; 7; 9], array0);
letsubarrayEven=Subarray([0; 2; 4; 6; 8; 10], array0);
AssertBoolEqual(ForAll(IsEven, subarrayEven), true, "the even elements of [1..10] were not correctly sliced.");
AssertBoolEqual(ForAny(IsEven, subarrayOdd), false, "the odd elements of [1..10] were not correctly sliced.");
letarray1= [10; 11; 12; 13];
Ignore(Map(AssertIntEqual(_, _, "Subarray failed: subpermutation case."), Zip([12; 11], Subarray([2; 1], array1))));
}
functionFilterTest() : () {
letarray= [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
letevenArray=Filter(IsEven, array);
AssertBoolEqual(ForAll(IsEven, evenArray), true, "the even elements of [1..10] were not correctly filtered.");
}
functionReverseTest() : () {
letarray= [1; 2; 3];
Ignore(Map(AssertIntEqual(_, _, "Reverse failed."), Zip([3; 2; 1], Reverse(array))));
}
functionExcludeTest() : () {
letarray= [10; 11; 12; 13; 14; 15];
Ignore(Map(AssertIntEqual(_, _, "Exclude failed."), Zip([10; 11; 13; 14], Exclude([2; 5], array))));
}
functionPadTest() : () {
mutablearrayTestCase= [(-5,2,[10; 11; 12],[10; 11; 12; 2; 2]);(5,2,[10; 11; 12],[2; 2; 10; 11; 12]);(-3,-2,[10; 11; 12],[10; 11; 12])];
for(idxTestin0..Length(arrayTestCase)-1){
let (nElementsTotal, defaultElement, inputArray, outputArray) =arrayTestCase[idxTest];
letpaddedArray=Pad(nElementsTotal, defaultElement, inputArray);
Ignore(Map(AssertIntEqual(_, _, "Pad failed."), Zip(outputArray, paddedArray)));
}
}
}