PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/synopsis-0.12/tests/old_cxx_regressions/regressions.py

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