microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
library/qtest/src/Tests.qs
94lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import Std.Diagnostics.Fact; |
| 5 | import Std.Arrays.All; |
| 6 | |
| 7 | operation Main() : Unit { |
| 8 | FunctionTestMatrixTests(); |
| 9 | OperationTestMatrixTests(); |
| 10 | BasicTests(); |
| 11 | } |
| 12 | |
| 13 | operation OperationTestMatrixTests() : Unit { |
| 14 | let test_cases : (Qubit[] => Unit, Int)[] = [ |
| 15 | (qs => { X(qs[0]); X(qs[3]); }, 0b1001), |
| 16 | (qs => { X(qs[0]); X(qs[1]); }, 0b0011) |
| 17 | ]; |
| 18 | |
| 19 | let res1 : Util.TestCaseResult[] = Operations.TestMatrix( |
| 20 | "QubitTestMatrix", |
| 21 | qs => MeasureInteger(qs), |
| 22 | 4, |
| 23 | test_cases, |
| 24 | Operations.RunAllTestCases |
| 25 | ); |
| 26 | |
| 27 | let res2 : Util.TestCaseResult[] = Operations.RunTestMatrix( |
| 28 | "QubitTestMatrix", |
| 29 | qs => MeasureInteger(qs), |
| 30 | 4, |
| 31 | test_cases, |
| 32 | ); |
| 33 | |
| 34 | Fact(All(x -> x.did_pass, res1) and All(x -> x.did_pass, res2), "RunTestMatrix and TestMatrix did not return the same results"); |
| 35 | } |
| 36 | |
| 37 | function FunctionTestMatrixTests() : Unit { |
| 38 | let all_passed = Functions.TestMatrix("Return 42", TestCaseOne, [((), 42), ((), 42)], Functions.CheckAllTestCases); |
| 39 | Fact(all_passed, "basic test matrix did not pass"); |
| 40 | |
| 41 | let at_least_one_failed = not Functions.TestMatrix("Return 42", TestCaseOne, [((), 42), ((), 43)], Functions.CheckAllTestCases); |
| 42 | Fact(at_least_one_failed, "basic test matrix did not report failure"); |
| 43 | |
| 44 | let results = Functions.TestMatrix("AddOne", AddOne, [(5, 6), (6, 7)], Functions.RunAllTestCases); |
| 45 | Fact(Length(results) == 2, "test matrix did not return results for all test cases"); |
| 46 | Fact(All(result -> result.did_pass, results), "test matrix did not pass all test cases"); |
| 47 | } |
| 48 | |
| 49 | function BasicTests() : (String, () -> Int, Int)[] { |
| 50 | return [ |
| 51 | ("Should return 42", TestCaseOne, 43), |
| 52 | ("Should add one", () -> AddOne(5), 42), |
| 53 | ("Should add one", () -> AddOne(5), 6) |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | @Test() |
| 58 | function ReturnsFalseForFailingTest() : Unit { |
| 59 | Fact( |
| 60 | not Functions.CheckAllTestCases(BasicTests()), |
| 61 | "Test harness failed to return false for a failing tests." |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | @Test() |
| 66 | function ReturnsTrueForPassingTest() : Unit { |
| 67 | Fact( |
| 68 | Functions.CheckAllTestCases([("always returns true", () -> true, true)]), |
| 69 | "Test harness failed to return true for a passing test" |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | @Test() |
| 74 | function RunAllTests() : Unit { |
| 75 | let run_all_result = Functions.RunAllTestCases(BasicTests()); |
| 76 | |
| 77 | Fact( |
| 78 | Length(run_all_result) == 3, |
| 79 | "Test harness did not return results for all test cases." |
| 80 | ); |
| 81 | |
| 82 | Fact(not run_all_result[0].did_pass, "test one passed when it should have failed"); |
| 83 | Fact(not run_all_result[1].did_pass, "test two passed when it should have failed"); |
| 84 | Fact(run_all_result[2].did_pass, "test three failed when it should have passed"); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | function TestCaseOne() : Int { |
| 89 | 42 |
| 90 | } |
| 91 | |
| 92 | function AddOne(x : Int) : Int { |
| 93 | x + 1 |
| 94 | } |
| 95 | |