new tests

From-SVN: r16426
This commit is contained in:
Jason Merrill 1997-11-11 01:38:34 -05:00
parent 343fdf03d4
commit 1afc355025
29 changed files with 871 additions and 0 deletions

View File

@ -0,0 +1,45 @@
// Testcase for proper handling of rethrow.
#include <stdio.h>
int c, d;
struct A
{
int i;
A () { i = ++c; printf ("A() %d\n", i); }
A (const A&) { i = ++c; printf ("A(const A&) %d\n", i); }
~A() { printf ("~A() %d\n", i); ++d; }
};
int
main ()
{
try
{
try
{
printf ("Throwing 1...\n");
throw A();
}
catch (A)
{
try
{
printf ("Throwing 2...\n");
throw A();
}
catch (A)
{
printf ("Throwing 3...\n");
throw;
}
}
}
catch (A)
{
printf ("Caught.\n");
}
printf ("c == %d, d == %d\n", c, d);
return c != d;
}

View File

@ -0,0 +1,45 @@
// Testcase for proper handling of rethrow.
#include <stdio.h>
int c, d;
struct A
{
int i;
A () { i = ++c; printf ("A() %d\n", i); }
A (const A&) { i = ++c; printf ("A(const A&) %d\n", i); }
~A() { printf ("~A() %d\n", i); ++d; }
};
int
main ()
{
try
{
try
{
printf ("Throwing 1...\n");
throw A();
}
catch (A)
{
try
{
printf ("Throwing 2...\n");
throw;
}
catch (A)
{
printf ("Throwing 3...\n");
throw;
}
}
}
catch (A)
{
printf ("Caught.\n");
}
printf ("c == %d, d == %d\n", c, d);
return c != d;
}

View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <exception>
static void
eh_terminate ()
{
printf ("CALLING TERMINATE\n");
exit (1);
}
void
eh_test (int level)
{
try
{
if (level < 2)
eh_test (level + 1);
else
{
printf ("%d: Throwing\n", level);
throw (level);
}
}
catch (int &x)
{
printf ("%d: Got level %d\n",
level, x);
if (level > 0)
throw;
}
}
main ()
{
set_terminate (&eh_terminate);
eh_test (0);
}

View File

@ -0,0 +1,42 @@
// Testcase for order of destruction.
// Special g++ Options: -O2
extern "C" int printf( char const*, ... );
int c;
int r;
struct B {
B();
B( B const& );
~B();
};
struct A {
A();
A( A const& );
~A();
operator B ();
};
inline A::operator B () { printf( "operator B ()\n"); return B(); }
A f();
void g( B const& );
int
main()
{
g( f() );
return r;
}
B::B() { printf( "B::B()\n" ); if (++c != 2) r = 1; }
B::B( B const& ) { printf( "B::B( B const& )\n" ); r = 1; }
B::~B() { printf( "B::~B()\n" ); if (--c != 1) r = 1; }
A::A() { printf( "A::A()\n" ); if (++c != 1) r = 1; }
A::A( A const& ) { printf( "A::A( A const& )\n" ); r = 1; }
A::~A() { printf( "A::~A()\n" ); if (--c != 0) r = 1; }
A f() { printf( "f()\n"); return A(); }
void g( B const& ) { printf( "g()\n"); }

View File

@ -0,0 +1,15 @@
// Test for subsequence checking in overload resolution.
class foo {
public:
void operator <<(char *) { }
void operator <<(const char * const &);
};
int
main()
{
char s[20];
foo f;
f << s;
}

View File

@ -0,0 +1,57 @@
// PRMS id: g++/13340
// Build don't link:
class rectangle {
public:
rectangle();
int overlaps() const;
};
class region
{
friend class region_impl;
public:
region();
typedef int (region::* region_func)() const;
};
class region_impl {
friend class region;
private:
rectangle content, mbb;
region_impl *link_p;
region_impl(const rectangle &content);
public:
int iterate(region *region_p, region::region_func what,
const rectangle &clip_rect) const;
int iterate(region *region_p, region::region_func what,
const region_impl &clip_rgn) const;
};
int
region_impl::iterate (region *region_p, region::region_func what,
const rectangle &clip_rect) const
{
for (const region_impl *p = this; p != 0 && p->mbb.overlaps();
p = p->link_p)
if (p->content.overlaps())
if (!(region_p->*what)()) return 0;
return 1;
}
int
region_impl::iterate (region *region_p, region::region_func what,
const region_impl &clip_rgn) const
{
for (const region_impl *p = this; p != 0 && p->mbb.overlaps();
p = p->link_p)
if (!clip_rgn.iterate(region_p, what, p->content)) return 0;
return 1;
}

View File

