diff --git a/library/qtest/src/Functions.qs b/library/qtest/src/Functions.qs index 63d758c3da..c1b278d190 100644 --- a/library/qtest/src/Functions.qs +++ b/library/qtest/src/Functions.qs @@ -62,11 +62,32 @@ function RunAllTestCases<'T : Eq + Show>(test_cases : (String, () -> 'T, 'T)[]) /// TestMatrix("Add One", x -> x + 1, [(2, 3), (3, 4)], CheckAllTestCases); /// ``` -function TestMatrix<'T, 'O : Show + Eq, 'U>(test_suite_name : String, func : 'T -> 'O, test_cases : ('T, 'O)[], mode : ((String, () -> 'O, 'O)[]) -> 'U) : 'U { +function TestMatrix<'T, 'O : Show + Eq, 'U>( + test_suite_name : String, + func : 'T -> 'O, + test_cases : ('T, 'O)[], + mode : ((String, () -> 'O, 'O)[]) -> 'U +) : 'U { let test_cases_qs = Mapped((ix, (input, expected)) -> (test_suite_name + $" {ix + 1}", () -> func(input), expected), Enumerated(test_cases)); mode(test_cases_qs) } +function RunTestMatrix<'T : Show, 'O : Show + Eq>( + test_suite_name : String, + func : 'T -> 'O, + test_cases : ('T, 'O)[] +) : TestCaseResult[] { + TestMatrix(test_suite_name, func, test_cases, RunAllTestCases) +} + +function CheckTestMatrix<'T : Show, 'O : Show + Eq>( + test_suite_name : String, + func : 'T -> 'O, + test_cases : ('T, 'O)[] +) : Bool { + TestMatrix(test_suite_name, func, test_cases, CheckAllTestCases) +} + /// Internal (non-exported) helper function. Runs a test case and produces a `TestCaseResult` function TestCase<'T : Eq + Show>(name : String, test_case : () -> 'T, expected : 'T) : TestCaseResult { let result = test_case(); @@ -77,4 +98,4 @@ function TestCase<'T : Eq + Show>(name : String, test_case : () -> 'T, expected } } -export CheckAllTestCases, RunAllTestCases, TestMatrix; +export CheckAllTestCases, RunAllTestCases, TestMatrix, RunTestMatrix, CheckTestMatrix; diff --git a/library/qtest/src/Operations.qs b/library/qtest/src/Operations.qs index c36091c969..1f334d3ad5 100644 --- a/library/qtest/src/Operations.qs +++ b/library/qtest/src/Operations.qs @@ -98,11 +98,34 @@ operation MappedOperation<'T, 'U>(mapper : ('T => 'U), array : 'T[]) : 'U[] { /// ); /// ``` -operation TestMatrix<'O : Show + Eq, 'U>(test_suite_name : String, func : Qubit[] => 'O, num_qubits : Int, test_cases : (Qubit[] => Unit, 'O)[], mode : ((String, Int, Qubit[] => Unit, Qubit[] => 'O, 'O)[]) => 'U) : 'U { +operation TestMatrix<'O : Show + Eq, 'U>( + test_suite_name : String, + func : Qubit[] => 'O, + num_qubits : Int, + test_cases : (Qubit[] => Unit, 'O)[], + mode : ((String, Int, Qubit[] => Unit, Qubit[] => 'O, 'O)[]) => 'U +) : 'U { let test_cases_qs = Mapped((ix, (qubit_prep_function, expected)) -> (test_suite_name + $" {ix + 1}", num_qubits, qubit_prep_function, func, expected), Enumerated(test_cases)); mode(test_cases_qs) } +operation CheckTestMatrix<'O : Show + Eq>( + test_suite_name : String, + func : Qubit[] => 'O, + num_qubits : Int, + test_cases : (Qubit[] => Unit, 'O)[] +) : Bool { + TestMatrix(test_suite_name, func, num_qubits, test_cases, CheckAllTestCases) +} + +operation RunTestMatrix<'O : Show + Eq>( + test_suite_name : String, + func : Qubit[] => 'O, + num_qubits : Int, + test_cases : (Qubit[] => Unit, 'O)[] +) : TestCaseResult[] { + TestMatrix(test_suite_name, func, num_qubits, test_cases, RunAllTestCases) +} /// Internal (non-exported) helper function. Runs a test case and produces a `TestCaseResult` operation TestCase<'T : Eq + Show>(name : String, qubits : Qubit[], test_case : (Qubit[]) => 'T, expected : 'T) : TestCaseResult { @@ -114,4 +137,4 @@ operation TestCase<'T : Eq + Show>(name : String, qubits : Qubit[], test_case : } } -export CheckAllTestCases, RunAllTestCases; \ No newline at end of file +export CheckAllTestCases, RunAllTestCases, TestMatrix, CheckTestMatrix, RunTestMatrix; \ No newline at end of file diff --git a/library/qtest/src/Tests.qs b/library/qtest/src/Tests.qs index 34515ca86f..ab8b9e147d 100644 --- a/library/qtest/src/Tests.qs +++ b/library/qtest/src/Tests.qs @@ -16,13 +16,22 @@ operation OperationTestMatrixTests() : Unit { (qs => { X(qs[0]); X(qs[1]); }, 0b0011) ]; - let res : Util.TestCaseResult[] = Operations.TestMatrix( + let res1 : Util.TestCaseResult[] = Operations.TestMatrix( "QubitTestMatrix", qs => MeasureInteger(qs), 4, test_cases, Operations.RunAllTestCases ); + + let res2 : Util.TestCaseResult[] = Operations.RunTestMatrix( + "QubitTestMatrix", + qs => MeasureInteger(qs), + 4, + test_cases, + ); + + Fact(All(x -> x.did_pass, res1) and All(x -> x.did_pass, res2), "RunTestMatrix and TestMatrix did not return the same results"); } function FunctionTestMatrixTests() : Unit {