File size: 8,315 Bytes
684db41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <gtest/gtest.h>

#include "kernel/rtlil.h"

YOSYS_NAMESPACE_BEGIN

// Test fixture with helper functions
class SigSpecRepTest : public ::testing::Test {
protected:
	Design* d;
	Module* m;

	void SetUp() override {
		d = new Design;
		m = d->addModule("$test");
	}

	void TearDown() override {
		delete d;
	}

	// Create n wires with given width
	std::vector<Wire*> createWires(int count, int width = 4) {
		std::vector<Wire*> wires;
		for (int i = 0; i < count; i++) {
			Wire* w = m->addWire(stringf("$w%d", i), width);
			wires.push_back(w);
		}
		return wires;
	}

	// Append all wires to a SigSpec
	SigSpec wiresAsSigSpec(const std::vector<Wire*>& wires) {
		SigSpec sig;
		for (auto w : wires)
			sig.append(w);
		return sig;
	}

	// Create a SigSpec of constants
	SigSpec constsAsSigSpec(int count, int width = 4) {
		SigSpec sig;
		for (int i = 0; i < count; i++)
			sig.append(Const(i, width));
		return sig;
	}

	// Convert wires to pool of SigBits
	pool<SigBit> wiresToPool(const std::vector<Wire*>& wires) {
		pool<SigBit> pool;
		for (auto w : wires)
			for (auto &bit : SigSpec(w))
				pool.insert(bit);
		return pool;
	}

	// Convert wires to set of SigBits
	std::set<SigBit> wiresToSet(const std::vector<Wire*>& wires) {
		std::set<SigBit> set;
		for (auto w : wires)
			for (auto &bit : SigSpec(w))
				set.insert(bit);
		return set;
	}
};

TEST_F(SigSpecRepTest, WithSigSpecPattern)
{
	auto wires = createWires(4);
	SigSpec sig = wiresAsSigSpec(wires);
	SigSpec pattern(wires[1]);  // Remove w2
	SigSpec other = constsAsSigSpec(4);

	EXPECT_EQ(sig.size(), 16);
	sig.remove2(pattern, &other);
	EXPECT_EQ(sig.size(), 12);
	EXPECT_EQ(other.size(), 12);

	// Verify correct wires remain (w1, w3, w4)
	SigSpec expected;
	expected.append(wires[0]);
	expected.append(wires[2]);
	expected.append(wires[3]);
	EXPECT_EQ(sig, expected);
}

TEST_F(SigSpecRepTest, WithPoolPattern)
{
	auto wires = createWires(3);
	SigSpec sig = wiresAsSigSpec(wires);
	auto pattern = wiresToPool({wires[1]});
	SigSpec other = constsAsSigSpec(3);

	EXPECT_EQ(sig.size(), 12);
	sig.remove2(pattern, &other);
	EXPECT_EQ(sig.size(), 8);
	EXPECT_EQ(other.size(), 8);

	// Verify correct wires remain (w0, w2)
	SigSpec expected;
	expected.append(wires[0]);
	expected.append(wires[2]);
	EXPECT_EQ(sig, expected);
}

TEST_F(SigSpecRepTest, WithSetPattern)
{
	auto wires = createWires(3);
	SigSpec sig = wiresAsSigSpec(wires);
	auto pattern = wiresToSet({wires[1]});
	SigSpec other = constsAsSigSpec(3);

	EXPECT_EQ(sig.size(), 12);
	sig.remove2(pattern, &other);
	EXPECT_EQ(sig.size(), 8);
	EXPECT_EQ(other.size(), 8);

	// Verify correct wires remain (w0, w2)
	SigSpec expected;
	expected.append(wires[0]);
	expected.append(wires[2]);
	EXPECT_EQ(sig, expected);
}

TEST_F(SigSpecRepTest, ManyElements)
{
	const int num_wires = 100;
	auto wires = createWires(num_wires);
	SigSpec sig = wiresAsSigSpec(wires);

	// Remove every other wire
	std::vector<Wire*> to_remove;
	for (int i = 0; i < num_wires; i += 2)
		to_remove.push_back(wires[i]);
	auto pattern = wiresToPool(to_remove);

	EXPECT_EQ(sig.size(), num_wires * 4);
	sig.remove2(pattern, nullptr);
	EXPECT_EQ(sig.size(), (num_wires / 2) * 4);

	// Verify odd-indexed wires remain (w1, w3, w5, ..., w99)
	SigSpec expected;
	for (int i = 1; i < num_wires; i += 2)
		expected.append(wires[i]);
	EXPECT_EQ(sig, expected);
}

// Test remove2 with very large dataset to check scaling
TEST_F(SigSpecRepTest, VeryLargeScalingTest)
{
	const int num_wires = 50000;
	auto wires = createWires(num_wires);
	SigSpec sig = wiresAsSigSpec(wires);
	SigSpec other = constsAsSigSpec(num_wires);

	// Create pattern with many chunks (one per wire)
	SigSpec pattern;
	for (int i = 0; i < num_wires; i += 2)
		pattern.append(wires[i]);

	EXPECT_EQ(sig.size(), num_wires * 4);
	sig.remove2(pattern, &other);
	EXPECT_EQ(sig.size(), (num_wires / 2) * 4);
	EXPECT_EQ(other.size(), (num_wires / 2) * 4);

	// Spot-check: odd-indexed wires should remain at expected positions
	for (int i = 0; i < num_wires / 2; i++) {
		EXPECT_EQ(sig[i * 4 + 0].wire, wires[i * 2 + 1]);
		EXPECT_EQ(sig[i * 4 + 1].wire, wires[i * 2 + 1]);
		EXPECT_EQ(sig[i * 4 + 2].wire, wires[i * 2 + 1]);
		EXPECT_EQ(sig[i * 4 + 3].wire, wires[i * 2 + 1]);
	}
}

