Open History to see the last commit
1import { describe, expect, test } from "vitest"
2import { D2 } from "../../src/d2.js"
3import { MultiSet } from "../../src/multiset.js"
4import { concat, output } from "../../src/operators/index.js"
5
6describe(`Operators`, () => {
7 describe(`Concat operation`, () => {
8 test(`basic concat operation`, () => {
9 const graph = new D2()
10 const input1 = graph.newInput<number>()
11 const input2 = graph.newInput<number>()
12 const messages: Array<MultiSet<number>> = []
13
14 input1.pipe(
15 concat(input2),
16 output((message) => {
17 messages.push(message)
18 })
19 )
20
21 graph.finalize()
22
23 input1.sendData(
24 new MultiSet([
25 [1, 1],
26 [2, 1],
27 ])
28 )
29
30 input2.sendData(
31 new MultiSet([
32 [3, 1],
33 [4, 1],
34 ])
35 )
36
37 graph.run()
38
39 expect(messages).toEqual([
40 new MultiSet([
41 [1, 1],
42 [2, 1],
43 ]),
44 new MultiSet([
45 [3, 1],
46 [4, 1],
47 ]),
48 ])
49 })
50
51 test(`concat with mixed multiplicities`, () => {
52 const graph = new D2()
53 const input1 = graph.newInput<number>()
54 const input2 = graph.newInput<number>()
55 const messages: Array<MultiSet<number>> = []
56
57 input1.pipe(
58 concat(input2),
59 output((message) => {
60 messages.push(message)
61 })
62 )
63
64 graph.finalize()
65
66 input1.sendData(
67 new MultiSet([
68 [1, -1],
69 [2, 2],
70 ])
71 )
72
73 input2.sendData(
74 new MultiSet([
75 [3, -2],
76 [4, 1],
77 ])
78 )
79
80 graph.run()
81
82 expect(messages).toEqual([
83 new MultiSet([
84 [1, -1],
85 [2, 2],
86 ]),
87 new MultiSet([
88 [3, -2],
89 [4, 1],
90 ]),
91 ])
92 })
93
94 test(`concat with different types`, () => {
95 const graph = new D2()
96 const input1 = graph.newInput<number>()
97 const input2 = graph.newInput<string>()
98 const messages: Array<MultiSet<number | string>> = []
99
100 input1.pipe(
101 concat(input2),
102 output((message) => {
103 messages.push(message)
104 })
105 )
106
107 graph.finalize()
108
109 input1.sendData(
110 new MultiSet([
111 [1, 1],
112 [2, 1],
113 ])
114 )
115
116 input2.sendData(
117 new MultiSet([
118 [`a`, 1],
119 [`b`, 1],
120 ])
121 )
122
123 graph.run()
124
125 expect(messages).toEqual([
126 new MultiSet([
127 [1, 1],
128 [2, 1],
129 ]),
130 new MultiSet([
131 [`a`, 1],
132 [`b`, 1],
133 ]),
134 ])
135 })
136
137 test(`concat with overlapping data`, () => {
138 const graph = new D2()
139 const input1 = graph.newInput<number>()
140 const input2 = graph.newInput<number>()
141 const messages: Array<MultiSet<number>> = []
142
143 input1.pipe(
144 concat(input2),
145 output((message) => {
146 messages.push(message)
147 })
148 )
149
150 graph.finalize()
151
152 input1.sendData(
153 new MultiSet([
154 [1, 1],
155 [2, 1],
156 [3, 1],
157 ])
158 )
159
160 input2.sendData(
161 new MultiSet([
162 [2, 2],
163 [3, -1],
164 [4, 1],
165 ])
166 )
167
168 graph.run()
169
170 expect(messages).toEqual([
171 new MultiSet([
172 [1, 1],
173 [2, 1],
174 [3, 1],
175 ]),
176 new MultiSet([
177 [2, 2],
178 [3, -1],
179 [4, 1],
180 ]),
181 ])
182 })
183 })
184})