Skip to content

Commit 01013b3

Browse files
committed
typescript unit tests
1 parent 88c1b19 commit 01013b3

File tree

14 files changed

+440
-4
lines changed

14 files changed

+440
-4
lines changed

Multiplex.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,18 @@
8686
<Content Include="src\typescript\_references.ts" />
8787
<Content Include="test\data\_references.js" />
8888
<TypeScriptCompile Include="test\data\qunit.d.ts" />
89-
<Content Include="test\data\_references.ts" />
89+
<TypeScriptCompile Include="test\typescript\dictionary.ts" />
90+
<TypeScriptCompile Include="test\typescript\hashset.ts" />
91+
<TypeScriptCompile Include="test\typescript\linkedlist.ts" />
92+
<TypeScriptCompile Include="test\typescript\linq.ts" />
93+
<TypeScriptCompile Include="test\typescript\list.ts" />
94+
<TypeScriptCompile Include="test\typescript\lookup.ts" />
95+
<TypeScriptCompile Include="test\typescript\mx.ts" />
96+
<TypeScriptCompile Include="test\typescript\queue.ts" />
97+
<TypeScriptCompile Include="test\typescript\readonlycollection.ts" />
98+
<TypeScriptCompile Include="test\typescript\runtime.ts" />
99+
<TypeScriptCompile Include="test\typescript\sortedlist.ts" />
100+
<TypeScriptCompile Include="test\typescript\stack.ts" />
90101
</ItemGroup>
91102
<ItemGroup>
92103
<Reference Include="System" />

