Repository navigation
Expand file tree
/
Copy pathtest_cpp_concepts.py
More file actions
432 lines (352 loc) · 10.6 KB
/
Copy pathtest_cpp_concepts.py
File metadata and controls
432 lines (352 loc) · 10.6 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
Comprehensive C++20 concepts testing.
Tests concept definitions, constraints, requires clauses, and concept-constrained templates.
"""
from pathlib import Path
from typing import cast
from unittest.mock import MagicMock
import pytest
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.parser_loader import load_parsers
@pytest.fixture
def cpp_concepts_project(temp_repo: Path) -> Path:
"""Create a comprehensive C++ project with concepts patterns."""
project_path = temp_repo / "cpp_concepts_test"
project_path.mkdir()
# Create basic structure
(project_path / "src").mkdir()
(project_path / "include").mkdir()
# Create base files
(project_path / "include" / "concepts.h").write_text(
"#pragma once\n// Concepts header"
)
return project_path
def test_concept_definitions_and_constraints(
cpp_concepts_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test basic concept definitions and template constraints."""
test_file = cpp_concepts_project / "concept_definitions.cpp"
test_file.write_text(
"""
#include <concepts>
#include <type_traits>
#include <vector>
#include <string>
#include <iterator>
// Basic concept definition
template<typename T>
concept Integral = std::is_integral_v<T>;
// Compound concept with multiple requirements
template<typename T>
concept SignedIntegral = Integral<T> && std::is_signed_v<T>;
// Concept with requires clause
template<typename T>
concept Addable = requires(T a, T b) {
a + b;
{ a + b } -> std::same_as<T>;
};
// Complex concept with nested requirements
template<typename T>
concept Container = requires(T t) {
typename T::value_type;
typename T::iterator;
{ t.begin() } -> std::same_as<typename T::iterator>;
{ t.end() } -> std::same_as<typename T::iterator>;
{ t.size() } -> std::convertible_to<std::size_t>;
};
// Function template with concept constraint
template<Integral T>
T multiply(T a, T b) {
return a * b;
}
// Function with requires clause
template<typename T>
requires Addable<T>
T add_values(T a, T b) {
return a + b;
}
// Class template with concept constraint
template<Container C>
class ContainerProcessor {
public:
using ValueType = typename C::value_type;
explicit ContainerProcessor(const C& container)
: container_(container) {}
std::size_t process() {
return container_.size();
}
private:
const C& container_;
};
// Concept with auto parameter
auto process_integral(Integral auto value) {
return value * 2;
}
// Abbreviated function template
void print_addable(Addable auto value) {
// Process addable type
}
void demonstrateBasicConcepts() {
// Test integral concepts
int x = 42;
long y = 100L;
auto result1 = multiply(x, 5);
auto result2 = multiply(y, 3L);
// Test container concepts
std::vector<int> vec{1, 2, 3, 4, 5};
ContainerProcessor processor(vec);
auto size = processor.process();
// Test abbreviated templates
auto doubled = process_integral(42);
print_addable(3.14);
}
"""
)
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=cpp_concepts_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify concept definitions are detected
all_relationships = cast(
MagicMock, mock_ingestor.ensure_relationship_batch
).call_args_list
defines_relationships = [c for c in all_relationships if c.args[1] == "DEFINES"]
# Look for concept-related definitions
concept_definitions = [
call
for call in defines_relationships
if "concept_definitions" in call.args[0][2]
and (
"concept" in call.args[2][2].lower()
or "template" in call.args[2][2].lower()
)
]
assert len(concept_definitions) >= 3, (
f"Expected at least 3 concept-related definitions, found {len(concept_definitions)}"
)
def test_advanced_concept_patterns(
cpp_concepts_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test advanced concepts usage patterns and custom concept definitions."""
test_file = cpp_concepts_project / "advanced_concepts.cpp"
test_file.write_text(
"""
#include <concepts>
#include <type_traits>
#include <memory>
#include <iostream>
namespace custom_concepts {
// Advanced concept for smart pointer types
template<typename T>
concept SmartPointer = requires(T t) {
typename T::element_type;
{ t.get() } -> std::convertible_to<typename T::element_type*>;
{ t.reset() } -> std::same_as<void>;
{ static_cast<bool>(t) } -> std::same_as<bool>;
};
// Concept for movable types
template<typename T>
concept Movable = std::is_move_constructible_v<T> &&
std::is_move_assignable_v<T>;
// Resource management concept
template<typename T>
concept Resource = requires(T t) {
{ t.acquire() } -> std::same_as<void>;
{ t.release() } -> std::same_as<void>;
{ t.is_valid() } -> std::convertible_to<bool>;
};
// Serializable concept
template<typename T>
concept Serializable = requires(T t, std::ostream& os, std::istream& is) {
{ t.serialize(os) } -> std::same_as<void>;
{ T::deserialize(is) } -> std::same_as<T>;
};
// Polymorphic concept
template<typename T, typename Base>
concept DerivedFrom = std::is_base_of_v<Base, T> &&
std::is_convertible_v<T*, Base*>;
} // namespace custom_concepts
using namespace custom_concepts;
// Smart pointer implementation
template<SmartPointer T>
class SmartPointerWrapper {
private:
T ptr_;
public:
explicit SmartPointerWrapper(T ptr) : ptr_(std::move(ptr)) {}
auto get() const { return ptr_.get(); }
void reset() { ptr_.reset(); }
operator bool() const { return static_cast<bool>(ptr_); }
};
// Resource management with RAII
template<Resource R>
class ResourceManager {
private:
R resource_;
bool owned_;
public:
explicit ResourceManager(R resource)
: resource_(std::move(resource)), owned_(true) {
resource_.acquire();
}
~ResourceManager() {
if (owned_ && resource_.is_valid()) {
resource_.release();
}
}
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(const ResourceManager&) = delete;
ResourceManager(ResourceManager&& other) noexcept
: resource_(std::move(other.resource_)), owned_(other.owned_) {
other.owned_ = false;
}
const R& get() const { return resource_; }
};
// Factory pattern with concepts
template<typename T>
requires std::is_default_constructible_v<T>
class SimpleFactory {
public:
T create() {
return T{};
}
void destroy(T& object) {
// Custom destruction logic if needed
}
};
void demonstrateAdvancedConcepts() {
// Smart pointer concept
auto unique_ptr = std::make_unique<int>(42);
SmartPointerWrapper wrapper(std::move(unique_ptr));
// Factory concept
SimpleFactory<std::string> factory;
auto str = factory.create();
factory.destroy(str);
}
"""
)
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=cpp_concepts_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify advanced concept usage
all_relationships = cast(
MagicMock, mock_ingestor.ensure_relationship_batch
).call_args_list
call_relationships = [c for c in all_relationships if c.args[1] == "CALLS"]
# Look for concept-constrained template usage
advanced_calls = [
call for call in call_relationships if "advanced_concepts" in call.args[0][2]
]
assert len(advanced_calls) >= 5, (
f"Expected at least 5 advanced concept calls, found {len(advanced_calls)}"
)
def test_concept_composition_and_specialization(
cpp_concepts_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test concept composition, specialization, and nested concepts."""
test_file = cpp_concepts_project / "concept_composition.cpp"
test_file.write_text(
"""
#include <concepts>
#include <type_traits>
#include <iostream>
// Basic concepts
template<typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
// Concept specialization
template<typename T>
concept Number = Arithmetic<T> && !std::is_same_v<T, bool>;
template<Number T>
class Calculator {
public:
T calculate(T a, T b) {
return a + b;
}
};
// Nested concepts
namespace math {
template<typename T>
concept Numeric = std::is_arithmetic_v<T>;
template<Numeric T>
concept Positive = requires(T t) {
requires t > T{0};
};
template<Positive T>
T square_root_estimate(T value) {
return value / 2; // Simplified
}
}
// Concept composition
template<typename T>
concept Container = requires(T t) {
typename T::value_type;
{ t.begin() };
{ t.end() };
{ t.size() } -> std::convertible_to<std::size_t>;
};
template<typename T>
concept IterableContainer = Container<T> && requires(T t) {
std::begin(t);
std::end(t);
};
template<IterableContainer T>
void process_elements(const T& container) {
for (const auto& element : container) {
// Process element
}
}
void demonstrateConceptComposition() {
// Test numeric concepts
Calculator<double> calc;
auto sum = calc.calculate(1.5, 2.7);
// Test nested concepts
auto estimate = math::square_root_estimate(16.0);
// Test container concepts
std::vector<int> vec{1, 2, 3, 4, 5};
process_elements(vec);
}
"""
)
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=cpp_concepts_project,
parsers=parsers,
queries=queries,
)
updater.run()
# Verify concept relationships and namespace usage
all_relationships = cast(
MagicMock, mock_ingestor.ensure_relationship_batch
).call_args_list
# Look for namespace relationships
namespace_relationships = [
call
for call in all_relationships
if "concept_composition" in call.args[0][2] and "math" in str(call.args).lower()
]
assert len(namespace_relationships) >= 1, (
f"Expected namespace relationships for concepts, found {len(namespace_relationships)}"
)
# Verify template specializations
defines_relationships = [c for c in all_relationships if c.args[1] == "DEFINES"]
composition_definitions = [
call
for call in defines_relationships
if "concept_composition" in call.args[0][2]
]
assert len(composition_definitions) >= 5, (
f"Expected at least 5 concept composition definitions, found {len(composition_definitions)}"
)