@ -0,0 +1,61 @@
#include <stdlib.h>
#include <string.h>
template <class T>
class List {
public:
int len;
T *array;
int length() const { return( len ); }
List() : len( 0 ), array( 0 ) {}
};
template <class T>
int AlgoStdCompare(const T* a, const T* b) {
if (*a < *b)
return -1;
else
return (*a > *b); // 0 if equal, 1 if greater
}
int AlgoStdCompare(const char* const* a, const char * const*b)
{
return strcmp(*a,*b);
}
template <class T>
void AlgoFixupSort(List< T >* , int, int ) {
}
template <class T>
void AlgoSort(int (*compare)(const T *, const T *),
void (*fixup)( List<T> *, int first, int last),
List< T >* theList, int first, int last) {
if (last < 0)
last = theList->length()-1;
qsort(theList->array+first, last-first+1, sizeof(T),
(int (*)(const void *, const void *))compare);
if (fixup)
fixup(theList, first, last);
}
template <class T>
void AlgoSort(List< T >* theList, int first = 0, int last = -1) {
int (*compare)(const T*, const T*) = AlgoStdCompare;
void (*fixup)( List<T> *, int first, int last) = AlgoFixupSort;
AlgoSort(compare, fixup, theList, first, last);
}
int
main()
{
List<const char *> slist;
AlgoSort( &slist );
List<int> ilist;
AlgoSort( &ilist );
}

View File

@ -0,0 +1,39 @@
// This testcase ensures that we can build vtable names for complex MI
// classes.
class C_A {
public:
virtual int foo(void *) { }
} a;
class C_B : public C_A {
} b;
class C_C : public C_A {
} c;
class C_D : public C_A {
} d;
class C_E : public C_C, public C_B {
public:
virtual int foo(void *) { }
} e;
class C_F : public C_D, public C_B {
public:
virtual int foo(void *) { }
} f;
class C_G : public C_A {
public:
virtual int foo(void *) { }
} g;
class C_H : public C_G, public C_E, public C_F {
public:
virtual int foo(void *) { }
} h;
int main() {
}

View File

@ -0,0 +1,15 @@
extern "C" void abort ();
template <class T> int f ()
{
return sizeof(T);
}
int main ()
{
if (f<long> () != sizeof(long)
|| f<char> () != sizeof(char)
|| f<long> () != sizeof(long)
|| f<long int> () != sizeof(long int))
abort ();
}

View File

@ -0,0 +1,18 @@
extern "C" void abort ();
template <int a> int fact ()
{
return 0;
}
template <> int fact<1> ()
{
return 1;
}
int main()
{
if (fact<3> () != 0 || fact<1> () != 1
|| fact<3> () != 0 || fact<1> () != 1 || fact<1+0> () != 1)
abort ();
}

View File

@ -0,0 +1,18 @@
extern "C" void abort ();
template <int a> inline int fact ()
{
return a * fact<a-1> ();
}
template <> inline int fact<1> ()
{
return 1;
}
int main()
{
if (fact<3> () != 6 || fact<1> () != 1
|| fact<3> () != 6 || fact<1> () != 1 || fact<1+0> () != 1)
abort ();
}

View File

@ -0,0 +1,21 @@
extern "C" void abort ();
template <int a> inline int fact ();
template <> inline int fact<1> ();
template <int a> inline int fact ()
{
return a * fact<a-1> ();
}
template <> inline int fact<1> ()
{
return 1;
}
int main()
{
if (fact<3> () != 6 || fact<1> () != 1
|| fact<3> () != 6 || fact<1> () != 1 || fact<1+0> () != 1)
abort ();
}

View File

@ -0,0 +1,35 @@
extern "C" void abort ();
template <int a> inline int fact2 ();
template <int a> inline int fact ()
{
return a * fact2<a-1> ();
}
template <> inline int fact<1> ()
{
return 1;
}
template <int a> inline int fact2 ()
{
return a*fact<a-1>();
}
template <> inline int fact2<1> ()
{
return 1;
}
int main()
{
if (fact<3> () != 6 || fact<1> () != 1
|| fact<3> () != 6 || fact<1> () != 1 || fact<1+0> () != 1)
abort ();
if (fact2<3> () != 6 || fact2<1> () != 1
|| fact2<3> () != 6 || fact2<1> () != 1 || fact2<1+0> () != 1)
abort ();
if (fact2<4> () != 24 || fact<4> () != 24)
abort ();
}

View File

@ -0,0 +1,14 @@
template <class T> T* create ()
{
return new T;
}
template <class T> T* create2()
{
return create<T>();
}
int main()
{
int *p = create2<int>();
}

View File

@ -0,0 +1,16 @@
template <class T> T* create ();
template <class T> T* create2()
{
return create<T>();
}
template <class T> T* create ()
{
return new T;
}
int main()
{
int *p = create2<int>();
}

View File

@ -0,0 +1,42 @@
extern "C" void abort ();
int a = 0;
template <class T> void f ();
template <class T> void g ()
{
if (a)
abort ();
}
template <> void g<char> ()
{
}
template <class T> class C
{
public:
void ff () { f<T> (); }
void gg () { g<T> (); }
};
template <class T> void f ()
{
if (a)
abort ();
}
template <> void f<char> ()
{
}
int main ()
{
C<int> c;
c.ff();
c.gg();
a = 1;
C<char> d;
d.ff();
d.gg();
}

View File