test/data/_references.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/typescript/dictionary.ts

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
module MxTests {
2+
3+
4+
function CreateDictionary(): Dictionary<number, string> {
5+
var dic = new Dictionary<number, string>();
6+
dic.add(1, "A");
7+
dic.add(2, "B");
8+
dic.add(3, "C");
9+
dic.add(4, "D");
10+
dic.add(5, "E");
11+
12+
return dic;
13+
}
14+
15+
16+
QUnit.module("Dictionary");
17+
18+
19+
QUnit.test("constructor", function (assert) {
20+
21+
var _comparer = EqualityComparer.create(
22+
function (o) { return mx.hash(o); },
23+
function (a, b) { return a === b; }
24+
),
25+
_d1 = new Dictionary<number, string>(),
26+
_d2 = new Dictionary<number, string>(CreateDictionary()),
27+
_d3 = new Dictionary<number, string>(_comparer),
28+
_d4 = new Dictionary<number, string>(5),
29+
_d5 = new Dictionary<number, string>(5, _comparer),
30+
_d6 = new Dictionary<number, string>(CreateDictionary(), _comparer);
31+
32+
33+
assert.ok(_d1.count() === 0, "initialize a Dictionary!");
34+
assert.ok(_d2.count() === 5, "initialize a Dictionary using specified dictionary!");
35+
assert.ok(_d3.count() === 0, "initialize a Dictionary using specified comparer!");
36+
assert.ok(_d4.count() === 0, "initialize a Dictionary using initial capacity!");
37+
assert.ok(_d5.count() === 0, "initialize a Dictionary using using initial capacity and comparer!");
38+
assert.ok(_d6.count() === 5, "initialize a Dictionary using specified dictionary and comparer!");
39+
});
40+
41+
42+
QUnit.test("add", function (assert) {
43+
var _dic = new Dictionary<number, string>();
44+
_dic.add(1, "A");
45+
46+
assert.ok(_dic.count() === 1, "ditionary add");
47+
assert.throws(function () {
48+
_dic.add(1, "B");
49+
}, "throws an error adding duplicate key");
50+
});
51+
52+
53+
QUnit.test("clear", function (assert) {
54+
var _dic = CreateDictionary();
55+
_dic.clear();
56+
57+
assert.ok(_dic.count() === 0, "ditionary clear!");
58+
});
59+
60+
61+
QUnit.test("containsKey", function (assert) {
62+
63+
var _dic = CreateDictionary();
64+
65+
assert.ok(_dic.containsKey(1) === true, "dictionary contains key!");
66+
assert.ok(_dic.containsKey(10) === false, "dictionary does not contain key!");
67+
});
68+
69+
70+
QUnit.test("containsValue", function (assert) {
71+
72+
var _dic = CreateDictionary();
73+
74+
assert.ok(_dic.containsValue("A") === true, "dictionary contains value!");
75+
assert.ok(_dic.containsValue("Z") === false, "dictionary does not contain value!");
76+
});
77+
78+
79+
QUnit.test("copyTo", function (assert) {
80+
81+
var _dic = CreateDictionary(),
82+
_arr = new Array(_dic.count());
83+
84+
_dic.copyTo(_arr, 0);
85+
86+
assert.deepEqual(_arr, [1, 2, 3, 4, 5], "dictionary copy to an array!");
87+
assert.throws(function () {
88+
_dic.copyTo([], 0);
89+
}, "throws an error when the number of elements is greater than the number of elements that the destination array can contain!");
90+
});
91+
92+
93+
QUnit.test("keys", function (assert) {
94+
95+
var _dic = CreateDictionary();
96+
97+
assert.deepEqual(_dic.keys(), [1, 2, 3, 4, 5], "dictionary keys!");
98+
assert.deepEqual(new Dictionary().keys(), [], "empty dictionary keys!");
99+
});
100+
101+
102+
QUnit.test("values", function (assert) {
103+
104+
var _dic = CreateDictionary();
105+
106+
assert.deepEqual(_dic.values(), ["A", "B", "C", "D", "E"], "dictionary values!");
107+
assert.deepEqual(new Dictionary().values(), [], "empty dictionary values!");
108+
});
109+
110+
111+
QUnit.test("get", function (assert) {
112+
113+
var _dic = CreateDictionary();
114+
115+
assert.ok(_dic.get(1) === "A", "dictionary get value!");
116+
assert.throws(function () {
117+
_dic.get(10);
118+
}, "throws an error getting non existing key!");
119+
});
120+
121+
122+
QUnit.test("set", function (assert) {
123+
124+
var _dic = CreateDictionary();
125+
_dic.set(1, "AA");
126+
127+
assert.ok(_dic.get(1) === "AA", "dictionary set value!");
128+
129+
_dic.set(6, "F");
130+
assert.ok(_dic.count() === 6 && _dic.get(6) === "F", "dictionary set new key and value!");
131+
});
132+
133+
134+
QUnit.test("tryGetValue", function (assert) {
135+
136+
var _dic = CreateDictionary();
137+
138+
assert.ok(function () {
139+
var value: string;
140+
141+
var res = _dic.tryGetValue(1, function (val) {
142+
value = val;
143+
});
144+
145+
return res && value === "A";
146+
147+
}, "dictionary tryGetValue, exisiting key!");
148+
149+
150+
assert.ok(function () {
151+
var value: string;
152+
153+
var res = _dic.tryGetValue(10, function (val) {
154+
value = val;
155+
});
156+
157+
return res === false;
158+
159+
}, "dictionary tryGetValue, invalid key!");
160+
});
161+
162+
163+
QUnit.test("remove", function (assert) {
164+
165+
var _dic = CreateDictionary();
166+
167+
assert.ok(_dic.remove(1) === true && _dic.count() === 4, "dictionary remove key!");
168+
assert.ok(_dic.remove(10) === false && _dic.count() === 4, "dictionary remove non existing key!");
169+
});
170+
171+
172+
QUnit.test("key-value pair", function (assert) {
173+
174+
var _pair1 = new KeyValuePair(1, "A"),
175+
_pair2 = new KeyValuePair(1, "A");
176+
177+
assert.ok(_pair1.key === 1 && _pair1.value === "A", "KeyValuePair get key/value!");
178+
179+
_pair1.key = 2;
180+
_pair1.value = "B";
181+
assert.ok(_pair1.key === 1 && _pair1.value === "A", "KeyValuePair key/value immutable!");
182+
183+
assert.ok(mx.hash(_pair1) === mx.hash(_pair2), "KeyValuePair get hash code!");
184+
assert.ok(mx.equals(_pair1, _pair2), "KeyValuePair equality check!");
185+
});
186+
187+
188+
QUnit.test("dictionary enumerable", function (assert) {
189+
190+
var _dic = CreateDictionary();
191+
192+
assert.deepEqual(_dic.select(t => t.key).toArray(), [1, 2, 3, 4, 5], "dictionary select keys, to array!");
193+
assert.deepEqual(_dic.select(t => t.value).toArray(), ["A", "B", "C", "D", "E"], "dictionary select values, to array!");
194+
assert.ok(_dic.toArray().first().key === 1 && _dic.toArray().first().value === "A", "dictionary select key-value items!");
195+
});
196+
}

