summaryrefslogtreecommitdiffstats
path: root/libcxxabi/test/test_vector1.cpp
diff options
context:
space:
mode:
authorMarshall Clow <mclow@qualcomm.com>2011-06-10 03:40:19 +0000
committerMarshall Clow <mclow@qualcomm.com>2011-06-10 03:40:19 +0000
commit280ddee8bded79832e101e6874743a487f00bde7 (patch)
treec7131850a1eb360ba39f46bf95b776615a321cf2 /libcxxabi/test/test_vector1.cpp
parentcaa33d36fb3f126d9cbe854793ca617d05170255 (diff)
downloadbcm5719-llvm-280ddee8bded79832e101e6874743a487f00bde7.tar.gz
bcm5719-llvm-280ddee8bded79832e101e6874743a487f00bde7.zip
Implement vector new and delete functionality
llvm-svn: 132832
Diffstat (limited to 'libcxxabi/test/test_vector1.cpp')
-rw-r--r--libcxxabi/test/test_vector1.cpp207
1 files changed, 207 insertions, 0 deletions
diff --git a/libcxxabi/test/test_vector1.cpp b/libcxxabi/test/test_vector1.cpp
new file mode 100644
index 00000000000..2a539d4d4a3
--- /dev/null
+++ b/libcxxabi/test/test_vector1.cpp
@@ -0,0 +1,207 @@
+//===---------------------------- test_vector.cpp -------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "cxxabi.h"
+
+#include <iostream>
+#include <cstdlib>
+
+// Wrapper routines
+void *my_alloc2 ( size_t sz ) {
+ void *p = std::malloc ( sz );
+// std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
+ return p;
+ }
+
+void my_dealloc2 ( void *p ) {
+// std::printf ( "Freeing %lx\n", (unsigned long) p );
+ std::free ( p );
+ }
+
+void my_dealloc3 ( void *p, size_t sz ) {
+// std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
+ std::free ( p );
+ }
+
+void my_construct ( void *p ) {
+// std::printf ( "Constructing %lx\n", (unsigned long) p );
+ }
+
+void my_destruct ( void *p ) {
+// std::printf ( "Destructing %lx\n", (unsigned long) p );
+ }
+
+int gCounter;
+void count_construct ( void *p ) { ++gCounter; }
+void count_destruct ( void *p ) { --gCounter; }
+
+
+int gConstructorCounter;
+int gConstructorThrowTarget;
+int gDestructorCounter;
+int gDestructorThrowTarget;
+void throw_construct ( void *p ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
+void throw_destruct ( void *p ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; }
+
+struct vec_on_stack {
+ void *storage;
+ vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {}
+ ~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); }
+ };
+
+// Test calls with empty constructors and destructors
+int test_empty ( ) {
+ void *one, *two, *three;
+
+// Try with no padding and no con/destructors
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, NULL, NULL );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, NULL, NULL, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, NULL, NULL, my_alloc2, my_dealloc3 );
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 0, NULL );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 0, NULL, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 0, NULL, my_dealloc3 );
+
+// Try with no padding
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, my_construct, my_destruct );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, my_construct, my_destruct, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, my_construct, my_destruct, my_alloc2, my_dealloc3 );
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 0, my_destruct );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 0, my_destruct, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 0, my_destruct, my_dealloc3 );
+
+// Padding and no con/destructors
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, NULL, NULL );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, NULL, NULL, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, NULL, NULL, my_alloc2, my_dealloc3 );
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 8, NULL );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 8, NULL, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 8, NULL, my_dealloc3 );
+
+// Padding with con/destructors
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, my_construct, my_destruct );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, my_construct, my_destruct, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, my_construct, my_destruct, my_alloc2, my_dealloc3 );
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 8, my_destruct );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 8, my_destruct, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 8, my_destruct, my_dealloc3 );
+
+ return 0;
+ }
+
+// Make sure the constructors and destructors are matched
+int test_counted ( ) {
+ int retVal = 0;
+ void *one, *two, *three;
+
+// Try with no padding
+ gCounter = 0;
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, count_construct, count_destruct );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, count_construct, count_destruct, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, count_construct, count_destruct, my_alloc2, my_dealloc3 );
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 0, count_destruct );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 0, count_destruct, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 0, count_destruct, my_dealloc3 );
+
+// Since there was no padding, the # of elements in the array are not stored
+// and the destructors are not called.
+ if ( gCounter != 30 ) {
+ std::cerr << "Mismatched Constructor/Destructor calls (1)" << std::endl;
+ std::cerr << " Expected 30, got " << gCounter << std::endl;
+ retVal = 1;
+ }
+
+ gCounter = 0;
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, count_construct, count_destruct );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, count_construct, count_destruct, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, count_construct, count_destruct, my_alloc2, my_dealloc3 );
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 8, count_destruct );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 8, count_destruct, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 8, count_destruct, my_dealloc3 );
+
+ if ( gCounter != 0 ) {
+ std::cerr << "Mismatched Constructor/Destructor calls (2)" << std::endl;
+ std::cerr << " Expected 0, got " << gCounter << std::endl;
+ retVal = 1;
+ }
+
+ return retVal;
+ }
+
+// Make sure the constructors and destructors are matched
+int test_exception_in_constructor ( ) {
+ int retVal = 0;
+ void *one, *two, *three;
+
+// Try with no padding
+ gConstructorCounter = gDestructorCounter = 0;
+ gConstructorThrowTarget = 15;
+ gDestructorThrowTarget = -1;
+ try {
+ one = two = three = NULL;
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, throw_construct, throw_destruct );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, throw_construct, throw_destruct, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, throw_construct, throw_destruct, my_alloc2, my_dealloc3 );
+ }
+ catch ( int i ) {}
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 0, throw_destruct );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 0, throw_destruct, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 0, throw_destruct, my_dealloc3 );
+
+// Since there was no padding, the # of elements in the array are not stored
+// and the destructors are not called.
+// Since we threw after 15 calls to the constructor, we should see 5 calls to
+// the destructor from the partially constructed array.
+ if ( gConstructorCounter - gDestructorCounter != 10 ) {
+ std::cerr << "Mismatched Constructor/Destructor calls (1C)" << std::endl;
+ std::cerr << gConstructorCounter << " constructors, but " <<
+ gDestructorCounter << " destructors" << std::endl;
+ retVal = 1;
+ }
+
+ gConstructorCounter = gDestructorCounter = 0;
+ gConstructorThrowTarget = 15;
+ gDestructorThrowTarget = -1;
+ try {
+ one = two = three = NULL;
+ one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct );
+ two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc2 );
+ three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc3 );
+ }
+ catch ( int i ) {}
+
+ __cxxabiv1::__cxa_vec_delete ( one, 40, 8, throw_destruct );
+ __cxxabiv1::__cxa_vec_delete2( two, 40, 8, throw_destruct, my_dealloc2 );
+ __cxxabiv1::__cxa_vec_delete3( three, 40, 8, throw_destruct, my_dealloc3 );
+
+ if ( gConstructorCounter != gDestructorCounter ) {
+ std::cerr << "Mismatched Constructor/Destructor calls (2C)" << std::endl;
+ std::cerr << gConstructorCounter << " constructors, but " <<
+ gDestructorCounter << " destructors" << std::endl;
+ retVal = 1;
+ }
+
+ return retVal;
+ }
+
+void my_terminate () { exit ( 0 ); }
+
+int main ( int argc, char *argv [] ) {
+ int retVal = 0;
+ retVal += test_empty ();
+ retVal += test_counted ();
+ retVal += test_exception_in_constructor ();
+ return retVal;
+ }
OpenPOWER on IntegriCloud