// Test multiple sequential removals (simulates removeSignalFromCaseTree)
TEST_F(SigSpecRepTest, MultipleSequentialRemovals)
{
	const int num_wires = 512;
	auto wires = createWires(num_wires);

	// Create many actions, each with one wire
	std::vector<SigSpec> actions;
	for (auto w : wires)
		actions.push_back(SigSpec(w));

	// Remove half the wires from all actions
	for (int i = 0; i < num_wires / 2; i++) {
		SigSpec pattern(wires[i]);
		for (auto &action : actions)
			if (action.size() > 0)
				action.remove2(pattern, nullptr);
	}

	// Verify correct actions were cleared
	for (int i = 0; i < num_wires; i++) {
		EXPECT_EQ(actions[i].size(), i < num_wires / 2 ? 0 : 4);
	}

	// Verify remaining actions contain the correct wires
	for (int i = num_wires / 2; i < num_wires; i++) {
		SigSpec expected(wires[i]);
		EXPECT_EQ(actions[i], expected);
	}
}

// Test remove2 with very large dataset to check scaling
TEST_F(SigSpecRepTest, PoolOverloadLargeDataset)
{
	const int num_wires = 50000;
	auto wires = createWires(num_wires, 1);
	SigSpec sig = wiresAsSigSpec(wires);
	SigSpec other = constsAsSigSpec(num_wires, 1);

	// Remove half
	pool<SigBit> pattern;
	for (int i = 0; i < num_wires; i += 2)
		pattern.insert(SigBit(wires[i], 0));

	EXPECT_EQ(sig.size(), num_wires);
	sig.remove2(pattern, &other);
	EXPECT_EQ(sig.size(), num_wires / 2);
	EXPECT_EQ(other.size(), num_wires / 2);

	// Spot-check: odd-indexed wires should remain
	for (int i = 0; i < num_wires / 2; i++) {
		EXPECT_EQ(sig[i].wire, wires[i * 2 + 1]);
	}
}

// Test set overload (same perf characteristics as pool)
TEST_F(SigSpecRepTest, SetOverloadLargeDataset)
{
	const int num_wires = 50000;
	auto wires = createWires(num_wires, 1);
	SigSpec sig = wiresAsSigSpec(wires);
	SigSpec other = constsAsSigSpec(num_wires, 1);

	// Remove half
	std::set<SigBit> pattern;
	for (int i = 0; i < num_wires; i += 2)
		pattern.insert(SigBit(wires[i], 0));

	EXPECT_EQ(sig.size(), num_wires);
	sig.remove2(pattern, &other);
	EXPECT_EQ(sig.size(), num_wires / 2);
	EXPECT_EQ(other.size(), num_wires / 2);

	// Spot-check: odd-indexed wires should remain
	for (int i = 0; i < num_wires / 2; i++) {
		EXPECT_EQ(sig[i].wire, wires[i * 2 + 1]);
	}
}

// Worst case: remove almost all elements
TEST_F(SigSpecRepTest, RemoveAlmostAllElements)
{
	const int num_wires = 10000;
	auto wires = createWires(num_wires, 1);
	SigSpec sig = wiresAsSigSpec(wires);

	// Remove all but last
	pool<SigBit> pattern;
	for (int i = 0; i < num_wires - 1; i++)
		pattern.insert(SigBit(wires[i], 0));

	EXPECT_EQ(sig.size(), num_wires);
	sig.remove2(pattern, nullptr);
	EXPECT_EQ(sig.size(), 1);
	EXPECT_EQ(sig[0].wire, wires[num_wires - 1]);
}

TEST_F(SigSpecRepTest, EmptyPattern)
{
	auto wires = createWires(1);
	SigSpec sig(wires[0]);
	pool<SigBit> empty_pattern;

	EXPECT_EQ(sig.size(), 4);
	sig.remove2(empty_pattern, nullptr);
	EXPECT_EQ(sig.size(), 4);

	// Verify the wire is unchanged
	SigSpec expected(wires[0]);
	EXPECT_EQ(sig, expected);
}

// Test that NULL wire bits (constants) are not removed
TEST_F(SigSpecRepTest, NullWireBitsStay)
{
	auto wires = createWires(1);
	SigSpec sig;
	sig.append(wires[0]);
	sig.append(Const(15, 4));

	// Try to remove the constants
	pool<SigBit> pattern;
	SigSpec const_spec(Const(15, 4));
	for (auto &bit : const_spec)
		pattern.insert(bit);

	EXPECT_EQ(sig.size(), 8);
	sig.remove2(pattern, nullptr);
	EXPECT_EQ(sig.size(), 8);  // Constants stay

	// Verify original content is preserved
	SigSpec expected;
	expected.append(wires[0]);
	expected.append(Const(15, 4));
	EXPECT_EQ(sig, expected);
}

TEST_F(SigSpecRepTest, PartialBitRemoval)
{
	Wire* w = m->addWire("$w1", 8);
	SigSpec sig(w);

	// Remove bits 2-5
	pool<SigBit> pattern;
	for (int i = 2; i < 6; i++)
		pattern.insert(SigBit(w, i));

	EXPECT_EQ(sig.size(), 8);
	sig.remove2(pattern, nullptr);
	EXPECT_EQ(sig.size(), 4);

	// Verify only bits 0,1,6,7 remain
	EXPECT_EQ(sig[0], SigBit(w, 0));
	EXPECT_EQ(sig[1], SigBit(w, 1));
	EXPECT_EQ(sig[2], SigBit(w, 6));
	EXPECT_EQ(sig[3], SigBit(w, 7));
}

YOSYS_NAMESPACE_END