@ -0,0 +1,41 @@
extern "C" void abort ();
template <class T> void f ();
template <class T> void g ()
{
abort ();
}
template <> void g<char> ()
{
abort ();
}
template <class T> class C
{
public:
template <class U> void f () {}
template <class U> void g () {}
void ff () { f<T> (); }
void gg () { g<T> (); }
};
template <class T> void f ()
{
abort ();
}
template <> void f<char> ()
{
abort ();
}
int main ()
{
C<int> c;
c.ff();
c.gg();
C<char> d;
d.ff();
d.gg();
}

View File

@ -0,0 +1,41 @@
extern "C" void abort ();
template <class T> void f ();
template <class T> void g ()
{
abort ();
}
template <> void g<char> ()
{
abort ();
}
template <class T> class C
{
public:
void ff () { f<T> (); }
void gg () { g<T> (); }
template <class U> void f () {}
template <class U> void g () {}
};
template <class T> void f ()
{
abort ();
}
template <> void f<char> ()
{
abort ();
}
int main ()
{
C<int> c;
c.ff();
c.gg();
C<char> d;
d.ff();
d.gg();
}

View File

@ -0,0 +1,43 @@
extern "C" void abort ();
template <class T> void f ();
template <class T> void g ()
{
abort ();
}
template <> void g<char> ()
{
abort ();
}
template <class T> class C
{
public:
void ff () { f<T> (); }
void gg () { g<T> (); }
template <class U> void f () {}
template <class U> void g () {}
template <class U> void f (int) { abort(); }
template <class U> void g (int) { abort(); }
};
template <class T> void f ()
{
abort ();
}
template <> void f<char> ()
{
abort ();
}
int main ()
{
C<int> c;
c.ff();
c.gg();
C<char> d;
d.ff();
d.gg();
}

View File

@ -0,0 +1,43 @@
extern "C" void abort ();
template <class T> void f ();
template <class T> void g ()
{
abort ();
}
template <> void g<char> ()
{
abort ();
}
template <class T> class C
{
public:
void ff () { f<T> (0); }
void gg () { g<T> (1); }
template <class U> void f () { abort(); }
template <class U> void g () { abort(); }
template <class U> void f (int) {}
template <class U> void g (int) {}
};
template <class T> void f ()
{
abort ();
}
template <> void f<char> ()
{
abort ();
}
int main ()
{
C<int> c;
c.ff();
c.gg();
C<char> d;
d.ff();
d.gg();
}

View File

@ -0,0 +1,19 @@
extern "C" void abort ();
template <class T> void f ()
{
}
template <class T> class C
{
friend void f<char> ();
public:
void ff () { f<char> (); }
};
int main ()
{
C<int> c;
c.ff();
}

View File

@ -0,0 +1,19 @@
extern "C" void abort ();
template <class T> void f ()
{
}
template <class T> class C
{
friend void f<T> ();
public:
void ff () { f<T> (); }
};
int main ()
{
C<int> c;
c.ff();
}

View File

@ -0,0 +1,23 @@
extern "C" void abort ();
template <class T> void f ()
{
abort ();
}
template <> void f<char> ()
{
}
template <class T> class C
{
friend void f<char> ();
public:
void ff () { f<char> (); }
};
int main ()
{
C<int> c;
c.ff();
}

View File

@ -0,0 +1,33 @@
extern "C" void abort ();
template <class T> void f ()
{
abort ();
}
template <> void f<char> ()
{
abort ();
}
template <class T> void f (int)
{
abort ();
}
template <> void f<char> (int)
{
}
template <class T> class C
{
friend void f<char> (int);
public:
void ff () { f<char> (0); }
};
int main ()
{
C<int> c;
c.ff();
}

View File

@ -0,0 +1,13 @@
template <class T> struct A {
template <class U> void f (U u);
};
A<int> a;
template <class T> template <class U> void A<T>::f (U u) { }
main()
{
a.f (24);
}

View File

@ -0,0 +1,26 @@
template<class T_type, int N>
class foo {
public:
enum bar { z = 0 };
};
template<int N>
class foo<double, N> {
public:
enum bar { z = 1 };
};
template<class T_type>
class foo<T_type, 2> {
public:
enum bar { z = 2 };
};
int main()
{
if ((foo<int,3>::z == 0) && (foo<double,3>::z == 1)
&& (foo<float,2>::z == 2))
return 0;
else
return 1;
}

View File

@ -0,0 +1,18 @@
// Testcase for handling of typedef wierdness.
// Build don't link:
template <class T>
class A
{
typedef enum
{
foo
} B;
A (B b);
};
template <class T>
A<T>::A (B b)
{
}

View File

@ -0,0 +1,17 @@
// Build don't link:
// GROUPS passed templates
template <class T>
struct bar {
typedef typename T::baz baz;
};
template <class T>
void foo(T)
{
bar<T>::baz(); // ERROR - T is int.
}
void foobar()
{
foo(3);
}

View File

@ -0,0 +1,14 @@
// Tests non-unification of parms that don't use template parms.
// Build don't link:
enum kind {a, b};
class C { public: C () {} };
template<class P>
void f (P c, kind k) {}
template<class P>
void f (P c, P d, kind k) {}
template void f (C c, C c, kind k);