test/typescript/hashset.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MxTests {
2+
3+
4+
}

test/typescript/linkedlist.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MxTests {
2+
3+
4+
}

test/typescript/linq.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MxTests {
2+
3+
4+
}

test/typescript/list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MxTests {
2+
3+
4+
}

test/typescript/lookup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MxTests {
2+
3+
4+
}

test/typescript/mx.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MxTests {
2+
3+
4+
}

test/typescript/queue.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module MxTests {
2+
3+
function CreateQueue(): Queue<number> {
4+
return new Queue(mx.range(1, 5));
5+
}
6+
7+
8+
QUnit.module("Queue");
9+
10+
11+
QUnit.test("constructor", function (assert) {
12+
13+
assert.ok(new Queue<number>().count() === 0, "initialize an empty Queue!");
14+
assert.ok(CreateQueue().count() === 5, "initialize a Queue using specified collection!");
15+
});
16+
17+
18+
QUnit.test("clear", function (assert) {
19+
20+
var _queue = CreateQueue();
21+
22+
_queue.clear();
23+
assert.ok(_queue.count() === 0, "clears a Queue!");
24+
});
25+
26+
27+
QUnit.test("contains", function (assert) {
28+
29+
var _queue = CreateQueue();
30+
31+
assert.ok(_queue.contains(1) === true, "queue containing an item!");
32+
assert.ok(_queue.contains(10) === false, "queue does not contain an item!");
33+
});
34+
35+
36+
QUnit.test("copyTo", function (assert) {
37+
38+
var _queue = CreateQueue(),
39+
_arr = new Array(_queue.count());
40+
41+
_queue.copyTo(_arr, 0);
42+
43+
assert.deepEqual(_arr, [1, 2, 3, 4, 5], "queue copy to an array!");
44+
assert.throws(function () {
45+
_queue.copyTo([], 0);
46+
}, "throws an error when the number of elements is greater than the number of elements that the destination array can contain!");
47+
});
48+
49+
50+
QUnit.test("dequeue", function (assert) {
51+
52+
var _queue = CreateQueue();
53+
assert.ok(_queue.dequeue() === 1, "queue dequeue an item!");
54+
55+
_queue.clear();
56+
assert.throws(function () {
57+
_queue.dequeue();
58+
}, "throws an error dequeue from empty queue!");
59+
});
60+
61+
62+
QUnit.test("enqueue", function (assert) {
63+
64+
var _queue = CreateQueue();
65+
66+
_queue.enqueue(6);
67+
assert.ok(_queue.count() === 6 && _queue.peek() === 1, "queue dequeue an item!");
68+
});
69+
70+
71+
QUnit.test("peek", function (assert) {
72+
73+
var _queue = CreateQueue();
74+
75+
assert.ok(_queue.peek() === 1, "queue peek an item!");
76+
77+
_queue.clear();
78+
assert.throws(function () {
79+
_queue.peek();
80+
}, "throws an error peek from empty queue!");
81+
});
82+
83+
84+
QUnit.test("toArray", function (assert) {
85+
86+
var _queue = CreateQueue();
87+
assert.deepEqual(_queue.toArray(), [1, 2, 3, 4, 5], "queue to array!");
88+
});
89+
90+
91+
QUnit.test("queue enumerable", function (assert) {
92+
93+
var _queue = CreateQueue();
94+
assert.deepEqual(_queue.select(t => t * 2).where(t => t > 5).toArray(), [6, 8, 10], "select-where-toArray over a queue!");
95+
});
96+
}

0 commit comments

Comments
 (0)