harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gshiman...@apache.org
Subject svn commit: r473089 - in /incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist: ./ Makefile ulist_test.cpp unit_test.h unit_test_logger.h unit_test_main.h
Date Thu, 09 Nov 2006 22:07:50 GMT
Author: gshimansky
Date: Thu Nov  9 14:07:49 2006
New Revision: 473089

URL: http://svn.apache.org/viewvc?view=rev&rev=473089
Log:
Applied HARMONY-2131 [drlvm] C++ unit test infrastructure

Since patch contains only unit tests I just made sure that they just pass on windows and linux


Added:
    incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/   (with props)
    incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/Makefile
    incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/ulist_test.cpp
    incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test.h
    incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_logger.h
    incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_main.h

Propchange: incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Nov  9 14:07:49 2006
@@ -0,0 +1,3 @@
+ulist_test.exe
+ulist_test.obj
+

Added: incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/Makefile
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/Makefile?view=auto&rev=473089
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/Makefile (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/Makefile Thu Nov  9 14:07:49
2006
@@ -0,0 +1,41 @@
+# Licensed to the Apache Software Foundation (ASF)
+# see the full text at the end of file
+
+SOURCES = $(wildcard *test.cpp test*.cpp)
+PROGRAMS = $(SOURCES:%.cpp=%.exe)
+INCLUDES = ../../../vmcore/src/jvmti .
+
+all: $(PROGRAMS)
+	set -e; for i in $(PROGRAMS); do ./$$i -Xwarn; done
+
+clean:
+	$(RM) $(PROGRAMS)
+
+CPPFLAGS = $(addprefix -I,$(INCLUDES))
+
+OS = $(shell uname -s)
+ifeq ($(OS),Linux)
+%.exe: %.cpp
+	$(CXX) $(CXXFLAGS) -o $@ $< $(CPPFLAGS)
+else
+CXX = cl
+%.exe: %.cpp
+	$(CXX) $(CXXFLAGS) -EHsc $< $(CPPFLAGS)
+endif
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#

Added: incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/ulist_test.cpp
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/ulist_test.cpp?view=auto&rev=473089
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/ulist_test.cpp (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/ulist_test.cpp Thu Nov  9 14:07:49
2006
@@ -0,0 +1,25 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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 "unit_test_main.h"
+
+#define UNIT_TEST
+#include <ulist.h>
+
+int main (int argc, char** argv) {
+    run(argc, argv);
+}

Added: incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test.h
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test.h?view=auto&rev=473089
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test.h (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test.h Thu Nov  9 14:07:49
2006
@@ -0,0 +1,250 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ */
+
+#ifndef UNIT_TEST_H
+#define UNIT_TEST_H
+
+/** \file
+ * Unit test framework.
+ */
+
+#include <iostream>
+#include <list>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <windows.h>
+#endif
+
+#ifdef __linux__
+#include <unistd.h>
+#include <sys/wait.h>
+#endif // __linux__
+
+#ifndef LOG_DOMAIN
+#define LOG_DOMAIN "test"
+#endif
+
+#include "unit_test_logger.h"
+
+#ifdef _WIN32
+/// Translates exception code into string message
+inline char* get_exception_message(int code) {
+    if (EXCEPTION_ACCESS_VIOLATION == code) {
+        return "segmentation fault";
+    } else {
+        return "exception caught";
+    }
+}
+#endif // _WIN32
+
+#ifdef __linux__
+void segfault(int signum) {
+    switch (signum) {
+        case SIGSEGV: WARN("segmentation fault"); break;
+        default: WARN("fatal error");
+    }
+    exit(1);
+}
+#endif // __linux__
+
+/// Container of a description of a test case, setup or teardown.
+class Descriptor {
+    protected:
+        void (*func)();
+        char* name;
+        char* file;
+        int line;
+    public:
+        Descriptor(void (*func)(), char* name, char* file, int line)
+            : func(func), name(name), file(file), line(line) {}
+        friend std::ostream& operator<<(std::ostream& out, Descriptor* descriptor);
+};
+
+inline std::ostream& operator<<(std::ostream& out, Descriptor* descriptor)
{
+    out << descriptor->name << "() at "
+        << descriptor->file << ":" << descriptor->line;
+    return out;
+}
+
+class SetupDescriptor;
+
+/// A setup function
+extern SetupDescriptor* setup_descriptor;
+
+/// Container of a setup function description.
+class SetupDescriptor : public Descriptor {
+    public:
+        SetupDescriptor(void (*func)(), char* name, char* file, int line)
+            : Descriptor(func, name, file, line)
+        {
+            if (NULL != setup_descriptor) {
+                WARN2("test", "Ignoring duplicate SETUP("
+                        << (setup_descriptor->name) << ") function at "
+                        << (setup_descriptor->file) << ":"
+                        << (setup_descriptor->line));
+            }
+            setup_descriptor = this;
+        }
+
+        int setup() {
+#ifdef _WIN32
+            __try {
+#endif // _WIN32
+                TRACE2("test","setting up by " << this);
+                func();
+                TRACE2("test",this << " setup complete");
+#ifdef _WIN32
+            } __except(EXCEPTION_EXECUTE_HANDLER) {
+                WARN2("test",std::endl << get_exception_message(GetExceptionCode())
<< " in " << this);
+                return 0;
+            }
+#endif // _WIN32
+            return 1;
+        }
+};
+
+class TeardownDescriptor;
+
+/// A global teardown descriptor
+extern TeardownDescriptor* teardown_descriptor;
+
+/// Container of teardown function description.
+class TeardownDescriptor : public Descriptor {
+    public:
+        TeardownDescriptor(void (*func)(), char* name, char* file, int line)
+            : Descriptor(func, name, file, line)
+        {
+            if (NULL != teardown_descriptor) {
+                WARN2("test","Ignoring duplicate TEARDOWN("
+                        << (teardown_descriptor->name) << ") function at "
+                        << (teardown_descriptor->file) << ":"
+                        << (teardown_descriptor->line));
+            }
+            teardown_descriptor = this;
+        }
+
+        int teardown() {
+#ifdef _WIN32
+            __try {
+#endif // _WIN32
+                TRACE2("test","tearing down by " << this);
+                func();
+                TRACE2("test",this << " teardown complete");
+#ifdef _WIN32
+            } __except(EXCEPTION_EXECUTE_HANDLER) {
+                WARN2("test",std::endl << get_exception_message(GetExceptionCode())
<< " in " << this);
+                return 0;
+            }
+#endif // _WIN32
+            return 1;
+        }
+};
+
+class TestCaseDescriptor;
+
+/// A global list of all test cases.
+extern std::list<TestCaseDescriptor*> test_cases;
+
+/// Container of test case description.
+class TestCaseDescriptor : public Descriptor {
+    public:
+        TestCaseDescriptor(void (*func)(), char* name, char* file, int line)
+            : Descriptor(func, name, file, line)
+        {
+            test_cases.push_back(this);
+        }
+
+        int run() {
+            status = 0;
+            INFO(" ... " << name);
+#ifdef _WIN32
+            __try {
+#endif // _WIN32
+#ifdef __linux__
+            if (fork() == 0) {
+                signal(SIGSEGV, segfault);
+#endif // __linux__
+                if (NULL != setup_descriptor) {
+                    // run the setup and check its result
+                    if (!setup_descriptor->setup()) {
+                        fail("Setup function " << setup_descriptor << " failed");
+                        return status;
+                    }
+                }
+                // run the test case
+                func();
+                if (NULL != teardown_descriptor) {
+                    // run the teardown
+                    if (!teardown_descriptor->teardown()) {
+                        // print a warning, but do not change test case result
+                        WARN2("test","Teardown function " << teardown_descriptor <<
" failed");
+                    }
+                }
+#ifdef _WIN32
+            } __except(EXCEPTION_EXECUTE_HANDLER) {
+                ++status;
+                WARN2("test", std::endl << get_exception_message(GetExceptionCode())
+                        << " in " << name << "() at " << file <<
":" << line);
+            }
+#endif // _WIN32
+#ifdef __linux__
+                exit(status);
+            }
+            // we are the parent process
+            int error;
+            wait(&error);
+            if (error != 0) {
+                ++status;
+            }
+#endif // __linux__
+            if (0 == status) {
+                TICK(".");
+                INFO(name << " passed");
+            } else {
+                TICK("x");
+                INFO(this << " failed");
+            }
+            return status;
+        }
+};
+
+/// Defines the test case function and adds it to the global test case list.
+#define TEST(x) \
+    static void x (); \
+    TestCaseDescriptor test_case_descriptor_ ## x (&x, #x, __FILE__, __LINE__); \
+    static void x ()
+
+/// Defines the excluded test case function (not put to the global test case list).
+#define XTEST(x) \
+    static void x ()
+
+/// Defines the setup function
+#define SETUP(x) \
+    static void x (); \
+    SetupDescriptor setup_descriptor_ ## x (&x, #x, __FILE__, __LINE__); \
+    static void x ()
+
+/// Defines the teardown function
+#define TEARDOWN(x) \
+    static void x (); \
+    TeardownDescriptor teardown_descriptor_ ## x (&x, #x, __FILE__, __LINE__); \
+    static void x ()
+
+#endif // UNIT_TEST_H

Added: incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_logger.h
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_logger.h?view=auto&rev=473089
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_logger.h (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_logger.h Thu Nov
 9 14:07:49 2006
@@ -0,0 +1,63 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ */
+
+#ifndef UNIT_TEST_LOGGER_H
+#define UNIT_TEST_LOGGER_H
+
+#include <iostream>
+
+#define EXPAND_(a) #a
+#define EXPAND(a) EXPAND_(a)
+#define __FILELINE__ __FILE__ ":" EXPAND(__LINE__)
+
+#define TICK(x) do { if (log_level <= 1) std::cerr << x << std::flush; } while(0)
+
+/**
+ * holds current unit test status value.
+ * 0 is okay, non-zero value means failure.
+ */
+extern int status;
+
+#undef fail
+/// Fails a unit test and prints a message
+#define fail(x) \
+    ++status; \
+    WARN2("test",std::endl << x); \
+    throw "failed";
+
+#undef assert
+/// Makes an assertion
+#define assert(x) \
+    if (!(x)) { \
+        fail("assertion " #x " failed at " __FILELINE__); \
+    }
+
+extern int log_level;
+
+#define ECHO2(x,y) std::cerr << x << ": " << y << std::endl
+#define TRACE2(x,y) do { if(log_level > 3) ECHO2(x,y); } while(0)
+#define LOG2(x,y) do { if (log_level > 2) ECHO2(x,y); } while(0)
+#define INFO2(x,y) do { if (log_level > 1) ECHO2(x,y); } while(0)
+#define WARN2(x,y) do { if (log_level > 0) ECHO2(x,y); } while(0)
+
+#define ECHO(x) std::cerr << x << std::endl
+#define TRACE(x) do { if (log_level > 3) ECHO(x); } while(0)
+#define LOG(x) do { if (log_level > 2) ECHO(x); } while(0)
+#define INFO(x) do { if (log_level > 1) ECHO(x); } while(0)
+#define WARN(x) do { if (log_level > 0) ECHO(x); } while(0)
+
+#endif // UNIT_TEST_LOGGER_H

Added: incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_main.h
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_main.h?view=auto&rev=473089
==============================================================================
--- incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_main.h (added)
+++ incubator/harmony/enhanced/drlvm/trunk/vm/tests/unit/ulist/unit_test_main.h Thu Nov  9
14:07:49 2006
@@ -0,0 +1,91 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ */
+
+#ifndef UNIT_TEST_MAIN_H
+#define UNIT_TEST_MAIN_H
+
+/** \file
+ * Unit test framework: main function.
+ */
+
+#include "unit_test.h"
+
+/// Log level
+int log_level = 2;
+
+/// Current test case status.
+int status = 0;
+
+/// Global list of all test cases.
+std::list<TestCaseDescriptor*> test_cases;
+
+/// Global setup descriptor. None by default.
+SetupDescriptor* setup_descriptor = NULL;
+
+/// Global teardown descriptor. None by default.
+TeardownDescriptor* teardown_descriptor = NULL;
+
+static inline bool begins_with(const char* str, const char* beginning)
+{
+    return strncmp(str, beginning, strlen(beginning)) == 0;
+}
+
+static void set_log_levels_from_cmd(int argc, char* argv[])
+{
+    int arg_num;
+    for (arg_num = 1; arg_num < argc; arg_num++) {
+        char *option = argv[arg_num];
+
+        if (begins_with(option, "-Xwarn")) {
+            log_level = 1;
+        } else if (begins_with(option, "-Xinfo")) {
+            log_level = 2;
+        } else if (begins_with(option, "-Xlog")) {
+            log_level = 3;
+        } else if (begins_with(option, "-Xtrace")) {
+            log_level = 4;
+        }
+    }
+}
+
+/// Prints the summary of the unit test
+inline void summary(int failed) {
+    if (0 == failed) {
+        std::cerr << std::endl << "OK" << std::endl << std::flush;
+    } else {
+        std::cerr << std::endl << "FAILED, " << failed
+            << (failed > 1 ? " failures" : " failure") << std::endl <<
std::flush;
+    }
+}
+
+/// Configures and runs unit tests.
+inline void run(int argc, char** argv) {
+    set_log_levels_from_cmd(argc, argv);
+    std::list<TestCaseDescriptor*>::iterator i;
+    int failed = 0;
+    for (i = test_cases.begin(); i != test_cases.end(); i++) {
+        TestCaseDescriptor* test_case = *i;
+        int status = test_case->run();
+        if (0 != status) {
+            ++failed;
+        }
+    }
+    summary(failed);
+    exit(failed);
+}
+
+#endif // UNIT_TEST_MAIN_H



Mime
View raw message