This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
WorkspaceControllerTest.cs
147 lines (117 loc) · 4.92 KB
/
WorkspaceControllerTest.cs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Quantum.IQSharp;
using Microsoft.Quantum.IQSharp.Common;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
namespace Tests.IQSharp
{
[TestClass]
public class WorkspaceControllerTest
{
public WorkspaceController Init(string root = @"Workspace")
{
return Startup.Create<WorkspaceController>(root);
}
[TestMethod]
public async Task GetMany()
{
var controller = Init();
var response = await controller.GetMany();
Assert.AreEqual(Status.Success, response.Status);
Assert.AreEqual(0, response.Messages.Length);
Assert.IsTrue(response.Result.Length > 1);
}
[TestMethod]
public async Task GetOne()
{
var controller = Init();
var response = await controller.GetOne("Tests.qss.NoOp");
Assert.AreEqual(Status.Success, response.Status);
Assert.AreEqual(0, response.Messages.Length);
Assert.AreEqual("Tests.qss.NoOp", response.Result.FullName);
response = await controller.GetOne("Tests.qss.HelloAgain");
Assert.AreEqual(Status.Success, response.Status);
Assert.AreEqual(0, response.Messages.Length);
Assert.AreEqual("Tests.qss.HelloAgain", response.Result.FullName);
}
[TestMethod]
public async Task SimulateHelloQ()
{
var controller = Init();
var response = await controller.Simulate("Tests.qss.HelloQ");
Assert.AreEqual(Status.Success, response.Status);
Assert.AreEqual(1, response.Messages.Length);
Assert.AreEqual("Hello from quantum world!", response.Messages[0]);
Assert.AreEqual(QVoid.Instance, response.Result);
}
[TestMethod]
public async Task SimulateHelloAgain()
{
var args = new Dictionary<string, string> { { "name", "'foo'" }, { "count", "5" } };
var controller = Init();
var messages = new List<string>();
var response = await controller.Simulate("Tests.qss.HelloAgain", args, messages.Add);
Assert.AreEqual(1, messages.Count);
Assert.AreEqual($"Hello foo again!", messages[0]);
Assert.AreEqual(5L, ((QArray<Result>)response).Length);
}
[TestMethod]
public async Task SimulateUnknown()
{
var controller = Init();
var response = await controller.Simulate("Foo.Q");
Assert.AreEqual(Status.Error, response.Status);
Assert.AreEqual(1, response.Messages.Length);
Assert.AreEqual($"Invalid operation name: Foo.Q", response.Messages[0]);
}
[TestMethod]
public async Task SimulateWithNullWorkspace()
{
var controller = Init();
controller.Workspace = null;
var response = await controller.Simulate("Foo.Q");
Assert.AreEqual(Status.Error, response.Status);
Assert.AreEqual(1, response.Messages.Length);
Assert.AreEqual($"Workspace is not ready. Try again.", response.Messages[0]);
}
[TestMethod]
public async Task SimulateOnBrokenWorkspace()
{
var controller = Init(@"Workspace.Broken");
var response = await controller.Simulate("Tests.qss.NoOp");
Assert.AreEqual(Status.Error, response.Status);
Assert.IsTrue(response.Messages.Length > 0, "No messages.");
}
[TestMethod]
public void JsonToDict()
{
var s = "bar";
var i = 1;
var t = ("A", ("A.a", "A.b"));
var a = new int[] { 1, 2, 3, 4};
var arg = new Dictionary<string, object>
{
{ "s", s },
{ "i", i },
{ "t", t },
{ "a", a }
};
var json = JsonConvert.SerializeObject(arg);
var result = JsonConverters.JsonToDict(json);
Assert.AreEqual(4, result.Count);
Assert.AreEqual(JsonConvert.SerializeObject(s), result["s"]);
Assert.AreEqual(JsonConvert.SerializeObject(i), result["i"]);
Assert.AreEqual(JsonConvert.SerializeObject(t), result["t"]);
Assert.AreEqual(JsonConvert.SerializeObject(a), result["a"]);
}
}
}
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously