-
Notifications
You must be signed in to change notification settings - Fork 18
/
Average.ts
77 lines (57 loc) · 2.72 KB
/
Average.ts
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
import { InvalidOperationException } from "linq-to-typescript"
import { asAsync, expectAsync, itAsync, itEnumerable, itParallel } from "../TestHelpers"
describe("average", () => {
itEnumerable("basic", (asEnumerable) =>
expect(asEnumerable([0, 10]).average()).toBe(5))
itAsync("basic", async () =>
expect(await asAsync([0, 10]).average()).toBe(5))
itParallel("basic", async (asParallel) =>
expect(await asParallel([0, 10]).average()).toBe(5))
itEnumerable("EmptyThrowsException", (asEnumerable) =>
expect(() => asEnumerable([]).average()).toThrowError(InvalidOperationException))
itAsync("EmptyThrowsException", async () => {
const expect = await expectAsync(asAsync([]).average())
expect.toThrowError(InvalidOperationException)
})
itParallel("EmptyThrowsException", async (asParallel) => {
const expect = await expectAsync(asParallel([]).average())
expect.toThrowError(InvalidOperationException)
})
itEnumerable("selector", (asEnumerable) =>
expect(asEnumerable([0, 10]).average((x) => x * 10)).toBe(50))
itAsync("selector", async () =>
expect(await asAsync([0, 10]).average((x) => x * 10)).toBe(50))
itParallel("selector", async (asParallel) =>
expect(await asParallel([0, 10]).average((x) => x * 10)).toBe(50))
itEnumerable("empty array with selector throws exception",
(asEnumerable) => expect(
() => asEnumerable([] as number[]).average((x) => x * 10)).toThrowError(InvalidOperationException))
itAsync("empty array with selector throws exception", async () => {
const expect = await expectAsync((asAsync([] as number[])).average((x) => x * 10))
expect.toThrowError(InvalidOperationException)
})
itParallel("empty array with selector throws exception", async (asParallel) => {
const expect = await expectAsync((asParallel([] as number[])).average((x) => x * 10))
expect.toThrowError(InvalidOperationException)
})
//#region Zero Array
itEnumerable("Zero Array", async (asEnumerable) => {
const avg = asEnumerable([0, 0]).average()
expect(avg).toBe(0)
const avg2 = asEnumerable([0, 0]).average((x) => x * 2)
expect(avg2).toBe(0)
})
itAsync("Zero Array", async () => {
const avg = await asAsync([0, 0]).average()
expect(avg).toBe(0)
const avg2 = await asAsync([0, 0]).average((x) => x * 2)
expect(avg2).toBe(0)
})
itParallel("Zero Array", async (asParallel) => {
const avg = await asParallel([0, 0]).average()
expect(avg).toBe(0)
const avg2 = await asParallel([0, 0]).average((x) => x * 2)
expect(avg2).toBe(0)
})
//#endregion
})