stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ant...@apache.org
Subject svn commit: r423070 - /incubator/stdcxx/trunk/tests/self/0.alloc.cpp
Date Tue, 18 Jul 2006 12:26:10 GMT
Author: antonp
Date: Tue Jul 18 05:26:09 2006
New Revision: 423070

URL: http://svn.apache.org/viewvc?rev=423070&view=rev
Log:
2006-07-18  Anton Pevtsov  <antonp@moscow.vdiweb.com>

	STDCXX-3
	* 0.alloc.cpp: New test exercising the rw_alloc() and 
	rw_free() functions

Added:
    incubator/stdcxx/trunk/tests/self/0.alloc.cpp   (with props)

Added: incubator/stdcxx/trunk/tests/self/0.alloc.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/self/0.alloc.cpp?rev=423070&view=auto
==============================================================================
--- incubator/stdcxx/trunk/tests/self/0.alloc.cpp (added)
+++ incubator/stdcxx/trunk/tests/self/0.alloc.cpp Tue Jul 18 05:26:09 2006
@@ -0,0 +1,178 @@
+/************************************************************************
+ *
+ * 0.alloc.cpp - test exercising the rw_alloc() and rw_free() functions
+ *
+ * $Id$
+ *
+ ************************************************************************
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ **************************************************************************/
+
+#include <signal.h>      // for signal
+#include <setjmp.h>      // for setjmp, longjmp
+
+#include <rw_alloc.h>    // for rw_alloc, rw_free
+#include <rw_printf.h>   // for rw_printf
+#include <driver.h>
+
+#ifdef __CYGWIN__
+// use the Windows API on Cygwin
+#  define _WIN32
+#endif
+
+static jmp_buf mark;
+
+static void sig_handler (int)
+{
+    longjmp (mark, -1);
+}
+
+#ifdef sigsetjmp
+#define SETJMP(env) sigsetjmp (env, 1)
+#else
+#define SETJMP(env) setjmp (env)
+#endif
+
+#define BEGIN_TEST_OVERRUN()        \
+    (signal (SIGSEGV, sig_handler), \
+    (0 == SETJMP (mark)))
+
+struct TestCase
+{
+    size_t       size_;
+    int          index_;
+    int          prot_;
+    const char*  file_;
+    int          line_;
+};
+
+void test (const TestCase& tcase)
+{
+    if (void* buf = rw_alloc (tcase.size_, tcase.prot_)) {
+
+        char* str = _RWSTD_STATIC_CAST (char*, buf);
+
+        bool protbelow = 0 != (tcase.prot_ & RW_PROT_BELOW);
+
+        bool isguardpage = (protbelow && 0 > tcase.index_)
+            || (!protbelow && tcase.index_ >= int (tcase.size_));
+
+        bool canread = (tcase.prot_ & RW_PROT_READ) && !isguardpage;
+
+        bool canwrite = (tcase.prot_ & RW_PROT_WRITE) && !isguardpage;
+
+        char c = 'a';
+
+        // check read access only if:
+        // tcase.prot_ & RW_PROT_READ != 0
+        // tcase.prot_ == RW_PROT_NONE
+        // tcase.prot_ & RW_PROT_WRITE != 0 and the
+        // accessed memory is placed in the guard page
+        if (tcase.prot_ & RW_PROT_READ || !canwrite) {
+            if (BEGIN_TEST_OVERRUN ()) {
+                c = str[tcase.index_];
+                rw_assert (canread, tcase.file_, tcase.line_,
+                    "expected no read access, got read access");
+            } else
+                rw_assert (!canread, tcase.file_, tcase.line_,
+                "expected read access, got no read access");
+        }
+
+        if (BEGIN_TEST_OVERRUN ()) {
+            str[tcase.index_] = c;
+            rw_assert (canwrite, tcase.file_, tcase.line_,
+                       "expected no write access, got write access");
+        } else
+            rw_assert (!canwrite, tcase.file_, tcase.line_,
+                       "expected write access, got no write access");
+
+        rw_free (buf);
+    }
+}
+
+static int
+run_test (int, char**)
+{
+    const size_t BUF_SIZE = 10;
+
+    static const TestCase tcases[] = {
+
+#ifdef TEST
+#undef TEST
+#endif
+#define TEST(size, index, prot) \
+    { size, index, prot, __FILE__, __LINE__ }
+
+        TEST(BUF_SIZE, -1, RW_PROT_NONE),
+        TEST(BUF_SIZE, -1, RW_PROT_READ),
+        TEST(BUF_SIZE, -1, RW_PROT_WRITE),
+        TEST(BUF_SIZE, -1, RW_PROT_RDWR),
+
+        TEST(BUF_SIZE, -1, RW_PROT_NONE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, -1, RW_PROT_READ | RW_PROT_BELOW),
+        TEST(BUF_SIZE, -1, RW_PROT_WRITE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, -1, RW_PROT_RDWR | RW_PROT_BELOW),
+
+        TEST(BUF_SIZE, 0, RW_PROT_NONE),
+        TEST(BUF_SIZE, 0, RW_PROT_READ),
+        TEST(BUF_SIZE, 0, RW_PROT_WRITE),
+        TEST(BUF_SIZE, 0, RW_PROT_RDWR),
+
+        TEST(BUF_SIZE, 0, RW_PROT_NONE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, 0, RW_PROT_READ | RW_PROT_BELOW),
+        TEST(BUF_SIZE, 0, RW_PROT_WRITE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, 0, RW_PROT_RDWR | RW_PROT_BELOW),
+
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_NONE),
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_READ),
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_WRITE),
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_RDWR),
+
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_NONE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_READ | RW_PROT_BELOW),
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_WRITE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, BUF_SIZE - 1, RW_PROT_RDWR | RW_PROT_BELOW),
+
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_NONE),
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_READ),
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_WRITE),
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_RDWR),
+
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_NONE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_READ | RW_PROT_BELOW),
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_WRITE | RW_PROT_BELOW),
+        TEST(BUF_SIZE, BUF_SIZE, RW_PROT_RDWR | RW_PROT_BELOW)
+    };
+
+    for (size_t i = 0; i < sizeof (tcases) / sizeof (tcases[0]); ++i)
+        test (tcases [i]);
+
+    return 0;
+}
+
+/***********************************************************************/
+
+int main (int argc, char *argv[])
+{
+    return rw_test (argc, argv, __FILE__,
+        "",
+        0,
+        run_test,
+        "",
+        (void*)0 /* sentinel */);
+}

Propchange: incubator/stdcxx/trunk/tests/self/0.alloc.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/stdcxx/trunk/tests/self/0.alloc.cpp
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message