Synopsis - Cross-Reference
File: /tests/old_cxx_regressions/regressions.py1## regressions.py 2## Copyright (c) 2002 by Stephen Davies 3## 4## This file contains the regression tests for the C++ parser 5## Each one is in it's own class. 6## The tests are threefold: 7## 1. That the test parses w/o errors 8## 2. Testing of name resolution using link tests 9## eg; x = i; // test i = "::i" 10## will check that the generated i is linked to the global i variable 11## 3. Testing of AST structure, using separate list of checks 12## eg: class Foo has method "instance" returns "Foo*" params () 13## will check that there is a class Foo, with a method called instance 14## that returns a Foo* and has no parameters 15 16from test import * 17 18class Comments (Regression, Test): 19 test = """// 1. Comments Test: One line cpp test 20 21// 2. Two line cpp test 22// Second line of test 23 24/* 3. One line c test */ 25/* 4. Another c test */ 26/* 5. Two on one */ /* 6. line */ 27/* 7. Multi 28 * line 29 */ 30""" 31 32class CommentProximity (Regression, Test): 33 test = """// Comment at start 34 35int test1; 36 37// Test2: This should have a comment, unlike test1 38// It should also have two lines 39int test2; 40 41/* A comment by itself 42 * Over multiple lines. */ 43 44int test3; 45 46/* This comment should be ignored. 47 */ 48/* Test4: Should have a comment 49 * */ 50int test4; 51 52/* Test5: This should have a comment, unlike test 3 53 * Which had a gap. 54 */ 55int test5; 56//< This should still be tail-appended! 57""" 58 59class IfTest (Regression, Test): 60 test = """// If test 61void func() { 62 int x = 0; 63 if (x) cout << "Hi"; // test x = "`func()::x" 64 if (x == 2) cout << "hi"; // test x = "`func()::x" 65 if (!x) cout << "foo"; // test x = "`func()::x" 66 if (x) cout << "one"; else cout << "two"; 67 if (x) {cout << "one"; cout << "two"; } else { cout<<"three"; } 68} 69""" 70 71class ForTest (Regression, Test): 72 test = """ 73void func() { 74 int x; 75 for (int x = 3, y=10; x < y; x++) { 76 cout << x; 77 } 78} 79""" 80 81class UsingTest (Regression, Test): 82 test = """ 83namespace Foo { 84 int x; 85} 86void func() { 87 using namespace Foo; 88 x; 89} 90void func2() { 91 using namespace Foo = Bar; 92 Bar::x; 93} 94void func3() { 95 Foo::x; 96} 97void func4() { 98 using Foo::x; 99 x; 100} 101""" 102 103class UsingTest2 (Regression, Test): 104 test = """// From C++WD'96 7.4.3.2 Example 105namespace A { 106 int i; 107 namespace B { 108 namespace C { 109 int i; 110 } 111 using namespace A::B::C; 112 void f1() { 113 i = 5; // C::i hides A::i 114 } 115 } 116 namespace D { 117 using namespace B; 118 using namespace C; 119 void f2() { 120 i = 5; // ambiguous, B::C::i or A::i 121 } 122 } 123 void f3() { 124 i = 5; // uses A::i 125 } 126} 127void f4() { 128 i = 5; // ill-formed, neither i visible 129} 130""" 131 132class UsingTest3 (Regression, Test): 133 test = """// From C++WD'96 7.4.3.3 Example 2 134namespace A { 135 int i; 136} 137namespace B { 138 int i; 139 int j; 140 namespace C { 141 namespace D { 142 using namespace A; 143 int j; 144 int k; 145 int a = i; // B::i hides A::i 146 } 147 using namespace D; 148 int k = 89; // no problem yet 149 int l = k; // ambiguous: C::k or D::k 150 int m = i; // B::i hides A::i 151 int n = j; // D::j hides B::j 152 } 153} 154""" 155 156class UsingTest4 (Regression, Test): 157 test = """// From C++WD'96 7.4.3.6 Example 158namespace D { 159 int d1; 160 void f(char); 161} 162using namespace D; 163int d1; // ok, no conflict with D::d1 164namespace E { 165 int e; 166 void f(int); 167} 168namespace D { // namespace extension 169 int d2; 170 using namespace E; 171 void f(int); 172} 173void f() 174{ 175 d1++; // error: ambiguous: ::d1 or D::d1 176 ::d1++; // ok 177 D::d1++; // ok 178 d2++; // ok D::d2 179 e++; // ok E::e 180 f(1); // error: ambiguous: D::f(int) or E::f(int) 181 f('a'); // ok: D::f(char) 182} 183""" 184 185class CastTest (Regression, Test): 186 test = """ 187typedef int Foo; 188void func() { 189 (Foo)1; 190 (Foo*)1; 191 (const Foo&)1; 192} 193""" 194 195class TryTest (Regression, Test): 196 test = """ 197void func() { 198 try { 199 cout << bar; 200 } 201 catch (string foo) { 202 cout << "Error: " << foo << endl; 203 } 204 catch (...) { 205 cout << "Catchall"; 206 } 207} 208""" 209 210class MacroTest (Regression, Test): 211 test = """ 212int x; 213#define LONGER 12345678 214#define SHORTER 1234 215#define LINKINSIDE x 216#define ARGS(a, b, c) x 217int A = LONGER, A2 = x; 218int B = SHORTER, B2 = x; 219int C = LINKINSIDE, C2 = x; 220int D = ARGS(1, 2, 3), D2 = x; 221""" 222 223class FuncTest (Regression, Test): 224 test = """ 225void func(char); 226void func(int); 227void func(double); 228void func(const char*); 229void test() { 230 func('c'); 231 func(123); 232 func(1.2); 233 func("s"); 234}""" 235 236class OperTest (Regression, Test): 237 test = """ 238struct A {}; 239struct B {}; 240A operator +(const B&, const B&); 241int operator +(const A&, const A&); 242void func(A); 243void func(B); 244void func(int); 245void main() { 246 B x, y; 247 func( (x + y) + (x + y) ); // should call func(int), test func = "func(int)" 248} 249""" 250 251class KoenigTest (Regression, Test): 252 test = """ 253namespace NS { 254 struct A {}; 255 int operator +(A, A); 256}; 257void func(int); 258void func(NS::A) { 259 NS::A x, y; 260 func(x + y); // should call func(int) 261} 262""" 263 264class TemplateTest (Regression, Test): 265 test = """ 266struct Object { 267 float f; 268 double func(); 269 Object(); 270 Object(const Object&); 271 Object& operator = (const Object&); 272}; 273 274template <typename T> 275class list { 276 T* m_array; 277 int m_size; 278public: 279 list(); 280 list(T*); 281 282 T& operator [] (int index) { return m_array[index]; } 283 int size() { return m_size; } 284 285 void replace(int index, const T& with) { m_array[index] = with; } 286}; 287 288void main() { 289 list<Object> a_list; 290 a_list.replace(1, Object()); 291 Object b(a_list[1]); 292 a_list[2].func(); 293 b = a_list[3]; 294} 295""" 296 297class TemplateSpecTest (Regression, Test): 298 test = """// Test template specializations 299template <typename T> 300class list { 301public: 302 list(T*, int size); 303}; 304 305template <> 306class list<void> { 307public: 308 list(void*, int size) {} 309}; 310 311template <> 312class list<int> { 313public: 314 list(int*, int size) {} 315}; 316""" 317 318class TemplateSpecTest2 (Regression, Test): 319 test = """// Test template specializations #2 320template <typename T, int I = 4> 321class list { 322public: 323 list(T*, int size); 324 int size() { return I; } 325}; 326 327template <typename T> 328class list<T,0> { 329public: 330 list(void*, int size) {} 331 int size() { return 0; } 332}; 333 334template <int I> 335class list<int, I> { 336public: 337 list(int*, int size) {} 338 int size() { return I; } 339}; 340""" 341 342class StdTest (Regression, Test): 343 flags = gcc_include + python_include + "-Wp,-f " 344 test = """ 345#include <vector> 346namespace Foo { 347 void func(std::vector<int> array); 348} 349""" 350 351class StaticCastTest (Regression, Test): 352 test = """ 353typedef unsigned int size_type; 354int really_big_number = static_cast<size_type>(-1); 355""" 356 357class InlineConstrTest (Regression, Test): 358 test = """ 359namespace std { struct type_info {}; } 360struct type_info { 361 inline type_info(std::type_info const& = typeid(void)); 362}; 363""" 364 365class NestedMacroTest (Regression, Test): 366 test = """ 367#define CAT(a, b) CAT_D(a, b) 368#define CAT_D(a, b) a ## b 369 370#define AB(x, y) CAT(x, y) 371 372// There should be a variable XY here 373int 374CAT(A, B)(X, Y) 375; 376""" 377 378class TypenameTest (Regression, Test): 379 test = """ 380template < std::size_t Bits, typename ::boost::uint_t<Bits>::fast TruncPoly > 381class crc_optimal 382{ 383 // Implementation type 384 typedef detail::mask_uint_t<Bits> masking_type; 385 386public: 387 // Type 388 typedef typename masking_type::fast value_type; 389 390 // Constants for the template parameters 391 static const std::size_t bit_count = Bits ; 392}; 393""" 394 395class ConcatTest (Regression, Test): 396 test = """ 397// According to spec, the ## operator must produce a valid PP token. 398// Unfortunately, no compilers known to man enforce this rule except UCPP, so 399// this test is to make sure it is 'fixed' to ignore the error. 400#define CAT(a,b) a ## b 401 402class Foo {}; 403void CAT(operator,+) (const Foo&, const Foo&) {} 404void operator CAT(+,=) (const Foo&, const Foo&) {} 405""" 406 407class FuncTemplTest (Regression, Test): 408 test = """ 409// Test function templates 410 411// Test template arg 412template <class A> 413int func1(A a) { return 0; }; 414 415// Test template return 416template <class A> 417A func2(int i) { return 0; }; 418 419// Test template arg and return 420template <class A> 421A func2(A a) { return 0; }; 422 423// Test template arg and return w/ different types 424template <class A, class B> 425B func2(A a) { return 0; }; 426 427// Test template arg and return w/ different types. Function declaration 428template <class A, class B> 429B func2(A a); 430""" 431 432class FuncTemplArgTest (Regression, Test): 433 test = """ 434// Test function pointers as template arguments 435 436template <class Foo> 437struct function { }; 438 439// Test template class with function argument 440// (not really a template function) 441template <class A1, class A2> 442struct function<void (A1,A2)> { }; 443 444// Test return type fptr 445template <class R> 446struct function<R (void)> { }; 447 448// Test return type fptr 449template <class R> 450struct function<R (int, int)> { }; 451""" 452 453class FuncPtrTest (Regression, Test): 454 test = """ 455// Tests function pointers 456 457typedef void PyObject; 458 459// A function which takes a func ptr as a parameter 460void insert(void* (*convert)(PyObject*), bool yesno); 461 462// A function which takes a func ptr as a parameter 463void insert(void* (*convert)(PyObject*, int), bool yesno); 464 465// A function which returns a func ptr 466void* (*insert2(int))(PyObject*); 467 468int main() { 469 (void)insert2(1); 470} 471""" 472 473class ForwardClassTest (Regression, Test): 474 test = """ 475// Tests a particular case where forward is found by a using directive. 476 477namespace Prague { 478struct Fork { 479 struct Process; 480}; 481} 482using namespace Prague; 483struct Fork::Process { }; 484""" 485 486class MemberPointerTest (Regression, Test): 487 test = """ 488class X { 489public: 490 void f(int); 491 int a; 492}; 493class Y; 494 495int X::* pmi = &X::a; 496void (X::* pmf)(int) = &X::f; 497double X::* pmd; 498char Y::* pmc; 499X obj; 500X* pobj; 501void foo() 502{ 503 obj.*pmi = 7; // assign 7 to an integer 504 // member of obj 505 (obj.*pmf)(7); // call a function member of obj 506 // with the argument 7 507 pobj->*pmi = 7; // assign 7 to an integer 508 (pobj->*pmf)(7); // call a function member of obj 509} 510""" 511 512class ConditionTest (Regression, Test): 513 test = """ 514// Tests the ability to use a declaration in the condition of an if or switch 515struct X { 516 operator bool() const {return true;} 517 struct Y { 518 operator bool() const {return true;} 519 }; 520}; 521void foo() 522{ 523 int _i; 524 X _x; 525 X::Y _y; 526 527 if (int i = 3) {} 528 if (const int i = 3) {} 529 if (const int* i = &_i) {} 530 if (int* const i = &_i) {} 531 532 if (X foo = _x) {} 533 if (const X foo = _x) {} 534 if (const X* foo = &_x) {} 535 if (X* const foo = &_x) {} 536 537 if (X::Y foo = _y) {} 538 if (const X::Y foo = _y) {} 539 if (const X::Y* foo = &_y) {} 540 if (X::Y* const foo = &_y) {} 541 542 switch (int i = 3) {} 543 switch (const int i = 3) {} 544 545} 546""" 547 548# vim: set et sts=2 ts=8 sw=2: