Initial Commit
[packages] / xemacs-packages / semantic / tests / test.cpp
1 /* Test file for C++ language.
2  * Attempt to include as many aspects of the C++ language as possible.
3  * Do not include things tested in test.c since that shares the
4  * same language.
5  *
6  * $Id: test.cpp,v 1.1 2007-11-26 15:12:22 michaels Exp $
7  *
8  */
9
10 /* An include test */
11 #include <cmath>
12
13 #include "c++-test.hh"
14
15 #include <c++-test.hh>
16
17 #include <stdio.h>
18
19 double var1 = 1.2;
20
21 int simple1(int a) {
22
23 }
24
25 struct foo1 {
26   int test;
27 };
28
29 struct foo2 : public foo1 {
30   const int foo21(int a, int b);
31   const int foo22(int a, int b) { return 1 }
32 };
33
34 /* Classes */
35 class class1 {
36 private:
37   int var11;
38   struct foo1 var12;
39 public:
40   int p_var11;
41   struct foo p_var12;
42 };
43
44 class i_class1 : public class1 {
45 private:
46   int var11;
47   struct foo var12;
48 public:
49   int p_var11;
50   struct foo p_var12;
51 };
52
53 class class2 {
54 private:
55   int var21;
56   struct foo var22;
57 public:
58   int p_var21;
59   struct foo p_var22;
60 };
61
62 class i_class2 : public class1, public class2 {
63 private:
64   int var21;
65   struct foo var22;
66 protected:
67   int pt_var21;
68 public:
69   int p_var21;
70   struct foo p_var22;
71 };
72
73 class class3 {
74   /* A class with strange things in it */
75 public:
76   class3(); /* A constructor */
77   enum embedded_foo_enum {
78     a, b, c
79   } embed1;
80   struct embedded_bar_struct {
81     int a;
82     int b;
83   } embed2;
84   class embedded_baz_class {
85     embedded_baz_class();
86     ~embedded_baz_class();
87   } embed3;
88   ~class3(); /* destructor */
89   
90   /* Methods */
91   int method_for_class3(int a, char b);
92
93   int inline_method(int c) { return c; }
94
95   /* Operators */
96   class3& operator^= (const class3& something);
97
98   /* Funny declmods */
99   const class3 * const method_const_ptr_ptr(const int * const argconst) const = 0;
100 };
101
102 class3::class3()
103 {
104   /* Constructor outside the definition. */
105 }
106
107 int class3::method_for_class3(int a, char b)
108 {
109 }
110
111 int class3::method1_for_class3( int a, int &b)
112 {
113   int cvariablename;
114   class3 fooy[];
115
116   // Complktion testing line should find external members.
117   a = fooy[1].me ;
118   b = cv ;
119
120   if (fooy.emb) {
121     simple1(c);
122   }
123
124   cos(10);
125   abs(10);
126
127   return 1;
128 }
129
130 char class3::method2_for_class3( int a, int b) throw ( exception1 )
131 {
132   return 'a';
133 }
134
135 void *class3::method3_for_class3( int a, int b) throw ( exception1, exception2 )
136 {
137   int q = a;
138   return "Moose";
139 }
140
141 void *class3::method31_for_class3( int a, int b) throw ( )
142 {
143   int q = a;
144   return "Moose";
145 }
146
147 void *class3::method4_for_class3( int a, int b) reentrant
148 {
149   class3 ct;
150
151   ct.method5_for_class3(1,a)
152 }
153
154 /*
155  * A method on class3.
156  */
157 void *class3::method5_for_class3( int a, int b) const
158 {
159 }
160
161 // Stuff Klaus found.
162 // Inheritance w/out a specifying for public.
163 class class4 : class1 {
164   // Pure virtual methods.
165   void virtual print () const = 0;
166
167 public:
168   // The whacky constructor type
169   class4()
170     try : class1(args)
171   {
172     // constructor body 
173   }
174   catch ()
175     {
176       
177     }
178   
179
180 };
181
182 class class5 : public virtual class4 {
183   // Virtual inheritance
184 };
185
186 class class6 : class1 {
187   // Mutable
188   mutable int i;
189 };
190
191 /* Namespaces */
192 namespace namespace1 {
193   void ns_method1() { }
194
195   class n_class1 {
196   public:
197     void method11(int a) { }
198   };
199
200   /* This shouldn't parse due to missing semicolon. */
201   class _n_class2 : public n_class1 {
202     void n_c2_method1(int a, int b) { }
203   };
204
205   // Macros in the namespace
206 #define NSMACRO 1
207
208   // Template in the namespace
209   template<class T> T nsti1(const Foo& foo);
210   template<> int nsti1<int>(const Foo& foo);
211     
212 }
213
214 namespace namespace2 {
215
216   using namespace1::n_class1;
217
218 }
219
220 /* Initializers */
221 void tinitializers1(): inita1(False),
222                        inita2(False)
223 {
224   inita1= 1;
225 }
226
227 /* How about Extern C type things. */
228 int funny_prototype(int ,int b,float c)
229 {
230
231 }
232
233 extern "C"
234 int extern_c_1(int a, int b)
235 {
236
237   funny_prototype(1,2,3.4);
238
239   return 1;
240 }
241
242 extern "C" {
243
244   int extern_c_2(int a, int b)
245   {
246     return 1;
247   }
248
249 }
250
251 // Some operator stuff
252 class Action
253 {
254   // Problems!! operator() and operator[] can not be parsed with semantic
255   // 1.4.2 but with latest c.bnf
256   virtual void operator()(int i, char *p ) = 0;
257   virtual String& operator[]() = 0;
258   virtual void operator!() = 0;
259   virtual void operator->() = 0;
260   virtual T& operator+=();
261 };
262
263 // class with namespace qualified parents
264 class Multiinherit : public virtual POA::Parent,
265                      public virtual POA::Parent1,
266                      Parent
267 {
268 private:
269   int i;
270
271 public:
272   Multiinherit();
273   ~Multiinherit();
274
275   // method with a list of qualified exceptions
276   void* throwtest()
277     throw(Exception0,
278           Testnamespace::Exception1,
279           Testnamespace::Excpetion2,
280           Testnamespace::testnamespace1::Exception3);
281   
282 };
283
284 void*
285 Multiinherit::throwtest()
286   throw (Exception0,
287          Testnamespace::Exception1,
288          Testnamespace::Excpetion2,
289          Testnamespace::testnamespace1::Exception3)
290 {
291   return;
292 }
293
294 // Jens Rock <jens.rock@asamnet.de>: Nested classes or structs defined
295 // outside of the containing class/struct.
296 class container
297 {
298  public:
299   struct contained;
300   container();
301   ~container();
302 };
303
304 struct container::contained
305 {
306   public:
307   contained();
308   ~contained();
309 };
310
311 /*
312  * Ok, how about some template stuff.
313  */
314 template <class CT, class container = vector<CT> >
315 const CT& max (const CT& a, const CT& b)
316 {
317   return a < b ? b : a;
318 }
319
320 class TemplateUsingClass
321 {
322   typedef TestClassMap::iterator iterator;
323   typedef map<long, long> TestClassMap;
324
325   // typedefs with const and volatile
326   typedef const map<long, long> const_TestClassMap;
327   typedef TestClassMap<string>::iterator volatile volatile_iterator;
328
329   map<int, int> mapclassvarthingy;
330 };
331
332 template<class T> T ti1(const Foo& foo);
333 template<> int ti1<int>(const Foo& foo);
334
335
336 // -----------------------------------
337 // Now some namespace and related stuff
338 // -----------------------------------
339
340 using CORBA::LEX::get_token;
341 using Namespace1;
342
343 using namespace POA::std;
344 using namespace Test;
345
346
347
348 namespace Parser
349 {
350   namespace
351   {
352     using Lexer::get_test;
353     string str = "";
354   }
355   
356   namespace XXX
357   {
358     
359     class Foobar : public virtual POA::Parent,
360                    public virtual POA::Parent1,
361                    private POA::list<fact>,
362                    private map<string>
363     {
364       ini i;
365       list <shared_ptr<item> >::const_iterator l;
366     public:
367       
368       Foobar();
369       ~Foobar();
370     };
371   }
372   
373
374   void test_function(int i);
375     
376 };
377
378 // unnamed namespaces - even nested
379 namespace
380 {
381   namespace
382   {
383     using Lexer::get_test;
384     string str = "";
385   }
386
387   // some builtin types
388   long long ll = 0;
389   long double d = 0.0;
390   unsigned test;
391   unsigned long int **uli = 0;
392   signed si = 0;
393   signed short ss = 0;
394   short int i = 0;
395   long int li = 0;
396   
397   // expressions with namespace/class-qualifyiers
398   ORB_var cGlobalOrb = ORB::_nil();
399   ORB_var1 cGlobalOrb1 = ORB::_test;
400
401   class Testclass
402   {
403     #define TEST 0
404     ini i;
405
406   public:
407
408     Testclass();
409     ~Testclass();
410   };
411
412   static void test_function(unsigned int i);
413
414 };
415
416
417 // outside method implementations which should be grouped to type Test
418 XXX&
419 Test::waiting()
420 {
421   return;
422 }
423
424 void
425 Test::print()
426 {
427   return;
428 }
429
430 // outside method implementations with namespaces which should be grouped to
431 // their complete (incl. namespace) types
432 void*
433 Parser::XXX::Foobar::wait(int i, const char const * const * p)
434 {
435   return;
436 }
437
438 void*
439 Namespace1::Test::wait1(int i)
440 {
441   return;
442 }
443
444 int
445 Namespace1::Test::waiting(int i)
446 {
447   return;
448 }
449
450 // a class with some outside implementations which should all be grouped to
451 // this class declaration
452 class ClassWithExternals
453 {
454 private:
455   int i;
456
457 public:
458   ClassWithExternals();
459   ~ClassWithExternals();
460   void non_nil();
461 };
462
463
464 // Foobar is not displayed; seems that semantic tries to add this to the class
465 // Foobar but can not find/display it, because contained in the namespace above.
466 void
467 Foobar::non_nil()
468 {
469   return;
470 }
471
472 // are correctly grouped to the ClassWithExternals class
473 void
474 ClassWithExternals::non_nil()
475 {
476   String s = "lödfjg dlfgkdlfkgjdl";
477   return;
478 }
479
480 ClassWithExternals::ClassWithExternals()
481 {
482   return;
483 }
484
485 void
486 ClassWithExternals::~ClassWithExternals()
487 {
488   return;
489 }
490
491
492 // -------------------------------
493 // Now some macro and define stuff
494 // -------------------------------
495
496 #define TEST 0
497 #define TEST1 "String"
498
499 // The first backslash makes this macro unmatched syntax with semantic 1.4.2!
500 // With flexing \+newline as nothing all is working fine!
501 #define MZK_ENTER(METHOD) \
502 { \
503   CzkMethodLog lMethodLog(METHOD,"Framework");\
504 }
505
506 #define ZK_ASSERTM(METHOD,ASSERTION,MESSAGE) \
507    { if(!(ASSERTION))\
508       {\
509         std::ostringstream lMesgStream; \
510         lMesgStream << "Assertion failed: " \
511         << MESSAGE; \
512         CzkLogManager::doLog(CzkLogManager::FATAL,"",METHOD, \
513         "Assert",lMesgStream); \
514         assert(ASSERTION);\
515       }\
516    }
517
518 // Test if not newline-backslashes are handled correctly
519 string s = "My \"quoted\" string";
520
521 // parsed fine as macro
522 #define FOO (arg) method(arg, "foo");
523
524 // With semantic 1.4.2 this parsed as macro BAR *and* function method.
525 // With latest c.bnf at least one-liner macros can be parsed correctly.
526 #define BAR (arg) CzkMessageLog method(arg, "bar");
527
528 // some const and volatile stuff
529 char * p1 = "Hello"; // 1. variable Pointer, variable Data
530 const char * p2 = "Hello"; // 2. variable pointer, constant data
531 char * const p3 = "Hello"; // 3. constant pointer, variable data
532 const char * const p4 = "Hello"; // 4. constant pointer, constant data
533
534 // Case 2 and 4 can exchange first "const" and "char"
535 char const * p21 = "Hello"; // variable pointer, constant data
536 char const * const p41 = "Hello"; // constant pointer, constant data
537
538 char volatile a = 0; // a volatile char
539 void foo(bar const &arg); // a reference to a const bar
540 int foobar(bar const * const p); // a const pointer to a const bar
541 int foobar(bar const volatile * const p); // a const pointer to a const bar
542 int foobar3(char* p); // a const pointer to a const bar
543
544 // Should not be parsed because this is invalid code
545 int const & const r3 = i;
546
547 boolean i = 0;
548 boolean & r1 = i;
549 boolean const & r2 = i;
550
551 // const * sequences can be very long in C++ ;-)
552 char const * const * const * const * ppp;
553
554 // complex function declarationen with named pointer-arguments
555 const char** foobar1(volatile char const * const **p);
556 const char** foobar11(volatile Test::Namespace::Char<char*> const * const **p);
557
558 // complex function declarationen with unnamed pointer-arguments
559 const char* foobar2(const char***);
560 const char* foobar21(const Test::Namespace::Char<char>***);
561
562 // string literal parsing even with wchar_t
563 char const *p = "string1";
564 char const *q = "string1" "str\"ing2" "string3";
565 wchar_t testc = L'a';
566
567 wchar_t const *wp = L"string with a \" in it";
568 wchar_t const *wq = L"string \n\t\"test" L"string2";
569 wchar_t const *wr = L"string L";